blob: e85d69bd75f05bcee7c4263602fd47cd6ce7f877 [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é-Gonnarda658a402015-01-23 09:45:19 +00008 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker17373852011-01-06 14:20:01 +00009 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000010 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000011 *
Paul Bakker17373852011-01-06 14:20:01 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker17373852011-01-06 14:20:01 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000034
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/md_internal.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/md2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/md4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/md5.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000047#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/ripemd160.h"
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +010051#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/sha1.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000055#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/sha256.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000059#endif
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/sha512.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000063#endif
Paul Bakker17373852011-01-06 14:20:01 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020067#else
Rich Evans00ab4702015-02-06 13:43:58 +000068#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020069#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020071#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_MD2_C)
Paul Bakker17373852011-01-06 14:20:01 +000074
75static void md2_starts_wrap( void *ctx )
76{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_md2_starts( (mbedtls_md2_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +000078}
79
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020080static void md2_update_wrap( void *ctx, const unsigned char *input,
81 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000082{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_md2_update( (mbedtls_md2_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +000084}
85
86static void md2_finish_wrap( void *ctx, unsigned char *output )
87{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_md2_finish( (mbedtls_md2_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +000089}
90
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +020091static void *md2_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +000092{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +020093 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
94
95 if( ctx != NULL )
96 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
97
98 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +000099}
100
101static void md2_ctx_free( void *ctx )
102{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200103 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000105}
106
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200107static void md2_clone_wrap( void *dst, const void *src )
108{
109 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
110 (const mbedtls_md2_context *) src );
111}
112
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100113static void md2_process_wrap( void *ctx, const unsigned char *data )
114{
115 ((void) data);
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_md2_process( (mbedtls_md2_context *) ctx );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100118}
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120const mbedtls_md_info_t mbedtls_md2_info = {
121 MBEDTLS_MD_MD2,
Paul Bakker23986e52011-04-24 08:57:21 +0000122 "MD2",
123 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100124 16,
Paul Bakker23986e52011-04-24 08:57:21 +0000125 md2_starts_wrap,
126 md2_update_wrap,
127 md2_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_md2,
Paul Bakker23986e52011-04-24 08:57:21 +0000129 md2_ctx_alloc,
130 md2_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200131 md2_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100132 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000133};
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#endif /* MBEDTLS_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if defined(MBEDTLS_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +0000138
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100139static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_md4_starts( (mbedtls_md4_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000142}
143
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200144static void md4_update_wrap( void *ctx, const unsigned char *input,
145 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_md4_update( (mbedtls_md4_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000148}
149
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100150static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_md4_finish( (mbedtls_md4_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000153}
154
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100155static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000156{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200157 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
158
159 if( ctx != NULL )
160 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
161
162 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000163}
164
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100165static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000166{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200167 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000169}
170
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200171static void md4_clone_wrap( void *dst, const void *src )
172{
173 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
174 (const mbedtls_md4_context *) src );
175}
176
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100177static void md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100178{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_md4_process( (mbedtls_md4_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100180}
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182const mbedtls_md_info_t mbedtls_md4_info = {
183 MBEDTLS_MD_MD4,
Paul Bakker23986e52011-04-24 08:57:21 +0000184 "MD4",
185 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100186 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000187 md4_starts_wrap,
188 md4_update_wrap,
189 md4_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_md4,
Paul Bakker23986e52011-04-24 08:57:21 +0000191 md4_ctx_alloc,
192 md4_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200193 md4_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100194 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000195};
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#endif /* MBEDTLS_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +0000200
201static void md5_starts_wrap( void *ctx )
202{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_md5_starts( (mbedtls_md5_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000204}
205
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200206static void md5_update_wrap( void *ctx, const unsigned char *input,
207 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_md5_update( (mbedtls_md5_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000210}
211
212static void md5_finish_wrap( void *ctx, unsigned char *output )
213{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_md5_finish( (mbedtls_md5_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000215}
216
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200217static void *md5_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000218{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200219 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
220
221 if( ctx != NULL )
222 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
223
224 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000225}
226
227static void md5_ctx_free( void *ctx )
228{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200229 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000231}
232
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200233static void md5_clone_wrap( void *dst, const void *src )
234{
235 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
236 (const mbedtls_md5_context *) src );
237}
238
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100239static void md5_process_wrap( void *ctx, const unsigned char *data )
240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_md5_process( (mbedtls_md5_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100242}
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244const mbedtls_md_info_t mbedtls_md5_info = {
245 MBEDTLS_MD_MD5,
Paul Bakker23986e52011-04-24 08:57:21 +0000246 "MD5",
247 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100248 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000249 md5_starts_wrap,
250 md5_update_wrap,
251 md5_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_md5,
Paul Bakker23986e52011-04-24 08:57:21 +0000253 md5_ctx_alloc,
254 md5_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200255 md5_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100256 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000257};
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#endif /* MBEDTLS_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100262
Paul Bakker61b699e2014-01-22 13:35:29 +0100263static void ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100264{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_ripemd160_starts( (mbedtls_ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100266}
267
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200268static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
269 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_ripemd160_update( (mbedtls_ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100272}
273
Paul Bakker61b699e2014-01-22 13:35:29 +0100274static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_ripemd160_finish( (mbedtls_ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100277}
278
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200279static void *ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100280{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200281 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200282
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200283 if( ctx != NULL )
284 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200285
286 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100287}
288
Paul Bakker61b699e2014-01-22 13:35:29 +0100289static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
292 mbedtls_free( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100293}
294
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200295static void ripemd160_clone_wrap( void *dst, const void *src )
296{
297 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
298 (const mbedtls_ripemd160_context *) src );
299}
300
Paul Bakker61b699e2014-01-22 13:35:29 +0100301static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_ripemd160_process( (mbedtls_ripemd160_context *) ctx, data );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100304}
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306const mbedtls_md_info_t mbedtls_ripemd160_info = {
307 MBEDTLS_MD_RIPEMD160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100308 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100309 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100310 64,
Paul Bakker61b699e2014-01-22 13:35:29 +0100311 ripemd160_starts_wrap,
312 ripemd160_update_wrap,
313 ripemd160_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_ripemd160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100315 ripemd160_ctx_alloc,
316 ripemd160_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200317 ripemd160_clone_wrap,
Paul Bakker61b699e2014-01-22 13:35:29 +0100318 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100319};
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#endif /* MBEDTLS_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +0000324
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100325static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_sha1_starts( (mbedtls_sha1_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000328}
329
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200330static void sha1_update_wrap( void *ctx, const unsigned char *input,
331 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000332{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_sha1_update( (mbedtls_sha1_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000334}
335
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100336static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_sha1_finish( (mbedtls_sha1_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000339}
340
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200341static void *sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000342{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200343 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200344
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200345 if( ctx != NULL )
346 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200347
348 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000349}
350
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200351static void sha1_clone_wrap( void *dst, const void *src )
352{
353 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
354 (const mbedtls_sha1_context *) src );
355}
356
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100357static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000358{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
360 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000361}
362
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100363static void sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100364{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_sha1_process( (mbedtls_sha1_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100366}
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368const mbedtls_md_info_t mbedtls_sha1_info = {
369 MBEDTLS_MD_SHA1,
Paul Bakker23986e52011-04-24 08:57:21 +0000370 "SHA1",
371 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100372 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000373 sha1_starts_wrap,
374 sha1_update_wrap,
375 sha1_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_sha1,
Paul Bakker23986e52011-04-24 08:57:21 +0000377 sha1_ctx_alloc,
378 sha1_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200379 sha1_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100380 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000381};
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#endif /* MBEDTLS_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000384
385/*
386 * Wrappers for generic message digests
387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#if defined(MBEDTLS_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000389
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100390static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000391{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000393}
394
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200395static void sha224_update_wrap( void *ctx, const unsigned char *input,
396 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000397{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_sha256_update( (mbedtls_sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000399}
400
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100401static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_sha256_finish( (mbedtls_sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000404}
405
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100406static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000407 unsigned char *output )
408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_sha256( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000410}
411
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200412static void *sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000413{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200414 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
415
416 if( ctx != NULL )
417 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
418
419 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000420}
421
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100422static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000423{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200424 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000426}
427
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200428static void sha224_clone_wrap( void *dst, const void *src )
429{
430 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
431 (const mbedtls_sha256_context *) src );
432}
433
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100434static void sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100435{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_sha256_process( (mbedtls_sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100437}
438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439const mbedtls_md_info_t mbedtls_sha224_info = {
440 MBEDTLS_MD_SHA224,
Paul Bakker23986e52011-04-24 08:57:21 +0000441 "SHA224",
442 28,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100443 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000444 sha224_starts_wrap,
445 sha224_update_wrap,
446 sha224_finish_wrap,
447 sha224_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000448 sha224_ctx_alloc,
449 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200450 sha224_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100451 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000452};
453
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100454static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000455{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000457}
458
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100459static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000460 unsigned char *output )
461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000463}
464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465const mbedtls_md_info_t mbedtls_sha256_info = {
466 MBEDTLS_MD_SHA256,
Paul Bakker23986e52011-04-24 08:57:21 +0000467 "SHA256",
468 32,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100469 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000470 sha256_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200471 sha224_update_wrap,
472 sha224_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000473 sha256_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200474 sha224_ctx_alloc,
475 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200476 sha224_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200477 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000478};
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#endif /* MBEDTLS_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#if defined(MBEDTLS_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000483
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100484static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000485{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000487}
488
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200489static void sha384_update_wrap( void *ctx, const unsigned char *input,
490 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000491{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_sha512_update( (mbedtls_sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000493}
494
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100495static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_sha512_finish( (mbedtls_sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000498}
499
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100500static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000501 unsigned char *output )
502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000504}
505
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200506static void *sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000507{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200508 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
509
510 if( ctx != NULL )
511 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
512
513 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000514}
515
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100516static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000517{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200518 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000520}
521
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200522static void sha384_clone_wrap( void *dst, const void *src )
523{
524 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
525 (const mbedtls_sha512_context *) src );
526}
527
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100528static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100529{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_sha512_process( (mbedtls_sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100531}
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533const mbedtls_md_info_t mbedtls_sha384_info = {
534 MBEDTLS_MD_SHA384,
Paul Bakker23986e52011-04-24 08:57:21 +0000535 "SHA384",
536 48,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100537 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000538 sha384_starts_wrap,
539 sha384_update_wrap,
540 sha384_finish_wrap,
541 sha384_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000542 sha384_ctx_alloc,
543 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200544 sha384_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100545 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000546};
547
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100548static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000551}
552
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100553static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000554 unsigned char *output )
555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000557}
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559const mbedtls_md_info_t mbedtls_sha512_info = {
560 MBEDTLS_MD_SHA512,
Paul Bakker23986e52011-04-24 08:57:21 +0000561 "SHA512",
562 64,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100563 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000564 sha512_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200565 sha384_update_wrap,
566 sha384_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000567 sha512_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200568 sha384_ctx_alloc,
569 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200570 sha384_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200571 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000572};
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_MD_C */