~cpick/mongrel2/release

« back to all changes in this revision

Viewing changes to src/polarssl/cipher_wrap.c

  • Committer: Chris Pick
  • Date: 2013-06-30 16:39:57 UTC
  • mfrom: (1106.1.15)
  • Revision ID: git-v1:ec39967acb6bc9867ed9b9dc3774304ca6b9c294
Merge tag 'v1.8.1' into debian

Hotfix for github issue 148

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \file md_wrap.c
 
3
 * 
 
4
 * \brief Generic cipher wrapper for PolarSSL
 
5
 *
 
6
 * \author Adriaan de Jong <dejong@fox-it.com>
 
7
 *
 
8
 *  Copyright (C) 2006-2011, Brainspark B.V.
 
9
 *
 
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"
 
35
#include "polarssl/aes.h"
 
36
#include "polarssl/camellia.h"
 
37
#include "polarssl/des.h"
 
38
 
 
39
#include <stdlib.h>
 
40
 
 
41
#if defined(POLARSSL_AES_C)
 
42
 
 
43
int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
 
44
        unsigned char *iv, const unsigned char *input, unsigned char *output )
 
45
{
 
46
    return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
 
47
}
 
48
 
 
49
int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
 
50
        size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
 
51
{
 
52
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
53
    return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
 
54
#else
 
55
    ((void) ctx);
 
56
    ((void) operation);
 
57
    ((void) length);
 
58
    ((void) iv_off);
 
59
    ((void) iv);
 
60
    ((void) input);
 
61
    ((void) output);
 
62
 
 
63
    return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
 
64
#endif
 
65
}
 
66
 
 
67
int aes_crypt_ctr_wrap( void *ctx, size_t length,
 
68
        size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
 
69
        const unsigned char *input, unsigned char *output )
 
70
{
 
71
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
72
    return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
 
73
                          stream_block, input, output );
 
74
#else
 
75
    ((void) ctx);
 
76
    ((void) length);
 
77
    ((void) nc_off);
 
78
    ((void) nonce_counter);
 
79
    ((void) stream_block);
 
80
    ((void) input);
 
81
    ((void) output);
 
82
 
 
83
    return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
 
84
#endif
 
85
}
 
86
 
 
87
int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
 
88
{
 
89
    return aes_setkey_dec( (aes_context *) ctx, key, key_length );
 
90
}
 
91
 
 
92
int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
 
93
{
 
94
    return aes_setkey_enc( (aes_context *) ctx, key, key_length );
 
95
}
 
96
 
 
97
static void * aes_ctx_alloc( void )
 
98
{
 
99
    return malloc( sizeof( aes_context ) );
 
100
}
 
101
 
 
102
static void aes_ctx_free( void *ctx )
 
103
{
 
104
    free( ctx );
 
105
}
 
106
 
 
107
const cipher_base_t aes_info = {
 
108
    POLARSSL_CIPHER_ID_AES,
 
109
    aes_crypt_cbc_wrap,
 
110
    aes_crypt_cfb128_wrap,
 
111
    aes_crypt_ctr_wrap,
 
112
    aes_setkey_enc_wrap,
 
113
    aes_setkey_dec_wrap,
 
114
    aes_ctx_alloc,
 
115
    aes_ctx_free
 
116
};
 
117
 
 
118
const cipher_info_t aes_128_cbc_info = {
 
119
    POLARSSL_CIPHER_AES_128_CBC,
 
120
    POLARSSL_MODE_CBC,
 
121
    128,
 
122
    "AES-128-CBC",
 
123
    16,
 
124
    16,
 
125
    &aes_info
 
126
};
 
127
 
 
128
const cipher_info_t aes_192_cbc_info = {
 
129
    POLARSSL_CIPHER_AES_192_CBC,
 
130
    POLARSSL_MODE_CBC,
 
131
    192,
 
132
    "AES-192-CBC",
 
133
    16,
 
134
    16,
 
135
    &aes_info
 
136
};
 
137
 
 
138
const cipher_info_t aes_256_cbc_info = {
 
139
    POLARSSL_CIPHER_AES_256_CBC,
 
140
    POLARSSL_MODE_CBC,
 
141
    256,
 
142
    "AES-256-CBC",
 
143
    16,
 
144
    16,
 
145
    &aes_info
 
146
};
 
