blob: fdae018f90da531d20087f7258b759ac69ff23bd [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 Bakker7dc4c442014-02-01 22:50:26 +01008 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker17373852011-01-06 14:20:01 +00009 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +000010 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker17373852011-01-06 14:20:01 +000011 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker17373852011-01-06 14:20:01 +000031#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker17373852011-01-06 14:20:01 +000035
36#if defined(POLARSSL_MD_C)
37
38#include "polarssl/md_wrap.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000039
40#if defined(POLARSSL_MD2_C)
Paul Bakker17373852011-01-06 14:20:01 +000041#include "polarssl/md2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000042#endif
43
44#if defined(POLARSSL_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +000045#include "polarssl/md4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000046#endif
47
48#if defined(POLARSSL_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +000049#include "polarssl/md5.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000050#endif
51
Paul Bakker61b699e2014-01-22 13:35:29 +010052#if defined(POLARSSL_RIPEMD160_C)
53#include "polarssl/ripemd160.h"
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +010054#endif
55
Paul Bakkerf6543712012-03-05 14:01:29 +000056#if defined(POLARSSL_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +000057#include "polarssl/sha1.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000058#endif
59
Paul Bakker9e36f042013-06-30 14:34:05 +020060#if defined(POLARSSL_SHA256_C)
Paul Bakkerd2681d82013-06-30 14:49:12 +020061#include "polarssl/sha256.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000062#endif
63
Paul Bakker9e36f042013-06-30 14:34:05 +020064#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2681d82013-06-30 14:49:12 +020065#include "polarssl/sha512.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000066#endif
Paul Bakker17373852011-01-06 14:20:01 +000067
Paul Bakker7dc4c442014-02-01 22:50:26 +010068#if defined(POLARSSL_PLATFORM_C)
69#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020070#else
71#define polarssl_malloc malloc
72#define polarssl_free free
73#endif
74
Paul Bakker17373852011-01-06 14:20:01 +000075#include <stdlib.h>
76
Paul Bakker34617722014-06-13 17:20:13 +020077/* Implementation that should never be optimized out by the compiler */
78static void polarssl_zeroize( void *v, size_t n ) {
79 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
80}
81
Paul Bakker17373852011-01-06 14:20:01 +000082#if defined(POLARSSL_MD2_C)
83
84static void md2_starts_wrap( void *ctx )
85{
86 md2_starts( (md2_context *) ctx );
87}
88
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020089static void md2_update_wrap( void *ctx, const unsigned char *input,
90 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000091{
92 md2_update( (md2_context *) ctx, input, ilen );
93}
94
95static void md2_finish_wrap( void *ctx, unsigned char *output )
96{
97 md2_finish( (md2_context *) ctx, output );
98}
99
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200100static int md2_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000101{
102#if defined(POLARSSL_FS_IO)
103 return md2_file( path, output );
104#else
105 ((void) path);
106 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200107 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000108#endif
109}
110
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200111static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key,
112 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000113{
114 md2_hmac_starts( (md2_context *) ctx, key, keylen );
115}
116
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200117static void md2_hmac_update_wrap( void *ctx, const unsigned char *input,
118 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000119{
120 md2_hmac_update( (md2_context *) ctx, input, ilen );
121}
122
123static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
124{
125 md2_hmac_finish( (md2_context *) ctx, output );
126}
127
128static void md2_hmac_reset_wrap( void *ctx )
129{
130 md2_hmac_reset( (md2_context *) ctx );
131}
132
133static void * md2_ctx_alloc( void )
134{
Paul Bakker6e339b52013-07-03 13:37:05 +0200135 return polarssl_malloc( sizeof( md2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000136}
137
138static void md2_ctx_free( void *ctx )
139{
Paul Bakker34617722014-06-13 17:20:13 +0200140 polarssl_zeroize( ctx, sizeof( md2_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200141 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000142}
143
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100144static void md2_process_wrap( void *ctx, const unsigned char *data )
145{
146 ((void) data);
147
148 md2_process( (md2_context *) ctx );
149}
150
Paul Bakker17373852011-01-06 14:20:01 +0000151const md_info_t md2_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000152 POLARSSL_MD_MD2,
153 "MD2",
154 16,
155 md2_starts_wrap,
156 md2_update_wrap,
157 md2_finish_wrap,
158 md2,
Paul Bakker335db3f2011-04-25 15:28:35 +0000159 md2_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000160 md2_hmac_starts_wrap,
161 md2_hmac_update_wrap,
162 md2_hmac_finish_wrap,
163 md2_hmac_reset_wrap,
164 md2_hmac,
165 md2_ctx_alloc,
166 md2_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100167 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000168};
169
Paul Bakker9af723c2014-05-01 13:03:14 +0200170#endif /* POLARSSL_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000171
172#if defined(POLARSSL_MD4_C)
173
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100174static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000175{
176 md4_starts( (md4_context *) ctx );
177}
178
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200179static void md4_update_wrap( void *ctx, const unsigned char *input,
180 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000181{
182 md4_update( (md4_context *) ctx, input, ilen );
183}
184
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100185static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000186{
187 md4_finish( (md4_context *) ctx, output );
188}
189
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100190static int md4_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000191{
192#if defined(POLARSSL_FS_IO)
193 return md4_file( path, output );
194#else
195 ((void) path);
196 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200197 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000198#endif
199}
200
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200201static void md4_hmac_starts_wrap( void *ctx, const unsigned char *key,
202 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000203{
204 md4_hmac_starts( (md4_context *) ctx, key, keylen );
205}
206
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200207static void md4_hmac_update_wrap( void *ctx, const unsigned char *input,
208 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000209{
210 md4_hmac_update( (md4_context *) ctx, input, ilen );
211}
212
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100213static void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000214{
215 md4_hmac_finish( (md4_context *) ctx, output );
216}
217
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100218static void md4_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000219{
220 md4_hmac_reset( (md4_context *) ctx );
221}
222
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100223static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000224{
Paul Bakker6e339b52013-07-03 13:37:05 +0200225 return polarssl_malloc( sizeof( md4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000226}
227
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100228static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000229{
Paul Bakker34617722014-06-13 17:20:13 +0200230 polarssl_zeroize( ctx, sizeof( md4_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200231 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000232}
233
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100234static void md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100235{
236 md4_process( (md4_context *) ctx, data );
237}
238
Paul Bakker17373852011-01-06 14:20:01 +0000239const md_info_t md4_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000240 POLARSSL_MD_MD4,
241 "MD4",
242 16,
243 md4_starts_wrap,
244 md4_update_wrap,
245 md4_finish_wrap,
246 md4,
Paul Bakker335db3f2011-04-25 15:28:35 +0000247 md4_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000248 md4_hmac_starts_wrap,
249 md4_hmac_update_wrap,
250 md4_hmac_finish_wrap,
251 md4_hmac_reset_wrap,
252 md4_hmac,
253 md4_ctx_alloc,
254 md4_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100255 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000256};
257
Paul Bakker9af723c2014-05-01 13:03:14 +0200258#endif /* POLARSSL_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000259
260#if defined(POLARSSL_MD5_C)
261
262static void md5_starts_wrap( void *ctx )
263{
264 md5_starts( (md5_context *) ctx );
265}
266
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200267static void md5_update_wrap( void *ctx, const unsigned char *input,
268 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000269{
270 md5_update( (md5_context *) ctx, input, ilen );
271}
272
273static void md5_finish_wrap( void *ctx, unsigned char *output )
274{
275 md5_finish( (md5_context *) ctx, output );
276}
277
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200278static int md5_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000279{
280#if defined(POLARSSL_FS_IO)
281 return md5_file( path, output );
282#else
283 ((void) path);
284 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200285 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000286#endif
287}
288
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200289static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key,
290 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000291{
292 md5_hmac_starts( (md5_context *) ctx, key, keylen );
293}
294
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200295static void md5_hmac_update_wrap( void *ctx, const unsigned char *input,
296 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000297{
298 md5_hmac_update( (md5_context *) ctx, input, ilen );
299}
300
301static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
302{
303 md5_hmac_finish( (md5_context *) ctx, output );
304}
305
306static void md5_hmac_reset_wrap( void *ctx )
307{
308 md5_hmac_reset( (md5_context *) ctx );
309}
310
311static void * md5_ctx_alloc( void )
312{
Paul Bakker6e339b52013-07-03 13:37:05 +0200313 return polarssl_malloc( sizeof( md5_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000314}
315
316static void md5_ctx_free( void *ctx )
317{
Paul Bakker34617722014-06-13 17:20:13 +0200318 polarssl_zeroize( ctx, sizeof( md5_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200319 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000320}
321
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100322static void md5_process_wrap( void *ctx, const unsigned char *data )
323{
324 md5_process( (md5_context *) ctx, data );
325}
326
Paul Bakker17373852011-01-06 14:20:01 +0000327const md_info_t md5_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000328 POLARSSL_MD_MD5,
329 "MD5",
330 16,
331 md5_starts_wrap,
332 md5_update_wrap,
333 md5_finish_wrap,
334 md5,
Paul Bakker335db3f2011-04-25 15:28:35 +0000335 md5_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000336 md5_hmac_starts_wrap,
337 md5_hmac_update_wrap,
338 md5_hmac_finish_wrap,
339 md5_hmac_reset_wrap,
340 md5_hmac,
341 md5_ctx_alloc,
342 md5_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100343 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000344};
345
Paul Bakker9af723c2014-05-01 13:03:14 +0200346#endif /* POLARSSL_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000347
Paul Bakker61b699e2014-01-22 13:35:29 +0100348#if defined(POLARSSL_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100349
Paul Bakker61b699e2014-01-22 13:35:29 +0100350static void ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100351{
Paul Bakker61b699e2014-01-22 13:35:29 +0100352 ripemd160_starts( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100353}
354
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200355static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
356 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100357{
Paul Bakker61b699e2014-01-22 13:35:29 +0100358 ripemd160_update( (ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100359}
360
Paul Bakker61b699e2014-01-22 13:35:29 +0100361static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100362{
Paul Bakker61b699e2014-01-22 13:35:29 +0100363 ripemd160_finish( (ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100364}
365
Paul Bakker61b699e2014-01-22 13:35:29 +0100366static int ripemd160_file_wrap( const char *path, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100367{
368#if defined(POLARSSL_FS_IO)
Paul Bakker61b699e2014-01-22 13:35:29 +0100369 return ripemd160_file( path, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100370#else
371 ((void) path);
372 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200373 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100374#endif
375}
376
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200377static void ripemd160_hmac_starts_wrap( void *ctx, const unsigned char *key,
378 size_t keylen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100379{
Paul Bakker61b699e2014-01-22 13:35:29 +0100380 ripemd160_hmac_starts( (ripemd160_context *) ctx, key, keylen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100381}
382
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200383static void ripemd160_hmac_update_wrap( void *ctx, const unsigned char *input,
384 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100385{
Paul Bakker61b699e2014-01-22 13:35:29 +0100386 ripemd160_hmac_update( (ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100387}
388
Paul Bakker61b699e2014-01-22 13:35:29 +0100389static void ripemd160_hmac_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100390{
Paul Bakker61b699e2014-01-22 13:35:29 +0100391 ripemd160_hmac_finish( (ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100392}
393
Paul Bakker61b699e2014-01-22 13:35:29 +0100394static void ripemd160_hmac_reset_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100395{
Paul Bakker61b699e2014-01-22 13:35:29 +0100396 ripemd160_hmac_reset( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100397}
398
Paul Bakker61b699e2014-01-22 13:35:29 +0100399static void * ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100400{
Paul Bakker5b4af392014-06-26 12:09:34 +0200401 ripemd160_context *ctx;
402 ctx = (ripemd160_context *) polarssl_malloc( sizeof( ripemd160_context ) );
403
404 if( ctx == NULL )
405 return( NULL );
406
407 ripemd160_init( ctx );
408
409 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100410}
411
Paul Bakker61b699e2014-01-22 13:35:29 +0100412static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100413{
Paul Bakker5b4af392014-06-26 12:09:34 +0200414 ripemd160_free( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100415 polarssl_free( ctx );
416}
417
Paul Bakker61b699e2014-01-22 13:35:29 +0100418static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100419{
Paul Bakker61b699e2014-01-22 13:35:29 +0100420 ripemd160_process( (ripemd160_context *) ctx, data );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100421}
422
Paul Bakker61b699e2014-01-22 13:35:29 +0100423const md_info_t ripemd160_info = {
424 POLARSSL_MD_RIPEMD160,
425 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100426 20,
Paul Bakker61b699e2014-01-22 13:35:29 +0100427 ripemd160_starts_wrap,
428 ripemd160_update_wrap,
429 ripemd160_finish_wrap,
430 ripemd160,
431 ripemd160_file_wrap,
432 ripemd160_hmac_starts_wrap,
433 ripemd160_hmac_update_wrap,
434 ripemd160_hmac_finish_wrap,
435 ripemd160_hmac_reset_wrap,
436 ripemd160_hmac,
437 ripemd160_ctx_alloc,
438 ripemd160_ctx_free,
439 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100440};
441
Paul Bakker9af723c2014-05-01 13:03:14 +0200442#endif /* POLARSSL_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100443
Paul Bakker17373852011-01-06 14:20:01 +0000444#if defined(POLARSSL_SHA1_C)
445
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100446static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000447{
448 sha1_starts( (sha1_context *) ctx );
449}
450
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200451static void sha1_update_wrap( void *ctx, const unsigned char *input,
452 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000453{
454 sha1_update( (sha1_context *) ctx, input, ilen );
455}
456
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100457static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000458{
459 sha1_finish( (sha1_context *) ctx, output );
460}
461
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100462static int sha1_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000463{
464#if defined(POLARSSL_FS_IO)
465 return sha1_file( path, output );
466#else
467 ((void) path);
468 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200469 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000470#endif
471}
472
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200473static void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key,
474 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000475{
476 sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
477}
478
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200479static void sha1_hmac_update_wrap( void *ctx, const unsigned char *input,
480 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000481{
482 sha1_hmac_update( (sha1_context *) ctx, input, ilen );
483}
484
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100485static void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000486{
487 sha1_hmac_finish( (sha1_context *) ctx, output );
488}
489
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100490static void sha1_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000491{
492 sha1_hmac_reset( (sha1_context *) ctx );
493}
494
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100495static void * sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000496{
Paul Bakker5b4af392014-06-26 12:09:34 +0200497 sha1_context *ctx;
498 ctx = (sha1_context *) polarssl_malloc( sizeof( sha1_context ) );
499
500 if( ctx == NULL )
501 return( NULL );
502
503 sha1_init( ctx );
504
505 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000506}
507
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100508static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000509{
Paul Bakker5b4af392014-06-26 12:09:34 +0200510 sha1_free( (sha1_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200511 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000512}
513
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100514static void sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100515{
516 sha1_process( (sha1_context *) ctx, data );
517}
518
Paul Bakker17373852011-01-06 14:20:01 +0000519const md_info_t sha1_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000520 POLARSSL_MD_SHA1,
521 "SHA1",
522 20,
523 sha1_starts_wrap,
524 sha1_update_wrap,
525 sha1_finish_wrap,
526 sha1,
Paul Bakker335db3f2011-04-25 15:28:35 +0000527 sha1_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000528 sha1_hmac_starts_wrap,
529 sha1_hmac_update_wrap,
530 sha1_hmac_finish_wrap,
531 sha1_hmac_reset_wrap,
532 sha1_hmac,
533 sha1_ctx_alloc,
534 sha1_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100535 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000536};
537
Paul Bakker9af723c2014-05-01 13:03:14 +0200538#endif /* POLARSSL_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000539
540/*
541 * Wrappers for generic message digests
542 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200543#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000544
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100545static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000546{
Paul Bakker9e36f042013-06-30 14:34:05 +0200547 sha256_starts( (sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000548}
549
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200550static void sha224_update_wrap( void *ctx, const unsigned char *input,
551 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000552{
Paul Bakker9e36f042013-06-30 14:34:05 +0200553 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000554}
555
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100556static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000557{
Paul Bakker9e36f042013-06-30 14:34:05 +0200558 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000559}
560
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100561static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000562 unsigned char *output )
563{
Paul Bakker9e36f042013-06-30 14:34:05 +0200564 sha256( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000565}
566
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100567static int sha224_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000568{
Paul Bakker335db3f2011-04-25 15:28:35 +0000569#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200570 return sha256_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000571#else
572 ((void) path);
573 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200574 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000575#endif
Paul Bakker17373852011-01-06 14:20:01 +0000576}
577
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200578static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key,
579 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000580{
Paul Bakker9e36f042013-06-30 14:34:05 +0200581 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000582}
583
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200584static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input,
585 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000586{
Paul Bakker9e36f042013-06-30 14:34:05 +0200587 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000588}
589
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100590static void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000591{
Paul Bakker9e36f042013-06-30 14:34:05 +0200592 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000593}
594
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100595static void sha224_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000596{
Paul Bakker9e36f042013-06-30 14:34:05 +0200597 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000598}
599
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100600static void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000601 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000602 unsigned char *output )
603{
Paul Bakker9e36f042013-06-30 14:34:05 +0200604 sha256_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000605}
606
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100607static void * sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000608{
Paul Bakker6e339b52013-07-03 13:37:05 +0200609 return polarssl_malloc( sizeof( sha256_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000610}
611
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100612static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000613{
Paul Bakker34617722014-06-13 17:20:13 +0200614 polarssl_zeroize( ctx, sizeof( sha256_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200615 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000616}
617
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100618static void sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100619{
Paul Bakker9e36f042013-06-30 14:34:05 +0200620 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100621}
622
Paul Bakker17373852011-01-06 14:20:01 +0000623const md_info_t sha224_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000624 POLARSSL_MD_SHA224,
625 "SHA224",
626 28,
627 sha224_starts_wrap,
628 sha224_update_wrap,
629 sha224_finish_wrap,
630 sha224_wrap,
631 sha224_file_wrap,
632 sha224_hmac_starts_wrap,
633 sha224_hmac_update_wrap,
634 sha224_hmac_finish_wrap,
635 sha224_hmac_reset_wrap,
636 sha224_hmac_wrap,
637 sha224_ctx_alloc,
638 sha224_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100639 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000640};
641
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100642static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000643{
Paul Bakker9e36f042013-06-30 14:34:05 +0200644 sha256_starts( (sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000645}
646
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200647static void sha256_update_wrap( void *ctx, const unsigned char *input,
648 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000649{
Paul Bakker9e36f042013-06-30 14:34:05 +0200650 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000651}
652
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100653static void sha256_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000654{
Paul Bakker9e36f042013-06-30 14:34:05 +0200655 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000656}
657
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100658static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000659 unsigned char *output )
660{
Paul Bakker9e36f042013-06-30 14:34:05 +0200661 sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000662}
663
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100664static int sha256_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000665{
Paul Bakker335db3f2011-04-25 15:28:35 +0000666#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200667 return sha256_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000668#else
669 ((void) path);
670 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000672#endif
Paul Bakker17373852011-01-06 14:20:01 +0000673}
674
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200675static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key,
676 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000677{
Paul Bakker9e36f042013-06-30 14:34:05 +0200678 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000679}
680
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200681static void sha256_hmac_update_wrap( void *ctx, const unsigned char *input,
682 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000683{
Paul Bakker9e36f042013-06-30 14:34:05 +0200684 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000685}
686
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100687static void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000688{
Paul Bakker9e36f042013-06-30 14:34:05 +0200689 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000690}
691
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100692static void sha256_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000693{
Paul Bakker9e36f042013-06-30 14:34:05 +0200694 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000695}
696
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100697static void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000698 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000699 unsigned char *output )
700{
Paul Bakker9e36f042013-06-30 14:34:05 +0200701 sha256_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000702}
703
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100704static void * sha256_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000705{
Paul Bakker5b4af392014-06-26 12:09:34 +0200706 sha256_context *ctx;
707 ctx = (sha256_context *) polarssl_malloc( sizeof( sha256_context ) );
708
709 if( ctx == NULL )
710 return( NULL );
711
712 sha256_init( ctx );
713
714 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000715}
716
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100717static void sha256_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000718{
Paul Bakker5b4af392014-06-26 12:09:34 +0200719 sha256_free( (sha256_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200720 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000721}
722
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100723static void sha256_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100724{
Paul Bakker9e36f042013-06-30 14:34:05 +0200725 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100726}
727
Paul Bakker17373852011-01-06 14:20:01 +0000728const md_info_t sha256_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000729 POLARSSL_MD_SHA256,
730 "SHA256",
731 32,
732 sha256_starts_wrap,
733 sha256_update_wrap,
734 sha256_finish_wrap,
735 sha256_wrap,
736 sha256_file_wrap,
737 sha256_hmac_starts_wrap,
738 sha256_hmac_update_wrap,
739 sha256_hmac_finish_wrap,
740 sha256_hmac_reset_wrap,
741 sha256_hmac_wrap,
742 sha256_ctx_alloc,
743 sha256_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100744 sha256_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000745};
746
Paul Bakker9af723c2014-05-01 13:03:14 +0200747#endif /* POLARSSL_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000748
Paul Bakker9e36f042013-06-30 14:34:05 +0200749#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000750
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100751static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000752{
Paul Bakker9e36f042013-06-30 14:34:05 +0200753 sha512_starts( (sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000754}
755
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200756static void sha384_update_wrap( void *ctx, const unsigned char *input,
757 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000758{
Paul Bakker9e36f042013-06-30 14:34:05 +0200759 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000760}
761
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100762static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000763{
Paul Bakker9e36f042013-06-30 14:34:05 +0200764 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000765}
766
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100767static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000768 unsigned char *output )
769{
Paul Bakker9e36f042013-06-30 14:34:05 +0200770 sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000771}
772
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100773static int sha384_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000774{
Paul Bakker335db3f2011-04-25 15:28:35 +0000775#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200776 return sha512_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000777#else
778 ((void) path);
779 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200780 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000781#endif
Paul Bakker17373852011-01-06 14:20:01 +0000782}
783
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200784static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key,
785 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000786{
Paul Bakker9e36f042013-06-30 14:34:05 +0200787 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000788}
789
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200790static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input,
791 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000792{
Paul Bakker9e36f042013-06-30 14:34:05 +0200793 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000794}
795
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100796static void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000797{
Paul Bakker9e36f042013-06-30 14:34:05 +0200798 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000799}
800
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100801static void sha384_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000802{
Paul Bakker9e36f042013-06-30 14:34:05 +0200803 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000804}
805
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100806static void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000807 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000808 unsigned char *output )
809{
Paul Bakker9e36f042013-06-30 14:34:05 +0200810 sha512_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000811}
812
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100813static void * sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000814{
Paul Bakker6e339b52013-07-03 13:37:05 +0200815 return polarssl_malloc( sizeof( sha512_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000816}
817
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100818static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000819{
Paul Bakker34617722014-06-13 17:20:13 +0200820 polarssl_zeroize( ctx, sizeof( sha512_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200821 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000822}
823
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100824static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100825{
Paul Bakker9e36f042013-06-30 14:34:05 +0200826 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100827}
828
Paul Bakker17373852011-01-06 14:20:01 +0000829const md_info_t sha384_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000830 POLARSSL_MD_SHA384,
831 "SHA384",
832 48,
833 sha384_starts_wrap,
834 sha384_update_wrap,
835 sha384_finish_wrap,
836 sha384_wrap,
837 sha384_file_wrap,
838 sha384_hmac_starts_wrap,
839 sha384_hmac_update_wrap,
840 sha384_hmac_finish_wrap,
841 sha384_hmac_reset_wrap,
842 sha384_hmac_wrap,
843 sha384_ctx_alloc,
844 sha384_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100845 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000846};
847
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100848static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000849{
Paul Bakker9e36f042013-06-30 14:34:05 +0200850 sha512_starts( (sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000851}
852
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200853static void sha512_update_wrap( void *ctx, const unsigned char *input,
854 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000855{
Paul Bakker9e36f042013-06-30 14:34:05 +0200856 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000857}
858
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100859static void sha512_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000860{
Paul Bakker9e36f042013-06-30 14:34:05 +0200861 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000862}
863
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100864static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000865 unsigned char *output )
866{
Paul Bakker9e36f042013-06-30 14:34:05 +0200867 sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000868}
869
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100870static int sha512_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000871{
Paul Bakker335db3f2011-04-25 15:28:35 +0000872#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200873 return sha512_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000874#else
875 ((void) path);
876 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200877 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000878#endif
Paul Bakker17373852011-01-06 14:20:01 +0000879}
880
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200881static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key,
882 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000883{
Paul Bakker9e36f042013-06-30 14:34:05 +0200884 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000885}
886
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200887static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input,
888 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000889{
Paul Bakker9e36f042013-06-30 14:34:05 +0200890 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000891}
892
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100893static void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000894{
Paul Bakker9e36f042013-06-30 14:34:05 +0200895 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000896}
897
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100898static void sha512_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000899{
Paul Bakker9e36f042013-06-30 14:34:05 +0200900 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000901}
902
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100903static void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000904 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000905 unsigned char *output )
906{
Paul Bakker9e36f042013-06-30 14:34:05 +0200907 sha512_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000908}
909
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100910static void * sha512_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000911{
Paul Bakker5b4af392014-06-26 12:09:34 +0200912 sha512_context *ctx;
913 ctx = (sha512_context *) polarssl_malloc( sizeof( sha512_context ) );
914
915 if( ctx == NULL )
916 return( NULL );
917
918 sha512_init( ctx );
919
920 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000921}
922
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100923static void sha512_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000924{
Paul Bakker5b4af392014-06-26 12:09:34 +0200925 sha512_free( (sha512_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200926 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000927}
928
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100929static void sha512_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100930{
Paul Bakker9e36f042013-06-30 14:34:05 +0200931 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100932}
933
Paul Bakker17373852011-01-06 14:20:01 +0000934const md_info_t sha512_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000935 POLARSSL_MD_SHA512,
936 "SHA512",
937 64,
938 sha512_starts_wrap,
939 sha512_update_wrap,
940 sha512_finish_wrap,
941 sha512_wrap,
942 sha512_file_wrap,
943 sha512_hmac_starts_wrap,
944 sha512_hmac_update_wrap,
945 sha512_hmac_finish_wrap,
946 sha512_hmac_reset_wrap,
947 sha512_hmac_wrap,
948 sha512_ctx_alloc,
949 sha512_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100950 sha512_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000951};
952
Paul Bakker9af723c2014-05-01 13:03:14 +0200953#endif /* POLARSSL_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000954
Paul Bakker9af723c2014-05-01 13:03:14 +0200955#endif /* POLARSSL_MD_C */