blob: 528271e3dd8c21f6e27c18e4a53a44b4c0a5e5b3 [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 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00008 * Copyright (C) 2006-2010, ARM Limited, All Rights Reserved
Paul Bakker17373852011-01-06 14:20:01 +00009 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +000010 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker17373852011-01-06 14:20:01 +000011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27#include "polarssl/config.h"
28
29#if defined(POLARSSL_MD_C)
30
31#include "polarssl/md_wrap.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000032
33#if defined(POLARSSL_MD2_C)
Paul Bakker17373852011-01-06 14:20:01 +000034#include "polarssl/md2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000035#endif
36
37#if defined(POLARSSL_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +000038#include "polarssl/md4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000039#endif
40
41#if defined(POLARSSL_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +000042#include "polarssl/md5.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000043#endif
44
45#if defined(POLARSSL_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +000046#include "polarssl/sha1.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000047#endif
48
49#if defined(POLARSSL_SHA2_C)
Paul Bakker17373852011-01-06 14:20:01 +000050#include "polarssl/sha2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000051#endif
52
53#if defined(POLARSSL_SHA4_C)
Paul Bakker17373852011-01-06 14:20:01 +000054#include "polarssl/sha4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000055#endif
Paul Bakker17373852011-01-06 14:20:01 +000056
Paul Bakker17373852011-01-06 14:20:01 +000057#include <stdlib.h>
58
Paul Bakker312da332014-06-13 17:20:13 +020059/* Implementation that should never be optimized out by the compiler */
60static void polarssl_zeroize( void *v, size_t n ) {
61 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
62}
63
Paul Bakker17373852011-01-06 14:20:01 +000064#if defined(POLARSSL_MD2_C)
65
66static void md2_starts_wrap( void *ctx )
67{
68 md2_starts( (md2_context *) ctx );
69}
70
Paul Bakker23986e52011-04-24 08:57:21 +000071static void md2_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000072{
73 md2_update( (md2_context *) ctx, input, ilen );
74}
75
76static void md2_finish_wrap( void *ctx, unsigned char *output )
77{
78 md2_finish( (md2_context *) ctx, output );
79}
80
Paul Bakker1d073c52014-07-08 20:15:51 +020081static int md2_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +000082{
83#if defined(POLARSSL_FS_IO)
84 return md2_file( path, output );
85#else
86 ((void) path);
87 ((void) output);
88 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
89#endif
90}
91
Paul Bakker23986e52011-04-24 08:57:21 +000092static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +000093{
94 md2_hmac_starts( (md2_context *) ctx, key, keylen );
95}
96
Paul Bakker23986e52011-04-24 08:57:21 +000097static void md2_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000098{
99 md2_hmac_update( (md2_context *) ctx, input, ilen );
100}
101
102static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
103{
104 md2_hmac_finish( (md2_context *) ctx, output );
105}
106
107static void md2_hmac_reset_wrap( void *ctx )
108{
109 md2_hmac_reset( (md2_context *) ctx );
110}
111
112static void * md2_ctx_alloc( void )
113{
114 return malloc( sizeof( md2_context ) );
115}
116
117static void md2_ctx_free( void *ctx )
118{
Paul Bakker312da332014-06-13 17:20:13 +0200119 polarssl_zeroize( ctx, sizeof( md2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000120 free( ctx );
121}
122
123const md_info_t md2_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000124 POLARSSL_MD_MD2,
125 "MD2",
126 16,
127 md2_starts_wrap,
128 md2_update_wrap,
129 md2_finish_wrap,
130 md2,
Paul Bakker335db3f2011-04-25 15:28:35 +0000131 md2_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000132 md2_hmac_starts_wrap,
133 md2_hmac_update_wrap,
134 md2_hmac_finish_wrap,
135 md2_hmac_reset_wrap,
136 md2_hmac,
137 md2_ctx_alloc,
138 md2_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000139};
140
141#endif
142
143#if defined(POLARSSL_MD4_C)
144
Paul Bakker1d073c52014-07-08 20:15:51 +0200145static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000146{
147 md4_starts( (md4_context *) ctx );
148}
149
Paul Bakker1d073c52014-07-08 20:15:51 +0200150static void md4_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000151{
152 md4_update( (md4_context *) ctx, input, ilen );
153}
154
Paul Bakker1d073c52014-07-08 20:15:51 +0200155static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000156{
157 md4_finish( (md4_context *) ctx, output );
158}
159
Paul Bakker1d073c52014-07-08 20:15:51 +0200160static int md4_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000161{
162#if defined(POLARSSL_FS_IO)
163 return md4_file( path, output );
164#else
165 ((void) path);
166 ((void) output);
167 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
168#endif
169}
170
Paul Bakker1d073c52014-07-08 20:15:51 +0200171static void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000172{
173 md4_hmac_starts( (md4_context *) ctx, key, keylen );
174}
175
Paul Bakker1d073c52014-07-08 20:15:51 +0200176static void md4_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000177{
178 md4_hmac_update( (md4_context *) ctx, input, ilen );
179}
180
Paul Bakker1d073c52014-07-08 20:15:51 +0200181static void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000182{
183 md4_hmac_finish( (md4_context *) ctx, output );
184}
185
Paul Bakker1d073c52014-07-08 20:15:51 +0200186static void md4_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000187{
188 md4_hmac_reset( (md4_context *) ctx );
189}
190
Paul Bakker1d073c52014-07-08 20:15:51 +0200191static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000192{
193 return malloc( sizeof( md4_context ) );
194}
195
Paul Bakker1d073c52014-07-08 20:15:51 +0200196static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000197{
Paul Bakker312da332014-06-13 17:20:13 +0200198 polarssl_zeroize( ctx, sizeof( md4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000199 free( ctx );
200}
201
202const md_info_t md4_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000203 POLARSSL_MD_MD4,
204 "MD4",
205 16,
206 md4_starts_wrap,
207 md4_update_wrap,
208 md4_finish_wrap,
209 md4,
Paul Bakker335db3f2011-04-25 15:28:35 +0000210 md4_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000211 md4_hmac_starts_wrap,
212 md4_hmac_update_wrap,
213 md4_hmac_finish_wrap,
214 md4_hmac_reset_wrap,
215 md4_hmac,
216 md4_ctx_alloc,
217 md4_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000218};
219
220#endif
221
222#if defined(POLARSSL_MD5_C)
223
224static void md5_starts_wrap( void *ctx )
225{
226 md5_starts( (md5_context *) ctx );
227}
228
Paul Bakker23986e52011-04-24 08:57:21 +0000229static void md5_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000230{
231 md5_update( (md5_context *) ctx, input, ilen );
232}
233
234static void md5_finish_wrap( void *ctx, unsigned char *output )
235{
236 md5_finish( (md5_context *) ctx, output );
237}
238
Paul Bakker1d073c52014-07-08 20:15:51 +0200239static int md5_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000240{
241#if defined(POLARSSL_FS_IO)
242 return md5_file( path, output );
243#else
244 ((void) path);
245 ((void) output);
246 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
247#endif
248}
249
Paul Bakker23986e52011-04-24 08:57:21 +0000250static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000251{
252 md5_hmac_starts( (md5_context *) ctx, key, keylen );
253}
254
Paul Bakker23986e52011-04-24 08:57:21 +0000255static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000256{
257 md5_hmac_update( (md5_context *) ctx, input, ilen );
258}
259
260static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
261{
262 md5_hmac_finish( (md5_context *) ctx, output );
263}
264
265static void md5_hmac_reset_wrap( void *ctx )
266{
267 md5_hmac_reset( (md5_context *) ctx );
268}
269
270static void * md5_ctx_alloc( void )
271{
272 return malloc( sizeof( md5_context ) );
273}
274
275static void md5_ctx_free( void *ctx )
276{
Paul Bakker312da332014-06-13 17:20:13 +0200277 polarssl_zeroize( ctx, sizeof( md5_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000278 free( ctx );
279}
280
281const md_info_t md5_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000282 POLARSSL_MD_MD5,
283 "MD5",
284 16,
285 md5_starts_wrap,
286 md5_update_wrap,
287 md5_finish_wrap,
288 md5,
Paul Bakker335db3f2011-04-25 15:28:35 +0000289 md5_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000290 md5_hmac_starts_wrap,
291 md5_hmac_update_wrap,
292 md5_hmac_finish_wrap,
293 md5_hmac_reset_wrap,
294 md5_hmac,
295 md5_ctx_alloc,
296 md5_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000297};
298
299#endif
300
301#if defined(POLARSSL_SHA1_C)
302
Paul Bakker1d073c52014-07-08 20:15:51 +0200303static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000304{
305 sha1_starts( (sha1_context *) ctx );
306}
307
Paul Bakker1d073c52014-07-08 20:15:51 +0200308static void sha1_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000309{
310 sha1_update( (sha1_context *) ctx, input, ilen );
311}
312
Paul Bakker1d073c52014-07-08 20:15:51 +0200313static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000314{
315 sha1_finish( (sha1_context *) ctx, output );
316}
317
Paul Bakker1d073c52014-07-08 20:15:51 +0200318static int sha1_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000319{
320#if defined(POLARSSL_FS_IO)
321 return sha1_file( path, output );
322#else
323 ((void) path);
324 ((void) output);
325 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
326#endif
327}
328
Paul Bakker1d073c52014-07-08 20:15:51 +0200329static void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000330{
331 sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
332}
333
Paul Bakker1d073c52014-07-08 20:15:51 +0200334static void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000335{
336 sha1_hmac_update( (sha1_context *) ctx, input, ilen );
337}
338
Paul Bakker1d073c52014-07-08 20:15:51 +0200339static void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000340{
341 sha1_hmac_finish( (sha1_context *) ctx, output );
342}
343
Paul Bakker1d073c52014-07-08 20:15:51 +0200344static void sha1_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000345{
346 sha1_hmac_reset( (sha1_context *) ctx );
347}
348
Paul Bakker1d073c52014-07-08 20:15:51 +0200349static void * sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000350{
351 return malloc( sizeof( sha1_context ) );
352}
353
Paul Bakker1d073c52014-07-08 20:15:51 +0200354static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000355{
Paul Bakker312da332014-06-13 17:20:13 +0200356 polarssl_zeroize( ctx, sizeof( sha1_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000357 free( ctx );
358}
359
360const md_info_t sha1_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000361 POLARSSL_MD_SHA1,
362 "SHA1",
363 20,
364 sha1_starts_wrap,
365 sha1_update_wrap,
366 sha1_finish_wrap,
367 sha1,
Paul Bakker335db3f2011-04-25 15:28:35 +0000368 sha1_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000369 sha1_hmac_starts_wrap,
370 sha1_hmac_update_wrap,
371 sha1_hmac_finish_wrap,
372 sha1_hmac_reset_wrap,
373 sha1_hmac,
374 sha1_ctx_alloc,
375 sha1_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000376};
377
378#endif
379
380/*
381 * Wrappers for generic message digests
382 */
383#if defined(POLARSSL_SHA2_C)
384
Paul Bakker1d073c52014-07-08 20:15:51 +0200385static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000386{
387 sha2_starts( (sha2_context *) ctx, 1 );
388}
389
Paul Bakker1d073c52014-07-08 20:15:51 +0200390static void sha224_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000391{
392 sha2_update( (sha2_context *) ctx, input, ilen );
393}
394
Paul Bakker1d073c52014-07-08 20:15:51 +0200395static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000396{
397 sha2_finish( (sha2_context *) ctx, output );
398}
399
Paul Bakker1d073c52014-07-08 20:15:51 +0200400static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000401 unsigned char *output )
402{
403 sha2( input, ilen, output, 1 );
404}
405
Paul Bakker1d073c52014-07-08 20:15:51 +0200406static int sha224_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000407{
Paul Bakker335db3f2011-04-25 15:28:35 +0000408#if defined(POLARSSL_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000409 return sha2_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000410#else
411 ((void) path);
412 ((void) output);
413 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
414#endif
Paul Bakker17373852011-01-06 14:20:01 +0000415}
416
Paul Bakker1d073c52014-07-08 20:15:51 +0200417static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000418{
419 sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 1 );
420}
421
Paul Bakker1d073c52014-07-08 20:15:51 +0200422static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000423{
424 sha2_hmac_update( (sha2_context *) ctx, input, ilen );
425}
426
Paul Bakker1d073c52014-07-08 20:15:51 +0200427static void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000428{
429 sha2_hmac_finish( (sha2_context *) ctx, output );
430}
431
Paul Bakker1d073c52014-07-08 20:15:51 +0200432static void sha224_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000433{
434 sha2_hmac_reset( (sha2_context *) ctx );
435}
436
Paul Bakker1d073c52014-07-08 20:15:51 +0200437static void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000438 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000439 unsigned char *output )
440{
441 sha2_hmac( key, keylen, input, ilen, output, 1 );
442}
443
Paul Bakker1d073c52014-07-08 20:15:51 +0200444static void * sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000445{
446 return malloc( sizeof( sha2_context ) );
447}
448
Paul Bakker1d073c52014-07-08 20:15:51 +0200449static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000450{
Paul Bakker312da332014-06-13 17:20:13 +0200451 polarssl_zeroize( ctx, sizeof( sha2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000452 free( ctx );
453}
454
455const md_info_t sha224_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000456 POLARSSL_MD_SHA224,
457 "SHA224",
458 28,
459 sha224_starts_wrap,
460 sha224_update_wrap,
461 sha224_finish_wrap,
462 sha224_wrap,
463 sha224_file_wrap,
464 sha224_hmac_starts_wrap,
465 sha224_hmac_update_wrap,
466 sha224_hmac_finish_wrap,
467 sha224_hmac_reset_wrap,
468 sha224_hmac_wrap,
469 sha224_ctx_alloc,
470 sha224_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000471};
472
Paul Bakker1d073c52014-07-08 20:15:51 +0200473static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000474{
475 sha2_starts( (sha2_context *) ctx, 0 );
476}
477
Paul Bakker1d073c52014-07-08 20:15:51 +0200478static void sha256_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000479{
480 sha2_update( (sha2_context *) ctx, input, ilen );
481}
482
Paul Bakker1d073c52014-07-08 20:15:51 +0200483static void sha256_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000484{
485 sha2_finish( (sha2_context *) ctx, output );
486}
487
Paul Bakker1d073c52014-07-08 20:15:51 +0200488static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000489 unsigned char *output )
490{
491 sha2( input, ilen, output, 0 );
492}
493
Paul Bakker1d073c52014-07-08 20:15:51 +0200494static int sha256_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000495{
Paul Bakker335db3f2011-04-25 15:28:35 +0000496#if defined(POLARSSL_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000497 return sha2_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000498#else
499 ((void) path);
500 ((void) output);
501 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
502#endif
Paul Bakker17373852011-01-06 14:20:01 +0000503}
504
Paul Bakker1d073c52014-07-08 20:15:51 +0200505static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000506{
507 sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 0 );
508}
509
Paul Bakker1d073c52014-07-08 20:15:51 +0200510static void sha256_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000511{
512 sha2_hmac_update( (sha2_context *) ctx, input, ilen );
513}
514
Paul Bakker1d073c52014-07-08 20:15:51 +0200515static void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000516{
517 sha2_hmac_finish( (sha2_context *) ctx, output );
518}
519
Paul Bakker1d073c52014-07-08 20:15:51 +0200520static void sha256_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000521{
522 sha2_hmac_reset( (sha2_context *) ctx );
523}
524
Paul Bakker1d073c52014-07-08 20:15:51 +0200525static void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000526 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000527 unsigned char *output )
528{
529 sha2_hmac( key, keylen, input, ilen, output, 0 );
530}
531
Paul Bakker1d073c52014-07-08 20:15:51 +0200532static void * sha256_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000533{
Paul Bakker6d468122011-01-06 15:35:45 +0000534 return malloc( sizeof( sha2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000535}
536
Paul Bakker1d073c52014-07-08 20:15:51 +0200537static void sha256_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000538{
Paul Bakker312da332014-06-13 17:20:13 +0200539 polarssl_zeroize( ctx, sizeof( sha2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000540 free( ctx );
541}
542
543const md_info_t sha256_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000544 POLARSSL_MD_SHA256,
545 "SHA256",
546 32,
547 sha256_starts_wrap,
548 sha256_update_wrap,
549 sha256_finish_wrap,
550 sha256_wrap,
551 sha256_file_wrap,
552 sha256_hmac_starts_wrap,
553 sha256_hmac_update_wrap,
554 sha256_hmac_finish_wrap,
555 sha256_hmac_reset_wrap,
556 sha256_hmac_wrap,
557 sha256_ctx_alloc,
558 sha256_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000559};
560
561#endif
562
563#if defined(POLARSSL_SHA4_C)
564
Paul Bakker1d073c52014-07-08 20:15:51 +0200565static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000566{
567 sha4_starts( (sha4_context *) ctx, 1 );
568}
569
Paul Bakker1d073c52014-07-08 20:15:51 +0200570static void sha384_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000571{
572 sha4_update( (sha4_context *) ctx, input, ilen );
573}
574
Paul Bakker1d073c52014-07-08 20:15:51 +0200575static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000576{
577 sha4_finish( (sha4_context *) ctx, output );
578}
579
Paul Bakker1d073c52014-07-08 20:15:51 +0200580static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000581 unsigned char *output )
582{
583 sha4( input, ilen, output, 1 );
584}
585
Paul Bakker1d073c52014-07-08 20:15:51 +0200586static int sha384_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000587{
Paul Bakker335db3f2011-04-25 15:28:35 +0000588#if defined(POLARSSL_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000589 return sha4_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000590#else
591 ((void) path);
592 ((void) output);
593 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
594#endif
Paul Bakker17373852011-01-06 14:20:01 +0000595}
596
Paul Bakker1d073c52014-07-08 20:15:51 +0200597static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000598{
599 sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 1 );
600}
601
Paul Bakker1d073c52014-07-08 20:15:51 +0200602static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000603{
604 sha4_hmac_update( (sha4_context *) ctx, input, ilen );
605}
606
Paul Bakker1d073c52014-07-08 20:15:51 +0200607static void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000608{
609 sha4_hmac_finish( (sha4_context *) ctx, output );
610}
611
Paul Bakker1d073c52014-07-08 20:15:51 +0200612static void sha384_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000613{
614 sha4_hmac_reset( (sha4_context *) ctx );
615}
616
Paul Bakker1d073c52014-07-08 20:15:51 +0200617static void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000618 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000619 unsigned char *output )
620{
621 sha4_hmac( key, keylen, input, ilen, output, 1 );
622}
623
Paul Bakker1d073c52014-07-08 20:15:51 +0200624static void * sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000625{
626 return malloc( sizeof( sha4_context ) );
627}
628
Paul Bakker1d073c52014-07-08 20:15:51 +0200629static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000630{
Paul Bakker312da332014-06-13 17:20:13 +0200631 polarssl_zeroize( ctx, sizeof( sha4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000632 free( ctx );
633}
634
635const md_info_t sha384_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000636 POLARSSL_MD_SHA384,
637 "SHA384",
638 48,
639 sha384_starts_wrap,
640 sha384_update_wrap,
641 sha384_finish_wrap,
642 sha384_wrap,
643 sha384_file_wrap,
644 sha384_hmac_starts_wrap,
645 sha384_hmac_update_wrap,
646 sha384_hmac_finish_wrap,
647 sha384_hmac_reset_wrap,
648 sha384_hmac_wrap,
649 sha384_ctx_alloc,
650 sha384_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000651};
652
Paul Bakker1d073c52014-07-08 20:15:51 +0200653static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000654{
655 sha4_starts( (sha4_context *) ctx, 0 );
656}
657
Paul Bakker1d073c52014-07-08 20:15:51 +0200658static void sha512_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000659{
660 sha4_update( (sha4_context *) ctx, input, ilen );
661}
662
Paul Bakker1d073c52014-07-08 20:15:51 +0200663static void sha512_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000664{
665 sha4_finish( (sha4_context *) ctx, output );
666}
667
Paul Bakker1d073c52014-07-08 20:15:51 +0200668static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000669 unsigned char *output )
670{
671 sha4( input, ilen, output, 0 );
672}
673
Paul Bakker1d073c52014-07-08 20:15:51 +0200674static int sha512_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000675{
Paul Bakker335db3f2011-04-25 15:28:35 +0000676#if defined(POLARSSL_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000677 return sha4_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000678#else
679 ((void) path);
680 ((void) output);
681 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
682#endif
Paul Bakker17373852011-01-06 14:20:01 +0000683}
684
Paul Bakker1d073c52014-07-08 20:15:51 +0200685static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000686{
687 sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 0 );
688}
689
Paul Bakker1d073c52014-07-08 20:15:51 +0200690static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000691{
692 sha4_hmac_update( (sha4_context *) ctx, input, ilen );
693}
694
Paul Bakker1d073c52014-07-08 20:15:51 +0200695static void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000696{
697 sha4_hmac_finish( (sha4_context *) ctx, output );
698}
699
Paul Bakker1d073c52014-07-08 20:15:51 +0200700static void sha512_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000701{
702 sha4_hmac_reset( (sha4_context *) ctx );
703}
704
Paul Bakker1d073c52014-07-08 20:15:51 +0200705static void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000706 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000707 unsigned char *output )
708{
709 sha4_hmac( key, keylen, input, ilen, output, 0 );
710}
711
Paul Bakker1d073c52014-07-08 20:15:51 +0200712static void * sha512_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000713{
714 return malloc( sizeof( sha4_context ) );
715}
716
Paul Bakker1d073c52014-07-08 20:15:51 +0200717static void sha512_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000718{
Paul Bakker312da332014-06-13 17:20:13 +0200719 polarssl_zeroize( ctx, sizeof( sha4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000720 free( ctx );
721}
722
723const md_info_t sha512_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000724 POLARSSL_MD_SHA512,
725 "SHA512",
726 64,
727 sha512_starts_wrap,
728 sha512_update_wrap,
729 sha512_finish_wrap,
730 sha512_wrap,
731 sha512_file_wrap,
732 sha512_hmac_starts_wrap,
733 sha512_hmac_update_wrap,
734 sha512_hmac_finish_wrap,
735 sha512_hmac_reset_wrap,
736 sha512_hmac_wrap,
737 sha512_ctx_alloc,
738 sha512_ctx_free,
Paul Bakker17373852011-01-06 14:20:01 +0000739};
740
741#endif
742
743#endif