blob: 6b7cca9e8a58584b4b75107ea3bd46617cf1b3eb [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
2 * \file md_wrap.c
3
4 * \brief Generic message digest wrapper for PolarSSL
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
8 * Copyright (C) 2006-2010, 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_MD_C)
33
34#include "polarssl/md_wrap.h"
35#include "polarssl/md2.h"
36#include "polarssl/md4.h"
37#include "polarssl/md5.h"
38#include "polarssl/sha1.h"
39#include "polarssl/sha2.h"
40#include "polarssl/sha4.h"
41
42#include <string.h>
43#include <stdlib.h>
44
45#if defined(POLARSSL_MD2_C)
46
47static void md2_starts_wrap( void *ctx )
48{
49 md2_starts( (md2_context *) ctx );
50}
51
52static void md2_update_wrap( void *ctx, const unsigned char *input, int ilen )
53{
54 md2_update( (md2_context *) ctx, input, ilen );
55}
56
57static void md2_finish_wrap( void *ctx, unsigned char *output )
58{
59 md2_finish( (md2_context *) ctx, output );
60}
61
62static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key, int keylen )
63{
64 md2_hmac_starts( (md2_context *) ctx, key, keylen );
65}
66
67static void md2_hmac_update_wrap( void *ctx, const unsigned char *input, int ilen )
68{
69 md2_hmac_update( (md2_context *) ctx, input, ilen );
70}
71
72static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
73{
74 md2_hmac_finish( (md2_context *) ctx, output );
75}
76
77static void md2_hmac_reset_wrap( void *ctx )
78{
79 md2_hmac_reset( (md2_context *) ctx );
80}
81
82static void * md2_ctx_alloc( void )
83{
84 return malloc( sizeof( md2_context ) );
85}
86
87static void md2_ctx_free( void *ctx )
88{
89 free( ctx );
90}
91
92const md_info_t md2_info = {
93 .type = POLARSSL_MD_MD2,
94 .name = "MD2",
95 .size = 16,
96 .starts_func = md2_starts_wrap,
97 .update_func = md2_update_wrap,
98 .finish_func = md2_finish_wrap,
99 .digest_func = md2,
100 .file_func = md2_file,
101 .hmac_starts_func = md2_hmac_starts_wrap,
102 .hmac_update_func = md2_hmac_update_wrap,
103 .hmac_finish_func = md2_hmac_finish_wrap,
104 .hmac_reset_func = md2_hmac_reset_wrap,
105 .hmac_func = md2_hmac,
106 .ctx_alloc_func = md2_ctx_alloc,
107 .ctx_free_func = md2_ctx_free,
108};
109
110#endif
111
112#if defined(POLARSSL_MD4_C)
113
114void md4_starts_wrap( void *ctx )
115{
116 md4_starts( (md4_context *) ctx );
117}
118
119void md4_update_wrap( void *ctx, const unsigned char *input, int ilen )
120{
121 md4_update( (md4_context *) ctx, input, ilen );
122}
123
124void md4_finish_wrap( void *ctx, unsigned char *output )
125{
126 md4_finish( (md4_context *) ctx, output );
127}
128
129void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, int keylen )
130{
131 md4_hmac_starts( (md4_context *) ctx, key, keylen );
132}
133
134void md4_hmac_update_wrap( void *ctx, const unsigned char *input, int ilen )
135{
136 md4_hmac_update( (md4_context *) ctx, input, ilen );
137}
138
139void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
140{
141 md4_hmac_finish( (md4_context *) ctx, output );
142}
143
144void md4_hmac_reset_wrap( void *ctx )
145{
146 md4_hmac_reset( (md4_context *) ctx );
147}
148
149void *md4_ctx_alloc( void )
150{
151 return malloc( sizeof( md4_context ) );
152}
153
154void md4_ctx_free( void *ctx )
155{
156 free( ctx );
157}
158
159const md_info_t md4_info = {
160 .type = POLARSSL_MD_MD4,
161 .name = "MD4",
162 .size = 16,
163 .starts_func = md4_starts_wrap,
164 .update_func = md4_update_wrap,
165 .finish_func = md4_finish_wrap,
166 .digest_func = md4,
167 .file_func = md4_file,
168 .hmac_starts_func = md4_hmac_starts_wrap,
169 .hmac_update_func = md4_hmac_update_wrap,
170 .hmac_finish_func = md4_hmac_finish_wrap,
171 .hmac_reset_func = md4_hmac_reset_wrap,
172 .hmac_func = md4_hmac,
173 .ctx_alloc_func = md4_ctx_alloc,
174 .ctx_free_func = md4_ctx_free,
175};
176
177#endif
178
179#if defined(POLARSSL_MD5_C)
180
181static void md5_starts_wrap( void *ctx )
182{
183 md5_starts( (md5_context *) ctx );
184}
185
186static void md5_update_wrap( void *ctx, const unsigned char *input, int ilen )
187{
188 md5_update( (md5_context *) ctx, input, ilen );
189}
190
191static void md5_finish_wrap( void *ctx, unsigned char *output )
192{
193 md5_finish( (md5_context *) ctx, output );
194}
195
196static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, int keylen )
197{
198 md5_hmac_starts( (md5_context *) ctx, key, keylen );
199}
200
201static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, int ilen )
202{
203 md5_hmac_update( (md5_context *) ctx, input, ilen );
204}
205
206static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
207{
208 md5_hmac_finish( (md5_context *) ctx, output );
209}
210
211static void md5_hmac_reset_wrap( void *ctx )
212{
213 md5_hmac_reset( (md5_context *) ctx );
214}
215
216static void * md5_ctx_alloc( void )
217{
218 return malloc( sizeof( md5_context ) );
219}
220
221static void md5_ctx_free( void *ctx )
222{
223 free( ctx );
224}
225
226const md_info_t md5_info = {
227 .type = POLARSSL_MD_MD5,
228 .name = "MD5",
229 .size = 16,
230 .starts_func = md5_starts_wrap,
231 .update_func = md5_update_wrap,
232 .finish_func = md5_finish_wrap,
233 .digest_func = md5,
234 .file_func = md5_file,
235 .hmac_starts_func = md5_hmac_starts_wrap,
236 .hmac_update_func = md5_hmac_update_wrap,
237 .hmac_finish_func = md5_hmac_finish_wrap,
238 .hmac_reset_func = md5_hmac_reset_wrap,
239 .hmac_func = md5_hmac,
240 .ctx_alloc_func = md5_ctx_alloc,
241 .ctx_free_func = md5_ctx_free,
242};
243
244#endif
245
246#if defined(POLARSSL_SHA1_C)
247
248void sha1_starts_wrap( void *ctx )
249{
250 sha1_starts( (sha1_context *) ctx );
251}
252
253void sha1_update_wrap( void *ctx, const unsigned char *input, int ilen )
254{
255 sha1_update( (sha1_context *) ctx, input, ilen );
256}
257
258void sha1_finish_wrap( void *ctx, unsigned char *output )
259{
260 sha1_finish( (sha1_context *) ctx, output );
261}
262
263void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, int keylen )
264{
265 sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
266}
267
268void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, int ilen )
269{
270 sha1_hmac_update( (sha1_context *) ctx, input, ilen );
271}
272
273void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
274{
275 sha1_hmac_finish( (sha1_context *) ctx, output );
276}
277
278void sha1_hmac_reset_wrap( void *ctx )
279{
280 sha1_hmac_reset( (sha1_context *) ctx );
281}
282
283void * sha1_ctx_alloc( void )
284{
285 return malloc( sizeof( sha1_context ) );
286}
287
288void sha1_ctx_free( void *ctx )
289{
290 free( ctx );
291}
292
293const md_info_t sha1_info = {
294 .type = POLARSSL_MD_SHA1,
295 .name = "SHA1",
296 .size = 20,
297 .starts_func = sha1_starts_wrap,
298 .update_func = sha1_update_wrap,
299 .finish_func = sha1_finish_wrap,
300 .digest_func = sha1,
301 .file_func = sha1_file,
302 .hmac_starts_func = sha1_hmac_starts_wrap,
303 .hmac_update_func = sha1_hmac_update_wrap,
304 .hmac_finish_func = sha1_hmac_finish_wrap,
305 .hmac_reset_func = sha1_hmac_reset_wrap,
306 .hmac_func = sha1_hmac,
307 .ctx_alloc_func = sha1_ctx_alloc,
308 .ctx_free_func = sha1_ctx_free,
309};
310
311#endif
312
313/*
314 * Wrappers for generic message digests
315 */
316#if defined(POLARSSL_SHA2_C)
317
318void sha224_starts_wrap( void *ctx )
319{
320 sha2_starts( (sha2_context *) ctx, 1 );
321}
322
323void sha224_update_wrap( void *ctx, const unsigned char *input, int ilen )
324{
325 sha2_update( (sha2_context *) ctx, input, ilen );
326}
327
328void sha224_finish_wrap( void *ctx, unsigned char *output )
329{
330 sha2_finish( (sha2_context *) ctx, output );
331}
332
333void sha224_wrap( const unsigned char *input, int ilen,
334 unsigned char *output )
335{
336 sha2( input, ilen, output, 1 );
337}
338
339int sha224_file_wrap( const char *path, unsigned char *output )
340{
341 return sha2_file( path, output, 1 );
342}
343
344void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, int keylen )
345{
346 sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 1 );
347}
348
349void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, int ilen )
350{
351 sha2_hmac_update( (sha2_context *) ctx, input, ilen );
352}
353
354void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
355{
356 sha2_hmac_finish( (sha2_context *) ctx, output );
357}
358
359void sha224_hmac_reset_wrap( void *ctx )
360{
361 sha2_hmac_reset( (sha2_context *) ctx );
362}
363
364void sha224_hmac_wrap( const unsigned char *key, int keylen,
365 const unsigned char *input, int ilen,
366 unsigned char *output )
367{
368 sha2_hmac( key, keylen, input, ilen, output, 1 );
369}
370
371void * sha224_ctx_alloc( void )
372{
373 return malloc( sizeof( sha2_context ) );
374}
375
376void sha224_ctx_free( void *ctx )
377{
378 free( ctx );
379}
380
381const md_info_t sha224_info = {
382 .type = POLARSSL_MD_SHA224,
383 .name = "SHA224",
384 .size = 28,
385 .starts_func = sha224_starts_wrap,
386 .update_func = sha224_update_wrap,
387 .finish_func = sha224_finish_wrap,
388 .digest_func = sha224_wrap,
389 .file_func = sha224_file_wrap,
390 .hmac_starts_func = sha224_hmac_starts_wrap,
391 .hmac_update_func = sha224_hmac_update_wrap,
392 .hmac_finish_func = sha224_hmac_finish_wrap,
393 .hmac_reset_func = sha224_hmac_reset_wrap,
394 .hmac_func = sha224_hmac_wrap,
395 .ctx_alloc_func = sha224_ctx_alloc,
396 .ctx_free_func = sha224_ctx_free,
397};
398
399void sha256_starts_wrap( void *ctx )
400{
401 sha2_starts( (sha2_context *) ctx, 0 );
402}
403
404void sha256_update_wrap( void *ctx, const unsigned char *input, int ilen )
405{
406 sha2_update( (sha2_context *) ctx, input, ilen );
407}
408
409void sha256_finish_wrap( void *ctx, unsigned char *output )
410{
411 sha2_finish( (sha2_context *) ctx, output );
412}
413
414void sha256_wrap( const unsigned char *input, int ilen,
415 unsigned char *output )
416{
417 sha2( input, ilen, output, 0 );
418}
419
420int sha256_file_wrap( const char *path, unsigned char *output )
421{
422 return sha2_file( path, output, 0 );
423}
424
425void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, int keylen )
426{
427 sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 0 );
428}
429
430void sha256_hmac_update_wrap( void *ctx, const unsigned char *input, int ilen )
431{
432 sha2_hmac_update( (sha2_context *) ctx, input, ilen );
433}
434
435void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
436{
437 sha2_hmac_finish( (sha2_context *) ctx, output );
438}
439
440void sha256_hmac_reset_wrap( void *ctx )
441{
442 sha2_hmac_reset( (sha2_context *) ctx );
443}
444
445void sha256_hmac_wrap( const unsigned char *key, int keylen,
446 const unsigned char *input, int ilen,
447 unsigned char *output )
448{
449 sha2_hmac( key, keylen, input, ilen, output, 0 );
450}
451
452void * sha256_ctx_alloc( void )
453{
454 return malloc( sizeof( sha2_context ) `);
455}
456
457void sha256_ctx_free( void *ctx )
458{
459 free( ctx );
460}
461
462const md_info_t sha256_info = {
463 .type = POLARSSL_MD_SHA256,
464 .name = "SHA256",
465 .size = 32,
466 .starts_func = sha256_starts_wrap,
467 .update_func = sha256_update_wrap,
468 .finish_func = sha256_finish_wrap,
469 .digest_func = sha256_wrap,
470 .file_func = sha256_file_wrap,
471 .hmac_starts_func = sha256_hmac_starts_wrap,
472 .hmac_update_func = sha256_hmac_update_wrap,
473 .hmac_finish_func = sha256_hmac_finish_wrap,
474 .hmac_reset_func = sha256_hmac_reset_wrap,
475 .hmac_func = sha256_hmac_wrap,
476 .ctx_alloc_func = sha256_ctx_alloc,
477 .ctx_free_func = sha256_ctx_free,
478};
479
480#endif
481
482#if defined(POLARSSL_SHA4_C)
483
484void sha384_starts_wrap( void *ctx )
485{
486 sha4_starts( (sha4_context *) ctx, 1 );
487}
488
489void sha384_update_wrap( void *ctx, const unsigned char *input, int ilen )
490{
491 sha4_update( (sha4_context *) ctx, input, ilen );
492}
493
494void sha384_finish_wrap( void *ctx, unsigned char *output )
495{
496 sha4_finish( (sha4_context *) ctx, output );
497}
498
499void sha384_wrap( const unsigned char *input, int ilen,
500 unsigned char *output )
501{
502 sha4( input, ilen, output, 1 );
503}
504
505int sha384_file_wrap( const char *path, unsigned char *output )
506{
507 return sha4_file( path, output, 1 );
508}
509
510void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, int keylen )
511{
512 sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 1 );
513}
514
515void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, int ilen )
516{
517 sha4_hmac_update( (sha4_context *) ctx, input, ilen );
518}
519
520void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
521{
522 sha4_hmac_finish( (sha4_context *) ctx, output );
523}
524
525void sha384_hmac_reset_wrap( void *ctx )
526{
527 sha4_hmac_reset( (sha4_context *) ctx );
528}
529
530void sha384_hmac_wrap( const unsigned char *key, int keylen,
531 const unsigned char *input, int ilen,
532 unsigned char *output )
533{
534 sha4_hmac( key, keylen, input, ilen, output, 1 );
535}
536
537void * sha384_ctx_alloc( void )
538{
539 return malloc( sizeof( sha4_context ) );
540}
541
542void sha384_ctx_free( void *ctx )
543{
544 free( ctx );
545}
546
547const md_info_t sha384_info = {
548 .type = POLARSSL_MD_SHA384,
549 .name = "SHA384",
550 .size = 48,
551 .starts_func = sha384_starts_wrap,
552 .update_func = sha384_update_wrap,
553 .finish_func = sha384_finish_wrap,
554 .digest_func = sha384_wrap,
555 .file_func = sha384_file_wrap,
556 .hmac_starts_func = sha384_hmac_starts_wrap,
557 .hmac_update_func = sha384_hmac_update_wrap,
558 .hmac_finish_func = sha384_hmac_finish_wrap,
559 .hmac_reset_func = sha384_hmac_reset_wrap,
560 .hmac_func = sha384_hmac_wrap,
561 .ctx_alloc_func = sha384_ctx_alloc,
562 .ctx_free_func = sha384_ctx_free,
563};
564
565void sha512_starts_wrap( void *ctx )
566{
567 sha4_starts( (sha4_context *) ctx, 0 );
568}
569
570void sha512_update_wrap( void *ctx, const unsigned char *input, int ilen )
571{
572 sha4_update( (sha4_context *) ctx, input, ilen );
573}
574
575void sha512_finish_wrap( void *ctx, unsigned char *output )
576{
577 sha4_finish( (sha4_context *) ctx, output );
578}
579
580void sha512_wrap( const unsigned char *input, int ilen,
581 unsigned char *output )
582{
583 sha4( input, ilen, output, 0 );
584}
585
586int sha512_file_wrap( const char *path, unsigned char *output )
587{
588 return sha4_file( path, output, 0 );
589}
590
591void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, int keylen )
592{
593 sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 0 );
594}
595
596void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, int ilen )
597{
598 sha4_hmac_update( (sha4_context *) ctx, input, ilen );
599}
600
601void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
602{
603 sha4_hmac_finish( (sha4_context *) ctx, output );
604}
605
606void sha512_hmac_reset_wrap( void *ctx )
607{
608 sha4_hmac_reset( (sha4_context *) ctx );
609}
610
611void sha512_hmac_wrap( const unsigned char *key, int keylen,
612 const unsigned char *input, int ilen,
613 unsigned char *output )
614{
615 sha4_hmac( key, keylen, input, ilen, output, 0 );
616}
617
618void * sha512_ctx_alloc( void )
619{
620 return malloc( sizeof( sha4_context ) );
621}
622
623void sha512_ctx_free( void *ctx )
624{
625 free( ctx );
626}
627
628const md_info_t sha512_info = {
629 .type = POLARSSL_MD_SHA512,
630 .name = "SHA512",
631 .size = 64,
632 .starts_func = sha512_starts_wrap,
633 .update_func = sha512_update_wrap,
634 .finish_func = sha512_finish_wrap,
635 .digest_func = sha512_wrap,
636 .file_func = sha512_file_wrap,
637 .hmac_starts_func = sha512_hmac_starts_wrap,
638 .hmac_update_func = sha512_hmac_update_wrap,
639 .hmac_finish_func = sha512_hmac_finish_wrap,
640 .hmac_reset_func = sha512_hmac_reset_wrap,
641 .hmac_func = sha512_hmac_wrap,
642 .ctx_alloc_func = sha512_ctx_alloc,
643 .ctx_free_func = sha512_ctx_free,
644};
645
646#endif
647
648#endif