blob: 4f12ed6b1ff68e3634cf348153e8cdaec68a61aa [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker17373852011-01-06 14:20:01 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000034
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/md_internal.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_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>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020069#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020071#endif
72
Paul Bakker34617722014-06-13 17:20:13 +020073/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020075 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
76}
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_MD2_C)
Paul Bakker17373852011-01-06 14:20:01 +000079
80static void md2_starts_wrap( void *ctx )
81{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_md2_starts( (mbedtls_md2_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +000083}
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_md2_update( (mbedtls_md2_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +000089}
90
91static void md2_finish_wrap( void *ctx, unsigned char *output )
92{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_md2_finish( (mbedtls_md2_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +000094}
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_FS_IO)
99 return mbedtls_md2_file( path, output );
Paul Bakker335db3f2011-04-25 15:28:35 +0000100#else
101 ((void) path);
102 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 return( MBEDTLS_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{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200109 return mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000110}
111
112static void md2_ctx_free( void *ctx )
113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_zeroize( ctx, sizeof( mbedtls_md2_context ) );
115 mbedtls_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_md2_process( (mbedtls_md2_context *) ctx );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100123}
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125const mbedtls_md_info_t mbedtls_md2_info = {
126 MBEDTLS_MD_MD2,
Paul Bakker23986e52011-04-24 08:57:21 +0000127 "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,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#endif /* MBEDTLS_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +0000143
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100144static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_md4_starts( (mbedtls_md4_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000147}
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_md4_update( (mbedtls_md4_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000153}
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_md4_finish( (mbedtls_md4_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000158}
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_FS_IO)
163 return mbedtls_md4_file( path, output );
Paul Bakker335db3f2011-04-25 15:28:35 +0000164#else
165 ((void) path);
166 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 return( MBEDTLS_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{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200173 return mbedtls_calloc( 1, sizeof( mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_zeroize( ctx, sizeof( mbedtls_md4_context ) );
179 mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_md4_process( (mbedtls_md4_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100185}
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187const mbedtls_md_info_t mbedtls_md4_info = {
188 MBEDTLS_MD_MD4,
Paul Bakker23986e52011-04-24 08:57:21 +0000189 "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,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +0000205
206static void md5_starts_wrap( void *ctx )
207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_md5_starts( (mbedtls_md5_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000209}
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_md5_update( (mbedtls_md5_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000215}
216
217static void md5_finish_wrap( void *ctx, unsigned char *output )
218{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_md5_finish( (mbedtls_md5_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000220}
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_FS_IO)
225 return mbedtls_md5_file( path, output );
Paul Bakker335db3f2011-04-25 15:28:35 +0000226#else
227 ((void) path);
228 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 return( MBEDTLS_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{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200235 return mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000236}
237
238static void md5_ctx_free( void *ctx )
239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) );
241 mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_md5_process( (mbedtls_md5_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100247}
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249const mbedtls_md_info_t mbedtls_md5_info = {
250 MBEDTLS_MD_MD5,
Paul Bakker23986e52011-04-24 08:57:21 +0000251 "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,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#endif /* MBEDTLS_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266#if defined(MBEDTLS_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_ripemd160_starts( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_ripemd160_update( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_ripemd160_finish( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#if defined(MBEDTLS_FS_IO)
287 return mbedtls_ripemd160_file( path, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100288#else
289 ((void) path);
290 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 return( MBEDTLS_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_ripemd160_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200298 ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200299
300 if( ctx == NULL )
301 return( NULL );
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_ripemd160_init( ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200304
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
311 mbedtls_free( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100312}
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_ripemd160_process( (mbedtls_ripemd160_context *) ctx, data );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100317}
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319const mbedtls_md_info_t mbedtls_ripemd160_info = {
320 MBEDTLS_MD_RIPEMD160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100321 "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,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_ripemd160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100328 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#endif /* MBEDTLS_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#if defined(MBEDTLS_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +0000337
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100338static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000339{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 mbedtls_sha1_starts( (mbedtls_sha1_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000341}
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_sha1_update( (mbedtls_sha1_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000347}
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_sha1_finish( (mbedtls_sha1_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000352}
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_FS_IO)
357 return mbedtls_sha1_file( path, output );
Paul Bakker335db3f2011-04-25 15:28:35 +0000358#else
359 ((void) path);
360 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_sha1_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200368 ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200369
370 if( ctx == NULL )
371 return( NULL );
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_sha1_init( ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200374
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
381 mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_sha1_process( (mbedtls_sha1_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100387}
388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389const mbedtls_md_info_t mbedtls_sha1_info = {
390 MBEDTLS_MD_SHA1,
Paul Bakker23986e52011-04-24 08:57:21 +0000391 "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,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#endif /* MBEDTLS_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000405
406/*
407 * Wrappers for generic message digests
408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#if defined(MBEDTLS_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_sha256_starts( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_sha256_update( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_sha256_finish( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#if defined(MBEDTLS_FS_IO)
436 return mbedtls_sha256_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000437#else
438 ((void) path);
439 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 return( MBEDTLS_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{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200446 return mbedtls_calloc( 1, sizeof( mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
452 mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_sha256_process( (mbedtls_sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100458}
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460const mbedtls_md_info_t mbedtls_sha224_info = {
461 MBEDTLS_MD_SHA224,
Paul Bakker23986e52011-04-24 08:57:21 +0000462 "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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_sha256_starts( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_sha256_update( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_sha256_finish( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499#if defined(MBEDTLS_FS_IO)
500 return mbedtls_sha256_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000501#else
502 ((void) path);
503 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 return( MBEDTLS_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_sha256_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200511 ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200512
513 if( ctx == NULL )
514 return( NULL );
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_sha256_init( ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200517
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
524 mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_sha256_process( (mbedtls_sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100530}
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532const mbedtls_md_info_t mbedtls_sha256_info = {
533 MBEDTLS_MD_SHA256,
Paul Bakker23986e52011-04-24 08:57:21 +0000534 "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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#endif /* MBEDTLS_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_sha512_starts( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_sha512_update( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_sha512_finish( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#if defined(MBEDTLS_FS_IO)
576 return mbedtls_sha512_file( path, output, 1 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000577#else
578 ((void) path);
579 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 return( MBEDTLS_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{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200586 return mbedtls_calloc( 1, sizeof( mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_zeroize( ctx, sizeof( mbedtls_sha512_context ) );
592 mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_sha512_process( (mbedtls_sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100598}
599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600const mbedtls_md_info_t mbedtls_sha384_info = {
601 MBEDTLS_MD_SHA384,
Paul Bakker23986e52011-04-24 08:57:21 +0000602 "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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_sha512_starts( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_sha512_update( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_sha512_finish( (mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_FS_IO)
640 return mbedtls_sha512_file( path, output, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +0000641#else
642 ((void) path);
643 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_sha512_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200651 ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200652
653 if( ctx == NULL )
654 return( NULL );
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_sha512_init( ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200657
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
664 mbedtls_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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_sha512_process( (mbedtls_sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100670}
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672const mbedtls_md_info_t mbedtls_sha512_info = {
673 MBEDTLS_MD_SHA512,
Paul Bakker23986e52011-04-24 08:57:21 +0000674 "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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_MD_C */