blob: f554333e782ee96e3ad70862cd82bcd27f92af12 [file] [log] [blame] [raw]
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)
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
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
Paul Bakker17373852011-01-06 14:20:01 +000072
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,
151 md2_starts_wrap,
152 md2_update_wrap,
153 md2_finish_wrap,
154 md2,
Paul Bakker335db3f2011-04-25 15:28:35 +0000155 md2_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000156 md2_hmac_starts_wrap,
157 md2_hmac_update_wrap,
158 md2_hmac_finish_wrap,
159 md2_hmac_reset_wrap,
160 md2_hmac,
161 md2_ctx_alloc,
162 md2_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100163 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000164};
165
Paul Bakker9af723c2014-05-01 13:03:14 +0200166#endif /* POLARSSL_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000167
168#if defined(POLARSSL_MD4_C)
169
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100170static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000171{
172 md4_starts( (md4_context *) ctx );
173}
174
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200175static void md4_update_wrap( void *ctx, const unsigned char *input,
176 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000177{
178 md4_update( (md4_context *) ctx, input, ilen );
179}
180
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100181static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000182{
183 md4_finish( (md4_context *) ctx, output );
Paul Bakker335db3f2011-04-25 15:28:35 +0000184}
185
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100186static int md4_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000187{
188#if defined(POLARSSL_FS_IO)
189 return md4_file( path, output );
190#else
191 ((void) path);
192 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200193 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000194#endif
Paul Bakker17373852011-01-06 14:20:01 +0000195}
196
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200197static void md4_hmac_starts_wrap( void *ctx, const unsigned char *key,
198 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000199{
200 md4_hmac_starts( (md4_context *) ctx, key, keylen );
201}
202
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200203static void md4_hmac_update_wrap( void *ctx, const unsigned char *input,
204 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000205{
206 md4_hmac_update( (md4_context *) ctx, input, ilen );
207}
208
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100209static void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000210{
211 md4_hmac_finish( (md4_context *) ctx, output );
212}
213
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100214static void md4_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000215{
216 md4_hmac_reset( (md4_context *) ctx );
217}
218
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100219static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000220{
Paul Bakker6e339b52013-07-03 13:37:05 +0200221 return polarssl_malloc( sizeof( md4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000222}
223
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100224static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000225{
Paul Bakker34617722014-06-13 17:20:13 +0200226 polarssl_zeroize( ctx, sizeof( md4_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200227 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000228}
229
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100230static void md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100231{
232 md4_process( (md4_context *) ctx, data );
233}
234
Paul Bakker17373852011-01-06 14:20:01 +0000235const md_info_t md4_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000236 POLARSSL_MD_MD4,
237 "MD4",
238 16,
239 md4_starts_wrap,
240 md4_update_wrap,
241 md4_finish_wrap,
242 md4,
Paul Bakker335db3f2011-04-25 15:28:35 +0000243 md4_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000244 md4_hmac_starts_wrap,
245 md4_hmac_update_wrap,
246 md4_hmac_finish_wrap,
247 md4_hmac_reset_wrap,
248 md4_hmac,
249 md4_ctx_alloc,
250 md4_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100251 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000252};
253
Paul Bakker9af723c2014-05-01 13:03:14 +0200254#endif /* POLARSSL_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000255
256#if defined(POLARSSL_MD5_C)
257
258static void md5_starts_wrap( void *ctx )
259{
260 md5_starts( (md5_context *) ctx );
261}
262
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200263static void md5_update_wrap( void *ctx, const unsigned char *input,
264 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000265{
266 md5_update( (md5_context *) ctx, input, ilen );
267}
268
269static void md5_finish_wrap( void *ctx, unsigned char *output )
270{
271 md5_finish( (md5_context *) ctx, output );
Paul Bakker335db3f2011-04-25 15:28:35 +0000272}
273
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200274static int md5_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000275{
276#if defined(POLARSSL_FS_IO)
277 return md5_file( path, output );
278#else
279 ((void) path);
280 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200281 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000282#endif
Paul Bakker17373852011-01-06 14:20:01 +0000283}
284
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200285static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key,
286 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000287{
288 md5_hmac_starts( (md5_context *) ctx, key, keylen );
289}
290
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200291static void md5_hmac_update_wrap( void *ctx, const unsigned char *input,
292 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000293{
294 md5_hmac_update( (md5_context *) ctx, input, ilen );
295}
296
297static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
298{
299 md5_hmac_finish( (md5_context *) ctx, output );
300}
301
302static void md5_hmac_reset_wrap( void *ctx )
303{
304 md5_hmac_reset( (md5_context *) ctx );
305}
306
307static void * md5_ctx_alloc( void )
308{
Paul Bakker6e339b52013-07-03 13:37:05 +0200309 return polarssl_malloc( sizeof( md5_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000310}
311
312static void md5_ctx_free( void *ctx )
313{
Paul Bakker34617722014-06-13 17:20:13 +0200314 polarssl_zeroize( ctx, sizeof( md5_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200315 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000316}
317
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100318static void md5_process_wrap( void *ctx, const unsigned char *data )
319{
320 md5_process( (md5_context *) ctx, data );
321}
322
Paul Bakker17373852011-01-06 14:20:01 +0000323const md_info_t md5_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000324 POLARSSL_MD_MD5,
325 "MD5",
326 16,
327 md5_starts_wrap,
328 md5_update_wrap,
329 md5_finish_wrap,
330 md5,
Paul Bakker335db3f2011-04-25 15:28:35 +0000331 md5_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000332 md5_hmac_starts_wrap,
333 md5_hmac_update_wrap,
334 md5_hmac_finish_wrap,
335 md5_hmac_reset_wrap,
336 md5_hmac,
337 md5_ctx_alloc,
338 md5_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100339 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000340};
341
Paul Bakker9af723c2014-05-01 13:03:14 +0200342#endif /* POLARSSL_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000343
Paul Bakker61b699e2014-01-22 13:35:29 +0100344#if defined(POLARSSL_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100345
Paul Bakker61b699e2014-01-22 13:35:29 +0100346static void ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100347{
Paul Bakker61b699e2014-01-22 13:35:29 +0100348 ripemd160_starts( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100349}
350
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200351static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
352 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100353{
Paul Bakker61b699e2014-01-22 13:35:29 +0100354 ripemd160_update( (ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100355}
356
Paul Bakker61b699e2014-01-22 13:35:29 +0100357static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100358{
Paul Bakker61b699e2014-01-22 13:35:29 +0100359 ripemd160_finish( (ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100360}
361
Paul Bakker61b699e2014-01-22 13:35:29 +0100362static int ripemd160_file_wrap( const char *path, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100363{
364#if defined(POLARSSL_FS_IO)
Paul Bakker61b699e2014-01-22 13:35:29 +0100365 return ripemd160_file( path, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100366#else
367 ((void) path);
368 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200369 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100370#endif
371}
372
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200373static void ripemd160_hmac_starts_wrap( void *ctx, const unsigned char *key,
374 size_t keylen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100375{
Paul Bakker61b699e2014-01-22 13:35:29 +0100376 ripemd160_hmac_starts( (ripemd160_context *) ctx, key, keylen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100377}
378
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200379static void ripemd160_hmac_update_wrap( void *ctx, const unsigned char *input,
380 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100381{
Paul Bakker61b699e2014-01-22 13:35:29 +0100382 ripemd160_hmac_update( (ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100383}
384
Paul Bakker61b699e2014-01-22 13:35:29 +0100385static void ripemd160_hmac_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100386{
Paul Bakker61b699e2014-01-22 13:35:29 +0100387 ripemd160_hmac_finish( (ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100388}
389
Paul Bakker61b699e2014-01-22 13:35:29 +0100390static void ripemd160_hmac_reset_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100391{
Paul Bakker61b699e2014-01-22 13:35:29 +0100392 ripemd160_hmac_reset( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100393}
394
Paul Bakker61b699e2014-01-22 13:35:29 +0100395static void * ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100396{
Paul Bakker5b4af392014-06-26 12:09:34 +0200397 ripemd160_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500398 ctx = polarssl_malloc( sizeof( ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200399
400 if( ctx == NULL )
401 return( NULL );
402
403 ripemd160_init( ctx );
404
405 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100406}
407
Paul Bakker61b699e2014-01-22 13:35:29 +0100408static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100409{
Paul Bakker5b4af392014-06-26 12:09:34 +0200410 ripemd160_free( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100411 polarssl_free( ctx );
412}
413
Paul Bakker61b699e2014-01-22 13:35:29 +0100414static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100415{
Paul Bakker61b699e2014-01-22 13:35:29 +0100416 ripemd160_process( (ripemd160_context *) ctx, data );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100417}
418
Paul Bakker61b699e2014-01-22 13:35:29 +0100419const md_info_t ripemd160_info = {
420 POLARSSL_MD_RIPEMD160,
421 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100422 20,
Paul Bakker61b699e2014-01-22 13:35:29 +0100423 ripemd160_starts_wrap,
424 ripemd160_update_wrap,
425 ripemd160_finish_wrap,
426 ripemd160,
427 ripemd160_file_wrap,
428 ripemd160_hmac_starts_wrap,
429 ripemd160_hmac_update_wrap,
430 ripemd160_hmac_finish_wrap,
431 ripemd160_hmac_reset_wrap,
432 ripemd160_hmac,
433 ripemd160_ctx_alloc,
434 ripemd160_ctx_free,
435 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100436};
437
Paul Bakker9af723c2014-05-01 13:03:14 +0200438#endif /* POLARSSL_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100439
Paul Bakker17373852011-01-06 14:20:01 +0000440#if defined(POLARSSL_SHA1_C)
441
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100442static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000443{
444 sha1_starts( (sha1_context *) ctx );
445}
446
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200447static void sha1_update_wrap( void *ctx, const unsigned char *input,
448 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000449{
450 sha1_update( (sha1_context *) ctx, input, ilen );
451}
452
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100453static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000454{
455 sha1_finish( (sha1_context *) ctx, output );
Paul Bakker335db3f2011-04-25 15:28:35 +0000456}
457
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100458static int sha1_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000459{
460#if defined(POLARSSL_FS_IO)
461 return sha1_file( path, output );
462#else
463 ((void) path);
464 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200465 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000466#endif
Paul Bakker17373852011-01-06 14:20:01 +0000467}
468
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200469static void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key,
470 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000471{
472 sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
473}
474
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200475static void sha1_hmac_update_wrap( void *ctx, const unsigned char *input,
476 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000477{
478 sha1_hmac_update( (sha1_context *) ctx, input, ilen );
479}
480
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100481static void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000482{
483 sha1_hmac_finish( (sha1_context *) ctx, output );
484}
485
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100486static void sha1_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000487{
488 sha1_hmac_reset( (sha1_context *) ctx );
489}
490
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100491static void * sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000492{
Paul Bakker5b4af392014-06-26 12:09:34 +0200493 sha1_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500494 ctx = polarssl_malloc( sizeof( sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200495
496 if( ctx == NULL )
497 return( NULL );
498
499 sha1_init( ctx );
500
501 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000502}
503
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100504static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000505{
Paul Bakker5b4af392014-06-26 12:09:34 +0200506 sha1_free( (sha1_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200507 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000508}
509
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100510static void sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100511{
512 sha1_process( (sha1_context *) ctx, data );
513}
514
Paul Bakker17373852011-01-06 14:20:01 +0000515const md_info_t sha1_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000516 POLARSSL_MD_SHA1,
517 "SHA1",
518 20,
519 sha1_starts_wrap,
520 sha1_update_wrap,
521 sha1_finish_wrap,
522 sha1,
Paul Bakker335db3f2011-04-25 15:28:35 +0000523 sha1_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000524 sha1_hmac_starts_wrap,
525 sha1_hmac_update_wrap,
526 sha1_hmac_finish_wrap,
527 sha1_hmac_reset_wrap,
528 sha1_hmac,
529 sha1_ctx_alloc,
530 sha1_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100531 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000532};
533
Paul Bakker9af723c2014-05-01 13:03:14 +0200534#endif /* POLARSSL_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000535
536/*
537 * Wrappers for generic message digests
538 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200539#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000540
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100541static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000542{
Paul Bakker9e36f042013-06-30 14:34:05 +0200543 sha256_starts( (sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000544}
545
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200546static void sha224_update_wrap( void *ctx, const unsigned char *input,
547 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000548{
Paul Bakker9e36f042013-06-30 14:34:05 +0200549 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000550}
551
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100552static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000553{
Paul Bakker9e36f042013-06-30 14:34:05 +0200554 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000555}
556
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100557static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000558 unsigned char *output )
559{
Paul Bakker9e36f042013-06-30 14:34:05 +0200560 sha256( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000561}
562
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100563static int sha224_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000564{
Paul Bakker335db3f2011-04-25 15:28:35 +0000565#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200566 return sha256_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000567#else
568 ((void) path);
569 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200570 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000571#endif
Paul Bakker17373852011-01-06 14:20:01 +0000572}
573
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200574static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key,
575 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000576{
Paul Bakker9e36f042013-06-30 14:34:05 +0200577 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000578}
579
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200580static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input,
581 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000582{
Paul Bakker9e36f042013-06-30 14:34:05 +0200583 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000584}
585
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100586static void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000587{
Paul Bakker9e36f042013-06-30 14:34:05 +0200588 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000589}
590
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100591static void sha224_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000592{
Paul Bakker9e36f042013-06-30 14:34:05 +0200593 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000594}
595
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100596static void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000597 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000598 unsigned char *output )
599{
Paul Bakker9e36f042013-06-30 14:34:05 +0200600 sha256_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000601}
602
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100603static void * sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000604{
Paul Bakker6e339b52013-07-03 13:37:05 +0200605 return polarssl_malloc( sizeof( sha256_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000606}
607
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100608static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000609{
Paul Bakker34617722014-06-13 17:20:13 +0200610 polarssl_zeroize( ctx, sizeof( sha256_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200611 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000612}
613
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100614static void sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100615{
Paul Bakker9e36f042013-06-30 14:34:05 +0200616 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100617}
618
Paul Bakker17373852011-01-06 14:20:01 +0000619const md_info_t sha224_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000620 POLARSSL_MD_SHA224,
621 "SHA224",
622 28,
623 sha224_starts_wrap,
624 sha224_update_wrap,
625 sha224_finish_wrap,
626 sha224_wrap,
627 sha224_file_wrap,
628 sha224_hmac_starts_wrap,
629 sha224_hmac_update_wrap,
630 sha224_hmac_finish_wrap,
631 sha224_hmac_reset_wrap,
632 sha224_hmac_wrap,
633 sha224_ctx_alloc,
634 sha224_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100635 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000636};
637
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100638static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000639{
Paul Bakker9e36f042013-06-30 14:34:05 +0200640 sha256_starts( (sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000641}
642
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200643static void sha256_update_wrap( void *ctx, const unsigned char *input,
644 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000645{
Paul Bakker9e36f042013-06-30 14:34:05 +0200646 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000647}
648
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100649static void sha256_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000650{
Paul Bakker9e36f042013-06-30 14:34:05 +0200651 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000652}
653
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100654static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000655 unsigned char *output )
656{
Paul Bakker9e36f042013-06-30 14:34:05 +0200657 sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000658}
659
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100660static int sha256_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000661{
Paul Bakker335db3f2011-04-25 15:28:35 +0000662#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200663 return sha256_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000664#else
665 ((void) path);
666 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200667 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000668#endif
Paul Bakker17373852011-01-06 14:20:01 +0000669}
670
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200671static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key,
672 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000673{
Paul Bakker9e36f042013-06-30 14:34:05 +0200674 sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000675}
676
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200677static void sha256_hmac_update_wrap( void *ctx, const unsigned char *input,
678 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000679{
Paul Bakker9e36f042013-06-30 14:34:05 +0200680 sha256_hmac_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000681}
682
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100683static void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000684{
Paul Bakker9e36f042013-06-30 14:34:05 +0200685 sha256_hmac_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000686}
687
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100688static void sha256_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000689{
Paul Bakker9e36f042013-06-30 14:34:05 +0200690 sha256_hmac_reset( (sha256_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000691}
692
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100693static void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000694 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000695 unsigned char *output )
696{
Paul Bakker9e36f042013-06-30 14:34:05 +0200697 sha256_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000698}
699
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100700static void * sha256_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000701{
Paul Bakker5b4af392014-06-26 12:09:34 +0200702 sha256_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500703 ctx = polarssl_malloc( sizeof( sha256_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200704
705 if( ctx == NULL )
706 return( NULL );
707
708 sha256_init( ctx );
709
710 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000711}
712
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100713static void sha256_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000714{
Paul Bakker5b4af392014-06-26 12:09:34 +0200715 sha256_free( (sha256_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200716 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000717}
718
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100719static void sha256_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100720{
Paul Bakker9e36f042013-06-30 14:34:05 +0200721 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100722}
723
Paul Bakker17373852011-01-06 14:20:01 +0000724const md_info_t sha256_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000725 POLARSSL_MD_SHA256,
726 "SHA256",
727 32,
728 sha256_starts_wrap,
729 sha256_update_wrap,
730 sha256_finish_wrap,
731 sha256_wrap,
732 sha256_file_wrap,
733 sha256_hmac_starts_wrap,
734 sha256_hmac_update_wrap,
735 sha256_hmac_finish_wrap,
736 sha256_hmac_reset_wrap,
737 sha256_hmac_wrap,
738 sha256_ctx_alloc,
739 sha256_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100740 sha256_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000741};
742
Paul Bakker9af723c2014-05-01 13:03:14 +0200743#endif /* POLARSSL_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000744
Paul Bakker9e36f042013-06-30 14:34:05 +0200745#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000746
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100747static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000748{
Paul Bakker9e36f042013-06-30 14:34:05 +0200749 sha512_starts( (sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000750}
751
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200752static void sha384_update_wrap( void *ctx, const unsigned char *input,
753 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000754{
Paul Bakker9e36f042013-06-30 14:34:05 +0200755 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000756}
757
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100758static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000759{
Paul Bakker9e36f042013-06-30 14:34:05 +0200760 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000761}
762
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100763static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000764 unsigned char *output )
765{
Paul Bakker9e36f042013-06-30 14:34:05 +0200766 sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000767}
768
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100769static int sha384_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000770{
Paul Bakker335db3f2011-04-25 15:28:35 +0000771#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200772 return sha512_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000773#else
774 ((void) path);
775 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200776 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000777#endif
Paul Bakker17373852011-01-06 14:20:01 +0000778}
779
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200780static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key,
781 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000782{
Paul Bakker9e36f042013-06-30 14:34:05 +0200783 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000784}
785
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200786static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input,
787 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000788{
Paul Bakker9e36f042013-06-30 14:34:05 +0200789 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000790}
791
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100792static void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000793{
Paul Bakker9e36f042013-06-30 14:34:05 +0200794 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000795}
796
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100797static void sha384_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000798{
Paul Bakker9e36f042013-06-30 14:34:05 +0200799 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000800}
801
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100802static void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000803 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000804 unsigned char *output )
805{
Paul Bakker9e36f042013-06-30 14:34:05 +0200806 sha512_hmac( key, keylen, input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000807}
808
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100809static void * sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000810{
Paul Bakker6e339b52013-07-03 13:37:05 +0200811 return polarssl_malloc( sizeof( sha512_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000812}
813
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100814static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000815{
Paul Bakker34617722014-06-13 17:20:13 +0200816 polarssl_zeroize( ctx, sizeof( sha512_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200817 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000818}
819
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100820static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100821{
Paul Bakker9e36f042013-06-30 14:34:05 +0200822 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100823}
824
Paul Bakker17373852011-01-06 14:20:01 +0000825const md_info_t sha384_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000826 POLARSSL_MD_SHA384,
827 "SHA384",
828 48,
829 sha384_starts_wrap,
830 sha384_update_wrap,
831 sha384_finish_wrap,
832 sha384_wrap,
833 sha384_file_wrap,
834 sha384_hmac_starts_wrap,
835 sha384_hmac_update_wrap,
836 sha384_hmac_finish_wrap,
837 sha384_hmac_reset_wrap,
838 sha384_hmac_wrap,
839 sha384_ctx_alloc,
840 sha384_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100841 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000842};
843
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100844static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000845{
Paul Bakker9e36f042013-06-30 14:34:05 +0200846 sha512_starts( (sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000847}
848
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200849static void sha512_update_wrap( void *ctx, const unsigned char *input,
850 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000851{
Paul Bakker9e36f042013-06-30 14:34:05 +0200852 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000853}
854
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100855static void sha512_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000856{
Paul Bakker9e36f042013-06-30 14:34:05 +0200857 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000858}
859
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100860static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000861 unsigned char *output )
862{
Paul Bakker9e36f042013-06-30 14:34:05 +0200863 sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000864}
865
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100866static int sha512_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000867{
Paul Bakker335db3f2011-04-25 15:28:35 +0000868#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200869 return sha512_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000870#else
871 ((void) path);
872 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200873 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000874#endif
Paul Bakker17373852011-01-06 14:20:01 +0000875}
876
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200877static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key,
878 size_t keylen )
Paul Bakker17373852011-01-06 14:20:01 +0000879{
Paul Bakker9e36f042013-06-30 14:34:05 +0200880 sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000881}
882
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200883static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input,
884 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000885{
Paul Bakker9e36f042013-06-30 14:34:05 +0200886 sha512_hmac_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000887}
888
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100889static void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000890{
Paul Bakker9e36f042013-06-30 14:34:05 +0200891 sha512_hmac_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000892}
893
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100894static void sha512_hmac_reset_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000895{
Paul Bakker9e36f042013-06-30 14:34:05 +0200896 sha512_hmac_reset( (sha512_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000897}
898
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100899static void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000900 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000901 unsigned char *output )
902{
Paul Bakker9e36f042013-06-30 14:34:05 +0200903 sha512_hmac( key, keylen, input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000904}
905
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100906static void * sha512_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000907{
Paul Bakker5b4af392014-06-26 12:09:34 +0200908 sha512_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500909 ctx = polarssl_malloc( sizeof( sha512_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200910
911 if( ctx == NULL )
912 return( NULL );
913
914 sha512_init( ctx );
915
916 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000917}
918
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100919static void sha512_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000920{
Paul Bakker5b4af392014-06-26 12:09:34 +0200921 sha512_free( (sha512_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200922 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000923}
924
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100925static void sha512_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100926{
Paul Bakker9e36f042013-06-30 14:34:05 +0200927 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100928}
929
Paul Bakker17373852011-01-06 14:20:01 +0000930const md_info_t sha512_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000931 POLARSSL_MD_SHA512,
932 "SHA512",
933 64,
934 sha512_starts_wrap,
935 sha512_update_wrap,
936 sha512_finish_wrap,
937 sha512_wrap,
938 sha512_file_wrap,
939 sha512_hmac_starts_wrap,
940 sha512_hmac_update_wrap,
941 sha512_hmac_finish_wrap,
942 sha512_hmac_reset_wrap,
943 sha512_hmac_wrap,
944 sha512_ctx_alloc,
945 sha512_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100946 sha512_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000947};
948
Paul Bakker9af723c2014-05-01 13:03:14 +0200949#endif /* POLARSSL_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000950
Paul Bakker9af723c2014-05-01 13:03:14 +0200951#endif /* POLARSSL_MD_C */