blob: c1140d7f8664415abe7b2b41f95af9af0f62f0ba [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 Bakker17373852011-01-06 14:20:01 +000096static void * md2_ctx_alloc( void )
97{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020098 return mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
Paul Bakker17373852011-01-06 14:20:01 +000099}
100
101static void md2_ctx_free( void *ctx )
102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_zeroize( ctx, sizeof( mbedtls_md2_context ) );
104 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000105}
106
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100107static void md2_process_wrap( void *ctx, const unsigned char *data )
108{
109 ((void) data);
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_md2_process( (mbedtls_md2_context *) ctx );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100112}
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114const mbedtls_md_info_t mbedtls_md2_info = {
115 MBEDTLS_MD_MD2,
Paul Bakker23986e52011-04-24 08:57:21 +0000116 "MD2",
117 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100118 16,
Paul Bakker23986e52011-04-24 08:57:21 +0000119 md2_starts_wrap,
120 md2_update_wrap,
121 md2_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_md2,
Paul Bakker23986e52011-04-24 08:57:21 +0000123 md2_ctx_alloc,
124 md2_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100125 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000126};
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#endif /* MBEDTLS_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +0000131
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100132static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_md4_starts( (mbedtls_md4_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000135}
136
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200137static void md4_update_wrap( void *ctx, const unsigned char *input,
138 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_md4_update( (mbedtls_md4_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000141}
142
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100143static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_md4_finish( (mbedtls_md4_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000146}
147
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100148static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000149{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200150 return mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000151}
152
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100153static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_zeroize( ctx, sizeof( mbedtls_md4_context ) );
156 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000157}
158
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100159static void md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_md4_process( (mbedtls_md4_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100162}
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164const mbedtls_md_info_t mbedtls_md4_info = {
165 MBEDTLS_MD_MD4,
Paul Bakker23986e52011-04-24 08:57:21 +0000166 "MD4",
167 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100168 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000169 md4_starts_wrap,
170 md4_update_wrap,
171 md4_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_md4,
Paul Bakker23986e52011-04-24 08:57:21 +0000173 md4_ctx_alloc,
174 md4_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100175 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000176};
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#endif /* MBEDTLS_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +0000181
182static void md5_starts_wrap( void *ctx )
183{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_md5_starts( (mbedtls_md5_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000185}
186
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200187static void md5_update_wrap( void *ctx, const unsigned char *input,
188 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000189{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_md5_update( (mbedtls_md5_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000191}
192
193static void md5_finish_wrap( void *ctx, unsigned char *output )
194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_md5_finish( (mbedtls_md5_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000196}
197
Paul Bakker17373852011-01-06 14:20:01 +0000198static void * md5_ctx_alloc( void )
199{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200200 return mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000201}
202
203static void md5_ctx_free( void *ctx )
204{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) );
206 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000207}
208
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100209static void md5_process_wrap( void *ctx, const unsigned char *data )
210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_md5_process( (mbedtls_md5_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100212}
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214const mbedtls_md_info_t mbedtls_md5_info = {
215 MBEDTLS_MD_MD5,
Paul Bakker23986e52011-04-24 08:57:21 +0000216 "MD5",
217 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100218 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000219 md5_starts_wrap,
220 md5_update_wrap,
221 md5_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_md5,
Paul Bakker23986e52011-04-24 08:57:21 +0000223 md5_ctx_alloc,
224 md5_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100225 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000226};
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#endif /* MBEDTLS_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100231
Paul Bakker61b699e2014-01-22 13:35:29 +0100232static void ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100233{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_ripemd160_starts( (mbedtls_ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100235}
236
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200237static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
238 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_ripemd160_update( (mbedtls_ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100241}
242
Paul Bakker61b699e2014-01-22 13:35:29 +0100243static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_ripemd160_finish( (mbedtls_ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100246}
247
Paul Bakker61b699e2014-01-22 13:35:29 +0100248static void * ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100249{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_ripemd160_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200251 ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200252
253 if( ctx == NULL )
254 return( NULL );
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_ripemd160_init( ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200257
258 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100259}
260
Paul Bakker61b699e2014-01-22 13:35:29 +0100261static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100262{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
264 mbedtls_free( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100265}
266
Paul Bakker61b699e2014-01-22 13:35:29 +0100267static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100268{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_ripemd160_process( (mbedtls_ripemd160_context *) ctx, data );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100270}
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272const mbedtls_md_info_t mbedtls_ripemd160_info = {
273 MBEDTLS_MD_RIPEMD160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100274 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100275 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100276 64,
Paul Bakker61b699e2014-01-22 13:35:29 +0100277 ripemd160_starts_wrap,
278 ripemd160_update_wrap,
279 ripemd160_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_ripemd160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100281 ripemd160_ctx_alloc,
282 ripemd160_ctx_free,
283 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100284};
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#endif /* MBEDTLS_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +0000289
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100290static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_sha1_starts( (mbedtls_sha1_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000293}
294
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200295static void sha1_update_wrap( void *ctx, const unsigned char *input,
296 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_sha1_update( (mbedtls_sha1_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000299}
300
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100301static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_sha1_finish( (mbedtls_sha1_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000304}
305
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100306static void * sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000307{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_sha1_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200309 ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200310
311 if( ctx == NULL )
312 return( NULL );
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_sha1_init( ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200315
316 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000317}
318
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100319static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000320{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
322 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000323}
324
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100325static void sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_sha1_process( (mbedtls_sha1_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100328}
329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330const mbedtls_md_info_t mbedtls_sha1_info = {
331 MBEDTLS_MD_SHA1,
Paul Bakker23986e52011-04-24 08:57:21 +0000332 "SHA1",
333 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100334 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000335 sha1_starts_wrap,
336 sha1_update_wrap,
337 sha1_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_sha1,
Paul Bakker23986e52011-04-24 08:57:21 +0000339 sha1_ctx_alloc,
340 sha1_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100341 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000342};
343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#endif /* MBEDTLS_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000345
346/*
347 * Wrappers for generic message digests
348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#if defined(MBEDTLS_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000350
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100351static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000354}
355
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200356static void sha224_update_wrap( void *ctx, const unsigned char *input,
357 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000358{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_sha256_update( (mbedtls_sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000360}
361
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100362static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000363{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_sha256_finish( (mbedtls_sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000365}
366
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100367static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000368 unsigned char *output )
369{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_sha256( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000371}
372
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100373static void * sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000374{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200375 return mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000376}
377
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100378static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000379{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
381 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000382}
383
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100384static void sha224_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_sha256_process( (mbedtls_sha256_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_sha224_info = {
390 MBEDTLS_MD_SHA224,
Paul Bakker23986e52011-04-24 08:57:21 +0000391 "SHA224",
392 28,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100393 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000394 sha224_starts_wrap,
395 sha224_update_wrap,
396 sha224_finish_wrap,
397 sha224_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000398 sha224_ctx_alloc,
399 sha224_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100400 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000401};
402
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100403static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000406}
407
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200408static void sha256_update_wrap( void *ctx, const unsigned char *input,
409 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000410{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_sha256_update( (mbedtls_sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000412}
413
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100414static void sha256_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_sha256_finish( (mbedtls_sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000417}
418
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100419static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000420 unsigned char *output )
421{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000423}
424
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100425static void * sha256_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000426{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_sha256_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200428 ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200429
430 if( ctx == NULL )
431 return( NULL );
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_sha256_init( ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200434
435 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000436}
437
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100438static void sha256_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000439{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
441 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000442}
443
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100444static void sha256_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100445{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_sha256_process( (mbedtls_sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100447}
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449const mbedtls_md_info_t mbedtls_sha256_info = {
450 MBEDTLS_MD_SHA256,
Paul Bakker23986e52011-04-24 08:57:21 +0000451 "SHA256",
452 32,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100453 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000454 sha256_starts_wrap,
455 sha256_update_wrap,
456 sha256_finish_wrap,
457 sha256_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000458 sha256_ctx_alloc,
459 sha256_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100460 sha256_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000461};
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#endif /* MBEDTLS_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#if defined(MBEDTLS_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000466
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100467static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000468{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000470}
471
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200472static void sha384_update_wrap( void *ctx, const unsigned char *input,
473 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000474{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_sha512_update( (mbedtls_sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000476}
477
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100478static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000479{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_sha512_finish( (mbedtls_sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000481}
482
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100483static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000484 unsigned char *output )
485{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000487}
488
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100489static void * sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000490{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200491 return mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
Paul Bakker17373852011-01-06 14:20:01 +0000492}
493
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100494static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000495{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_zeroize( ctx, sizeof( mbedtls_sha512_context ) );
497 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000498}
499
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100500static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100501{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_sha512_process( (mbedtls_sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100503}
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505const mbedtls_md_info_t mbedtls_sha384_info = {
506 MBEDTLS_MD_SHA384,
Paul Bakker23986e52011-04-24 08:57:21 +0000507 "SHA384",
508 48,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100509 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000510 sha384_starts_wrap,
511 sha384_update_wrap,
512 sha384_finish_wrap,
513 sha384_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000514 sha384_ctx_alloc,
515 sha384_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100516 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000517};
518
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100519static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000520{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000522}
523
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200524static void sha512_update_wrap( void *ctx, const unsigned char *input,
525 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000526{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_sha512_update( (mbedtls_sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000528}
529
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100530static void sha512_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_sha512_finish( (mbedtls_sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000533}
534
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100535static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000536 unsigned char *output )
537{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000539}
540
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100541static void * sha512_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000542{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_sha512_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200544 ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200545
546 if( ctx == NULL )
547 return( NULL );
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_sha512_init( ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200550
551 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000552}
553
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100554static void sha512_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
557 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000558}
559
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100560static void sha512_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100561{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_sha512_process( (mbedtls_sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100563}
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565const mbedtls_md_info_t mbedtls_sha512_info = {
566 MBEDTLS_MD_SHA512,
Paul Bakker23986e52011-04-24 08:57:21 +0000567 "SHA512",
568 64,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100569 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000570 sha512_starts_wrap,
571 sha512_update_wrap,
572 sha512_finish_wrap,
573 sha512_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000574 sha512_ctx_alloc,
575 sha512_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100576 sha512_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000577};
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#endif /* MBEDTLS_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#endif /* MBEDTLS_MD_C */