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