147
 
 
148
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
149
const cipher_info_t aes_128_cfb128_info = {
 
150
    POLARSSL_CIPHER_AES_128_CFB128,
 
151
    POLARSSL_MODE_CFB128,
 
152
    128,
 
153
    "AES-128-CFB128",
 
154
    16,
 
155
    16,
 
156
    &aes_info
 
157
};
 
158
 
 
159
const cipher_info_t aes_192_cfb128_info = {
 
160
    POLARSSL_CIPHER_AES_192_CFB128,
 
161
    POLARSSL_MODE_CFB128,
 
162
    192,
 
163
    "AES-192-CFB128",
 
164
    16,
 
165
    16,
 
166
    &aes_info
 
167
};
 
168
 
 
169
const cipher_info_t aes_256_cfb128_info = {
 
170
    POLARSSL_CIPHER_AES_256_CFB128,
 
171
    POLARSSL_MODE_CFB128,
 
172
    256,
 
173
    "AES-256-CFB128",
 
174
    16,
 
175
    16,
 
176
    &aes_info
 
177
};
 
178
#endif /* POLARSSL_CIPHER_MODE_CFB */
 
179
 
 
180
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
181
const cipher_info_t aes_128_ctr_info = {
 
182
    POLARSSL_CIPHER_AES_128_CTR,
 
183
    POLARSSL_MODE_CTR,
 
184
    128,
 
185
    "AES-128-CTR",
 
186
    16,
 
187
    16,
 
188
    &aes_info
 
189
};
 
190
 
 
191
const cipher_info_t aes_192_ctr_info = {
 
192
    POLARSSL_CIPHER_AES_192_CTR,
 
193
    POLARSSL_MODE_CTR,
 
194
    192,
 
195
    "AES-192-CTR",
 
196
    16,
 
197
    16,
 
198
    &aes_info
 
199
};
 
200
 
 
201
const cipher_info_t aes_256_ctr_info = {
 
202
    POLARSSL_CIPHER_AES_256_CTR,
 
203
    POLARSSL_MODE_CTR,
 
204
    256,
 
205
    "AES-256-CTR",
 
206
    16,
 
207
    16,
 
208
    &aes_info
 
209
};
 
210
#endif /* POLARSSL_CIPHER_MODE_CTR */
 
211
 
 
212
#endif
 
213
 
 
214
#if defined(POLARSSL_CAMELLIA_C)
 
215
 
 
216
int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
 
217
        unsigned char *iv, const unsigned char *input, unsigned char *output )
 
218
{
 
219
    return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
 
220
}
 
221
 
 
222
int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
 
223
        size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
 
224
{
 
225
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
226
    return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
 
227
#else
 
228
    ((void) ctx);
 
229
    ((void) operation);
 
230
    ((void) length);
 
231
    ((void) iv_off);
 
232
    ((void) iv);
 
233
    ((void) input);
 
234
    ((void) output);
 
235
 
 
236
    return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
 
237
#endif
 
238
}
 
239
 
 
240
int camellia_crypt_ctr_wrap( void *ctx, size_t length,
 
241
        size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
 
242
        const unsigned char *input, unsigned char *output )
 
243
{
 
244
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
245
    return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
 
246
                          stream_block, input, output );
 
247
#else
 
248
    ((void) ctx);
 
249
    ((void) length);
 
250
    ((void) nc_off);
 
251
    ((void) nonce_counter);
 
252
    ((void) stream_block);
 
253
    ((void) input);
 
254
    ((void) output);
 
255
 
 
256
    return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
 
257
#endif
 
258
}
 
259
 
 
260
int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
 
261
{
 
262
    return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
 
263
}
 
264
 
 
265
int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
 
266
{
 
267
    return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
 
268
}
 
269
 
 
270
static void * camellia_ctx_alloc( void )
 
271
{
 
272
    return malloc( sizeof( camellia_context ) );
 
273
}
 
274
 
 
275
static void camellia_ctx_free( void *ctx )
 
276
{
 
277
    free( ctx );
 
278
}
 
279
 
 
280
const cipher_base_t camellia_info = {
 
281
    POLARSSL_CIPHER_ID_CAMELLIA,
 
282
    camellia_crypt_cbc_wrap,
 
283
    camellia_crypt_cfb128_wrap,
 
284
    camellia_crypt_ctr_wrap,
 
285
    camellia_setkey_enc_wrap,
 
286
    camellia_setkey_dec_wrap,
 
287
    camellia_ctx_alloc,
 
288
    camellia_ctx_free
 
289
};
 
