blob: 0f8132fbf32526f52f2fa8f39788efac5637cab3 [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
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200394#if !defined(MBEDTLS_SHA256_NO_SHA224)
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100395static int sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000396{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100397 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000398}
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200399#endif /* !MBEDTLS_SHA256_NO_SHA224 */
Paul Bakker17373852011-01-06 14:20:01 +0000400
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100401static int sha224_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200402 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000403{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100405 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000406}
407
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100408static int sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000409{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100410 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100411 output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000412}
413
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200414#if !defined(MBEDTLS_SHA256_NO_SHA224)
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100415static int sha224_wrap( const unsigned char *input, size_t ilen,
416 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000417{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100418 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000419}
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200420#endif /* !MBEDTLS_SHA256_NO_SHA224 */
Paul Bakker17373852011-01-06 14:20:01 +0000421
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200422static void *sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000423{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200424 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
425
426 if( ctx != NULL )
427 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
428
429 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000430}
431
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100432static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000433{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200434 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000436}
437
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200438static void sha224_clone_wrap( void *dst, const void *src )
439{
440 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
441 (const mbedtls_sha256_context *) src );
442}
443
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100444static int sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100445{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100446 return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
447 data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100448}
449
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200450#if !defined(MBEDTLS_SHA256_NO_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451const mbedtls_md_info_t mbedtls_sha224_info = {
452 MBEDTLS_MD_SHA224,
Paul Bakker23986e52011-04-24 08:57:21 +0000453 "SHA224",
454 28,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100455 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000456 sha224_starts_wrap,
457 sha224_update_wrap,
458 sha224_finish_wrap,
459 sha224_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000460 sha224_ctx_alloc,
461 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200462 sha224_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100463 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000464};
Manuel Pégourié-Gonnard394c5fb2019-07-16 15:57:36 +0200465#endif /* !MBEDTLS_SHA256_NO_SHA224 */
Paul Bakker17373852011-01-06 14:20:01 +0000466
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100467static int sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000468{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100469 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000470}
471
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100472static int sha256_wrap( const unsigned char *input, size_t ilen,
473 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000474{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100475 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478const mbedtls_md_info_t mbedtls_sha256_info = {
479 MBEDTLS_MD_SHA256,
Paul Bakker23986e52011-04-24 08:57:21 +0000480 "SHA256",
481 32,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100482 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000483 sha256_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200484 sha224_update_wrap,
485 sha224_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000486 sha256_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200487 sha224_ctx_alloc,
488 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200489 sha224_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200490 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000491};
492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#endif /* MBEDTLS_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#if defined(MBEDTLS_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000496
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100497static int sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000498{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100499 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000500}
501
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100502static int sha384_update_wrap( void *ctx, const unsigned char *input,
503 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000504{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100505 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100506 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000507}
508
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100509static int sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000510{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100511 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100512 output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000513}
514
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100515static int sha384_wrap( const unsigned char *input, size_t ilen,
516 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000517{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100518 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000519}
520
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200521static void *sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000522{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200523 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
524
525 if( ctx != NULL )
526 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
527
528 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000529}
530
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100531static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000532{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200533 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000535}
536
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200537static void sha384_clone_wrap( void *dst, const void *src )
538{
539 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
540 (const mbedtls_sha512_context *) src );
541}
542
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100543static int sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100544{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100545 return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
546 data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100547}
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549const mbedtls_md_info_t mbedtls_sha384_info = {
550 MBEDTLS_MD_SHA384,
Paul Bakker23986e52011-04-24 08:57:21 +0000551 "SHA384",
552 48,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100553 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000554 sha384_starts_wrap,
555 sha384_update_wrap,
556 sha384_finish_wrap,
557 sha384_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000558 sha384_ctx_alloc,
559 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200560 sha384_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100561 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000562};
563
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100564static int sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000565{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100566 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000567}
568
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100569static int sha512_wrap( const unsigned char *input, size_t ilen,
570 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000571{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100572 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000573}
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575const mbedtls_md_info_t mbedtls_sha512_info = {
576 MBEDTLS_MD_SHA512,
Paul Bakker23986e52011-04-24 08:57:21 +0000577 "SHA512",
578 64,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100579 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000580 sha512_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200581 sha384_update_wrap,
582 sha384_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000583 sha512_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200584 sha384_ctx_alloc,
585 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200586 sha384_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200587 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000588};
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#endif /* MBEDTLS_MD_C */