blob: 4ea2e677002a9e95b208bb6130ff6179703121e0 [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"
Paul Bakkerf6543712012-03-05 14:01:29 +000035
36#if defined(POLARSSL_MD2_C)
Paul Bakker17373852011-01-06 14:20:01 +000037#include "polarssl/md2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000038#endif
39
40#if defined(POLARSSL_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +000041#include "polarssl/md4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000042#endif
43
44#if defined(POLARSSL_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +000045#include "polarssl/md5.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000046#endif
47
48#if defined(POLARSSL_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +000049#include "polarssl/sha1.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000050#endif
51
52#if defined(POLARSSL_SHA2_C)
Paul Bakker17373852011-01-06 14:20:01 +000053#include "polarssl/sha2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000054#endif
55
56#if defined(POLARSSL_SHA4_C)
Paul Bakker17373852011-01-06 14:20:01 +000057#include "polarssl/sha4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000058#endif
Paul Bakker17373852011-01-06 14:20:01 +000059
Paul Bakker17373852011-01-06 14:20:01 +000060#include <stdlib.h>
61
Paul Bakker312da332014-06-13 17:20:13 +020062/* Implementation that should never be optimized out by the compiler */
63static void polarssl_zeroize( void *v, size_t n ) {
64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
65}
66
Paul Bakker17373852011-01-06 14:20:01 +000067#if defined(POLARSSL_MD2_C)
68
69static void md2_starts_wrap( void *ctx )
70{
71 md2_starts( (md2_context *) ctx );
72}
73
Paul Bakker23986e52011-04-24 08:57:21 +000074static void md2_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000075{
76 md2_update( (md2_context *) ctx, input, ilen );
77}
78
79static void md2_finish_wrap( void *ctx, unsigned char *output )
80{
81 md2_finish( (md2_context *) ctx, output );
82}
83
Paul Bakker1d073c52014-07-08 20:15:51 +020084static int md2_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +000085{
86#if defined(POLARSSL_FS_IO)
87 return md2_file( path, output );
88#else
89 ((void) path);
90 ((void) output);
91 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
92#endif
93}
94
Paul Bakker23986e52011-04-24 08:57:21 +000095static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +000096{
97 md2_hmac_starts( (md2_context *) ctx, key, keylen );
98}
99
Paul Bakker23986e52011-04-24 08:57:21 +0000100static void md2_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000101{
102 md2_hmac_update( (md2_context *) ctx, input, ilen );
103}
104
105static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
106{
107 md2_hmac_finish( (md2_context *) ctx, output );
108}
109
110static void md2_hmac_reset_wrap( void *ctx )
111{
112 md2_hmac_reset( (md2_context *) ctx );
113}
114
115static void * md2_ctx_alloc( void )
116{
117 return malloc( sizeof( md2_context ) );
118}
119
120static void md2_ctx_free( void *ctx )
121{
Paul Bakker312da332014-06-13 17:20:13 +0200122 polarssl_zeroize( ctx, sizeof( md2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000123 free( ctx );
124}
125
126const md_info_t md2_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000127 POLARSSL_MD_MD2,
128 "MD2",
129 16,
130 md2_starts_wrap,
131 md2_update_wrap,
132 md2_finish_wrap,
133 md2,
Paul Bakker335db3f2011-04-25 15:28:35 +0000134 md2_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000135 md2_hmac_starts_wrap,
136 md2_hmac_update_wrap,
137 md2_hmac_finish_wrap,
138 md2_hmac_reset_wrap,
139 md2_hmac,
140 md2_ctx_alloc,
141 md2_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000142};
143
144#endif
145
146#if defined(POLARSSL_MD4_C)
147
Paul Bakker1d073c52014-07-08 20:15:51 +0200148static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000149{
150 md4_starts( (md4_context *) ctx );
151}
152
Paul Bakker1d073c52014-07-08 20:15:51 +0200153static void md4_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000154{
155 md4_update( (md4_context *) ctx, input, ilen );
156}
157
Paul Bakker1d073c52014-07-08 20:15:51 +0200158static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000159{
160 md4_finish( (md4_context *) ctx, output );
161}
162
Paul Bakker1d073c52014-07-08 20:15:51 +0200163static int md4_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000164{
165#if defined(POLARSSL_FS_IO)
166 return md4_file( path, output );
167#else
168 ((void) path);
169 ((void) output);
170 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
171#endif
172}
173
Paul Bakker1d073c52014-07-08 20:15:51 +0200174static void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000175{
176 md4_hmac_starts( (md4_context *) ctx, key, keylen );
177}
178
Paul Bakker1d073c52014-07-08 20:15:51 +0200179static void md4_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000180{
181 md4_hmac_update( (md4_context *) ctx, input, ilen );
182}
183
Paul Bakker1d073c52014-07-08 20:15:51 +0200184static void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000185{
186 md4_hmac_finish( (md4_context *) ctx, output );
187}
188
Paul Bakker1d073c52014-07-08 20:15:51 +0200189static void md4_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000190{
191 md4_hmac_reset( (md4_context *) ctx );
192}
193
Paul Bakker1d073c52014-07-08 20:15:51 +0200194static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000195{
196 return malloc( sizeof( md4_context ) );
197}
198
Paul Bakker1d073c52014-07-08 20:15:51 +0200199static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000200{
Paul Bakker312da332014-06-13 17:20:13 +0200201 polarssl_zeroize( ctx, sizeof( md4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000202 free( ctx );
203}
204
205const md_info_t md4_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000206 POLARSSL_MD_MD4,
207 "MD4",
208 16,
209 md4_starts_wrap,
210 md4_update_wrap,
211 md4_finish_wrap,
212 md4,
Paul Bakker335db3f2011-04-25 15:28:35 +0000213 md4_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000214 md4_hmac_starts_wrap,
215 md4_hmac_update_wrap,
216 md4_hmac_finish_wrap,
217 md4_hmac_reset_wrap,
218 md4_hmac,
219 md4_ctx_alloc,
220 md4_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000221};
222
223#endif
224
225#if defined(POLARSSL_MD5_C)
226
227static void md5_starts_wrap( void *ctx )
228{
229 md5_starts( (md5_context *) ctx );
230}
231
Paul Bakker23986e52011-04-24 08:57:21 +0000232static void md5_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000233{
234 md5_update( (md5_context *) ctx, input, ilen );
235}
236
237static void md5_finish_wrap( void *ctx, unsigned char *output )
238{
239 md5_finish( (md5_context *) ctx, output );
240}
241
Paul Bakker1d073c52014-07-08 20:15:51 +0200242static int md5_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000243{
244#if defined(POLARSSL_FS_IO)
245 return md5_file( path, output );
246#else
247 ((void) path);
248 ((void) output);
249 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
250#endif
251}
252
Paul Bakker23986e52011-04-24 08:57:21 +0000253static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000254{
255 md5_hmac_starts( (md5_context *) ctx, key, keylen );
256}
257
Paul Bakker23986e52011-04-24 08:57:21 +0000258static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000259{
260 md5_hmac_update( (md5_context *) ctx, input, ilen );
261}
262
263static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
264{
265 md5_hmac_finish( (md5_context *) ctx, output );
266}
267
268static void md5_hmac_reset_wrap( void *ctx )
269{
270 md5_hmac_reset( (md5_context *) ctx );
271}
272
273static void * md5_ctx_alloc( void )
274{
275 return malloc( sizeof( md5_context ) );
276}
277
278static void md5_ctx_free( void *ctx )
279{
Paul Bakker312da332014-06-13 17:20:13 +0200280 polarssl_zeroize( ctx, sizeof( md5_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000281 free( ctx );
282}
283
284const md_info_t md5_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000285 POLARSSL_MD_MD5,
286 "MD5",
287 16,
288 md5_starts_wrap,
289 md5_update_wrap,
290 md5_finish_wrap,
291 md5,
Paul Bakker335db3f2011-04-25 15:28:35 +0000292 md5_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000293 md5_hmac_starts_wrap,
294 md5_hmac_update_wrap,
295 md5_hmac_finish_wrap,
296 md5_hmac_reset_wrap,
297 md5_hmac,
298 md5_ctx_alloc,
299 md5_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000300};
301
302#endif
303
304#if defined(POLARSSL_SHA1_C)
305
Paul Bakker1d073c52014-07-08 20:15:51 +0200306static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000307{
308 sha1_starts( (sha1_context *) ctx );
309}
310
Paul Bakker1d073c52014-07-08 20:15:51 +0200311static void sha1_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000312{
313 sha1_update( (sha1_context *) ctx, input, ilen );
314}
315
Paul Bakker1d073c52014-07-08 20:15:51 +0200316static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000317{
318 sha1_finish( (sha1_context *) ctx, output );
319}
320
Paul Bakker1d073c52014-07-08 20:15:51 +0200321static int sha1_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000322{
323#if defined(POLARSSL_FS_IO)
324 return sha1_file( path, output );
325#else
326 ((void) path);
327 ((void) output);
328 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
329#endif
330}
331
Paul Bakker1d073c52014-07-08 20:15:51 +0200332static void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000333{
334 sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
335}
336
Paul Bakker1d073c52014-07-08 20:15:51 +0200337static void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000338{
339 sha1_hmac_update( (sha1_context *) ctx, input, ilen );
340}
341
Paul Bakker1d073c52014-07-08 20:15:51 +0200342static void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000343{
344 sha1_hmac_finish( (sha1_context *) ctx, output );
345}
346
Paul Bakker1d073c52014-07-08 20:15:51 +0200347static void sha1_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000348{
349 sha1_hmac_reset( (sha1_context *) ctx );
350}
351
Paul Bakker1d073c52014-07-08 20:15:51 +0200352static void * sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000353{
354 return malloc( sizeof( sha1_context ) );
355}
356
Paul Bakker1d073c52014-07-08 20:15:51 +0200357static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000358{
Paul Bakker312da332014-06-13 17:20:13 +0200359 polarssl_zeroize( ctx, sizeof( sha1_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000360 free( ctx );
361}
362
363const md_info_t sha1_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000364 POLARSSL_MD_SHA1,
365 "SHA1",
366 20,
367 sha1_starts_wrap,
368 sha1_update_wrap,
369 sha1_finish_wrap,
370 sha1,
Paul Bakker335db3f2011-04-25 15:28:35 +0000371 sha1_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000372 sha1_hmac_starts_wrap,
373 sha1_hmac_update_wrap,
374 sha1_hmac_finish_wrap,
375 sha1_hmac_reset_wrap,
376 sha1_hmac,
377 sha1_ctx_alloc,
378 sha1_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000379};
380
381#endif
382
383/*
384 * Wrappers for generic message digests
385 */
386#if defined(POLARSSL_SHA2_C)
387
Paul Bakker1d073c52014-07-08 20:15:51 +0200388static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000389{
390 sha2_starts( (sha2_context *) ctx, 1 );
391}
392
Paul Bakker1d073c52014-07-08 20:15:51 +0200393static void sha224_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000394{
395 sha2_update( (sha2_context *) ctx, input, ilen );
396}
397
Paul Bakker1d073c52014-07-08 20:15:51 +0200398static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000399{
400 sha2_finish( (sha2_context *) ctx, output );
401}
402
Paul Bakker1d073c52014-07-08 20:15:51 +0200403static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000404 unsigned char *output )
405{
406 sha2( input, ilen, output, 1 );
407}
408
Paul Bakker1d073c52014-07-08 20:15:51 +0200409static int sha224_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000410{
Paul Bakker335db3f2011-04-25 15:28:35 +0000411#if defined(POLARSSL_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000412 return sha2_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000413#else
414 ((void) path);
415 ((void) output);
416 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
417#endif
Paul Bakker17373852011-01-06 14:20:01 +0000418}
419
Paul Bakker1d073c52014-07-08 20:15:51 +0200420static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000421{
422 sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 1 );
423}
424
Paul Bakker1d073c52014-07-08 20:15:51 +0200425static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000426{
427 sha2_hmac_update( (sha2_context *) ctx, input, ilen );
428}
429
Paul Bakker1d073c52014-07-08 20:15:51 +0200430static void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000431{
432 sha2_hmac_finish( (sha2_context *) ctx, output );
433}
434
Paul Bakker1d073c52014-07-08 20:15:51 +0200435static void sha224_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000436{
437 sha2_hmac_reset( (sha2_context *) ctx );
438}
439
Paul Bakker1d073c52014-07-08 20:15:51 +0200440static void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000441 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000442 unsigned char *output )
443{
444 sha2_hmac( key, keylen, input, ilen, output, 1 );
445}
446
Paul Bakker1d073c52014-07-08 20:15:51 +0200447static void * sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000448{
449 return malloc( sizeof( sha2_context ) );
450}
451
Paul Bakker1d073c52014-07-08 20:15:51 +0200452static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000453{
Paul Bakker312da332014-06-13 17:20:13 +0200454 polarssl_zeroize( ctx, sizeof( sha2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000455 free( ctx );
456}
457
458const md_info_t sha224_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000459 POLARSSL_MD_SHA224,
460 "SHA224",
461 28,
462 sha224_starts_wrap,
463 sha224_update_wrap,
464 sha224_finish_wrap,
465 sha224_wrap,
466 sha224_file_wrap,
467 sha224_hmac_starts_wrap,
468 sha224_hmac_update_wrap,
469 sha224_hmac_finish_wrap,
470 sha224_hmac_reset_wrap,
471 sha224_hmac_wrap,
472 sha224_ctx_alloc,
473 sha224_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000474};
475
Paul Bakker1d073c52014-07-08 20:15:51 +0200476static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000477{
478 sha2_starts( (sha2_context *) ctx, 0 );
479}
480
Paul Bakker1d073c52014-07-08 20:15:51 +0200481static void sha256_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000482{
483 sha2_update( (sha2_context *) ctx, input, ilen );
484}
485
Paul Bakker1d073c52014-07-08 20:15:51 +0200486static void sha256_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000487{
488 sha2_finish( (sha2_context *) ctx, output );
489}
490
Paul Bakker1d073c52014-07-08 20:15:51 +0200491static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000492 unsigned char *output )
493{
494 sha2( input, ilen, output, 0 );
495}
496
Paul Bakker1d073c52014-07-08 20:15:51 +0200497static int sha256_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000498{
Paul Bakker335db3f2011-04-25 15:28:35 +0000499#if defined(POLARSSL_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000500 return sha2_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000501#else
502 ((void) path);
503 ((void) output);
504 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
505#endif
Paul Bakker17373852011-01-06 14:20:01 +0000506}
507
Paul Bakker1d073c52014-07-08 20:15:51 +0200508static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000509{
510 sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 0 );
511}
512
Paul Bakker1d073c52014-07-08 20:15:51 +0200513static void sha256_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000514{
515 sha2_hmac_update( (sha2_context *) ctx, input, ilen );
516}
517
Paul Bakker1d073c52014-07-08 20:15:51 +0200518static void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000519{
520 sha2_hmac_finish( (sha2_context *) ctx, output );
521}
522
Paul Bakker1d073c52014-07-08 20:15:51 +0200523static void sha256_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000524{
525 sha2_hmac_reset( (sha2_context *) ctx );
526}
527
Paul Bakker1d073c52014-07-08 20:15:51 +0200528static void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000529 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000530 unsigned char *output )
531{
532 sha2_hmac( key, keylen, input, ilen, output, 0 );
533}
534
Paul Bakker1d073c52014-07-08 20:15:51 +0200535static void * sha256_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000536{
Paul Bakker6d468122011-01-06 15:35:45 +0000537 return malloc( sizeof( sha2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000538}
539
Paul Bakker1d073c52014-07-08 20:15:51 +0200540static void sha256_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000541{
Paul Bakker312da332014-06-13 17:20:13 +0200542 polarssl_zeroize( ctx, sizeof( sha2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000543 free( ctx );
544}
545
546const md_info_t sha256_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000547 POLARSSL_MD_SHA256,
548 "SHA256",
549 32,
550 sha256_starts_wrap,
551 sha256_update_wrap,
552 sha256_finish_wrap,
553 sha256_wrap,
554 sha256_file_wrap,
555 sha256_hmac_starts_wrap,
556 sha256_hmac_update_wrap,
557 sha256_hmac_finish_wrap,
558 sha256_hmac_reset_wrap,
559 sha256_hmac_wrap,
560 sha256_ctx_alloc,
561 sha256_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000562};
563
564#endif
565
566#if defined(POLARSSL_SHA4_C)
567
Paul Bakker1d073c52014-07-08 20:15:51 +0200568static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000569{
570 sha4_starts( (sha4_context *) ctx, 1 );
571}
572
Paul Bakker1d073c52014-07-08 20:15:51 +0200573static void sha384_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000574{
575 sha4_update( (sha4_context *) ctx, input, ilen );
576}
577
Paul Bakker1d073c52014-07-08 20:15:51 +0200578static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000579{
580 sha4_finish( (sha4_context *) ctx, output );
581}
582
Paul Bakker1d073c52014-07-08 20:15:51 +0200583static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000584 unsigned char *output )
585{
586 sha4( input, ilen, output, 1 );
587}
588
Paul Bakker1d073c52014-07-08 20:15:51 +0200589static int sha384_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000590{
Paul Bakker335db3f2011-04-25 15:28:35 +0000591#if defined(POLARSSL_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000592 return sha4_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000593#else
594 ((void) path);
595 ((void) output);
596 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
597#endif
Paul Bakker17373852011-01-06 14:20:01 +0000598}
599
Paul Bakker1d073c52014-07-08 20:15:51 +0200600static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000601{
602 sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 1 );
603}
604
Paul Bakker1d073c52014-07-08 20:15:51 +0200605static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000606{
607 sha4_hmac_update( (sha4_context *) ctx, input, ilen );
608}
609
Paul Bakker1d073c52014-07-08 20:15:51 +0200610static void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000611{
612 sha4_hmac_finish( (sha4_context *) ctx, output );
613}
614
Paul Bakker1d073c52014-07-08 20:15:51 +0200615static void sha384_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000616{
617 sha4_hmac_reset( (sha4_context *) ctx );
618}
619
Paul Bakker1d073c52014-07-08 20:15:51 +0200620static void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000621 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000622 unsigned char *output )
623{
624 sha4_hmac( key, keylen, input, ilen, output, 1 );
625}
626
Paul Bakker1d073c52014-07-08 20:15:51 +0200627static void * sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000628{
629 return malloc( sizeof( sha4_context ) );
630}
631
Paul Bakker1d073c52014-07-08 20:15:51 +0200632static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000633{
Paul Bakker312da332014-06-13 17:20:13 +0200634 polarssl_zeroize( ctx, sizeof( sha4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000635 free( ctx );
636}
637
638const md_info_t sha384_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000639 POLARSSL_MD_SHA384,
640 "SHA384",
641 48,
642 sha384_starts_wrap,
643 sha384_update_wrap,
644 sha384_finish_wrap,
645 sha384_wrap,
646 sha384_file_wrap,
647 sha384_hmac_starts_wrap,
648 sha384_hmac_update_wrap,
649 sha384_hmac_finish_wrap,
650 sha384_hmac_reset_wrap,
651 sha384_hmac_wrap,
652 sha384_ctx_alloc,
653 sha384_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000654};
655
Paul Bakker1d073c52014-07-08 20:15:51 +0200656static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000657{
658 sha4_starts( (sha4_context *) ctx, 0 );
659}
660
Paul Bakker1d073c52014-07-08 20:15:51 +0200661static void sha512_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000662{
663 sha4_update( (sha4_context *) ctx, input, ilen );
664}
665
Paul Bakker1d073c52014-07-08 20:15:51 +0200666static void sha512_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000667{
668 sha4_finish( (sha4_context *) ctx, output );
669}
670
Paul Bakker1d073c52014-07-08 20:15:51 +0200671static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000672 unsigned char *output )
673{
674 sha4( input, ilen, output, 0 );
675}
676
Paul Bakker1d073c52014-07-08 20:15:51 +0200677static int sha512_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000678{
Paul Bakker335db3f2011-04-25 15:28:35 +0000679#if defined(POLARSSL_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000680 return sha4_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000681#else
682 ((void) path);
683 ((void) output);
684 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
685#endif
Paul Bakker17373852011-01-06 14:20:01 +0000686}
687
Paul Bakker1d073c52014-07-08 20:15:51 +0200688static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000689{
690 sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 0 );
691}
692
Paul Bakker1d073c52014-07-08 20:15:51 +0200693static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000694{
695 sha4_hmac_update( (sha4_context *) ctx, input, ilen );
696}
697
Paul Bakker1d073c52014-07-08 20:15:51 +0200698static void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000699{
700 sha4_hmac_finish( (sha4_context *) ctx, output );
701}
702
Paul Bakker1d073c52014-07-08 20:15:51 +0200703static void sha512_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000704{
705 sha4_hmac_reset( (sha4_context *) ctx );
706}
707
Paul Bakker1d073c52014-07-08 20:15:51 +0200708static void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000709 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000710 unsigned char *output )
711{
712 sha4_hmac( key, keylen, input, ilen, output, 0 );
713}
714
Paul Bakker1d073c52014-07-08 20:15:51 +0200715static void * sha512_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000716{
717 return malloc( sizeof( sha4_context ) );
718}
719
Paul Bakker1d073c52014-07-08 20:15:51 +0200720static void sha512_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000721{
Paul Bakker312da332014-06-13 17:20:13 +0200722 polarssl_zeroize( ctx, sizeof( sha4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000723 free( ctx );
724}
725
726const md_info_t sha512_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000727 POLARSSL_MD_SHA512,
728 "SHA512",
729 64,
730 sha512_starts_wrap,
731 sha512_update_wrap,
732 sha512_finish_wrap,
733 sha512_wrap,
734 sha512_file_wrap,
735 sha512_hmac_starts_wrap,
736 sha512_hmac_update_wrap,
737 sha512_hmac_finish_wrap,
738 sha512_hmac_reset_wrap,
739 sha512_hmac_wrap,
740 sha512_ctx_alloc,
741 sha512_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000742};
743
744#endif
745
746#endif