Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file gcm.h |
| 3 | * |
| 4 | * \brief Galois/Counter mode for AES |
| 5 | * |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_GCM_H |
| 28 | #define POLARSSL_GCM_H |
| 29 | |
| 30 | #include "aes.h" |
| 31 | |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 32 | #ifdef _MSC_VER |
| 33 | #include <basetsd.h> |
| 34 | typedef UINT64 uint64_t; |
| 35 | #else |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 36 | #include <stdint.h> |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 37 | #endif |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 38 | |
| 39 | #define GCM_ENCRYPT 1 |
| 40 | #define GCM_DECRYPT 0 |
| 41 | |
| 42 | #define POLARSSL_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 43 | #define POLARSSL_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 44 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 45 | #ifdef __cplusplus |
| 46 | extern "C" { |
| 47 | #endif |
| 48 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 49 | /** |
| 50 | * \brief GCM context structure |
| 51 | */ |
| 52 | typedef struct { |
| 53 | aes_context aes_ctx; /*!< AES context used */ |
| 54 | uint64_t HL[16]; /*!< Precalculated HTable */ |
| 55 | uint64_t HH[16]; /*!< Precalculated HTable */ |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 56 | uint64_t len; /*!< Total data length */ |
| 57 | uint64_t add_len; /*!< Total add length */ |
| 58 | unsigned char base_ectr[16];/*!< First ECTR for tag */ |
| 59 | unsigned char y[16]; /*!< Y working value */ |
| 60 | unsigned char buf[16]; /*!< buf working value */ |
| 61 | int mode; /*!< Encrypt or Decrypt */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 62 | } |
| 63 | gcm_context; |
| 64 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 65 | /** |
| 66 | * \brief GCM initialization (encryption) |
| 67 | * |
| 68 | * \param ctx GCM context to be initialized |
| 69 | * \param key encryption key |
| 70 | * \param keysize must be 128, 192 or 256 |
| 71 | * |
| 72 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH |
| 73 | */ |
| 74 | int gcm_init( gcm_context *ctx, const unsigned char *key, unsigned int keysize ); |
| 75 | |
| 76 | /** |
| 77 | * \brief GCM buffer encryption/decryption using AES |
| 78 | * |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 79 | * \note On encryption, the output buffer can be the same as the input buffer. |
| 80 | * On decryption, the output buffer cannot be the same as input buffer. |
| 81 | * If buffers overlap, the output buffer must trail at least 8 bytes |
| 82 | * behind the input buffer. |
| 83 | * |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 84 | * \param ctx GCM context |
| 85 | * \param mode GCM_ENCRYPT or GCM_DECRYPT |
| 86 | * \param length length of the input data |
| 87 | * \param iv initialization vector |
| 88 | * \param iv_len length of IV |
| 89 | * \param add additional data |
| 90 | * \param add_len length of additional data |
| 91 | * \param input buffer holding the input data |
| 92 | * \param output buffer for holding the output data |
| 93 | * \param tag_len length of the tag to generate |
| 94 | * \param tag buffer for holding the tag |
| 95 | * |
| 96 | * \return 0 if successful |
| 97 | */ |
| 98 | int gcm_crypt_and_tag( gcm_context *ctx, |
| 99 | int mode, |
| 100 | size_t length, |
| 101 | const unsigned char *iv, |
| 102 | size_t iv_len, |
| 103 | const unsigned char *add, |
| 104 | size_t add_len, |
| 105 | const unsigned char *input, |
| 106 | unsigned char *output, |
| 107 | size_t tag_len, |
| 108 | unsigned char *tag ); |
| 109 | |
| 110 | /** |
| 111 | * \brief GCM buffer authenticated decryption using AES |
| 112 | * |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 113 | * \note On decryption, the output buffer cannot be the same as input buffer. |
| 114 | * If buffers overlap, the output buffer must trail at least 8 bytes |
| 115 | * behind the input buffer. |
| 116 | * |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 117 | * \param ctx GCM context |
| 118 | * \param length length of the input data |
| 119 | * \param iv initialization vector |
| 120 | * \param iv_len length of IV |
| 121 | * \param add additional data |
| 122 | * \param add_len length of additional data |
| 123 | * \param tag buffer holding the tag |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 124 | * \param tag_len length of the tag |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 125 | * \param input buffer holding the input data |
| 126 | * \param output buffer for holding the output data |
| 127 | * |
| 128 | * \return 0 if successful and authenticated, |
| 129 | * POLARSSL_ERR_GCM_AUTH_FAILED if tag does not match |
| 130 | */ |
| 131 | int gcm_auth_decrypt( gcm_context *ctx, |
| 132 | size_t length, |
| 133 | const unsigned char *iv, |
| 134 | size_t iv_len, |
| 135 | const unsigned char *add, |
| 136 | size_t add_len, |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 137 | const unsigned char *tag, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 138 | size_t tag_len, |
| 139 | const unsigned char *input, |
| 140 | unsigned char *output ); |
| 141 | |
| 142 | /** |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 143 | * \brief Generic GCM stream start function |
| 144 | * |
| 145 | * \param ctx GCM context |
| 146 | * \param mode GCM_ENCRYPT or GCM_DECRYPT |
| 147 | * \param iv initialization vector |
| 148 | * \param iv_len length of IV |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 149 | * \param add additional data (or NULL if length is 0) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 150 | * \param add_len length of additional data |
| 151 | * |
| 152 | * \return 0 if successful |
| 153 | */ |
| 154 | int gcm_starts( gcm_context *ctx, |
| 155 | int mode, |
| 156 | const unsigned char *iv, |
| 157 | size_t iv_len, |
| 158 | const unsigned char *add, |
| 159 | size_t add_len ); |
| 160 | |
| 161 | /** |
| 162 | * \brief Generic GCM update function. Encrypts/decrypts using the |
| 163 | * given GCM context. Expects input to be a multiple of 16 |
| 164 | * bytes! Only the last call before gcm_finish() can be less |
| 165 | * than 16 bytes! |
| 166 | * |
| 167 | * \note On decryption, the output buffer cannot be the same as input buffer. |
| 168 | * If buffers overlap, the output buffer must trail at least 8 bytes |
| 169 | * behind the input buffer. |
| 170 | * |
| 171 | * \param ctx GCM context |
| 172 | * \param length length of the input data |
| 173 | * \param input buffer holding the input data |
| 174 | * \param output buffer for holding the output data |
| 175 | * |
| 176 | * \return 0 if successful or POLARSSL_ERR_GCM_BAD_INPUT |
| 177 | */ |
| 178 | int gcm_update( gcm_context *ctx, |
| 179 | size_t length, |
| 180 | const unsigned char *input, |
| 181 | unsigned char *output ); |
| 182 | |
| 183 | /** |
| 184 | * \brief Generic GCM finalisation function. Wraps up the GCM stream |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 185 | * and generates the tag. The tag can have a maximum length of |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 186 | * 16 bytes. |
| 187 | * |
| 188 | * \param ctx GCM context |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 189 | * \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] | 190 | * \param tag_len length of the tag to generate |
| 191 | * |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 192 | * \return 0 if successful or POLARSSL_ERR_GCM_BAD_INPUT |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 193 | */ |
| 194 | int gcm_finish( gcm_context *ctx, |
| 195 | unsigned char *tag, |
| 196 | size_t tag_len ); |
| 197 | |
| 198 | /** |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 199 | * \brief Checkup routine |
| 200 | * |
| 201 | * \return 0 if successful, or 1 if the test failed |
| 202 | */ |
| 203 | int gcm_self_test( int verbose ); |
| 204 | |
| 205 | #ifdef __cplusplus |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | #endif /* gcm.h */ |