blob: ebe60cf208ae42334ffc88ecad3c1dcd8df1b28f [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
289const cipher_info_t aes_256_gcm_info = {
290 POLARSSL_CIPHER_AES_256_GCM,
291 POLARSSL_MODE_GCM,
292 256,
293 "AES-256-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200294 12,
295 1,
Paul Bakker68884e32013-01-07 18:20:04 +0100296 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200297 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100298};
299#endif /* POLARSSL_GCM_C */
300
Paul Bakker8123e9d2011-01-06 15:37:30 +0000301#endif
302
303#if defined(POLARSSL_CAMELLIA_C)
304
Paul Bakkerfae35f02013-03-13 10:33:51 +0100305static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000306 unsigned char *iv, const unsigned char *input, unsigned char *output )
307{
308 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
309}
310
Paul Bakkerfae35f02013-03-13 10:33:51 +0100311static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000312 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
313{
314#if defined(POLARSSL_CIPHER_MODE_CFB)
315 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
316#else
317 ((void) ctx);
318 ((void) operation);
319 ((void) length);
320 ((void) iv_off);
321 ((void) iv);
322 ((void) input);
323 ((void) output);
324
325 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
326#endif
327}
328
Paul Bakkerfae35f02013-03-13 10:33:51 +0100329static int camellia_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000330 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
331 const unsigned char *input, unsigned char *output )
332{
333#if defined(POLARSSL_CIPHER_MODE_CTR)
334 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
335 stream_block, input, output );
336#else
337 ((void) ctx);
338 ((void) length);
339 ((void) nc_off);
340 ((void) nonce_counter);
341 ((void) stream_block);
342 ((void) input);
343 ((void) output);
344
345 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
346#endif
347}
348
Paul Bakkerfae35f02013-03-13 10:33:51 +0100349static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350{
351 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
352}
353
Paul Bakkerfae35f02013-03-13 10:33:51 +0100354static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000355{
356 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
357}
358
359static void * camellia_ctx_alloc( void )
360{
Paul Bakker6e339b52013-07-03 13:37:05 +0200361 return polarssl_malloc( sizeof( camellia_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362}
363
364static void camellia_ctx_free( void *ctx )
365{
Paul Bakker6e339b52013-07-03 13:37:05 +0200366 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000367}
368
Paul Bakker343a8702011-06-09 14:27:58 +0000369const cipher_base_t camellia_info = {
370 POLARSSL_CIPHER_ID_CAMELLIA,
371 camellia_crypt_cbc_wrap,
372 camellia_crypt_cfb128_wrap,
373 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200374 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000375 camellia_setkey_enc_wrap,
376 camellia_setkey_dec_wrap,
377 camellia_ctx_alloc,
378 camellia_ctx_free
379};
380
Paul Bakker8123e9d2011-01-06 15:37:30 +0000381const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000382 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000383 POLARSSL_MODE_CBC,
384 128,
385 "CAMELLIA-128-CBC",
386 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200387 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000388 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000389 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000390};
391
392const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000393 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000394 POLARSSL_MODE_CBC,
395 192,
396 "CAMELLIA-192-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_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000404 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000405 POLARSSL_MODE_CBC,
406 256,
407 "CAMELLIA-256-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};
Paul Bakker343a8702011-06-09 14:27:58 +0000413
414#if defined(POLARSSL_CIPHER_MODE_CFB)
415const cipher_info_t camellia_128_cfb128_info = {
416 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000417 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000418 128,
419 "CAMELLIA-128-CFB128",
420 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200421 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000422 16,
423 &camellia_info
424};
425
426const cipher_info_t camellia_192_cfb128_info = {
427 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000428 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000429 192,
430 "CAMELLIA-192-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_256_cfb128_info = {
438 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000439 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000440 256,
441 "CAMELLIA-256-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#endif /* POLARSSL_CIPHER_MODE_CFB */
448
449#if defined(POLARSSL_CIPHER_MODE_CTR)
450const cipher_info_t camellia_128_ctr_info = {
451 POLARSSL_CIPHER_CAMELLIA_128_CTR,
452 POLARSSL_MODE_CTR,
453 128,
454 "CAMELLIA-128-CTR",
455 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200456 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000457 16,
458 &camellia_info
459};
460
461const cipher_info_t camellia_192_ctr_info = {
462 POLARSSL_CIPHER_CAMELLIA_192_CTR,
463 POLARSSL_MODE_CTR,
464 192,
465 "CAMELLIA-192-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_256_ctr_info = {
473 POLARSSL_CIPHER_CAMELLIA_256_CTR,
474 POLARSSL_MODE_CTR,
475 256,
476 "CAMELLIA-256-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#endif /* POLARSSL_CIPHER_MODE_CTR */
483
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484#endif
485
486#if defined(POLARSSL_DES_C)
487
Paul Bakkerfae35f02013-03-13 10:33:51 +0100488static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000489 unsigned char *iv, const unsigned char *input, unsigned char *output )
490{
491 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
492}
493
Paul Bakkerfae35f02013-03-13 10:33:51 +0100494static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495 unsigned char *iv, const unsigned char *input, unsigned char *output )
496{
497 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
498}
499
Paul Bakkerfae35f02013-03-13 10:33:51 +0100500static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000501 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
502{
503 ((void) ctx);
504 ((void) operation);
505 ((void) length);
506 ((void) iv_off);
507 ((void) iv);
508 ((void) input);
509 ((void) output);
510
511 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
512}
513
Paul Bakkerfae35f02013-03-13 10:33:51 +0100514static int des_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000515 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
516 const unsigned char *input, unsigned char *output )
517{
518 ((void) ctx);
519 ((void) length);
520 ((void) nc_off);
521 ((void) nonce_counter);
522 ((void) stream_block);
523 ((void) input);
524 ((void) output);
525
526 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
527}
528
Paul Bakkerfae35f02013-03-13 10:33:51 +0100529static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000530{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000531 ((void) key_length);
532
Paul Bakker8123e9d2011-01-06 15:37:30 +0000533 return des_setkey_dec( (des_context *) ctx, key );
534}
535
Paul Bakkerfae35f02013-03-13 10:33:51 +0100536static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000537{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000538 ((void) key_length);
539
Paul Bakker8123e9d2011-01-06 15:37:30 +0000540 return des_setkey_enc( (des_context *) ctx, key );
541}
542
Paul Bakkerfae35f02013-03-13 10:33:51 +0100543static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000544{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000545 ((void) key_length);
546
Paul Bakker8123e9d2011-01-06 15:37:30 +0000547 return des3_set2key_dec( (des3_context *) ctx, key );
548}
549
Paul Bakkerfae35f02013-03-13 10:33:51 +0100550static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000551{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000552 ((void) key_length);
553
Paul Bakker8123e9d2011-01-06 15:37:30 +0000554 return des3_set2key_enc( (des3_context *) ctx, key );
555}
556
Paul Bakkerfae35f02013-03-13 10:33:51 +0100557static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000558{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000559 ((void) key_length);
560
Paul Bakker8123e9d2011-01-06 15:37:30 +0000561 return des3_set3key_dec( (des3_context *) ctx, key );
562}
563
Paul Bakkerfae35f02013-03-13 10:33:51 +0100564static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000566 ((void) key_length);
567
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568 return des3_set3key_enc( (des3_context *) ctx, key );
569}
570
571static void * des_ctx_alloc( void )
572{
Paul Bakker6e339b52013-07-03 13:37:05 +0200573 return polarssl_malloc( sizeof( des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000574}
575
576static void * des3_ctx_alloc( void )
577{
Paul Bakker6e339b52013-07-03 13:37:05 +0200578 return polarssl_malloc( sizeof( des3_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579}
580
581static void des_ctx_free( void *ctx )
582{
Paul Bakker6e339b52013-07-03 13:37:05 +0200583 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584}
585
Paul Bakker343a8702011-06-09 14:27:58 +0000586const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000587 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000588 des_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000589 des_crypt_cfb128_wrap,
590 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200591 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000592 des_setkey_enc_wrap,
593 des_setkey_dec_wrap,
594 des_ctx_alloc,
595 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596};
597
Paul Bakker343a8702011-06-09 14:27:58 +0000598const cipher_info_t des_cbc_info = {
599 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000600 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000601 POLARSSL_KEY_LENGTH_DES,
602 "DES-CBC",
603 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200604 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000605 8,
606 &des_info
607};
608
609const cipher_base_t des_ede_info = {
610 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000611 des3_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000612 des_crypt_cfb128_wrap,
613 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200614 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000615 des3_set2key_enc_wrap,
616 des3_set2key_dec_wrap,
617 des3_ctx_alloc,
618 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000619};
620
Paul Bakker343a8702011-06-09 14:27:58 +0000621const cipher_info_t des_ede_cbc_info = {
622 POLARSSL_CIPHER_DES_EDE_CBC,
623 POLARSSL_MODE_CBC,
624 POLARSSL_KEY_LENGTH_DES_EDE,
625 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +0200626 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200627 0,
Paul Bakker0e342352013-06-24 19:33:02 +0200628 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000629 &des_ede_info
630};
631
632const cipher_base_t des_ede3_info = {
633 POLARSSL_CIPHER_ID_DES,
634 des3_crypt_cbc_wrap,
635 des_crypt_cfb128_wrap,
636 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200637 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000638 des3_set3key_enc_wrap,
639 des3_set3key_dec_wrap,
640 des3_ctx_alloc,
641 des_ctx_free
642};
643
Paul Bakker8123e9d2011-01-06 15:37:30 +0000644const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000645 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000646 POLARSSL_MODE_CBC,
647 POLARSSL_KEY_LENGTH_DES_EDE3,
648 "DES-EDE3-CBC",
649 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200650 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000651 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000652 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000653};
654#endif
655
Paul Bakker6132d0a2012-07-04 17:10:40 +0000656#if defined(POLARSSL_BLOWFISH_C)
657
Paul Bakkerfae35f02013-03-13 10:33:51 +0100658static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000659 unsigned char *iv, const unsigned char *input, unsigned char *output )
660{
661 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
662}
663
Paul Bakkerfae35f02013-03-13 10:33:51 +0100664static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000665 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
666{
667#if defined(POLARSSL_CIPHER_MODE_CFB)
668 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
669#else
670 ((void) ctx);
671 ((void) operation);
672 ((void) length);
673 ((void) iv_off);
674 ((void) iv);
675 ((void) input);
676 ((void) output);
677
678 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
679#endif
680}
681
Paul Bakkerfae35f02013-03-13 10:33:51 +0100682static int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000683 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
684 const unsigned char *input, unsigned char *output )
685{
686#if defined(POLARSSL_CIPHER_MODE_CTR)
687 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
688 stream_block, input, output );
689#else
690 ((void) ctx);
691 ((void) length);
692 ((void) nc_off);
693 ((void) nonce_counter);
694 ((void) stream_block);
695 ((void) input);
696 ((void) output);
697
698 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
699#endif
700}
701
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200702static int blowfish_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000703{
704 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
705}
706
707static void * blowfish_ctx_alloc( void )
708{
Paul Bakker6e339b52013-07-03 13:37:05 +0200709 return polarssl_malloc( sizeof( blowfish_context ) );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000710}
711
712static void blowfish_ctx_free( void *ctx )
713{
Paul Bakker6e339b52013-07-03 13:37:05 +0200714 polarssl_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000715}
716
717const cipher_base_t blowfish_info = {
718 POLARSSL_CIPHER_ID_BLOWFISH,
719 blowfish_crypt_cbc_wrap,
720 blowfish_crypt_cfb64_wrap,
721 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200722 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200723 blowfish_setkey_wrap,
724 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000725 blowfish_ctx_alloc,
726 blowfish_ctx_free
727};
728
729const cipher_info_t blowfish_cbc_info = {
730 POLARSSL_CIPHER_BLOWFISH_CBC,
731 POLARSSL_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200732 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000733 "BLOWFISH-CBC",
734 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200735 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000736 8,
737 &blowfish_info
738};
739
740#if defined(POLARSSL_CIPHER_MODE_CFB)
741const cipher_info_t blowfish_cfb64_info = {
742 POLARSSL_CIPHER_BLOWFISH_CFB64,
743 POLARSSL_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200744 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000745 "BLOWFISH-CFB64",
746 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200747 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000748 8,
749 &blowfish_info
750};
751#endif /* POLARSSL_CIPHER_MODE_CFB */
752
753#if defined(POLARSSL_CIPHER_MODE_CTR)
754const cipher_info_t blowfish_ctr_info = {
755 POLARSSL_CIPHER_BLOWFISH_CTR,
756 POLARSSL_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200757 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000758 "BLOWFISH-CTR",
759 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200760 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000761 8,
762 &blowfish_info
763};
764#endif /* POLARSSL_CIPHER_MODE_CTR */
765#endif /* POLARSSL_BLOWFISH_C */
766
Paul Bakker68884e32013-01-07 18:20:04 +0100767#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200768static int arc4_crypt_stream_wrap( void *ctx, size_t length,
769 const unsigned char *input,
770 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +0100771{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200772 return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +0100773}
774
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200775static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
776 unsigned int key_length )
777{
778 arc4_setup( (arc4_context *) ctx, key, key_length );
779 return( 0 );
780}
781
782static void * arc4_ctx_alloc( void )
783{
784 return polarssl_malloc( sizeof( arc4_context ) );
785}
Paul Bakker68884e32013-01-07 18:20:04 +0100786
787static void arc4_ctx_free( void *ctx )
788{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200789 polarssl_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +0100790}
791
792const cipher_base_t arc4_base_info = {
793 POLARSSL_CIPHER_ID_ARC4,
794 NULL,
795 NULL,
796 NULL,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200797 arc4_crypt_stream_wrap,
798 arc4_setkey_wrap,
799 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +0100800 arc4_ctx_alloc,
801 arc4_ctx_free
802};
803
804const cipher_info_t arc4_128_info = {
805 POLARSSL_CIPHER_ARC4_128,
806 POLARSSL_MODE_STREAM,
807 128,
808 "ARC4-128",
809 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200810 0,
Paul Bakker68884e32013-01-07 18:20:04 +0100811 1,
812 &arc4_base_info
813};
814#endif /* POLARSSL_ARC4_C */
815
Paul Bakkerfab5c822012-02-06 16:45:10 +0000816#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200817static int null_crypt_stream( void *ctx, size_t length,
818 const unsigned char *input,
819 unsigned char *output )
820{
821 ((void) ctx);
822 memmove( output, input, length );
823 return( 0 );
824}
825
826static int null_setkey( void *ctx, const unsigned char *key,
827 unsigned int key_length )
828{
829 ((void) ctx);
830 ((void) key);
831 ((void) key_length);
832
833 return( 0 );
834}
835
Paul Bakkerfab5c822012-02-06 16:45:10 +0000836static void * null_ctx_alloc( void )
837{
838 return (void *) 1;
839}
840
Paul Bakkerfab5c822012-02-06 16:45:10 +0000841static void null_ctx_free( void *ctx )
842{
843 ((void) ctx);
844}
845
846const cipher_base_t null_base_info = {
847 POLARSSL_CIPHER_ID_NULL,
848 NULL,
849 NULL,
850 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200851 null_crypt_stream,
852 null_setkey,
853 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000854 null_ctx_alloc,
855 null_ctx_free
856};
857
858const cipher_info_t null_cipher_info = {
859 POLARSSL_CIPHER_NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200860 POLARSSL_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000861 0,
862 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +0100863 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200864 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000865 1,
866 &null_base_info
867};
868#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
869
Paul Bakker8123e9d2011-01-06 15:37:30 +0000870#endif