blob: 7459db2faf9b59fbeeaf9f24452f7e62078ad27c [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 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02008 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-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útif744bd72020-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 * **********
Paul Bakker17373852011-01-06 14:20:01 +000049 */
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#endif
Paul Bakker17373852011-01-06 14:20:01 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000058
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020059#include "mbedtls/md_internal.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/md2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000063#endif
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/md4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000067#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000070#include "mbedtls/md5.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000071#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/ripemd160.h"
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +010075#endif
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/sha1.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000079#endif
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000082#include "mbedtls/sha256.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000083#endif
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000086#include "mbedtls/sha512.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000087#endif
Paul Bakker17373852011-01-06 14:20:01 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000090#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020091#else
Rich Evans00ab4702015-02-06 13:43:58 +000092#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020093#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020095#endif
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if defined(MBEDTLS_MD2_C)
Paul Bakker17373852011-01-06 14:20:01 +000098
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +010099static int md2_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000100{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100101 return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000102}
103
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100104static int md2_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200105 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000106{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100107 return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000108}
109
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100110static int md2_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000111{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100112 return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000113}
114
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200115static void *md2_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000116{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200117 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
118
119 if( ctx != NULL )
120 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
121
122 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000123}
124
125static void md2_ctx_free( void *ctx )
126{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200127 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000129}
130
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200131static void md2_clone_wrap( void *dst, const void *src )
132{
133 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
134 (const mbedtls_md2_context *) src );
135}
136
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100137static int md2_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100138{
139 ((void) data);
140
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100141 return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100142}
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144const mbedtls_md_info_t mbedtls_md2_info = {
145 MBEDTLS_MD_MD2,
Paul Bakker23986e52011-04-24 08:57:21 +0000146 "MD2",
147 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100148 16,
Paul Bakker23986e52011-04-24 08:57:21 +0000149 md2_starts_wrap,
150 md2_update_wrap,
151 md2_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100152 mbedtls_md2_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000153 md2_ctx_alloc,
154 md2_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200155 md2_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100156 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000157};
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#endif /* MBEDTLS_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +0000162
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100163static int md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000164{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100165 return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000166}
167
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100168static int md4_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200169 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000170{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100171 return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000172}
173
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100174static int md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000175{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100176 return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000177}
178
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100179static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000180{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200181 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
182
183 if( ctx != NULL )
184 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
185
186 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000187}
188
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100189static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000190{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200191 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000193}
194
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200195static void md4_clone_wrap( void *dst, const void *src )
196{
197 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100198 (const mbedtls_md4_context *) src );
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200199}
200
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100201static int md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100202{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100203 return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100204}
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206const mbedtls_md_info_t mbedtls_md4_info = {
207 MBEDTLS_MD_MD4,
Paul Bakker23986e52011-04-24 08:57:21 +0000208 "MD4",
209 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100210 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000211 md4_starts_wrap,
212 md4_update_wrap,
213 md4_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100214 mbedtls_md4_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000215 md4_ctx_alloc,
216 md4_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200217 md4_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100218 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000219};
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#endif /* MBEDTLS_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +0000224
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100225static int md5_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000226{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100227 return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000228}
229
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100230static int md5_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200231 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000232{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100233 return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000234}
235
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100236static int md5_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000237{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100238 return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000239}
240
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200241static void *md5_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000242{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200243 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
244
245 if( ctx != NULL )
246 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
247
248 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000249}
250
251static void md5_ctx_free( void *ctx )
252{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200253 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000255}
256
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200257static void md5_clone_wrap( void *dst, const void *src )
258{
259 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100260 (const mbedtls_md5_context *) src );
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200261}
262
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100263static int md5_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100264{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100265 return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100266}
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268const mbedtls_md_info_t mbedtls_md5_info = {
269 MBEDTLS_MD_MD5,
Paul Bakker23986e52011-04-24 08:57:21 +0000270 "MD5",
271 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100272 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000273 md5_starts_wrap,
274 md5_update_wrap,
275 md5_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100276 mbedtls_md5_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000277 md5_ctx_alloc,
278 md5_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200279 md5_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100280 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000281};
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#endif /* MBEDTLS_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100286
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100287static int ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100288{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100289 return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100290}
291
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100292static int ripemd160_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200293 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100294{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100295 return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100296 input, ilen ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100297}
298
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100299static int ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100300{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100301 return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100302 output ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100303}
304
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200305static void *ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100306{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200307 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200308
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200309 if( ctx != NULL )
310 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200311
312 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100313}
314
Paul Bakker61b699e2014-01-22 13:35:29 +0100315static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100316{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
318 mbedtls_free( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100319}
320
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200321static void ripemd160_clone_wrap( void *dst, const void *src )
322{
323 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
324 (const mbedtls_ripemd160_context *) src );
325}
326
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100327static int ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100328{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100329 return( mbedtls_internal_ripemd160_process(
330 (mbedtls_ripemd160_context *) ctx, data ) );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100331}
332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333const mbedtls_md_info_t mbedtls_ripemd160_info = {
334 MBEDTLS_MD_RIPEMD160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100335 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100336 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100337 64,
Paul Bakker61b699e2014-01-22 13:35:29 +0100338 ripemd160_starts_wrap,
339 ripemd160_update_wrap,
340 ripemd160_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100341 mbedtls_ripemd160_ret,
Paul Bakker61b699e2014-01-22 13:35:29 +0100342 ripemd160_ctx_alloc,
343 ripemd160_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200344 ripemd160_clone_wrap,
Paul Bakker61b699e2014-01-22 13:35:29 +0100345 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100346};
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#endif /* MBEDTLS_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350#if defined(MBEDTLS_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +0000351
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100352static int sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000353{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100354 return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000355}
356
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100357static int sha1_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200358 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000359{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100360 return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100361 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000362}
363
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100364static int sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000365{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100366 return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000367}
368
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200369static void *sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000370{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200371 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200372
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200373 if( ctx != NULL )
374 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200375
376 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000377}
378
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200379static void sha1_clone_wrap( void *dst, const void *src )
380{
381 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
382 (const mbedtls_sha1_context *) src );
383}
384
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100385static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
388 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000389}
390
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100391static int sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100392{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100393 return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
394 data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100395}
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397const mbedtls_md_info_t mbedtls_sha1_info = {
398 MBEDTLS_MD_SHA1,
Paul Bakker23986e52011-04-24 08:57:21 +0000399 "SHA1",
400 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100401 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000402 sha1_starts_wrap,
403 sha1_update_wrap,
404 sha1_finish_wrap,
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100405 mbedtls_sha1_ret,
Paul Bakker23986e52011-04-24 08:57:21 +0000406 sha1_ctx_alloc,
407 sha1_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200408 sha1_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100409 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000410};
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000413
414/*
415 * Wrappers for generic message digests
416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#if defined(MBEDTLS_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000418
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100419static int sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000420{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100421 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000422}
423
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100424static int sha224_update_wrap( void *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200425 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000426{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100427 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100428 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000429}
430
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100431static int sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000432{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100433 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100434 output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000435}
436
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100437static int sha224_wrap( const unsigned char *input, size_t ilen,
438 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000439{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100440 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000441}
442
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200443static void *sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000444{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200445 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
446
447 if( ctx != NULL )
448 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
449
450 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000451}
452
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100453static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000454{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200455 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000457}
458
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200459static void sha224_clone_wrap( void *dst, const void *src )
460{
461 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
462 (const mbedtls_sha256_context *) src );
463}
464
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100465static int sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100466{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100467 return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
468 data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100469}
470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471const mbedtls_md_info_t mbedtls_sha224_info = {
472 MBEDTLS_MD_SHA224,
Paul Bakker23986e52011-04-24 08:57:21 +0000473 "SHA224",
474 28,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100475 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000476 sha224_starts_wrap,
477 sha224_update_wrap,
478 sha224_finish_wrap,
479 sha224_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000480 sha224_ctx_alloc,
481 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200482 sha224_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100483 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000484};
485
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100486static int sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000487{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100488 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000489}
490
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100491static int sha256_wrap( const unsigned char *input, size_t ilen,
492 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000493{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100494 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000495}
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497const mbedtls_md_info_t mbedtls_sha256_info = {
498 MBEDTLS_MD_SHA256,
Paul Bakker23986e52011-04-24 08:57:21 +0000499 "SHA256",
500 32,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100501 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000502 sha256_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200503 sha224_update_wrap,
504 sha224_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000505 sha256_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200506 sha224_ctx_alloc,
507 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200508 sha224_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200509 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000510};
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#endif /* MBEDTLS_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#if defined(MBEDTLS_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000515
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100516static int sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000517{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100518 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000519}
520
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100521static int sha384_update_wrap( void *ctx, const unsigned char *input,
522 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000523{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100524 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100525 input, ilen ) );
Paul Bakker17373852011-01-06 14:20:01 +0000526}
527
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100528static int sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000529{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100530 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100531 output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000532}
533
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100534static int sha384_wrap( const unsigned char *input, size_t ilen,
535 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000536{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100537 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000538}
539
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200540static void *sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000541{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200542 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
543
544 if( ctx != NULL )
545 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
546
547 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000548}
549
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100550static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000551{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200552 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000554}
555
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200556static void sha384_clone_wrap( void *dst, const void *src )
557{
558 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
559 (const mbedtls_sha512_context *) src );
560}
561
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100562static int sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100563{
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100564 return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
565 data ) );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100566}
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568const mbedtls_md_info_t mbedtls_sha384_info = {
569 MBEDTLS_MD_SHA384,
Paul Bakker23986e52011-04-24 08:57:21 +0000570 "SHA384",
571 48,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100572 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000573 sha384_starts_wrap,
574 sha384_update_wrap,
575 sha384_finish_wrap,
576 sha384_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000577 sha384_ctx_alloc,
578 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200579 sha384_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100580 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000581};
582
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100583static int sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000584{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100585 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000586}
587
Andres Amaya Garcia5f872df2017-06-28 14:12:44 +0100588static int sha512_wrap( const unsigned char *input, size_t ilen,
589 unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000590{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100591 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000592}
593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594const mbedtls_md_info_t mbedtls_sha512_info = {
595 MBEDTLS_MD_SHA512,
Paul Bakker23986e52011-04-24 08:57:21 +0000596 "SHA512",
597 64,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100598 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000599 sha512_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200600 sha384_update_wrap,
601 sha384_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000602 sha512_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200603 sha384_ctx_alloc,
604 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200605 sha384_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200606 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000607};
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#endif /* MBEDTLS_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#endif /* MBEDTLS_MD_C */