blob: 7c737d87e98491f5c2e91b769fb6d94668d4e1a7 [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 *
11 * This file is provided under the Apache License 2.0, or the
12 * GNU General Public License v2.0 or later.
13 *
14 * **********
15 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020016 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000028 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020029 * **********
30 *
31 * **********
32 * GNU General Public License v2.0 or later:
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 *
48 * **********
49 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000050 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000051 */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#endif
Paul Bakker17373852011-01-06 14:20:01 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000060
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020061#include "mbedtls/md_internal.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/md2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000065#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/md4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000069#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000072#include "mbedtls/md5.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000073#endif
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#include "mbedtls/ripemd160.h"
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +010077#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/sha1.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000081#endif
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000084#include "mbedtls/sha256.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000085#endif
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000088#include "mbedtls/sha512.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000089#endif
Paul Bakker17373852011-01-06 14:20:01 +000090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000092#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020093#else
Rich Evans00ab4702015-02-06 13:43:58 +000094#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020095#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020097#endif
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if defined(MBEDTLS_MD2_C)
Paul Bakker17373852011-01-06 14:20:01 +0000100
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100101static int md2_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000102{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100103 return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000104}
105
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100106static int md2_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200107 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000108{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100109 return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000110}
111
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100112static int md2_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000113{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100114 return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000115}
116
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200117static void *md2_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000118{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200119 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
120
121 if( ctx != NULL )
122 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
123
124 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000125}
126
127static void md2_ctx_free( void *ctx )
128{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200129 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000131}
132
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200133static void md2_clone_wrap( void *dst, const void *src )
134{
135 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
136 (const mbedtls_md2_context *) src );
137}
138
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100139static int md2_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100140{
141 ((void) data);
142
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100143 return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100144}
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146const mbedtls_md_info_t mbedtls_md2_info = {
147 MBEDTLS_MD_MD2,
Paul Bakker23986e52011-04-24 08:57:21 +0000148 "MD2",
149 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100150 16,
Paul Bakker23986e52011-04-24 08:57:21 +0000151 md2_starts_wrap,
152 md2_update_wrap,
153 md2_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100154 mbedtls_md2_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000155 md2_ctx_alloc,
156 md2_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200157 md2_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100158 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000159};
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#endif /* MBEDTLS_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +0000164
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100165static int md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000166{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100167 return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000168}
169
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100170static int md4_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200171 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000172{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100173 return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000174}
175
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100176static int md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000177{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100178 return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000179}
180
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100181static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000182{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200183 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
184
185 if( ctx != NULL )
186 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
187
188 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000189}
190
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100191static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000192{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200193 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000195}
196
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200197static void md4_clone_wrap( void *dst, const void *src )
198{
199 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100200 (const mbedtls_md4_context *) src );
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200201}
202
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100203static int md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100204{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100205 return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100206}
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208const mbedtls_md_info_t mbedtls_md4_info = {
209 MBEDTLS_MD_MD4,
Paul Bakker23986e52011-04-24 08:57:21 +0000210 "MD4",
211 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100212 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000213 md4_starts_wrap,
214 md4_update_wrap,
215 md4_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100216 mbedtls_md4_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000217 md4_ctx_alloc,
218 md4_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200219 md4_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100220 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000221};
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#endif /* MBEDTLS_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +0000226
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100227static int md5_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000228{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100229 return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000230}
231
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100232static int md5_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200233 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000234{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100235 return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000236}
237
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100238static int md5_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000239{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100240 return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000241}
242
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200243static void *md5_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000244{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200245 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
246
247 if( ctx != NULL )
248 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
249
250 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000251}
252
253static void md5_ctx_free( void *ctx )
254{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200255 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000257}
258
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200259static void md5_clone_wrap( void *dst, const void *src )
260{
261 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100262 (const mbedtls_md5_context *) src );
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200263}
264
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100265static int md5_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100266{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100267 return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100268}
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270const mbedtls_md_info_t mbedtls_md5_info = {
271 MBEDTLS_MD_MD5,
Paul Bakker23986e52011-04-24 08:57:21 +0000272 "MD5",
273 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100274 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000275 md5_starts_wrap,
276 md5_update_wrap,
277 md5_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100278 mbedtls_md5_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000279 md5_ctx_alloc,
280 md5_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200281 md5_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100282 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000283};
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#endif /* MBEDTLS_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100288
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100289static int ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100290{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100291 return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100292}
293
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100294static int ripemd160_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200295 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100296{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100297 return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100298 input, ilen ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100299}
300
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100301static int ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100302{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100303 return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100304 output ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100305}
306
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200307static void *ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100308{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200309 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200310
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200311 if( ctx != NULL )
312 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200313
314 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100315}
316
Paul Bakker61b699e2014-01-22 13:35:29 +0100317static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
320 mbedtls_free( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100321}
322
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200323static void ripemd160_clone_wrap( void *dst, const void *src )
324{
325 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
326 (const mbedtls_ripemd160_context *) src );
327}
328
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100329static int ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100330{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100331 return( mbedtls_internal_ripemd160_process(
332 (mbedtls_ripemd160_context *) ctx, data ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100333}
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335const mbedtls_md_info_t mbedtls_ripemd160_info = {
336 MBEDTLS_MD_RIPEMD160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100337 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100338 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100339 64,
Paul Bakker61b699e2014-01-22 13:35:29 +0100340 ripemd160_starts_wrap,
341 ripemd160_update_wrap,
342 ripemd160_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100343 mbedtls_ripemd160_ret,
Paul Bakker61b699e2014-01-22 13:35:29 +0100344 ripemd160_ctx_alloc,
345 ripemd160_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200346 ripemd160_clone_wrap,
Paul Bakker61b699e2014-01-22 13:35:29 +0100347 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100348};
349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350#endif /* MBEDTLS_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +0000353
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100354static int sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000355{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100356 return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000357}
358
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100359static int sha1_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200360 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000361{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100362 return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100363 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000364}
365
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100366static int sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000367{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100368 return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000369}
370
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200371static void *sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000372{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200373 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200374
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200375 if( ctx != NULL )
376 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200377
378 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000379}
380
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200381static void sha1_clone_wrap( void *dst, const void *src )
382{
383 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
384 (const mbedtls_sha1_context *) src );
385}
386
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100387static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
390 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000391}
392
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100393static int sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100394{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100395 return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
396 data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399const mbedtls_md_info_t mbedtls_sha1_info = {
400 MBEDTLS_MD_SHA1,
Paul Bakker23986e52011-04-24 08:57:21 +0000401 "SHA1",
402 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100403 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000404 sha1_starts_wrap,
405 sha1_update_wrap,
406 sha1_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100407 mbedtls_sha1_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000408 sha1_ctx_alloc,
409 sha1_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200410 sha1_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100411 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000412};
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414#endif /* MBEDTLS_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000415
416/*
417 * Wrappers for generic message digests
418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000420
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100421static int sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000422{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100423 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000424}
425
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100426static int sha224_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200427 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000428{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100429 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100430 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000431}
432
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100433static int sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000434{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100435 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100436 output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000437}
438
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100439static int sha224_wrap( const unsigned char *input, size_t ilen,
440 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000441{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100442 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000443}
444
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200445static void *sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000446{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200447 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
448
449 if( ctx != NULL )
450 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
451
452 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000453}
454
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100455static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000456{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200457 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000459}
460
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200461static void sha224_clone_wrap( void *dst, const void *src )
462{
463 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
464 (const mbedtls_sha256_context *) src );
465}
466
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100467static int sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100468{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100469 return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
470 data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100471}
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473const mbedtls_md_info_t mbedtls_sha224_info = {
474 MBEDTLS_MD_SHA224,
Paul Bakker23986e52011-04-24 08:57:21 +0000475 "SHA224",
476 28,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100477 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000478 sha224_starts_wrap,
479 sha224_update_wrap,
480 sha224_finish_wrap,
481 sha224_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000482 sha224_ctx_alloc,
483 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200484 sha224_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100485 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000486};
487
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100488static int sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000489{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100490 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000491}
492
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100493static int sha256_wrap( const unsigned char *input, size_t ilen,
494 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000495{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100496 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000497}
498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499const mbedtls_md_info_t mbedtls_sha256_info = {
500 MBEDTLS_MD_SHA256,
Paul Bakker23986e52011-04-24 08:57:21 +0000501 "SHA256",
502 32,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100503 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000504 sha256_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200505 sha224_update_wrap,
506 sha224_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000507 sha256_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200508 sha224_ctx_alloc,
509 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200510 sha224_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200511 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000512};
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#endif /* MBEDTLS_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000517
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100518static int sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000519{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100520 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000521}
522
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100523static int sha384_update_wrap( void *ctx, const unsigned char *input,
524 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000525{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100526 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100527 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000528}
529
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100530static int sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000531{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100532 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100533 output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000534}
535
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100536static int sha384_wrap( const unsigned char *input, size_t ilen,
537 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000538{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100539 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000540}
541
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200542static void *sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000543{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200544 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
545
546 if( ctx != NULL )
547 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
548
549 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000550}
551
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100552static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000553{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200554 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000556}
557
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200558static void sha384_clone_wrap( void *dst, const void *src )
559{
560 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
561 (const mbedtls_sha512_context *) src );
562}
563
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100564static int sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100565{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100566 return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
567 data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100568}
569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570const mbedtls_md_info_t mbedtls_sha384_info = {
571 MBEDTLS_MD_SHA384,
Paul Bakker23986e52011-04-24 08:57:21 +0000572 "SHA384",
573 48,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100574 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000575 sha384_starts_wrap,
576 sha384_update_wrap,
577 sha384_finish_wrap,
578 sha384_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000579 sha384_ctx_alloc,
580 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200581 sha384_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100582 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000583};
584
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100585static int sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000586{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100587 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000588}
589
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100590static int sha512_wrap( const unsigned char *input, size_t ilen,
591 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000592{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100593 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000594}
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596const mbedtls_md_info_t mbedtls_sha512_info = {
597 MBEDTLS_MD_SHA512,
Paul Bakker23986e52011-04-24 08:57:21 +0000598 "SHA512",
599 64,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100600 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000601 sha512_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200602 sha384_update_wrap,
603 sha384_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000604 sha512_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200605 sha384_ctx_alloc,
606 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200607 sha384_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200608 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000609};
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#endif /* MBEDTLS_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#endif /* MBEDTLS_MD_C */