blob: 556fe3e8c6bbc1be87bf87e9a1042259f2e0f2e7 [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é-Gonnard0edee5e2015-01-26 15:29:40 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00008 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker38119b12009-01-10 23:31:23 +00009 *
10 * 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
Paul Bakker4087c472013-06-12 16:49:10 +020027#include "config.h"
28
Paul Bakker23986e52011-04-24 08:57:21 +000029#include <string.h>
30
Paul Bakker477fd322009-10-04 13:22:13 +000031#ifdef _MSC_VER
32#include <basetsd.h>
33typedef UINT32 uint32_t;
Paul Bakker80ab9f52009-05-24 14:42:46 +000034#else
Paul Bakker477fd322009-10-04 13:22:13 +000035#include <inttypes.h>
Paul Bakker80ab9f52009-05-24 14:42:46 +000036#endif
Paul Bakkerc81f6c32009-05-03 13:09:15 +000037
Paul Bakker38119b12009-01-10 23:31:23 +000038#define CAMELLIA_ENCRYPT 1
39#define CAMELLIA_DECRYPT 0
40
Paul Bakker9d781402011-05-09 16:17:09 +000041#define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
42#define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000043
Paul Bakker4087c472013-06-12 16:49:10 +020044#if !defined(POLARSSL_CAMELLIA_ALT)
45// Regular implementation
46//
47
Paul Bakker38119b12009-01-10 23:31:23 +000048/**
49 * \brief CAMELLIA context structure
50 */
51typedef struct
52{
53 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000054 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000055}
56camellia_context;
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62/**
63 * \brief CAMELLIA key schedule (encryption)
64 *
65 * \param ctx CAMELLIA context to be initialized
66 * \param key encryption key
67 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000068 *
69 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000070 */
Paul Bakker23986e52011-04-24 08:57:21 +000071int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key, unsigned int keysize );
Paul Bakker38119b12009-01-10 23:31:23 +000072
73/**
74 * \brief CAMELLIA key schedule (decryption)
75 *
76 * \param ctx CAMELLIA context to be initialized
77 * \param key decryption key
78 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000079 *
80 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000081 */
Paul Bakker23986e52011-04-24 08:57:21 +000082int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key, unsigned int keysize );
Paul Bakker38119b12009-01-10 23:31:23 +000083
84/**
85 * \brief CAMELLIA-ECB block encryption/decryption
86 *
87 * \param ctx CAMELLIA context
88 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
89 * \param input 16-byte input block
90 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +000091 *
Paul Bakker27caa8a2010-03-21 15:43:59 +000092 * \return 0 if successful
Paul Bakker38119b12009-01-10 23:31:23 +000093 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000094int camellia_crypt_ecb( camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +000095 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000096 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +000097 unsigned char output[16] );
98
99/**
100 * \brief CAMELLIA-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000101 * Length should be a multiple of the block
102 * size (16 bytes)
Paul Bakker38119b12009-01-10 23:31:23 +0000103 *
104 * \param ctx CAMELLIA context
105 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
106 * \param length length of the input data
107 * \param iv initialization vector (updated after use)
108 * \param input buffer holding the input data
109 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000110 *
111 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000112 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000113int camellia_crypt_cbc( camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000114 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000115 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000116 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000117 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000118 unsigned char *output );
119
120/**
121 * \brief CAMELLIA-CFB128 buffer encryption/decryption
122 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000123 * Note: Due to the nature of CFB you should use the same key schedule for
124 * both encryption and decryption. So a context initialized with
125 * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
126 *
Paul Bakker38119b12009-01-10 23:31:23 +0000127 * \param ctx CAMELLIA context
128 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
129 * \param length length of the input data
130 * \param iv_off offset in IV (updated after use)
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 Bakkerf3ccc682010-03-18 21:21:02 +0000134 *
135 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000136 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000137int camellia_crypt_cfb128( 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 Bakker1ef71df2011-06-09 14:14:58 +0000140 size_t *iv_off,
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 );
144
Paul Bakker9a736322012-11-14 12:39:52 +0000145/**
Paul Bakker1ef71df2011-06-09 14:14:58 +0000146 * \brief CAMELLIA-CTR buffer encryption/decryption
147 *
148 * Warning: You have to keep the maximum use of your counter in mind!
149 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000150 * Note: Due to the nature of CTR you should use the same key schedule for
151 * both encryption and decryption. So a context initialized with
152 * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIA_DECRYPT.
153 *
Paul Bakker1ef71df2011-06-09 14:14:58 +0000154 * \param length The length of the data
155 * \param nc_off The offset in the current stream_block (for resuming
156 * within current cipher stream). The offset pointer to
157 * should be 0 at the start of a stream.
158 * \param nonce_counter The 128-bit nonce and counter.
159 * \param stream_block The saved stream-block for resuming. Is overwritten
160 * by the function.
161 * \param input The input data stream
162 * \param output The output data stream
163 *
164 * \return 0 if successful
165 */
166int camellia_crypt_ctr( camellia_context *ctx,
167 size_t length,
168 size_t *nc_off,
169 unsigned char nonce_counter[16],
170 unsigned char stream_block[16],
171 const unsigned char *input,
172 unsigned char *output );
173
Paul Bakker4087c472013-06-12 16:49:10 +0200174#ifdef __cplusplus
175}
176#endif
177
178#else /* POLARSSL_CAMELLIA_ALT */
179#include "camellia_alt.h"
180#endif /* POLARSSL_CAMELLIA_ALT */
181
182#ifdef __cplusplus
183extern "C" {
184#endif
185
Paul Bakker38119b12009-01-10 23:31:23 +0000186/**
187 * \brief Checkup routine
188 *
189 * \return 0 if successful, or 1 if the test failed
190 */
191int camellia_self_test( int verbose );
192
193#ifdef __cplusplus
194}
195#endif
196
197#endif /* camellia.h */