Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file camellia.h |
| 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief Camellia block cipher |
| 5 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 9 | * |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +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 | */ |
| 24 | #ifndef POLARSSL_CAMELLIA_H |
| 25 | #define POLARSSL_CAMELLIA_H |
Paul Bakker | 477fd32 | 2009-10-04 13:22:13 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 28 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
| 30 | #include POLARSSL_CONFIG_FILE |
| 31 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 32 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame^] | 33 | #include <stddef.h> |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 34 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 35 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 477fd32 | 2009-10-04 13:22:13 +0000 | [diff] [blame] | 36 | #include <basetsd.h> |
| 37 | typedef UINT32 uint32_t; |
Paul Bakker | 80ab9f5 | 2009-05-24 14:42:46 +0000 | [diff] [blame] | 38 | #else |
Paul Bakker | 477fd32 | 2009-10-04 13:22:13 +0000 | [diff] [blame] | 39 | #include <inttypes.h> |
Paul Bakker | 80ab9f5 | 2009-05-24 14:42:46 +0000 | [diff] [blame] | 40 | #endif |
Paul Bakker | c81f6c3 | 2009-05-03 13:09:15 +0000 | [diff] [blame] | 41 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 42 | #define CAMELLIA_ENCRYPT 1 |
| 43 | #define CAMELLIA_DECRYPT 0 |
| 44 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 45 | #define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */ |
| 46 | #define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */ |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 47 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 48 | #if !defined(POLARSSL_CAMELLIA_ALT) |
| 49 | // Regular implementation |
| 50 | // |
| 51 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 52 | #ifdef __cplusplus |
| 53 | extern "C" { |
| 54 | #endif |
| 55 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 56 | /** |
| 57 | * \brief CAMELLIA context structure |
| 58 | */ |
| 59 | typedef struct |
| 60 | { |
| 61 | int nr; /*!< number of rounds */ |
Paul Bakker | c81f6c3 | 2009-05-03 13:09:15 +0000 | [diff] [blame] | 62 | uint32_t rk[68]; /*!< CAMELLIA round keys */ |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 63 | } |
| 64 | camellia_context; |
| 65 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 66 | /** |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 67 | * \brief Initialize CAMELLIA context |
| 68 | * |
| 69 | * \param ctx CAMELLIA context to be initialized |
| 70 | */ |
| 71 | void camellia_init( camellia_context *ctx ); |
| 72 | |
| 73 | /** |
| 74 | * \brief Clear CAMELLIA context |
| 75 | * |
| 76 | * \param ctx CAMELLIA context to be cleared |
| 77 | */ |
| 78 | void camellia_free( camellia_context *ctx ); |
| 79 | |
| 80 | /** |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 81 | * \brief CAMELLIA key schedule (encryption) |
| 82 | * |
| 83 | * \param ctx CAMELLIA context to be initialized |
| 84 | * \param key encryption key |
| 85 | * \param keysize must be 128, 192 or 256 |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 86 | * |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 87 | * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 88 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 89 | int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key, |
| 90 | unsigned int keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * \brief CAMELLIA key schedule (decryption) |
| 94 | * |
| 95 | * \param ctx CAMELLIA context to be initialized |
| 96 | * \param key decryption key |
| 97 | * \param keysize must be 128, 192 or 256 |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 98 | * |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 99 | * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 100 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 101 | int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key, |
| 102 | unsigned int keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * \brief CAMELLIA-ECB block encryption/decryption |
| 106 | * |
| 107 | * \param ctx CAMELLIA context |
| 108 | * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT |
| 109 | * \param input 16-byte input block |
| 110 | * \param output 16-byte output block |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 111 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 112 | * \return 0 if successful |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 113 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 114 | int camellia_crypt_ecb( camellia_context *ctx, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 115 | int mode, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 116 | const unsigned char input[16], |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 117 | unsigned char output[16] ); |
| 118 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 119 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 120 | /** |
| 121 | * \brief CAMELLIA-CBC buffer encryption/decryption |
Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 122 | * Length should be a multiple of the block |
| 123 | * size (16 bytes) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 124 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 125 | * \note Upon exit, the content of the IV is updated so that you can |
| 126 | * call the function same function again on the following |
| 127 | * block(s) of data and get the same result as if it was |
| 128 | * encrypted in one call. This allows a "streaming" usage. |
| 129 | * If on the other hand you need to retain the contents of the |
| 130 | * IV, you should either save it manually or use the cipher |
| 131 | * module instead. |
| 132 | * |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 133 | * \param ctx CAMELLIA context |
| 134 | * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT |
| 135 | * \param length length of the input data |
| 136 | * \param iv initialization vector (updated after use) |
| 137 | * \param input buffer holding the input data |
| 138 | * \param output buffer holding the output data |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 139 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 140 | * \return 0 if successful, or |
| 141 | * POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 142 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 143 | int camellia_crypt_cbc( camellia_context *ctx, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 144 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 145 | size_t length, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 146 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 147 | const unsigned char *input, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 148 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 149 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 150 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 151 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 152 | /** |
| 153 | * \brief CAMELLIA-CFB128 buffer encryption/decryption |
| 154 | * |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 155 | * Note: Due to the nature of CFB you should use the same key schedule for |
| 156 | * both encryption and decryption. So a context initialized with |
| 157 | * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT. |
| 158 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 159 | * \note Upon exit, the content of the IV is updated so that you can |
| 160 | * call the function same function again on the following |
| 161 | * block(s) of data and get the same result as if it was |
| 162 | * encrypted in one call. This allows a "streaming" usage. |
| 163 | * If on the other hand you need to retain the contents of the |
| 164 | * IV, you should either save it manually or use the cipher |
| 165 | * module instead. |
| 166 | * |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 167 | * \param ctx CAMELLIA context |
| 168 | * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT |
| 169 | * \param length length of the input data |
| 170 | * \param iv_off offset in IV (updated after use) |
| 171 | * \param iv initialization vector (updated after use) |
| 172 | * \param input buffer holding the input data |
| 173 | * \param output buffer holding the output data |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 174 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 175 | * \return 0 if successful, or |
| 176 | * POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 177 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 178 | int camellia_crypt_cfb128( camellia_context *ctx, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 179 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 180 | size_t length, |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 181 | size_t *iv_off, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 182 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 183 | const unsigned char *input, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 184 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 185 | #endif /* POLARSSL_CIPHER_MODE_CFB */ |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 186 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 187 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 188 | /** |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 189 | * \brief CAMELLIA-CTR buffer encryption/decryption |
| 190 | * |
| 191 | * Warning: You have to keep the maximum use of your counter in mind! |
| 192 | * |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 193 | * Note: Due to the nature of CTR you should use the same key schedule for |
| 194 | * both encryption and decryption. So a context initialized with |
| 195 | * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIA_DECRYPT. |
| 196 | * |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 197 | * \param ctx CAMELLIA context |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 198 | * \param length The length of the data |
| 199 | * \param nc_off The offset in the current stream_block (for resuming |
| 200 | * within current cipher stream). The offset pointer to |
| 201 | * should be 0 at the start of a stream. |
| 202 | * \param nonce_counter The 128-bit nonce and counter. |
| 203 | * \param stream_block The saved stream-block for resuming. Is overwritten |
| 204 | * by the function. |
| 205 | * \param input The input data stream |
| 206 | * \param output The output data stream |
| 207 | * |
| 208 | * \return 0 if successful |
| 209 | */ |
| 210 | int camellia_crypt_ctr( camellia_context *ctx, |
| 211 | size_t length, |
| 212 | size_t *nc_off, |
| 213 | unsigned char nonce_counter[16], |
| 214 | unsigned char stream_block[16], |
| 215 | const unsigned char *input, |
| 216 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 217 | #endif /* POLARSSL_CIPHER_MODE_CTR */ |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 218 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 219 | #ifdef __cplusplus |
| 220 | } |
| 221 | #endif |
| 222 | |
| 223 | #else /* POLARSSL_CAMELLIA_ALT */ |
| 224 | #include "camellia_alt.h" |
| 225 | #endif /* POLARSSL_CAMELLIA_ALT */ |
| 226 | |
| 227 | #ifdef __cplusplus |
| 228 | extern "C" { |
| 229 | #endif |
| 230 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 231 | /** |
| 232 | * \brief Checkup routine |
| 233 | * |
| 234 | * \return 0 if successful, or 1 if the test failed |
| 235 | */ |
| 236 | int camellia_self_test( int verbose ); |
| 237 | |
| 238 | #ifdef __cplusplus |
| 239 | } |
| 240 | #endif |
| 241 | |
| 242 | #endif /* camellia.h */ |