blob: c81066c89af98253ba62ce3615f342f684c3dc49 [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 Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, 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 Bakker23986e52011-04-24 08:57:21 +000030#include <string.h>
31
Paul Bakker477fd322009-10-04 13:22:13 +000032#ifdef _MSC_VER
33#include <basetsd.h>
34typedef UINT32 uint32_t;
Paul Bakker80ab9f52009-05-24 14:42:46 +000035#else
Paul Bakker477fd322009-10-04 13:22:13 +000036#include <inttypes.h>
Paul Bakker80ab9f52009-05-24 14:42:46 +000037#endif
Paul Bakkerc81f6c32009-05-03 13:09:15 +000038
Paul Bakker38119b12009-01-10 23:31:23 +000039#define CAMELLIA_ENCRYPT 1
40#define CAMELLIA_DECRYPT 0
41
Paul Bakker9d781402011-05-09 16:17:09 +000042#define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
43#define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000044
Paul Bakker38119b12009-01-10 23:31:23 +000045/**
46 * \brief CAMELLIA context structure
47 */
48typedef struct
49{
50 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000051 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000052}
53camellia_context;
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59/**
60 * \brief CAMELLIA key schedule (encryption)
61 *
62 * \param ctx CAMELLIA context to be initialized
63 * \param key encryption key
64 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000065 *
66 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000067 */
Paul Bakker23986e52011-04-24 08:57:21 +000068int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key, unsigned int keysize );
Paul Bakker38119b12009-01-10 23:31:23 +000069
70/**
71 * \brief CAMELLIA key schedule (decryption)
72 *
73 * \param ctx CAMELLIA context to be initialized
74 * \param key decryption key
75 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000076 *
77 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000078 */
Paul Bakker23986e52011-04-24 08:57:21 +000079int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key, unsigned int keysize );
Paul Bakker38119b12009-01-10 23:31:23 +000080
81/**
82 * \brief CAMELLIA-ECB block encryption/decryption
83 *
84 * \param ctx CAMELLIA context
85 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
86 * \param input 16-byte input block
87 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +000088 *
Paul Bakker27caa8a2010-03-21 15:43:59 +000089 * \return 0 if successful
Paul Bakker38119b12009-01-10 23:31:23 +000090 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000091int camellia_crypt_ecb( camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +000092 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000093 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +000094 unsigned char output[16] );
95
96/**
97 * \brief CAMELLIA-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +000098 * Length should be a multiple of the block
99 * size (16 bytes)
Paul Bakker38119b12009-01-10 23:31:23 +0000100 *
101 * \param ctx CAMELLIA context
102 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
103 * \param length length of the input data
104 * \param iv initialization vector (updated after use)
105 * \param input buffer holding the input data
106 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000107 *
108 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000109 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000110int camellia_crypt_cbc( camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000111 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000112 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000113 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000114 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000115 unsigned char *output );
116
117/**
118 * \brief CAMELLIA-CFB128 buffer encryption/decryption
119 *
120 * \param ctx CAMELLIA context
121 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
122 * \param length length of the input data
123 * \param iv_off offset in IV (updated after use)
124 * \param iv initialization vector (updated after use)
125 * \param input buffer holding the input data
126 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000127 *
128 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000129 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000130int camellia_crypt_cfb128( camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000131 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000132 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000133 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000134 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000135 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000136 unsigned char *output );
137
Paul Bakker1ef71df2011-06-09 14:14:58 +0000138/*
139 * \brief CAMELLIA-CTR buffer encryption/decryption
140 *
141 * Warning: You have to keep the maximum use of your counter in mind!
142 *
143 * \param length The length of the data
144 * \param nc_off The offset in the current stream_block (for resuming
145 * within current cipher stream). The offset pointer to
146 * should be 0 at the start of a stream.
147 * \param nonce_counter The 128-bit nonce and counter.
148 * \param stream_block The saved stream-block for resuming. Is overwritten
149 * by the function.
150 * \param input The input data stream
151 * \param output The output data stream
152 *
153 * \return 0 if successful
154 */
155int camellia_crypt_ctr( camellia_context *ctx,
156 size_t length,
157 size_t *nc_off,
158 unsigned char nonce_counter[16],
159 unsigned char stream_block[16],
160 const unsigned char *input,
161 unsigned char *output );
162
Paul Bakker38119b12009-01-10 23:31:23 +0000163/**
164 * \brief Checkup routine
165 *
166 * \return 0 if successful, or 1 if the test failed
167 */
168int camellia_self_test( int verbose );
169
170#ifdef __cplusplus
171}
172#endif
173
174#endif /* camellia.h */