Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file gcm.h |
| 3 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 4 | * \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 Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 11 | */ |
| 12 | /* |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 13 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 14 | * SPDX-License-Identifier: Apache-2.0 |
| 15 | * |
| 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 17 | * not use this file except in compliance with the License. |
| 18 | * You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 27 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 28 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 29 | */ |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 30 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #ifndef MBEDTLS_GCM_H |
| 32 | #define MBEDTLS_GCM_H |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 33 | |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 34 | #include "cipher.h" |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 35 | |
| 36 | #include <stdint.h> |
| 37 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #define MBEDTLS_GCM_ENCRYPT 1 |
| 39 | #define MBEDTLS_GCM_DECRYPT 0 |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 40 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 42 | #define MBEDTLS_ERR_GCM_HW_ACCEL_FAILED -0x0013 /**< GCM hardware accelerator failed. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 44 | |
Jaeden Amero | 1526330 | 2017-09-21 12:53:48 +0100 | [diff] [blame] | 45 | #if !defined(MBEDTLS_GCM_ALT) |
| 46 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 51 | /** |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 52 | * \brief The GCM context structure. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 53 | */ |
| 54 | typedef struct { |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 55 | mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */ |
| 56 | uint64_t HL[16]; /*!< Precalculated HTable low. */ |
| 57 | uint64_t HH[16]; /*!< Precalculated HTable high. */ |
| 58 | uint64_t len; /*!< The total length of the encrypted data. */ |
| 59 | uint64_t add_len; /*!< The total length of the additional data. */ |
| 60 | unsigned char base_ectr[16]; /*!< The first ECTR for tag. */ |
| 61 | unsigned char y[16]; /*!< The Y working value. */ |
| 62 | unsigned char buf[16]; /*!< The buf working value. */ |
| 63 | int mode; /*!< The operation to perform: |
| 64 | #MBEDTLS_GCM_ENCRYPT or |
| 65 | #MBEDTLS_GCM_DECRYPT. */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 66 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | mbedtls_gcm_context; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 68 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 69 | /** |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 70 | * \brief This function initializes the specified GCM context, |
| 71 | * to make references valid, and prepares the context |
| 72 | * for mbedtls_gcm_setkey() or mbedtls_gcm_free(). |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 73 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 74 | * The function does not bind the GCM context to a particular |
| 75 | * cipher, nor set the key. For this purpose, use |
| 76 | * mbedtls_gcm_setkey(). |
| 77 | * |
| 78 | * \param ctx The GCM context to initialize. |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 79 | */ |
| 80 | void mbedtls_gcm_init( mbedtls_gcm_context *ctx ); |
| 81 | |
| 82 | /** |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 83 | * \brief This function associates a GCM context with a |
| 84 | * cipher algorithm and a key. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 85 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 86 | * \param ctx The GCM context to initialize. |
| 87 | * \param cipher The 128-bit block cipher to use. |
| 88 | * \param key The encryption key. |
| 89 | * \param keybits The key size in bits. Valid options are: |
| 90 | * <ul><li>128 bits</li> |
| 91 | * <li>192 bits</li> |
| 92 | * <li>256 bits</li></ul> |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 93 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 94 | * \return \c 0 on success, or a cipher specific error code. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 95 | */ |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 96 | int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx, |
| 97 | mbedtls_cipher_id_t cipher, |
| 98 | const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 99 | unsigned int keybits ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 100 | |
| 101 | /** |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 102 | * \brief This function performs GCM encryption or decryption of a buffer. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 103 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 104 | * \note For encryption, the output buffer can be the same as the input buffer. |
| 105 | * For decryption, the output buffer cannot be the same as input buffer. |
| 106 | * If the buffers overlap, the output buffer must trail at least 8 Bytes |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 107 | * behind the input buffer. |
| 108 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 109 | * \param ctx The GCM context to use for encryption or decryption. |
| 110 | * \param mode The operation to perform: #MBEDTLS_GCM_ENCRYPT or |
| 111 | * #MBEDTLS_GCM_DECRYPT. |
| 112 | * \param length The length of the input data. This must be a multiple of 16 except in the last call before mbedtls_gcm_finish(). |
| 113 | * \param iv The initialization vector. |
| 114 | * \param iv_len The length of the IV. |
| 115 | * \param add The buffer holding the additional data. |
| 116 | * \param add_len The length of the additional data. |
| 117 | * \param input The buffer holding the input data. |
| 118 | * \param output The buffer for holding the output data. |
| 119 | * \param tag_len The length of the tag to generate. |
| 120 | * \param tag The buffer for holding the tag. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 121 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 122 | * \return \c 0 on success. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 123 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 125 | int mode, |
| 126 | size_t length, |
| 127 | const unsigned char *iv, |
| 128 | size_t iv_len, |
| 129 | const unsigned char *add, |
| 130 | size_t add_len, |
| 131 | const unsigned char *input, |
| 132 | unsigned char *output, |
| 133 | size_t tag_len, |
| 134 | unsigned char *tag ); |
| 135 | |
| 136 | /** |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 137 | * \brief This function performs a GCM authenticated decryption of a |
| 138 | * buffer. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 139 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 140 | * \note For decryption, the output buffer cannot be the same as input buffer. |
| 141 | * If the buffers overlap, the output buffer must trail at least 8 Bytes |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 142 | * behind the input buffer. |
| 143 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 144 | * \param ctx The GCM context. |
| 145 | * \param length The length of the input data. This must be a multiple of 16 except in the last call before mbedtls_gcm_finish(). |
| 146 | * \param iv The initialization vector. |
| 147 | * \param iv_len The length of the IV. |
| 148 | * \param add The buffer holding the additional data. |
| 149 | * \param add_len The length of the additional data. |
| 150 | * \param tag The buffer holding the tag. |
| 151 | * \param tag_len The length of the tag. |
| 152 | * \param input The buffer holding the input data. |
| 153 | * \param output The buffer for holding the output data. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 154 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 155 | * \return 0 if successful and authenticated, or |
| 156 | * #MBEDTLS_ERR_GCM_AUTH_FAILED if tag does not match. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 157 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 159 | size_t length, |
| 160 | const unsigned char *iv, |
| 161 | size_t iv_len, |
| 162 | const unsigned char *add, |
| 163 | size_t add_len, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 164 | const unsigned char *tag, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 165 | size_t tag_len, |
| 166 | const unsigned char *input, |
| 167 | unsigned char *output ); |
| 168 | |
| 169 | /** |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 170 | * \brief This function starts a GCM encryption or decryption |
| 171 | * operation. |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 172 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 173 | * \param ctx The GCM context. |
| 174 | * \param mode The operation to perform: #MBEDTLS_GCM_ENCRYPT or |
| 175 | * #MBEDTLS_GCM_DECRYPT. |
| 176 | * \param iv The initialization vector. |
| 177 | * \param iv_len The length of the IV. |
| 178 | * \param add The buffer holding the additional data, or NULL if \p add_len is 0. |
| 179 | * \param add_len The length of the additional data. If 0, \p add is NULL. |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 180 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 181 | * \return \c 0 on success. |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 182 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | int mbedtls_gcm_starts( mbedtls_gcm_context *ctx, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 184 | int mode, |
| 185 | const unsigned char *iv, |
| 186 | size_t iv_len, |
| 187 | const unsigned char *add, |
| 188 | size_t add_len ); |
| 189 | |
| 190 | /** |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 191 | * \brief This function feeds an input buffer into an ongoing GCM |
| 192 | * encryption or decryption operation. |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 193 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 194 | * ` The function expects input to be a multiple of 16 |
| 195 | * Bytes. Only the last call before calling |
| 196 | * mbedtls_gcm_finish() can be less than 16 Bytes. |
| 197 | * |
| 198 | * \note For decryption, the output buffer cannot be the same as input buffer. |
| 199 | * If the buffers overlap, the output buffer must trail at least 8 Bytes |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 200 | * behind the input buffer. |
| 201 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 202 | * \param ctx The GCM context. |
| 203 | * \param length The length of the input data. This must be a multiple of 16 except in the last call before mbedtls_gcm_finish(). |
| 204 | * \param input The buffer holding the input data. |
| 205 | * \param output The buffer for holding the output data. |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 206 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 207 | * \return \c 0 on success, or #MBEDTLS_ERR_GCM_BAD_INPUT on failure. |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 208 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | int mbedtls_gcm_update( mbedtls_gcm_context *ctx, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 210 | size_t length, |
| 211 | const unsigned char *input, |
| 212 | unsigned char *output ); |
| 213 | |
| 214 | /** |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 215 | * \brief This function finishes the GCM operation and generates |
| 216 | * the authentication tag. |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 217 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 218 | * It wraps up the GCM stream, and generates the |
| 219 | * tag. The tag can have a maximum length of 16 Bytes. |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 220 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 221 | * \param ctx The GCM context. |
| 222 | * \param tag The buffer for holding the tag. |
| 223 | * \param tag_len The length of the tag to generate. Must be at least four. |
| 224 | * |
| 225 | * \return \c 0 on success, or #MBEDTLS_ERR_GCM_BAD_INPUT on failure. |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 226 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | int mbedtls_gcm_finish( mbedtls_gcm_context *ctx, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 228 | unsigned char *tag, |
| 229 | size_t tag_len ); |
| 230 | |
| 231 | /** |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 232 | * \brief This function clears a GCM context and the underlying |
| 233 | * cipher sub-context. |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 234 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 235 | * \param ctx The GCM context to clear. |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 236 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | void mbedtls_gcm_free( mbedtls_gcm_context *ctx ); |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 238 | |
Jaeden Amero | 1526330 | 2017-09-21 12:53:48 +0100 | [diff] [blame] | 239 | #ifdef __cplusplus |
| 240 | } |
| 241 | #endif |
| 242 | |
| 243 | #else /* !MBEDTLS_GCM_ALT */ |
| 244 | #include "gcm_alt.h" |
| 245 | #endif /* !MBEDTLS_GCM_ALT */ |
| 246 | |
| 247 | #ifdef __cplusplus |
| 248 | extern "C" { |
| 249 | #endif |
| 250 | |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 251 | /** |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 252 | * \brief The GCM checkup routine. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 253 | * |
Rose Zadik | 17b4f7f | 2018-01-26 10:56:42 +0000 | [diff] [blame^] | 254 | * \return \c 0 on success, or \c 1 on failure. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 255 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 256 | int mbedtls_gcm_self_test( int verbose ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 257 | |
| 258 | #ifdef __cplusplus |
| 259 | } |
| 260 | #endif |
| 261 | |
Jaeden Amero | 1526330 | 2017-09-21 12:53:48 +0100 | [diff] [blame] | 262 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 263 | #endif /* gcm.h */ |