• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *  This file is provided under the Apache License 2.0, or the
11  *  GNU General Public License v2.0 or later.
12  *
13  *  **********
14  *  Apache License 2.0:
15  *
16  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
17  *  not use this file except in compliance with the License.
18  *  You may obtain a copy of the License at
19  *
20  *  http://www.apache.org/licenses/LICENSE-2.0
21  *
22  *  Unless required by applicable law or agreed to in writing, software
23  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  *  See the License for the specific language governing permissions and
26  *  limitations under the License.
27  *
28  *  **********
29  *
30  *  **********
31  *  GNU General Public License v2.0 or later:
32  *
33  *  This program is free software; you can redistribute it and/or modify
34  *  it under the terms of the GNU General Public License as published by
35  *  the Free Software Foundation; either version 2 of the License, or
36  *  (at your option) any later version.
37  *
38  *  This program is distributed in the hope that it will be useful,
39  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
40  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41  *  GNU General Public License for more details.
42  *
43  *  You should have received a copy of the GNU General Public License along
44  *  with this program; if not, write to the Free Software Foundation, Inc.,
45  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46  *
47  *  **********
48  */
49 #ifndef MBEDTLS_CAMELLIA_H
50 #define MBEDTLS_CAMELLIA_H
51 
52 #if !defined(MBEDTLS_CONFIG_FILE)
53 #include "config.h"
54 #else
55 #include MBEDTLS_CONFIG_FILE
56 #endif
57 
58 #include <stddef.h>
59 #include <stdint.h>
60 
61 #include "platform_util.h"
62 
63 #define MBEDTLS_CAMELLIA_ENCRYPT     1
64 #define MBEDTLS_CAMELLIA_DECRYPT     0
65 
66 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
67 #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH   MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0024 )
68 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
69 #define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024 /**< Bad input data. */
70 
71 #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
72 
73 /* MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED is deprecated and should not be used.
74  */
75 #define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED              -0x0027  /**< Camellia hardware accelerator failed. */
76 
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
80 
81 #if !defined(MBEDTLS_CAMELLIA_ALT)
82 // Regular implementation
83 //
84 
85 /**
86  * \brief          CAMELLIA context structure
87  */
88 typedef struct mbedtls_camellia_context
89 {
90     int nr;                     /*!<  number of rounds  */
91     uint32_t rk[68];            /*!<  CAMELLIA round keys    */
92 }
93 mbedtls_camellia_context;
94 
95 #else  /* MBEDTLS_CAMELLIA_ALT */
96 #include "camellia_alt.h"
97 #endif /* MBEDTLS_CAMELLIA_ALT */
98 
99 /**
100  * \brief          Initialize a CAMELLIA context.
101  *
102  * \param ctx      The CAMELLIA context to be initialized.
103  *                 This must not be \c NULL.
104  */
105 void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
106 
107 /**
108  * \brief          Clear a CAMELLIA context.
109  *
110  * \param ctx      The CAMELLIA context to be cleared. This may be \c NULL,
111  *                 in which case this function returns immediately. If it is not
112  *                 \c NULL, it must be initialized.
113  */
114 void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
115 
116 /**
117  * \brief          Perform a CAMELLIA key schedule operation for encryption.
118  *
119  * \param ctx      The CAMELLIA context to use. This must be initialized.
120  * \param key      The encryption key to use. This must be a readable buffer
121  *                 of size \p keybits Bits.
122  * \param keybits  The length of \p key in Bits. This must be either \c 128,
123  *                 \c 192 or \c 256.
124  *
125  * \return         \c 0 if successful.
126  * \return         A negative error code on failure.
127  */
128 int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx,
129                                  const unsigned char *key,
130                                  unsigned int keybits );
131 
132 /**
133  * \brief          Perform a CAMELLIA key schedule operation for decryption.
134  *
135  * \param ctx      The CAMELLIA context to use. This must be initialized.
136  * \param key      The decryption key. This must be a readable buffer
137  *                 of size \p keybits Bits.
138  * \param keybits  The length of \p key in Bits. This must be either \c 128,
139  *                 \c 192 or \c 256.
140  *
141  * \return         \c 0 if successful.
142  * \return         A negative error code on failure.
143  */
144 int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx,
145                                  const unsigned char *key,
146                                  unsigned int keybits );
147 
148 /**
149  * \brief          Perform a CAMELLIA-ECB block encryption/decryption operation.
150  *
151  * \param ctx      The CAMELLIA context to use. This must be initialized
152  *                 and bound to a key.
153  * \param mode     The mode of operation. This must be either
154  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
155  * \param input    The input block. This must be a readable buffer
156  *                 of size \c 16 Bytes.
157  * \param output   The output block. This must be a writable buffer
158  *                 of size \c 16 Bytes.
159  *
160  * \return         \c 0 if successful.
161  * \return         A negative error code on failure.
162  */
163 int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
164                     int mode,
165                     const unsigned char input[16],
166                     unsigned char output[16] );
167 
168 #if defined(MBEDTLS_CIPHER_MODE_CBC)
169 /**
170  * \brief          Perform a CAMELLIA-CBC buffer encryption/decryption operation.
171  *
172  * \note           Upon exit, the content of the IV is updated so that you can
173  *                 call the function same function again on the following
174  *                 block(s) of data and get the same result as if it was
175  *                 encrypted in one call. This allows a "streaming" usage.
176  *                 If on the other hand you need to retain the contents of the
177  *                 IV, you should either save it manually or use the cipher
178  *                 module instead.
179  *
180  * \param ctx      The CAMELLIA context to use. This must be initialized
181  *                 and bound to a key.
182  * \param mode     The mode of operation. This must be either
183  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
184  * \param length   The length in Bytes of the input data \p input.
185  *                 This must be a multiple of \c 16 Bytes.
186  * \param iv       The initialization vector. This must be a read/write buffer
187  *                 of length \c 16 Bytes. It is updated to allow streaming
188  *                 use as explained above.
189  * \param input    The buffer holding the input data. This must point to a
190  *                 readable buffer of length \p length Bytes.
191  * \param output   The buffer holding the output data. This must point to a
192  *                 writable buffer of length \p length Bytes.
193  *
194  * \return         \c 0 if successful.
195  * \return         A negative error code on failure.
196  */
197 int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
198                     int mode,
199                     size_t length,
200                     unsigned char iv[16],
201                     const unsigned char *input,
202                     unsigned char *output );
203 #endif /* MBEDTLS_CIPHER_MODE_CBC */
204 
205 #if defined(MBEDTLS_CIPHER_MODE_CFB)
206 /**
207  * \brief          Perform a CAMELLIA-CFB128 buffer encryption/decryption
208  *                 operation.
209  *
210  * \note           Due to the nature of CFB 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
213  *                 mbedtls_camellia_setkey_enc() regardless of whether \p mode
214  *                 is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
215  *
216  * \note           Upon exit, the content of the IV is updated so that you can
217  *                 call the function same function again on the following
218  *                 block(s) of data and get the same result as if it was
219  *                 encrypted in one call. This allows a "streaming" usage.
220  *                 If on the other hand you need to retain the contents of the
221  *                 IV, you should either save it manually or use the cipher
222  *                 module instead.
223  *
224  * \param ctx      The CAMELLIA context to use. This must be initialized
225  *                 and bound to a key.
226  * \param mode     The mode of operation. This must be either
227  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
228  * \param length   The length of the input data \p input. Any value is allowed.
229  * \param iv_off   The current offset in the IV. This must be smaller
230  *                 than \c 16 Bytes. It is updated after this call to allow
231  *                 the aforementioned streaming usage.
232  * \param iv       The initialization vector. This must be a read/write buffer
233  *                 of length \c 16 Bytes. It is updated after this call to
234  *                 allow the aforementioned streaming usage.
235  * \param input    The buffer holding the input data. This must be a readable
236  *                 buffer of size \p length Bytes.
237  * \param output   The buffer to hold the output data. This must be a writable
238  *                 buffer of length \p length Bytes.
239  *
240  * \return         \c 0 if successful.
241  * \return         A negative error code on failure.
242  */
243 int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
244                        int mode,
245                        size_t length,
246                        size_t *iv_off,
247                        unsigned char iv[16],
248                        const unsigned char *input,
249                        unsigned char *output );
250 #endif /* MBEDTLS_CIPHER_MODE_CFB */
251 
252 #if defined(MBEDTLS_CIPHER_MODE_CTR)
253 /**
254  * \brief      Perform a CAMELLIA-CTR buffer encryption/decryption operation.
255  *
256  * *note       Due to the nature of CTR mode, you should use the same
257  *             key for both encryption and decryption. In particular, calls
258  *             to this function should be preceded by a key-schedule via
259  *             mbedtls_camellia_setkey_enc() regardless of whether \p mode
260  *             is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
261  *
262  * \warning    You must never reuse a nonce value with the same key. Doing so
263  *             would void the encryption for the two messages encrypted with
264  *             the same nonce and key.
265  *
266  *             There are two common strategies for managing nonces with CTR:
267  *
268  *             1. You can handle everything as a single message processed over
269  *             successive calls to this function. In that case, you want to
270  *             set \p nonce_counter and \p nc_off to 0 for the first call, and
271  *             then preserve the values of \p nonce_counter, \p nc_off and \p
272  *             stream_block across calls to this function as they will be
273  *             updated by this function.
274  *
275  *             With this strategy, you must not encrypt more than 2**128
276  *             blocks of data with the same key.
277  *
278  *             2. You can encrypt separate messages by dividing the \p
279  *             nonce_counter buffer in two areas: the first one used for a
280  *             per-message nonce, handled by yourself, and the second one
281  *             updated by this function internally.
282  *
283  *             For example, you might reserve the first \c 12 Bytes for the
284  *             per-message nonce, and the last \c 4 Bytes for internal use.
285  *             In that case, before calling this function on a new message you
286  *             need to set the first \c 12 Bytes of \p nonce_counter to your
287  *             chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
288  *             (which will cause \p stream_block to be ignored). That way, you
289  *             can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
290  *             each  with the same key.
291  *
292  *             The per-message nonce (or information sufficient to reconstruct
293  *             it) needs to be communicated with the ciphertext and must be
294  *             unique. The recommended way to ensure uniqueness is to use a
295  *             message counter. An alternative is to generate random nonces,
296  *             but this limits the number of messages that can be securely
297  *             encrypted: for example, with 96-bit random nonces, you should
298  *             not encrypt more than 2**32 messages with the same key.
299  *
300  *             Note that for both stategies, sizes are measured in blocks and
301  *             that a CAMELLIA block is \c 16 Bytes.
302  *
303  * \warning    Upon return, \p stream_block contains sensitive data. Its
304  *             content must not be written to insecure storage and should be
305  *             securely discarded as soon as it's no longer needed.
306  *
307  * \param ctx           The CAMELLIA context to use. This must be initialized
308  *                      and bound to a key.
309  * \param length        The length of the input data \p input in Bytes.
310  *                      Any value is allowed.
311  * \param nc_off        The offset in the current \p stream_block (for resuming
312  *                      within current cipher stream). The offset pointer to
313  *                      should be \c 0 at the start of a stream. It is updated
314  *                      at the end of this call.
315  * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
316  *                      buffer of length \c 16 Bytes.
317  * \param stream_block  The saved stream-block for resuming. This must be a
318  *                      read/write buffer of length \c 16 Bytes.
319  * \param input         The input data stream. This must be a readable buffer of
320  *                      size \p length Bytes.
321  * \param output        The output data stream. This must be a writable buffer
322  *                      of size \p length Bytes.
323  *
324  * \return              \c 0 if successful.
325  * \return              A negative error code on failure.
326  */
327 int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
328                        size_t length,
329                        size_t *nc_off,
330                        unsigned char nonce_counter[16],
331                        unsigned char stream_block[16],
332                        const unsigned char *input,
333                        unsigned char *output );
334 #endif /* MBEDTLS_CIPHER_MODE_CTR */
335 
336 #if defined(MBEDTLS_SELF_TEST)
337 
338 /**
339  * \brief          Checkup routine
340  *
341  * \return         0 if successful, or 1 if the test failed
342  */
343 int mbedtls_camellia_self_test( int verbose );
344 
345 #endif /* MBEDTLS_SELF_TEST */
346 
347 #ifdef __cplusplus
348 }
349 #endif
350 
351 #endif /* camellia.h */
352