blob: e52461281ac331109b58525cb93b5d0c93420787 [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/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020075 * \brief Initialize CAMELLIA context
76 *
77 * \param ctx CAMELLIA context to be initialized
78 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020080
81/**
82 * \brief Clear CAMELLIA context
83 *
84 * \param ctx CAMELLIA context to be cleared
85 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020087
88/**
Paul Bakker38119b12009-01-10 23:31:23 +000089 * \brief CAMELLIA key schedule (encryption)
90 *
91 * \param ctx CAMELLIA context to be initialized
92 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020093 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +020094 *
Hanno Becker4fb258a2018-12-17 16:09:15 +000095 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA
Paul Bakker38119b12009-01-10 23:31:23 +000096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020098 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +000099
100/**
101 * \brief CAMELLIA key schedule (decryption)
102 *
103 * \param ctx CAMELLIA context to be initialized
104 * \param key decryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200105 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +0200106 *
Hanno Becker4fb258a2018-12-17 16:09:15 +0000107 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA
Paul Bakker38119b12009-01-10 23:31:23 +0000108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200110 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000111
112/**
113 * \brief CAMELLIA-ECB block encryption/decryption
114 *
115 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000117 * \param input 16-byte input block
118 * \param output 16-byte output block
Paul Bakker9af723c2014-05-01 13:03:14 +0200119 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000120 * \return 0 if successful
Paul Bakker38119b12009-01-10 23:31:23 +0000121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000123 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000124 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000125 unsigned char output[16] );
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000128/**
129 * \brief CAMELLIA-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000130 * Length should be a multiple of the block
131 * size (16 bytes)
Paul Bakker38119b12009-01-10 23:31:23 +0000132 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000133 * \note Upon exit, the content of the IV is updated so that you can
134 * call the function same function again on the following
135 * block(s) of data and get the same result as if it was
136 * encrypted in one call. This allows a "streaming" usage.
137 * If on the other hand you need to retain the contents of the
138 * IV, you should either save it manually or use the cipher
139 * module instead.
140 *
Paul Bakker38119b12009-01-10 23:31:23 +0000141 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000143 * \param length length of the input data
144 * \param iv initialization vector (updated after use)
145 * \param input buffer holding the input data
146 * \param output buffer holding the output data
Paul Bakker9af723c2014-05-01 13:03:14 +0200147 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200148 * \return 0 if successful, or
Hanno Becker4fb258a2018-12-17 16:09:15 +0000149 * MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA
Paul Bakker38119b12009-01-10 23:31:23 +0000150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000152 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000153 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000154 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000155 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000156 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000160/**
161 * \brief CAMELLIA-CFB128 buffer encryption/decryption
162 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000163 * Note: Due to the nature of CFB you should use the same key schedule for
164 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000166 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000167 * \note Upon exit, the content of the IV is updated so that you can
168 * call the function same function again on the following
169 * block(s) of data and get the same result as if it was
170 * encrypted in one call. This allows a "streaming" usage.
171 * If on the other hand you need to retain the contents of the
172 * IV, you should either save it manually or use the cipher
173 * module instead.
174 *
Paul Bakker38119b12009-01-10 23:31:23 +0000175 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000177 * \param length length of the input data
178 * \param iv_off offset in IV (updated after use)
179 * \param iv initialization vector (updated after use)
180 * \param input buffer holding the input data
181 * \param output buffer holding the output data
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200182 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200183 * \return 0 if successful, or
Hanno Becker4fb258a2018-12-17 16:09:15 +0000184 * MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA.
Paul Bakker38119b12009-01-10 23:31:23 +0000185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000187 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000188 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000189 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000190 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000191 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000192 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000196/**
Paul Bakker1ef71df2011-06-09 14:14:58 +0000197 * \brief CAMELLIA-CTR buffer encryption/decryption
198 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000199 * Note: Due to the nature of CTR you should use the same key schedule for
200 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000202 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100203 * \warning You must never reuse a nonce value with the same key. Doing so
204 * would void the encryption for the two messages encrypted with
205 * the same nonce and key.
206 *
207 * There are two common strategies for managing nonces with CTR:
208 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200209 * 1. You can handle everything as a single message processed over
210 * successive calls to this function. In that case, you want to
211 * set \p nonce_counter and \p nc_off to 0 for the first call, and
212 * then preserve the values of \p nonce_counter, \p nc_off and \p
213 * stream_block across calls to this function as they will be
214 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100215 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200216 * With this strategy, you must not encrypt more than 2**128
217 * blocks of data with the same key.
218 *
219 * 2. You can encrypt separate messages by dividing the \p
220 * nonce_counter buffer in two areas: the first one used for a
221 * per-message nonce, handled by yourself, and the second one
222 * updated by this function internally.
223 *
224 * For example, you might reserve the first 12 bytes for the
225 * per-message nonce, and the last 4 bytes for internal use. In that
226 * case, before calling this function on a new message you need to
227 * set the first 12 bytes of \p nonce_counter to your chosen nonce
228 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
229 * stream_block to be ignored). That way, you can encrypt at most
230 * 2**96 messages of up to 2**32 blocks each with the same key.
231 *
232 * The per-message nonce (or information sufficient to reconstruct
233 * it) needs to be communicated with the ciphertext and must be unique.
234 * The recommended way to ensure uniqueness is to use a message
235 * counter. An alternative is to generate random nonces, but this
236 * limits the number of messages that can be securely encrypted:
237 * for example, with 96-bit random nonces, you should not encrypt
238 * more than 2**32 messages with the same key.
239 *
240 * Note that for both stategies, sizes are measured in blocks and
241 * that a CAMELLIA block is 16 bytes.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100242 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200243 * \warning Upon return, \p stream_block contains sensitive data. Its
244 * content must not be written to insecure storage and should be
245 * securely discarded as soon as it's no longer needed.
246 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200247 * \param ctx CAMELLIA context
Paul Bakker1ef71df2011-06-09 14:14:58 +0000248 * \param length The length of the data
249 * \param nc_off The offset in the current stream_block (for resuming
250 * within current cipher stream). The offset pointer to
251 * should be 0 at the start of a stream.
252 * \param nonce_counter The 128-bit nonce and counter.
253 * \param stream_block The saved stream-block for resuming. Is overwritten
254 * by the function.
255 * \param input The input data stream
256 * \param output The output data stream
257 *
258 * \return 0 if successful
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000261 size_t length,
262 size_t *nc_off,
263 unsigned char nonce_counter[16],
264 unsigned char stream_block[16],
265 const unsigned char *input,
266 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000268
Paul Bakker38119b12009-01-10 23:31:23 +0000269/**
270 * \brief Checkup routine
271 *
272 * \return 0 if successful, or 1 if the test failed
273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274int mbedtls_camellia_self_test( int verbose );
Paul Bakker38119b12009-01-10 23:31:23 +0000275
276#ifdef __cplusplus
277}
278#endif
279
280#endif /* camellia.h */