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