blob: 2621d222ebd43920be3e6fdf7cc067649a322966 [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 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02008 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakker17373852011-01-06 14:20:01 +00009 *
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
Paul Bakker9e36f042013-06-30 14:34:05 +020052#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +000053#include "polarssl/sha2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000054#endif
55
Paul Bakker9e36f042013-06-30 14:34:05 +020056#if defined(POLARSSL_SHA512_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
62#if defined(POLARSSL_MD2_C)
63
64static void md2_starts_wrap( void *ctx )
65{
66 md2_starts( (md2_context *) ctx );
67}
68
Paul Bakker23986e52011-04-24 08:57:21 +000069static void md2_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000070{
71 md2_update( (md2_context *) ctx, input, ilen );
72}
73
74static void md2_finish_wrap( void *ctx, unsigned char *output )
75{
76 md2_finish( (md2_context *) ctx, output );
77}
78
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020079static int md2_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +000080{
81#if defined(POLARSSL_FS_IO)
82 return md2_file( path, output );
83#else
84 ((void) path);
85 ((void) output);
86 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
87#endif
88}
89
Paul Bakker23986e52011-04-24 08:57:21 +000090static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +000091{
92 md2_hmac_starts( (md2_context *) ctx, key, keylen );
93}
94
Paul Bakker23986e52011-04-24 08:57:21 +000095static void md2_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000096{
97 md2_hmac_update( (md2_context *) ctx, input, ilen );
98}
99
100static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
101{
102 md2_hmac_finish( (md2_context *) ctx, output );
103}
104
105static void md2_hmac_reset_wrap( void *ctx )
106{
107 md2_hmac_reset( (md2_context *) ctx );
108}
109
110static void * md2_ctx_alloc( void )
111{
112 return malloc( sizeof( md2_context ) );
113}
114
115static void md2_ctx_free( void *ctx )
116{
117 free( ctx );
118}
119
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100120static void md2_process_wrap( void *ctx, const unsigned char *data )
121{
122 ((void) data);
123
124 md2_process( (md2_context *) ctx );
125}
126
Paul Bakker17373852011-01-06 14:20:01 +0000127const md_info_t md2_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000128 POLARSSL_MD_MD2,
129 "MD2",
130 16,
131 md2_starts_wrap,
132 md2_update_wrap,
133 md2_finish_wrap,
134 md2,
Paul Bakker335db3f2011-04-25 15:28:35 +0000135 md2_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000136 md2_hmac_starts_wrap,
137 md2_hmac_update_wrap,
138 md2_hmac_finish_wrap,
139 md2_hmac_reset_wrap,
140 md2_hmac,
141 md2_ctx_alloc,
142 md2_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100143 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000144};
145
146#endif
147
148#if defined(POLARSSL_MD4_C)
149
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100150static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000151{
152 md4_starts( (md4_context *) ctx );
153}
154
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100155static void md4_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000156{
157 md4_update( (md4_context *) ctx, input, ilen );
158}
159
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100160static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000161{
162 md4_finish( (md4_context *) ctx, output );
163}
164
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100165static int md4_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000166{
167#if defined(POLARSSL_FS_IO)
168 return md4_file( path, output );
169#else
170 ((void) path);
171 ((void) output);
172 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
173#endif
174}
175
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100176static void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000177{
178 md4_hmac_starts( (md4_context *) ctx, key, keylen );
179}
180
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100181static void md4_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000182{
183 md4_hmac_update( (md4_context *) ctx, input, ilen );
184}
185
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100186static void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000187{
188 md4_hmac_finish( (md4_context *) ctx, output );
189}
190
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100191static void md4_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000192{
193 md4_hmac_reset( (md4_context *) ctx );
194}
195
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100196static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000197{
198 return malloc( sizeof( md4_context ) );
199}
200
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100201static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000202{
203 free( ctx );
204}
205
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100206static void md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100207{
208 md4_process( (md4_context *) ctx, data );
209}
210
Paul Bakker17373852011-01-06 14:20:01 +0000211const md_info_t md4_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000212 POLARSSL_MD_MD4,
213 "MD4",
214 16,
215 md4_starts_wrap,
216 md4_update_wrap,
217 md4_finish_wrap,
218 md4,
Paul Bakker335db3f2011-04-25 15:28:35 +0000219 md4_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000220 md4_hmac_starts_wrap,
221 md4_hmac_update_wrap,
222 md4_hmac_finish_wrap,
223 md4_hmac_reset_wrap,
224 md4_hmac,
225 md4_ctx_alloc,
226 md4_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100227 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000228};
229
230#endif
231
232#if defined(POLARSSL_MD5_C)
233
234static void md5_starts_wrap( void *ctx )
235{
236 md5_starts( (md5_context *) ctx );
237}
238
Paul Bakker23986e52011-04-24 08:57:21 +0000239static void md5_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000240{
241 md5_update( (md5_context *) ctx, input, ilen );
242}
243
244static void md5_finish_wrap( void *ctx, unsigned char *output )
245{
246 md5_finish( (md5_context *) ctx, output );
247}
248
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200249static int md5_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000250{
251#if defined(POLARSSL_FS_IO)
252 return md5_file( path, output );
253#else
254 ((void) path);
255 ((void) output);
256 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
257#endif
258}
259
Paul Bakker23986e52011-04-24 08:57:21 +0000260static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000261{
262 md5_hmac_starts( (md5_context *) ctx, key, keylen );
263}
264
Paul Bakker23986e52011-04-24 08:57:21 +0000265static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000266{
267 md5_hmac_update( (md5_context *) ctx, input, ilen );
268}
269
270static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
271{
272 md5_hmac_finish( (md5_context *) ctx, output );
273}
274
275static void md5_hmac_reset_wrap( void *ctx )
276{
277 md5_hmac_reset( (md5_context *) ctx );
278}
279
280static void * md5_ctx_alloc( void )
281{
282 return malloc( sizeof( md5_context ) );
283}
284
285static void md5_ctx_free( void *ctx )
286{
287 free( ctx );
288}
289
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100290static void md5_process_wrap( void *ctx, const unsigned char *data )
291{
292 md5_process( (md5_context *) ctx, data );
293}
294
Paul Bakker17373852011-01-06 14:20:01 +0000295const md_info_t md5_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000296 POLARSSL_MD_MD5,
297 "MD5",
298 16,
299 md5_starts_wrap,
300 md5_update_wrap,
301 md5_finish_wrap,
302 md5,
Paul Bakker335db3f2011-04-25 15:28:35 +0000303 md5_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000304 md5_hmac_starts_wrap,
305 md5_hmac_update_wrap,
306 md5_hmac_finish_wrap,
307 md5_hmac_reset_wrap,
308 md5_hmac,
309 md5_ctx_alloc,
310 md5_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100311 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000312};
313
314#endif
315
316#if defined(POLARSSL_SHA1_C)
317
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100318static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000319{
320 sha1_starts( (sha1_context *) ctx );
321}
322
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100323static void sha1_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000324{
325 sha1_update( (sha1_context *) ctx, input, ilen );
326}
327
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100328static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000329{
330 sha1_finish( (sha1_context *) ctx, output );
331}
332
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100333static int sha1_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000334{
335#if defined(POLARSSL_FS_IO)
336 return sha1_file( path, output );
337#else
338 ((void) path);
339 ((void) output);
340 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
341#endif
342}
343
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100344static void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000345{
346 sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
347}
348
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100349static void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000350{
351 sha1_hmac_update( (sha1_context *) ctx, input, ilen );
352}
353
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100354static void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000355{
356 sha1_hmac_finish( (sha1_context *) ctx, output );
357}
358
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100359static void sha1_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000360{
361 sha1_hmac_reset( (sha1_context *) ctx );
362}
363
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100364static void * sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000365{
366 return malloc( sizeof( sha1_context ) );
367}
368
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100369static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000370{
371 free( ctx );
372}
373
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100374static void sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100375{
376 sha1_process( (sha1_context *) ctx, data );
377}
378
Paul Bakker17373852011-01-06 14:20:01 +0000379const md_info_t sha1_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000380 POLARSSL_MD_SHA1,
381 "SHA1",
382 20,
383 sha1_starts_wrap,
384 sha1_update_wrap,
385 sha1_finish_wrap,
386 sha1,
Paul Bakker335db3f2011-04-25 15:28:35 +0000387 sha1_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000388 sha1_hmac_starts_wrap,
389 sha1_hmac_update_wrap,
390 sha1_hmac_finish_wrap,
391 sha1_hmac_reset_wrap,
392 sha1_hmac,
393 sha1_ctx_alloc,
394 sha1_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100395 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000396};
397
398#endif
399
400/*
401 * Wrappers for generic message digests
402 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200403#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000404
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100405static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000406{
Paul Bakker9e36f042013-06-30 14:34:05 +0200407 sha256_starts( (sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000408}
409
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100410static void sha224_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000411{
Paul Bakker9e36f042013-06-30 14:34:05 +0200412 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000413}
414
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100415static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000416{
Paul Bakker9e36f042013-06-30 14:34:05 +0200417 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000418}
419
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100420static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000421 unsigned char *output )
422{
Paul Bakker9e36f042013-06-30 14:34:05 +0200423 sha256( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000424}
425
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100426static int sha224_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000427{
Paul Bakker335db3f2011-04-25 15:28:35 +0000428#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200429 return sha256_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000430#else
431 ((void) path);
432 ((void) output);
433 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
434#endif
Paul Bakker17373852011-01-06 14:20:01 +0000435}
436
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100437static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000438{
Paul Bakker9e36f042013-06-30 14:34:05 +0200439 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000440}
441
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100442static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000443{
Paul Bakker9e36f042013-06-30 14:34:05 +0200444 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000445}
446
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100447static void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000448{
Paul Bakker9e36f042013-06-30 14:34:05 +0200449 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000450}
451
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100452static void sha224_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000453{
Paul Bakker9e36f042013-06-30 14:34:05 +0200454 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000455}
456
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100457static void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000458 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000459 unsigned char *output )
460{
Paul Bakker9e36f042013-06-30 14:34:05 +0200461 sha256_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000462}
463
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100464static void * sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000465{
Paul Bakker9e36f042013-06-30 14:34:05 +0200466 return malloc( sizeof( sha256_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000467}
468
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100469static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000470{
471 free( ctx );
472}
473
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100474static void sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100475{
Paul Bakker9e36f042013-06-30 14:34:05 +0200476 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100477}
478
Paul Bakker17373852011-01-06 14:20:01 +0000479const md_info_t sha224_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000480 POLARSSL_MD_SHA224,
481 "SHA224",
482 28,
483 sha224_starts_wrap,
484 sha224_update_wrap,
485 sha224_finish_wrap,
486 sha224_wrap,
487 sha224_file_wrap,
488 sha224_hmac_starts_wrap,
489 sha224_hmac_update_wrap,
490 sha224_hmac_finish_wrap,
491 sha224_hmac_reset_wrap,
492 sha224_hmac_wrap,
493 sha224_ctx_alloc,
494 sha224_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100495 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000496};
497
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100498static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000499{
Paul Bakker9e36f042013-06-30 14:34:05 +0200500 sha256_starts( (sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000501}
502
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100503static void sha256_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000504{
Paul Bakker9e36f042013-06-30 14:34:05 +0200505 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000506}
507
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100508static void sha256_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000509{
Paul Bakker9e36f042013-06-30 14:34:05 +0200510 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000511}
512
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100513static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000514 unsigned char *output )
515{
Paul Bakker9e36f042013-06-30 14:34:05 +0200516 sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000517}
518
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100519static int sha256_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000520{
Paul Bakker335db3f2011-04-25 15:28:35 +0000521#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200522 return sha256_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000523#else
524 ((void) path);
525 ((void) output);
526 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
527#endif
Paul Bakker17373852011-01-06 14:20:01 +0000528}
529
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100530static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000531{
Paul Bakker9e36f042013-06-30 14:34:05 +0200532 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000533}
534
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100535static void sha256_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000536{
Paul Bakker9e36f042013-06-30 14:34:05 +0200537 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000538}
539
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100540static void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000541{
Paul Bakker9e36f042013-06-30 14:34:05 +0200542 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000543}
544
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100545static void sha256_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000546{
Paul Bakker9e36f042013-06-30 14:34:05 +0200547 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000548}
549
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100550static void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000551 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000552 unsigned char *output )
553{
Paul Bakker9e36f042013-06-30 14:34:05 +0200554 sha256_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000555}
556
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100557static void * sha256_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000558{
Paul Bakker9e36f042013-06-30 14:34:05 +0200559 return malloc( sizeof( sha256_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000560}
561
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100562static void sha256_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000563{
564 free( ctx );
565}
566
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100567static void sha256_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100568{
Paul Bakker9e36f042013-06-30 14:34:05 +0200569 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100570}
571
Paul Bakker17373852011-01-06 14:20:01 +0000572const md_info_t sha256_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000573 POLARSSL_MD_SHA256,
574 "SHA256",
575 32,
576 sha256_starts_wrap,
577 sha256_update_wrap,
578 sha256_finish_wrap,
579 sha256_wrap,
580 sha256_file_wrap,
581 sha256_hmac_starts_wrap,
582 sha256_hmac_update_wrap,
583 sha256_hmac_finish_wrap,
584 sha256_hmac_reset_wrap,
585 sha256_hmac_wrap,
586 sha256_ctx_alloc,
587 sha256_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100588 sha256_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000589};
590
591#endif
592
Paul Bakker9e36f042013-06-30 14:34:05 +0200593#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000594
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100595static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000596{
Paul Bakker9e36f042013-06-30 14:34:05 +0200597 sha512_starts( (sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000598}
599
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100600static void sha384_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000601{
Paul Bakker9e36f042013-06-30 14:34:05 +0200602 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000603}
604
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100605static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000606{
Paul Bakker9e36f042013-06-30 14:34:05 +0200607 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000608}
609
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100610static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000611 unsigned char *output )
612{
Paul Bakker9e36f042013-06-30 14:34:05 +0200613 sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000614}
615
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100616static int sha384_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000617{
Paul Bakker335db3f2011-04-25 15:28:35 +0000618#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200619 return sha512_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000620#else
621 ((void) path);
622 ((void) output);
623 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
624#endif
Paul Bakker17373852011-01-06 14:20:01 +0000625}
626
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100627static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000628{
Paul Bakker9e36f042013-06-30 14:34:05 +0200629 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000630}
631
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100632static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000633{
Paul Bakker9e36f042013-06-30 14:34:05 +0200634 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000635}
636
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100637static void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000638{
Paul Bakker9e36f042013-06-30 14:34:05 +0200639 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000640}
641
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100642static void sha384_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000643{
Paul Bakker9e36f042013-06-30 14:34:05 +0200644 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000645}
646
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100647static void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000648 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000649 unsigned char *output )
650{
Paul Bakker9e36f042013-06-30 14:34:05 +0200651 sha512_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000652}
653
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100654static void * sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000655{
Paul Bakker9e36f042013-06-30 14:34:05 +0200656 return malloc( sizeof( sha512_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000657}
658
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100659static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000660{
661 free( ctx );
662}
663
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100664static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100665{
Paul Bakker9e36f042013-06-30 14:34:05 +0200666 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100667}
668
Paul Bakker17373852011-01-06 14:20:01 +0000669const md_info_t sha384_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000670 POLARSSL_MD_SHA384,
671 "SHA384",
672 48,
673 sha384_starts_wrap,
674 sha384_update_wrap,
675 sha384_finish_wrap,
676 sha384_wrap,
677 sha384_file_wrap,
678 sha384_hmac_starts_wrap,
679 sha384_hmac_update_wrap,
680 sha384_hmac_finish_wrap,
681 sha384_hmac_reset_wrap,
682 sha384_hmac_wrap,
683 sha384_ctx_alloc,
684 sha384_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100685 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000686};
687
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100688static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000689{
Paul Bakker9e36f042013-06-30 14:34:05 +0200690 sha512_starts( (sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000691}
692
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100693static void sha512_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000694{
Paul Bakker9e36f042013-06-30 14:34:05 +0200695 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000696}
697
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100698static void sha512_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000699{
Paul Bakker9e36f042013-06-30 14:34:05 +0200700 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000701}
702
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100703static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000704 unsigned char *output )
705{
Paul Bakker9e36f042013-06-30 14:34:05 +0200706 sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000707}
708
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100709static int sha512_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000710{
Paul Bakker335db3f2011-04-25 15:28:35 +0000711#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200712 return sha512_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000713#else
714 ((void) path);
715 ((void) output);
716 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
717#endif
Paul Bakker17373852011-01-06 14:20:01 +0000718}
719
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100720static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000721{
Paul Bakker9e36f042013-06-30 14:34:05 +0200722 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000723}
724
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100725static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000726{
Paul Bakker9e36f042013-06-30 14:34:05 +0200727 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000728}
729
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100730static void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000731{
Paul Bakker9e36f042013-06-30 14:34:05 +0200732 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000733}
734
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100735static void sha512_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000736{
Paul Bakker9e36f042013-06-30 14:34:05 +0200737 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000738}
739
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100740static void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000741 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000742 unsigned char *output )
743{
Paul Bakker9e36f042013-06-30 14:34:05 +0200744 sha512_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000745}
746
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100747static void * sha512_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000748{
Paul Bakker9e36f042013-06-30 14:34:05 +0200749 return malloc( sizeof( sha512_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000750}
751
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100752static void sha512_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000753{
754 free( ctx );
755}
756
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100757static void sha512_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100758{
Paul Bakker9e36f042013-06-30 14:34:05 +0200759 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100760}
761
Paul Bakker17373852011-01-06 14:20:01 +0000762const md_info_t sha512_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000763 POLARSSL_MD_SHA512,
764 "SHA512",
765 64,
766 sha512_starts_wrap,
767 sha512_update_wrap,
768 sha512_finish_wrap,
769 sha512_wrap,
770 sha512_file_wrap,
771 sha512_hmac_starts_wrap,
772 sha512_hmac_update_wrap,
773 sha512_hmac_finish_wrap,
774 sha512_hmac_reset_wrap,
775 sha512_hmac_wrap,
776 sha512_ctx_alloc,
777 sha512_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100778 sha512_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000779};
780
781#endif
782
783#endif