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