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 | * |
| 6 | * Copyright (C) 2006-2012, Brainspark B.V. |
| 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 | |
| 32 | #include <stdint.h> |
| 33 | |
| 34 | #define GCM_ENCRYPT 1 |
| 35 | #define GCM_DECRYPT 0 |
| 36 | |
| 37 | #define POLARSSL_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 38 | #define POLARSSL_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * \brief GCM context structure |
| 42 | */ |
| 43 | typedef struct { |
| 44 | aes_context aes_ctx; /*!< AES context used */ |
| 45 | uint64_t HL[16]; /*!< Precalculated HTable */ |
| 46 | uint64_t HH[16]; /*!< Precalculated HTable */ |
| 47 | } |
| 48 | gcm_context; |
| 49 | |
| 50 | #ifdef __cplusplus |
| 51 | extern "C" { |
| 52 | #endif |
| 53 | |
| 54 | /** |
| 55 | * \brief GCM initialization (encryption) |
| 56 | * |
| 57 | * \param ctx GCM context to be initialized |
| 58 | * \param key encryption key |
| 59 | * \param keysize must be 128, 192 or 256 |
| 60 | * |
| 61 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH |
| 62 | */ |
| 63 | int gcm_init( gcm_context *ctx, const unsigned char *key, unsigned int keysize ); |
| 64 | |
| 65 | /** |
| 66 | * \brief GCM buffer encryption/decryption using AES |
| 67 | * |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 68 | * \note On encryption, the output buffer can be the same as the input buffer. |
| 69 | * On decryption, the output buffer cannot be the same as input buffer. |
| 70 | * If buffers overlap, the output buffer must trail at least 8 bytes |
| 71 | * behind the input buffer. |
| 72 | * |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 73 | * \param ctx GCM context |
| 74 | * \param mode GCM_ENCRYPT or GCM_DECRYPT |
| 75 | * \param length length of the input data |
| 76 | * \param iv initialization vector |
| 77 | * \param iv_len length of IV |
| 78 | * \param add additional data |
| 79 | * \param add_len length of additional data |
| 80 | * \param input buffer holding the input data |
| 81 | * \param output buffer for holding the output data |
| 82 | * \param tag_len length of the tag to generate |
| 83 | * \param tag buffer for holding the tag |
| 84 | * |
| 85 | * \return 0 if successful |
| 86 | */ |
| 87 | int gcm_crypt_and_tag( gcm_context *ctx, |
| 88 | int mode, |
| 89 | size_t length, |
| 90 | const unsigned char *iv, |
| 91 | size_t iv_len, |
| 92 | const unsigned char *add, |
| 93 | size_t add_len, |
| 94 | const unsigned char *input, |
| 95 | unsigned char *output, |
| 96 | size_t tag_len, |
| 97 | unsigned char *tag ); |
| 98 | |
| 99 | /** |
| 100 | * \brief GCM buffer authenticated decryption using AES |
| 101 | * |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 102 | * \note On decryption, the output buffer cannot be the same as input buffer. |
| 103 | * If buffers overlap, the output buffer must trail at least 8 bytes |
| 104 | * behind the input buffer. |
| 105 | * |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 106 | * \param ctx GCM context |
| 107 | * \param length length of the input data |
| 108 | * \param iv initialization vector |
| 109 | * \param iv_len length of IV |
| 110 | * \param add additional data |
| 111 | * \param add_len length of additional data |
| 112 | * \param tag buffer holding the tag |
| 113 | * \param tag_len length of the tag |
| 114 | * \param input buffer holding the input data |
| 115 | * \param output buffer for holding the output data |
| 116 | * |
| 117 | * \return 0 if successful and authenticated, |
| 118 | * POLARSSL_ERR_GCM_AUTH_FAILED if tag does not match |
| 119 | */ |
| 120 | int gcm_auth_decrypt( gcm_context *ctx, |
| 121 | size_t length, |
| 122 | const unsigned char *iv, |
| 123 | size_t iv_len, |
| 124 | const unsigned char *add, |
| 125 | size_t add_len, |
| 126 | const unsigned char *tag, |
| 127 | size_t tag_len, |
| 128 | const unsigned char *input, |
| 129 | unsigned char *output ); |
| 130 | |
| 131 | /** |
| 132 | * \brief Checkup routine |
| 133 | * |
| 134 | * \return 0 if successful, or 1 if the test failed |
| 135 | */ |
| 136 | int gcm_self_test( int verbose ); |
| 137 | |
| 138 | #ifdef __cplusplus |
| 139 | } |
| 140 | #endif |
| 141 | |
| 142 | #endif /* gcm.h */ |