blob: c3adce30d88dc3fa8d2e03345a8aa5aea4776444 [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
2 * \file md_wrap.c
3
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
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100107static void md2_process_wrap( void *ctx, const unsigned char *data )
108{
109 ((void) data);
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_md2_process( (mbedtls_md2_context *) ctx );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100112}
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114const mbedtls_md_info_t mbedtls_md2_info = {
115 MBEDTLS_MD_MD2,
Paul Bakker23986e52011-04-24 08:57:21 +0000116 "MD2",
117 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100118 16,
Paul Bakker23986e52011-04-24 08:57:21 +0000119 md2_starts_wrap,
120 md2_update_wrap,
121 md2_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_md2,
Paul Bakker23986e52011-04-24 08:57:21 +0000123 md2_ctx_alloc,
124 md2_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100125 md2_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000126};
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#endif /* MBEDTLS_MD2_C */
Paul Bakker17373852011-01-06 14:20:01 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_MD4_C)
Paul Bakker17373852011-01-06 14:20:01 +0000131
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100132static void md4_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_md4_starts( (mbedtls_md4_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000135}
136
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200137static void md4_update_wrap( void *ctx, const unsigned char *input,
138 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_md4_update( (mbedtls_md4_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000141}
142
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100143static void md4_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_md4_finish( (mbedtls_md4_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000146}
147
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100148static void *md4_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000149{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200150 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
151
152 if( ctx != NULL )
153 mbedtls_md4_init( (mbedtls_md4_context *) ctx );
154
155 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000156}
157
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100158static void md4_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000159{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200160 mbedtls_md4_free( (mbedtls_md4_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000162}
163
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100164static void md4_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_md4_process( (mbedtls_md4_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100167}
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169const mbedtls_md_info_t mbedtls_md4_info = {
170 MBEDTLS_MD_MD4,
Paul Bakker23986e52011-04-24 08:57:21 +0000171 "MD4",
172 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100173 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000174 md4_starts_wrap,
175 md4_update_wrap,
176 md4_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_md4,
Paul Bakker23986e52011-04-24 08:57:21 +0000178 md4_ctx_alloc,
179 md4_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100180 md4_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000181};
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#endif /* MBEDTLS_MD4_C */
Paul Bakker17373852011-01-06 14:20:01 +0000184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#if defined(MBEDTLS_MD5_C)
Paul Bakker17373852011-01-06 14:20:01 +0000186
187static void md5_starts_wrap( void *ctx )
188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_md5_starts( (mbedtls_md5_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000190}
191
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200192static void md5_update_wrap( void *ctx, const unsigned char *input,
193 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_md5_update( (mbedtls_md5_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000196}
197
198static void md5_finish_wrap( void *ctx, unsigned char *output )
199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_md5_finish( (mbedtls_md5_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000201}
202
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200203static void *md5_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000204{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200205 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
206
207 if( ctx != NULL )
208 mbedtls_md5_init( (mbedtls_md5_context *) ctx );
209
210 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000211}
212
213static void md5_ctx_free( void *ctx )
214{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200215 mbedtls_md5_free( (mbedtls_md5_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000217}
218
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100219static void md5_process_wrap( void *ctx, const unsigned char *data )
220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_md5_process( (mbedtls_md5_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100222}
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224const mbedtls_md_info_t mbedtls_md5_info = {
225 MBEDTLS_MD_MD5,
Paul Bakker23986e52011-04-24 08:57:21 +0000226 "MD5",
227 16,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100228 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000229 md5_starts_wrap,
230 md5_update_wrap,
231 md5_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_md5,
Paul Bakker23986e52011-04-24 08:57:21 +0000233 md5_ctx_alloc,
234 md5_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100235 md5_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000236};
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#endif /* MBEDTLS_MD5_C */
Paul Bakker17373852011-01-06 14:20:01 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100241
Paul Bakker61b699e2014-01-22 13:35:29 +0100242static void ripemd160_starts_wrap( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_ripemd160_starts( (mbedtls_ripemd160_context *) ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100245}
246
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200247static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
248 size_t ilen )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100249{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_ripemd160_update( (mbedtls_ripemd160_context *) ctx, input, ilen );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100251}
252
Paul Bakker61b699e2014-01-22 13:35:29 +0100253static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100254{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_ripemd160_finish( (mbedtls_ripemd160_context *) ctx, output );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100256}
257
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200258static void *ripemd160_ctx_alloc( void )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100259{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200260 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200261
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200262 if( ctx != NULL )
263 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200264
265 return( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100266}
267
Paul Bakker61b699e2014-01-22 13:35:29 +0100268static void ripemd160_ctx_free( void *ctx )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
271 mbedtls_free( ctx );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100272}
273
Paul Bakker61b699e2014-01-22 13:35:29 +0100274static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_ripemd160_process( (mbedtls_ripemd160_context *) ctx, data );
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100277}
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279const mbedtls_md_info_t mbedtls_ripemd160_info = {
280 MBEDTLS_MD_RIPEMD160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100281 "RIPEMD160",
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100282 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100283 64,
Paul Bakker61b699e2014-01-22 13:35:29 +0100284 ripemd160_starts_wrap,
285 ripemd160_update_wrap,
286 ripemd160_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_ripemd160,
Paul Bakker61b699e2014-01-22 13:35:29 +0100288 ripemd160_ctx_alloc,
289 ripemd160_ctx_free,
290 ripemd160_process_wrap,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100291};
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_RIPEMD160_C */
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +0100294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_SHA1_C)
Paul Bakker17373852011-01-06 14:20:01 +0000296
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100297static void sha1_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000298{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_sha1_starts( (mbedtls_sha1_context *) ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000300}
301
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200302static void sha1_update_wrap( void *ctx, const unsigned char *input,
303 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_sha1_update( (mbedtls_sha1_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000306}
307
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100308static void sha1_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_sha1_finish( (mbedtls_sha1_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000311}
312
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200313static void *sha1_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000314{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200315 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200316
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200317 if( ctx != NULL )
318 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200319
320 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000321}
322
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100323static void sha1_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
326 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000327}
328
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100329static void sha1_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100330{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_sha1_process( (mbedtls_sha1_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100332}
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334const mbedtls_md_info_t mbedtls_sha1_info = {
335 MBEDTLS_MD_SHA1,
Paul Bakker23986e52011-04-24 08:57:21 +0000336 "SHA1",
337 20,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100338 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000339 sha1_starts_wrap,
340 sha1_update_wrap,
341 sha1_finish_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_sha1,
Paul Bakker23986e52011-04-24 08:57:21 +0000343 sha1_ctx_alloc,
344 sha1_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100345 sha1_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000346};
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#endif /* MBEDTLS_SHA1_C */
Paul Bakker17373852011-01-06 14:20:01 +0000349
350/*
351 * Wrappers for generic message digests
352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#if defined(MBEDTLS_SHA256_C)
Paul Bakker17373852011-01-06 14:20:01 +0000354
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100355static void sha224_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000356{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000358}
359
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200360static void sha224_update_wrap( void *ctx, const unsigned char *input,
361 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000362{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_sha256_update( (mbedtls_sha256_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000364}
365
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100366static void sha224_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_sha256_finish( (mbedtls_sha256_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000369}
370
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100371static void sha224_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000372 unsigned char *output )
373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_sha256( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000375}
376
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200377static void *sha224_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000378{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200379 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
380
381 if( ctx != NULL )
382 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
383
384 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000385}
386
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100387static void sha224_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000388{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200389 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000391}
392
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100393static void sha224_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_sha256_process( (mbedtls_sha256_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100396}
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398const mbedtls_md_info_t mbedtls_sha224_info = {
399 MBEDTLS_MD_SHA224,
Paul Bakker23986e52011-04-24 08:57:21 +0000400 "SHA224",
401 28,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100402 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000403 sha224_starts_wrap,
404 sha224_update_wrap,
405 sha224_finish_wrap,
406 sha224_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000407 sha224_ctx_alloc,
408 sha224_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100409 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000410};
411
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100412static void sha256_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000413{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000415}
416
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100417static void sha256_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000418 unsigned char *output )
419{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_sha256( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000421}
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423const mbedtls_md_info_t mbedtls_sha256_info = {
424 MBEDTLS_MD_SHA256,
Paul Bakker23986e52011-04-24 08:57:21 +0000425 "SHA256",
426 32,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100427 64,
Paul Bakker23986e52011-04-24 08:57:21 +0000428 sha256_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200429 sha224_update_wrap,
430 sha224_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000431 sha256_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200432 sha224_ctx_alloc,
433 sha224_ctx_free,
434 sha224_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000435};
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437#endif /* MBEDTLS_SHA256_C */
Paul Bakker17373852011-01-06 14:20:01 +0000438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439#if defined(MBEDTLS_SHA512_C)
Paul Bakker17373852011-01-06 14:20:01 +0000440
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100441static void sha384_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000444}
445
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200446static void sha384_update_wrap( void *ctx, const unsigned char *input,
447 size_t ilen )
Paul Bakker17373852011-01-06 14:20:01 +0000448{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_sha512_update( (mbedtls_sha512_context *) ctx, input, ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000450}
451
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100452static void sha384_finish_wrap( void *ctx, unsigned char *output )
Paul Bakker17373852011-01-06 14:20:01 +0000453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_sha512_finish( (mbedtls_sha512_context *) ctx, output );
Paul Bakker17373852011-01-06 14:20:01 +0000455}
456
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100457static void sha384_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000458 unsigned char *output )
459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_sha512( input, ilen, output, 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000461}
462
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200463static void *sha384_ctx_alloc( void )
Paul Bakker17373852011-01-06 14:20:01 +0000464{
Manuel Pégourié-Gonnardab593212015-06-23 11:30:47 +0200465 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
466
467 if( ctx != NULL )
468 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
469
470 return( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000471}
472
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100473static void sha384_ctx_free( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000474{
Manuel Pégourié-Gonnard71d296a2015-07-06 11:27:12 +0200475 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_free( ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000477}
478
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100479static void sha384_process_wrap( void *ctx, const unsigned char *data )
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100480{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_sha512_process( (mbedtls_sha512_context *) ctx, data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100482}
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484const mbedtls_md_info_t mbedtls_sha384_info = {
485 MBEDTLS_MD_SHA384,
Paul Bakker23986e52011-04-24 08:57:21 +0000486 "SHA384",
487 48,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100488 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000489 sha384_starts_wrap,
490 sha384_update_wrap,
491 sha384_finish_wrap,
492 sha384_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000493 sha384_ctx_alloc,
494 sha384_ctx_free,
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100495 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000496};
497
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100498static void sha512_starts_wrap( void *ctx )
Paul Bakker17373852011-01-06 14:20:01 +0000499{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000501}
502
Paul Bakkerd1df02a2013-03-13 10:31:31 +0100503static void sha512_wrap( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000504 unsigned char *output )
505{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_sha512( input, ilen, output, 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000507}
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509const mbedtls_md_info_t mbedtls_sha512_info = {
510 MBEDTLS_MD_SHA512,
Paul Bakker23986e52011-04-24 08:57:21 +0000511 "SHA512",
512 64,
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100513 128,
Paul Bakker23986e52011-04-24 08:57:21 +0000514 sha512_starts_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200515 sha384_update_wrap,
516 sha384_finish_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000517 sha512_wrap,
Manuel Pégourié-Gonnard496f24e2015-06-23 11:34:35 +0200518 sha384_ctx_alloc,
519 sha384_ctx_free,
520 sha384_process_wrap,
Paul Bakker17373852011-01-06 14:20:01 +0000521};
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#endif /* MBEDTLS_SHA512_C */
Paul Bakker17373852011-01-06 14:20:01 +0000524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#endif /* MBEDTLS_MD_C */