Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon Cooksey 0:fb7af294d5d9 1 /**
Simon Cooksey 0:fb7af294d5d9 2 * \file camellia.h
Simon Cooksey 0:fb7af294d5d9 3 *
Simon Cooksey 0:fb7af294d5d9 4 * \brief Camellia block cipher
Simon Cooksey 0:fb7af294d5d9 5 *
Simon Cooksey 0:fb7af294d5d9 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Simon Cooksey 0:fb7af294d5d9 7 * SPDX-License-Identifier: Apache-2.0
Simon Cooksey 0:fb7af294d5d9 8 *
Simon Cooksey 0:fb7af294d5d9 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Simon Cooksey 0:fb7af294d5d9 10 * not use this file except in compliance with the License.
Simon Cooksey 0:fb7af294d5d9 11 * You may obtain a copy of the License at
Simon Cooksey 0:fb7af294d5d9 12 *
Simon Cooksey 0:fb7af294d5d9 13 * http://www.apache.org/licenses/LICENSE-2.0
Simon Cooksey 0:fb7af294d5d9 14 *
Simon Cooksey 0:fb7af294d5d9 15 * Unless required by applicable law or agreed to in writing, software
Simon Cooksey 0:fb7af294d5d9 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Simon Cooksey 0:fb7af294d5d9 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Simon Cooksey 0:fb7af294d5d9 18 * See the License for the specific language governing permissions and
Simon Cooksey 0:fb7af294d5d9 19 * limitations under the License.
Simon Cooksey 0:fb7af294d5d9 20 *
Simon Cooksey 0:fb7af294d5d9 21 * This file is part of mbed TLS (https://tls.mbed.org)
Simon Cooksey 0:fb7af294d5d9 22 */
Simon Cooksey 0:fb7af294d5d9 23 #ifndef MBEDTLS_CAMELLIA_H
Simon Cooksey 0:fb7af294d5d9 24 #define MBEDTLS_CAMELLIA_H
Simon Cooksey 0:fb7af294d5d9 25
Simon Cooksey 0:fb7af294d5d9 26 #if !defined(MBEDTLS_CONFIG_FILE)
Simon Cooksey 0:fb7af294d5d9 27 #include "config.h"
Simon Cooksey 0:fb7af294d5d9 28 #else
Simon Cooksey 0:fb7af294d5d9 29 #include MBEDTLS_CONFIG_FILE
Simon Cooksey 0:fb7af294d5d9 30 #endif
Simon Cooksey 0:fb7af294d5d9 31
Simon Cooksey 0:fb7af294d5d9 32 #include <stddef.h>
Simon Cooksey 0:fb7af294d5d9 33 #include <stdint.h>
Simon Cooksey 0:fb7af294d5d9 34
Simon Cooksey 0:fb7af294d5d9 35 #define MBEDTLS_CAMELLIA_ENCRYPT 1
Simon Cooksey 0:fb7af294d5d9 36 #define MBEDTLS_CAMELLIA_DECRYPT 0
Simon Cooksey 0:fb7af294d5d9 37
Simon Cooksey 0:fb7af294d5d9 38 #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
Simon Cooksey 0:fb7af294d5d9 39 #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
Simon Cooksey 0:fb7af294d5d9 40
Simon Cooksey 0:fb7af294d5d9 41 #if !defined(MBEDTLS_CAMELLIA_ALT)
Simon Cooksey 0:fb7af294d5d9 42 // Regular implementation
Simon Cooksey 0:fb7af294d5d9 43 //
Simon Cooksey 0:fb7af294d5d9 44
Simon Cooksey 0:fb7af294d5d9 45 #ifdef __cplusplus
Simon Cooksey 0:fb7af294d5d9 46 extern "C" {
Simon Cooksey 0:fb7af294d5d9 47 #endif
Simon Cooksey 0:fb7af294d5d9 48
Simon Cooksey 0:fb7af294d5d9 49 /**
Simon Cooksey 0:fb7af294d5d9 50 * \brief CAMELLIA context structure
Simon Cooksey 0:fb7af294d5d9 51 */
Simon Cooksey 0:fb7af294d5d9 52 typedef struct
Simon Cooksey 0:fb7af294d5d9 53 {
Simon Cooksey 0:fb7af294d5d9 54 int nr; /*!< number of rounds */
Simon Cooksey 0:fb7af294d5d9 55 uint32_t rk[68]; /*!< CAMELLIA round keys */
Simon Cooksey 0:fb7af294d5d9 56 }
Simon Cooksey 0:fb7af294d5d9 57 mbedtls_camellia_context;
Simon Cooksey 0:fb7af294d5d9 58
Simon Cooksey 0:fb7af294d5d9 59 /**
Simon Cooksey 0:fb7af294d5d9 60 * \brief Initialize CAMELLIA context
Simon Cooksey 0:fb7af294d5d9 61 *
Simon Cooksey 0:fb7af294d5d9 62 * \param ctx CAMELLIA context to be initialized
Simon Cooksey 0:fb7af294d5d9 63 */
Simon Cooksey 0:fb7af294d5d9 64 void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
Simon Cooksey 0:fb7af294d5d9 65
Simon Cooksey 0:fb7af294d5d9 66 /**
Simon Cooksey 0:fb7af294d5d9 67 * \brief Clear CAMELLIA context
Simon Cooksey 0:fb7af294d5d9 68 *
Simon Cooksey 0:fb7af294d5d9 69 * \param ctx CAMELLIA context to be cleared
Simon Cooksey 0:fb7af294d5d9 70 */
Simon Cooksey 0:fb7af294d5d9 71 void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
Simon Cooksey 0:fb7af294d5d9 72
Simon Cooksey 0:fb7af294d5d9 73 /**
Simon Cooksey 0:fb7af294d5d9 74 * \brief CAMELLIA key schedule (encryption)
Simon Cooksey 0:fb7af294d5d9 75 *
Simon Cooksey 0:fb7af294d5d9 76 * \param ctx CAMELLIA context to be initialized
Simon Cooksey 0:fb7af294d5d9 77 * \param key encryption key
Simon Cooksey 0:fb7af294d5d9 78 * \param keybits must be 128, 192 or 256
Simon Cooksey 0:fb7af294d5d9 79 *
Simon Cooksey 0:fb7af294d5d9 80 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Simon Cooksey 0:fb7af294d5d9 81 */
Simon Cooksey 0:fb7af294d5d9 82 int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
Simon Cooksey 0:fb7af294d5d9 83 unsigned int keybits );
Simon Cooksey 0:fb7af294d5d9 84
Simon Cooksey 0:fb7af294d5d9 85 /**
Simon Cooksey 0:fb7af294d5d9 86 * \brief CAMELLIA key schedule (decryption)
Simon Cooksey 0:fb7af294d5d9 87 *
Simon Cooksey 0:fb7af294d5d9 88 * \param ctx CAMELLIA context to be initialized
Simon Cooksey 0:fb7af294d5d9 89 * \param key decryption key
Simon Cooksey 0:fb7af294d5d9 90 * \param keybits must be 128, 192 or 256
Simon Cooksey 0:fb7af294d5d9 91 *
Simon Cooksey 0:fb7af294d5d9 92 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Simon Cooksey 0:fb7af294d5d9 93 */
Simon Cooksey 0:fb7af294d5d9 94 int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
Simon Cooksey 0:fb7af294d5d9 95 unsigned int keybits );
Simon Cooksey 0:fb7af294d5d9 96
Simon Cooksey 0:fb7af294d5d9 97 /**
Simon Cooksey 0:fb7af294d5d9 98 * \brief CAMELLIA-ECB block encryption/decryption
Simon Cooksey 0:fb7af294d5d9 99 *
Simon Cooksey 0:fb7af294d5d9 100 * \param ctx CAMELLIA context
Simon Cooksey 0:fb7af294d5d9 101 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Simon Cooksey 0:fb7af294d5d9 102 * \param input 16-byte input block
Simon Cooksey 0:fb7af294d5d9 103 * \param output 16-byte output block
Simon Cooksey 0:fb7af294d5d9 104 *
Simon Cooksey 0:fb7af294d5d9 105 * \return 0 if successful
Simon Cooksey 0:fb7af294d5d9 106 */
Simon Cooksey 0:fb7af294d5d9 107 int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
Simon Cooksey 0:fb7af294d5d9 108 int mode,
Simon Cooksey 0:fb7af294d5d9 109 const unsigned char input[16],
Simon Cooksey 0:fb7af294d5d9 110 unsigned char output[16] );
Simon Cooksey 0:fb7af294d5d9 111
Simon Cooksey 0:fb7af294d5d9 112 #if defined(MBEDTLS_CIPHER_MODE_CBC)
Simon Cooksey 0:fb7af294d5d9 113 /**
Simon Cooksey 0:fb7af294d5d9 114 * \brief CAMELLIA-CBC buffer encryption/decryption
Simon Cooksey 0:fb7af294d5d9 115 * Length should be a multiple of the block
Simon Cooksey 0:fb7af294d5d9 116 * size (16 bytes)
Simon Cooksey 0:fb7af294d5d9 117 *
Simon Cooksey 0:fb7af294d5d9 118 * \note Upon exit, the content of the IV is updated so that you can
Simon Cooksey 0:fb7af294d5d9 119 * call the function same function again on the following
Simon Cooksey 0:fb7af294d5d9 120 * block(s) of data and get the same result as if it was
Simon Cooksey 0:fb7af294d5d9 121 * encrypted in one call. This allows a "streaming" usage.
Simon Cooksey 0:fb7af294d5d9 122 * If on the other hand you need to retain the contents of the
Simon Cooksey 0:fb7af294d5d9 123 * IV, you should either save it manually or use the cipher
Simon Cooksey 0:fb7af294d5d9 124 * module instead.
Simon Cooksey 0:fb7af294d5d9 125 *
Simon Cooksey 0:fb7af294d5d9 126 * \param ctx CAMELLIA context
Simon Cooksey 0:fb7af294d5d9 127 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Simon Cooksey 0:fb7af294d5d9 128 * \param length length of the input data
Simon Cooksey 0:fb7af294d5d9 129 * \param iv initialization vector (updated after use)
Simon Cooksey 0:fb7af294d5d9 130 * \param input buffer holding the input data
Simon Cooksey 0:fb7af294d5d9 131 * \param output buffer holding the output data
Simon Cooksey 0:fb7af294d5d9 132 *
Simon Cooksey 0:fb7af294d5d9 133 * \return 0 if successful, or
Simon Cooksey 0:fb7af294d5d9 134 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Simon Cooksey 0:fb7af294d5d9 135 */
Simon Cooksey 0:fb7af294d5d9 136 int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
Simon Cooksey 0:fb7af294d5d9 137 int mode,
Simon Cooksey 0:fb7af294d5d9 138 size_t length,
Simon Cooksey 0:fb7af294d5d9 139 unsigned char iv[16],
Simon Cooksey 0:fb7af294d5d9 140 const unsigned char *input,
Simon Cooksey 0:fb7af294d5d9 141 unsigned char *output );
Simon Cooksey 0:fb7af294d5d9 142 #endif /* MBEDTLS_CIPHER_MODE_CBC */
Simon Cooksey 0:fb7af294d5d9 143
Simon Cooksey 0:fb7af294d5d9 144 #if defined(MBEDTLS_CIPHER_MODE_CFB)
Simon Cooksey 0:fb7af294d5d9 145 /**
Simon Cooksey 0:fb7af294d5d9 146 * \brief CAMELLIA-CFB128 buffer encryption/decryption
Simon Cooksey 0:fb7af294d5d9 147 *
Simon Cooksey 0:fb7af294d5d9 148 * Note: Due to the nature of CFB you should use the same key schedule for
Simon Cooksey 0:fb7af294d5d9 149 * both encryption and decryption. So a context initialized with
Simon Cooksey 0:fb7af294d5d9 150 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
Simon Cooksey 0:fb7af294d5d9 151 *
Simon Cooksey 0:fb7af294d5d9 152 * \note Upon exit, the content of the IV is updated so that you can
Simon Cooksey 0:fb7af294d5d9 153 * call the function same function again on the following
Simon Cooksey 0:fb7af294d5d9 154 * block(s) of data and get the same result as if it was
Simon Cooksey 0:fb7af294d5d9 155 * encrypted in one call. This allows a "streaming" usage.
Simon Cooksey 0:fb7af294d5d9 156 * If on the other hand you need to retain the contents of the
Simon Cooksey 0:fb7af294d5d9 157 * IV, you should either save it manually or use the cipher
Simon Cooksey 0:fb7af294d5d9 158 * module instead.
Simon Cooksey 0:fb7af294d5d9 159 *
Simon Cooksey 0:fb7af294d5d9 160 * \param ctx CAMELLIA context
Simon Cooksey 0:fb7af294d5d9 161 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Simon Cooksey 0:fb7af294d5d9 162 * \param length length of the input data
Simon Cooksey 0:fb7af294d5d9 163 * \param iv_off offset in IV (updated after use)
Simon Cooksey 0:fb7af294d5d9 164 * \param iv initialization vector (updated after use)
Simon Cooksey 0:fb7af294d5d9 165 * \param input buffer holding the input data
Simon Cooksey 0:fb7af294d5d9 166 * \param output buffer holding the output data
Simon Cooksey 0:fb7af294d5d9 167 *
Simon Cooksey 0:fb7af294d5d9 168 * \return 0 if successful, or
Simon Cooksey 0:fb7af294d5d9 169 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Simon Cooksey 0:fb7af294d5d9 170 */
Simon Cooksey 0:fb7af294d5d9 171 int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
Simon Cooksey 0:fb7af294d5d9 172 int mode,
Simon Cooksey 0:fb7af294d5d9 173 size_t length,
Simon Cooksey 0:fb7af294d5d9 174 size_t *iv_off,
Simon Cooksey 0:fb7af294d5d9 175 unsigned char iv[16],
Simon Cooksey 0:fb7af294d5d9 176 const unsigned char *input,
Simon Cooksey 0:fb7af294d5d9 177 unsigned char *output );
Simon Cooksey 0:fb7af294d5d9 178 #endif /* MBEDTLS_CIPHER_MODE_CFB */
Simon Cooksey 0:fb7af294d5d9 179
Simon Cooksey 0:fb7af294d5d9 180 #if defined(MBEDTLS_CIPHER_MODE_CTR)
Simon Cooksey 0:fb7af294d5d9 181 /**
Simon Cooksey 0:fb7af294d5d9 182 * \brief CAMELLIA-CTR buffer encryption/decryption
Simon Cooksey 0:fb7af294d5d9 183 *
Simon Cooksey 0:fb7af294d5d9 184 * Warning: You have to keep the maximum use of your counter in mind!
Simon Cooksey 0:fb7af294d5d9 185 *
Simon Cooksey 0:fb7af294d5d9 186 * Note: Due to the nature of CTR you should use the same key schedule for
Simon Cooksey 0:fb7af294d5d9 187 * both encryption and decryption. So a context initialized with
Simon Cooksey 0:fb7af294d5d9 188 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT.
Simon Cooksey 0:fb7af294d5d9 189 *
Simon Cooksey 0:fb7af294d5d9 190 * \param ctx CAMELLIA context
Simon Cooksey 0:fb7af294d5d9 191 * \param length The length of the data
Simon Cooksey 0:fb7af294d5d9 192 * \param nc_off The offset in the current stream_block (for resuming
Simon Cooksey 0:fb7af294d5d9 193 * within current cipher stream). The offset pointer to
Simon Cooksey 0:fb7af294d5d9 194 * should be 0 at the start of a stream.
Simon Cooksey 0:fb7af294d5d9 195 * \param nonce_counter The 128-bit nonce and counter.
Simon Cooksey 0:fb7af294d5d9 196 * \param stream_block The saved stream-block for resuming. Is overwritten
Simon Cooksey 0:fb7af294d5d9 197 * by the function.
Simon Cooksey 0:fb7af294d5d9 198 * \param input The input data stream
Simon Cooksey 0:fb7af294d5d9 199 * \param output The output data stream
Simon Cooksey 0:fb7af294d5d9 200 *
Simon Cooksey 0:fb7af294d5d9 201 * \return 0 if successful
Simon Cooksey 0:fb7af294d5d9 202 */
Simon Cooksey 0:fb7af294d5d9 203 int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
Simon Cooksey 0:fb7af294d5d9 204 size_t length,
Simon Cooksey 0:fb7af294d5d9 205 size_t *nc_off,
Simon Cooksey 0:fb7af294d5d9 206 unsigned char nonce_counter[16],
Simon Cooksey 0:fb7af294d5d9 207 unsigned char stream_block[16],
Simon Cooksey 0:fb7af294d5d9 208 const unsigned char *input,
Simon Cooksey 0:fb7af294d5d9 209 unsigned char *output );
Simon Cooksey 0:fb7af294d5d9 210 #endif /* MBEDTLS_CIPHER_MODE_CTR */
Simon Cooksey 0:fb7af294d5d9 211
Simon Cooksey 0:fb7af294d5d9 212 #ifdef __cplusplus
Simon Cooksey 0:fb7af294d5d9 213 }
Simon Cooksey 0:fb7af294d5d9 214 #endif
Simon Cooksey 0:fb7af294d5d9 215
Simon Cooksey 0:fb7af294d5d9 216 #else /* MBEDTLS_CAMELLIA_ALT */
Simon Cooksey 0:fb7af294d5d9 217 #include "camellia_alt.h"
Simon Cooksey 0:fb7af294d5d9 218 #endif /* MBEDTLS_CAMELLIA_ALT */
Simon Cooksey 0:fb7af294d5d9 219
Simon Cooksey 0:fb7af294d5d9 220 #ifdef __cplusplus
Simon Cooksey 0:fb7af294d5d9 221 extern "C" {
Simon Cooksey 0:fb7af294d5d9 222 #endif
Simon Cooksey 0:fb7af294d5d9 223
Simon Cooksey 0:fb7af294d5d9 224 /**
Simon Cooksey 0:fb7af294d5d9 225 * \brief Checkup routine
Simon Cooksey 0:fb7af294d5d9 226 *
Simon Cooksey 0:fb7af294d5d9 227 * \return 0 if successful, or 1 if the test failed
Simon Cooksey 0:fb7af294d5d9 228 */
Simon Cooksey 0:fb7af294d5d9 229 int mbedtls_camellia_self_test( int verbose );
Simon Cooksey 0:fb7af294d5d9 230
Simon Cooksey 0:fb7af294d5d9 231 #ifdef __cplusplus
Simon Cooksey 0:fb7af294d5d9 232 }
Simon Cooksey 0:fb7af294d5d9 233 #endif
Simon Cooksey 0:fb7af294d5d9 234
Simon Cooksey 0:fb7af294d5d9 235 #endif /* camellia.h */