blob: 5226853f9cf1f1a5444714fb969e0a5b128e8f63 [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
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakker38119b12009-01-10 23:31:23 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
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. */
Paul Bakker2b222c82009-07-27 21:03:45 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_CAMELLIA_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020043// Regular implementation
44//
45
Paul Bakker407a0da2013-06-27 14:29:21 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Paul Bakker38119b12009-01-10 23:31:23 +000050/**
51 * \brief CAMELLIA context structure
52 */
53typedef struct
54{
55 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000056 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000057}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000059
Paul Bakker38119b12009-01-10 23:31:23 +000060/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020061 * \brief Initialize CAMELLIA context
62 *
63 * \param ctx CAMELLIA context to be initialized
64 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020066
67/**
68 * \brief Clear CAMELLIA context
69 *
70 * \param ctx CAMELLIA context to be cleared
71 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020073
74/**
Paul Bakker38119b12009-01-10 23:31:23 +000075 * \brief CAMELLIA key schedule (encryption)
76 *
77 * \param ctx CAMELLIA context to be initialized
78 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020079 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +020080 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020084 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +000085
86/**
87 * \brief CAMELLIA key schedule (decryption)
88 *
89 * \param ctx CAMELLIA context to be initialized
90 * \param key decryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020091 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +020092 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020096 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +000097
98/**
99 * \brief CAMELLIA-ECB block encryption/decryption
100 *
101 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000103 * \param input 16-byte input block
104 * \param output 16-byte output block
Paul Bakker9af723c2014-05-01 13:03:14 +0200105 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000106 * \return 0 if successful
Paul Bakker38119b12009-01-10 23:31:23 +0000107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000109 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000110 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000111 unsigned char output[16] );
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000114/**
115 * \brief CAMELLIA-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000116 * Length should be a multiple of the block
117 * size (16 bytes)
Paul Bakker38119b12009-01-10 23:31:23 +0000118 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000119 * \note Upon exit, the content of the IV is updated so that you can
120 * call the function same function again on the following
121 * block(s) of data and get the same result as if it was
122 * encrypted in one call. This allows a "streaming" usage.
123 * If on the other hand you need to retain the contents of the
124 * IV, you should either save it manually or use the cipher
125 * module instead.
126 *
Paul Bakker38119b12009-01-10 23:31:23 +0000127 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000129 * \param length length of the input data
130 * \param iv initialization vector (updated after use)
131 * \param input buffer holding the input data
132 * \param output buffer holding the output data
Paul Bakker9af723c2014-05-01 13:03:14 +0200133 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200134 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000138 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000139 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000140 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000141 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000142 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000146/**
147 * \brief CAMELLIA-CFB128 buffer encryption/decryption
148 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000149 * Note: Due to the nature of CFB you should use the same key schedule for
150 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000152 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000153 * \note Upon exit, the content of the IV is updated so that you can
154 * call the function same function again on the following
155 * block(s) of data and get the same result as if it was
156 * encrypted in one call. This allows a "streaming" usage.
157 * If on the other hand you need to retain the contents of the
158 * IV, you should either save it manually or use the cipher
159 * module instead.
160 *
Paul Bakker38119b12009-01-10 23:31:23 +0000161 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000163 * \param length length of the input data
164 * \param iv_off offset in IV (updated after use)
165 * \param iv initialization vector (updated after use)
166 * \param input buffer holding the input data
167 * \param output buffer holding the output data
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200168 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200169 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000173 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000174 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000175 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000176 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000177 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000178 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000182/**
Paul Bakker1ef71df2011-06-09 14:14:58 +0000183 * \brief CAMELLIA-CTR buffer encryption/decryption
184 *
185 * Warning: You have to keep the maximum use of your counter in mind!
186 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000187 * Note: Due to the nature of CTR you should use the same key schedule for
188 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000190 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200191 * \param ctx CAMELLIA context
Paul Bakker1ef71df2011-06-09 14:14:58 +0000192 * \param length The length of the data
193 * \param nc_off The offset in the current stream_block (for resuming
194 * within current cipher stream). The offset pointer to
195 * should be 0 at the start of a stream.
196 * \param nonce_counter The 128-bit nonce and counter.
197 * \param stream_block The saved stream-block for resuming. Is overwritten
198 * by the function.
199 * \param input The input data stream
200 * \param output The output data stream
201 *
202 * \return 0 if successful
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000205 size_t length,
206 size_t *nc_off,
207 unsigned char nonce_counter[16],
208 unsigned char stream_block[16],
209 const unsigned char *input,
210 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000212
Paul Bakker90995b52013-06-24 19:20:35 +0200213#ifdef __cplusplus
214}
215#endif
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#else /* MBEDTLS_CAMELLIA_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200218#include "camellia_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /* MBEDTLS_CAMELLIA_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200220
221#ifdef __cplusplus
222extern "C" {
223#endif
224
Paul Bakker38119b12009-01-10 23:31:23 +0000225/**
226 * \brief Checkup routine
227 *
228 * \return 0 if successful, or 1 if the test failed
229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_camellia_self_test( int verbose );
Paul Bakker38119b12009-01-10 23:31:23 +0000231
232#ifdef __cplusplus
233}
234#endif
235
236#endif /* camellia.h */