blob: 038b13272b60cf8b450c9ce2e255379461fc854c [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 Bakkerd2681d82013-06-30 14:49:12 +020053#include "polarssl/sha256.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 Bakkerd2681d82013-06-30 14:49:12 +020057#include "polarssl/sha512.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000058#endif
Paul Bakker17373852011-01-06 14:20:01 +000059
Paul Bakker6e339b52013-07-03 13:37:05 +020060#if defined(POLARSSL_MEMORY_C)
61#include "polarssl/memory.h"
62#else
63#define polarssl_malloc malloc
64#define polarssl_free free
65#endif
66
Paul Bakker17373852011-01-06 14:20:01 +000067#include <stdlib.h>
68
69#if defined(POLARSSL_MD2_C)
70
71static void md2_starts_wrap( void *ctx )
72{
73 md2_starts( (md2_context *) ctx );
74}
75
Paul Bakker23986e52011-04-24 08:57:21 +000076static void md2_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000077{
78 md2_update( (md2_context *) ctx, input, ilen );
79}
80
81static void md2_finish_wrap( void *ctx, unsigned char *output )
82{
83 md2_finish( (md2_context *) ctx, output );
84}
85
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020086static int md2_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +000087{
88#if defined(POLARSSL_FS_IO)
89 return md2_file( path, output );
90#else
91 ((void) path);
92 ((void) output);
93 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
94#endif
95}
96
Paul Bakker23986e52011-04-24 08:57:21 +000097static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +000098{
99 md2_hmac_starts( (md2_context *) ctx, key, keylen );
100}
101
Paul Bakker23986e52011-04-24 08:57:21 +0000102static void md2_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000103{
104 md2_hmac_update( (md2_context *) ctx, input, ilen );
105}
106
107static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
108{
109 md2_hmac_finish( (md2_context *) ctx, output );
110}
111
112static void md2_hmac_reset_wrap( void *ctx )
113{
114 md2_hmac_reset( (md2_context *) ctx );
115}
116
117static void * md2_ctx_alloc( void )
118{
Paul Bakker6e339b52013-07-03 13:37:05 +0200119 return polarssl_malloc( sizeof( md2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000120}
121
122static void md2_ctx_free( void *ctx )
123{
Paul Bakker6e339b52013-07-03 13:37:05 +0200124 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000125}
126
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100127static void md2_process_wrap( void *ctx, const unsigned char *data )
128{
129 ((void) data);
130
131 md2_process( (md2_context *) ctx );
132}
133
Paul Bakker17373852011-01-06 14:20:01 +0000134const md_info_t md2_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000135 POLARSSL_MD_MD2,
136 "MD2",
137 16,
138 md2_starts_wrap,
139 md2_update_wrap,
140 md2_finish_wrap,
141 md2,
Paul Bakker335db3f2011-04-25 15:28:35 +0000142 md2_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000143 md2_hmac_starts_wrap,
144 md2_hmac_update_wrap,
145 md2_hmac_finish_wrap,
146 md2_hmac_reset_wrap,
147 md2_hmac,
148 md2_ctx_alloc,
149 md2_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100150 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000151};
152
153#endif
154
155#if defined(POLARSSL_MD4_C)
156
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100157static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000158{
159 md4_starts( (md4_context *) ctx );
160}
161
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100162static void md4_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000163{
164 md4_update( (md4_context *) ctx, input, ilen );
165}
166
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100167static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000168{
169 md4_finish( (md4_context *) ctx, output );
170}
171
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100172static int md4_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000173{
174#if defined(POLARSSL_FS_IO)
175 return md4_file( path, output );
176#else
177 ((void) path);
178 ((void) output);
179 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
180#endif
181}
182
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100183static void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000184{
185 md4_hmac_starts( (md4_context *) ctx, key, keylen );
186}
187
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100188static void md4_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000189{
190 md4_hmac_update( (md4_context *) ctx, input, ilen );
191}
192
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100193static void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000194{
195 md4_hmac_finish( (md4_context *) ctx, output );
196}
197
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100198static void md4_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000199{
200 md4_hmac_reset( (md4_context *) ctx );
201}
202
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100203static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000204{
Paul Bakker6e339b52013-07-03 13:37:05 +0200205 return polarssl_malloc( sizeof( md4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000206}
207
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100208static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000209{
Paul Bakker6e339b52013-07-03 13:37:05 +0200210 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000211}
212
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100213static void md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100214{
215 md4_process( (md4_context *) ctx, data );
216}
217
Paul Bakker17373852011-01-06 14:20:01 +0000218const md_info_t md4_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000219 POLARSSL_MD_MD4,
220 "MD4",
221 16,
222 md4_starts_wrap,
223 md4_update_wrap,
224 md4_finish_wrap,
225 md4,
Paul Bakker335db3f2011-04-25 15:28:35 +0000226 md4_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000227 md4_hmac_starts_wrap,
228 md4_hmac_update_wrap,
229 md4_hmac_finish_wrap,
230 md4_hmac_reset_wrap,
231 md4_hmac,
232 md4_ctx_alloc,
233 md4_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100234 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000235};
236
237#endif
238
239#if defined(POLARSSL_MD5_C)
240
241static void md5_starts_wrap( void *ctx )
242{
243 md5_starts( (md5_context *) ctx );
244}
245
Paul Bakker23986e52011-04-24 08:57:21 +0000246static void md5_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000247{
248 md5_update( (md5_context *) ctx, input, ilen );
249}
250
251static void md5_finish_wrap( void *ctx, unsigned char *output )
252{
253 md5_finish( (md5_context *) ctx, output );
254}
255
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200256static int md5_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000257{
258#if defined(POLARSSL_FS_IO)
259 return md5_file( path, output );
260#else
261 ((void) path);
262 ((void) output);
263 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
264#endif
265}
266
Paul Bakker23986e52011-04-24 08:57:21 +0000267static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000268{
269 md5_hmac_starts( (md5_context *) ctx, key, keylen );
270}
271
Paul Bakker23986e52011-04-24 08:57:21 +0000272static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000273{
274 md5_hmac_update( (md5_context *) ctx, input, ilen );
275}
276
277static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
278{
279 md5_hmac_finish( (md5_context *) ctx, output );
280}
281
282static void md5_hmac_reset_wrap( void *ctx )
283{
284 md5_hmac_reset( (md5_context *) ctx );
285}
286
287static void * md5_ctx_alloc( void )
288{
Paul Bakker6e339b52013-07-03 13:37:05 +0200289 return polarssl_malloc( sizeof( md5_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000290}
291
292static void md5_ctx_free( void *ctx )
293{
Paul Bakker6e339b52013-07-03 13:37:05 +0200294 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000295}
296
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100297static void md5_process_wrap( void *ctx, const unsigned char *data )
298{
299 md5_process( (md5_context *) ctx, data );
300}
301
Paul Bakker17373852011-01-06 14:20:01 +0000302const md_info_t md5_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000303 POLARSSL_MD_MD5,
304 "MD5",
305 16,
306 md5_starts_wrap,
307 md5_update_wrap,
308 md5_finish_wrap,
309 md5,
Paul Bakker335db3f2011-04-25 15:28:35 +0000310 md5_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000311 md5_hmac_starts_wrap,
312 md5_hmac_update_wrap,
313 md5_hmac_finish_wrap,
314 md5_hmac_reset_wrap,
315 md5_hmac,
316 md5_ctx_alloc,
317 md5_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100318 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000319};
320
321#endif
322
323#if defined(POLARSSL_SHA1_C)
324
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100325static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000326{
327 sha1_starts( (sha1_context *) ctx );
328}
329
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100330static void sha1_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000331{
332 sha1_update( (sha1_context *) ctx, input, ilen );
333}
334
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100335static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000336{
337 sha1_finish( (sha1_context *) ctx, output );
338}
339
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100340static int sha1_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000341{
342#if defined(POLARSSL_FS_IO)
343 return sha1_file( path, output );
344#else
345 ((void) path);
346 ((void) output);
347 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
348#endif
349}
350
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100351static void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000352{
353 sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
354}
355
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100356static void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000357{
358 sha1_hmac_update( (sha1_context *) ctx, input, ilen );
359}
360
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100361static void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000362{
363 sha1_hmac_finish( (sha1_context *) ctx, output );
364}
365
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100366static void sha1_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000367{
368 sha1_hmac_reset( (sha1_context *) ctx );
369}
370
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100371static void * sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000372{
Paul Bakker6e339b52013-07-03 13:37:05 +0200373 return polarssl_malloc( sizeof( sha1_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000374}
375
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100376static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000377{
Paul Bakker6e339b52013-07-03 13:37:05 +0200378 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000379}
380
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100381static void sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100382{
383 sha1_process( (sha1_context *) ctx, data );
384}
385
Paul Bakker17373852011-01-06 14:20:01 +0000386const md_info_t sha1_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000387 POLARSSL_MD_SHA1,
388 "SHA1",
389 20,
390 sha1_starts_wrap,
391 sha1_update_wrap,
392 sha1_finish_wrap,
393 sha1,
Paul Bakker335db3f2011-04-25 15:28:35 +0000394 sha1_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000395 sha1_hmac_starts_wrap,
396 sha1_hmac_update_wrap,
397 sha1_hmac_finish_wrap,
398 sha1_hmac_reset_wrap,
399 sha1_hmac,
400 sha1_ctx_alloc,
401 sha1_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100402 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000403};
404
405#endif
406
407/*
408 * Wrappers for generic message digests
409 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200410#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000411
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100412static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000413{
Paul Bakker9e36f042013-06-30 14:34:05 +0200414 sha256_starts( (sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000415}
416
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100417static void sha224_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000418{
Paul Bakker9e36f042013-06-30 14:34:05 +0200419 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000420}
421
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100422static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000423{
Paul Bakker9e36f042013-06-30 14:34:05 +0200424 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000425}
426
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100427static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000428 unsigned char *output )
429{
Paul Bakker9e36f042013-06-30 14:34:05 +0200430 sha256( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000431}
432
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100433static int sha224_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000434{
Paul Bakker335db3f2011-04-25 15:28:35 +0000435#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200436 return sha256_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000437#else
438 ((void) path);
439 ((void) output);
440 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
441#endif
Paul Bakker17373852011-01-06 14:20:01 +0000442}
443
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100444static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000445{
Paul Bakker9e36f042013-06-30 14:34:05 +0200446 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000447}
448
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100449static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000450{
Paul Bakker9e36f042013-06-30 14:34:05 +0200451 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000452}
453
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100454static void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000455{
Paul Bakker9e36f042013-06-30 14:34:05 +0200456 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000457}
458
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100459static void sha224_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000460{
Paul Bakker9e36f042013-06-30 14:34:05 +0200461 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000462}
463
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100464static void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000465 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000466 unsigned char *output )
467{
Paul Bakker9e36f042013-06-30 14:34:05 +0200468 sha256_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000469}
470
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100471static void * sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000472{
Paul Bakker6e339b52013-07-03 13:37:05 +0200473 return polarssl_malloc( sizeof( sha256_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000474}
475
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100476static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000477{
Paul Bakker6e339b52013-07-03 13:37:05 +0200478 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000479}
480
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100481static void sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100482{
Paul Bakker9e36f042013-06-30 14:34:05 +0200483 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100484}
485
Paul Bakker17373852011-01-06 14:20:01 +0000486const md_info_t sha224_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000487 POLARSSL_MD_SHA224,
488 "SHA224",
489 28,
490 sha224_starts_wrap,
491 sha224_update_wrap,
492 sha224_finish_wrap,
493 sha224_wrap,
494 sha224_file_wrap,
495 sha224_hmac_starts_wrap,
496 sha224_hmac_update_wrap,
497 sha224_hmac_finish_wrap,
498 sha224_hmac_reset_wrap,
499 sha224_hmac_wrap,
500 sha224_ctx_alloc,
501 sha224_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100502 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000503};
504
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100505static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000506{
Paul Bakker9e36f042013-06-30 14:34:05 +0200507 sha256_starts( (sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000508}
509
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100510static void sha256_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000511{
Paul Bakker9e36f042013-06-30 14:34:05 +0200512 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000513}
514
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100515static void sha256_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000516{
Paul Bakker9e36f042013-06-30 14:34:05 +0200517 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000518}
519
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100520static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000521 unsigned char *output )
522{
Paul Bakker9e36f042013-06-30 14:34:05 +0200523 sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000524}
525
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100526static int sha256_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000527{
Paul Bakker335db3f2011-04-25 15:28:35 +0000528#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200529 return sha256_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000530#else
531 ((void) path);
532 ((void) output);
533 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
534#endif
Paul Bakker17373852011-01-06 14:20:01 +0000535}
536
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100537static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000538{
Paul Bakker9e36f042013-06-30 14:34:05 +0200539 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000540}
541
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100542static void sha256_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000543{
Paul Bakker9e36f042013-06-30 14:34:05 +0200544 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000545}
546
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100547static void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000548{
Paul Bakker9e36f042013-06-30 14:34:05 +0200549 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000550}
551
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100552static void sha256_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000553{
Paul Bakker9e36f042013-06-30 14:34:05 +0200554 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000555}
556
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100557static void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000558 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000559 unsigned char *output )
560{
Paul Bakker9e36f042013-06-30 14:34:05 +0200561 sha256_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000562}
563
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100564static void * sha256_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000565{
Paul Bakker6e339b52013-07-03 13:37:05 +0200566 return polarssl_malloc( sizeof( sha256_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000567}
568
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100569static void sha256_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000570{
Paul Bakker6e339b52013-07-03 13:37:05 +0200571 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000572}
573
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100574static void sha256_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100575{
Paul Bakker9e36f042013-06-30 14:34:05 +0200576 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100577}
578
Paul Bakker17373852011-01-06 14:20:01 +0000579const md_info_t sha256_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000580 POLARSSL_MD_SHA256,
581 "SHA256",
582 32,
583 sha256_starts_wrap,
584 sha256_update_wrap,
585 sha256_finish_wrap,
586 sha256_wrap,
587 sha256_file_wrap,
588 sha256_hmac_starts_wrap,
589 sha256_hmac_update_wrap,
590 sha256_hmac_finish_wrap,
591 sha256_hmac_reset_wrap,
592 sha256_hmac_wrap,
593 sha256_ctx_alloc,
594 sha256_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100595 sha256_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000596};
597
598#endif
599
Paul Bakker9e36f042013-06-30 14:34:05 +0200600#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000601
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100602static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000603{
Paul Bakker9e36f042013-06-30 14:34:05 +0200604 sha512_starts( (sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000605}
606
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100607static void sha384_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000608{
Paul Bakker9e36f042013-06-30 14:34:05 +0200609 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000610}
611
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100612static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000613{
Paul Bakker9e36f042013-06-30 14:34:05 +0200614 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000615}
616
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100617static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000618 unsigned char *output )
619{
Paul Bakker9e36f042013-06-30 14:34:05 +0200620 sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000621}
622
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100623static int sha384_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000624{
Paul Bakker335db3f2011-04-25 15:28:35 +0000625#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200626 return sha512_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000627#else
628 ((void) path);
629 ((void) output);
630 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
631#endif
Paul Bakker17373852011-01-06 14:20:01 +0000632}
633
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100634static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000635{
Paul Bakker9e36f042013-06-30 14:34:05 +0200636 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000637}
638
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100639static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000640{
Paul Bakker9e36f042013-06-30 14:34:05 +0200641 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000642}
643
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100644static void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000645{
Paul Bakker9e36f042013-06-30 14:34:05 +0200646 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000647}
648
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100649static void sha384_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000650{
Paul Bakker9e36f042013-06-30 14:34:05 +0200651 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000652}
653
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100654static void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000655 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000656 unsigned char *output )
657{
Paul Bakker9e36f042013-06-30 14:34:05 +0200658 sha512_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000659}
660
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100661static void * sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000662{
Paul Bakker6e339b52013-07-03 13:37:05 +0200663 return polarssl_malloc( sizeof( sha512_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000664}
665
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100666static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000667{
Paul Bakker6e339b52013-07-03 13:37:05 +0200668 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000669}
670
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100671static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100672{
Paul Bakker9e36f042013-06-30 14:34:05 +0200673 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100674}
675
Paul Bakker17373852011-01-06 14:20:01 +0000676const md_info_t sha384_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000677 POLARSSL_MD_SHA384,
678 "SHA384",
679 48,
680 sha384_starts_wrap,
681 sha384_update_wrap,
682 sha384_finish_wrap,
683 sha384_wrap,
684 sha384_file_wrap,
685 sha384_hmac_starts_wrap,
686 sha384_hmac_update_wrap,
687 sha384_hmac_finish_wrap,
688 sha384_hmac_reset_wrap,
689 sha384_hmac_wrap,
690 sha384_ctx_alloc,
691 sha384_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100692 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000693};
694
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100695static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000696{
Paul Bakker9e36f042013-06-30 14:34:05 +0200697 sha512_starts( (sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000698}
699
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100700static void sha512_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000701{
Paul Bakker9e36f042013-06-30 14:34:05 +0200702 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000703}
704
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100705static void sha512_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000706{
Paul Bakker9e36f042013-06-30 14:34:05 +0200707 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000708}
709
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100710static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000711 unsigned char *output )
712{
Paul Bakker9e36f042013-06-30 14:34:05 +0200713 sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000714}
715
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100716static int sha512_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000717{
Paul Bakker335db3f2011-04-25 15:28:35 +0000718#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200719 return sha512_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000720#else
721 ((void) path);
722 ((void) output);
723 return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
724#endif
Paul Bakker17373852011-01-06 14:20:01 +0000725}
726
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100727static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000728{
Paul Bakker9e36f042013-06-30 14:34:05 +0200729 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000730}
731
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100732static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000733{
Paul Bakker9e36f042013-06-30 14:34:05 +0200734 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000735}
736
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100737static void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000738{
Paul Bakker9e36f042013-06-30 14:34:05 +0200739 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000740}
741
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100742static void sha512_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000743{
Paul Bakker9e36f042013-06-30 14:34:05 +0200744 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000745}
746
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100747static void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000748 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000749 unsigned char *output )
750{
Paul Bakker9e36f042013-06-30 14:34:05 +0200751 sha512_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000752}
753
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100754static void * sha512_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000755{
Paul Bakker6e339b52013-07-03 13:37:05 +0200756 return polarssl_malloc( sizeof( sha512_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000757}
758
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100759static void sha512_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000760{
Paul Bakker6e339b52013-07-03 13:37:05 +0200761 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000762}
763
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100764static void sha512_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100765{
Paul Bakker9e36f042013-06-30 14:34:05 +0200766 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100767}
768
Paul Bakker17373852011-01-06 14:20:01 +0000769const md_info_t sha512_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000770 POLARSSL_MD_SHA512,
771 "SHA512",
772 64,
773 sha512_starts_wrap,
774 sha512_update_wrap,
775 sha512_finish_wrap,
776 sha512_wrap,
777 sha512_file_wrap,
778 sha512_hmac_starts_wrap,
779 sha512_hmac_update_wrap,
780 sha512_hmac_finish_wrap,
781 sha512_hmac_reset_wrap,
782 sha512_hmac_wrap,
783 sha512_ctx_alloc,
784 sha512_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100785 sha512_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000786};
787
788#endif
789
790#endif