Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file gcm.h |
| 3 | * |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 4 | * \brief Galois/Counter mode for 128-bit block ciphers |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 20 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 22 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #ifndef MBEDTLS_GCM_H |
| 24 | #define MBEDTLS_GCM_H |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 25 | |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 26 | #include "cipher.h" |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 27 | |
| 28 | #include <stdint.h> |
| 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #define MBEDTLS_GCM_ENCRYPT 1 |
| 31 | #define MBEDTLS_GCM_DECRYPT 0 |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */ |
| 34 | #define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 35 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 40 | /** |
| 41 | * \brief GCM context structure |
| 42 | */ |
| 43 | typedef struct { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | mbedtls_cipher_context_t cipher_ctx;/*!< cipher context used */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 45 | uint64_t HL[16]; /*!< Precalculated HTable */ |
| 46 | uint64_t HH[16]; /*!< Precalculated HTable */ |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 47 | uint64_t len; /*!< Total data length */ |
| 48 | uint64_t add_len; /*!< Total add length */ |
| 49 | unsigned char base_ectr[16];/*!< First ECTR for tag */ |
| 50 | unsigned char y[16]; /*!< Y working value */ |
| 51 | unsigned char buf[16]; /*!< buf working value */ |
| 52 | int mode; /*!< Encrypt or Decrypt */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 53 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | mbedtls_gcm_context; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 55 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 56 | /** |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 57 | * \brief Initialize GCM context (just makes references valid) |
| 58 | * Makes the context ready for mbedtls_gcm_setkey() or |
| 59 | * mbedtls_gcm_free(). |
| 60 | * |
| 61 | * \param ctx GCM context to initialize |
| 62 | */ |
| 63 | void mbedtls_gcm_init( mbedtls_gcm_context *ctx ); |
| 64 | |
| 65 | /** |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 66 | * \brief GCM initialization (encryption) |
| 67 | * |
| 68 | * \param ctx GCM context to be initialized |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 69 | * \param cipher cipher to use (a 128-bit block cipher) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 70 | * \param key encryption key |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 71 | * \param keybits must be 128, 192 or 256 |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 72 | * |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 73 | * \return 0 if successful, or a cipher specific error code |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 74 | */ |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 75 | int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx, |
| 76 | mbedtls_cipher_id_t cipher, |
| 77 | const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 78 | unsigned int keybits ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 79 | |
| 80 | /** |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 81 | * \brief GCM buffer encryption/decryption using a block cipher |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 82 | * |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 83 | * \note On encryption, the output buffer can be the same as the input buffer. |
| 84 | * On decryption, the output buffer cannot be the same as input buffer. |
| 85 | * If buffers overlap, the output buffer must trail at least 8 bytes |
| 86 | * behind the input buffer. |
| 87 | * |
Gilles Peskine | bfc3b74 | 2018-06-01 17:55:41 +0200 | [diff] [blame] | 88 | * \warning When this function performs a decryption, it outputs the |
| 89 | * authentication tag and does not verify that the data is |
| 90 | * authentic. You should use this function to perform encryption |
| 91 | * only. For decryption, use mbedtls_gcm_auth_decrypt() instead. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 92 | * |
Gilles Peskine | bfc3b74 | 2018-06-01 17:55:41 +0200 | [diff] [blame] | 93 | * \param ctx The GCM context to use for encryption or decryption. |
Gilles Peskine | 5b256af | 2018-06-07 14:46:02 +0200 | [diff] [blame^] | 94 | * \param mode The operation to perform: |
| 95 | * - #MBEDTLS_GCM_ENCRYPT to perform authenticated encryption. |
| 96 | * The ciphertext is written to \p output and the |
| 97 | * authentication tag is written to \p tag. |
| 98 | * - #MBEDTLS_GCM_DECRYPT to perform decryption. |
| 99 | * The plaintext is written to \p output and the |
| 100 | * authentication tag is written to \p tag. |
| 101 | * Note that this mode is not recommended, because it does |
| 102 | * not verify the authenticity of the data. For this reason, |
| 103 | * you should use mbedtls_gcm_auth_decrypt() instead of |
| 104 | * calling this function in decryption mode. |
Gilles Peskine | bfc3b74 | 2018-06-01 17:55:41 +0200 | [diff] [blame] | 105 | * \param length The length of the input data, which is equal to the length |
| 106 | * of the output data. |
| 107 | * \param iv The initialization vector. |
| 108 | * \param iv_len The length of the IV. |
| 109 | * \param add The buffer holding the additional data. |
| 110 | * \param add_len The length of the additional data. |
| 111 | * \param input The buffer holding the input data. Its size is \b length. |
| 112 | * \param output The buffer for holding the output data. It must have room |
| 113 | * for \b length bytes. |
| 114 | * \param tag_len The length of the tag to generate. |
| 115 | * \param tag The buffer for holding the tag. |
| 116 | * |
| 117 | * \return \c 0 if the encryption or decryption was performed |
| 118 | * successfully. Note that in #MBEDTLS_GCM_DECRYPT mode, |
| 119 | * this does not indicate that the data is authentic. |
| 120 | * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid. |
| 121 | * \return A cipher-specific error code if the encryption or |
| 122 | * decryption failed. |
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 | /** |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 137 | * \brief GCM buffer authenticated decryption using a block cipher |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 138 | * |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 139 | * \note On decryption, the output buffer cannot be the same as input buffer. |
| 140 | * If buffers overlap, the output buffer must trail at least 8 bytes |
| 141 | * behind the input buffer. |
| 142 | * |
Gilles Peskine | bfc3b74 | 2018-06-01 17:55:41 +0200 | [diff] [blame] | 143 | * \param ctx The GCM context. |
| 144 | * \param length The length of the ciphertext to decrypt, which is also |
| 145 | * the length of the decrypted plaintext. |
| 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 to verify. |
| 151 | * \param tag_len The length of the tag to verify. |
| 152 | * \param input The buffer holding the ciphertext. Its size is \b length. |
| 153 | * \param output The buffer for holding the decrypted plaintext. It must |
| 154 | * have room for \b length bytes. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 155 | * |
Gilles Peskine | bfc3b74 | 2018-06-01 17:55:41 +0200 | [diff] [blame] | 156 | * \return \c 0 if successful and authenticated. |
| 157 | * \return #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match. |
| 158 | * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid. |
| 159 | * \return A cipher-specific error code if the decryption failed. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 160 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 162 | size_t length, |
| 163 | const unsigned char *iv, |
| 164 | size_t iv_len, |
| 165 | const unsigned char *add, |
| 166 | size_t add_len, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 167 | const unsigned char *tag, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 168 | size_t tag_len, |
| 169 | const unsigned char *input, |
| 170 | unsigned char *output ); |
| 171 | |
| 172 | /** |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 173 | * \brief Generic GCM stream start function |
| 174 | * |
| 175 | * \param ctx GCM context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 177 | * \param iv initialization vector |
| 178 | * \param iv_len length of IV |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 179 | * \param add additional data (or NULL if length is 0) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 180 | * \param add_len length of additional data |
| 181 | * |
| 182 | * \return 0 if successful |
| 183 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | int mbedtls_gcm_starts( mbedtls_gcm_context *ctx, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 185 | int mode, |
| 186 | const unsigned char *iv, |
| 187 | size_t iv_len, |
| 188 | const unsigned char *add, |
| 189 | size_t add_len ); |
| 190 | |
| 191 | /** |
| 192 | * \brief Generic GCM update function. Encrypts/decrypts using the |
| 193 | * given GCM context. Expects input to be a multiple of 16 |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | * bytes! Only the last call before mbedtls_gcm_finish() can be less |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 195 | * than 16 bytes! |
| 196 | * |
| 197 | * \note On decryption, the output buffer cannot be the same as input buffer. |
| 198 | * If buffers overlap, the output buffer must trail at least 8 bytes |
| 199 | * behind the input buffer. |
| 200 | * |
| 201 | * \param ctx GCM context |
| 202 | * \param length length of the input data |
| 203 | * \param input buffer holding the input data |
| 204 | * \param output buffer for holding the output data |
| 205 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 207 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | int mbedtls_gcm_update( mbedtls_gcm_context *ctx, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 209 | size_t length, |
| 210 | const unsigned char *input, |
| 211 | unsigned char *output ); |
| 212 | |
| 213 | /** |
| 214 | * \brief Generic GCM finalisation function. Wraps up the GCM stream |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 215 | * and generates the tag. The tag can have a maximum length of |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 216 | * 16 bytes. |
| 217 | * |
| 218 | * \param ctx GCM context |
Andres AG | 6c05208 | 2016-09-26 10:09:30 +0100 | [diff] [blame] | 219 | * \param tag buffer for holding the tag |
| 220 | * \param tag_len length of the tag to generate (must be at least 4) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 221 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 222 | * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 223 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | int mbedtls_gcm_finish( mbedtls_gcm_context *ctx, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 225 | unsigned char *tag, |
| 226 | size_t tag_len ); |
| 227 | |
| 228 | /** |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 229 | * \brief Free a GCM context and underlying cipher sub-context |
| 230 | * |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 231 | * \param ctx GCM context to free |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 232 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | void mbedtls_gcm_free( mbedtls_gcm_context *ctx ); |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 234 | |
| 235 | /** |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 236 | * \brief Checkup routine |
| 237 | * |
| 238 | * \return 0 if successful, or 1 if the test failed |
| 239 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | int mbedtls_gcm_self_test( int verbose ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 241 | |
| 242 | #ifdef __cplusplus |
| 243 | } |
| 244 | #endif |
| 245 | |
| 246 | #endif /* gcm.h */ |