blob: bb87c63b9e58d0f2aa87771727817f38024c50f1 [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
Paul Bakker17373852011-01-06 14:20:01 +000042#include <stdlib.h>
43
44#if defined(POLARSSL_MD2_C)
45
46static void md2_starts_wrap( void *ctx )
47{
48 md2_starts( (md2_context *) ctx );
49}
50
Paul Bakker23986e52011-04-24 08:57:21 +000051static void md2_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000052{
53 md2_update( (md2_context *) ctx, input, ilen );
54}
55
56static void md2_finish_wrap( void *ctx, unsigned char *output )
57{
58 md2_finish( (md2_context *) ctx, output );
59}
60
Paul Bakker23986e52011-04-24 08:57:21 +000061static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +000062{
63 md2_hmac_starts( (md2_context *) ctx, key, keylen );
64}
65
Paul Bakker23986e52011-04-24 08:57:21 +000066static void md2_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000067{
68 md2_hmac_update( (md2_context *) ctx, input, ilen );
69}
70
71static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
72{
73 md2_hmac_finish( (md2_context *) ctx, output );
74}
75
76static void md2_hmac_reset_wrap( void *ctx )
77{
78 md2_hmac_reset( (md2_context *) ctx );
79}
80
81static void * md2_ctx_alloc( void )
82{
83 return malloc( sizeof( md2_context ) );
84}
85
86static void md2_ctx_free( void *ctx )
87{
88 free( ctx );
89}
90
91const md_info_t md2_info = {
Paul Bakker23986e52011-04-24 08:57:21 +000092 POLARSSL_MD_MD2,
93 "MD2",
94 16,
95 md2_starts_wrap,
96 md2_update_wrap,
97 md2_finish_wrap,
98 md2,
99 md2_file,
100 md2_hmac_starts_wrap,
101 md2_hmac_update_wrap,
102 md2_hmac_finish_wrap,
103 md2_hmac_reset_wrap,
104 md2_hmac,
105 md2_ctx_alloc,
106 md2_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000107};
108
109#endif
110
111#if defined(POLARSSL_MD4_C)
112
113void md4_starts_wrap( void *ctx )
114{
115 md4_starts( (md4_context *) ctx );
116}
117
Paul Bakker23986e52011-04-24 08:57:21 +0000118void md4_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000119{
120 md4_update( (md4_context *) ctx, input, ilen );
121}
122
123void md4_finish_wrap( void *ctx, unsigned char *output )
124{
125 md4_finish( (md4_context *) ctx, output );
126}
127
Paul Bakker23986e52011-04-24 08:57:21 +0000128void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000129{
130 md4_hmac_starts( (md4_context *) ctx, key, keylen );
131}
132
Paul Bakker23986e52011-04-24 08:57:21 +0000133void md4_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000134{
135 md4_hmac_update( (md4_context *) ctx, input, ilen );
136}
137
138void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
139{
140 md4_hmac_finish( (md4_context *) ctx, output );
141}
142
143void md4_hmac_reset_wrap( void *ctx )
144{
145 md4_hmac_reset( (md4_context *) ctx );
146}
147
148void *md4_ctx_alloc( void )
149{
150 return malloc( sizeof( md4_context ) );
151}
152
153void md4_ctx_free( void *ctx )
154{
155 free( ctx );
156}
157
158const md_info_t md4_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000159 POLARSSL_MD_MD4,
160 "MD4",
161 16,
162 md4_starts_wrap,
163 md4_update_wrap,
164 md4_finish_wrap,
165 md4,
166 md4_file,
167 md4_hmac_starts_wrap,
168 md4_hmac_update_wrap,
169 md4_hmac_finish_wrap,
170 md4_hmac_reset_wrap,
171 md4_hmac,
172 md4_ctx_alloc,
173 md4_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000174};
175
176#endif
177
178#if defined(POLARSSL_MD5_C)
179
180static void md5_starts_wrap( void *ctx )
181{
182 md5_starts( (md5_context *) ctx );
183}
184
Paul Bakker23986e52011-04-24 08:57:21 +0000185static void md5_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000186{
187 md5_update( (md5_context *) ctx, input, ilen );
188}
189
190static void md5_finish_wrap( void *ctx, unsigned char *output )
191{
192 md5_finish( (md5_context *) ctx, output );
193}
194
Paul Bakker23986e52011-04-24 08:57:21 +0000195static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000196{
197 md5_hmac_starts( (md5_context *) ctx, key, keylen );
198}
199
Paul Bakker23986e52011-04-24 08:57:21 +0000200static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000201{
202 md5_hmac_update( (md5_context *) ctx, input, ilen );
203}
204
205static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
206{
207 md5_hmac_finish( (md5_context *) ctx, output );
208}
209
210static void md5_hmac_reset_wrap( void *ctx )
211{
212 md5_hmac_reset( (md5_context *) ctx );
213}
214
215static void * md5_ctx_alloc( void )
216{
217 return malloc( sizeof( md5_context ) );
218}
219
220static void md5_ctx_free( void *ctx )
221{
222 free( ctx );
223}
224
225const md_info_t md5_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000226 POLARSSL_MD_MD5,
227 "MD5",
228 16,
229 md5_starts_wrap,
230 md5_update_wrap,
231 md5_finish_wrap,
232 md5,
233 md5_file,
234 md5_hmac_starts_wrap,
235 md5_hmac_update_wrap,
236 md5_hmac_finish_wrap,
237 md5_hmac_reset_wrap,
238 md5_hmac,
239 md5_ctx_alloc,
240 md5_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000241};
242
243#endif
244
245#if defined(POLARSSL_SHA1_C)
246
247void sha1_starts_wrap( void *ctx )
248{
249 sha1_starts( (sha1_context *) ctx );
250}
251
Paul Bakker23986e52011-04-24 08:57:21 +0000252void sha1_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000253{
254 sha1_update( (sha1_context *) ctx, input, ilen );
255}
256
257void sha1_finish_wrap( void *ctx, unsigned char *output )
258{
259 sha1_finish( (sha1_context *) ctx, output );
260}
261
Paul Bakker23986e52011-04-24 08:57:21 +0000262void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000263{
264 sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
265}
266
Paul Bakker23986e52011-04-24 08:57:21 +0000267void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000268{
269 sha1_hmac_update( (sha1_context *) ctx, input, ilen );
270}
271
272void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
273{
274 sha1_hmac_finish( (sha1_context *) ctx, output );
275}
276
277void sha1_hmac_reset_wrap( void *ctx )
278{
279 sha1_hmac_reset( (sha1_context *) ctx );
280}
281
282void * sha1_ctx_alloc( void )
283{
284 return malloc( sizeof( sha1_context ) );
285}
286
287void sha1_ctx_free( void *ctx )
288{
289 free( ctx );
290}
291
292const md_info_t sha1_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000293 POLARSSL_MD_SHA1,
294 "SHA1",
295 20,
296 sha1_starts_wrap,
297 sha1_update_wrap,
298 sha1_finish_wrap,
299 sha1,
300 sha1_file,
301 sha1_hmac_starts_wrap,
302 sha1_hmac_update_wrap,
303 sha1_hmac_finish_wrap,
304 sha1_hmac_reset_wrap,
305 sha1_hmac,
306 sha1_ctx_alloc,
307 sha1_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000308};
309
310#endif
311
312/*
313 * Wrappers for generic message digests
314 */
315#if defined(POLARSSL_SHA2_C)
316
317void sha224_starts_wrap( void *ctx )
318{
319 sha2_starts( (sha2_context *) ctx, 1 );
320}
321
Paul Bakker23986e52011-04-24 08:57:21 +0000322void sha224_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000323{
324 sha2_update( (sha2_context *) ctx, input, ilen );
325}
326
327void sha224_finish_wrap( void *ctx, unsigned char *output )
328{
329 sha2_finish( (sha2_context *) ctx, output );
330}
331
Paul Bakker23986e52011-04-24 08:57:21 +0000332void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000333 unsigned char *output )
334{
335 sha2( input, ilen, output, 1 );
336}
337
338int sha224_file_wrap( const char *path, unsigned char *output )
339{
340 return sha2_file( path, output, 1 );
341}
342
Paul Bakker23986e52011-04-24 08:57:21 +0000343void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000344{
345 sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 1 );
346}
347
Paul Bakker23986e52011-04-24 08:57:21 +0000348void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000349{
350 sha2_hmac_update( (sha2_context *) ctx, input, ilen );
351}
352
353void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
354{
355 sha2_hmac_finish( (sha2_context *) ctx, output );
356}
357
358void sha224_hmac_reset_wrap( void *ctx )
359{
360 sha2_hmac_reset( (sha2_context *) ctx );
361}
362
Paul Bakker23986e52011-04-24 08:57:21 +0000363void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
364 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000365 unsigned char *output )
366{
367 sha2_hmac( key, keylen, input, ilen, output, 1 );
368}
369
370void * sha224_ctx_alloc( void )
371{
372 return malloc( sizeof( sha2_context ) );
373}
374
375void sha224_ctx_free( void *ctx )
376{
377 free( ctx );
378}
379
380const md_info_t sha224_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000381 POLARSSL_MD_SHA224,
382 "SHA224",
383 28,
384 sha224_starts_wrap,
385 sha224_update_wrap,
386 sha224_finish_wrap,
387 sha224_wrap,
388 sha224_file_wrap,
389 sha224_hmac_starts_wrap,
390 sha224_hmac_update_wrap,
391 sha224_hmac_finish_wrap,
392 sha224_hmac_reset_wrap,
393 sha224_hmac_wrap,
394 sha224_ctx_alloc,
395 sha224_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000396};
397
398void sha256_starts_wrap( void *ctx )
399{
400 sha2_starts( (sha2_context *) ctx, 0 );
401}
402
Paul Bakker23986e52011-04-24 08:57:21 +0000403void sha256_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000404{
405 sha2_update( (sha2_context *) ctx, input, ilen );
406}
407
408void sha256_finish_wrap( void *ctx, unsigned char *output )
409{
410 sha2_finish( (sha2_context *) ctx, output );
411}
412
Paul Bakker23986e52011-04-24 08:57:21 +0000413void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000414 unsigned char *output )
415{
416 sha2( input, ilen, output, 0 );
417}
418
419int sha256_file_wrap( const char *path, unsigned char *output )
420{
421 return sha2_file( path, output, 0 );
422}
423
Paul Bakker23986e52011-04-24 08:57:21 +0000424void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000425{
426 sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 0 );
427}
428
Paul Bakker23986e52011-04-24 08:57:21 +0000429void sha256_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000430{
431 sha2_hmac_update( (sha2_context *) ctx, input, ilen );
432}
433
434void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
435{
436 sha2_hmac_finish( (sha2_context *) ctx, output );
437}
438
439void sha256_hmac_reset_wrap( void *ctx )
440{
441 sha2_hmac_reset( (sha2_context *) ctx );
442}
443
Paul Bakker23986e52011-04-24 08:57:21 +0000444void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
445 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000446 unsigned char *output )
447{
448 sha2_hmac( key, keylen, input, ilen, output, 0 );
449}
450
451void * sha256_ctx_alloc( void )
452{
Paul Bakker6d468122011-01-06 15:35:45 +0000453 return malloc( sizeof( sha2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000454}
455
456void sha256_ctx_free( void *ctx )
457{
458 free( ctx );
459}
460
461const md_info_t sha256_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000462 POLARSSL_MD_SHA256,
463 "SHA256",
464 32,
465 sha256_starts_wrap,
466 sha256_update_wrap,
467 sha256_finish_wrap,
468 sha256_wrap,
469 sha256_file_wrap,
470 sha256_hmac_starts_wrap,
471 sha256_hmac_update_wrap,
472 sha256_hmac_finish_wrap,
473 sha256_hmac_reset_wrap,
474 sha256_hmac_wrap,
475 sha256_ctx_alloc,
476 sha256_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000477};
478
479#endif
480
481#if defined(POLARSSL_SHA4_C)
482
483void sha384_starts_wrap( void *ctx )
484{
485 sha4_starts( (sha4_context *) ctx, 1 );
486}
487
Paul Bakker23986e52011-04-24 08:57:21 +0000488void sha384_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000489{
490 sha4_update( (sha4_context *) ctx, input, ilen );
491}
492
493void sha384_finish_wrap( void *ctx, unsigned char *output )
494{
495 sha4_finish( (sha4_context *) ctx, output );
496}
497
Paul Bakker23986e52011-04-24 08:57:21 +0000498void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000499 unsigned char *output )
500{
501 sha4( input, ilen, output, 1 );
502}
503
504int sha384_file_wrap( const char *path, unsigned char *output )
505{
506 return sha4_file( path, output, 1 );
507}
508
Paul Bakker23986e52011-04-24 08:57:21 +0000509void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000510{
511 sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 1 );
512}
513
Paul Bakker23986e52011-04-24 08:57:21 +0000514void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000515{
516 sha4_hmac_update( (sha4_context *) ctx, input, ilen );
517}
518
519void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
520{
521 sha4_hmac_finish( (sha4_context *) ctx, output );
522}
523
524void sha384_hmac_reset_wrap( void *ctx )
525{
526 sha4_hmac_reset( (sha4_context *) ctx );
527}
528
Paul Bakker23986e52011-04-24 08:57:21 +0000529void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
530 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000531 unsigned char *output )
532{
533 sha4_hmac( key, keylen, input, ilen, output, 1 );
534}
535
536void * sha384_ctx_alloc( void )
537{
538 return malloc( sizeof( sha4_context ) );
539}
540
541void sha384_ctx_free( void *ctx )
542{
543 free( ctx );
544}
545
546const md_info_t sha384_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000547 POLARSSL_MD_SHA384,
548 "SHA384",
549 48,
550 sha384_starts_wrap,
551 sha384_update_wrap,
552 sha384_finish_wrap,
553 sha384_wrap,
554 sha384_file_wrap,
555 sha384_hmac_starts_wrap,
556 sha384_hmac_update_wrap,
557 sha384_hmac_finish_wrap,
558 sha384_hmac_reset_wrap,
559 sha384_hmac_wrap,
560 sha384_ctx_alloc,
561 sha384_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000562};
563
564void sha512_starts_wrap( void *ctx )
565{
566 sha4_starts( (sha4_context *) ctx, 0 );
567}
568
Paul Bakker23986e52011-04-24 08:57:21 +0000569void sha512_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000570{
571 sha4_update( (sha4_context *) ctx, input, ilen );
572}
573
574void sha512_finish_wrap( void *ctx, unsigned char *output )
575{
576 sha4_finish( (sha4_context *) ctx, output );
577}
578
Paul Bakker23986e52011-04-24 08:57:21 +0000579void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000580 unsigned char *output )
581{
582 sha4( input, ilen, output, 0 );
583}
584
585int sha512_file_wrap( const char *path, unsigned char *output )
586{
587 return sha4_file( path, output, 0 );
588}
589
Paul Bakker23986e52011-04-24 08:57:21 +0000590void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000591{
592 sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 0 );
593}
594
Paul Bakker23986e52011-04-24 08:57:21 +0000595void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000596{
597 sha4_hmac_update( (sha4_context *) ctx, input, ilen );
598}
599
600void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
601{
602 sha4_hmac_finish( (sha4_context *) ctx, output );
603}
604
605void sha512_hmac_reset_wrap( void *ctx )
606{
607 sha4_hmac_reset( (sha4_context *) ctx );
608}
609
Paul Bakker23986e52011-04-24 08:57:21 +0000610void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
611 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000612 unsigned char *output )
613{
614 sha4_hmac( key, keylen, input, ilen, output, 0 );
615}
616
617void * sha512_ctx_alloc( void )
618{
619 return malloc( sizeof( sha4_context ) );
620}
621
622void sha512_ctx_free( void *ctx )
623{
624 free( ctx );
625}
626
627const md_info_t sha512_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000628 POLARSSL_MD_SHA512,
629 "SHA512",
630 64,
631 sha512_starts_wrap,
632 sha512_update_wrap,
633 sha512_finish_wrap,
634 sha512_wrap,
635 sha512_file_wrap,
636 sha512_hmac_starts_wrap,
637 sha512_hmac_update_wrap,
638 sha512_hmac_finish_wrap,
639 sha512_hmac_reset_wrap,
640 sha512_hmac_wrap,
641 sha512_ctx_alloc,
642 sha512_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000643};
644
645#endif
646
647#endif