blob: 79daaf9877ebd7964b235d994b106706d42913e9 [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
69#if defined(POLARSSL_AES_C)
70
Paul Bakkerfae35f02013-03-13 10:33:51 +010071static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +000072 unsigned char *iv, const unsigned char *input, unsigned char *output )
73{
74 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
75}
76
Paul Bakkerfae35f02013-03-13 10:33:51 +010077static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000078 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
79{
80#if defined(POLARSSL_CIPHER_MODE_CFB)
81 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
82#else
83 ((void) ctx);
84 ((void) operation);
85 ((void) length);
86 ((void) iv_off);
87 ((void) iv);
88 ((void) input);
89 ((void) output);
90
91 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
92#endif
93}
94
Paul Bakkerfae35f02013-03-13 10:33:51 +010095static int aes_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000096 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
97 const unsigned char *input, unsigned char *output )
98{
99#if defined(POLARSSL_CIPHER_MODE_CTR)
100 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
101 stream_block, input, output );
102#else
103 ((void) ctx);
104 ((void) length);
105 ((void) nc_off);
106 ((void) nonce_counter);
107 ((void) stream_block);
108 ((void) input);
109 ((void) output);
110
111 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
112#endif
113}
114
Paul Bakkerfae35f02013-03-13 10:33:51 +0100115static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000116{
117 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
118}
119
Paul Bakkerfae35f02013-03-13 10:33:51 +0100120static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000121{
122 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
123}
124
125static void * aes_ctx_alloc( void )
126{
Paul Bakker6e339b52013-07-03 13:37:05 +0200127 return polarssl_malloc( sizeof( aes_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000128}
129
130static void aes_ctx_free( void *ctx )
131{
Paul Bakker6e339b52013-07-03 13:37:05 +0200132 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000133}
134
Paul Bakker343a8702011-06-09 14:27:58 +0000135const cipher_base_t aes_info = {
136 POLARSSL_CIPHER_ID_AES,
137 aes_crypt_cbc_wrap,
138 aes_crypt_cfb128_wrap,
139 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200140 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000141 aes_setkey_enc_wrap,
142 aes_setkey_dec_wrap,
143 aes_ctx_alloc,
144 aes_ctx_free
145};
146
Paul Bakker8123e9d2011-01-06 15:37:30 +0000147const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000148 POLARSSL_CIPHER_AES_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000149 POLARSSL_MODE_CBC,
150 128,
151 "AES-128-CBC",
152 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200153 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000154 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000155 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156};
157
158const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000159 POLARSSL_CIPHER_AES_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000160 POLARSSL_MODE_CBC,
161 192,
162 "AES-192-CBC",
163 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200164 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000165 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000166 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000167};
168
169const cipher_info_t aes_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000170 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000171 POLARSSL_MODE_CBC,
172 256,
173 "AES-256-CBC",
174 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200175 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000176 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000177 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000178};
Paul Bakker343a8702011-06-09 14:27:58 +0000179
180#if defined(POLARSSL_CIPHER_MODE_CFB)
181const cipher_info_t aes_128_cfb128_info = {
182 POLARSSL_CIPHER_AES_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000183 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000184 128,
185 "AES-128-CFB128",
186 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200187 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000188 16,
189 &aes_info
190};
191
192const cipher_info_t aes_192_cfb128_info = {
193 POLARSSL_CIPHER_AES_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000194 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000195 192,
196 "AES-192-CFB128",
197 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200198 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000199 16,
200 &aes_info
201};
202
203const cipher_info_t aes_256_cfb128_info = {
204 POLARSSL_CIPHER_AES_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000205 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000206 256,
207 "AES-256-CFB128",
208 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200209 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000210 16,
211 &aes_info
212};
213#endif /* POLARSSL_CIPHER_MODE_CFB */
214
215#if defined(POLARSSL_CIPHER_MODE_CTR)
216const cipher_info_t aes_128_ctr_info = {
217 POLARSSL_CIPHER_AES_128_CTR,
218 POLARSSL_MODE_CTR,
219 128,
220 "AES-128-CTR",
221 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200222 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000223 16,
224 &aes_info
225};
226
227const cipher_info_t aes_192_ctr_info = {
228 POLARSSL_CIPHER_AES_192_CTR,
229 POLARSSL_MODE_CTR,
230 192,
231 "AES-192-CTR",
232 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200233 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000234 16,
235 &aes_info
236};
237
238const cipher_info_t aes_256_ctr_info = {
239 POLARSSL_CIPHER_AES_256_CTR,
240 POLARSSL_MODE_CTR,
241 256,
242 "AES-256-CTR",
243 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200244 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000245 16,
246 &aes_info
247};
248#endif /* POLARSSL_CIPHER_MODE_CTR */
249
Paul Bakker68884e32013-01-07 18:20:04 +0100250#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200251static void *gcm_ctx_alloc( void )
252{
253 return polarssl_malloc( sizeof( gcm_context ) );
254}
255
256static void gcm_ctx_free( void *ctx )
257{
258 polarssl_free( ctx );
259}
260
261static int gcm_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
262{
263 return gcm_init( (gcm_context *) ctx, key, key_length );
264}
265
266const cipher_base_t gcm_aes_info = {
267 POLARSSL_CIPHER_ID_AES,
268 NULL,
269 NULL,
270 NULL,
271 NULL,
272 gcm_setkey_wrap,
273 gcm_setkey_wrap,
274 gcm_ctx_alloc,
275 gcm_ctx_free,
276};
277
Paul Bakker68884e32013-01-07 18:20:04 +0100278const cipher_info_t aes_128_gcm_info = {
279 POLARSSL_CIPHER_AES_128_GCM,
280 POLARSSL_MODE_GCM,
281 128,
282 "AES-128-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200283 12,
284 1,
Paul Bakker68884e32013-01-07 18:20:04 +0100285 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200286 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100287};
288
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200289const cipher_info_t aes_192_gcm_info = {
290 POLARSSL_CIPHER_AES_192_GCM,
291 POLARSSL_MODE_GCM,
292 192,
293 "AES-192-GCM",
294 12,
295 1,
296 16,
297 &gcm_aes_info
298};
299
Paul Bakker68884e32013-01-07 18:20:04 +0100300const cipher_info_t aes_256_gcm_info = {
301 POLARSSL_CIPHER_AES_256_GCM,
302 POLARSSL_MODE_GCM,
303 256,
304 "AES-256-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200305 12,
306 1,
Paul Bakker68884e32013-01-07 18:20:04 +0100307 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200308 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100309};
310#endif /* POLARSSL_GCM_C */
311
Paul Bakker8123e9d2011-01-06 15:37:30 +0000312#endif
313
314#if defined(POLARSSL_CAMELLIA_C)
315
Paul Bakkerfae35f02013-03-13 10:33:51 +0100316static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000317 unsigned char *iv, const unsigned char *input, unsigned char *output )
318{
319 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
320}
321
Paul Bakkerfae35f02013-03-13 10:33:51 +0100322static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000323 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
324{
325#if defined(POLARSSL_CIPHER_MODE_CFB)
326 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
327#else
328 ((void) ctx);
329 ((void) operation);
330 ((void) length);
331 ((void) iv_off);
332 ((void) iv);
333 ((void) input);
334 ((void) output);
335
336 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
337#endif
338}
339
Paul Bakkerfae35f02013-03-13 10:33:51 +0100340static int camellia_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000341 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
342 const unsigned char *input, unsigned char *output )
343{
344#if defined(POLARSSL_CIPHER_MODE_CTR)
345 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
346 stream_block, input, output );
347#else
348 ((void) ctx);
349 ((void) length);
350 ((void) nc_off);
351 ((void) nonce_counter);
352 ((void) stream_block);
353 ((void) input);
354 ((void) output);
355
356 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
357#endif
358}
359
Paul Bakkerfae35f02013-03-13 10:33:51 +0100360static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000361{
362 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
363}
364
Paul Bakkerfae35f02013-03-13 10:33:51 +0100365static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366{
367 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
368}
369
370static void * camellia_ctx_alloc( void )
371{
Paul Bakker6e339b52013-07-03 13:37:05 +0200372 return polarssl_malloc( sizeof( camellia_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373}
374
375static void camellia_ctx_free( void *ctx )
376{
Paul Bakker6e339b52013-07-03 13:37:05 +0200377 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378}
379
Paul Bakker343a8702011-06-09 14:27:58 +0000380const cipher_base_t camellia_info = {
381 POLARSSL_CIPHER_ID_CAMELLIA,
382 camellia_crypt_cbc_wrap,
383 camellia_crypt_cfb128_wrap,
384 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200385 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000386 camellia_setkey_enc_wrap,
387 camellia_setkey_dec_wrap,
388 camellia_ctx_alloc,
389 camellia_ctx_free
390};
391
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000393 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000394 POLARSSL_MODE_CBC,
395 128,
396 "CAMELLIA-128-CBC",
397 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200398 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000399 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000400 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000401};
402
403const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000404 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000405 POLARSSL_MODE_CBC,
406 192,
407 "CAMELLIA-192-CBC",
408 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200409 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000410 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000411 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412};
413
414const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000415 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000416 POLARSSL_MODE_CBC,
417 256,
418 "CAMELLIA-256-CBC",
419 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200420 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000421 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000422 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000423};
Paul Bakker343a8702011-06-09 14:27:58 +0000424
425#if defined(POLARSSL_CIPHER_MODE_CFB)
426const cipher_info_t camellia_128_cfb128_info = {
427 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000428 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000429 128,
430 "CAMELLIA-128-CFB128",
431 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200432 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000433 16,
434 &camellia_info
435};
436
437const cipher_info_t camellia_192_cfb128_info = {
438 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000439 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000440 192,
441 "CAMELLIA-192-CFB128",
442 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200443 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000444 16,
445 &camellia_info
446};
447
448const cipher_info_t camellia_256_cfb128_info = {
449 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000450 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000451 256,
452 "CAMELLIA-256-CFB128",
453 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200454 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000455 16,
456 &camellia_info
457};
458#endif /* POLARSSL_CIPHER_MODE_CFB */
459
460#if defined(POLARSSL_CIPHER_MODE_CTR)
461const cipher_info_t camellia_128_ctr_info = {
462 POLARSSL_CIPHER_CAMELLIA_128_CTR,
463 POLARSSL_MODE_CTR,
464 128,
465 "CAMELLIA-128-CTR",
466 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200467 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000468 16,
469 &camellia_info
470};
471
472const cipher_info_t camellia_192_ctr_info = {
473 POLARSSL_CIPHER_CAMELLIA_192_CTR,
474 POLARSSL_MODE_CTR,
475 192,
476 "CAMELLIA-192-CTR",
477 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200478 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000479 16,
480 &camellia_info
481};
482
483const cipher_info_t camellia_256_ctr_info = {
484 POLARSSL_CIPHER_CAMELLIA_256_CTR,
485 POLARSSL_MODE_CTR,
486 256,
487 "CAMELLIA-256-CTR",
488 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200489 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000490 16,
491 &camellia_info
492};
493#endif /* POLARSSL_CIPHER_MODE_CTR */
494
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495#endif
496
497#if defined(POLARSSL_DES_C)
498
Paul Bakkerfae35f02013-03-13 10:33:51 +0100499static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500 unsigned char *iv, const unsigned char *input, unsigned char *output )
501{
502 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
503}
504
Paul Bakkerfae35f02013-03-13 10:33:51 +0100505static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506 unsigned char *iv, const unsigned char *input, unsigned char *output )
507{
508 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
509}
510
Paul Bakkerfae35f02013-03-13 10:33:51 +0100511static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000512 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
513{
514 ((void) ctx);
515 ((void) operation);
516 ((void) length);
517 ((void) iv_off);
518 ((void) iv);
519 ((void) input);
520 ((void) output);
521
522 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
523}
524
Paul Bakkerfae35f02013-03-13 10:33:51 +0100525static int des_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000526 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
527 const unsigned char *input, unsigned char *output )
528{
529 ((void) ctx);
530 ((void) length);
531 ((void) nc_off);
532 ((void) nonce_counter);
533 ((void) stream_block);
534 ((void) input);
535 ((void) output);
536
537 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
538}
539
Paul Bakkerfae35f02013-03-13 10:33:51 +0100540static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000541{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000542 ((void) key_length);
543
Paul Bakker8123e9d2011-01-06 15:37:30 +0000544 return des_setkey_dec( (des_context *) ctx, key );
545}
546
Paul Bakkerfae35f02013-03-13 10:33:51 +0100547static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000548{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000549 ((void) key_length);
550
Paul Bakker8123e9d2011-01-06 15:37:30 +0000551 return des_setkey_enc( (des_context *) ctx, key );
552}
553
Paul Bakkerfae35f02013-03-13 10:33:51 +0100554static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000555{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000556 ((void) key_length);
557
Paul Bakker8123e9d2011-01-06 15:37:30 +0000558 return des3_set2key_dec( (des3_context *) ctx, key );
559}
560
Paul Bakkerfae35f02013-03-13 10:33:51 +0100561static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000563 ((void) key_length);
564
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565 return des3_set2key_enc( (des3_context *) ctx, key );
566}
567
Paul Bakkerfae35f02013-03-13 10:33:51 +0100568static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000570 ((void) key_length);
571
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572 return des3_set3key_dec( (des3_context *) ctx, key );
573}
574
Paul Bakkerfae35f02013-03-13 10:33:51 +0100575static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000577 ((void) key_length);
578
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579 return des3_set3key_enc( (des3_context *) ctx, key );
580}
581
582static void * des_ctx_alloc( void )
583{
Paul Bakker6e339b52013-07-03 13:37:05 +0200584 return polarssl_malloc( sizeof( des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585}
586
587static void * des3_ctx_alloc( void )
588{
Paul Bakker6e339b52013-07-03 13:37:05 +0200589 return polarssl_malloc( sizeof( des3_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590}
591
592static void des_ctx_free( void *ctx )
593{
Paul Bakker6e339b52013-07-03 13:37:05 +0200594 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595}
596
Paul Bakker343a8702011-06-09 14:27:58 +0000597const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000598 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000599 des_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000600 des_crypt_cfb128_wrap,
601 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200602 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000603 des_setkey_enc_wrap,
604 des_setkey_dec_wrap,
605 des_ctx_alloc,
606 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607};
608
Paul Bakker343a8702011-06-09 14:27:58 +0000609const cipher_info_t des_cbc_info = {
610 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000611 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000612 POLARSSL_KEY_LENGTH_DES,
613 "DES-CBC",
614 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200615 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000616 8,
617 &des_info
618};
619
620const cipher_base_t des_ede_info = {
621 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000622 des3_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000623 des_crypt_cfb128_wrap,
624 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200625 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000626 des3_set2key_enc_wrap,
627 des3_set2key_dec_wrap,
628 des3_ctx_alloc,
629 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000630};
631
Paul Bakker343a8702011-06-09 14:27:58 +0000632const cipher_info_t des_ede_cbc_info = {
633 POLARSSL_CIPHER_DES_EDE_CBC,
634 POLARSSL_MODE_CBC,
635 POLARSSL_KEY_LENGTH_DES_EDE,
636 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +0200637 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200638 0,
Paul Bakker0e342352013-06-24 19:33:02 +0200639 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000640 &des_ede_info
641};
642
643const cipher_base_t des_ede3_info = {
644 POLARSSL_CIPHER_ID_DES,
645 des3_crypt_cbc_wrap,
646 des_crypt_cfb128_wrap,
647 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200648 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000649 des3_set3key_enc_wrap,
650 des3_set3key_dec_wrap,
651 des3_ctx_alloc,
652 des_ctx_free
653};
654
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000656 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000657 POLARSSL_MODE_CBC,
658 POLARSSL_KEY_LENGTH_DES_EDE3,
659 "DES-EDE3-CBC",
660 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200661 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000662 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000663 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664};
665#endif
666
Paul Bakker6132d0a2012-07-04 17:10:40 +0000667#if defined(POLARSSL_BLOWFISH_C)
668
Paul Bakkerfae35f02013-03-13 10:33:51 +0100669static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000670 unsigned char *iv, const unsigned char *input, unsigned char *output )
671{
672 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
673}
674
Paul Bakkerfae35f02013-03-13 10:33:51 +0100675static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000676 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
677{
678#if defined(POLARSSL_CIPHER_MODE_CFB)
679 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
680#else
681 ((void) ctx);
682 ((void) operation);
683 ((void) length);
684 ((void) iv_off);
685 ((void) iv);
686 ((void) input);
687 ((void) output);
688
689 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
690#endif
691}
692
Paul Bakkerfae35f02013-03-13 10:33:51 +0100693static int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000694 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
695 const unsigned char *input, unsigned char *output )
696{
697#if defined(POLARSSL_CIPHER_MODE_CTR)
698 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
699 stream_block, input, output );
700#else
701 ((void) ctx);
702 ((void) length);
703 ((void) nc_off);
704 ((void) nonce_counter);
705 ((void) stream_block);
706 ((void) input);
707 ((void) output);
708
709 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
710#endif
711}
712
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200713static int blowfish_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000714{
715 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
716}
717
718static void * blowfish_ctx_alloc( void )
719{
Paul Bakker6e339b52013-07-03 13:37:05 +0200720 return polarssl_malloc( sizeof( blowfish_context ) );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000721}
722
723static void blowfish_ctx_free( void *ctx )
724{
Paul Bakker6e339b52013-07-03 13:37:05 +0200725 polarssl_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000726}
727
728const cipher_base_t blowfish_info = {
729 POLARSSL_CIPHER_ID_BLOWFISH,
730 blowfish_crypt_cbc_wrap,
731 blowfish_crypt_cfb64_wrap,
732 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200733 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200734 blowfish_setkey_wrap,
735 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000736 blowfish_ctx_alloc,
737 blowfish_ctx_free
738};
739
740const cipher_info_t blowfish_cbc_info = {
741 POLARSSL_CIPHER_BLOWFISH_CBC,
742 POLARSSL_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200743 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000744 "BLOWFISH-CBC",
745 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200746 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000747 8,
748 &blowfish_info
749};
750
751#if defined(POLARSSL_CIPHER_MODE_CFB)
752const cipher_info_t blowfish_cfb64_info = {
753 POLARSSL_CIPHER_BLOWFISH_CFB64,
754 POLARSSL_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200755 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000756 "BLOWFISH-CFB64",
757 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200758 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000759 8,
760 &blowfish_info
761};
762#endif /* POLARSSL_CIPHER_MODE_CFB */
763
764#if defined(POLARSSL_CIPHER_MODE_CTR)
765const cipher_info_t blowfish_ctr_info = {
766 POLARSSL_CIPHER_BLOWFISH_CTR,
767 POLARSSL_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200768 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000769 "BLOWFISH-CTR",
770 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200771 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000772 8,
773 &blowfish_info
774};
775#endif /* POLARSSL_CIPHER_MODE_CTR */
776#endif /* POLARSSL_BLOWFISH_C */
777
Paul Bakker68884e32013-01-07 18:20:04 +0100778#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200779static int arc4_crypt_stream_wrap( void *ctx, size_t length,
780 const unsigned char *input,
781 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +0100782{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200783 return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +0100784}
785
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200786static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
787 unsigned int key_length )
788{
789 arc4_setup( (arc4_context *) ctx, key, key_length );
790 return( 0 );
791}
792
793static void * arc4_ctx_alloc( void )
794{
795 return polarssl_malloc( sizeof( arc4_context ) );
796}
Paul Bakker68884e32013-01-07 18:20:04 +0100797
798static void arc4_ctx_free( void *ctx )
799{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200800 polarssl_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +0100801}
802
803const cipher_base_t arc4_base_info = {
804 POLARSSL_CIPHER_ID_ARC4,
805 NULL,
806 NULL,
807 NULL,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200808 arc4_crypt_stream_wrap,
809 arc4_setkey_wrap,
810 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +0100811 arc4_ctx_alloc,
812 arc4_ctx_free
813};
814
815const cipher_info_t arc4_128_info = {
816 POLARSSL_CIPHER_ARC4_128,
817 POLARSSL_MODE_STREAM,
818 128,
819 "ARC4-128",
820 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200821 0,
Paul Bakker68884e32013-01-07 18:20:04 +0100822 1,
823 &arc4_base_info
824};
825#endif /* POLARSSL_ARC4_C */
826
Paul Bakkerfab5c822012-02-06 16:45:10 +0000827#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200828static int null_crypt_stream( void *ctx, size_t length,
829 const unsigned char *input,
830 unsigned char *output )
831{
832 ((void) ctx);
833 memmove( output, input, length );
834 return( 0 );
835}
836
837static int null_setkey( void *ctx, const unsigned char *key,
838 unsigned int key_length )
839{
840 ((void) ctx);
841 ((void) key);
842 ((void) key_length);
843
844 return( 0 );
845}
846
Paul Bakkerfab5c822012-02-06 16:45:10 +0000847static void * null_ctx_alloc( void )
848{
849 return (void *) 1;
850}
851
Paul Bakkerfab5c822012-02-06 16:45:10 +0000852static void null_ctx_free( void *ctx )
853{
854 ((void) ctx);
855}
856
857const cipher_base_t null_base_info = {
858 POLARSSL_CIPHER_ID_NULL,
859 NULL,
860 NULL,
861 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200862 null_crypt_stream,
863 null_setkey,
864 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000865 null_ctx_alloc,
866 null_ctx_free
867};
868
869const cipher_info_t null_cipher_info = {
870 POLARSSL_CIPHER_NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200871 POLARSSL_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000872 0,
873 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +0100874 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200875 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000876 1,
877 &null_base_info
878};
879#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
880
Paul Bakker8123e9d2011-01-06 15:37:30 +0000881#endif