blob: 603a4ac37de068a2558771936dd5242ddb30ee24 [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é-Gonnardfe446432015-03-06 13:17:10 +000010 * This file is part of mbed TLS (https://tls.mbed.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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/md_wrap.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000036
37#if defined(POLARSSL_MD2_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/md2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000039#endif
40
41#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/md4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000043#endif
44
45#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020067#else
Rich Evans00ab4702015-02-06 13:43:58 +000068#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020069#define polarssl_malloc malloc
70#define polarssl_free free
71#endif
72
Paul Bakker34617722014-06-13 17:20:13 +020073/* Implementation that should never be optimized out by the compiler */
74static void polarssl_zeroize( void *v, size_t n ) {
75 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
76}
77
Paul Bakker17373852011-01-06 14:20:01 +000078#if defined(POLARSSL_MD2_C)
79
80static void md2_starts_wrap( void *ctx )
81{
82 md2_starts( (md2_context *) ctx );
83}
84
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020085static void md2_update_wrap( void *ctx, const unsigned char *input,
86 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000087{
88 md2_update( (md2_context *) ctx, input, ilen );
89}
90
91static void md2_finish_wrap( void *ctx, unsigned char *output )
92{
93 md2_finish( (md2_context *) ctx, output );
94}
95
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020096static int md2_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +000097{
98#if defined(POLARSSL_FS_IO)
99 return md2_file( path, output );
100#else
101 ((void) path);
102 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200103 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000104#endif
105}
106
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200107static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key,
108 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000109{
110 md2_hmac_starts( (md2_context *) ctx, key, keylen );
111}
112
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200113static void md2_hmac_update_wrap( void *ctx, const unsigned char *input,
114 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000115{
116 md2_hmac_update( (md2_context *) ctx, input, ilen );
117}
118
119static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
120{
121 md2_hmac_finish( (md2_context *) ctx, output );
122}
123
124static void md2_hmac_reset_wrap( void *ctx )
125{
126 md2_hmac_reset( (md2_context *) ctx );
127}
128
129static void * md2_ctx_alloc( void )
130{
Paul Bakker6e339b52013-07-03 13:37:05 +0200131 return polarssl_malloc( sizeof( md2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000132}
133
134static void md2_ctx_free( void *ctx )
135{
Paul Bakker34617722014-06-13 17:20:13 +0200136 polarssl_zeroize( ctx, sizeof( md2_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200137 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000138}
139
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100140static void md2_process_wrap( void *ctx, const unsigned char *data )
141{
142 ((void) data);
143
144 md2_process( (md2_context *) ctx );
145}
146
Paul Bakker17373852011-01-06 14:20:01 +0000147const md_info_t md2_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000148 POLARSSL_MD_MD2,
149 "MD2",
150 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100151 16,
Paul Bakker23986e52011-04-24 08:57:21 +0000152 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,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100240 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000241 md4_starts_wrap,
242 md4_update_wrap,
243 md4_finish_wrap,
244 md4,
Paul Bakker335db3f2011-04-25 15:28:35 +0000245 md4_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000246 md4_hmac_starts_wrap,
247 md4_hmac_update_wrap,
248 md4_hmac_finish_wrap,
249 md4_hmac_reset_wrap,
250 md4_hmac,
251 md4_ctx_alloc,
252 md4_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100253 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000254};
255
Paul Bakker9af723c2014-05-01 13:03:14 +0200256#endif /* POLARSSL_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000257
258#if defined(POLARSSL_MD5_C)
259
260static void md5_starts_wrap( void *ctx )
261{
262 md5_starts( (md5_context *) ctx );
263}
264
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200265static void md5_update_wrap( void *ctx, const unsigned char *input,
266 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000267{
268 md5_update( (md5_context *) ctx, input, ilen );
269}
270
271static void md5_finish_wrap( void *ctx, unsigned char *output )
272{
273 md5_finish( (md5_context *) ctx, output );
274}
275
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200276static int md5_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000277{
278#if defined(POLARSSL_FS_IO)
279 return md5_file( path, output );
280#else
281 ((void) path);
282 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200283 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000284#endif
285}
286
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200287static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key,
288 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000289{
290 md5_hmac_starts( (md5_context *) ctx, key, keylen );
291}
292
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200293static void md5_hmac_update_wrap( void *ctx, const unsigned char *input,
294 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000295{
296 md5_hmac_update( (md5_context *) ctx, input, ilen );
297}
298
299static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
300{
301 md5_hmac_finish( (md5_context *) ctx, output );
302}
303
304static void md5_hmac_reset_wrap( void *ctx )
305{
306 md5_hmac_reset( (md5_context *) ctx );
307}
308
309static void * md5_ctx_alloc( void )
310{
Paul Bakker6e339b52013-07-03 13:37:05 +0200311 return polarssl_malloc( sizeof( md5_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000312}
313
314static void md5_ctx_free( void *ctx )
315{
Paul Bakker34617722014-06-13 17:20:13 +0200316 polarssl_zeroize( ctx, sizeof( md5_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200317 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000318}
319
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100320static void md5_process_wrap( void *ctx, const unsigned char *data )
321{
322 md5_process( (md5_context *) ctx, data );
323}
324
Paul Bakker17373852011-01-06 14:20:01 +0000325const md_info_t md5_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000326 POLARSSL_MD_MD5,
327 "MD5",
328 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100329 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000330 md5_starts_wrap,
331 md5_update_wrap,
332 md5_finish_wrap,
333 md5,
Paul Bakker335db3f2011-04-25 15:28:35 +0000334 md5_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000335 md5_hmac_starts_wrap,
336 md5_hmac_update_wrap,
337 md5_hmac_finish_wrap,
338 md5_hmac_reset_wrap,
339 md5_hmac,
340 md5_ctx_alloc,
341 md5_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100342 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000343};
344
Paul Bakker9af723c2014-05-01 13:03:14 +0200345#endif /* POLARSSL_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000346
Paul Bakker61b699e2014-01-22 13:35:29 +0100347#if defined(POLARSSL_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100348
Paul Bakker61b699e2014-01-22 13:35:29 +0100349static void ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100350{
Paul Bakker61b699e2014-01-22 13:35:29 +0100351 ripemd160_starts( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100352}
353
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200354static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
355 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100356{
Paul Bakker61b699e2014-01-22 13:35:29 +0100357 ripemd160_update( (ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100358}
359
Paul Bakker61b699e2014-01-22 13:35:29 +0100360static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100361{
Paul Bakker61b699e2014-01-22 13:35:29 +0100362 ripemd160_finish( (ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100363}
364
Paul Bakker61b699e2014-01-22 13:35:29 +0100365static int ripemd160_file_wrap( const char *path, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100366{
367#if defined(POLARSSL_FS_IO)
Paul Bakker61b699e2014-01-22 13:35:29 +0100368 return ripemd160_file( path, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100369#else
370 ((void) path);
371 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200372 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100373#endif
374}
375
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200376static void ripemd160_hmac_starts_wrap( void *ctx, const unsigned char *key,
377 size_t keylen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100378{
Paul Bakker61b699e2014-01-22 13:35:29 +0100379 ripemd160_hmac_starts( (ripemd160_context *) ctx, key, keylen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100380}
381
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200382static void ripemd160_hmac_update_wrap( void *ctx, const unsigned char *input,
383 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100384{
Paul Bakker61b699e2014-01-22 13:35:29 +0100385 ripemd160_hmac_update( (ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100386}
387
Paul Bakker61b699e2014-01-22 13:35:29 +0100388static void ripemd160_hmac_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100389{
Paul Bakker61b699e2014-01-22 13:35:29 +0100390 ripemd160_hmac_finish( (ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100391}
392
Paul Bakker61b699e2014-01-22 13:35:29 +0100393static void ripemd160_hmac_reset_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100394{
Paul Bakker61b699e2014-01-22 13:35:29 +0100395 ripemd160_hmac_reset( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100396}
397
Paul Bakker61b699e2014-01-22 13:35:29 +0100398static void * ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100399{
Paul Bakker5b4af392014-06-26 12:09:34 +0200400 ripemd160_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500401 ctx = polarssl_malloc( sizeof( ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200402
403 if( ctx == NULL )
404 return( NULL );
405
406 ripemd160_init( ctx );
407
408 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100409}
410
Paul Bakker61b699e2014-01-22 13:35:29 +0100411static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100412{
Paul Bakker5b4af392014-06-26 12:09:34 +0200413 ripemd160_free( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100414 polarssl_free( ctx );
415}
416
Paul Bakker61b699e2014-01-22 13:35:29 +0100417static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100418{
Paul Bakker61b699e2014-01-22 13:35:29 +0100419 ripemd160_process( (ripemd160_context *) ctx, data );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100420}
421
Paul Bakker61b699e2014-01-22 13:35:29 +0100422const md_info_t ripemd160_info = {
423 POLARSSL_MD_RIPEMD160,
424 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100425 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100426 64,
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;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500498 ctx = polarssl_malloc( sizeof( sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200499
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,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100523 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000524 sha1_starts_wrap,
525 sha1_update_wrap,
526 sha1_finish_wrap,
527 sha1,
Paul Bakker335db3f2011-04-25 15:28:35 +0000528 sha1_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000529 sha1_hmac_starts_wrap,
530 sha1_hmac_update_wrap,
531 sha1_hmac_finish_wrap,
532 sha1_hmac_reset_wrap,
533 sha1_hmac,
534 sha1_ctx_alloc,
535 sha1_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100536 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000537};
538
Paul Bakker9af723c2014-05-01 13:03:14 +0200539#endif /* POLARSSL_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000540
541/*
542 * Wrappers for generic message digests
543 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200544#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000545
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100546static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000547{
Paul Bakker9e36f042013-06-30 14:34:05 +0200548 sha256_starts( (sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000549}
550
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200551static void sha224_update_wrap( void *ctx, const unsigned char *input,
552 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000553{
Paul Bakker9e36f042013-06-30 14:34:05 +0200554 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000555}
556
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100557static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000558{
Paul Bakker9e36f042013-06-30 14:34:05 +0200559 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000560}
561
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100562static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000563 unsigned char *output )
564{
Paul Bakker9e36f042013-06-30 14:34:05 +0200565 sha256( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000566}
567
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100568static int sha224_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000569{
Paul Bakker335db3f2011-04-25 15:28:35 +0000570#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200571 return sha256_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000572#else
573 ((void) path);
574 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200575 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000576#endif
Paul Bakker17373852011-01-06 14:20:01 +0000577}
578
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200579static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key,
580 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000581{
Paul Bakker9e36f042013-06-30 14:34:05 +0200582 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000583}
584
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200585static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input,
586 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000587{
Paul Bakker9e36f042013-06-30 14:34:05 +0200588 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000589}
590
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100591static void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000592{
Paul Bakker9e36f042013-06-30 14:34:05 +0200593 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000594}
595
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100596static void sha224_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000597{
Paul Bakker9e36f042013-06-30 14:34:05 +0200598 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000599}
600
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100601static void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000602 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000603 unsigned char *output )
604{
Paul Bakker9e36f042013-06-30 14:34:05 +0200605 sha256_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000606}
607
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100608static void * sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000609{
Paul Bakker6e339b52013-07-03 13:37:05 +0200610 return polarssl_malloc( sizeof( sha256_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000611}
612
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100613static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000614{
Paul Bakker34617722014-06-13 17:20:13 +0200615 polarssl_zeroize( ctx, sizeof( sha256_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200616 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000617}
618
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100619static void sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100620{
Paul Bakker9e36f042013-06-30 14:34:05 +0200621 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100622}
623
Paul Bakker17373852011-01-06 14:20:01 +0000624const md_info_t sha224_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000625 POLARSSL_MD_SHA224,
626 "SHA224",
627 28,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100628 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000629 sha224_starts_wrap,
630 sha224_update_wrap,
631 sha224_finish_wrap,
632 sha224_wrap,
633 sha224_file_wrap,
634 sha224_hmac_starts_wrap,
635 sha224_hmac_update_wrap,
636 sha224_hmac_finish_wrap,
637 sha224_hmac_reset_wrap,
638 sha224_hmac_wrap,
639 sha224_ctx_alloc,
640 sha224_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100641 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000642};
643
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100644static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000645{
Paul Bakker9e36f042013-06-30 14:34:05 +0200646 sha256_starts( (sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000647}
648
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200649static void sha256_update_wrap( void *ctx, const unsigned char *input,
650 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000651{
Paul Bakker9e36f042013-06-30 14:34:05 +0200652 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000653}
654
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100655static void sha256_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000656{
Paul Bakker9e36f042013-06-30 14:34:05 +0200657 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000658}
659
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100660static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000661 unsigned char *output )
662{
Paul Bakker9e36f042013-06-30 14:34:05 +0200663 sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000664}
665
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100666static int sha256_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000667{
Paul Bakker335db3f2011-04-25 15:28:35 +0000668#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200669 return sha256_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000670#else
671 ((void) path);
672 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200673 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000674#endif
Paul Bakker17373852011-01-06 14:20:01 +0000675}
676
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200677static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key,
678 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000679{
Paul Bakker9e36f042013-06-30 14:34:05 +0200680 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000681}
682
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200683static void sha256_hmac_update_wrap( void *ctx, const unsigned char *input,
684 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000685{
Paul Bakker9e36f042013-06-30 14:34:05 +0200686 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000687}
688
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100689static void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000690{
Paul Bakker9e36f042013-06-30 14:34:05 +0200691 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000692}
693
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100694static void sha256_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000695{
Paul Bakker9e36f042013-06-30 14:34:05 +0200696 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000697}
698
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100699static void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000700 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000701 unsigned char *output )
702{
Paul Bakker9e36f042013-06-30 14:34:05 +0200703 sha256_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000704}
705
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100706static void * sha256_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000707{
Paul Bakker5b4af392014-06-26 12:09:34 +0200708 sha256_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500709 ctx = polarssl_malloc( sizeof( sha256_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200710
711 if( ctx == NULL )
712 return( NULL );
713
714 sha256_init( ctx );
715
716 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000717}
718
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100719static void sha256_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000720{
Paul Bakker5b4af392014-06-26 12:09:34 +0200721 sha256_free( (sha256_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200722 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000723}
724
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100725static void sha256_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100726{
Paul Bakker9e36f042013-06-30 14:34:05 +0200727 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100728}
729
Paul Bakker17373852011-01-06 14:20:01 +0000730const md_info_t sha256_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000731 POLARSSL_MD_SHA256,
732 "SHA256",
733 32,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100734 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000735 sha256_starts_wrap,
736 sha256_update_wrap,
737 sha256_finish_wrap,
738 sha256_wrap,
739 sha256_file_wrap,
740 sha256_hmac_starts_wrap,
741 sha256_hmac_update_wrap,
742 sha256_hmac_finish_wrap,
743 sha256_hmac_reset_wrap,
744 sha256_hmac_wrap,
745 sha256_ctx_alloc,
746 sha256_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100747 sha256_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000748};
749
Paul Bakker9af723c2014-05-01 13:03:14 +0200750#endif /* POLARSSL_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000751
Paul Bakker9e36f042013-06-30 14:34:05 +0200752#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000753
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100754static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000755{
Paul Bakker9e36f042013-06-30 14:34:05 +0200756 sha512_starts( (sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000757}
758
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200759static void sha384_update_wrap( void *ctx, const unsigned char *input,
760 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000761{
Paul Bakker9e36f042013-06-30 14:34:05 +0200762 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000763}
764
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100765static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000766{
Paul Bakker9e36f042013-06-30 14:34:05 +0200767 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000768}
769
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100770static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000771 unsigned char *output )
772{
Paul Bakker9e36f042013-06-30 14:34:05 +0200773 sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000774}
775
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100776static int sha384_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000777{
Paul Bakker335db3f2011-04-25 15:28:35 +0000778#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200779 return sha512_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000780#else
781 ((void) path);
782 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200783 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000784#endif
Paul Bakker17373852011-01-06 14:20:01 +0000785}
786
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200787static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key,
788 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000789{
Paul Bakker9e36f042013-06-30 14:34:05 +0200790 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000791}
792
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200793static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input,
794 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000795{
Paul Bakker9e36f042013-06-30 14:34:05 +0200796 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000797}
798
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100799static void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000800{
Paul Bakker9e36f042013-06-30 14:34:05 +0200801 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000802}
803
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100804static void sha384_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000805{
Paul Bakker9e36f042013-06-30 14:34:05 +0200806 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000807}
808
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100809static void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000810 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000811 unsigned char *output )
812{
Paul Bakker9e36f042013-06-30 14:34:05 +0200813 sha512_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000814}
815
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100816static void * sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000817{
Paul Bakker6e339b52013-07-03 13:37:05 +0200818 return polarssl_malloc( sizeof( sha512_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000819}
820
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100821static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000822{
Paul Bakker34617722014-06-13 17:20:13 +0200823 polarssl_zeroize( ctx, sizeof( sha512_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200824 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000825}
826
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100827static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100828{
Paul Bakker9e36f042013-06-30 14:34:05 +0200829 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100830}
831
Paul Bakker17373852011-01-06 14:20:01 +0000832const md_info_t sha384_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000833 POLARSSL_MD_SHA384,
834 "SHA384",
835 48,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100836 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000837 sha384_starts_wrap,
838 sha384_update_wrap,
839 sha384_finish_wrap,
840 sha384_wrap,
841 sha384_file_wrap,
842 sha384_hmac_starts_wrap,
843 sha384_hmac_update_wrap,
844 sha384_hmac_finish_wrap,
845 sha384_hmac_reset_wrap,
846 sha384_hmac_wrap,
847 sha384_ctx_alloc,
848 sha384_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100849 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000850};
851
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100852static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000853{
Paul Bakker9e36f042013-06-30 14:34:05 +0200854 sha512_starts( (sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000855}
856
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200857static void sha512_update_wrap( void *ctx, const unsigned char *input,
858 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000859{
Paul Bakker9e36f042013-06-30 14:34:05 +0200860 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000861}
862
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100863static void sha512_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000864{
Paul Bakker9e36f042013-06-30 14:34:05 +0200865 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000866}
867
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100868static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000869 unsigned char *output )
870{
Paul Bakker9e36f042013-06-30 14:34:05 +0200871 sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000872}
873
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100874static int sha512_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000875{
Paul Bakker335db3f2011-04-25 15:28:35 +0000876#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200877 return sha512_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000878#else
879 ((void) path);
880 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200881 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000882#endif
Paul Bakker17373852011-01-06 14:20:01 +0000883}
884
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200885static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key,
886 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000887{
Paul Bakker9e36f042013-06-30 14:34:05 +0200888 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000889}
890
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200891static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input,
892 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000893{
Paul Bakker9e36f042013-06-30 14:34:05 +0200894 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000895}
896
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100897static void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000898{
Paul Bakker9e36f042013-06-30 14:34:05 +0200899 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000900}
901
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100902static void sha512_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000903{
Paul Bakker9e36f042013-06-30 14:34:05 +0200904 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000905}
906
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100907static void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000908 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000909 unsigned char *output )
910{
Paul Bakker9e36f042013-06-30 14:34:05 +0200911 sha512_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000912}
913
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100914static void * sha512_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000915{
Paul Bakker5b4af392014-06-26 12:09:34 +0200916 sha512_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500917 ctx = polarssl_malloc( sizeof( sha512_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200918
919 if( ctx == NULL )
920 return( NULL );
921
922 sha512_init( ctx );
923
924 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000925}
926
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100927static void sha512_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000928{
Paul Bakker5b4af392014-06-26 12:09:34 +0200929 sha512_free( (sha512_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200930 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000931}
932
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100933static void sha512_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100934{
Paul Bakker9e36f042013-06-30 14:34:05 +0200935 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100936}
937
Paul Bakker17373852011-01-06 14:20:01 +0000938const md_info_t sha512_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000939 POLARSSL_MD_SHA512,
940 "SHA512",
941 64,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100942 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000943 sha512_starts_wrap,
944 sha512_update_wrap,
945 sha512_finish_wrap,
946 sha512_wrap,
947 sha512_file_wrap,
948 sha512_hmac_starts_wrap,
949 sha512_hmac_update_wrap,
950 sha512_hmac_finish_wrap,
951 sha512_hmac_reset_wrap,
952 sha512_hmac_wrap,
953 sha512_ctx_alloc,
954 sha512_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100955 sha512_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000956};
957
Paul Bakker9af723c2014-05-01 13:03:14 +0200958#endif /* POLARSSL_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000959
Paul Bakker9af723c2014-05-01 13:03:14 +0200960#endif /* POLARSSL_MD_C */