blob: 17c6ab4356a881973cb0e2043e9113c41c48c289 [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 Bakker17373852011-01-06 14:20:01 +0000107static void * md2_ctx_alloc( void )
108{
Paul Bakker6e339b52013-07-03 13:37:05 +0200109 return polarssl_malloc( sizeof( md2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000110}
111
112static void md2_ctx_free( void *ctx )
113{
Paul Bakker34617722014-06-13 17:20:13 +0200114 polarssl_zeroize( ctx, sizeof( md2_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200115 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000116}
117
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100118static void md2_process_wrap( void *ctx, const unsigned char *data )
119{
120 ((void) data);
121
122 md2_process( (md2_context *) ctx );
123}
124
Paul Bakker17373852011-01-06 14:20:01 +0000125const md_info_t md2_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000126 POLARSSL_MD_MD2,
127 "MD2",
128 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100129 16,
Paul Bakker23986e52011-04-24 08:57:21 +0000130 md2_starts_wrap,
131 md2_update_wrap,
132 md2_finish_wrap,
133 md2,
Paul Bakker335db3f2011-04-25 15:28:35 +0000134 md2_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000135 md2_ctx_alloc,
136 md2_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100137 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000138};
139
Paul Bakker9af723c2014-05-01 13:03:14 +0200140#endif /* POLARSSL_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000141
142#if defined(POLARSSL_MD4_C)
143
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100144static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000145{
146 md4_starts( (md4_context *) ctx );
147}
148
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200149static void md4_update_wrap( void *ctx, const unsigned char *input,
150 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000151{
152 md4_update( (md4_context *) ctx, input, ilen );
153}
154
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100155static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000156{
157 md4_finish( (md4_context *) ctx, output );
158}
159
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100160static int md4_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000161{
162#if defined(POLARSSL_FS_IO)
163 return md4_file( path, output );
164#else
165 ((void) path);
166 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200167 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000168#endif
169}
170
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100171static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000172{
Paul Bakker6e339b52013-07-03 13:37:05 +0200173 return polarssl_malloc( sizeof( md4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000174}
175
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100176static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000177{
Paul Bakker34617722014-06-13 17:20:13 +0200178 polarssl_zeroize( ctx, sizeof( md4_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200179 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000180}
181
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100182static void md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100183{
184 md4_process( (md4_context *) ctx, data );
185}
186
Paul Bakker17373852011-01-06 14:20:01 +0000187const md_info_t md4_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000188 POLARSSL_MD_MD4,
189 "MD4",
190 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100191 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000192 md4_starts_wrap,
193 md4_update_wrap,
194 md4_finish_wrap,
195 md4,
Paul Bakker335db3f2011-04-25 15:28:35 +0000196 md4_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000197 md4_ctx_alloc,
198 md4_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100199 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000200};
201
Paul Bakker9af723c2014-05-01 13:03:14 +0200202#endif /* POLARSSL_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000203
204#if defined(POLARSSL_MD5_C)
205
206static void md5_starts_wrap( void *ctx )
207{
208 md5_starts( (md5_context *) ctx );
209}
210
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200211static void md5_update_wrap( void *ctx, const unsigned char *input,
212 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000213{
214 md5_update( (md5_context *) ctx, input, ilen );
215}
216
217static void md5_finish_wrap( void *ctx, unsigned char *output )
218{
219 md5_finish( (md5_context *) ctx, output );
220}
221
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200222static int md5_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000223{
224#if defined(POLARSSL_FS_IO)
225 return md5_file( path, output );
226#else
227 ((void) path);
228 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200229 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000230#endif
231}
232
Paul Bakker17373852011-01-06 14:20:01 +0000233static void * md5_ctx_alloc( void )
234{
Paul Bakker6e339b52013-07-03 13:37:05 +0200235 return polarssl_malloc( sizeof( md5_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000236}
237
238static void md5_ctx_free( void *ctx )
239{
Paul Bakker34617722014-06-13 17:20:13 +0200240 polarssl_zeroize( ctx, sizeof( md5_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200241 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000242}
243
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100244static void md5_process_wrap( void *ctx, const unsigned char *data )
245{
246 md5_process( (md5_context *) ctx, data );
247}
248
Paul Bakker17373852011-01-06 14:20:01 +0000249const md_info_t md5_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000250 POLARSSL_MD_MD5,
251 "MD5",
252 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100253 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000254 md5_starts_wrap,
255 md5_update_wrap,
256 md5_finish_wrap,
257 md5,
Paul Bakker335db3f2011-04-25 15:28:35 +0000258 md5_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000259 md5_ctx_alloc,
260 md5_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100261 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000262};
263
Paul Bakker9af723c2014-05-01 13:03:14 +0200264#endif /* POLARSSL_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000265
Paul Bakker61b699e2014-01-22 13:35:29 +0100266#if defined(POLARSSL_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100267
Paul Bakker61b699e2014-01-22 13:35:29 +0100268static void ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100269{
Paul Bakker61b699e2014-01-22 13:35:29 +0100270 ripemd160_starts( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100271}
272
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200273static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
274 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100275{
Paul Bakker61b699e2014-01-22 13:35:29 +0100276 ripemd160_update( (ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100277}
278
Paul Bakker61b699e2014-01-22 13:35:29 +0100279static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100280{
Paul Bakker61b699e2014-01-22 13:35:29 +0100281 ripemd160_finish( (ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100282}
283
Paul Bakker61b699e2014-01-22 13:35:29 +0100284static int ripemd160_file_wrap( const char *path, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100285{
286#if defined(POLARSSL_FS_IO)
Paul Bakker61b699e2014-01-22 13:35:29 +0100287 return ripemd160_file( path, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100288#else
289 ((void) path);
290 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200291 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100292#endif
293}
294
Paul Bakker61b699e2014-01-22 13:35:29 +0100295static void * ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100296{
Paul Bakker5b4af392014-06-26 12:09:34 +0200297 ripemd160_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500298 ctx = polarssl_malloc( sizeof( ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200299
300 if( ctx == NULL )
301 return( NULL );
302
303 ripemd160_init( ctx );
304
305 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100306}
307
Paul Bakker61b699e2014-01-22 13:35:29 +0100308static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100309{
Paul Bakker5b4af392014-06-26 12:09:34 +0200310 ripemd160_free( (ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100311 polarssl_free( ctx );
312}
313
Paul Bakker61b699e2014-01-22 13:35:29 +0100314static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100315{
Paul Bakker61b699e2014-01-22 13:35:29 +0100316 ripemd160_process( (ripemd160_context *) ctx, data );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100317}
318
Paul Bakker61b699e2014-01-22 13:35:29 +0100319const md_info_t ripemd160_info = {
320 POLARSSL_MD_RIPEMD160,
321 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100322 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100323 64,
Paul Bakker61b699e2014-01-22 13:35:29 +0100324 ripemd160_starts_wrap,
325 ripemd160_update_wrap,
326 ripemd160_finish_wrap,
327 ripemd160,
328 ripemd160_file_wrap,
Paul Bakker61b699e2014-01-22 13:35:29 +0100329 ripemd160_ctx_alloc,
330 ripemd160_ctx_free,
331 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100332};
333
Paul Bakker9af723c2014-05-01 13:03:14 +0200334#endif /* POLARSSL_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100335
Paul Bakker17373852011-01-06 14:20:01 +0000336#if defined(POLARSSL_SHA1_C)
337
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100338static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000339{
340 sha1_starts( (sha1_context *) ctx );
341}
342
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200343static void sha1_update_wrap( void *ctx, const unsigned char *input,
344 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000345{
346 sha1_update( (sha1_context *) ctx, input, ilen );
347}
348
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100349static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000350{
351 sha1_finish( (sha1_context *) ctx, output );
352}
353
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100354static int sha1_file_wrap( const char *path, unsigned char *output )
Paul Bakker335db3f2011-04-25 15:28:35 +0000355{
356#if defined(POLARSSL_FS_IO)
357 return sha1_file( path, output );
358#else
359 ((void) path);
360 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200361 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000362#endif
363}
364
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100365static void * sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000366{
Paul Bakker5b4af392014-06-26 12:09:34 +0200367 sha1_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500368 ctx = polarssl_malloc( sizeof( sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200369
370 if( ctx == NULL )
371 return( NULL );
372
373 sha1_init( ctx );
374
375 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000376}
377
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100378static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000379{
Paul Bakker5b4af392014-06-26 12:09:34 +0200380 sha1_free( (sha1_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200381 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000382}
383
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100384static void sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100385{
386 sha1_process( (sha1_context *) ctx, data );
387}
388
Paul Bakker17373852011-01-06 14:20:01 +0000389const md_info_t sha1_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000390 POLARSSL_MD_SHA1,
391 "SHA1",
392 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100393 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000394 sha1_starts_wrap,
395 sha1_update_wrap,
396 sha1_finish_wrap,
397 sha1,
Paul Bakker335db3f2011-04-25 15:28:35 +0000398 sha1_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000399 sha1_ctx_alloc,
400 sha1_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100401 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000402};
403
Paul Bakker9af723c2014-05-01 13:03:14 +0200404#endif /* POLARSSL_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000405
406/*
407 * Wrappers for generic message digests
408 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200409#if defined(POLARSSL_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000410
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100411static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000412{
Paul Bakker9e36f042013-06-30 14:34:05 +0200413 sha256_starts( (sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000414}
415
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200416static void sha224_update_wrap( void *ctx, const unsigned char *input,
417 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000418{
Paul Bakker9e36f042013-06-30 14:34:05 +0200419 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000420}
421
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100422static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000423{
Paul Bakker9e36f042013-06-30 14:34:05 +0200424 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000425}
426
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100427static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000428 unsigned char *output )
429{
Paul Bakker9e36f042013-06-30 14:34:05 +0200430 sha256( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000431}
432
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100433static int sha224_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000434{
Paul Bakker335db3f2011-04-25 15:28:35 +0000435#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200436 return sha256_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000437#else
438 ((void) path);
439 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200440 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000441#endif
Paul Bakker17373852011-01-06 14:20:01 +0000442}
443
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100444static void * sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000445{
Paul Bakker6e339b52013-07-03 13:37:05 +0200446 return polarssl_malloc( sizeof( sha256_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000447}
448
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100449static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000450{
Paul Bakker34617722014-06-13 17:20:13 +0200451 polarssl_zeroize( ctx, sizeof( sha256_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200452 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000453}
454
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100455static void sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100456{
Paul Bakker9e36f042013-06-30 14:34:05 +0200457 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100458}
459
Paul Bakker17373852011-01-06 14:20:01 +0000460const md_info_t sha224_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000461 POLARSSL_MD_SHA224,
462 "SHA224",
463 28,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100464 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000465 sha224_starts_wrap,
466 sha224_update_wrap,
467 sha224_finish_wrap,
468 sha224_wrap,
469 sha224_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000470 sha224_ctx_alloc,
471 sha224_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100472 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000473};
474
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100475static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000476{
Paul Bakker9e36f042013-06-30 14:34:05 +0200477 sha256_starts( (sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000478}
479
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200480static void sha256_update_wrap( void *ctx, const unsigned char *input,
481 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000482{
Paul Bakker9e36f042013-06-30 14:34:05 +0200483 sha256_update( (sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000484}
485
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100486static void sha256_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000487{
Paul Bakker9e36f042013-06-30 14:34:05 +0200488 sha256_finish( (sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000489}
490
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100491static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000492 unsigned char *output )
493{
Paul Bakker9e36f042013-06-30 14:34:05 +0200494 sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000495}
496
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100497static int sha256_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000498{
Paul Bakker335db3f2011-04-25 15:28:35 +0000499#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200500 return sha256_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000501#else
502 ((void) path);
503 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200504 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000505#endif
Paul Bakker17373852011-01-06 14:20:01 +0000506}
507
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100508static void * sha256_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000509{
Paul Bakker5b4af392014-06-26 12:09:34 +0200510 sha256_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500511 ctx = polarssl_malloc( sizeof( sha256_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200512
513 if( ctx == NULL )
514 return( NULL );
515
516 sha256_init( ctx );
517
518 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000519}
520
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100521static void sha256_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000522{
Paul Bakker5b4af392014-06-26 12:09:34 +0200523 sha256_free( (sha256_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200524 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000525}
526
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100527static void sha256_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100528{
Paul Bakker9e36f042013-06-30 14:34:05 +0200529 sha256_process( (sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100530}
531
Paul Bakker17373852011-01-06 14:20:01 +0000532const md_info_t sha256_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000533 POLARSSL_MD_SHA256,
534 "SHA256",
535 32,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100536 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000537 sha256_starts_wrap,
538 sha256_update_wrap,
539 sha256_finish_wrap,
540 sha256_wrap,
541 sha256_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000542 sha256_ctx_alloc,
543 sha256_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100544 sha256_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000545};
546
Paul Bakker9af723c2014-05-01 13:03:14 +0200547#endif /* POLARSSL_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000548
Paul Bakker9e36f042013-06-30 14:34:05 +0200549#if defined(POLARSSL_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000550
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100551static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000552{
Paul Bakker9e36f042013-06-30 14:34:05 +0200553 sha512_starts( (sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000554}
555
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200556static void sha384_update_wrap( void *ctx, const unsigned char *input,
557 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000558{
Paul Bakker9e36f042013-06-30 14:34:05 +0200559 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000560}
561
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100562static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000563{
Paul Bakker9e36f042013-06-30 14:34:05 +0200564 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000565}
566
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100567static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000568 unsigned char *output )
569{
Paul Bakker9e36f042013-06-30 14:34:05 +0200570 sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000571}
572
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100573static int sha384_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000574{
Paul Bakker335db3f2011-04-25 15:28:35 +0000575#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200576 return sha512_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000577#else
578 ((void) path);
579 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200580 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000581#endif
Paul Bakker17373852011-01-06 14:20:01 +0000582}
583
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100584static void * sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000585{
Paul Bakker6e339b52013-07-03 13:37:05 +0200586 return polarssl_malloc( sizeof( sha512_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000587}
588
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100589static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000590{
Paul Bakker34617722014-06-13 17:20:13 +0200591 polarssl_zeroize( ctx, sizeof( sha512_context ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200592 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000593}
594
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100595static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100596{
Paul Bakker9e36f042013-06-30 14:34:05 +0200597 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100598}
599
Paul Bakker17373852011-01-06 14:20:01 +0000600const md_info_t sha384_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000601 POLARSSL_MD_SHA384,
602 "SHA384",
603 48,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100604 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000605 sha384_starts_wrap,
606 sha384_update_wrap,
607 sha384_finish_wrap,
608 sha384_wrap,
609 sha384_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000610 sha384_ctx_alloc,
611 sha384_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100612 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000613};
614
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100615static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000616{
Paul Bakker9e36f042013-06-30 14:34:05 +0200617 sha512_starts( (sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000618}
619
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200620static void sha512_update_wrap( void *ctx, const unsigned char *input,
621 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000622{
Paul Bakker9e36f042013-06-30 14:34:05 +0200623 sha512_update( (sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000624}
625
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100626static void sha512_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000627{
Paul Bakker9e36f042013-06-30 14:34:05 +0200628 sha512_finish( (sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000629}
630
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100631static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000632 unsigned char *output )
633{
Paul Bakker9e36f042013-06-30 14:34:05 +0200634 sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000635}
636
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100637static int sha512_file_wrap( const char *path, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000638{
Paul Bakker335db3f2011-04-25 15:28:35 +0000639#if defined(POLARSSL_FS_IO)
Paul Bakker9e36f042013-06-30 14:34:05 +0200640 return sha512_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000641#else
642 ((void) path);
643 ((void) output);
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200644 return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE );
Paul Bakker335db3f2011-04-25 15:28:35 +0000645#endif
Paul Bakker17373852011-01-06 14:20:01 +0000646}
647
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100648static void * sha512_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000649{
Paul Bakker5b4af392014-06-26 12:09:34 +0200650 sha512_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500651 ctx = polarssl_malloc( sizeof( sha512_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200652
653 if( ctx == NULL )
654 return( NULL );
655
656 sha512_init( ctx );
657
658 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000659}
660
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100661static void sha512_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000662{
Paul Bakker5b4af392014-06-26 12:09:34 +0200663 sha512_free( (sha512_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200664 polarssl_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000665}
666
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100667static void sha512_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100668{
Paul Bakker9e36f042013-06-30 14:34:05 +0200669 sha512_process( (sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100670}
671
Paul Bakker17373852011-01-06 14:20:01 +0000672const md_info_t sha512_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000673 POLARSSL_MD_SHA512,
674 "SHA512",
675 64,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100676 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000677 sha512_starts_wrap,
678 sha512_update_wrap,
679 sha512_finish_wrap,
680 sha512_wrap,
681 sha512_file_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000682 sha512_ctx_alloc,
683 sha512_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100684 sha512_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000685};
686
Paul Bakker9af723c2014-05-01 13:03:14 +0200687#endif /* POLARSSL_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000688
Paul Bakker9af723c2014-05-01 13:03:14 +0200689#endif /* POLARSSL_MD_C */