blob: 3020e14e21b8ecde5a8dfe851c3efc67d28e34f6 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
Paul Bakkerfae35f02013-03-13 10:33:51 +01002 * \file cipher_wrap.c
Paul Bakker8123e9d2011-01-06 15:37:30 +00003 *
Paul Bakker20281562011-11-11 10:34:04 +00004 * \brief Generic cipher wrapper for PolarSSL
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Paul Bakker68884e32013-01-07 18:20:04 +01008 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakker8123e9d2011-01-06 15:37:30 +00009 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
30#include "polarssl/config.h"
31
32#if defined(POLARSSL_CIPHER_C)
33
34#include "polarssl/cipher_wrap.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000035
36#if defined(POLARSSL_AES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000037#include "polarssl/aes.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000038#endif
39
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020040#if defined(POLARSSL_ARC4_C)
41#include "polarssl/arc4.h"
42#endif
43
Paul Bakkerf6543712012-03-05 14:01:29 +000044#if defined(POLARSSL_CAMELLIA_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000045#include "polarssl/camellia.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000046#endif
47
48#if defined(POLARSSL_DES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000049#include "polarssl/des.h"
Paul Bakker02f61692012-03-15 10:54:25 +000050#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000051
Paul Bakker6132d0a2012-07-04 17:10:40 +000052#if defined(POLARSSL_BLOWFISH_C)
53#include "polarssl/blowfish.h"
54#endif
55
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020056#if defined(POLARSSL_GCM_C)
57#include "polarssl/gcm.h"
58#endif
59
Paul Bakker6e339b52013-07-03 13:37:05 +020060#if defined(POLARSSL_MEMORY_C)
61#include "polarssl/memory.h"
62#else
63#define polarssl_malloc malloc
64#define polarssl_free free
65#endif
66
Paul Bakker8123e9d2011-01-06 15:37:30 +000067#include <stdlib.h>
68
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020069#if defined(POLARSSL_GCM_C)
70/* shared by all GCM ciphers */
71static void *gcm_ctx_alloc( void )
72{
73 return polarssl_malloc( sizeof( gcm_context ) );
74}
75
76static void gcm_ctx_free( void *ctx )
77{
78 gcm_free( ctx );
79 polarssl_free( ctx );
80}
81#endif
82
Paul Bakker8123e9d2011-01-06 15:37:30 +000083#if defined(POLARSSL_AES_C)
84
Paul Bakker5e0efa72013-09-08 23:04:04 +020085static int aes_crypt_ecb_wrap( void *ctx, operation_t operation,
86 const unsigned char *input, unsigned char *output )
87{
88 return aes_crypt_ecb( (aes_context *) ctx, operation, input, output );
89}
90
Paul Bakkerfae35f02013-03-13 10:33:51 +010091static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +000092 unsigned char *iv, const unsigned char *input, unsigned char *output )
93{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020094#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +000095 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020096#else
97 ((void) ctx);
98 ((void) operation);
99 ((void) length);
100 ((void) iv);
101 ((void) input);
102 ((void) output);
103
104 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
105#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000106}
107
Paul Bakkerfae35f02013-03-13 10:33:51 +0100108static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000109 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
110{
111#if defined(POLARSSL_CIPHER_MODE_CFB)
112 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
113#else
114 ((void) ctx);
115 ((void) operation);
116 ((void) length);
117 ((void) iv_off);
118 ((void) iv);
119 ((void) input);
120 ((void) output);
121
122 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
123#endif
124}
125
Paul Bakkerfae35f02013-03-13 10:33:51 +0100126static int aes_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000127 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
128 const unsigned char *input, unsigned char *output )
129{
130#if defined(POLARSSL_CIPHER_MODE_CTR)
131 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
132 stream_block, input, output );
133#else
134 ((void) ctx);
135 ((void) length);
136 ((void) nc_off);
137 ((void) nonce_counter);
138 ((void) stream_block);
139 ((void) input);
140 ((void) output);
141
142 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
143#endif
144}
145
Paul Bakkerfae35f02013-03-13 10:33:51 +0100146static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000147{
148 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
149}
150
Paul Bakkerfae35f02013-03-13 10:33:51 +0100151static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152{
153 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
154}
155
156static void * aes_ctx_alloc( void )
157{
Paul Bakker6e339b52013-07-03 13:37:05 +0200158 return polarssl_malloc( sizeof( aes_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000159}
160
161static void aes_ctx_free( void *ctx )
162{
Paul Bakker6e339b52013-07-03 13:37:05 +0200163 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164}
165
Paul Bakker343a8702011-06-09 14:27:58 +0000166const cipher_base_t aes_info = {
167 POLARSSL_CIPHER_ID_AES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200168 aes_crypt_ecb_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000169 aes_crypt_cbc_wrap,
170 aes_crypt_cfb128_wrap,
171 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200172 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000173 aes_setkey_enc_wrap,
174 aes_setkey_dec_wrap,
175 aes_ctx_alloc,
176 aes_ctx_free
177};
178
Paul Bakker5e0efa72013-09-08 23:04:04 +0200179const cipher_info_t aes_128_ecb_info = {
180 POLARSSL_CIPHER_AES_128_ECB,
181 POLARSSL_MODE_ECB,
182 128,
183 "AES-128-ECB",
184 16,
185 0,
186 16,
187 &aes_info
188};
189
190const cipher_info_t aes_192_ecb_info = {
191 POLARSSL_CIPHER_AES_192_ECB,
192 POLARSSL_MODE_ECB,
193 192,
194 "AES-192-ECB",
195 16,
196 0,
197 16,
198 &aes_info
199};
200
201const cipher_info_t aes_256_ecb_info = {
202 POLARSSL_CIPHER_AES_256_ECB,
203 POLARSSL_MODE_ECB,
204 256,
205 "AES-256-ECB",
206 16,
207 0,
208 16,
209 &aes_info
210};
211
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200212#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000214 POLARSSL_CIPHER_AES_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000215 POLARSSL_MODE_CBC,
216 128,
217 "AES-128-CBC",
218 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200219 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000220 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000221 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000222};
223
224const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000225 POLARSSL_CIPHER_AES_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000226 POLARSSL_MODE_CBC,
227 192,
228 "AES-192-CBC",
229 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200230 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000231 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000232 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000233};
234
235const cipher_info_t aes_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000236 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000237 POLARSSL_MODE_CBC,
238 256,
239 "AES-256-CBC",
240 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200241 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000242 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000243 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000244};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200245#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000246
247#if defined(POLARSSL_CIPHER_MODE_CFB)
248const cipher_info_t aes_128_cfb128_info = {
249 POLARSSL_CIPHER_AES_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000250 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000251 128,
252 "AES-128-CFB128",
253 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200254 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000255 16,
256 &aes_info
257};
258
259const cipher_info_t aes_192_cfb128_info = {
260 POLARSSL_CIPHER_AES_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000261 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000262 192,
263 "AES-192-CFB128",
264 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200265 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000266 16,
267 &aes_info
268};
269
270const cipher_info_t aes_256_cfb128_info = {
271 POLARSSL_CIPHER_AES_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000272 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000273 256,
274 "AES-256-CFB128",
275 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200276 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000277 16,
278 &aes_info
279};
280#endif /* POLARSSL_CIPHER_MODE_CFB */
281
282#if defined(POLARSSL_CIPHER_MODE_CTR)
283const cipher_info_t aes_128_ctr_info = {
284 POLARSSL_CIPHER_AES_128_CTR,
285 POLARSSL_MODE_CTR,
286 128,
287 "AES-128-CTR",
288 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200289 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000290 16,
291 &aes_info
292};
293
294const cipher_info_t aes_192_ctr_info = {
295 POLARSSL_CIPHER_AES_192_CTR,
296 POLARSSL_MODE_CTR,
297 192,
298 "AES-192-CTR",
299 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200300 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000301 16,
302 &aes_info
303};
304
305const cipher_info_t aes_256_ctr_info = {
306 POLARSSL_CIPHER_AES_256_CTR,
307 POLARSSL_MODE_CTR,
308 256,
309 "AES-256-CTR",
310 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200311 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000312 16,
313 &aes_info
314};
315#endif /* POLARSSL_CIPHER_MODE_CTR */
316
Paul Bakker68884e32013-01-07 18:20:04 +0100317#if defined(POLARSSL_GCM_C)
Paul Bakker43aff2a2013-09-09 00:10:27 +0200318static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200319{
Paul Bakker43aff2a2013-09-09 00:10:27 +0200320 return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_AES,
321 key, key_length );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200322}
323
324const cipher_base_t gcm_aes_info = {
325 POLARSSL_CIPHER_ID_AES,
326 NULL,
327 NULL,
328 NULL,
329 NULL,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200330 NULL,
Paul Bakker43aff2a2013-09-09 00:10:27 +0200331 gcm_aes_setkey_wrap,
332 gcm_aes_setkey_wrap,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200333 gcm_ctx_alloc,
334 gcm_ctx_free,
335};
336
Paul Bakker68884e32013-01-07 18:20:04 +0100337const cipher_info_t aes_128_gcm_info = {
338 POLARSSL_CIPHER_AES_128_GCM,
339 POLARSSL_MODE_GCM,
340 128,
341 "AES-128-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200342 12,
343 1,
Paul Bakker68884e32013-01-07 18:20:04 +0100344 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200345 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100346};
347
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200348const cipher_info_t aes_192_gcm_info = {
349 POLARSSL_CIPHER_AES_192_GCM,
350 POLARSSL_MODE_GCM,
351 192,
352 "AES-192-GCM",
353 12,
354 1,
355 16,
356 &gcm_aes_info
357};
358
Paul Bakker68884e32013-01-07 18:20:04 +0100359const cipher_info_t aes_256_gcm_info = {
360 POLARSSL_CIPHER_AES_256_GCM,
361 POLARSSL_MODE_GCM,
362 256,
363 "AES-256-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200364 12,
365 1,
Paul Bakker68884e32013-01-07 18:20:04 +0100366 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200367 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100368};
369#endif /* POLARSSL_GCM_C */
370
Paul Bakker8123e9d2011-01-06 15:37:30 +0000371#endif
372
373#if defined(POLARSSL_CAMELLIA_C)
374
Paul Bakker5e0efa72013-09-08 23:04:04 +0200375static int camellia_crypt_ecb_wrap( void *ctx, operation_t operation,
376 const unsigned char *input, unsigned char *output )
377{
378 return camellia_crypt_ecb( (camellia_context *) ctx, operation, input, output );
379}
380
Paul Bakkerfae35f02013-03-13 10:33:51 +0100381static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382 unsigned char *iv, const unsigned char *input, unsigned char *output )
383{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200384#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200386#else
387 ((void) ctx);
388 ((void) operation);
389 ((void) length);
390 ((void) iv);
391 ((void) input);
392 ((void) output);
393
394 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
395#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000396}
397
Paul Bakkerfae35f02013-03-13 10:33:51 +0100398static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000399 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
400{
401#if defined(POLARSSL_CIPHER_MODE_CFB)
402 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
403#else
404 ((void) ctx);
405 ((void) operation);
406 ((void) length);
407 ((void) iv_off);
408 ((void) iv);
409 ((void) input);
410 ((void) output);
411
412 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
413#endif
414}
415
Paul Bakkerfae35f02013-03-13 10:33:51 +0100416static int camellia_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000417 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
418 const unsigned char *input, unsigned char *output )
419{
420#if defined(POLARSSL_CIPHER_MODE_CTR)
421 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
422 stream_block, input, output );
423#else
424 ((void) ctx);
425 ((void) length);
426 ((void) nc_off);
427 ((void) nonce_counter);
428 ((void) stream_block);
429 ((void) input);
430 ((void) output);
431
432 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
433#endif
434}
435
Paul Bakkerfae35f02013-03-13 10:33:51 +0100436static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000437{
438 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
439}
440
Paul Bakkerfae35f02013-03-13 10:33:51 +0100441static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000442{
443 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
444}
445
446static void * camellia_ctx_alloc( void )
447{
Paul Bakker6e339b52013-07-03 13:37:05 +0200448 return polarssl_malloc( sizeof( camellia_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449}
450
451static void camellia_ctx_free( void *ctx )
452{
Paul Bakker6e339b52013-07-03 13:37:05 +0200453 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000454}
455
Paul Bakker343a8702011-06-09 14:27:58 +0000456const cipher_base_t camellia_info = {
457 POLARSSL_CIPHER_ID_CAMELLIA,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200458 camellia_crypt_ecb_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000459 camellia_crypt_cbc_wrap,
460 camellia_crypt_cfb128_wrap,
461 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200462 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000463 camellia_setkey_enc_wrap,
464 camellia_setkey_dec_wrap,
465 camellia_ctx_alloc,
466 camellia_ctx_free
467};
468
Paul Bakker5e0efa72013-09-08 23:04:04 +0200469const cipher_info_t camellia_128_ecb_info = {
470 POLARSSL_CIPHER_CAMELLIA_128_ECB,
471 POLARSSL_MODE_ECB,
472 128,
473 "CAMELLIA-128-ECB",
474 16,
475 0,
476 16,
477 &camellia_info
478};
479
480const cipher_info_t camellia_192_ecb_info = {
481 POLARSSL_CIPHER_CAMELLIA_192_ECB,
482 POLARSSL_MODE_ECB,
483 192,
484 "CAMELLIA-192-ECB",
485 16,
486 0,
487 16,
488 &camellia_info
489};
490
491const cipher_info_t camellia_256_ecb_info = {
492 POLARSSL_CIPHER_CAMELLIA_256_ECB,
493 POLARSSL_MODE_ECB,
494 256,
495 "CAMELLIA-256-ECB",
496 16,
497 0,
498 16,
499 &camellia_info
500};
501
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200502#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000503const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000504 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000505 POLARSSL_MODE_CBC,
506 128,
507 "CAMELLIA-128-CBC",
508 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200509 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000510 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000511 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000512};
513
514const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000515 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000516 POLARSSL_MODE_CBC,
517 192,
518 "CAMELLIA-192-CBC",
519 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200520 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000521 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000522 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000523};
524
525const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000526 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000527 POLARSSL_MODE_CBC,
528 256,
529 "CAMELLIA-256-CBC",
530 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200531 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000532 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000533 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000534};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200535#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000536
537#if defined(POLARSSL_CIPHER_MODE_CFB)
538const cipher_info_t camellia_128_cfb128_info = {
539 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000540 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000541 128,
542 "CAMELLIA-128-CFB128",
543 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200544 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000545 16,
546 &camellia_info
547};
548
549const cipher_info_t camellia_192_cfb128_info = {
550 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000551 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000552 192,
553 "CAMELLIA-192-CFB128",
554 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200555 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000556 16,
557 &camellia_info
558};
559
560const cipher_info_t camellia_256_cfb128_info = {
561 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000562 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000563 256,
564 "CAMELLIA-256-CFB128",
565 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200566 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000567 16,
568 &camellia_info
569};
570#endif /* POLARSSL_CIPHER_MODE_CFB */
571
572#if defined(POLARSSL_CIPHER_MODE_CTR)
573const cipher_info_t camellia_128_ctr_info = {
574 POLARSSL_CIPHER_CAMELLIA_128_CTR,
575 POLARSSL_MODE_CTR,
576 128,
577 "CAMELLIA-128-CTR",
578 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200579 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000580 16,
581 &camellia_info
582};
583
584const cipher_info_t camellia_192_ctr_info = {
585 POLARSSL_CIPHER_CAMELLIA_192_CTR,
586 POLARSSL_MODE_CTR,
587 192,
588 "CAMELLIA-192-CTR",
589 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200590 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000591 16,
592 &camellia_info
593};
594
595const cipher_info_t camellia_256_ctr_info = {
596 POLARSSL_CIPHER_CAMELLIA_256_CTR,
597 POLARSSL_MODE_CTR,
598 256,
599 "CAMELLIA-256-CTR",
600 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200601 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000602 16,
603 &camellia_info
604};
605#endif /* POLARSSL_CIPHER_MODE_CTR */
606
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200607#if defined(POLARSSL_GCM_C)
608static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
609{
610 return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_CAMELLIA,
611 key, key_length );
612}
613
614const cipher_base_t gcm_camellia_info = {
615 POLARSSL_CIPHER_ID_CAMELLIA,
616 NULL,
617 NULL,
618 NULL,
619 NULL,
620 NULL,
621 gcm_camellia_setkey_wrap,
622 gcm_camellia_setkey_wrap,
623 gcm_ctx_alloc,
624 gcm_ctx_free,
625};
626
627const cipher_info_t camellia_128_gcm_info = {
628 POLARSSL_CIPHER_CAMELLIA_128_GCM,
629 POLARSSL_MODE_GCM,
630 128,
631 "CAMELLIA-128-GCM",
632 12,
633 1,
634 16,
635 &gcm_camellia_info
636};
637
638const cipher_info_t camellia_192_gcm_info = {
639 POLARSSL_CIPHER_CAMELLIA_192_GCM,
640 POLARSSL_MODE_GCM,
641 192,
642 "CAMELLIA-192-GCM",
643 12,
644 1,
645 16,
646 &gcm_camellia_info
647};
648
649const cipher_info_t camellia_256_gcm_info = {
650 POLARSSL_CIPHER_CAMELLIA_256_GCM,
651 POLARSSL_MODE_GCM,
652 256,
653 "CAMELLIA-256-GCM",
654 12,
655 1,
656 16,
657 &gcm_camellia_info
658};
659#endif /* POLARSSL_GCM_C */
660
661#endif /* POLARSSL_CAMELLIA_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662
663#if defined(POLARSSL_DES_C)
664
Paul Bakker5e0efa72013-09-08 23:04:04 +0200665static int des_crypt_ecb_wrap( void *ctx, operation_t operation,
666 const unsigned char *input, unsigned char *output )
667{
668 ((void) operation);
669 return des_crypt_ecb( (des_context *) ctx, input, output );
670}
671
672static int des3_crypt_ecb_wrap( void *ctx, operation_t operation,
673 const unsigned char *input, unsigned char *output )
674{
675 ((void) operation);
676 return des3_crypt_ecb( (des3_context *) ctx, input, output );
677}
678
Paul Bakkerfae35f02013-03-13 10:33:51 +0100679static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000680 unsigned char *iv, const unsigned char *input, unsigned char *output )
681{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200682#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000683 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200684#else
685 ((void) ctx);
686 ((void) operation);
687 ((void) length);
688 ((void) iv);
689 ((void) input);
690 ((void) output);
691
692 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
693#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000694}
695
Paul Bakkerfae35f02013-03-13 10:33:51 +0100696static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000697 unsigned char *iv, const unsigned char *input, unsigned char *output )
698{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200699#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000700 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200701#else
702 ((void) ctx);
703 ((void) operation);
704 ((void) length);
705 ((void) iv);
706 ((void) input);
707 ((void) output);
708
709 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
710#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000711}
712
Paul Bakkerfae35f02013-03-13 10:33:51 +0100713static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000714 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
715{
716 ((void) ctx);
717 ((void) operation);
718 ((void) length);
719 ((void) iv_off);
720 ((void) iv);
721 ((void) input);
722 ((void) output);
723
724 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
725}
726
Paul Bakkerfae35f02013-03-13 10:33:51 +0100727static int des_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000728 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
729 const unsigned char *input, unsigned char *output )
730{
731 ((void) ctx);
732 ((void) length);
733 ((void) nc_off);
734 ((void) nonce_counter);
735 ((void) stream_block);
736 ((void) input);
737 ((void) output);
738
739 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
740}
741
Paul Bakkerfae35f02013-03-13 10:33:51 +0100742static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000743{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000744 ((void) key_length);
745
Paul Bakker8123e9d2011-01-06 15:37:30 +0000746 return des_setkey_dec( (des_context *) ctx, key );
747}
748
Paul Bakkerfae35f02013-03-13 10:33:51 +0100749static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000750{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000751 ((void) key_length);
752
Paul Bakker8123e9d2011-01-06 15:37:30 +0000753 return des_setkey_enc( (des_context *) ctx, key );
754}
755
Paul Bakkerfae35f02013-03-13 10:33:51 +0100756static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000757{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000758 ((void) key_length);
759
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760 return des3_set2key_dec( (des3_context *) ctx, key );
761}
762
Paul Bakkerfae35f02013-03-13 10:33:51 +0100763static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000764{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000765 ((void) key_length);
766
Paul Bakker8123e9d2011-01-06 15:37:30 +0000767 return des3_set2key_enc( (des3_context *) ctx, key );
768}
769
Paul Bakkerfae35f02013-03-13 10:33:51 +0100770static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000771{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000772 ((void) key_length);
773
Paul Bakker8123e9d2011-01-06 15:37:30 +0000774 return des3_set3key_dec( (des3_context *) ctx, key );
775}
776
Paul Bakkerfae35f02013-03-13 10:33:51 +0100777static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000778{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000779 ((void) key_length);
780
Paul Bakker8123e9d2011-01-06 15:37:30 +0000781 return des3_set3key_enc( (des3_context *) ctx, key );
782}
783
784static void * des_ctx_alloc( void )
785{
Paul Bakker6e339b52013-07-03 13:37:05 +0200786 return polarssl_malloc( sizeof( des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000787}
788
789static void * des3_ctx_alloc( void )
790{
Paul Bakker6e339b52013-07-03 13:37:05 +0200791 return polarssl_malloc( sizeof( des3_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000792}
793
794static void des_ctx_free( void *ctx )
795{
Paul Bakker6e339b52013-07-03 13:37:05 +0200796 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000797}
798
Paul Bakker343a8702011-06-09 14:27:58 +0000799const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000800 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200801 des_crypt_ecb_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000802 des_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000803 des_crypt_cfb128_wrap,
804 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200805 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000806 des_setkey_enc_wrap,
807 des_setkey_dec_wrap,
808 des_ctx_alloc,
809 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000810};
811
Paul Bakker5e0efa72013-09-08 23:04:04 +0200812const cipher_info_t des_ecb_info = {
813 POLARSSL_CIPHER_DES_ECB,
814 POLARSSL_MODE_ECB,
815 POLARSSL_KEY_LENGTH_DES,
816 "DES-ECB",
817 8,
818 0,
819 8,
820 &des_info
821};
822
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200823#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000824const cipher_info_t des_cbc_info = {
825 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000826 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000827 POLARSSL_KEY_LENGTH_DES,
828 "DES-CBC",
829 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200830 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000831 8,
832 &des_info
833};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200834#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000835
836const cipher_base_t des_ede_info = {
837 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200838 des3_crypt_ecb_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000839 des3_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000840 des_crypt_cfb128_wrap,
841 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200842 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000843 des3_set2key_enc_wrap,
844 des3_set2key_dec_wrap,
845 des3_ctx_alloc,
846 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000847};
848
Paul Bakker5e0efa72013-09-08 23:04:04 +0200849const cipher_info_t des_ede_ecb_info = {
850 POLARSSL_CIPHER_DES_EDE_ECB,
851 POLARSSL_MODE_ECB,
852 POLARSSL_KEY_LENGTH_DES_EDE,
853 "DES-EDE-ECB",
854 8,
855 0,
856 8,
857 &des_ede_info
858};
859
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200860#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000861const cipher_info_t des_ede_cbc_info = {
862 POLARSSL_CIPHER_DES_EDE_CBC,
863 POLARSSL_MODE_CBC,
864 POLARSSL_KEY_LENGTH_DES_EDE,
865 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +0200866 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200867 0,
Paul Bakker0e342352013-06-24 19:33:02 +0200868 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000869 &des_ede_info
870};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200871#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000872
873const cipher_base_t des_ede3_info = {
874 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200875 des3_crypt_ecb_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000876 des3_crypt_cbc_wrap,
877 des_crypt_cfb128_wrap,
878 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200879 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000880 des3_set3key_enc_wrap,
881 des3_set3key_dec_wrap,
882 des3_ctx_alloc,
883 des_ctx_free
884};
885
Paul Bakker5e0efa72013-09-08 23:04:04 +0200886const cipher_info_t des_ede3_ecb_info = {
887 POLARSSL_CIPHER_DES_EDE3_ECB,
888 POLARSSL_MODE_ECB,
889 POLARSSL_KEY_LENGTH_DES_EDE3,
890 "DES-EDE3-ECB",
891 8,
892 0,
893 8,
894 &des_ede3_info
895};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200896#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000897const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000898 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000899 POLARSSL_MODE_CBC,
900 POLARSSL_KEY_LENGTH_DES_EDE3,
901 "DES-EDE3-CBC",
902 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200903 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000904 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000905 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000906};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200907#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000908#endif
909
Paul Bakker6132d0a2012-07-04 17:10:40 +0000910#if defined(POLARSSL_BLOWFISH_C)
911
Paul Bakker5e0efa72013-09-08 23:04:04 +0200912static int blowfish_crypt_ecb_wrap( void *ctx, operation_t operation,
913 const unsigned char *input, unsigned char *output )
914{
915 return blowfish_crypt_ecb( (blowfish_context *) ctx, operation, input, output );
916}
917
Paul Bakkerfae35f02013-03-13 10:33:51 +0100918static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000919 unsigned char *iv, const unsigned char *input, unsigned char *output )
920{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200921#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000922 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200923#else
924 ((void) ctx);
925 ((void) operation);
926 ((void) length);
927 ((void) iv);
928 ((void) input);
929 ((void) output);
930
931 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
932#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +0000933}
934
Paul Bakkerfae35f02013-03-13 10:33:51 +0100935static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000936 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
937{
938#if defined(POLARSSL_CIPHER_MODE_CFB)
939 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
940#else
941 ((void) ctx);
942 ((void) operation);
943 ((void) length);
944 ((void) iv_off);
945 ((void) iv);
946 ((void) input);
947 ((void) output);
948
949 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
950#endif
951}
952
Paul Bakkerfae35f02013-03-13 10:33:51 +0100953static int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000954 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
955 const unsigned char *input, unsigned char *output )
956{
957#if defined(POLARSSL_CIPHER_MODE_CTR)
958 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
959 stream_block, input, output );
960#else
961 ((void) ctx);
962 ((void) length);
963 ((void) nc_off);
964 ((void) nonce_counter);
965 ((void) stream_block);
966 ((void) input);
967 ((void) output);
968
969 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
970#endif
971}
972
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200973static int blowfish_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000974{
975 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
976}
977
978static void * blowfish_ctx_alloc( void )
979{
Paul Bakker6e339b52013-07-03 13:37:05 +0200980 return polarssl_malloc( sizeof( blowfish_context ) );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000981}
982
983static void blowfish_ctx_free( void *ctx )
984{
Paul Bakker6e339b52013-07-03 13:37:05 +0200985 polarssl_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000986}
987
988const cipher_base_t blowfish_info = {
989 POLARSSL_CIPHER_ID_BLOWFISH,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200990 blowfish_crypt_ecb_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000991 blowfish_crypt_cbc_wrap,
992 blowfish_crypt_cfb64_wrap,
993 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200994 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200995 blowfish_setkey_wrap,
996 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000997 blowfish_ctx_alloc,
998 blowfish_ctx_free
999};
1000
Paul Bakker5e0efa72013-09-08 23:04:04 +02001001const cipher_info_t blowfish_ecb_info = {
1002 POLARSSL_CIPHER_BLOWFISH_ECB,
1003 POLARSSL_MODE_ECB,
1004 128,
1005 "BLOWFISH-ECB",
1006 8,
1007 0,
1008 8,
1009 &blowfish_info
1010};
1011
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001012#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001013const cipher_info_t blowfish_cbc_info = {
1014 POLARSSL_CIPHER_BLOWFISH_CBC,
1015 POLARSSL_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001016 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001017 "BLOWFISH-CBC",
1018 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001019 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001020 8,
1021 &blowfish_info
1022};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001023#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001024
1025#if defined(POLARSSL_CIPHER_MODE_CFB)
1026const cipher_info_t blowfish_cfb64_info = {
1027 POLARSSL_CIPHER_BLOWFISH_CFB64,
1028 POLARSSL_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001029 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001030 "BLOWFISH-CFB64",
1031 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001032 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001033 8,
1034 &blowfish_info
1035};
1036#endif /* POLARSSL_CIPHER_MODE_CFB */
1037
1038#if defined(POLARSSL_CIPHER_MODE_CTR)
1039const cipher_info_t blowfish_ctr_info = {
1040 POLARSSL_CIPHER_BLOWFISH_CTR,
1041 POLARSSL_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001042 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001043 "BLOWFISH-CTR",
1044 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001045 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001046 8,
1047 &blowfish_info
1048};
1049#endif /* POLARSSL_CIPHER_MODE_CTR */
1050#endif /* POLARSSL_BLOWFISH_C */
1051
Paul Bakker68884e32013-01-07 18:20:04 +01001052#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001053static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1054 const unsigned char *input,
1055 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +01001056{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001057 return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001058}
1059
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001060static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
1061 unsigned int key_length )
1062{
Manuel Pégourié-Gonnardce411252013-09-04 12:28:37 +02001063 /* we get key_length in bits, arc4 expects it in bytes */
1064 if( key_length % 8 != 0)
1065 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
1066
1067 arc4_setup( (arc4_context *) ctx, key, key_length / 8 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001068 return( 0 );
1069}
1070
1071static void * arc4_ctx_alloc( void )
1072{
1073 return polarssl_malloc( sizeof( arc4_context ) );
1074}
Paul Bakker68884e32013-01-07 18:20:04 +01001075
1076static void arc4_ctx_free( void *ctx )
1077{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001078 polarssl_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +01001079}
1080
1081const cipher_base_t arc4_base_info = {
1082 POLARSSL_CIPHER_ID_ARC4,
1083 NULL,
1084 NULL,
1085 NULL,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001086 NULL,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001087 arc4_crypt_stream_wrap,
1088 arc4_setkey_wrap,
1089 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +01001090 arc4_ctx_alloc,
1091 arc4_ctx_free
1092};
1093
1094const cipher_info_t arc4_128_info = {
1095 POLARSSL_CIPHER_ARC4_128,
1096 POLARSSL_MODE_STREAM,
1097 128,
1098 "ARC4-128",
1099 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001100 0,
Paul Bakker68884e32013-01-07 18:20:04 +01001101 1,
1102 &arc4_base_info
1103};
1104#endif /* POLARSSL_ARC4_C */
1105
Paul Bakkerfab5c822012-02-06 16:45:10 +00001106#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001107static int null_crypt_stream( void *ctx, size_t length,
1108 const unsigned char *input,
1109 unsigned char *output )
1110{
1111 ((void) ctx);
1112 memmove( output, input, length );
1113 return( 0 );
1114}
1115
1116static int null_setkey( void *ctx, const unsigned char *key,
1117 unsigned int key_length )
1118{
1119 ((void) ctx);
1120 ((void) key);
1121 ((void) key_length);
1122
1123 return( 0 );
1124}
1125
Paul Bakkerfab5c822012-02-06 16:45:10 +00001126static void * null_ctx_alloc( void )
1127{
1128 return (void *) 1;
1129}
1130
Paul Bakkerfab5c822012-02-06 16:45:10 +00001131static void null_ctx_free( void *ctx )
1132{
1133 ((void) ctx);
1134}
1135
1136const cipher_base_t null_base_info = {
1137 POLARSSL_CIPHER_ID_NULL,
1138 NULL,
1139 NULL,
1140 NULL,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001141 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001142 null_crypt_stream,
1143 null_setkey,
1144 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001145 null_ctx_alloc,
1146 null_ctx_free
1147};
1148
1149const cipher_info_t null_cipher_info = {
1150 POLARSSL_CIPHER_NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001151 POLARSSL_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001152 0,
1153 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +01001154 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001155 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001156 1,
1157 &null_base_info
1158};
1159#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
1160
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001161const cipher_definition_t cipher_definitions[] =
1162{
1163#if defined(POLARSSL_AES_C)
1164 { POLARSSL_CIPHER_AES_128_ECB, &aes_128_ecb_info },
1165 { POLARSSL_CIPHER_AES_192_ECB, &aes_192_ecb_info },
1166 { POLARSSL_CIPHER_AES_256_ECB, &aes_256_ecb_info },
1167#if defined(POLARSSL_CIPHER_MODE_CBC)
1168 { POLARSSL_CIPHER_AES_128_CBC, &aes_128_cbc_info },
1169 { POLARSSL_CIPHER_AES_192_CBC, &aes_192_cbc_info },
1170 { POLARSSL_CIPHER_AES_256_CBC, &aes_256_cbc_info },
1171#endif
1172#if defined(POLARSSL_CIPHER_MODE_CFB)
1173 { POLARSSL_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
1174 { POLARSSL_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
1175 { POLARSSL_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
1176#endif
1177#if defined(POLARSSL_CIPHER_MODE_CTR)
1178 { POLARSSL_CIPHER_AES_128_CTR, &aes_128_ctr_info },
1179 { POLARSSL_CIPHER_AES_192_CTR, &aes_192_ctr_info },
1180 { POLARSSL_CIPHER_AES_256_CTR, &aes_256_ctr_info },
1181#endif
1182#if defined(POLARSSL_GCM_C)
1183 { POLARSSL_CIPHER_AES_128_GCM, &aes_128_gcm_info },
1184 { POLARSSL_CIPHER_AES_192_GCM, &aes_192_gcm_info },
1185 { POLARSSL_CIPHER_AES_256_GCM, &aes_256_gcm_info },
1186#endif
1187#endif /* POLARSSL_AES_C */
1188
1189#if defined(POLARSSL_ARC4_C)
1190 { POLARSSL_CIPHER_ARC4_128, &arc4_128_info },
1191#endif
1192
1193#if defined(POLARSSL_BLOWFISH_C)
1194 { POLARSSL_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
1195#if defined(POLARSSL_CIPHER_MODE_CBC)
1196 { POLARSSL_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
1197#endif
1198#if defined(POLARSSL_CIPHER_MODE_CFB)
1199 { POLARSSL_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
1200#endif
1201#if defined(POLARSSL_CIPHER_MODE_CTR)
1202 { POLARSSL_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
1203#endif
1204#endif /* POLARSSL_BLOWFISH_C */
1205
1206#if defined(POLARSSL_CAMELLIA_C)
1207 { POLARSSL_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
1208 { POLARSSL_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
Manuel Pégourié-Gonnard13e0d442013-10-24 12:59:00 +02001209 { POLARSSL_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001210#if defined(POLARSSL_CIPHER_MODE_CBC)
1211 { POLARSSL_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
1212 { POLARSSL_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
1213 { POLARSSL_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
1214#endif
1215#if defined(POLARSSL_CIPHER_MODE_CFB)
1216 { POLARSSL_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
1217 { POLARSSL_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
1218 { POLARSSL_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
1219#endif
1220#if defined(POLARSSL_CIPHER_MODE_CTR)
1221 { POLARSSL_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
1222 { POLARSSL_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
1223 { POLARSSL_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
1224#endif
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +02001225#if defined(POLARSSL_GCM_C)
1226 { POLARSSL_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
1227 { POLARSSL_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
1228 { POLARSSL_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
1229#endif
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001230#endif /* POLARSSL_CAMELLIA_C */
1231
1232#if defined(POLARSSL_DES_C)
1233 { POLARSSL_CIPHER_DES_ECB, &des_ecb_info },
1234 { POLARSSL_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
1235 { POLARSSL_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
1236#if defined(POLARSSL_CIPHER_MODE_CBC)
1237 { POLARSSL_CIPHER_DES_CBC, &des_cbc_info },
1238 { POLARSSL_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
1239 { POLARSSL_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
1240#endif
1241#endif /* POLARSSL_DES_C */
1242
1243#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard057e0cf2013-10-14 14:19:31 +02001244 { POLARSSL_CIPHER_NULL, &null_cipher_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001245#endif /* POLARSSL_CIPHER_NULL_CIPHER */
1246
1247 { 0, NULL }
1248};
1249
1250#define NUM_CIPHERS sizeof cipher_definitions / sizeof cipher_definitions[0]
1251int supported_ciphers[NUM_CIPHERS];
1252
Paul Bakker8123e9d2011-01-06 15:37:30 +00001253#endif