290
 
 
291
const cipher_info_t camellia_128_cbc_info = {
 
292
    POLARSSL_CIPHER_CAMELLIA_128_CBC,
 
293
    POLARSSL_MODE_CBC,
 
294
    128,
 
295
    "CAMELLIA-128-CBC",
 
296
    16,
 
297
    16,
 
298
    &camellia_info
 
299
};
 
300
 
 
301
const cipher_info_t camellia_192_cbc_info = {
 
302
    POLARSSL_CIPHER_CAMELLIA_192_CBC,
 
303
    POLARSSL_MODE_CBC,
 
304
    192,
 
305
    "CAMELLIA-192-CBC",
 
306
    16,
 
307
    16,
 
308
    &camellia_info
 
309
};
 
310
 
 
311
const cipher_info_t camellia_256_cbc_info = {
 
312
    POLARSSL_CIPHER_CAMELLIA_256_CBC,
 
313
    POLARSSL_MODE_CBC,
 
314
    256,
 
315
    "CAMELLIA-256-CBC",
 
316
    16,
 
317
    16,
 
318
    &camellia_info
 
319
};
 
320
 
 
321
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
322
const cipher_info_t camellia_128_cfb128_info = {
 
323
    POLARSSL_CIPHER_CAMELLIA_128_CFB128,
 
324
    POLARSSL_MODE_CFB128,
 
325
    128,
 
326
    "CAMELLIA-128-CFB128",
 
327
    16,
 
328
    16,
 
329
    &camellia_info
 
330
};
 
331
 
 
332
const cipher_info_t camellia_192_cfb128_info = {
 
333
    POLARSSL_CIPHER_CAMELLIA_192_CFB128,
 
334
    POLARSSL_MODE_CFB128,
 
335
    192,
 
336
    "CAMELLIA-192-CFB128",
 
337
    16,
 
338
    16,
 
339
    &camellia_info
 
340
};
 
341
 
 
342
const cipher_info_t camellia_256_cfb128_info = {
 
343
    POLARSSL_CIPHER_CAMELLIA_256_CFB128,
 
344
    POLARSSL_MODE_CFB128,
 
345
    256,
 
346
    "CAMELLIA-256-CFB128",
 
347
    16,
 
348
    16,
 
349
    &camellia_info
 
350
};
 
351
#endif /* POLARSSL_CIPHER_MODE_CFB */
 
352
 
 
353
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
354
const cipher_info_t camellia_128_ctr_info = {
 
355
    POLARSSL_CIPHER_CAMELLIA_128_CTR,
 
356
    POLARSSL_MODE_CTR,
 
357
    128,
 
358
    "CAMELLIA-128-CTR",
 
359
    16,
 
360
    16,
 
361
    &camellia_info
 
362
};
 
363
 
 
364
const cipher_info_t camellia_192_ctr_info = {
 
365
    POLARSSL_CIPHER_CAMELLIA_192_CTR,
 
366
    POLARSSL_MODE_CTR,
 
367
    192,
 
368
    "CAMELLIA-192-CTR",
 
369
    16,
 
370
    16,
 
371
    &camellia_info
 
372
};
 
373
 
 
374
const cipher_info_t camellia_256_ctr_info = {
 
375
    POLARSSL_CIPHER_CAMELLIA_256_CTR,
 
376
    POLARSSL_MODE_CTR,
 
377
    256,
 
378
    "CAMELLIA-256-CTR",
 
379
    16,
 
380
    16,
 
381
    &camellia_info
 
382
};
 
383
#endif /* POLARSSL_CIPHER_MODE_CTR */
 
384
 
 
385
#endif
 
386
 
 
387
#if defined(POLARSSL_DES_C)
 
388
 
 
389
int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
 
390
        unsigned char *iv, const unsigned char *input, unsigned char *output )
 
391
{
 
392
    return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
 
393
}
 
394
 
 
395
int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
 
396
        unsigned char *iv, const unsigned char *input, unsigned char *output )
 
397
{
 
398
    return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
 
399
}
 
400
 
 
401
int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
 
402
        size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
 
403
{
 
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
}
 
414
 
 
415
int des_crypt_ctr_wrap( void *ctx, size_t length,
 
416
        size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
 
417
        const unsigned char *input, unsigned char *output )
 
