blob: 5b96ca2c5c8c014f3d571ca811a0c0c492a45716 [file] [log] [blame]
Paul Bakker89e80c92012-03-20 13:50:09 +00001/**
2 * \file gcm.h
3 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +00004 * \brief Galois/Counter Mode (GCM) for 128-bit block ciphers, as defined
5 * in <em>D. McGrew, J. Viega, The Galois/Counter Mode of Operation
6 * (GCM), Natl. Inst. Stand. Technol.</em>
7 *
8 * For more information on GCM, see <em>NIST SP 800-38D: Recommendation for
9 * Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC</em>.
10 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Rose Zadik17b4f7f2018-01-26 10:56:42 +000013 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020014 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
15 *
16 * This file is provided under the Apache License 2.0, or the
17 * GNU General Public License v2.0 or later.
18 *
19 * **********
20 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020021 *
22 * Licensed under the Apache License, Version 2.0 (the "License"); you may
23 * not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
Paul Bakker89e80c92012-03-20 13:50:09 +000033 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020034 * **********
35 *
36 * **********
37 * GNU General Public License v2.0 or later:
38 *
39 * This program is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
43 *
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
48 *
49 * You should have received a copy of the GNU General Public License along
50 * with this program; if not, write to the Free Software Foundation, Inc.,
51 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
52 *
53 * **********
54 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +000055 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker89e80c92012-03-20 13:50:09 +000056 */
Rose Zadik17b4f7f2018-01-26 10:56:42 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#ifndef MBEDTLS_GCM_H
59#define MBEDTLS_GCM_H
Paul Bakker89e80c92012-03-20 13:50:09 +000060
Ron Eldor0559c662018-02-14 16:02:41 +020061#if !defined(MBEDTLS_CONFIG_FILE)
62#include "config.h"
63#else
64#include MBEDTLS_CONFIG_FILE
65#endif
66
Paul Bakker43aff2a2013-09-09 00:10:27 +020067#include "cipher.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000068
69#include <stdint.h>
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#define MBEDTLS_GCM_ENCRYPT 1
72#define MBEDTLS_GCM_DECRYPT 0
Paul Bakker89e80c92012-03-20 13:50:09 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010075#define MBEDTLS_ERR_GCM_HW_ACCEL_FAILED -0x0013 /**< GCM hardware accelerator failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
Paul Bakker89e80c92012-03-20 13:50:09 +000077
Jaeden Amero15263302017-09-21 12:53:48 +010078#if !defined(MBEDTLS_GCM_ALT)
79
Paul Bakker407a0da2013-06-27 14:29:21 +020080#ifdef __cplusplus
81extern "C" {
82#endif
83
Paul Bakker89e80c92012-03-20 13:50:09 +000084/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +000085 * \brief The GCM context structure.
Paul Bakker89e80c92012-03-20 13:50:09 +000086 */
87typedef struct {
Rose Zadik17b4f7f2018-01-26 10:56:42 +000088 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
89 uint64_t HL[16]; /*!< Precalculated HTable low. */
90 uint64_t HH[16]; /*!< Precalculated HTable high. */
91 uint64_t len; /*!< The total length of the encrypted data. */
92 uint64_t add_len; /*!< The total length of the additional data. */
93 unsigned char base_ectr[16]; /*!< The first ECTR for tag. */
94 unsigned char y[16]; /*!< The Y working value. */
95 unsigned char buf[16]; /*!< The buf working value. */
96 int mode; /*!< The operation to perform:
97 #MBEDTLS_GCM_ENCRYPT or
98 #MBEDTLS_GCM_DECRYPT. */
Paul Bakker89e80c92012-03-20 13:50:09 +000099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100mbedtls_gcm_context;
Paul Bakker89e80c92012-03-20 13:50:09 +0000101
Paul Bakker89e80c92012-03-20 13:50:09 +0000102/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000103 * \brief This function initializes the specified GCM context,
104 * to make references valid, and prepares the context
105 * for mbedtls_gcm_setkey() or mbedtls_gcm_free().
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200106 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000107 * The function does not bind the GCM context to a particular
108 * cipher, nor set the key. For this purpose, use
109 * mbedtls_gcm_setkey().
110 *
111 * \param ctx The GCM context to initialize.
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200112 */
113void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
114
115/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000116 * \brief This function associates a GCM context with a
117 * cipher algorithm and a key.
Paul Bakker89e80c92012-03-20 13:50:09 +0000118 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000119 * \param ctx The GCM context to initialize.
120 * \param cipher The 128-bit block cipher to use.
121 * \param key The encryption key.
122 * \param keybits The key size in bits. Valid options are:
123 * <ul><li>128 bits</li>
124 * <li>192 bits</li>
125 * <li>256 bits</li></ul>
Paul Bakker89e80c92012-03-20 13:50:09 +0000126 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000127 * \return \c 0 on success, or a cipher specific error code.
Paul Bakker89e80c92012-03-20 13:50:09 +0000128 */
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200129int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
130 mbedtls_cipher_id_t cipher,
131 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200132 unsigned int keybits );
Paul Bakker89e80c92012-03-20 13:50:09 +0000133
134/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000135 * \brief This function performs GCM encryption or decryption of a buffer.
Paul Bakker89e80c92012-03-20 13:50:09 +0000136 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000137 * \note For encryption, the output buffer can be the same as the input buffer.
138 * For decryption, the output buffer cannot be the same as input buffer.
139 * If the buffers overlap, the output buffer must trail at least 8 Bytes
Paul Bakkerca4ab492012-04-18 14:23:57 +0000140 * behind the input buffer.
141 *
Gilles Peskine282bd242018-06-01 17:55:41 +0200142 * \warning When this function performs a decryption, it outputs the
143 * authentication tag and does not verify that the data is
144 * authentic. You should use this function to perform encryption
145 * only. For decryption, use mbedtls_gcm_auth_decrypt() instead.
146 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000147 * \param ctx The GCM context to use for encryption or decryption.
Gilles Peskinedb37cb42018-06-07 14:46:02 +0200148 * \param mode The operation to perform:
149 * - #MBEDTLS_GCM_ENCRYPT to perform authenticated encryption.
150 * The ciphertext is written to \p output and the
151 * authentication tag is written to \p tag.
152 * - #MBEDTLS_GCM_DECRYPT to perform decryption.
153 * The plaintext is written to \p output and the
154 * authentication tag is written to \p tag.
155 * Note that this mode is not recommended, because it does
156 * not verify the authenticity of the data. For this reason,
157 * you should use mbedtls_gcm_auth_decrypt() instead of
158 * calling this function in decryption mode.
Gilles Peskine282bd242018-06-01 17:55:41 +0200159 * \param length The length of the input data, which is equal to the length
160 * of the output data.
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000161 * \param iv The initialization vector.
162 * \param iv_len The length of the IV.
163 * \param add The buffer holding the additional data.
164 * \param add_len The length of the additional data.
Gilles Peskine282bd242018-06-01 17:55:41 +0200165 * \param input The buffer holding the input data. Its size is \b length.
166 * \param output The buffer for holding the output data. It must have room
167 * for \b length bytes.
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000168 * \param tag_len The length of the tag to generate.
169 * \param tag The buffer for holding the tag.
Paul Bakker89e80c92012-03-20 13:50:09 +0000170 *
Gilles Peskine282bd242018-06-01 17:55:41 +0200171 * \return \c 0 if the encryption or decryption was performed
172 * successfully. Note that in #MBEDTLS_GCM_DECRYPT mode,
173 * this does not indicate that the data is authentic.
174 * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid.
175 * \return #MBEDTLS_ERR_GCM_HW_ACCEL_FAILED or a cipher-specific
176 * error code if the encryption or decryption failed.
Paul Bakker89e80c92012-03-20 13:50:09 +0000177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000179 int mode,
180 size_t length,
181 const unsigned char *iv,
182 size_t iv_len,
183 const unsigned char *add,
184 size_t add_len,
185 const unsigned char *input,
186 unsigned char *output,
187 size_t tag_len,
188 unsigned char *tag );
189
190/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000191 * \brief This function performs a GCM authenticated decryption of a
192 * buffer.
Paul Bakker89e80c92012-03-20 13:50:09 +0000193 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000194 * \note For decryption, the output buffer cannot be the same as input buffer.
195 * If the buffers overlap, the output buffer must trail at least 8 Bytes
Paul Bakkerca4ab492012-04-18 14:23:57 +0000196 * behind the input buffer.
197 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000198 * \param ctx The GCM context.
Gilles Peskine282bd242018-06-01 17:55:41 +0200199 * \param length The length of the ciphertext to decrypt, which is also
200 * the length of the decrypted plaintext.
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000201 * \param iv The initialization vector.
202 * \param iv_len The length of the IV.
203 * \param add The buffer holding the additional data.
204 * \param add_len The length of the additional data.
Gilles Peskine282bd242018-06-01 17:55:41 +0200205 * \param tag The buffer holding the tag to verify.
206 * \param tag_len The length of the tag to verify.
207 * \param input The buffer holding the ciphertext. Its size is \b length.
208 * \param output The buffer for holding the decrypted plaintext. It must
209 * have room for \b length bytes.
Paul Bakker89e80c92012-03-20 13:50:09 +0000210 *
Gilles Peskine282bd242018-06-01 17:55:41 +0200211 * \return \c 0 if successful and authenticated.
212 * \return #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match.
213 * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid.
214 * \return #MBEDTLS_ERR_GCM_HW_ACCEL_FAILED or a cipher-specific
215 * error code if the decryption failed.
Paul Bakker89e80c92012-03-20 13:50:09 +0000216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000218 size_t length,
219 const unsigned char *iv,
220 size_t iv_len,
221 const unsigned char *add,
222 size_t add_len,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200223 const unsigned char *tag,
Paul Bakker89e80c92012-03-20 13:50:09 +0000224 size_t tag_len,
225 const unsigned char *input,
226 unsigned char *output );
227
228/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000229 * \brief This function starts a GCM encryption or decryption
230 * operation.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200231 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000232 * \param ctx The GCM context.
233 * \param mode The operation to perform: #MBEDTLS_GCM_ENCRYPT or
234 * #MBEDTLS_GCM_DECRYPT.
235 * \param iv The initialization vector.
236 * \param iv_len The length of the IV.
237 * \param add The buffer holding the additional data, or NULL if \p add_len is 0.
238 * \param add_len The length of the additional data. If 0, \p add is NULL.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200239 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000240 * \return \c 0 on success.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200243 int mode,
244 const unsigned char *iv,
245 size_t iv_len,
246 const unsigned char *add,
247 size_t add_len );
248
249/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000250 * \brief This function feeds an input buffer into an ongoing GCM
251 * encryption or decryption operation.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200252 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000253 * ` The function expects input to be a multiple of 16
254 * Bytes. Only the last call before calling
255 * mbedtls_gcm_finish() can be less than 16 Bytes.
256 *
257 * \note For decryption, the output buffer cannot be the same as input buffer.
258 * If the buffers overlap, the output buffer must trail at least 8 Bytes
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200259 * behind the input buffer.
260 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000261 * \param ctx The GCM context.
262 * \param length The length of the input data. This must be a multiple of 16 except in the last call before mbedtls_gcm_finish().
263 * \param input The buffer holding the input data.
264 * \param output The buffer for holding the output data.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200265 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000266 * \return \c 0 on success, or #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200269 size_t length,
270 const unsigned char *input,
271 unsigned char *output );
272
273/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000274 * \brief This function finishes the GCM operation and generates
275 * the authentication tag.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200276 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000277 * It wraps up the GCM stream, and generates the
278 * tag. The tag can have a maximum length of 16 Bytes.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200279 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000280 * \param ctx The GCM context.
281 * \param tag The buffer for holding the tag.
282 * \param tag_len The length of the tag to generate. Must be at least four.
283 *
284 * \return \c 0 on success, or #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200287 unsigned char *tag,
288 size_t tag_len );
289
290/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000291 * \brief This function clears a GCM context and the underlying
292 * cipher sub-context.
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200293 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000294 * \param ctx The GCM context to clear.
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200297
Jaeden Amero15263302017-09-21 12:53:48 +0100298#ifdef __cplusplus
299}
300#endif
301
302#else /* !MBEDTLS_GCM_ALT */
303#include "gcm_alt.h"
304#endif /* !MBEDTLS_GCM_ALT */
305
306#ifdef __cplusplus
307extern "C" {
308#endif
309
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200310/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000311 * \brief The GCM checkup routine.
Paul Bakker89e80c92012-03-20 13:50:09 +0000312 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000313 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker89e80c92012-03-20 13:50:09 +0000314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315int mbedtls_gcm_self_test( int verbose );
Paul Bakker89e80c92012-03-20 13:50:09 +0000316
317#ifdef __cplusplus
318}
319#endif
320
Jaeden Amero15263302017-09-21 12:53:48 +0100321
Paul Bakker89e80c92012-03-20 13:50:09 +0000322#endif /* gcm.h */