mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /**
ansond 0:137634ff4186 2 * \file camellia.h
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * \brief Camellia block cipher
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 9 *
ansond 0:137634ff4186 10 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 11 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 12 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 13 * (at your option) any later version.
ansond 0:137634ff4186 14 *
ansond 0:137634ff4186 15 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 18 * GNU General Public License for more details.
ansond 0:137634ff4186 19 *
ansond 0:137634ff4186 20 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 21 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 23 */
ansond 0:137634ff4186 24 #ifndef POLARSSL_CAMELLIA_H
ansond 0:137634ff4186 25 #define POLARSSL_CAMELLIA_H
ansond 0:137634ff4186 26
ansond 0:137634ff4186 27 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 28 #include "config.h"
ansond 0:137634ff4186 29 #else
ansond 0:137634ff4186 30 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 31 #endif
ansond 0:137634ff4186 32
ansond 0:137634ff4186 33 #include <stddef.h>
ansond 0:137634ff4186 34
ansond 0:137634ff4186 35 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
ansond 0:137634ff4186 36 #include <basetsd.h>
ansond 0:137634ff4186 37 typedef UINT32 uint32_t;
ansond 0:137634ff4186 38 #else
ansond 0:137634ff4186 39 #include <inttypes.h>
ansond 0:137634ff4186 40 #endif
ansond 0:137634ff4186 41
ansond 0:137634ff4186 42 #define CAMELLIA_ENCRYPT 1
ansond 0:137634ff4186 43 #define CAMELLIA_DECRYPT 0
ansond 0:137634ff4186 44
ansond 0:137634ff4186 45 #define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
ansond 0:137634ff4186 46 #define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
ansond 0:137634ff4186 47
ansond 0:137634ff4186 48 #if !defined(POLARSSL_CAMELLIA_ALT)
ansond 0:137634ff4186 49 // Regular implementation
ansond 0:137634ff4186 50 //
ansond 0:137634ff4186 51
ansond 0:137634ff4186 52 #ifdef __cplusplus
ansond 0:137634ff4186 53 extern "C" {
ansond 0:137634ff4186 54 #endif
ansond 0:137634ff4186 55
ansond 0:137634ff4186 56 /**
ansond 0:137634ff4186 57 * \brief CAMELLIA context structure
ansond 0:137634ff4186 58 */
ansond 0:137634ff4186 59 typedef struct
ansond 0:137634ff4186 60 {
ansond 0:137634ff4186 61 int nr; /*!< number of rounds */
ansond 0:137634ff4186 62 uint32_t rk[68]; /*!< CAMELLIA round keys */
ansond 0:137634ff4186 63 }
ansond 0:137634ff4186 64 camellia_context;
ansond 0:137634ff4186 65
ansond 0:137634ff4186 66 /**
ansond 0:137634ff4186 67 * \brief Initialize CAMELLIA context
ansond 0:137634ff4186 68 *
ansond 0:137634ff4186 69 * \param ctx CAMELLIA context to be initialized
ansond 0:137634ff4186 70 */
ansond 0:137634ff4186 71 void camellia_init( camellia_context *ctx );
ansond 0:137634ff4186 72
ansond 0:137634ff4186 73 /**
ansond 0:137634ff4186 74 * \brief Clear CAMELLIA context
ansond 0:137634ff4186 75 *
ansond 0:137634ff4186 76 * \param ctx CAMELLIA context to be cleared
ansond 0:137634ff4186 77 */
ansond 0:137634ff4186 78 void camellia_free( camellia_context *ctx );
ansond 0:137634ff4186 79
ansond 0:137634ff4186 80 /**
ansond 0:137634ff4186 81 * \brief CAMELLIA key schedule (encryption)
ansond 0:137634ff4186 82 *
ansond 0:137634ff4186 83 * \param ctx CAMELLIA context to be initialized
ansond 0:137634ff4186 84 * \param key encryption key
ansond 0:137634ff4186 85 * \param keysize must be 128, 192 or 256
ansond 0:137634ff4186 86 *
ansond 0:137634ff4186 87 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
ansond 0:137634ff4186 88 */
ansond 0:137634ff4186 89 int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key,
ansond 0:137634ff4186 90 unsigned int keysize );
ansond 0:137634ff4186 91
ansond 0:137634ff4186 92 /**
ansond 0:137634ff4186 93 * \brief CAMELLIA key schedule (decryption)
ansond 0:137634ff4186 94 *
ansond 0:137634ff4186 95 * \param ctx CAMELLIA context to be initialized
ansond 0:137634ff4186 96 * \param key decryption key
ansond 0:137634ff4186 97 * \param keysize must be 128, 192 or 256
ansond 0:137634ff4186 98 *
ansond 0:137634ff4186 99 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
ansond 0:137634ff4186 100 */
ansond 0:137634ff4186 101 int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key,
ansond 0:137634ff4186 102 unsigned int keysize );
ansond 0:137634ff4186 103
ansond 0:137634ff4186 104 /**
ansond 0:137634ff4186 105 * \brief CAMELLIA-ECB block encryption/decryption
ansond 0:137634ff4186 106 *
ansond 0:137634ff4186 107 * \param ctx CAMELLIA context
ansond 0:137634ff4186 108 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
ansond 0:137634ff4186 109 * \param input 16-byte input block
ansond 0:137634ff4186 110 * \param output 16-byte output block
ansond 0:137634ff4186 111 *
ansond 0:137634ff4186 112 * \return 0 if successful
ansond 0:137634ff4186 113 */
ansond 0:137634ff4186 114 int camellia_crypt_ecb( camellia_context *ctx,
ansond 0:137634ff4186 115 int mode,
ansond 0:137634ff4186 116 const unsigned char input[16],
ansond 0:137634ff4186 117 unsigned char output[16] );
ansond 0:137634ff4186 118
ansond 0:137634ff4186 119 #if defined(POLARSSL_CIPHER_MODE_CBC)
ansond 0:137634ff4186 120 /**
ansond 0:137634ff4186 121 * \brief CAMELLIA-CBC buffer encryption/decryption
ansond 0:137634ff4186 122 * Length should be a multiple of the block
ansond 0:137634ff4186 123 * size (16 bytes)
ansond 0:137634ff4186 124 *
ansond 0:137634ff4186 125 * \note Upon exit, the content of the IV is updated so that you can
ansond 0:137634ff4186 126 * call the function same function again on the following
ansond 0:137634ff4186 127 * block(s) of data and get the same result as if it was
ansond 0:137634ff4186 128 * encrypted in one call. This allows a "streaming" usage.
ansond 0:137634ff4186 129 * If on the other hand you need to retain the contents of the
ansond 0:137634ff4186 130 * IV, you should either save it manually or use the cipher
ansond 0:137634ff4186 131 * module instead.
ansond 0:137634ff4186 132 *
ansond 0:137634ff4186 133 * \param ctx CAMELLIA context
ansond 0:137634ff4186 134 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
ansond 0:137634ff4186 135 * \param length length of the input data
ansond 0:137634ff4186 136 * \param iv initialization vector (updated after use)
ansond 0:137634ff4186 137 * \param input buffer holding the input data
ansond 0:137634ff4186 138 * \param output buffer holding the output data
ansond 0:137634ff4186 139 *
ansond 0:137634ff4186 140 * \return 0 if successful, or
ansond 0:137634ff4186 141 * POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
ansond 0:137634ff4186 142 */
ansond 0:137634ff4186 143 int camellia_crypt_cbc( camellia_context *ctx,
ansond 0:137634ff4186 144 int mode,
ansond 0:137634ff4186 145 size_t length,
ansond 0:137634ff4186 146 unsigned char iv[16],
ansond 0:137634ff4186 147 const unsigned char *input,
ansond 0:137634ff4186 148 unsigned char *output );
ansond 0:137634ff4186 149 #endif /* POLARSSL_CIPHER_MODE_CBC */
ansond 0:137634ff4186 150
ansond 0:137634ff4186 151 #if defined(POLARSSL_CIPHER_MODE_CFB)
ansond 0:137634ff4186 152 /**
ansond 0:137634ff4186 153 * \brief CAMELLIA-CFB128 buffer encryption/decryption
ansond 0:137634ff4186 154 *
ansond 0:137634ff4186 155 * Note: Due to the nature of CFB you should use the same key schedule for
ansond 0:137634ff4186 156 * both encryption and decryption. So a context initialized with
ansond 0:137634ff4186 157 * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
ansond 0:137634ff4186 158 *
ansond 0:137634ff4186 159 * \note Upon exit, the content of the IV is updated so that you can
ansond 0:137634ff4186 160 * call the function same function again on the following
ansond 0:137634ff4186 161 * block(s) of data and get the same result as if it was
ansond 0:137634ff4186 162 * encrypted in one call. This allows a "streaming" usage.
ansond 0:137634ff4186 163 * If on the other hand you need to retain the contents of the
ansond 0:137634ff4186 164 * IV, you should either save it manually or use the cipher
ansond 0:137634ff4186 165 * module instead.
ansond 0:137634ff4186 166 *
ansond 0:137634ff4186 167 * \param ctx CAMELLIA context
ansond 0:137634ff4186 168 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
ansond 0:137634ff4186 169 * \param length length of the input data
ansond 0:137634ff4186 170 * \param iv_off offset in IV (updated after use)
ansond 0:137634ff4186 171 * \param iv initialization vector (updated after use)
ansond 0:137634ff4186 172 * \param input buffer holding the input data
ansond 0:137634ff4186 173 * \param output buffer holding the output data
ansond 0:137634ff4186 174 *
ansond 0:137634ff4186 175 * \return 0 if successful, or
ansond 0:137634ff4186 176 * POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
ansond 0:137634ff4186 177 */
ansond 0:137634ff4186 178 int camellia_crypt_cfb128( camellia_context *ctx,
ansond 0:137634ff4186 179 int mode,
ansond 0:137634ff4186 180 size_t length,
ansond 0:137634ff4186 181 size_t *iv_off,
ansond 0:137634ff4186 182 unsigned char iv[16],
ansond 0:137634ff4186 183 const unsigned char *input,
ansond 0:137634ff4186 184 unsigned char *output );
ansond 0:137634ff4186 185 #endif /* POLARSSL_CIPHER_MODE_CFB */
ansond 0:137634ff4186 186
ansond 0:137634ff4186 187 #if defined(POLARSSL_CIPHER_MODE_CTR)
ansond 0:137634ff4186 188 /**
ansond 0:137634ff4186 189 * \brief CAMELLIA-CTR buffer encryption/decryption
ansond 0:137634ff4186 190 *
ansond 0:137634ff4186 191 * Warning: You have to keep the maximum use of your counter in mind!
ansond 0:137634ff4186 192 *
ansond 0:137634ff4186 193 * Note: Due to the nature of CTR you should use the same key schedule for
ansond 0:137634ff4186 194 * both encryption and decryption. So a context initialized with
ansond 0:137634ff4186 195 * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIA_DECRYPT.
ansond 0:137634ff4186 196 *
ansond 0:137634ff4186 197 * \param ctx CAMELLIA context
ansond 0:137634ff4186 198 * \param length The length of the data
ansond 0:137634ff4186 199 * \param nc_off The offset in the current stream_block (for resuming
ansond 0:137634ff4186 200 * within current cipher stream). The offset pointer to
ansond 0:137634ff4186 201 * should be 0 at the start of a stream.
ansond 0:137634ff4186 202 * \param nonce_counter The 128-bit nonce and counter.
ansond 0:137634ff4186 203 * \param stream_block The saved stream-block for resuming. Is overwritten
ansond 0:137634ff4186 204 * by the function.
ansond 0:137634ff4186 205 * \param input The input data stream
ansond 0:137634ff4186 206 * \param output The output data stream
ansond 0:137634ff4186 207 *
ansond 0:137634ff4186 208 * \return 0 if successful
ansond 0:137634ff4186 209 */
ansond 0:137634ff4186 210 int camellia_crypt_ctr( camellia_context *ctx,
ansond 0:137634ff4186 211 size_t length,
ansond 0:137634ff4186 212 size_t *nc_off,
ansond 0:137634ff4186 213 unsigned char nonce_counter[16],
ansond 0:137634ff4186 214 unsigned char stream_block[16],
ansond 0:137634ff4186 215 const unsigned char *input,
ansond 0:137634ff4186 216 unsigned char *output );
ansond 0:137634ff4186 217 #endif /* POLARSSL_CIPHER_MODE_CTR */
ansond 0:137634ff4186 218
ansond 0:137634ff4186 219 #ifdef __cplusplus
ansond 0:137634ff4186 220 }
ansond 0:137634ff4186 221 #endif
ansond 0:137634ff4186 222
ansond 0:137634ff4186 223 #else /* POLARSSL_CAMELLIA_ALT */
ansond 0:137634ff4186 224 #include "camellia_alt.h"
ansond 0:137634ff4186 225 #endif /* POLARSSL_CAMELLIA_ALT */
ansond 0:137634ff4186 226
ansond 0:137634ff4186 227 #ifdef __cplusplus
ansond 0:137634ff4186 228 extern "C" {
ansond 0:137634ff4186 229 #endif
ansond 0:137634ff4186 230
ansond 0:137634ff4186 231 /**
ansond 0:137634ff4186 232 * \brief Checkup routine
ansond 0:137634ff4186 233 *
ansond 0:137634ff4186 234 * \return 0 if successful, or 1 if the test failed
ansond 0:137634ff4186 235 */
ansond 0:137634ff4186 236 int camellia_self_test( int verbose );
ansond 0:137634ff4186 237
ansond 0:137634ff4186 238 #ifdef __cplusplus
ansond 0:137634ff4186 239 }
ansond 0:137634ff4186 240 #endif
ansond 0:137634ff4186 241
ansond 0:137634ff4186 242 #endif /* camellia.h */
ansond 0:137634ff4186 243