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 | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 9 | * |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #ifndef MBEDTLS_GCM_H |
| 25 | #define MBEDTLS_GCM_H |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 26 | |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 27 | #include "cipher.h" |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 28 | |
| 29 | #include <stdint.h> |
| 30 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #define MBEDTLS_GCM_ENCRYPT 1 |
| 32 | #define MBEDTLS_GCM_DECRYPT 0 |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */ |
| 35 | #define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 36 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 41 | /** |
| 42 | * \brief GCM context structure |
| 43 | */ |
| 44 | typedef struct { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | mbedtls_cipher_context_t cipher_ctx;/*!< cipher context used */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 46 | uint64_t HL[16]; /*!< Precalculated HTable */ |
| 47 | uint64_t HH[16]; /*!< Precalculated HTable */ |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 48 | uint64_t len; /*!< Total data length */ |
| 49 | uint64_t add_len; /*!< Total add length */ |
| 50 | unsigned char base_ectr[16];/*!< First ECTR for tag */ |
| 51 | unsigned char y[16]; /*!< Y working value */ |
| 52 | unsigned char buf[16]; /*!< buf working value */ |
| 53 | int mode; /*!< Encrypt or Decrypt */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 54 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | mbedtls_gcm_context; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 56 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 57 | /** |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 58 | * \brief Initialize GCM context (just makes references valid) |
| 59 | * Makes the context ready for mbedtls_gcm_setkey() or |
| 60 | * mbedtls_gcm_free(). |
| 61 | * |
| 62 | * \param ctx GCM context to initialize |
| 63 | */ |
| 64 | void mbedtls_gcm_init( mbedtls_gcm_context *ctx ); |
| 65 | |
| 66 | /** |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 67 | * \brief GCM initialization (encryption) |
| 68 | * |
| 69 | * \param ctx GCM context to be initialized |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 70 | * \param cipher cipher to use (a 128-bit block cipher) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 71 | * \param key encryption key |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 72 | * \param keybits must be 128, 192 or 256 |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 73 | * |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 74 | * \return 0 if successful, or a cipher specific error code |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 75 | */ |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 76 | int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx, |
| 77 | mbedtls_cipher_id_t cipher, |
| 78 | const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 79 | unsigned int keybits ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 80 | |
| 81 | /** |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 82 | * \brief GCM buffer encryption/decryption using a block cipher |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 83 | * |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 84 | * \note On encryption, the output buffer can be the same as the input buffer. |
| 85 | * On decryption, the output buffer cannot be the same as input buffer. |
| 86 | * If buffers overlap, the output buffer must trail at least 8 bytes |
| 87 | * behind the input buffer. |
| 88 | * |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 89 | * \param ctx GCM context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 91 | * \param length length of the input data |
| 92 | * \param iv initialization vector |
| 93 | * \param iv_len length of IV |
| 94 | * \param add additional data |
| 95 | * \param add_len length of additional data |
| 96 | * \param input buffer holding the input data |
| 97 | * \param output buffer for holding the output data |
| 98 | * \param tag_len length of the tag to generate |
| 99 | * \param tag buffer for holding the tag |
| 100 | * |
| 101 | * \return 0 if successful |
| 102 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 104 | int mode, |
| 105 | size_t length, |
| 106 | const unsigned char *iv, |
| 107 | size_t iv_len, |
| 108 | const unsigned char *add, |
| 109 | size_t add_len, |
| 110 | const unsigned char *input, |
| 111 | unsigned char *output, |
| 112 | size_t tag_len, |
| 113 | unsigned char *tag ); |
| 114 | |
| 115 | /** |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 116 | * \brief GCM buffer authenticated decryption using a block cipher |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 117 | * |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 118 | * \note On decryption, the output buffer cannot be the same as input buffer. |
| 119 | * If buffers overlap, the output buffer must trail at least 8 bytes |
| 120 | * behind the input buffer. |
| 121 | * |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 122 | * \param ctx GCM context |
| 123 | * \param length length of the input data |
| 124 | * \param iv initialization vector |
| 125 | * \param iv_len length of IV |
| 126 | * \param add additional data |
| 127 | * \param add_len length of additional data |
| 128 | * \param tag buffer holding the tag |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 129 | * \param tag_len length of the tag |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 130 | * \param input buffer holding the input data |
| 131 | * \param output buffer for holding the output data |
| 132 | * |
| 133 | * \return 0 if successful and authenticated, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | * MBEDTLS_ERR_GCM_AUTH_FAILED if tag does not match |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 135 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 137 | size_t length, |
| 138 | const unsigned char *iv, |
| 139 | size_t iv_len, |
| 140 | const unsigned char *add, |
| 141 | size_t add_len, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 142 | const unsigned char *tag, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 143 | size_t tag_len, |
| 144 | const unsigned char *input, |
| 145 | unsigned char *output ); |
| 146 | |
| 147 | /** |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 148 | * \brief Generic GCM stream start function |
| 149 | * |
| 150 | * \param ctx GCM context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 152 | * \param iv initialization vector |
| 153 | * \param iv_len length of IV |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 154 | * \param add additional data (or NULL if length is 0) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 155 | * \param add_len length of additional data |
| 156 | * |
| 157 | * \return 0 if successful |
| 158 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | int mbedtls_gcm_starts( mbedtls_gcm_context *ctx, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 160 | int mode, |
| 161 | const unsigned char *iv, |
| 162 | size_t iv_len, |
| 163 | const unsigned char *add, |
| 164 | size_t add_len ); |
| 165 | |
| 166 | /** |
| 167 | * \brief Generic GCM update function. Encrypts/decrypts using the |
| 168 | * 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] | 169 | * bytes! Only the last call before mbedtls_gcm_finish() can be less |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 170 | * than 16 bytes! |
| 171 | * |
| 172 | * \note On decryption, the output buffer cannot be the same as input buffer. |
| 173 | * If buffers overlap, the output buffer must trail at least 8 bytes |
| 174 | * behind the input buffer. |
| 175 | * |
| 176 | * \param ctx GCM context |
| 177 | * \param length length of the input data |
| 178 | * \param input buffer holding the input data |
| 179 | * \param output buffer for holding the output data |
| 180 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT |
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_update( mbedtls_gcm_context *ctx, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 184 | size_t length, |
| 185 | const unsigned char *input, |
| 186 | unsigned char *output ); |
| 187 | |
| 188 | /** |
| 189 | * \brief Generic GCM finalisation function. Wraps up the GCM stream |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 190 | * and generates the tag. The tag can have a maximum length of |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 191 | * 16 bytes. |
| 192 | * |
| 193 | * \param ctx GCM context |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 194 | * \param tag buffer for holding the tag (may be NULL if tag_len is 0) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 195 | * \param tag_len length of the tag to generate |
| 196 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 198 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | int mbedtls_gcm_finish( mbedtls_gcm_context *ctx, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 200 | unsigned char *tag, |
| 201 | size_t tag_len ); |
| 202 | |
| 203 | /** |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 204 | * \brief Free a GCM context and underlying cipher sub-context |
| 205 | * |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 206 | * \param ctx GCM context to free |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 207 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | void mbedtls_gcm_free( mbedtls_gcm_context *ctx ); |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 209 | |
| 210 | /** |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 211 | * \brief Checkup routine |
| 212 | * |
| 213 | * \return 0 if successful, or 1 if the test failed |
| 214 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | int mbedtls_gcm_self_test( int verbose ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 216 | |
| 217 | #ifdef __cplusplus |
| 218 | } |
| 219 | #endif |
| 220 | |
| 221 | #endif /* gcm.h */ |