Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/include/polarssl/camellia.h@0:796d0f61a05b, 2018-09-27 (annotated)
- Committer:
- HannesTschofenig
- Date:
- Thu Sep 27 06:34:22 2018 +0000
- Revision:
- 0:796d0f61a05b
Example AES-GCM test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /** |
HannesTschofenig | 0:796d0f61a05b | 2 | * \file camellia.h |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * \brief Camellia block cipher |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * Copyright (C) 2006-2014, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 7 | * |
HannesTschofenig | 0:796d0f61a05b | 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 12 | * |
HannesTschofenig | 0:796d0f61a05b | 13 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 14 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 15 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 16 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 17 | * |
HannesTschofenig | 0:796d0f61a05b | 18 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 21 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 22 | * |
HannesTschofenig | 0:796d0f61a05b | 23 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 26 | */ |
HannesTschofenig | 0:796d0f61a05b | 27 | #ifndef POLARSSL_CAMELLIA_H |
HannesTschofenig | 0:796d0f61a05b | 28 | #define POLARSSL_CAMELLIA_H |
HannesTschofenig | 0:796d0f61a05b | 29 | |
HannesTschofenig | 0:796d0f61a05b | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 31 | #include "config.h" |
HannesTschofenig | 0:796d0f61a05b | 32 | #else |
HannesTschofenig | 0:796d0f61a05b | 33 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 34 | #endif |
HannesTschofenig | 0:796d0f61a05b | 35 | |
HannesTschofenig | 0:796d0f61a05b | 36 | #include <string.h> |
HannesTschofenig | 0:796d0f61a05b | 37 | |
HannesTschofenig | 0:796d0f61a05b | 38 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
HannesTschofenig | 0:796d0f61a05b | 39 | #include <basetsd.h> |
HannesTschofenig | 0:796d0f61a05b | 40 | typedef UINT32 uint32_t; |
HannesTschofenig | 0:796d0f61a05b | 41 | #else |
HannesTschofenig | 0:796d0f61a05b | 42 | #include <inttypes.h> |
HannesTschofenig | 0:796d0f61a05b | 43 | #endif |
HannesTschofenig | 0:796d0f61a05b | 44 | |
HannesTschofenig | 0:796d0f61a05b | 45 | #define CAMELLIA_ENCRYPT 1 |
HannesTschofenig | 0:796d0f61a05b | 46 | #define CAMELLIA_DECRYPT 0 |
HannesTschofenig | 0:796d0f61a05b | 47 | |
HannesTschofenig | 0:796d0f61a05b | 48 | #define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */ |
HannesTschofenig | 0:796d0f61a05b | 49 | #define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */ |
HannesTschofenig | 0:796d0f61a05b | 50 | |
HannesTschofenig | 0:796d0f61a05b | 51 | #if !defined(POLARSSL_CAMELLIA_ALT) |
HannesTschofenig | 0:796d0f61a05b | 52 | // Regular implementation |
HannesTschofenig | 0:796d0f61a05b | 53 | // |
HannesTschofenig | 0:796d0f61a05b | 54 | |
HannesTschofenig | 0:796d0f61a05b | 55 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 56 | extern "C" { |
HannesTschofenig | 0:796d0f61a05b | 57 | #endif |
HannesTschofenig | 0:796d0f61a05b | 58 | |
HannesTschofenig | 0:796d0f61a05b | 59 | /** |
HannesTschofenig | 0:796d0f61a05b | 60 | * \brief CAMELLIA context structure |
HannesTschofenig | 0:796d0f61a05b | 61 | */ |
HannesTschofenig | 0:796d0f61a05b | 62 | typedef struct |
HannesTschofenig | 0:796d0f61a05b | 63 | { |
HannesTschofenig | 0:796d0f61a05b | 64 | int nr; /*!< number of rounds */ |
HannesTschofenig | 0:796d0f61a05b | 65 | uint32_t rk[68]; /*!< CAMELLIA round keys */ |
HannesTschofenig | 0:796d0f61a05b | 66 | } |
HannesTschofenig | 0:796d0f61a05b | 67 | camellia_context; |
HannesTschofenig | 0:796d0f61a05b | 68 | |
HannesTschofenig | 0:796d0f61a05b | 69 | /** |
HannesTschofenig | 0:796d0f61a05b | 70 | * \brief CAMELLIA key schedule (encryption) |
HannesTschofenig | 0:796d0f61a05b | 71 | * |
HannesTschofenig | 0:796d0f61a05b | 72 | * \param ctx CAMELLIA context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 73 | * \param key encryption key |
HannesTschofenig | 0:796d0f61a05b | 74 | * \param keysize must be 128, 192 or 256 |
HannesTschofenig | 0:796d0f61a05b | 75 | * |
HannesTschofenig | 0:796d0f61a05b | 76 | * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH |
HannesTschofenig | 0:796d0f61a05b | 77 | */ |
HannesTschofenig | 0:796d0f61a05b | 78 | int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key, |
HannesTschofenig | 0:796d0f61a05b | 79 | unsigned int keysize ); |
HannesTschofenig | 0:796d0f61a05b | 80 | |
HannesTschofenig | 0:796d0f61a05b | 81 | /** |
HannesTschofenig | 0:796d0f61a05b | 82 | * \brief CAMELLIA key schedule (decryption) |
HannesTschofenig | 0:796d0f61a05b | 83 | * |
HannesTschofenig | 0:796d0f61a05b | 84 | * \param ctx CAMELLIA context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 85 | * \param key decryption key |
HannesTschofenig | 0:796d0f61a05b | 86 | * \param keysize must be 128, 192 or 256 |
HannesTschofenig | 0:796d0f61a05b | 87 | * |
HannesTschofenig | 0:796d0f61a05b | 88 | * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH |
HannesTschofenig | 0:796d0f61a05b | 89 | */ |
HannesTschofenig | 0:796d0f61a05b | 90 | int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key, |
HannesTschofenig | 0:796d0f61a05b | 91 | unsigned int keysize ); |
HannesTschofenig | 0:796d0f61a05b | 92 | |
HannesTschofenig | 0:796d0f61a05b | 93 | /** |
HannesTschofenig | 0:796d0f61a05b | 94 | * \brief CAMELLIA-ECB block encryption/decryption |
HannesTschofenig | 0:796d0f61a05b | 95 | * |
HannesTschofenig | 0:796d0f61a05b | 96 | * \param ctx CAMELLIA context |
HannesTschofenig | 0:796d0f61a05b | 97 | * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT |
HannesTschofenig | 0:796d0f61a05b | 98 | * \param input 16-byte input block |
HannesTschofenig | 0:796d0f61a05b | 99 | * \param output 16-byte output block |
HannesTschofenig | 0:796d0f61a05b | 100 | * |
HannesTschofenig | 0:796d0f61a05b | 101 | * \return 0 if successful |
HannesTschofenig | 0:796d0f61a05b | 102 | */ |
HannesTschofenig | 0:796d0f61a05b | 103 | int camellia_crypt_ecb( camellia_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 104 | int mode, |
HannesTschofenig | 0:796d0f61a05b | 105 | const unsigned char input[16], |
HannesTschofenig | 0:796d0f61a05b | 106 | unsigned char output[16] ); |
HannesTschofenig | 0:796d0f61a05b | 107 | |
HannesTschofenig | 0:796d0f61a05b | 108 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
HannesTschofenig | 0:796d0f61a05b | 109 | /** |
HannesTschofenig | 0:796d0f61a05b | 110 | * \brief CAMELLIA-CBC buffer encryption/decryption |
HannesTschofenig | 0:796d0f61a05b | 111 | * Length should be a multiple of the block |
HannesTschofenig | 0:796d0f61a05b | 112 | * size (16 bytes) |
HannesTschofenig | 0:796d0f61a05b | 113 | * |
HannesTschofenig | 0:796d0f61a05b | 114 | * \param ctx CAMELLIA context |
HannesTschofenig | 0:796d0f61a05b | 115 | * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT |
HannesTschofenig | 0:796d0f61a05b | 116 | * \param length length of the input data |
HannesTschofenig | 0:796d0f61a05b | 117 | * \param iv initialization vector (updated after use) |
HannesTschofenig | 0:796d0f61a05b | 118 | * \param input buffer holding the input data |
HannesTschofenig | 0:796d0f61a05b | 119 | * \param output buffer holding the output data |
HannesTschofenig | 0:796d0f61a05b | 120 | * |
HannesTschofenig | 0:796d0f61a05b | 121 | * \return 0 if successful, or |
HannesTschofenig | 0:796d0f61a05b | 122 | * POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH |
HannesTschofenig | 0:796d0f61a05b | 123 | */ |
HannesTschofenig | 0:796d0f61a05b | 124 | int camellia_crypt_cbc( camellia_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 125 | int mode, |
HannesTschofenig | 0:796d0f61a05b | 126 | size_t length, |
HannesTschofenig | 0:796d0f61a05b | 127 | unsigned char iv[16], |
HannesTschofenig | 0:796d0f61a05b | 128 | const unsigned char *input, |
HannesTschofenig | 0:796d0f61a05b | 129 | unsigned char *output ); |
HannesTschofenig | 0:796d0f61a05b | 130 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
HannesTschofenig | 0:796d0f61a05b | 131 | |
HannesTschofenig | 0:796d0f61a05b | 132 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
HannesTschofenig | 0:796d0f61a05b | 133 | /** |
HannesTschofenig | 0:796d0f61a05b | 134 | * \brief CAMELLIA-CFB128 buffer encryption/decryption |
HannesTschofenig | 0:796d0f61a05b | 135 | * |
HannesTschofenig | 0:796d0f61a05b | 136 | * Note: Due to the nature of CFB you should use the same key schedule for |
HannesTschofenig | 0:796d0f61a05b | 137 | * both encryption and decryption. So a context initialized with |
HannesTschofenig | 0:796d0f61a05b | 138 | * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT. |
HannesTschofenig | 0:796d0f61a05b | 139 | * |
HannesTschofenig | 0:796d0f61a05b | 140 | * \param ctx CAMELLIA context |
HannesTschofenig | 0:796d0f61a05b | 141 | * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT |
HannesTschofenig | 0:796d0f61a05b | 142 | * \param length length of the input data |
HannesTschofenig | 0:796d0f61a05b | 143 | * \param iv_off offset in IV (updated after use) |
HannesTschofenig | 0:796d0f61a05b | 144 | * \param iv initialization vector (updated after use) |
HannesTschofenig | 0:796d0f61a05b | 145 | * \param input buffer holding the input data |
HannesTschofenig | 0:796d0f61a05b | 146 | * \param output buffer holding the output data |
HannesTschofenig | 0:796d0f61a05b | 147 | * |
HannesTschofenig | 0:796d0f61a05b | 148 | * \return 0 if successful, or |
HannesTschofenig | 0:796d0f61a05b | 149 | * POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH |
HannesTschofenig | 0:796d0f61a05b | 150 | */ |
HannesTschofenig | 0:796d0f61a05b | 151 | int camellia_crypt_cfb128( camellia_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 152 | int mode, |
HannesTschofenig | 0:796d0f61a05b | 153 | size_t length, |
HannesTschofenig | 0:796d0f61a05b | 154 | size_t *iv_off, |
HannesTschofenig | 0:796d0f61a05b | 155 | unsigned char iv[16], |
HannesTschofenig | 0:796d0f61a05b | 156 | const unsigned char *input, |
HannesTschofenig | 0:796d0f61a05b | 157 | unsigned char *output ); |
HannesTschofenig | 0:796d0f61a05b | 158 | #endif /* POLARSSL_CIPHER_MODE_CFB */ |
HannesTschofenig | 0:796d0f61a05b | 159 | |
HannesTschofenig | 0:796d0f61a05b | 160 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
HannesTschofenig | 0:796d0f61a05b | 161 | /** |
HannesTschofenig | 0:796d0f61a05b | 162 | * \brief CAMELLIA-CTR buffer encryption/decryption |
HannesTschofenig | 0:796d0f61a05b | 163 | * |
HannesTschofenig | 0:796d0f61a05b | 164 | * Warning: You have to keep the maximum use of your counter in mind! |
HannesTschofenig | 0:796d0f61a05b | 165 | * |
HannesTschofenig | 0:796d0f61a05b | 166 | * Note: Due to the nature of CTR you should use the same key schedule for |
HannesTschofenig | 0:796d0f61a05b | 167 | * both encryption and decryption. So a context initialized with |
HannesTschofenig | 0:796d0f61a05b | 168 | * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIA_DECRYPT. |
HannesTschofenig | 0:796d0f61a05b | 169 | * |
HannesTschofenig | 0:796d0f61a05b | 170 | * \param ctx CAMELLIA context |
HannesTschofenig | 0:796d0f61a05b | 171 | * \param length The length of the data |
HannesTschofenig | 0:796d0f61a05b | 172 | * \param nc_off The offset in the current stream_block (for resuming |
HannesTschofenig | 0:796d0f61a05b | 173 | * within current cipher stream). The offset pointer to |
HannesTschofenig | 0:796d0f61a05b | 174 | * should be 0 at the start of a stream. |
HannesTschofenig | 0:796d0f61a05b | 175 | * \param nonce_counter The 128-bit nonce and counter. |
HannesTschofenig | 0:796d0f61a05b | 176 | * \param stream_block The saved stream-block for resuming. Is overwritten |
HannesTschofenig | 0:796d0f61a05b | 177 | * by the function. |
HannesTschofenig | 0:796d0f61a05b | 178 | * \param input The input data stream |
HannesTschofenig | 0:796d0f61a05b | 179 | * \param output The output data stream |
HannesTschofenig | 0:796d0f61a05b | 180 | * |
HannesTschofenig | 0:796d0f61a05b | 181 | * \return 0 if successful |
HannesTschofenig | 0:796d0f61a05b | 182 | */ |
HannesTschofenig | 0:796d0f61a05b | 183 | int camellia_crypt_ctr( camellia_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 184 | size_t length, |
HannesTschofenig | 0:796d0f61a05b | 185 | size_t *nc_off, |
HannesTschofenig | 0:796d0f61a05b | 186 | unsigned char nonce_counter[16], |
HannesTschofenig | 0:796d0f61a05b | 187 | unsigned char stream_block[16], |
HannesTschofenig | 0:796d0f61a05b | 188 | const unsigned char *input, |
HannesTschofenig | 0:796d0f61a05b | 189 | unsigned char *output ); |
HannesTschofenig | 0:796d0f61a05b | 190 | #endif /* POLARSSL_CIPHER_MODE_CTR */ |
HannesTschofenig | 0:796d0f61a05b | 191 | |
HannesTschofenig | 0:796d0f61a05b | 192 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 193 | } |
HannesTschofenig | 0:796d0f61a05b | 194 | #endif |
HannesTschofenig | 0:796d0f61a05b | 195 | |
HannesTschofenig | 0:796d0f61a05b | 196 | #else /* POLARSSL_CAMELLIA_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 197 | #include "camellia_alt.h" |
HannesTschofenig | 0:796d0f61a05b | 198 | #endif /* POLARSSL_CAMELLIA_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 199 | |
HannesTschofenig | 0:796d0f61a05b | 200 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 201 | extern "C" { |
HannesTschofenig | 0:796d0f61a05b | 202 | #endif |
HannesTschofenig | 0:796d0f61a05b | 203 | |
HannesTschofenig | 0:796d0f61a05b | 204 | /** |
HannesTschofenig | 0:796d0f61a05b | 205 | * \brief Checkup routine |
HannesTschofenig | 0:796d0f61a05b | 206 | * |
HannesTschofenig | 0:796d0f61a05b | 207 | * \return 0 if successful, or 1 if the test failed |
HannesTschofenig | 0:796d0f61a05b | 208 | */ |
HannesTschofenig | 0:796d0f61a05b | 209 | int camellia_self_test( int verbose ); |
HannesTschofenig | 0:796d0f61a05b | 210 | |
HannesTschofenig | 0:796d0f61a05b | 211 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 212 | } |
HannesTschofenig | 0:796d0f61a05b | 213 | #endif |
HannesTschofenig | 0:796d0f61a05b | 214 | |
HannesTschofenig | 0:796d0f61a05b | 215 | #endif /* camellia.h */ |
HannesTschofenig | 0:796d0f61a05b | 216 | |
HannesTschofenig | 0:796d0f61a05b | 217 |