recover_pk Flake8 ignore E501
[RRG-proxmark3.git] / common / mbedtls / camellia.h
blob38154f81aac07de7fcc8dce50e5ae5f296883bd2
1 /**
2 * \file camellia.h
4 * \brief Camellia block cipher
5 */
6 /*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #ifndef MBEDTLS_CAMELLIA_H
23 #define MBEDTLS_CAMELLIA_H
25 #if !defined(MBEDTLS_CONFIG_FILE)
26 #include "mbedtls/config.h"
27 #else
28 #include MBEDTLS_CONFIG_FILE
29 #endif
31 #include <stddef.h>
32 #include <stdint.h>
34 #include "mbedtls/platform_util.h"
36 #define MBEDTLS_CAMELLIA_ENCRYPT 1
37 #define MBEDTLS_CAMELLIA_DECRYPT 0
39 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
40 #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0024 )
41 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
42 #define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024 /**< Bad input data. */
44 #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
46 /* MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED is deprecated and should not be used.
48 #define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027 /**< Camellia hardware accelerator failed. */
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
54 #if !defined(MBEDTLS_CAMELLIA_ALT)
55 // Regular implementation
58 /**
59 * \brief CAMELLIA context structure
61 typedef struct mbedtls_camellia_context {
62 int nr; /*!< number of rounds */
63 uint32_t rk[68]; /*!< CAMELLIA round keys */
65 mbedtls_camellia_context;
67 #else /* MBEDTLS_CAMELLIA_ALT */
68 #include "camellia_alt.h"
69 #endif /* MBEDTLS_CAMELLIA_ALT */
71 /**
72 * \brief Initialize a CAMELLIA context.
74 * \param ctx The CAMELLIA context to be initialized.
75 * This must not be \c NULL.
77 void mbedtls_camellia_init(mbedtls_camellia_context *ctx);
79 /**
80 * \brief Clear a CAMELLIA context.
82 * \param ctx The CAMELLIA context to be cleared. This may be \c NULL,
83 * in which case this function returns immediately. If it is not
84 * \c NULL, it must be initialized.
86 void mbedtls_camellia_free(mbedtls_camellia_context *ctx);
88 /**
89 * \brief Perform a CAMELLIA key schedule operation for encryption.
91 * \param ctx The CAMELLIA context to use. This must be initialized.
92 * \param key The encryption key to use. This must be a readable buffer
93 * of size \p keybits Bits.
94 * \param keybits The length of \p key in Bits. This must be either \c 128,
95 * \c 192 or \c 256.
97 * \return \c 0 if successful.
98 * \return A negative error code on failure.
100 int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx,
101 const unsigned char *key,
102 unsigned int keybits);
105 * \brief Perform a CAMELLIA key schedule operation for decryption.
107 * \param ctx The CAMELLIA context to use. This must be initialized.
108 * \param key The decryption key. This must be a readable buffer
109 * of size \p keybits Bits.
110 * \param keybits The length of \p key in Bits. This must be either \c 128,
111 * \c 192 or \c 256.
113 * \return \c 0 if successful.
114 * \return A negative error code on failure.
116 int mbedtls_camellia_setkey_dec(mbedtls_camellia_context *ctx,
117 const unsigned char *key,
118 unsigned int keybits);
121 * \brief Perform a CAMELLIA-ECB block encryption/decryption operation.
123 * \param ctx The CAMELLIA context to use. This must be initialized
124 * and bound to a key.
125 * \param mode The mode of operation. This must be either
126 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
127 * \param input The input block. This must be a readable buffer
128 * of size \c 16 Bytes.
129 * \param output The output block. This must be a writable buffer
130 * of size \c 16 Bytes.
132 * \return \c 0 if successful.
133 * \return A negative error code on failure.
135 int mbedtls_camellia_crypt_ecb(mbedtls_camellia_context *ctx,
136 int mode,
137 const unsigned char input[16],
138 unsigned char output[16]);
140 #if defined(MBEDTLS_CIPHER_MODE_CBC)
142 * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation.
144 * \note Upon exit, the content of the IV is updated so that you can
145 * call the function same function again on the following
146 * block(s) of data and get the same result as if it was
147 * encrypted in one call. This allows a "streaming" usage.
148 * If on the other hand you need to retain the contents of the
149 * IV, you should either save it manually or use the cipher
150 * module instead.
152 * \param ctx The CAMELLIA context to use. This must be initialized
153 * and bound to a key.
154 * \param mode The mode of operation. This must be either
155 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
156 * \param length The length in Bytes of the input data \p input.
157 * This must be a multiple of \c 16 Bytes.
158 * \param iv The initialization vector. This must be a read/write buffer
159 * of length \c 16 Bytes. It is updated to allow streaming
160 * use as explained above.
161 * \param input The buffer holding the input data. This must point to a
162 * readable buffer of length \p length Bytes.
163 * \param output The buffer holding the output data. This must point to a
164 * writable buffer of length \p length Bytes.
166 * \return \c 0 if successful.
167 * \return A negative error code on failure.
169 int mbedtls_camellia_crypt_cbc(mbedtls_camellia_context *ctx,
170 int mode,
171 size_t length,
172 unsigned char iv[16],
173 const unsigned char *input,
174 unsigned char *output);
175 #endif /* MBEDTLS_CIPHER_MODE_CBC */
177 #if defined(MBEDTLS_CIPHER_MODE_CFB)
179 * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption
180 * operation.
182 * \note Due to the nature of CFB mode, you should use the same
183 * key for both encryption and decryption. In particular, calls
184 * to this function should be preceded by a key-schedule via
185 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
186 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
188 * \note Upon exit, the content of the IV is updated so that you can
189 * call the function same function again on the following
190 * block(s) of data and get the same result as if it was
191 * encrypted in one call. This allows a "streaming" usage.
192 * If on the other hand you need to retain the contents of the
193 * IV, you should either save it manually or use the cipher
194 * module instead.
196 * \param ctx The CAMELLIA context to use. This must be initialized
197 * and bound to a key.
198 * \param mode The mode of operation. This must be either
199 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
200 * \param length The length of the input data \p input. Any value is allowed.
201 * \param iv_off The current offset in the IV. This must be smaller
202 * than \c 16 Bytes. It is updated after this call to allow
203 * the aforementioned streaming usage.
204 * \param iv The initialization vector. This must be a read/write buffer
205 * of length \c 16 Bytes. It is updated after this call to
206 * allow the aforementioned streaming usage.
207 * \param input The buffer holding the input data. This must be a readable
208 * buffer of size \p length Bytes.
209 * \param output The buffer to hold the output data. This must be a writable
210 * buffer of length \p length Bytes.
212 * \return \c 0 if successful.
213 * \return A negative error code on failure.
215 int mbedtls_camellia_crypt_cfb128(mbedtls_camellia_context *ctx,
216 int mode,
217 size_t length,
218 size_t *iv_off,
219 unsigned char iv[16],
220 const unsigned char *input,
221 unsigned char *output);
222 #endif /* MBEDTLS_CIPHER_MODE_CFB */
224 #if defined(MBEDTLS_CIPHER_MODE_CTR)
226 * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation.
228 * *note Due to the nature of CTR mode, you should use the same
229 * key for both encryption and decryption. In particular, calls
230 * to this function should be preceded by a key-schedule via
231 * mbedtls_camellia_setkey_enc() regardless of whether \p mode
232 * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
234 * \warning You must never reuse a nonce value with the same key. Doing so
235 * would void the encryption for the two messages encrypted with
236 * the same nonce and key.
238 * There are two common strategies for managing nonces with CTR:
240 * 1. You can handle everything as a single message processed over
241 * successive calls to this function. In that case, you want to
242 * set \p nonce_counter and \p nc_off to 0 for the first call, and
243 * then preserve the values of \p nonce_counter, \p nc_off and \p
244 * stream_block across calls to this function as they will be
245 * updated by this function.
247 * With this strategy, you must not encrypt more than 2**128
248 * blocks of data with the same key.
250 * 2. You can encrypt separate messages by dividing the \p
251 * nonce_counter buffer in two areas: the first one used for a
252 * per-message nonce, handled by yourself, and the second one
253 * updated by this function internally.
255 * For example, you might reserve the first \c 12 Bytes for the
256 * per-message nonce, and the last \c 4 Bytes for internal use.
257 * In that case, before calling this function on a new message you
258 * need to set the first \c 12 Bytes of \p nonce_counter to your
259 * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
260 * (which will cause \p stream_block to be ignored). That way, you
261 * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
262 * each with the same key.
264 * The per-message nonce (or information sufficient to reconstruct
265 * it) needs to be communicated with the ciphertext and must be
266 * unique. The recommended way to ensure uniqueness is to use a
267 * message counter. An alternative is to generate random nonces,
268 * but this limits the number of messages that can be securely
269 * encrypted: for example, with 96-bit random nonces, you should
270 * not encrypt more than 2**32 messages with the same key.
272 * Note that for both stategies, sizes are measured in blocks and
273 * that a CAMELLIA block is \c 16 Bytes.
275 * \warning Upon return, \p stream_block contains sensitive data. Its
276 * content must not be written to insecure storage and should be
277 * securely discarded as soon as it's no longer needed.
279 * \param ctx The CAMELLIA context to use. This must be initialized
280 * and bound to a key.
281 * \param length The length of the input data \p input in Bytes.
282 * Any value is allowed.
283 * \param nc_off The offset in the current \p stream_block (for resuming
284 * within current cipher stream). The offset pointer to
285 * should be \c 0 at the start of a stream. It is updated
286 * at the end of this call.
287 * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
288 * buffer of length \c 16 Bytes.
289 * \param stream_block The saved stream-block for resuming. This must be a
290 * read/write buffer of length \c 16 Bytes.
291 * \param input The input data stream. This must be a readable buffer of
292 * size \p length Bytes.
293 * \param output The output data stream. This must be a writable buffer
294 * of size \p length Bytes.
296 * \return \c 0 if successful.
297 * \return A negative error code on failure.
299 int mbedtls_camellia_crypt_ctr(mbedtls_camellia_context *ctx,
300 size_t length,
301 size_t *nc_off,
302 unsigned char nonce_counter[16],
303 unsigned char stream_block[16],
304 const unsigned char *input,
305 unsigned char *output);
306 #endif /* MBEDTLS_CIPHER_MODE_CTR */
308 #if defined(MBEDTLS_SELF_TEST)
311 * \brief Checkup routine
313 * \return 0 if successful, or 1 if the test failed
315 int mbedtls_camellia_self_test(int verbose);
317 #endif /* MBEDTLS_SELF_TEST */
319 #ifdef __cplusplus
321 #endif
323 #endif /* camellia.h */