blob: 2023775d22a7ceb838ed7be3219e8bbcb1ad310c [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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00008 * This file is part of mbed TLS (https://www.polarssl.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 */
24#ifndef POLARSSL_CAMELLIA_H
25#define POLARSSL_CAMELLIA_H
Paul Bakker477fd322009-10-04 13:22:13 +000026
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Paul Bakker23986e52011-04-24 08:57:21 +000033#include <string.h>
34
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker477fd322009-10-04 13:22:13 +000036#include <basetsd.h>
37typedef UINT32 uint32_t;
Paul Bakker80ab9f52009-05-24 14:42:46 +000038#else
Paul Bakker477fd322009-10-04 13:22:13 +000039#include <inttypes.h>
Paul Bakker80ab9f52009-05-24 14:42:46 +000040#endif
Paul Bakkerc81f6c32009-05-03 13:09:15 +000041
Paul Bakker38119b12009-01-10 23:31:23 +000042#define CAMELLIA_ENCRYPT 1
43#define CAMELLIA_DECRYPT 0
44
Paul Bakker9d781402011-05-09 16:17:09 +000045#define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
46#define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000047
Paul Bakker90995b52013-06-24 19:20:35 +020048#if !defined(POLARSSL_CAMELLIA_ALT)
49// Regular implementation
50//
51
Paul Bakker407a0da2013-06-27 14:29:21 +020052#ifdef __cplusplus
53extern "C" {
54#endif
55
Paul Bakker38119b12009-01-10 23:31:23 +000056/**
57 * \brief CAMELLIA context structure
58 */
59typedef struct
60{
61 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000062 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000063}
64camellia_context;
65
Paul Bakker38119b12009-01-10 23:31:23 +000066/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020067 * \brief Initialize CAMELLIA context
68 *
69 * \param ctx CAMELLIA context to be initialized
70 */
71void camellia_init( camellia_context *ctx );
72
73/**
74 * \brief Clear CAMELLIA context
75 *
76 * \param ctx CAMELLIA context to be cleared
77 */
78void camellia_free( camellia_context *ctx );
79
80/**
Paul Bakker38119b12009-01-10 23:31:23 +000081 * \brief CAMELLIA key schedule (encryption)
82 *
83 * \param ctx CAMELLIA context to be initialized
84 * \param key encryption key
85 * \param keysize must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +020086 *
Paul Bakker2b222c82009-07-27 21:03:45 +000087 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000088 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020089int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key,
90 unsigned int keysize );
Paul Bakker38119b12009-01-10 23:31:23 +000091
92/**
93 * \brief CAMELLIA key schedule (decryption)
94 *
95 * \param ctx CAMELLIA context to be initialized
96 * \param key decryption key
97 * \param keysize must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +020098 *
Paul Bakker2b222c82009-07-27 21:03:45 +000099 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000100 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200101int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key,
102 unsigned int keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000103
104/**
105 * \brief CAMELLIA-ECB block encryption/decryption
106 *
107 * \param ctx CAMELLIA context
108 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
109 * \param input 16-byte input block
110 * \param output 16-byte output block
Paul Bakker9af723c2014-05-01 13:03:14 +0200111 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000112 * \return 0 if successful
Paul Bakker38119b12009-01-10 23:31:23 +0000113 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000114int camellia_crypt_ecb( camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000115 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000116 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000117 unsigned char output[16] );
118
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200119#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000120/**
121 * \brief CAMELLIA-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000122 * Length should be a multiple of the block
123 * size (16 bytes)
Paul Bakker38119b12009-01-10 23:31:23 +0000124 *
125 * \param ctx CAMELLIA context
126 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
127 * \param length length of the input data
128 * \param iv initialization vector (updated after use)
129 * \param input buffer holding the input data
130 * \param output buffer holding the output data
Paul Bakker9af723c2014-05-01 13:03:14 +0200131 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200132 * \return 0 if successful, or
133 * POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000134 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000135int camellia_crypt_cbc( camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000136 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000137 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000138 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000139 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000140 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200141#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000142
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200143#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000144/**
145 * \brief CAMELLIA-CFB128 buffer encryption/decryption
146 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000147 * Note: Due to the nature of CFB you should use the same key schedule for
148 * both encryption and decryption. So a context initialized with
149 * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
150 *
Paul Bakker38119b12009-01-10 23:31:23 +0000151 * \param ctx CAMELLIA context
152 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
153 * \param length length of the input data
154 * \param iv_off offset in IV (updated after use)
155 * \param iv initialization vector (updated after use)
156 * \param input buffer holding the input data
157 * \param output buffer holding the output data
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200158 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200159 * \return 0 if successful, or
160 * POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000161 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000162int camellia_crypt_cfb128( camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000163 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000164 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000165 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000166 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000167 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000168 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200169#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000170
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200171#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000172/**
Paul Bakker1ef71df2011-06-09 14:14:58 +0000173 * \brief CAMELLIA-CTR buffer encryption/decryption
174 *
175 * Warning: You have to keep the maximum use of your counter in mind!
176 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000177 * Note: Due to the nature of CTR you should use the same key schedule for
178 * both encryption and decryption. So a context initialized with
179 * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIA_DECRYPT.
180 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200181 * \param ctx CAMELLIA context
Paul Bakker1ef71df2011-06-09 14:14:58 +0000182 * \param length The length of the data
183 * \param nc_off The offset in the current stream_block (for resuming
184 * within current cipher stream). The offset pointer to
185 * should be 0 at the start of a stream.
186 * \param nonce_counter The 128-bit nonce and counter.
187 * \param stream_block The saved stream-block for resuming. Is overwritten
188 * by the function.
189 * \param input The input data stream
190 * \param output The output data stream
191 *
192 * \return 0 if successful
193 */
194int camellia_crypt_ctr( camellia_context *ctx,
195 size_t length,
196 size_t *nc_off,
197 unsigned char nonce_counter[16],
198 unsigned char stream_block[16],
199 const unsigned char *input,
200 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200201#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000202
Paul Bakker90995b52013-06-24 19:20:35 +0200203#ifdef __cplusplus
204}
205#endif
206
207#else /* POLARSSL_CAMELLIA_ALT */
208#include "camellia_alt.h"
209#endif /* POLARSSL_CAMELLIA_ALT */
210
211#ifdef __cplusplus
212extern "C" {
213#endif
214
Paul Bakker38119b12009-01-10 23:31:23 +0000215/**
216 * \brief Checkup routine
217 *
218 * \return 0 if successful, or 1 if the test failed
219 */
220int camellia_self_test( int verbose );
221
222#ifdef __cplusplus
223}
224#endif
225
226#endif /* camellia.h */