blob: 2cfcae200e226c9818164104a909b58e51a2d803 [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
2 * \file md_wrap.c
Manuel Pégourié-Gonnard1bab7d72015-07-13 09:06:18 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic message digest wrapper for mbed TLS
Paul Bakker17373852011-01-06 14:20:01 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker17373852011-01-06 14:20:01 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +000033
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020034#include "mbedtls/md_internal.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_MD2_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/md2.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000038#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/md4.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000042#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/md5.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000046#endif
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/ripemd160.h"
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +010050#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/sha1.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000054#endif
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/sha256.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000058#endif
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/sha512.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000062#endif
Paul Bakker17373852011-01-06 14:20:01 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020066#else
Rich Evans00ab4702015-02-06 13:43:58 +000067#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020068#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020070#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_MD2_C)
Paul Bakker17373852011-01-06 14:20:01 +000073
74static void md2_starts_wrap( void *ctx )
75{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_md2_starts( (mbedtls_md2_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +000077}
78
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020079static void md2_update_wrap( void *ctx, const unsigned char *input,
80 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +000081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_md2_update( (mbedtls_md2_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +000083}
84
85static void md2_finish_wrap( void *ctx, unsigned char *output )
86{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_md2_finish( (mbedtls_md2_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +000088}
89
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +020090static void *md2_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +000091{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +020092 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
93
94 if( ctx != NULL )
95 mbedtls_md2_init( (mbedtls_md2_context *) ctx );
96
97 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +000098}
99
100static void md2_ctx_free( void *ctx )
101{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200102 mbedtls_md2_free( (mbedtls_md2_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000104}
105
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200106static void md2_clone_wrap( void *dst, const void *src )
107{
108 mbedtls_md2_clone( (mbedtls_md2_context *) dst,
109 (const mbedtls_md2_context *) src );
110}
111
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100112static void md2_process_wrap( void *ctx, const unsigned char *data )
113{
114 ((void) data);
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_md2_process( (mbedtls_md2_context *) ctx );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100117}
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119const mbedtls_md_info_t mbedtls_md2_info = {
120 MBEDTLS_MD_MD2,
Paul Bakker23986e52011-04-24 08:57:21 +0000121 "MD2",
122 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100123 16,
Paul Bakker23986e52011-04-24 08:57:21 +0000124 md2_starts_wrap,
125 md2_update_wrap,
126 md2_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_md2,
Paul Bakker23986e52011-04-24 08:57:21 +0000128 md2_ctx_alloc,
129 md2_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200130 md2_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100131 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000132};
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#endif /* MBEDTLS_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +0000137
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100138static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_md4_starts( (mbedtls_md4_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000141}
142
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200143static void md4_update_wrap( void *ctx, const unsigned char *input,
144 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_md4_update( (mbedtls_md4_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000147}
148
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100149static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_md4_finish( (mbedtls_md4_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000152}
153
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100154static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000155{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200156 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
157
158 if( ctx != NULL )
159 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
160
161 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000162}
163
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100164static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000165{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200166 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000168}
169
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200170static void md4_clone_wrap( void *dst, const void *src )
171{
172 mbedtls_md4_clone( (mbedtls_md4_context *) dst,
173 (const mbedtls_md4_context *) src );
174}
175
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100176static void md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_md4_process( (mbedtls_md4_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100179}
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181const mbedtls_md_info_t mbedtls_md4_info = {
182 MBEDTLS_MD_MD4,
Paul Bakker23986e52011-04-24 08:57:21 +0000183 "MD4",
184 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100185 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000186 md4_starts_wrap,
187 md4_update_wrap,
188 md4_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_md4,
Paul Bakker23986e52011-04-24 08:57:21 +0000190 md4_ctx_alloc,
191 md4_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200192 md4_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100193 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000194};
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#endif /* MBEDTLS_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +0000199
200static void md5_starts_wrap( void *ctx )
201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_md5_starts( (mbedtls_md5_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000203}
204
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200205static void md5_update_wrap( void *ctx, const unsigned char *input,
206 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_md5_update( (mbedtls_md5_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000209}
210
211static void md5_finish_wrap( void *ctx, unsigned char *output )
212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_md5_finish( (mbedtls_md5_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000214}
215
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200216static void *md5_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000217{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200218 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
219
220 if( ctx != NULL )
221 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
222
223 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000224}
225
226static void md5_ctx_free( void *ctx )
227{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200228 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000230}
231
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200232static void md5_clone_wrap( void *dst, const void *src )
233{
234 mbedtls_md5_clone( (mbedtls_md5_context *) dst,
235 (const mbedtls_md5_context *) src );
236}
237
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100238static void md5_process_wrap( void *ctx, const unsigned char *data )
239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_md5_process( (mbedtls_md5_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100241}
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243const mbedtls_md_info_t mbedtls_md5_info = {
244 MBEDTLS_MD_MD5,
Paul Bakker23986e52011-04-24 08:57:21 +0000245 "MD5",
246 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100247 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000248 md5_starts_wrap,
249 md5_update_wrap,
250 md5_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_md5,
Paul Bakker23986e52011-04-24 08:57:21 +0000252 md5_ctx_alloc,
253 md5_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200254 md5_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100255 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000256};
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#endif /* MBEDTLS_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100261
Paul Bakker61b699e2014-01-22 13:35:29 +0100262static void ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100263{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_ripemd160_starts( (mbedtls_ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100265}
266
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200267static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
268 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_ripemd160_update( (mbedtls_ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100271}
272
Paul Bakker61b699e2014-01-22 13:35:29 +0100273static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_ripemd160_finish( (mbedtls_ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100276}
277
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200278static void *ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100279{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200280 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200281
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200282 if( ctx != NULL )
283 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200284
285 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100286}
287
Paul Bakker61b699e2014-01-22 13:35:29 +0100288static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100289{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
291 mbedtls_free( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100292}
293
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200294static void ripemd160_clone_wrap( void *dst, const void *src )
295{
296 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
297 (const mbedtls_ripemd160_context *) src );
298}
299
Paul Bakker61b699e2014-01-22 13:35:29 +0100300static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_ripemd160_process( (mbedtls_ripemd160_context *) ctx, data );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100303}
304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305const mbedtls_md_info_t mbedtls_ripemd160_info = {
306 MBEDTLS_MD_RIPEMD160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100307 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100308 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100309 64,
Paul Bakker61b699e2014-01-22 13:35:29 +0100310 ripemd160_starts_wrap,
311 ripemd160_update_wrap,
312 ripemd160_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_ripemd160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100314 ripemd160_ctx_alloc,
315 ripemd160_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200316 ripemd160_clone_wrap,
Paul Bakker61b699e2014-01-22 13:35:29 +0100317 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100318};
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#endif /* MBEDTLS_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +0000323
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100324static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000325{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_sha1_starts( (mbedtls_sha1_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000327}
328
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200329static void sha1_update_wrap( void *ctx, const unsigned char *input,
330 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_sha1_update( (mbedtls_sha1_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000333}
334
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100335static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000336{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_sha1_finish( (mbedtls_sha1_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000338}
339
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200340static void *sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000341{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200342 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200343
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200344 if( ctx != NULL )
345 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200346
347 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000348}
349
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200350static void sha1_clone_wrap( void *dst, const void *src )
351{
352 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
353 (const mbedtls_sha1_context *) src );
354}
355
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100356static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
359 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000360}
361
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100362static void sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100363{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_sha1_process( (mbedtls_sha1_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100365}
366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367const mbedtls_md_info_t mbedtls_sha1_info = {
368 MBEDTLS_MD_SHA1,
Paul Bakker23986e52011-04-24 08:57:21 +0000369 "SHA1",
370 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100371 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000372 sha1_starts_wrap,
373 sha1_update_wrap,
374 sha1_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_sha1,
Paul Bakker23986e52011-04-24 08:57:21 +0000376 sha1_ctx_alloc,
377 sha1_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200378 sha1_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100379 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000380};
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#endif /* MBEDTLS_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000383
384/*
385 * Wrappers for generic message digests
386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000388
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100389static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000392}
393
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200394static void sha224_update_wrap( void *ctx, const unsigned char *input,
395 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_sha256_update( (mbedtls_sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000398}
399
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100400static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_sha256_finish( (mbedtls_sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000403}
404
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100405static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000406 unsigned char *output )
407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_sha256( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000409}
410
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200411static void *sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000412{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200413 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
414
415 if( ctx != NULL )
416 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
417
418 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000419}
420
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100421static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000422{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200423 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000425}
426
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200427static void sha224_clone_wrap( void *dst, const void *src )
428{
429 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
430 (const mbedtls_sha256_context *) src );
431}
432
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100433static void sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100434{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_sha256_process( (mbedtls_sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100436}
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438const mbedtls_md_info_t mbedtls_sha224_info = {
439 MBEDTLS_MD_SHA224,
Paul Bakker23986e52011-04-24 08:57:21 +0000440 "SHA224",
441 28,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100442 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000443 sha224_starts_wrap,
444 sha224_update_wrap,
445 sha224_finish_wrap,
446 sha224_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000447 sha224_ctx_alloc,
448 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200449 sha224_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100450 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000451};
452
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100453static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000454{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000456}
457
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100458static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000459 unsigned char *output )
460{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000462}
463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464const mbedtls_md_info_t mbedtls_sha256_info = {
465 MBEDTLS_MD_SHA256,
Paul Bakker23986e52011-04-24 08:57:21 +0000466 "SHA256",
467 32,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100468 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000469 sha256_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200470 sha224_update_wrap,
471 sha224_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000472 sha256_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200473 sha224_ctx_alloc,
474 sha224_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200475 sha224_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200476 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000477};
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#endif /* MBEDTLS_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#if defined(MBEDTLS_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000482
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100483static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000486}
487
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200488static void sha384_update_wrap( void *ctx, const unsigned char *input,
489 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000490{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_sha512_update( (mbedtls_sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000492}
493
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100494static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000495{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_sha512_finish( (mbedtls_sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000497}
498
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100499static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000500 unsigned char *output )
501{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000503}
504
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200505static void *sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000506{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200507 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
508
509 if( ctx != NULL )
510 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
511
512 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000513}
514
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100515static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000516{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200517 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000519}
520
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200521static void sha384_clone_wrap( void *dst, const void *src )
522{
523 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
524 (const mbedtls_sha512_context *) src );
525}
526
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100527static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_sha512_process( (mbedtls_sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100530}
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532const mbedtls_md_info_t mbedtls_sha384_info = {
533 MBEDTLS_MD_SHA384,
Paul Bakker23986e52011-04-24 08:57:21 +0000534 "SHA384",
535 48,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100536 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000537 sha384_starts_wrap,
538 sha384_update_wrap,
539 sha384_finish_wrap,
540 sha384_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000541 sha384_ctx_alloc,
542 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200543 sha384_clone_wrap,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100544 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000545};
546
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100547static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000550}
551
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100552static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000553 unsigned char *output )
554{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000556}
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558const mbedtls_md_info_t mbedtls_sha512_info = {
559 MBEDTLS_MD_SHA512,
Paul Bakker23986e52011-04-24 08:57:21 +0000560 "SHA512",
561 64,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100562 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000563 sha512_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200564 sha384_update_wrap,
565 sha384_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000566 sha512_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200567 sha384_ctx_alloc,
568 sha384_ctx_free,
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200569 sha384_clone_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200570 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000571};
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#endif /* MBEDTLS_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_MD_C */