blob: 4ce10dc41067eb7315ef5312918c5338836737ae [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 *
Paul Bakker90995b52013-06-24 19:20:35 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakker38119b12009-01-10 23:31:23 +000012 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_CAMELLIA_H
28#define POLARSSL_CAMELLIA_H
Paul Bakker477fd322009-10-04 13:22:13 +000029
Paul Bakker90995b52013-06-24 19:20:35 +020030#include "config.h"
31
Paul Bakker23986e52011-04-24 08:57:21 +000032#include <string.h>
33
Paul Bakker477fd322009-10-04 13:22:13 +000034#ifdef _MSC_VER
35#include <basetsd.h>
36typedef UINT32 uint32_t;
Paul Bakker80ab9f52009-05-24 14:42:46 +000037#else
Paul Bakker477fd322009-10-04 13:22:13 +000038#include <inttypes.h>
Paul Bakker80ab9f52009-05-24 14:42:46 +000039#endif
Paul Bakkerc81f6c32009-05-03 13:09:15 +000040
Paul Bakker38119b12009-01-10 23:31:23 +000041#define CAMELLIA_ENCRYPT 1
42#define CAMELLIA_DECRYPT 0
43
Paul Bakker9d781402011-05-09 16:17:09 +000044#define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
45#define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000046
Paul Bakker90995b52013-06-24 19:20:35 +020047#if !defined(POLARSSL_CAMELLIA_ALT)
48// Regular implementation
49//
50
Paul Bakker407a0da2013-06-27 14:29:21 +020051#ifdef __cplusplus
52extern "C" {
53#endif
54
Paul Bakker38119b12009-01-10 23:31:23 +000055/**
56 * \brief CAMELLIA context structure
57 */
58typedef struct
59{
60 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000061 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000062}
63camellia_context;
64
Paul Bakker38119b12009-01-10 23:31:23 +000065/**
66 * \brief CAMELLIA key schedule (encryption)
67 *
68 * \param ctx CAMELLIA context to be initialized
69 * \param key encryption key
70 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000071 *
72 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000073 */
Paul Bakker23986e52011-04-24 08:57:21 +000074int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key, unsigned int keysize );
Paul Bakker38119b12009-01-10 23:31:23 +000075
76/**
77 * \brief CAMELLIA key schedule (decryption)
78 *
79 * \param ctx CAMELLIA context to be initialized
80 * \param key decryption key
81 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000082 *
83 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000084 */
Paul Bakker23986e52011-04-24 08:57:21 +000085int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key, unsigned int keysize );
Paul Bakker38119b12009-01-10 23:31:23 +000086
87/**
88 * \brief CAMELLIA-ECB block encryption/decryption
89 *
90 * \param ctx CAMELLIA context
91 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
92 * \param input 16-byte input block
93 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +000094 *
Paul Bakker27caa8a2010-03-21 15:43:59 +000095 * \return 0 if successful
Paul Bakker38119b12009-01-10 23:31:23 +000096 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000097int camellia_crypt_ecb( camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +000098 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000099 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000100 unsigned char output[16] );
101
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200102#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000103/**
104 * \brief CAMELLIA-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000105 * Length should be a multiple of the block
106 * size (16 bytes)
Paul Bakker38119b12009-01-10 23:31:23 +0000107 *
108 * \param ctx CAMELLIA context
109 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
110 * \param length length of the input data
111 * \param iv initialization vector (updated after use)
112 * \param input buffer holding the input data
113 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000114 *
115 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000116 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000117int camellia_crypt_cbc( camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000118 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000119 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000120 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000121 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000122 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200123#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000124
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200125#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000126/**
127 * \brief CAMELLIA-CFB128 buffer encryption/decryption
128 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000129 * Note: Due to the nature of CFB you should use the same key schedule for
130 * both encryption and decryption. So a context initialized with
131 * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
132 *
Paul Bakker38119b12009-01-10 23:31:23 +0000133 * \param ctx CAMELLIA context
134 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
135 * \param length length of the input data
136 * \param iv_off offset in IV (updated after use)
137 * \param iv initialization vector (updated after use)
138 * \param input buffer holding the input data
139 * \param output buffer holding the output data
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200140 *
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000141 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000142 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000143int camellia_crypt_cfb128( camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000144 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000145 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000146 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000147 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000148 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000149 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200150#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000151
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200152#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000153/**
Paul Bakker1ef71df2011-06-09 14:14:58 +0000154 * \brief CAMELLIA-CTR buffer encryption/decryption
155 *
156 * Warning: You have to keep the maximum use of your counter in mind!
157 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000158 * Note: Due to the nature of CTR you should use the same key schedule for
159 * both encryption and decryption. So a context initialized with
160 * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIA_DECRYPT.
161 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200162 * \param ctx CAMELLIA context
Paul Bakker1ef71df2011-06-09 14:14:58 +0000163 * \param length The length of the data
164 * \param nc_off The offset in the current stream_block (for resuming
165 * within current cipher stream). The offset pointer to
166 * should be 0 at the start of a stream.
167 * \param nonce_counter The 128-bit nonce and counter.
168 * \param stream_block The saved stream-block for resuming. Is overwritten
169 * by the function.
170 * \param input The input data stream
171 * \param output The output data stream
172 *
173 * \return 0 if successful
174 */
175int camellia_crypt_ctr( camellia_context *ctx,
176 size_t length,
177 size_t *nc_off,
178 unsigned char nonce_counter[16],
179 unsigned char stream_block[16],
180 const unsigned char *input,
181 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200182#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000183
Paul Bakker90995b52013-06-24 19:20:35 +0200184#ifdef __cplusplus
185}
186#endif
187
188#else /* POLARSSL_CAMELLIA_ALT */
189#include "camellia_alt.h"
190#endif /* POLARSSL_CAMELLIA_ALT */
191
192#ifdef __cplusplus
193extern "C" {
194#endif
195
Paul Bakker38119b12009-01-10 23:31:23 +0000196/**
197 * \brief Checkup routine
198 *
199 * \return 0 if successful, or 1 if the test failed
200 */
201int camellia_self_test( int verbose );
202
203#ifdef __cplusplus
204}
205#endif
206
207#endif /* camellia.h */