Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file camellia.h |
| 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief Camellia block cipher |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 21 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #ifndef MBEDTLS_CAMELLIA_H |
| 23 | #define MBEDTLS_CAMELLIA_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 24 | #include "mbedtls/private_access.h" |
Paul Bakker | 477fd32 | 2009-10-04 13:22:13 +0000 | [diff] [blame] | 25 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 26 | #include "mbedtls/build_info.h" |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 27 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 28 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 29 | #include <stdint.h> |
Paul Bakker | c81f6c3 | 2009-05-03 13:09:15 +0000 | [diff] [blame] | 30 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 31 | #include "mbedtls/platform_util.h" |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #define MBEDTLS_CAMELLIA_ENCRYPT 1 |
| 34 | #define MBEDTLS_CAMELLIA_DECRYPT 0 |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 35 | |
Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 36 | /** Bad input data. */ |
| 37 | #define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024 |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 38 | |
Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 39 | /** Invalid data input length. */ |
| 40 | #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 41 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 42 | #ifdef __cplusplus |
| 43 | extern "C" { |
| 44 | #endif |
| 45 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 46 | #if !defined(MBEDTLS_CAMELLIA_ALT) |
| 47 | // Regular implementation |
| 48 | // |
| 49 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 50 | /** |
| 51 | * \brief CAMELLIA context structure |
| 52 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 53 | typedef struct mbedtls_camellia_context { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 54 | int MBEDTLS_PRIVATE(nr); /*!< number of rounds */ |
| 55 | uint32_t MBEDTLS_PRIVATE(rk)[68]; /*!< CAMELLIA round keys */ |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 56 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | mbedtls_camellia_context; |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 58 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 59 | #else /* MBEDTLS_CAMELLIA_ALT */ |
| 60 | #include "camellia_alt.h" |
| 61 | #endif /* MBEDTLS_CAMELLIA_ALT */ |
| 62 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 63 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 64 | * \brief Initialize a CAMELLIA context. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 65 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 66 | * \param ctx The CAMELLIA context to be initialized. |
| 67 | * This must not be \c NULL. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 68 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | void mbedtls_camellia_init(mbedtls_camellia_context *ctx); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 70 | |
| 71 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 72 | * \brief Clear a CAMELLIA context. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 73 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 74 | * \param ctx The CAMELLIA context to be cleared. This may be \c NULL, |
| 75 | * in which case this function returns immediately. If it is not |
| 76 | * \c NULL, it must be initialized. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 77 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | void mbedtls_camellia_free(mbedtls_camellia_context *ctx); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 79 | |
| 80 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 81 | * \brief Perform a CAMELLIA key schedule operation for encryption. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 82 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 83 | * \param ctx The CAMELLIA context to use. This must be initialized. |
| 84 | * \param key The encryption key to use. This must be a readable buffer |
| 85 | * of size \p keybits Bits. |
| 86 | * \param keybits The length of \p key in Bits. This must be either \c 128, |
| 87 | * \c 192 or \c 256. |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 88 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 89 | * \return \c 0 if successful. |
| 90 | * \return A negative error code on failure. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 91 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx, |
| 93 | const unsigned char *key, |
| 94 | unsigned int keybits); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 97 | * \brief Perform a CAMELLIA key schedule operation for decryption. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 98 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 99 | * \param ctx The CAMELLIA context to use. This must be initialized. |
| 100 | * \param key The decryption key. This must be a readable buffer |
| 101 | * of size \p keybits Bits. |
| 102 | * \param keybits The length of \p key in Bits. This must be either \c 128, |
| 103 | * \c 192 or \c 256. |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 104 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 105 | * \return \c 0 if successful. |
| 106 | * \return A negative error code on failure. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 107 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | int mbedtls_camellia_setkey_dec(mbedtls_camellia_context *ctx, |
| 109 | const unsigned char *key, |
| 110 | unsigned int keybits); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 111 | |
| 112 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 113 | * \brief Perform a CAMELLIA-ECB block encryption/decryption operation. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 114 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 115 | * \param ctx The CAMELLIA context to use. This must be initialized |
| 116 | * and bound to a key. |
| 117 | * \param mode The mode of operation. This must be either |
| 118 | * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. |
| 119 | * \param input The input block. This must be a readable buffer |
| 120 | * of size \c 16 Bytes. |
| 121 | * \param output The output block. This must be a writable buffer |
| 122 | * of size \c 16 Bytes. |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 123 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 124 | * \return \c 0 if successful. |
| 125 | * \return A negative error code on failure. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 126 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | int mbedtls_camellia_crypt_ecb(mbedtls_camellia_context *ctx, |
| 128 | int mode, |
| 129 | const unsigned char input[16], |
| 130 | unsigned char output[16]); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 131 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 133 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 134 | * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 135 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 136 | * \note Upon exit, the content of the IV is updated so that you can |
| 137 | * call the function same function again on the following |
| 138 | * block(s) of data and get the same result as if it was |
| 139 | * encrypted in one call. This allows a "streaming" usage. |
| 140 | * If on the other hand you need to retain the contents of the |
| 141 | * IV, you should either save it manually or use the cipher |
| 142 | * module instead. |
| 143 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 144 | * \param ctx The CAMELLIA context to use. This must be initialized |
| 145 | * and bound to a key. |
| 146 | * \param mode The mode of operation. This must be either |
| 147 | * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. |
| 148 | * \param length The length in Bytes of the input data \p input. |
| 149 | * This must be a multiple of \c 16 Bytes. |
| 150 | * \param iv The initialization vector. This must be a read/write buffer |
| 151 | * of length \c 16 Bytes. It is updated to allow streaming |
| 152 | * use as explained above. |
| 153 | * \param input The buffer holding the input data. This must point to a |
| 154 | * readable buffer of length \p length Bytes. |
| 155 | * \param output The buffer holding the output data. This must point to a |
| 156 | * writable buffer of length \p length Bytes. |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 157 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 158 | * \return \c 0 if successful. |
| 159 | * \return A negative error code on failure. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 160 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | int mbedtls_camellia_crypt_cbc(mbedtls_camellia_context *ctx, |
| 162 | int mode, |
| 163 | size_t length, |
| 164 | unsigned char iv[16], |
| 165 | const unsigned char *input, |
| 166 | unsigned char *output); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 168 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 170 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 171 | * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption |
| 172 | * operation. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 173 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 174 | * \note Due to the nature of CFB mode, you should use the same |
| 175 | * key for both encryption and decryption. In particular, calls |
| 176 | * to this function should be preceded by a key-schedule via |
| 177 | * mbedtls_camellia_setkey_enc() regardless of whether \p mode |
| 178 | * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 179 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 180 | * \note Upon exit, the content of the IV is updated so that you can |
| 181 | * call the function same function again on the following |
| 182 | * block(s) of data and get the same result as if it was |
| 183 | * encrypted in one call. This allows a "streaming" usage. |
| 184 | * If on the other hand you need to retain the contents of the |
| 185 | * IV, you should either save it manually or use the cipher |
| 186 | * module instead. |
| 187 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 188 | * \param ctx The CAMELLIA context to use. This must be initialized |
| 189 | * and bound to a key. |
| 190 | * \param mode The mode of operation. This must be either |
| 191 | * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. |
| 192 | * \param length The length of the input data \p input. Any value is allowed. |
| 193 | * \param iv_off The current offset in the IV. This must be smaller |
| 194 | * than \c 16 Bytes. It is updated after this call to allow |
| 195 | * the aforementioned streaming usage. |
| 196 | * \param iv The initialization vector. This must be a read/write buffer |
| 197 | * of length \c 16 Bytes. It is updated after this call to |
| 198 | * allow the aforementioned streaming usage. |
| 199 | * \param input The buffer holding the input data. This must be a readable |
| 200 | * buffer of size \p length Bytes. |
| 201 | * \param output The buffer to hold the output data. This must be a writable |
| 202 | * buffer of length \p length Bytes. |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 203 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 204 | * \return \c 0 if successful. |
| 205 | * \return A negative error code on failure. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 206 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | int mbedtls_camellia_crypt_cfb128(mbedtls_camellia_context *ctx, |
| 208 | int mode, |
| 209 | size_t length, |
| 210 | size_t *iv_off, |
| 211 | unsigned char iv[16], |
| 212 | const unsigned char *input, |
| 213 | unsigned char *output); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 214 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 217 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 218 | * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation. |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 219 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 220 | * *note Due to the nature of CTR mode, you should use the same |
| 221 | * key for both encryption and decryption. In particular, calls |
| 222 | * to this function should be preceded by a key-schedule via |
Andrzej Kurek | 377eb5f | 2023-05-06 09:57:40 -0400 | [diff] [blame^] | 223 | * mbedtls_camellia_setkey_enc() regardless of whether the mode |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 224 | * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 225 | * |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 226 | * \warning You must never reuse a nonce value with the same key. Doing so |
| 227 | * would void the encryption for the two messages encrypted with |
| 228 | * the same nonce and key. |
| 229 | * |
| 230 | * There are two common strategies for managing nonces with CTR: |
| 231 | * |
Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 232 | * 1. You can handle everything as a single message processed over |
| 233 | * successive calls to this function. In that case, you want to |
| 234 | * set \p nonce_counter and \p nc_off to 0 for the first call, and |
| 235 | * then preserve the values of \p nonce_counter, \p nc_off and \p |
| 236 | * stream_block across calls to this function as they will be |
| 237 | * updated by this function. |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 238 | * |
Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 239 | * With this strategy, you must not encrypt more than 2**128 |
| 240 | * blocks of data with the same key. |
| 241 | * |
| 242 | * 2. You can encrypt separate messages by dividing the \p |
| 243 | * nonce_counter buffer in two areas: the first one used for a |
| 244 | * per-message nonce, handled by yourself, and the second one |
| 245 | * updated by this function internally. |
| 246 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 247 | * For example, you might reserve the first \c 12 Bytes for the |
| 248 | * per-message nonce, and the last \c 4 Bytes for internal use. |
| 249 | * In that case, before calling this function on a new message you |
| 250 | * need to set the first \c 12 Bytes of \p nonce_counter to your |
| 251 | * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0 |
| 252 | * (which will cause \p stream_block to be ignored). That way, you |
| 253 | * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks |
| 254 | * each with the same key. |
Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 255 | * |
| 256 | * The per-message nonce (or information sufficient to reconstruct |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 257 | * it) needs to be communicated with the ciphertext and must be |
| 258 | * unique. The recommended way to ensure uniqueness is to use a |
| 259 | * message counter. An alternative is to generate random nonces, |
| 260 | * but this limits the number of messages that can be securely |
| 261 | * encrypted: for example, with 96-bit random nonces, you should |
| 262 | * not encrypt more than 2**32 messages with the same key. |
Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 263 | * |
Tom Cosgrove | 1e21144 | 2022-05-26 11:51:00 +0100 | [diff] [blame] | 264 | * Note that for both strategies, sizes are measured in blocks and |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 265 | * that a CAMELLIA block is \c 16 Bytes. |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 266 | * |
Manuel Pégourié-Gonnard | fa0c47d | 2018-05-24 19:02:06 +0200 | [diff] [blame] | 267 | * \warning Upon return, \p stream_block contains sensitive data. Its |
| 268 | * content must not be written to insecure storage and should be |
| 269 | * securely discarded as soon as it's no longer needed. |
| 270 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 271 | * \param ctx The CAMELLIA context to use. This must be initialized |
| 272 | * and bound to a key. |
| 273 | * \param length The length of the input data \p input in Bytes. |
| 274 | * Any value is allowed. |
| 275 | * \param nc_off The offset in the current \p stream_block (for resuming |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 276 | * within current cipher stream). The offset pointer to |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 277 | * should be \c 0 at the start of a stream. It is updated |
| 278 | * at the end of this call. |
| 279 | * \param nonce_counter The 128-bit nonce and counter. This must be a read/write |
| 280 | * buffer of length \c 16 Bytes. |
| 281 | * \param stream_block The saved stream-block for resuming. This must be a |
| 282 | * read/write buffer of length \c 16 Bytes. |
| 283 | * \param input The input data stream. This must be a readable buffer of |
| 284 | * size \p length Bytes. |
| 285 | * \param output The output data stream. This must be a writable buffer |
| 286 | * of size \p length Bytes. |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 287 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 288 | * \return \c 0 if successful. |
| 289 | * \return A negative error code on failure. |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 290 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | int mbedtls_camellia_crypt_ctr(mbedtls_camellia_context *ctx, |
| 292 | size_t length, |
| 293 | size_t *nc_off, |
| 294 | unsigned char nonce_counter[16], |
| 295 | unsigned char stream_block[16], |
| 296 | const unsigned char *input, |
| 297 | unsigned char *output); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 299 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 300 | #if defined(MBEDTLS_SELF_TEST) |
| 301 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 302 | /** |
| 303 | * \brief Checkup routine |
| 304 | * |
| 305 | * \return 0 if successful, or 1 if the test failed |
| 306 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | int mbedtls_camellia_self_test(int verbose); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 308 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 309 | #endif /* MBEDTLS_SELF_TEST */ |
| 310 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 311 | #ifdef __cplusplus |
| 312 | } |
| 313 | #endif |
| 314 | |
| 315 | #endif /* camellia.h */ |