blob: 2a49a87f72da59e757e0ef62162530450bd8ed31 [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/*
Bence Szépkúti44bfbe32020-08-19 16:54:51 +020013 * Copyright The Mbed TLS Contributors
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 * **********
Paul Bakker89e80c92012-03-20 13:50:09 +000054 */
Rose Zadik17b4f7f2018-01-26 10:56:42 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#ifndef MBEDTLS_GCM_H
57#define MBEDTLS_GCM_H
Paul Bakker89e80c92012-03-20 13:50:09 +000058
Ron Eldor0559c662018-02-14 16:02:41 +020059#if !defined(MBEDTLS_CONFIG_FILE)
60#include "config.h"
61#else
62#include MBEDTLS_CONFIG_FILE
63#endif
64
Paul Bakker43aff2a2013-09-09 00:10:27 +020065#include "cipher.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000066
67#include <stdint.h>
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#define MBEDTLS_GCM_ENCRYPT 1
70#define MBEDTLS_GCM_DECRYPT 0
Paul Bakker89e80c92012-03-20 13:50:09 +000071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010073#define MBEDTLS_ERR_GCM_HW_ACCEL_FAILED -0x0013 /**< GCM hardware accelerator failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
Paul Bakker89e80c92012-03-20 13:50:09 +000075
Jaeden Amero15263302017-09-21 12:53:48 +010076#if !defined(MBEDTLS_GCM_ALT)
77
Paul Bakker407a0da2013-06-27 14:29:21 +020078#ifdef __cplusplus
79extern "C" {
80#endif
81
Paul Bakker89e80c92012-03-20 13:50:09 +000082/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +000083 * \brief The GCM context structure.
Paul Bakker89e80c92012-03-20 13:50:09 +000084 */
85typedef struct {
Rose Zadik17b4f7f2018-01-26 10:56:42 +000086 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
87 uint64_t HL[16]; /*!< Precalculated HTable low. */
88 uint64_t HH[16]; /*!< Precalculated HTable high. */
89 uint64_t len; /*!< The total length of the encrypted data. */
90 uint64_t add_len; /*!< The total length of the additional data. */
91 unsigned char base_ectr[16]; /*!< The first ECTR for tag. */
92 unsigned char y[16]; /*!< The Y working value. */
93 unsigned char buf[16]; /*!< The buf working value. */
94 int mode; /*!< The operation to perform:
95 #MBEDTLS_GCM_ENCRYPT or
96 #MBEDTLS_GCM_DECRYPT. */
Paul Bakker89e80c92012-03-20 13:50:09 +000097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098mbedtls_gcm_context;
Paul Bakker89e80c92012-03-20 13:50:09 +000099
Paul Bakker89e80c92012-03-20 13:50:09 +0000100/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000101 * \brief This function initializes the specified GCM context,
102 * to make references valid, and prepares the context
103 * for mbedtls_gcm_setkey() or mbedtls_gcm_free().
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200104 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000105 * The function does not bind the GCM context to a particular
106 * cipher, nor set the key. For this purpose, use
107 * mbedtls_gcm_setkey().
108 *
109 * \param ctx The GCM context to initialize.
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200110 */
111void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
112
113/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000114 * \brief This function associates a GCM context with a
115 * cipher algorithm and a key.
Paul Bakker89e80c92012-03-20 13:50:09 +0000116 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000117 * \param ctx The GCM context to initialize.
118 * \param cipher The 128-bit block cipher to use.
119 * \param key The encryption key.
120 * \param keybits The key size in bits. Valid options are:
121 * <ul><li>128 bits</li>
122 * <li>192 bits</li>
123 * <li>256 bits</li></ul>
Paul Bakker89e80c92012-03-20 13:50:09 +0000124 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000125 * \return \c 0 on success, or a cipher specific error code.
Paul Bakker89e80c92012-03-20 13:50:09 +0000126 */
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200127int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
128 mbedtls_cipher_id_t cipher,
129 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200130 unsigned int keybits );
Paul Bakker89e80c92012-03-20 13:50:09 +0000131
132/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000133 * \brief This function performs GCM encryption or decryption of a buffer.
Paul Bakker89e80c92012-03-20 13:50:09 +0000134 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000135 * \note For encryption, the output buffer can be the same as the input buffer.
136 * For decryption, the output buffer cannot be the same as input buffer.
137 * If the buffers overlap, the output buffer must trail at least 8 Bytes
Paul Bakkerca4ab492012-04-18 14:23:57 +0000138 * behind the input buffer.
139 *
Gilles Peskine282bd242018-06-01 17:55:41 +0200140 * \warning When this function performs a decryption, it outputs the
141 * authentication tag and does not verify that the data is
142 * authentic. You should use this function to perform encryption
143 * only. For decryption, use mbedtls_gcm_auth_decrypt() instead.
144 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000145 * \param ctx The GCM context to use for encryption or decryption.
Gilles Peskinedb37cb42018-06-07 14:46:02 +0200146 * \param mode The operation to perform:
147 * - #MBEDTLS_GCM_ENCRYPT to perform authenticated encryption.
148 * The ciphertext is written to \p output and the
149 * authentication tag is written to \p tag.
150 * - #MBEDTLS_GCM_DECRYPT to perform decryption.
151 * The plaintext is written to \p output and the
152 * authentication tag is written to \p tag.
153 * Note that this mode is not recommended, because it does
154 * not verify the authenticity of the data. For this reason,
155 * you should use mbedtls_gcm_auth_decrypt() instead of
156 * calling this function in decryption mode.
Gilles Peskine282bd242018-06-01 17:55:41 +0200157 * \param length The length of the input data, which is equal to the length
158 * of the output data.
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000159 * \param iv The initialization vector.
160 * \param iv_len The length of the IV.
161 * \param add The buffer holding the additional data.
162 * \param add_len The length of the additional data.
Gilles Peskine282bd242018-06-01 17:55:41 +0200163 * \param input The buffer holding the input data. Its size is \b length.
164 * \param output The buffer for holding the output data. It must have room
165 * for \b length bytes.
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000166 * \param tag_len The length of the tag to generate.
167 * \param tag The buffer for holding the tag.
Paul Bakker89e80c92012-03-20 13:50:09 +0000168 *
Gilles Peskine282bd242018-06-01 17:55:41 +0200169 * \return \c 0 if the encryption or decryption was performed
170 * successfully. Note that in #MBEDTLS_GCM_DECRYPT mode,
171 * this does not indicate that the data is authentic.
172 * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid.
173 * \return #MBEDTLS_ERR_GCM_HW_ACCEL_FAILED or a cipher-specific
174 * error code if the encryption or decryption failed.
Paul Bakker89e80c92012-03-20 13:50:09 +0000175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000177 int mode,
178 size_t length,
179 const unsigned char *iv,
180 size_t iv_len,
181 const unsigned char *add,
182 size_t add_len,
183 const unsigned char *input,
184 unsigned char *output,
185 size_t tag_len,
186 unsigned char *tag );
187
188/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000189 * \brief This function performs a GCM authenticated decryption of a
190 * buffer.
Paul Bakker89e80c92012-03-20 13:50:09 +0000191 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000192 * \note For decryption, the output buffer cannot be the same as input buffer.
193 * If the buffers overlap, the output buffer must trail at least 8 Bytes
Paul Bakkerca4ab492012-04-18 14:23:57 +0000194 * behind the input buffer.
195 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000196 * \param ctx The GCM context.
Gilles Peskine282bd242018-06-01 17:55:41 +0200197 * \param length The length of the ciphertext to decrypt, which is also
198 * the length of the decrypted plaintext.
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000199 * \param iv The initialization vector.
200 * \param iv_len The length of the IV.
201 * \param add The buffer holding the additional data.
202 * \param add_len The length of the additional data.
Gilles Peskine282bd242018-06-01 17:55:41 +0200203 * \param tag The buffer holding the tag to verify.
204 * \param tag_len The length of the tag to verify.
205 * \param input The buffer holding the ciphertext. Its size is \b length.
206 * \param output The buffer for holding the decrypted plaintext. It must
207 * have room for \b length bytes.
Paul Bakker89e80c92012-03-20 13:50:09 +0000208 *
Gilles Peskine282bd242018-06-01 17:55:41 +0200209 * \return \c 0 if successful and authenticated.
210 * \return #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match.
211 * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid.
212 * \return #MBEDTLS_ERR_GCM_HW_ACCEL_FAILED or a cipher-specific
213 * error code if the decryption failed.
Paul Bakker89e80c92012-03-20 13:50:09 +0000214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
Paul Bakker89e80c92012-03-20 13:50:09 +0000216 size_t length,
217 const unsigned char *iv,
218 size_t iv_len,
219 const unsigned char *add,
220 size_t add_len,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200221 const unsigned char *tag,
Paul Bakker89e80c92012-03-20 13:50:09 +0000222 size_t tag_len,
223 const unsigned char *input,
224 unsigned char *output );
225
226/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000227 * \brief This function starts a GCM encryption or decryption
228 * operation.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200229 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000230 * \param ctx The GCM context.
231 * \param mode The operation to perform: #MBEDTLS_GCM_ENCRYPT or
232 * #MBEDTLS_GCM_DECRYPT.
233 * \param iv The initialization vector.
234 * \param iv_len The length of the IV.
235 * \param add The buffer holding the additional data, or NULL if \p add_len is 0.
236 * \param add_len The length of the additional data. If 0, \p add is NULL.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200237 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000238 * \return \c 0 on success.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200241 int mode,
242 const unsigned char *iv,
243 size_t iv_len,
244 const unsigned char *add,
245 size_t add_len );
246
247/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000248 * \brief This function feeds an input buffer into an ongoing GCM
249 * encryption or decryption operation.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200250 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000251 * ` The function expects input to be a multiple of 16
252 * Bytes. Only the last call before calling
253 * mbedtls_gcm_finish() can be less than 16 Bytes.
254 *
255 * \note For decryption, the output buffer cannot be the same as input buffer.
256 * If the buffers overlap, the output buffer must trail at least 8 Bytes
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200257 * behind the input buffer.
258 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000259 * \param ctx The GCM context.
260 * \param length The length of the input data. This must be a multiple of 16 except in the last call before mbedtls_gcm_finish().
261 * \param input The buffer holding the input data.
262 * \param output The buffer for holding the output data.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200263 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000264 * \return \c 0 on success, or #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200267 size_t length,
268 const unsigned char *input,
269 unsigned char *output );
270
271/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000272 * \brief This function finishes the GCM operation and generates
273 * the authentication tag.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200274 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000275 * It wraps up the GCM stream, and generates the
276 * tag. The tag can have a maximum length of 16 Bytes.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200277 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000278 * \param ctx The GCM context.
279 * \param tag The buffer for holding the tag.
280 * \param tag_len The length of the tag to generate. Must be at least four.
281 *
282 * \return \c 0 on success, or #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
Paul Bakkerb9d3cfa2013-06-26 15:07:16 +0200285 unsigned char *tag,
286 size_t tag_len );
287
288/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000289 * \brief This function clears a GCM context and the underlying
290 * cipher sub-context.
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200291 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000292 * \param ctx The GCM context to clear.
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200295
Jaeden Amero15263302017-09-21 12:53:48 +0100296#ifdef __cplusplus
297}
298#endif
299
300#else /* !MBEDTLS_GCM_ALT */
301#include "gcm_alt.h"
302#endif /* !MBEDTLS_GCM_ALT */
303
304#ifdef __cplusplus
305extern "C" {
306#endif
307
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200308/**
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000309 * \brief The GCM checkup routine.
Paul Bakker89e80c92012-03-20 13:50:09 +0000310 *
Rose Zadik17b4f7f2018-01-26 10:56:42 +0000311 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker89e80c92012-03-20 13:50:09 +0000312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313int mbedtls_gcm_self_test( int verbose );
Paul Bakker89e80c92012-03-20 13:50:09 +0000314
315#ifdef __cplusplus
316}
317#endif
318
Jaeden Amero15263302017-09-21 12:53:48 +0100319
Paul Bakker89e80c92012-03-20 13:50:09 +0000320#endif /* gcm.h */