blob: d1a2a1c3ad99bf5d7b7935386bb970193655cc52 [file] [log] [blame]
Paul Bakker38119b12009-01-10 23:31:23 +00001/**
2 * \file camellia.h
3 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Camellia block cipher
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * 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.
Paul Bakkerb96f1542010-07-18 20:36:00 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker38119b12009-01-10 23:31:23 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_CAMELLIA_H
25#define MBEDTLS_CAMELLIA_H
Paul Bakker477fd322009-10-04 13:22:13 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Paul Bakkerc81f6c32009-05-03 13:09:15 +000035
Hanno Becker4c029d02018-12-17 13:20:05 +000036#include "platform_util.h"
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#define MBEDTLS_CAMELLIA_ENCRYPT 1
39#define MBEDTLS_CAMELLIA_DECRYPT 0
Paul Bakker38119b12009-01-10 23:31:23 +000040
Hanno Becker4c029d02018-12-17 13:20:05 +000041#if !defined(MBEDTLS_DEPRECATED_REMOVED)
42#define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0024 )
Hanno Becker4c029d02018-12-17 13:20:05 +000043#endif /* !MBEDTLS_DEPRECATED_REMOVED */
44#define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024 /**< Bad input data. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030045
Hanno Becker938f9e92018-12-18 09:40:25 +000046#define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
47
Ron Eldor9924bdc2018-10-04 10:59:13 +030048/* MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED is deprecated and should not be used.
49 */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010050#define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027 /**< Camellia hardware accelerator failed. */
Paul Bakker2b222c82009-07-27 21:03:45 +000051
Paul Bakker407a0da2013-06-27 14:29:21 +020052#ifdef __cplusplus
53extern "C" {
54#endif
55
Ron Eldorb2aacec2017-05-18 16:53:08 +030056#if !defined(MBEDTLS_CAMELLIA_ALT)
57// Regular implementation
58//
59
Paul Bakker38119b12009-01-10 23:31:23 +000060/**
61 * \brief CAMELLIA context structure
62 */
Dawid Drozd428cc522018-07-24 10:02:47 +020063typedef struct mbedtls_camellia_context
Paul Bakker38119b12009-01-10 23:31:23 +000064{
65 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000066 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000069
Ron Eldorb2aacec2017-05-18 16:53:08 +030070#else /* MBEDTLS_CAMELLIA_ALT */
71#include "camellia_alt.h"
72#endif /* MBEDTLS_CAMELLIA_ALT */
73
Paul Bakker38119b12009-01-10 23:31:23 +000074/**
Hanno Becker7a16aad2018-12-12 14:54:16 +000075 * \brief Initialize a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020076 *
Hanno Becker7a16aad2018-12-12 14:54:16 +000077 * \param ctx The CAMELLIA context to be initialized.
78 * This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020081
82/**
Hanno Becker7a16aad2018-12-12 14:54:16 +000083 * \brief Clear a CAMELLIA context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020084 *
Hanno Becker7a16aad2018-12-12 14:54:16 +000085 * \param ctx The CAMELLIA context to be cleared. May be \c NULL,
86 * in which case this function is a no-op. If it is not
87 * \c NULL, it must be initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020090
91/**
Hanno Becker7a16aad2018-12-12 14:54:16 +000092 * \brief Perform a CAMELLIA key schedule (encryption).
Paul Bakker38119b12009-01-10 23:31:23 +000093 *
Hanno Becker7a16aad2018-12-12 14:54:16 +000094 * \param ctx The CAMELLIA context to use. This must be initialized.
95 * \param key The encryption key to use. Must be a readable buffer
96 * of size \p keybits bits.
97 * \param keybits The length of \p key in Bits. Must be either \c 128,
98 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +020099 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000100 * \return \c 0 if successful.
101 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000102 */
Hanno Becker7a16aad2018-12-12 14:54:16 +0000103int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx,
104 const unsigned char *key,
105 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000106
107/**
Hanno Becker7a16aad2018-12-12 14:54:16 +0000108 * \brief Perform a CAMELLIA key schedule (decryption).
Paul Bakker38119b12009-01-10 23:31:23 +0000109 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000110 * \param ctx The CAMELLIA context to use. This must be initialized.
111 * \param key The decryption key. Must be a readable buffer
112 * of size \p keybits bits.
113 * \param keybits The length of \p key in Bits. Must be either \c 128,
114 * \c 192 or \c 256.
Paul Bakker9af723c2014-05-01 13:03:14 +0200115 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000116 * \return \c 0 if successful.
117 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000118 */
Hanno Becker7a16aad2018-12-12 14:54:16 +0000119int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx,
120 const unsigned char *key,
121 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000122
123/**
Hanno Becker7a16aad2018-12-12 14:54:16 +0000124 * \brief Perform a CAMELLIA-ECB block encryption/decryption.
Paul Bakker38119b12009-01-10 23:31:23 +0000125 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000126 * \param ctx The CAMELLIA context to use. Must be initialized
127 * and bound to a key.
128 * \param mode The mode of operation. Must be either
129 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
130 * \param input The input block. Must be a readable buffer
131 * of size \c 16 Bytes.
132 * \param output The output block. Must be a writable buffer
133 * of size \c 16 Bytes.
Paul Bakker9af723c2014-05-01 13:03:14 +0200134 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000135 * \return \c 0 if successful.
136 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000139 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000140 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000141 unsigned char output[16] );
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000144/**
Hanno Becker7a16aad2018-12-12 14:54:16 +0000145 * \brief Perform a CAMELLIA-CBC buffer encryption/decryption.
Paul Bakker38119b12009-01-10 23:31:23 +0000146 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000147 * \note Upon exit, the content of the IV is updated so that you can
148 * call the function same function again on the following
149 * block(s) of data and get the same result as if it was
150 * encrypted in one call. This allows a "streaming" usage.
151 * If on the other hand you need to retain the contents of the
152 * IV, you should either save it manually or use the cipher
153 * module instead.
154 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000155 * \param ctx The CAMELLIA context to use. Must be initialized
156 * and bound to a key.
157 * \param mode The mode of operation. Possible values are
158 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
159 * \param length The length in Bytes of the input data.
160 * Must be a multiple of \c 16.
161 * \param iv The initialization vector. This must be RW buffer
162 * of length \c 16 Bytes. It is updated to allow streaming
163 * use as explained above.
164 * \param input The buffer holding the input data. Must point to a readable
165 * buffer of length \p length Bytes. May be \c NULL if
166 * `length == 0`.
167 * \param input The buffer holding the output data. Must point to a writable
168 * buffer of length \p length Bytes. May be \c NULL if
169 * `length == 0`.
Paul Bakker9af723c2014-05-01 13:03:14 +0200170 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000171 * \return \c 0 if successful.
172 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000175 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000176 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000177 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000178 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000179 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000183/**
Hanno Becker7a16aad2018-12-12 14:54:16 +0000184 * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption.
Paul Bakker38119b12009-01-10 23:31:23 +0000185 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000186 * \note Due to the nature of CFB you should use the same key
187 * schedule for both encryption and decryption. So a
188 * context initialized with mbedtls_camellia_setkey_enc()
189 * for both #MBEDTLS_CAMELLIA_ENCRYPT and
190 * #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000191 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000192 * \note Upon exit, the content of the IV is updated so that you can
193 * call the function same function again on the following
194 * block(s) of data and get the same result as if it was
195 * encrypted in one call. This allows a "streaming" usage.
196 * If on the other hand you need to retain the contents of the
197 * IV, you should either save it manually or use the cipher
198 * module instead.
199 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000200 * \param ctx The CAMELLIA context to use. Must be initialized
201 * and bound to a key.
202 * \param mode The mode of operation. Possible values are
203 * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
204 * \param length The length of the input data. Any value is allowed.
205 * \param iv_off The current offset in the IV. This must be smaller
206 * than \c 16. It is updated after this call to allow
207 * the aforementioned streaming usage.
208 * \param iv The initialization vector. Must be an RW buffer of
209 * length \c 16 Bytes. It is updated after this call to
210 * allow the aforementioned streaming usage.
211 * \param input The buffer holding the input data. Must be a readable
212 * buffer of size \p length Bytes. May be \c NULL if
213 * \p length is \c 0.
214 * \param output The buffer to hold the output data. Must be a writable
215 * buffer of length \p length Bytes. May be \c NULL if
216 * \p length is \c 0.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200217 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000218 * \return \c 0 if successful.
219 * \return A negative error code on failure.
Paul Bakker38119b12009-01-10 23:31:23 +0000220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000222 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000223 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000224 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000225 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000226 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000227 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000231/**
Hanno Becker7a16aad2018-12-12 14:54:16 +0000232 * \brief CAMELLIA-CTR buffer encryption/decryption
Paul Bakker1ef71df2011-06-09 14:14:58 +0000233 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000234 * \note Due to the nature of CTR you should use the same key
235 * schedule for both encryption and decryption. So a
236 * context initialized with mbedtls_camellia_setkey_enc()
237 * for both #MBEDTLS_CAMELLIA_ENCRYPT and
238 * #MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000239 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100240 * \warning You must never reuse a nonce value with the same key. Doing so
241 * would void the encryption for the two messages encrypted with
242 * the same nonce and key.
243 *
244 * There are two common strategies for managing nonces with CTR:
245 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200246 * 1. You can handle everything as a single message processed over
247 * successive calls to this function. In that case, you want to
248 * set \p nonce_counter and \p nc_off to 0 for the first call, and
249 * then preserve the values of \p nonce_counter, \p nc_off and \p
250 * stream_block across calls to this function as they will be
251 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100252 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200253 * With this strategy, you must not encrypt more than 2**128
254 * blocks of data with the same key.
255 *
256 * 2. You can encrypt separate messages by dividing the \p
257 * nonce_counter buffer in two areas: the first one used for a
258 * per-message nonce, handled by yourself, and the second one
259 * updated by this function internally.
260 *
261 * For example, you might reserve the first 12 bytes for the
262 * per-message nonce, and the last 4 bytes for internal use. In that
263 * case, before calling this function on a new message you need to
264 * set the first 12 bytes of \p nonce_counter to your chosen nonce
265 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
266 * stream_block to be ignored). That way, you can encrypt at most
267 * 2**96 messages of up to 2**32 blocks each with the same key.
268 *
269 * The per-message nonce (or information sufficient to reconstruct
270 * it) needs to be communicated with the ciphertext and must be unique.
271 * The recommended way to ensure uniqueness is to use a message
272 * counter. An alternative is to generate random nonces, but this
273 * limits the number of messages that can be securely encrypted:
274 * for example, with 96-bit random nonces, you should not encrypt
275 * more than 2**32 messages with the same key.
276 *
277 * Note that for both stategies, sizes are measured in blocks and
Hanno Becker7a16aad2018-12-12 14:54:16 +0000278 * that a CAMELLIA block is \c 16 bytes.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100279 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200280 * \warning Upon return, \p stream_block contains sensitive data. Its
281 * content must not be written to insecure storage and should be
282 * securely discarded as soon as it's no longer needed.
283 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000284 * \param ctx The CAMELLIA context to use.
285 * \param length The length of the input data. Any value is allowed.
286 * \param nc_off The offset in the current \p stream_block (for resuming
Paul Bakker1ef71df2011-06-09 14:14:58 +0000287 * within current cipher stream). The offset pointer to
Hanno Becker7a16aad2018-12-12 14:54:16 +0000288 * should be \c 0 at the start of a stream. It is updated
289 * at the end of this call.
290 * \param nonce_counter The 128-bit nonce and counter. Must be an RW buffer of
291 * length \c 16 Bytes.
292 * \param stream_block The saved stream-block for resuming. Must be an
293 * RW buffer of length \c 16 Bytes.
294 * \param input The input data stream. Must be a readable buffer of
295 * size \p length Bytes. This may be \c NULL if \p length
296 * is \c 0.
297 * \param output The output data stream. Must be a writable buffer of
298 * size \p length Bytes. This may be \c NULL if \p length
299 * is \c 0.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000300 *
Hanno Becker7a16aad2018-12-12 14:54:16 +0000301 * \return \c 0 if successful.
302 * \return A negative error code on failure.
Paul Bakker1ef71df2011-06-09 14:14:58 +0000303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000305 size_t length,
306 size_t *nc_off,
307 unsigned char nonce_counter[16],
308 unsigned char stream_block[16],
309 const unsigned char *input,
310 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000312
Paul Bakker38119b12009-01-10 23:31:23 +0000313/**
314 * \brief Checkup routine
315 *
316 * \return 0 if successful, or 1 if the test failed
317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318int mbedtls_camellia_self_test( int verbose );
Paul Bakker38119b12009-01-10 23:31:23 +0000319
320#ifdef __cplusplus
321}
322#endif
323
324#endif /* camellia.h */