blob: 62110ce7655369c605559a22b5a03e9f94ca3c23 [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
2 * \file md_wrap.c
3
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic message digest wrapper for mbed TLS
Paul Bakker17373852011-01-06 14:20:01 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00008 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker17373852011-01-06 14:20:01 +00009 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +000010 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker17373852011-01-06 14:20:01 +000011 *
Paul Bakker17373852011-01-06 14:20:01 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker17373852011-01-06 14:20:01 +000028#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker17373852011-01-06 14:20:01 +000032
33#if defined(POLARSSL_MD_C)
34
35#include "polarssl/md_wrap.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000036
37#if defined(POLARSSL_MD2_C)
Paul Bakker17373852011-01-06 14:20:01 +000038#include "polarssl/md2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000039#endif
40
41#if defined(POLARSSL_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +000042#include "polarssl/md4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000043#endif
44
45#if defined(POLARSSL_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +000046#include "polarssl/md5.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000047#endif
48
Paul Bakker61b699e2014-01-22 13:35:29 +010049#if defined(POLARSSL_RIPEMD160_C)
50#include "polarssl/ripemd160.h"
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +010051#endif
52
Paul Bakkerf6543712012-03-05 14:01:29 +000053#if defined(POLARSSL_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +000054#include "polarssl/sha1.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000055#endif
56
Paul Bakker9e36f042013-06-30 14:34:05 +020057#if defined(POLARSSL_SHA256_C)
Paul Bakkerd2681d82013-06-30 14:49:12 +020058#include "polarssl/sha256.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000059#endif
60
Paul Bakker9e36f042013-06-30 14:34:05 +020061#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2681d82013-06-30 14:49:12 +020062#include "polarssl/sha512.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000063#endif
Paul Bakker17373852011-01-06 14:20:01 +000064
Paul Bakker7dc4c442014-02-01 22:50:26 +010065#if defined(POLARSSL_PLATFORM_C)
66#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020067#else
68#define polarssl_malloc malloc
69#define polarssl_free free
70#endif
71
Paul Bakker17373852011-01-06 14:20:01 +000072#include <stdlib.h>
73
Paul Bakker34617722014-06-13 17:20:13 +020074/* Implementation that should never be optimized out by the compiler */
75static void polarssl_zeroize( void *v, size_t n ) {
76 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
77}
78
Paul Bakker17373852011-01-06 14:20:01 +000079#if defined(POLARSSL_MD2_C)
80
81static void md2_starts_wrap( void *ctx )
82{
83 md2_starts( (md2_context *) ctx );
84}
85
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020086static void md2_update_wrap( void *ctx, const unsigned char *input,
87 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000088{
89 md2_update( (md2_context *) ctx, input, ilen );
90}
91
92static void md2_finish_wrap( void *ctx, unsigned char *output )
93{
94 md2_finish( (md2_context *) ctx, output );
95}
96
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020097static int md2_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +000098{
99#if defined(POLARSSL_FS_IO)
100 return md2_file( path, output );
101#else
102 ((void) path);
103 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200104 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000105#endif
106}
107
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200108static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key,
109 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000110{
111 md2_hmac_starts( (md2_context *) ctx, key, keylen );
112}
113
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200114static void md2_hmac_update_wrap( void *ctx, const unsigned char *input,
115 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000116{
117 md2_hmac_update( (md2_context *) ctx, input, ilen );
118}
119
120static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
121{
122 md2_hmac_finish( (md2_context *) ctx, output );
123}
124
125static void md2_hmac_reset_wrap( void *ctx )
126{
127 md2_hmac_reset( (md2_context *) ctx );
128}
129
130static void * md2_ctx_alloc( void )
131{
Paul Bakker6e339b52013-07-03 13:37:05 +0200132 return polarssl_malloc( sizeof( md2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000133}
134
135static void md2_ctx_free( void *ctx )
136{
Paul Bakker34617722014-06-13 17:20:13 +0200137 polarssl_zeroize( ctx, sizeof( md2_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200138 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000139}
140
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100141static void md2_process_wrap( void *ctx, const unsigned char *data )
142{
143 ((void) data);
144
145 md2_process( (md2_context *) ctx );
146}
147
Paul Bakker17373852011-01-06 14:20:01 +0000148const md_info_t md2_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000149 POLARSSL_MD_MD2,
150 "MD2",
151 16,
152 md2_starts_wrap,
153 md2_update_wrap,
154 md2_finish_wrap,
155 md2,
Paul Bakker335db3f2011-04-25 15:28:35 +0000156 md2_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000157 md2_hmac_starts_wrap,
158 md2_hmac_update_wrap,
159 md2_hmac_finish_wrap,
160 md2_hmac_reset_wrap,
161 md2_hmac,
162 md2_ctx_alloc,
163 md2_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100164 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000165};
166
Paul Bakker9af723c2014-05-01 13:03:14 +0200167#endif /* POLARSSL_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000168
169#if defined(POLARSSL_MD4_C)
170
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100171static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000172{
173 md4_starts( (md4_context *) ctx );
174}
175
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200176static void md4_update_wrap( void *ctx, const unsigned char *input,
177 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000178{
179 md4_update( (md4_context *) ctx, input, ilen );
180}
181
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100182static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000183{
184 md4_finish( (md4_context *) ctx, output );
185}
186
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100187static int md4_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000188{
189#if defined(POLARSSL_FS_IO)
190 return md4_file( path, output );
191#else
192 ((void) path);
193 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200194 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000195#endif
196}
197
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200198static void md4_hmac_starts_wrap( void *ctx, const unsigned char *key,
199 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000200{
201 md4_hmac_starts( (md4_context *) ctx, key, keylen );
202}
203
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200204static void md4_hmac_update_wrap( void *ctx, const unsigned char *input,
205 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000206{
207 md4_hmac_update( (md4_context *) ctx, input, ilen );
208}
209
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100210static void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000211{
212 md4_hmac_finish( (md4_context *) ctx, output );
213}
214
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100215static void md4_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000216{
217 md4_hmac_reset( (md4_context *) ctx );
218}
219
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100220static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000221{
Paul Bakker6e339b52013-07-03 13:37:05 +0200222 return polarssl_malloc( sizeof( md4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000223}
224
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100225static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000226{
Paul Bakker34617722014-06-13 17:20:13 +0200227 polarssl_zeroize( ctx, sizeof( md4_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200228 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000229}
230
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100231static void md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100232{
233 md4_process( (md4_context *) ctx, data );
234}
235
Paul Bakker17373852011-01-06 14:20:01 +0000236const md_info_t md4_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000237 POLARSSL_MD_MD4,
238 "MD4",
239 16,
240 md4_starts_wrap,
241 md4_update_wrap,
242 md4_finish_wrap,
243 md4,
Paul Bakker335db3f2011-04-25 15:28:35 +0000244 md4_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000245 md4_hmac_starts_wrap,
246 md4_hmac_update_wrap,
247 md4_hmac_finish_wrap,
248 md4_hmac_reset_wrap,
249 md4_hmac,
250 md4_ctx_alloc,
251 md4_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100252 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000253};
254
Paul Bakker9af723c2014-05-01 13:03:14 +0200255#endif /* POLARSSL_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000256
257#if defined(POLARSSL_MD5_C)
258
259static void md5_starts_wrap( void *ctx )
260{
261 md5_starts( (md5_context *) ctx );
262}
263
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200264static void md5_update_wrap( void *ctx, const unsigned char *input,
265 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000266{
267 md5_update( (md5_context *) ctx, input, ilen );
268}
269
270static void md5_finish_wrap( void *ctx, unsigned char *output )
271{
272 md5_finish( (md5_context *) ctx, output );
273}
274
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200275static int md5_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000276{
277#if defined(POLARSSL_FS_IO)
278 return md5_file( path, output );
279#else
280 ((void) path);
281 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200282 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000283#endif
284}
285
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200286static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key,
287 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000288{
289 md5_hmac_starts( (md5_context *) ctx, key, keylen );
290}
291
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200292static void md5_hmac_update_wrap( void *ctx, const unsigned char *input,
293 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000294{
295 md5_hmac_update( (md5_context *) ctx, input, ilen );
296}
297
298static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
299{
300 md5_hmac_finish( (md5_context *) ctx, output );
301}
302
303static void md5_hmac_reset_wrap( void *ctx )
304{
305 md5_hmac_reset( (md5_context *) ctx );
306}
307
308static void * md5_ctx_alloc( void )
309{
Paul Bakker6e339b52013-07-03 13:37:05 +0200310 return polarssl_malloc( sizeof( md5_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000311}
312
313static void md5_ctx_free( void *ctx )
314{
Paul Bakker34617722014-06-13 17:20:13 +0200315 polarssl_zeroize( ctx, sizeof( md5_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200316 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000317}
318
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100319static void md5_process_wrap( void *ctx, const unsigned char *data )
320{
321 md5_process( (md5_context *) ctx, data );
322}
323
Paul Bakker17373852011-01-06 14:20:01 +0000324const md_info_t md5_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000325 POLARSSL_MD_MD5,
326 "MD5",
327 16,
328 md5_starts_wrap,
329 md5_update_wrap,
330 md5_finish_wrap,
331 md5,
Paul Bakker335db3f2011-04-25 15:28:35 +0000332 md5_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000333 md5_hmac_starts_wrap,
334 md5_hmac_update_wrap,
335 md5_hmac_finish_wrap,
336 md5_hmac_reset_wrap,
337 md5_hmac,
338 md5_ctx_alloc,
339 md5_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100340 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000341};
342
Paul Bakker9af723c2014-05-01 13:03:14 +0200343#endif /* POLARSSL_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000344
Paul Bakker61b699e2014-01-22 13:35:29 +0100345#if defined(POLARSSL_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100346
Paul Bakker61b699e2014-01-22 13:35:29 +0100347static void ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100348{
Paul Bakker61b699e2014-01-22 13:35:29 +0100349 ripemd160_starts( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100350}
351
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200352static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
353 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100354{
Paul Bakker61b699e2014-01-22 13:35:29 +0100355 ripemd160_update( (ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100356}
357
Paul Bakker61b699e2014-01-22 13:35:29 +0100358static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100359{
Paul Bakker61b699e2014-01-22 13:35:29 +0100360 ripemd160_finish( (ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100361}
362
Paul Bakker61b699e2014-01-22 13:35:29 +0100363static int ripemd160_file_wrap( const char *path, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100364{
365#if defined(POLARSSL_FS_IO)
Paul Bakker61b699e2014-01-22 13:35:29 +0100366 return ripemd160_file( path, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100367#else
368 ((void) path);
369 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200370 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100371#endif
372}
373
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200374static void ripemd160_hmac_starts_wrap( void *ctx, const unsigned char *key,
375 size_t keylen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100376{
Paul Bakker61b699e2014-01-22 13:35:29 +0100377 ripemd160_hmac_starts( (ripemd160_context *) ctx, key, keylen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100378}
379
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200380static void ripemd160_hmac_update_wrap( void *ctx, const unsigned char *input,
381 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100382{
Paul Bakker61b699e2014-01-22 13:35:29 +0100383 ripemd160_hmac_update( (ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100384}
385
Paul Bakker61b699e2014-01-22 13:35:29 +0100386static void ripemd160_hmac_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100387{
Paul Bakker61b699e2014-01-22 13:35:29 +0100388 ripemd160_hmac_finish( (ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100389}
390
Paul Bakker61b699e2014-01-22 13:35:29 +0100391static void ripemd160_hmac_reset_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100392{
Paul Bakker61b699e2014-01-22 13:35:29 +0100393 ripemd160_hmac_reset( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100394}
395
Paul Bakker61b699e2014-01-22 13:35:29 +0100396static void * ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100397{
Paul Bakker5b4af392014-06-26 12:09:34 +0200398 ripemd160_context *ctx;
399 ctx = (ripemd160_context *) polarssl_malloc( sizeof( ripemd160_context ) );
400
401 if( ctx == NULL )
402 return( NULL );
403
404 ripemd160_init( ctx );
405
406 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100407}
408
Paul Bakker61b699e2014-01-22 13:35:29 +0100409static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100410{
Paul Bakker5b4af392014-06-26 12:09:34 +0200411 ripemd160_free( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100412 polarssl_free( ctx );
413}
414
Paul Bakker61b699e2014-01-22 13:35:29 +0100415static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100416{
Paul Bakker61b699e2014-01-22 13:35:29 +0100417 ripemd160_process( (ripemd160_context *) ctx, data );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100418}
419
Paul Bakker61b699e2014-01-22 13:35:29 +0100420const md_info_t ripemd160_info = {
421 POLARSSL_MD_RIPEMD160,
422 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100423 20,
Paul Bakker61b699e2014-01-22 13:35:29 +0100424 ripemd160_starts_wrap,
425 ripemd160_update_wrap,
426 ripemd160_finish_wrap,
427 ripemd160,
428 ripemd160_file_wrap,
429 ripemd160_hmac_starts_wrap,
430 ripemd160_hmac_update_wrap,
431 ripemd160_hmac_finish_wrap,
432 ripemd160_hmac_reset_wrap,
433 ripemd160_hmac,
434 ripemd160_ctx_alloc,
435 ripemd160_ctx_free,
436 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100437};
438
Paul Bakker9af723c2014-05-01 13:03:14 +0200439#endif /* POLARSSL_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100440
Paul Bakker17373852011-01-06 14:20:01 +0000441#if defined(POLARSSL_SHA1_C)
442
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100443static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000444{
445 sha1_starts( (sha1_context *) ctx );
446}
447
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200448static void sha1_update_wrap( void *ctx, const unsigned char *input,
449 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000450{
451 sha1_update( (sha1_context *) ctx, input, ilen );
452}
453
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100454static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000455{
456 sha1_finish( (sha1_context *) ctx, output );
457}
458
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100459static int sha1_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000460{
461#if defined(POLARSSL_FS_IO)
462 return sha1_file( path, output );
463#else
464 ((void) path);
465 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200466 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000467#endif
468}
469
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200470static void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key,
471 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000472{
473 sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
474}
475
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200476static void sha1_hmac_update_wrap( void *ctx, const unsigned char *input,
477 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000478{
479 sha1_hmac_update( (sha1_context *) ctx, input, ilen );
480}
481
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100482static void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000483{
484 sha1_hmac_finish( (sha1_context *) ctx, output );
485}
486
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100487static void sha1_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000488{
489 sha1_hmac_reset( (sha1_context *) ctx );
490}
491
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100492static void * sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000493{
Paul Bakker5b4af392014-06-26 12:09:34 +0200494 sha1_context *ctx;
495 ctx = (sha1_context *) polarssl_malloc( sizeof( sha1_context ) );
496
497 if( ctx == NULL )
498 return( NULL );
499
500 sha1_init( ctx );
501
502 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000503}
504
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100505static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000506{
Paul Bakker5b4af392014-06-26 12:09:34 +0200507 sha1_free( (sha1_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200508 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000509}
510
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100511static void sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100512{
513 sha1_process( (sha1_context *) ctx, data );
514}
515
Paul Bakker17373852011-01-06 14:20:01 +0000516const md_info_t sha1_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000517 POLARSSL_MD_SHA1,
518 "SHA1",
519 20,
520 sha1_starts_wrap,
521 sha1_update_wrap,
522 sha1_finish_wrap,
523 sha1,
Paul Bakker335db3f2011-04-25 15:28:35 +0000524 sha1_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000525 sha1_hmac_starts_wrap,
526 sha1_hmac_update_wrap,
527 sha1_hmac_finish_wrap,
528 sha1_hmac_reset_wrap,
529 sha1_hmac,
530 sha1_ctx_alloc,
531 sha1_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100532 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000533};
534
Paul Bakker9af723c2014-05-01 13:03:14 +0200535#endif /* POLARSSL_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000536
537/*
538 * Wrappers for generic message digests
539 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200540#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000541
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100542static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000543{
Paul Bakker9e36f042013-06-30 14:34:05 +0200544 sha256_starts( (sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000545}
546
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200547static void sha224_update_wrap( void *ctx, const unsigned char *input,
548 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000549{
Paul Bakker9e36f042013-06-30 14:34:05 +0200550 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000551}
552
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100553static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000554{
Paul Bakker9e36f042013-06-30 14:34:05 +0200555 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000556}
557
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100558static void sha224_wrap( 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( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000562}
563
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100564static int sha224_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000565{
Paul Bakker335db3f2011-04-25 15:28:35 +0000566#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200567 return sha256_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000568#else
569 ((void) path);
570 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200571 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000572#endif
Paul Bakker17373852011-01-06 14:20:01 +0000573}
574
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200575static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key,
576 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000577{
Paul Bakker9e36f042013-06-30 14:34:05 +0200578 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000579}
580
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200581static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input,
582 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000583{
Paul Bakker9e36f042013-06-30 14:34:05 +0200584 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000585}
586
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100587static void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000588{
Paul Bakker9e36f042013-06-30 14:34:05 +0200589 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000590}
591
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100592static void sha224_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000593{
Paul Bakker9e36f042013-06-30 14:34:05 +0200594 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000595}
596
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100597static void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000598 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000599 unsigned char *output )
600{
Paul Bakker9e36f042013-06-30 14:34:05 +0200601 sha256_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000602}
603
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100604static void * sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000605{
Paul Bakker6e339b52013-07-03 13:37:05 +0200606 return polarssl_malloc( sizeof( sha256_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000607}
608
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100609static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000610{
Paul Bakker34617722014-06-13 17:20:13 +0200611 polarssl_zeroize( ctx, sizeof( sha256_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200612 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000613}
614
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100615static void sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100616{
Paul Bakker9e36f042013-06-30 14:34:05 +0200617 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100618}
619
Paul Bakker17373852011-01-06 14:20:01 +0000620const md_info_t sha224_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000621 POLARSSL_MD_SHA224,
622 "SHA224",
623 28,
624 sha224_starts_wrap,
625 sha224_update_wrap,
626 sha224_finish_wrap,
627 sha224_wrap,
628 sha224_file_wrap,
629 sha224_hmac_starts_wrap,
630 sha224_hmac_update_wrap,
631 sha224_hmac_finish_wrap,
632 sha224_hmac_reset_wrap,
633 sha224_hmac_wrap,
634 sha224_ctx_alloc,
635 sha224_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100636 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000637};
638
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100639static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000640{
Paul Bakker9e36f042013-06-30 14:34:05 +0200641 sha256_starts( (sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000642}
643
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200644static void sha256_update_wrap( void *ctx, const unsigned char *input,
645 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000646{
Paul Bakker9e36f042013-06-30 14:34:05 +0200647 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000648}
649
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100650static void sha256_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000651{
Paul Bakker9e36f042013-06-30 14:34:05 +0200652 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000653}
654
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100655static void sha256_wrap( 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 sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000659}
660
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100661static int sha256_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000662{
Paul Bakker335db3f2011-04-25 15:28:35 +0000663#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200664 return sha256_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000665#else
666 ((void) path);
667 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200668 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000669#endif
Paul Bakker17373852011-01-06 14:20:01 +0000670}
671
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200672static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key,
673 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000674{
Paul Bakker9e36f042013-06-30 14:34:05 +0200675 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000676}
677
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200678static void sha256_hmac_update_wrap( void *ctx, const unsigned char *input,
679 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000680{
Paul Bakker9e36f042013-06-30 14:34:05 +0200681 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000682}
683
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100684static void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000685{
Paul Bakker9e36f042013-06-30 14:34:05 +0200686 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000687}
688
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100689static void sha256_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000690{
Paul Bakker9e36f042013-06-30 14:34:05 +0200691 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000692}
693
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100694static void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000695 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000696 unsigned char *output )
697{
Paul Bakker9e36f042013-06-30 14:34:05 +0200698 sha256_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000699}
700
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100701static void * sha256_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000702{
Paul Bakker5b4af392014-06-26 12:09:34 +0200703 sha256_context *ctx;
704 ctx = (sha256_context *) polarssl_malloc( sizeof( sha256_context ) );
705
706 if( ctx == NULL )
707 return( NULL );
708
709 sha256_init( ctx );
710
711 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000712}
713
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100714static void sha256_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000715{
Paul Bakker5b4af392014-06-26 12:09:34 +0200716 sha256_free( (sha256_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200717 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000718}
719
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100720static void sha256_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100721{
Paul Bakker9e36f042013-06-30 14:34:05 +0200722 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100723}
724
Paul Bakker17373852011-01-06 14:20:01 +0000725const md_info_t sha256_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000726 POLARSSL_MD_SHA256,
727 "SHA256",
728 32,
729 sha256_starts_wrap,
730 sha256_update_wrap,
731 sha256_finish_wrap,
732 sha256_wrap,
733 sha256_file_wrap,
734 sha256_hmac_starts_wrap,
735 sha256_hmac_update_wrap,
736 sha256_hmac_finish_wrap,
737 sha256_hmac_reset_wrap,
738 sha256_hmac_wrap,
739 sha256_ctx_alloc,
740 sha256_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100741 sha256_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000742};
743
Paul Bakker9af723c2014-05-01 13:03:14 +0200744#endif /* POLARSSL_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000745
Paul Bakker9e36f042013-06-30 14:34:05 +0200746#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000747
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100748static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000749{
Paul Bakker9e36f042013-06-30 14:34:05 +0200750 sha512_starts( (sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000751}
752
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200753static void sha384_update_wrap( void *ctx, const unsigned char *input,
754 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000755{
Paul Bakker9e36f042013-06-30 14:34:05 +0200756 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000757}
758
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100759static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000760{
Paul Bakker9e36f042013-06-30 14:34:05 +0200761 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000762}
763
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100764static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000765 unsigned char *output )
766{
Paul Bakker9e36f042013-06-30 14:34:05 +0200767 sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000768}
769
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100770static int sha384_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000771{
Paul Bakker335db3f2011-04-25 15:28:35 +0000772#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200773 return sha512_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000774#else
775 ((void) path);
776 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200777 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000778#endif
Paul Bakker17373852011-01-06 14:20:01 +0000779}
780
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200781static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key,
782 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000783{
Paul Bakker9e36f042013-06-30 14:34:05 +0200784 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000785}
786
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200787static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input,
788 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000789{
Paul Bakker9e36f042013-06-30 14:34:05 +0200790 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000791}
792
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100793static void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000794{
Paul Bakker9e36f042013-06-30 14:34:05 +0200795 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000796}
797
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100798static void sha384_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000799{
Paul Bakker9e36f042013-06-30 14:34:05 +0200800 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000801}
802
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100803static void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000804 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000805 unsigned char *output )
806{
Paul Bakker9e36f042013-06-30 14:34:05 +0200807 sha512_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000808}
809
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100810static void * sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000811{
Paul Bakker6e339b52013-07-03 13:37:05 +0200812 return polarssl_malloc( sizeof( sha512_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000813}
814
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100815static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000816{
Paul Bakker34617722014-06-13 17:20:13 +0200817 polarssl_zeroize( ctx, sizeof( sha512_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200818 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000819}
820
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100821static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100822{
Paul Bakker9e36f042013-06-30 14:34:05 +0200823 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100824}
825
Paul Bakker17373852011-01-06 14:20:01 +0000826const md_info_t sha384_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000827 POLARSSL_MD_SHA384,
828 "SHA384",
829 48,
830 sha384_starts_wrap,
831 sha384_update_wrap,
832 sha384_finish_wrap,
833 sha384_wrap,
834 sha384_file_wrap,
835 sha384_hmac_starts_wrap,
836 sha384_hmac_update_wrap,
837 sha384_hmac_finish_wrap,
838 sha384_hmac_reset_wrap,
839 sha384_hmac_wrap,
840 sha384_ctx_alloc,
841 sha384_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100842 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000843};
844
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100845static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000846{
Paul Bakker9e36f042013-06-30 14:34:05 +0200847 sha512_starts( (sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000848}
849
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200850static void sha512_update_wrap( void *ctx, const unsigned char *input,
851 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000852{
Paul Bakker9e36f042013-06-30 14:34:05 +0200853 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000854}
855
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100856static void sha512_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000857{
Paul Bakker9e36f042013-06-30 14:34:05 +0200858 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000859}
860
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100861static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000862 unsigned char *output )
863{
Paul Bakker9e36f042013-06-30 14:34:05 +0200864 sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000865}
866
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100867static int sha512_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000868{
Paul Bakker335db3f2011-04-25 15:28:35 +0000869#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200870 return sha512_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000871#else
872 ((void) path);
873 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200874 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000875#endif
Paul Bakker17373852011-01-06 14:20:01 +0000876}
877
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200878static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key,
879 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000880{
Paul Bakker9e36f042013-06-30 14:34:05 +0200881 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000882}
883
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200884static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input,
885 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000886{
Paul Bakker9e36f042013-06-30 14:34:05 +0200887 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000888}
889
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100890static void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000891{
Paul Bakker9e36f042013-06-30 14:34:05 +0200892 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000893}
894
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100895static void sha512_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000896{
Paul Bakker9e36f042013-06-30 14:34:05 +0200897 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000898}
899
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100900static void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000901 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000902 unsigned char *output )
903{
Paul Bakker9e36f042013-06-30 14:34:05 +0200904 sha512_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000905}
906
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100907static void * sha512_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000908{
Paul Bakker5b4af392014-06-26 12:09:34 +0200909 sha512_context *ctx;
910 ctx = (sha512_context *) polarssl_malloc( sizeof( sha512_context ) );
911
912 if( ctx == NULL )
913 return( NULL );
914
915 sha512_init( ctx );
916
917 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000918}
919
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100920static void sha512_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000921{
Paul Bakker5b4af392014-06-26 12:09:34 +0200922 sha512_free( (sha512_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200923 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000924}
925
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100926static void sha512_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100927{
Paul Bakker9e36f042013-06-30 14:34:05 +0200928 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100929}
930
Paul Bakker17373852011-01-06 14:20:01 +0000931const md_info_t sha512_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000932 POLARSSL_MD_SHA512,
933 "SHA512",
934 64,
935 sha512_starts_wrap,
936 sha512_update_wrap,
937 sha512_finish_wrap,
938 sha512_wrap,
939 sha512_file_wrap,
940 sha512_hmac_starts_wrap,
941 sha512_hmac_update_wrap,
942 sha512_hmac_finish_wrap,
943 sha512_hmac_reset_wrap,
944 sha512_hmac_wrap,
945 sha512_ctx_alloc,
946 sha512_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100947 sha512_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000948};
949
Paul Bakker9af723c2014-05-01 13:03:14 +0200950#endif /* POLARSSL_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000951
Paul Bakker9af723c2014-05-01 13:03:14 +0200952#endif /* POLARSSL_MD_C */