blob: 1b138fc9e701da12fd60f02ba0a2dbcd4678bf19 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_CAMELLIA_ENCRYPT 1
37#define MBEDTLS_CAMELLIA_DECRYPT 0
Paul Bakker38119b12009-01-10 23:31:23 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
40#define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010041#define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027 /**< Camellia hardware accelerator failed. */
Paul Bakker2b222c82009-07-27 21:03:45 +000042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_CAMELLIA_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020044// Regular implementation
45//
46
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Paul Bakker38119b12009-01-10 23:31:23 +000051/**
52 * \brief CAMELLIA context structure
53 */
54typedef struct
55{
56 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000057 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000058}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000060
Paul Bakker38119b12009-01-10 23:31:23 +000061/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020062 * \brief Initialize CAMELLIA context
63 *
64 * \param ctx CAMELLIA context to be initialized
65 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020067
68/**
69 * \brief Clear CAMELLIA context
70 *
71 * \param ctx CAMELLIA context to be cleared
72 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020074
75/**
Paul Bakker38119b12009-01-10 23:31:23 +000076 * \brief CAMELLIA key schedule (encryption)
77 *
78 * \param ctx CAMELLIA context to be initialized
79 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020080 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +020081 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020085 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +000086
87/**
88 * \brief CAMELLIA key schedule (decryption)
89 *
90 * \param ctx CAMELLIA context to be initialized
91 * \param key decryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020092 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +020093 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020097 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +000098
99/**
100 * \brief CAMELLIA-ECB block encryption/decryption
101 *
102 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000104 * \param input 16-byte input block
105 * \param output 16-byte output block
Paul Bakker9af723c2014-05-01 13:03:14 +0200106 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000107 * \return 0 if successful
Paul Bakker38119b12009-01-10 23:31:23 +0000108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000110 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000111 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000112 unsigned char output[16] );
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000115/**
116 * \brief CAMELLIA-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000117 * Length should be a multiple of the block
118 * size (16 bytes)
Paul Bakker38119b12009-01-10 23:31:23 +0000119 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000120 * \note Upon exit, the content of the IV is updated so that you can
121 * call the function same function again on the following
122 * block(s) of data and get the same result as if it was
123 * encrypted in one call. This allows a "streaming" usage.
124 * If on the other hand you need to retain the contents of the
125 * IV, you should either save it manually or use the cipher
126 * module instead.
127 *
Paul Bakker38119b12009-01-10 23:31:23 +0000128 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000130 * \param length length of the input data
131 * \param iv initialization vector (updated after use)
132 * \param input buffer holding the input data
133 * \param output buffer holding the output data
Paul Bakker9af723c2014-05-01 13:03:14 +0200134 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200135 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000139 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000140 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000141 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000142 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000143 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000147/**
148 * \brief CAMELLIA-CFB128 buffer encryption/decryption
149 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000150 * Note: Due to the nature of CFB you should use the same key schedule for
151 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000153 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000154 * \note Upon exit, the content of the IV is updated so that you can
155 * call the function same function again on the following
156 * block(s) of data and get the same result as if it was
157 * encrypted in one call. This allows a "streaming" usage.
158 * If on the other hand you need to retain the contents of the
159 * IV, you should either save it manually or use the cipher
160 * module instead.
161 *
Paul Bakker38119b12009-01-10 23:31:23 +0000162 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000164 * \param length length of the input data
165 * \param iv_off offset in IV (updated after use)
166 * \param iv initialization vector (updated after use)
167 * \param input buffer holding the input data
168 * \param output buffer holding the output data
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200169 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200170 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000174 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000175 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000176 size_t *iv_off,
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_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000183/**
Paul Bakker1ef71df2011-06-09 14:14:58 +0000184 * \brief CAMELLIA-CTR buffer encryption/decryption
185 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000186 * Note: Due to the nature of CTR you should use the same key schedule for
187 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000189 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100190 * \warning You must never reuse a nonce value with the same key. Doing so
191 * would void the encryption for the two messages encrypted with
192 * the same nonce and key.
193 *
194 * There are two common strategies for managing nonces with CTR:
195 *
196 * 1. Use a counter starting at 0 or a random value. With this
197 * strategy, this function will increment the counter for you, so
198 * you only need to preserve the \p nonce_counter buffer between
199 * calls. With this strategy, you must not encrypt more than
200 * 2**128 blocks of data.
201 * 2. Use a randomly-generated \p nonce_counter for each call.
202 * With this strategy, you need to ensure the nonce is generated
203 * in an unbiased way and you must not encrypt more than 2**64
204 * block of data.
205 *
206 * Note that for both stategies, the limit is in number of blocks
207 * and that a CAMELLIA block is 16 bytes.
208 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200209 * \param ctx CAMELLIA context
Paul Bakker1ef71df2011-06-09 14:14:58 +0000210 * \param length The length of the data
211 * \param nc_off The offset in the current stream_block (for resuming
212 * within current cipher stream). The offset pointer to
213 * should be 0 at the start of a stream.
214 * \param nonce_counter The 128-bit nonce and counter.
215 * \param stream_block The saved stream-block for resuming. Is overwritten
216 * by the function.
217 * \param input The input data stream
218 * \param output The output data stream
219 *
220 * \return 0 if successful
221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000223 size_t length,
224 size_t *nc_off,
225 unsigned char nonce_counter[16],
226 unsigned char stream_block[16],
227 const unsigned char *input,
228 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000230
Paul Bakker90995b52013-06-24 19:20:35 +0200231#ifdef __cplusplus
232}
233#endif
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#else /* MBEDTLS_CAMELLIA_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200236#include "camellia_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#endif /* MBEDTLS_CAMELLIA_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200238
239#ifdef __cplusplus
240extern "C" {
241#endif
242
Paul Bakker38119b12009-01-10 23:31:23 +0000243/**
244 * \brief Checkup routine
245 *
246 * \return 0 if successful, or 1 if the test failed
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248int mbedtls_camellia_self_test( int verbose );
Paul Bakker38119b12009-01-10 23:31:23 +0000249
250#ifdef __cplusplus
251}
252#endif
253
254#endif /* camellia.h */