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