| 1 | /** |
|---|
| 2 | * \file camellia.h |
|---|
| 3 | * |
|---|
| 4 | * \brief Camellia block cipher |
|---|
| 5 | * |
|---|
| 6 | * Copyright (C) 2006-2010, 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_CAMELLIA_H |
|---|
| 28 | #define POLARSSL_CAMELLIA_H |
|---|
| 29 | |
|---|
| 30 | #include <string.h> |
|---|
| 31 | |
|---|
| 32 | #ifdef _MSC_VER |
|---|
| 33 | #include <basetsd.h> |
|---|
| 34 | typedef UINT32 uint32_t; |
|---|
| 35 | #else |
|---|
| 36 | #include <inttypes.h> |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | #define CAMELLIA_ENCRYPT 1 |
|---|
| 40 | #define CAMELLIA_DECRYPT 0 |
|---|
| 41 | |
|---|
| 42 | #define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */ |
|---|
| 43 | #define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */ |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * \brief CAMELLIA context structure |
|---|
| 47 | */ |
|---|
| 48 | typedef struct |
|---|
| 49 | { |
|---|
| 50 | int nr; /*!< number of rounds */ |
|---|
| 51 | uint32_t rk[68]; /*!< CAMELLIA round keys */ |
|---|
| 52 | } |
|---|
| 53 | camellia_context; |
|---|
| 54 | |
|---|
| 55 | #ifdef __cplusplus |
|---|
| 56 | extern "C" { |
|---|
| 57 | #endif |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * \brief CAMELLIA key schedule (encryption) |
|---|
| 61 | * |
|---|
| 62 | * \param ctx CAMELLIA context to be initialized |
|---|
| 63 | * \param key encryption key |
|---|
| 64 | * \param keysize must be 128, 192 or 256 |
|---|
| 65 | * |
|---|
| 66 | * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH |
|---|
| 67 | */ |
|---|
| 68 | int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key, unsigned int keysize ); |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * \brief CAMELLIA key schedule (decryption) |
|---|
| 72 | * |
|---|
| 73 | * \param ctx CAMELLIA context to be initialized |
|---|
| 74 | * \param key decryption key |
|---|
| 75 | * \param keysize must be 128, 192 or 256 |
|---|
| 76 | * |
|---|
| 77 | * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH |
|---|
| 78 | */ |
|---|
| 79 | int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key, unsigned int keysize ); |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * \brief CAMELLIA-ECB block encryption/decryption |
|---|
| 83 | * |
|---|
| 84 | * \param ctx CAMELLIA context |
|---|
| 85 | * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT |
|---|
| 86 | * \param input 16-byte input block |
|---|
| 87 | * \param output 16-byte output block |
|---|
| 88 | * |
|---|
| 89 | * \return 0 if successful |
|---|
| 90 | */ |
|---|
| 91 | int camellia_crypt_ecb( camellia_context *ctx, |
|---|
| 92 | int mode, |
|---|
| 93 | const unsigned char input[16], |
|---|
| 94 | unsigned char output[16] ); |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * \brief CAMELLIA-CBC buffer encryption/decryption |
|---|
| 98 | * Length should be a multiple of the block |
|---|
| 99 | * size (16 bytes) |
|---|
| 100 | * |
|---|
| 101 | * \param ctx CAMELLIA context |
|---|
| 102 | * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT |
|---|
| 103 | * \param length length of the input data |
|---|
| 104 | * \param iv initialization vector (updated after use) |
|---|
| 105 | * \param input buffer holding the input data |
|---|
| 106 | * \param output buffer holding the output data |
|---|
| 107 | * |
|---|
| 108 | * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH |
|---|
| 109 | */ |
|---|
| 110 | int camellia_crypt_cbc( camellia_context *ctx, |
|---|
| 111 | int mode, |
|---|
| 112 | size_t length, |
|---|
| 113 | unsigned char iv[16], |
|---|
| 114 | const unsigned char *input, |
|---|
| 115 | unsigned char *output ); |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * \brief CAMELLIA-CFB128 buffer encryption/decryption |
|---|
| 119 | * |
|---|
| 120 | * Note: Due to the nature of CFB you should use the same key schedule for |
|---|
| 121 | * both encryption and decryption. So a context initialized with |
|---|
| 122 | * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT. |
|---|
| 123 | * |
|---|
| 124 | * \param ctx CAMELLIA context |
|---|
| 125 | * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT |
|---|
| 126 | * \param length length of the input data |
|---|
| 127 | * \param iv_off offset in IV (updated after use) |
|---|
| 128 | * \param iv initialization vector (updated after use) |
|---|
| 129 | * \param input buffer holding the input data |
|---|
| 130 | * \param output buffer holding the output data |
|---|
| 131 | * |
|---|
| 132 | * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH |
|---|
| 133 | */ |
|---|
| 134 | int camellia_crypt_cfb128( camellia_context *ctx, |
|---|
| 135 | int mode, |
|---|
| 136 | size_t length, |
|---|
| 137 | size_t *iv_off, |
|---|
| 138 | unsigned char iv[16], |
|---|
| 139 | const unsigned char *input, |
|---|
| 140 | unsigned char *output ); |
|---|
| 141 | |
|---|
| 142 | /* |
|---|
| 143 | * \brief CAMELLIA-CTR buffer encryption/decryption |
|---|
| 144 | * |
|---|
| 145 | * Warning: You have to keep the maximum use of your counter in mind! |
|---|
| 146 | * |
|---|
| 147 | * Note: Due to the nature of CTR you should use the same key schedule for |
|---|
| 148 | * both encryption and decryption. So a context initialized with |
|---|
| 149 | * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIA_DECRYPT. |
|---|
| 150 | * |
|---|
| 151 | * \param length The length of the data |
|---|
| 152 | * \param nc_off The offset in the current stream_block (for resuming |
|---|
| 153 | * within current cipher stream). The offset pointer to |
|---|
| 154 | * should be 0 at the start of a stream. |
|---|
| 155 | * \param nonce_counter The 128-bit nonce and counter. |
|---|
| 156 | * \param stream_block The saved stream-block for resuming. Is overwritten |
|---|
| 157 | * by the function. |
|---|
| 158 | * \param input The input data stream |
|---|
| 159 | * \param output The output data stream |
|---|
| 160 | * |
|---|
| 161 | * \return 0 if successful |
|---|
| 162 | */ |
|---|
| 163 | int camellia_crypt_ctr( camellia_context *ctx, |
|---|
| 164 | size_t length, |
|---|
| 165 | size_t *nc_off, |
|---|
| 166 | unsigned char nonce_counter[16], |
|---|
| 167 | unsigned char stream_block[16], |
|---|
| 168 | const unsigned char *input, |
|---|
| 169 | unsigned char *output ); |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * \brief Checkup routine |
|---|
| 173 | * |
|---|
| 174 | * \return 0 if successful, or 1 if the test failed |
|---|
| 175 | */ |
|---|
| 176 | int camellia_self_test( int verbose ); |
|---|
| 177 | |
|---|
| 178 | #ifdef __cplusplus |
|---|
| 179 | } |
|---|
| 180 | #endif |
|---|
| 181 | |
|---|
| 182 | #endif /* camellia.h */ |
|---|