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