new polarssl
[syren.git] / src / libpolarssl / camellia.h
blob34c1990681e7d760365af53d10eb4a2b692b4fac
1 /**
2 * \file camellia.h
4 * \brief Camellia block cipher
6 * Copyright (C) 2006-2014, Brainspark B.V.
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
11 * All rights reserved.
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.
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.
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.
27 #ifndef POLARSSL_CAMELLIA_H
28 #define POLARSSL_CAMELLIA_H
30 #if !defined(POLARSSL_CONFIG_FILE)
31 #include "config.h"
32 #else
33 #include POLARSSL_CONFIG_FILE
34 #endif
36 #include <string.h>
38 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
39 #include <basetsd.h>
40 typedef UINT32 uint32_t;
41 #else
42 #include <inttypes.h>
43 #endif
45 #define CAMELLIA_ENCRYPT 1
46 #define CAMELLIA_DECRYPT 0
48 #define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
49 #define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
51 #if !defined(POLARSSL_CAMELLIA_ALT)
52 // Regular implementation
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
59 /**
60 * \brief CAMELLIA context structure
62 typedef struct
64 int nr; /*!< number of rounds */
65 uint32_t rk[68]; /*!< CAMELLIA round keys */
67 camellia_context;
69 /**
70 * \brief CAMELLIA key schedule (encryption)
72 * \param ctx CAMELLIA context to be initialized
73 * \param key encryption key
74 * \param keysize must be 128, 192 or 256
76 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
78 int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key,
79 unsigned int keysize );
81 /**
82 * \brief CAMELLIA key schedule (decryption)
84 * \param ctx CAMELLIA context to be initialized
85 * \param key decryption key
86 * \param keysize must be 128, 192 or 256
88 * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
90 int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key,
91 unsigned int keysize );
93 /**
94 * \brief CAMELLIA-ECB block encryption/decryption
96 * \param ctx CAMELLIA context
97 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
98 * \param input 16-byte input block
99 * \param output 16-byte output block
101 * \return 0 if successful
103 int camellia_crypt_ecb( camellia_context *ctx,
104 int mode,
105 const unsigned char input[16],
106 unsigned char output[16] );
108 #if defined(POLARSSL_CIPHER_MODE_CBC)
110 * \brief CAMELLIA-CBC buffer encryption/decryption
111 * Length should be a multiple of the block
112 * size (16 bytes)
114 * \param ctx CAMELLIA context
115 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
116 * \param length length of the input data
117 * \param iv initialization vector (updated after use)
118 * \param input buffer holding the input data
119 * \param output buffer holding the output data
121 * \return 0 if successful, or
122 * POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
124 int camellia_crypt_cbc( camellia_context *ctx,
125 int mode,
126 size_t length,
127 unsigned char iv[16],
128 const unsigned char *input,
129 unsigned char *output );
130 #endif /* POLARSSL_CIPHER_MODE_CBC */
132 #if defined(POLARSSL_CIPHER_MODE_CFB)
134 * \brief CAMELLIA-CFB128 buffer encryption/decryption
136 * Note: Due to the nature of CFB you should use the same key schedule for
137 * both encryption and decryption. So a context initialized with
138 * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
140 * \param ctx CAMELLIA context
141 * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
142 * \param length length of the input data
143 * \param iv_off offset in IV (updated after use)
144 * \param iv initialization vector (updated after use)
145 * \param input buffer holding the input data
146 * \param output buffer holding the output data
148 * \return 0 if successful, or
149 * POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
151 int camellia_crypt_cfb128( camellia_context *ctx,
152 int mode,
153 size_t length,
154 size_t *iv_off,
155 unsigned char iv[16],
156 const unsigned char *input,
157 unsigned char *output );
158 #endif /* POLARSSL_CIPHER_MODE_CFB */
160 #if defined(POLARSSL_CIPHER_MODE_CTR)
162 * \brief CAMELLIA-CTR buffer encryption/decryption
164 * Warning: You have to keep the maximum use of your counter in mind!
166 * Note: Due to the nature of CTR you should use the same key schedule for
167 * both encryption and decryption. So a context initialized with
168 * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIA_DECRYPT.
170 * \param ctx CAMELLIA context
171 * \param length The length of the data
172 * \param nc_off The offset in the current stream_block (for resuming
173 * within current cipher stream). The offset pointer to
174 * should be 0 at the start of a stream.
175 * \param nonce_counter The 128-bit nonce and counter.
176 * \param stream_block The saved stream-block for resuming. Is overwritten
177 * by the function.
178 * \param input The input data stream
179 * \param output The output data stream
181 * \return 0 if successful
183 int camellia_crypt_ctr( camellia_context *ctx,
184 size_t length,
185 size_t *nc_off,
186 unsigned char nonce_counter[16],
187 unsigned char stream_block[16],
188 const unsigned char *input,
189 unsigned char *output );
190 #endif /* POLARSSL_CIPHER_MODE_CTR */
192 #ifdef __cplusplus
194 #endif
196 #else /* POLARSSL_CAMELLIA_ALT */
197 #include "camellia_alt.h"
198 #endif /* POLARSSL_CAMELLIA_ALT */
200 #ifdef __cplusplus
201 extern "C" {
202 #endif
205 * \brief Checkup routine
207 * \return 0 if successful, or 1 if the test failed
209 int camellia_self_test( int verbose );
211 #ifdef __cplusplus
213 #endif
215 #endif /* camellia.h */