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