blob: 31083fc690ec59528c294ecd17dbfdb92f5b6533 [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
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +020096static void *md2_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +000097{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +020098 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
99
100 if( ctx != NULL )
101 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
102
103 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000104}
105
106static void md2_ctx_free( void *ctx )
107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_zeroize( ctx, sizeof( mbedtls_md2_context ) );
109 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000110}
111
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100112static void md2_process_wrap( void *ctx, const unsigned char *data )
113{
114 ((void) data);
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_md2_process( (mbedtls_md2_context *) ctx );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100117}
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119const mbedtls_md_info_t mbedtls_md2_info = {
120 MBEDTLS_MD_MD2,
Paul Bakker23986e52011-04-24 08:57:21 +0000121 "MD2",
122 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100123 16,
Paul Bakker23986e52011-04-24 08:57:21 +0000124 md2_starts_wrap,
125 md2_update_wrap,
126 md2_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_md2,
Paul Bakker23986e52011-04-24 08:57:21 +0000128 md2_ctx_alloc,
129 md2_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100130 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000131};
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#endif /* MBEDTLS_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +0000136
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100137static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000138{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_md4_starts( (mbedtls_md4_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000140}
141
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200142static void md4_update_wrap( void *ctx, const unsigned char *input,
143 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_md4_update( (mbedtls_md4_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000146}
147
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100148static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000149{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_md4_finish( (mbedtls_md4_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000151}
152
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100153static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000154{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200155 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
156
157 if( ctx != NULL )
158 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
159
160 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000161}
162
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100163static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_zeroize( ctx, sizeof( mbedtls_md4_context ) );
166 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000167}
168
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100169static void md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_md4_process( (mbedtls_md4_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100172}
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174const mbedtls_md_info_t mbedtls_md4_info = {
175 MBEDTLS_MD_MD4,
Paul Bakker23986e52011-04-24 08:57:21 +0000176 "MD4",
177 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100178 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000179 md4_starts_wrap,
180 md4_update_wrap,
181 md4_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_md4,
Paul Bakker23986e52011-04-24 08:57:21 +0000183 md4_ctx_alloc,
184 md4_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100185 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000186};
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#endif /* MBEDTLS_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +0000191
192static void md5_starts_wrap( void *ctx )
193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_md5_starts( (mbedtls_md5_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000195}
196
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200197static void md5_update_wrap( void *ctx, const unsigned char *input,
198 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_md5_update( (mbedtls_md5_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000201}
202
203static void md5_finish_wrap( void *ctx, unsigned char *output )
204{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_md5_finish( (mbedtls_md5_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000206}
207
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200208static void *md5_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000209{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200210 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
211
212 if( ctx != NULL )
213 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
214
215 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000216}
217
218static void md5_ctx_free( void *ctx )
219{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) );
221 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000222}
223
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100224static void md5_process_wrap( void *ctx, const unsigned char *data )
225{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_md5_process( (mbedtls_md5_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100227}
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229const mbedtls_md_info_t mbedtls_md5_info = {
230 MBEDTLS_MD_MD5,
Paul Bakker23986e52011-04-24 08:57:21 +0000231 "MD5",
232 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100233 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000234 md5_starts_wrap,
235 md5_update_wrap,
236 md5_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_md5,
Paul Bakker23986e52011-04-24 08:57:21 +0000238 md5_ctx_alloc,
239 md5_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100240 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000241};
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#endif /* MBEDTLS_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100246
Paul Bakker61b699e2014-01-22 13:35:29 +0100247static void ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100248{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_ripemd160_starts( (mbedtls_ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100250}
251
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200252static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
253 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100254{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_ripemd160_update( (mbedtls_ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100256}
257
Paul Bakker61b699e2014-01-22 13:35:29 +0100258static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100259{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_ripemd160_finish( (mbedtls_ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100261}
262
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200263static void *ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100264{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200265 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200266
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200267 if( ctx != NULL )
268 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200269
270 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100271}
272
Paul Bakker61b699e2014-01-22 13:35:29 +0100273static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
276 mbedtls_free( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100277}
278
Paul Bakker61b699e2014-01-22 13:35:29 +0100279static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_ripemd160_process( (mbedtls_ripemd160_context *) ctx, data );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100282}
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284const mbedtls_md_info_t mbedtls_ripemd160_info = {
285 MBEDTLS_MD_RIPEMD160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100286 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100287 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100288 64,
Paul Bakker61b699e2014-01-22 13:35:29 +0100289 ripemd160_starts_wrap,
290 ripemd160_update_wrap,
291 ripemd160_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_ripemd160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100293 ripemd160_ctx_alloc,
294 ripemd160_ctx_free,
295 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100296};
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +0000301
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100302static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_sha1_starts( (mbedtls_sha1_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000305}
306
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200307static void sha1_update_wrap( void *ctx, const unsigned char *input,
308 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_sha1_update( (mbedtls_sha1_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000311}
312
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100313static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_sha1_finish( (mbedtls_sha1_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000316}
317
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200318static void *sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000319{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200320 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200321
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200322 if( ctx != NULL )
323 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200324
325 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000326}
327
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100328static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
331 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000332}
333
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100334static void sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100335{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_sha1_process( (mbedtls_sha1_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100337}
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339const mbedtls_md_info_t mbedtls_sha1_info = {
340 MBEDTLS_MD_SHA1,
Paul Bakker23986e52011-04-24 08:57:21 +0000341 "SHA1",
342 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100343 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000344 sha1_starts_wrap,
345 sha1_update_wrap,
346 sha1_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_sha1,
Paul Bakker23986e52011-04-24 08:57:21 +0000348 sha1_ctx_alloc,
349 sha1_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100350 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000351};
352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#endif /* MBEDTLS_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000354
355/*
356 * Wrappers for generic message digests
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000359
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100360static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000361{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000363}
364
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200365static void sha224_update_wrap( void *ctx, const unsigned char *input,
366 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_sha256_update( (mbedtls_sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000369}
370
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100371static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_sha256_finish( (mbedtls_sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000374}
375
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100376static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000377 unsigned char *output )
378{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_sha256( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000380}
381
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200382static void *sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000383{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200384 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
385
386 if( ctx != NULL )
387 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
388
389 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000390}
391
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100392static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
395 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000396}
397
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100398static void sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_sha256_process( (mbedtls_sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100401}
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403const mbedtls_md_info_t mbedtls_sha224_info = {
404 MBEDTLS_MD_SHA224,
Paul Bakker23986e52011-04-24 08:57:21 +0000405 "SHA224",
406 28,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100407 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000408 sha224_starts_wrap,
409 sha224_update_wrap,
410 sha224_finish_wrap,
411 sha224_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000412 sha224_ctx_alloc,
413 sha224_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100414 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000415};
416
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100417static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000420}
421
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100422static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000423 unsigned char *output )
424{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000426}
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428const mbedtls_md_info_t mbedtls_sha256_info = {
429 MBEDTLS_MD_SHA256,
Paul Bakker23986e52011-04-24 08:57:21 +0000430 "SHA256",
431 32,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100432 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000433 sha256_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200434 sha224_update_wrap,
435 sha224_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000436 sha256_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200437 sha224_ctx_alloc,
438 sha224_ctx_free,
439 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000440};
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#endif /* MBEDTLS_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#if defined(MBEDTLS_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000445
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100446static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000447{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000449}
450
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200451static void sha384_update_wrap( void *ctx, const unsigned char *input,
452 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_sha512_update( (mbedtls_sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000455}
456
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100457static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000458{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_sha512_finish( (mbedtls_sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000460}
461
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100462static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000463 unsigned char *output )
464{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000466}
467
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200468static void *sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000469{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200470 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
471
472 if( ctx != NULL )
473 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
474
475 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000476}
477
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100478static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000479{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_zeroize( ctx, sizeof( mbedtls_sha512_context ) );
481 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000482}
483
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100484static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100485{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_sha512_process( (mbedtls_sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100487}
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489const mbedtls_md_info_t mbedtls_sha384_info = {
490 MBEDTLS_MD_SHA384,
Paul Bakker23986e52011-04-24 08:57:21 +0000491 "SHA384",
492 48,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100493 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000494 sha384_starts_wrap,
495 sha384_update_wrap,
496 sha384_finish_wrap,
497 sha384_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000498 sha384_ctx_alloc,
499 sha384_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100500 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000501};
502
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100503static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000504{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000506}
507
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100508static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000509 unsigned char *output )
510{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000512}
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514const mbedtls_md_info_t mbedtls_sha512_info = {
515 MBEDTLS_MD_SHA512,
Paul Bakker23986e52011-04-24 08:57:21 +0000516 "SHA512",
517 64,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100518 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000519 sha512_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200520 sha384_update_wrap,
521 sha384_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000522 sha512_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200523 sha384_ctx_alloc,
524 sha384_ctx_free,
525 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000526};
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#endif /* MBEDTLS_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#endif /* MBEDTLS_MD_C */