418
{
 
419
    ((void) ctx);
 
420
    ((void) length);
 
421
    ((void) nc_off);
 
422
    ((void) nonce_counter);
 
423
    ((void) stream_block);
 
424
    ((void) input);
 
425
    ((void) output);
 
426
 
 
427
    return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
 
428
}
 
429
 
 
430
 
 
431
int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
 
432
{
 
433
    ((void) key_length);
 
434
 
 
435
    return des_setkey_dec( (des_context *) ctx, key );
 
436
}
 
437
 
 
438
int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
 
439
{
 
440
    ((void) key_length);
 
441
 
 
442
    return des_setkey_enc( (des_context *) ctx, key );
 
443
}
 
444
 
 
445
int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
 
446
{
 
447
    ((void) key_length);
 
448
 
 
449
    return des3_set2key_dec( (des3_context *) ctx, key );
 
450
}
 
451
 
 
452
int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
 
453
{
 
454
    ((void) key_length);
 
455
 
 
456
    return des3_set2key_enc( (des3_context *) ctx, key );
 
457
}
 
458
 
 
459
int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
 
460
{
 
461
    ((void) key_length);
 
462
 
 
463
    return des3_set3key_dec( (des3_context *) ctx, key );
 
464
}
 
465
 
 
466
int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
 
467
{
 
468
    ((void) key_length);
 
469
 
 
470
    return des3_set3key_enc( (des3_context *) ctx, key );
 
471
}
 
472
 
 
473
static void * des_ctx_alloc( void )
 
474
{
 
475
    return malloc( sizeof( des_context ) );
 
476
}
 
477
 
 
478
static void * des3_ctx_alloc( void )
 
479
{
 
480
    return malloc( sizeof( des3_context ) );
 
481
}
 
482
 
 
483
static void des_ctx_free( void *ctx )
 
484
{
 
485
    free( ctx );
 
486
}
 
487
 
 
488
const cipher_base_t des_info = {
 
489
    POLARSSL_CIPHER_ID_DES,
 
490
    des_crypt_cbc_wrap,
 
491
    des_crypt_cfb128_wrap,
 
492
    des_crypt_ctr_wrap,
 
493
    des_setkey_enc_wrap,
 
494
    des_setkey_dec_wrap,
 
495
    des_ctx_alloc,
 
496
    des_ctx_free
 
497
};
 
498
 
 
499
const cipher_info_t des_cbc_info = {
 
500
    POLARSSL_CIPHER_DES_CBC,
 
501
    POLARSSL_MODE_CBC,
 
502
    POLARSSL_KEY_LENGTH_DES,
 
503
    "DES-CBC",
 
504
    8,
 
505
    8,
 
506
    &des_info
 
507
};
 
508
 
 
509
const cipher_base_t des_ede_info = {
 
510
    POLARSSL_CIPHER_ID_DES,
 
511
    des3_crypt_cbc_wrap,
 
512
    des_crypt_cfb128_wrap,
 
513
    des_crypt_ctr_wrap,
 
514
    des3_set2key_enc_wrap,
 
515
    des3_set2key_dec_wrap,
 
516
    des3_ctx_alloc,
 
517
    des_ctx_free
 
518
};
 
519
 
 
520
const cipher_info_t des_ede_cbc_info = {
 
521
    POLARSSL_CIPHER_DES_EDE_CBC,
 
522
    POLARSSL_MODE_CBC,
 
523
    POLARSSL_KEY_LENGTH_DES_EDE,
 
524
    "DES-EDE-CBC",
 
525
    16,
 
526
    16,
 
527
    &des_ede_info
 
528
};
 
529
 
 
530
const cipher_base_t des_ede3_info = {
 
531
    POLARSSL_CIPHER_ID_DES,
 
532
    des3_crypt_cbc_wrap,
 
533
    des_crypt_cfb128_wrap,
 
534
    des_crypt_ctr_wrap,
 
535
    des3_set3key_enc_wrap,
 
536
    des3_set3key_dec_wrap,
 
537
    des3_ctx_alloc,
 
538
    des_ctx_free
 
539
};
 
540
 
 
541
const cipher_info_t des_ede3_cbc_info = {
 
542
    POLARSSL_CIPHER_DES_EDE3_CBC,
 
543
    POLARSSL_MODE_CBC,
 
544
    POLARSSL_KEY_LENGTH_DES_EDE3,
 
545
    "DES-EDE3-CBC",
 
546
    8,
 
547
    8,
 
548
    &des_ede3_info
 
549
};
 
550
#endif
 
551
 
 
552
#endif