blob: 32f087197657e586ee113eca22307cd1ee84831b [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
2 * \file md_wrap.c
Manuel Pégourié-Gonnard1bab7d72015-07-13 09:06:18 +01003 *
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é-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker17373852011-01-06 14:20:01 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000033
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020034#include "mbedtls/md_internal.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/md2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000038#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/md4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000042#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/md5.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000046#endif
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/ripemd160.h"
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +010050#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/sha1.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000054#endif
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/sha256.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000058#endif
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/sha512.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000062#endif
Paul Bakker17373852011-01-06 14:20:01 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020066#else
Rich Evans00ab4702015-02-06 13:43:58 +000067#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020068#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020070#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_MD2_C)
Paul Bakker17373852011-01-06 14:20:01 +000073
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +010074static int md2_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +000075{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010076 return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +000077}
78
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +010079static int md2_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020080 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000081{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010082 return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +000083}
84
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +010085static int md2_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +000086{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010087 return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +000088}
89
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +020090static void *md2_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +000091{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +020092 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
93
94 if( ctx != NULL )
95 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
96
97 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +000098}
99
100static void md2_ctx_free( void *ctx )
101{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200102 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000104}
105
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200106static void md2_clone_wrap( void *dst, const void *src )
107{
108 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
109 (const mbedtls_md2_context *) src );
110}
111
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100112static int md2_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100113{
114 ((void) data);
115
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100116 return( mbedtls_internal_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,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100127 mbedtls_md2_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000128 md2_ctx_alloc,
129 md2_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200130 md2_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100131 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000132};
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#endif /* MBEDTLS_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +0000137
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100138static int md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000139{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100140 return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000141}
142
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100143static int md4_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200144 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000145{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100146 return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000147}
148
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100149static int md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000150{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100151 return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000152}
153
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100154static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000155{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200156 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
157
158 if( ctx != NULL )
159 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
160
161 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000162}
163
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100164static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000165{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200166 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000168}
169
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200170static void md4_clone_wrap( void *dst, const void *src )
171{
172 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100173 (const mbedtls_md4_context *) src );
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200174}
175
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100176static int md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100177{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100178 return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100179}
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181const mbedtls_md_info_t mbedtls_md4_info = {
182 MBEDTLS_MD_MD4,
Paul Bakker23986e52011-04-24 08:57:21 +0000183 "MD4",
184 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100185 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000186 md4_starts_wrap,
187 md4_update_wrap,
188 md4_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100189 mbedtls_md4_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000190 md4_ctx_alloc,
191 md4_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200192 md4_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100193 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000194};
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#endif /* MBEDTLS_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +0000199
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100200static int md5_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000201{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100202 return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000203}
204
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100205static int md5_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200206 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000207{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100208 return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000209}
210
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100211static int md5_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000212{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100213 return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000214}
215
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200216static void *md5_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000217{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200218 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
219
220 if( ctx != NULL )
221 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
222
223 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000224}
225
226static void md5_ctx_free( void *ctx )
227{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200228 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000230}
231
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200232static void md5_clone_wrap( void *dst, const void *src )
233{
234 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100235 (const mbedtls_md5_context *) src );
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200236}
237
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100238static int md5_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100239{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100240 return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100241}
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243const mbedtls_md_info_t mbedtls_md5_info = {
244 MBEDTLS_MD_MD5,
Paul Bakker23986e52011-04-24 08:57:21 +0000245 "MD5",
246 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100247 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000248 md5_starts_wrap,
249 md5_update_wrap,
250 md5_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100251 mbedtls_md5_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000252 md5_ctx_alloc,
253 md5_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200254 md5_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100255 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000256};
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#endif /* MBEDTLS_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100261
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100262static int ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100263{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100264 return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100265}
266
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100267static int ripemd160_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200268 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100269{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100270 return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100271 input, ilen ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100272}
273
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100274static int ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100275{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100276 return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100277 output ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100278}
279
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200280static void *ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100281{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200282 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200283
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200284 if( ctx != NULL )
285 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200286
287 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100288}
289
Paul Bakker61b699e2014-01-22 13:35:29 +0100290static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
293 mbedtls_free( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100294}
295
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200296static void ripemd160_clone_wrap( void *dst, const void *src )
297{
298 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
299 (const mbedtls_ripemd160_context *) src );
300}
301
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100302static int ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100303{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100304 return( mbedtls_internal_ripemd160_process(
305 (mbedtls_ripemd160_context *) ctx, data ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100306}
307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308const mbedtls_md_info_t mbedtls_ripemd160_info = {
309 MBEDTLS_MD_RIPEMD160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100310 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100311 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100312 64,
Paul Bakker61b699e2014-01-22 13:35:29 +0100313 ripemd160_starts_wrap,
314 ripemd160_update_wrap,
315 ripemd160_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100316 mbedtls_ripemd160_ret,
Paul Bakker61b699e2014-01-22 13:35:29 +0100317 ripemd160_ctx_alloc,
318 ripemd160_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200319 ripemd160_clone_wrap,
Paul Bakker61b699e2014-01-22 13:35:29 +0100320 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100321};
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* MBEDTLS_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#if defined(MBEDTLS_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +0000326
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100327static int sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000328{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100329 return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000330}
331
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100332static int sha1_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200333 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000334{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100335 return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100336 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000337}
338
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100339static int sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000340{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100341 return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000342}
343
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200344static void *sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000345{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200346 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200347
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200348 if( ctx != NULL )
349 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200350
351 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000352}
353
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200354static void sha1_clone_wrap( void *dst, const void *src )
355{
356 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
357 (const mbedtls_sha1_context *) src );
358}
359
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100360static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000361{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
363 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000364}
365
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100366static int sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100367{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100368 return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
369 data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100370}
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372const mbedtls_md_info_t mbedtls_sha1_info = {
373 MBEDTLS_MD_SHA1,
Paul Bakker23986e52011-04-24 08:57:21 +0000374 "SHA1",
375 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100376 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000377 sha1_starts_wrap,
378 sha1_update_wrap,
379 sha1_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100380 mbedtls_sha1_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000381 sha1_ctx_alloc,
382 sha1_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200383 sha1_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100384 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000385};
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#endif /* MBEDTLS_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000388
389/*
390 * Wrappers for generic message digests
391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000393
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100394static int sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000395{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100396 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000397}
398
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100399static int sha224_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200400 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000401{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100403 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000404}
405
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100406static int sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000407{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100408 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100409 output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000410}
411
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100412static int sha224_wrap( const unsigned char *input, size_t ilen,
413 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000414{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100415 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000416}
417
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200418static void *sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000419{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200420 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
421
422 if( ctx != NULL )
423 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
424
425 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000426}
427
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100428static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000429{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200430 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000432}
433
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200434static void sha224_clone_wrap( void *dst, const void *src )
435{
436 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
437 (const mbedtls_sha256_context *) src );
438}
439
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100440static int sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100441{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100442 return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
443 data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100444}
445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446const mbedtls_md_info_t mbedtls_sha224_info = {
447 MBEDTLS_MD_SHA224,
Paul Bakker23986e52011-04-24 08:57:21 +0000448 "SHA224",
449 28,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100450 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000451 sha224_starts_wrap,
452 sha224_update_wrap,
453 sha224_finish_wrap,
454 sha224_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000455 sha224_ctx_alloc,
456 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200457 sha224_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100458 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000459};
460
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100461static int sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000462{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100463 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000464}
465
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100466static int sha256_wrap( const unsigned char *input, size_t ilen,
467 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000468{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100469 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000470}
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472const mbedtls_md_info_t mbedtls_sha256_info = {
473 MBEDTLS_MD_SHA256,
Paul Bakker23986e52011-04-24 08:57:21 +0000474 "SHA256",
475 32,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100476 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000477 sha256_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200478 sha224_update_wrap,
479 sha224_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000480 sha256_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200481 sha224_ctx_alloc,
482 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200483 sha224_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200484 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000485};
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#endif /* MBEDTLS_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#if defined(MBEDTLS_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000490
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100491static int sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000492{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100493 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000494}
495
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100496static int sha384_update_wrap( void *ctx, const unsigned char *input,
497 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000498{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100499 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100500 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000501}
502
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100503static int sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000504{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100505 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100506 output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000507}
508
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100509static int sha384_wrap( const unsigned char *input, size_t ilen,
510 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000511{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100512 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000513}
514
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200515static void *sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000516{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200517 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
518
519 if( ctx != NULL )
520 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
521
522 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000523}
524
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100525static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000526{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200527 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000529}
530
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200531static void sha384_clone_wrap( void *dst, const void *src )
532{
533 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
534 (const mbedtls_sha512_context *) src );
535}
536
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100537static int sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100538{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100539 return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
540 data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100541}
542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543const mbedtls_md_info_t mbedtls_sha384_info = {
544 MBEDTLS_MD_SHA384,
Paul Bakker23986e52011-04-24 08:57:21 +0000545 "SHA384",
546 48,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100547 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000548 sha384_starts_wrap,
549 sha384_update_wrap,
550 sha384_finish_wrap,
551 sha384_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000552 sha384_ctx_alloc,
553 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200554 sha384_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100555 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000556};
557
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100558static int sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000559{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100560 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000561}
562
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100563static int sha512_wrap( const unsigned char *input, size_t ilen,
564 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000565{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100566 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000567}
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569const mbedtls_md_info_t mbedtls_sha512_info = {
570 MBEDTLS_MD_SHA512,
Paul Bakker23986e52011-04-24 08:57:21 +0000571 "SHA512",
572 64,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100573 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000574 sha512_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200575 sha384_update_wrap,
576 sha384_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000577 sha512_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200578 sha384_ctx_alloc,
579 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200580 sha384_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200581 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000582};
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#endif /* MBEDTLS_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#endif /* MBEDTLS_MD_C */