blob: 341a9cf20f9846dd2bf11eb0fbc55518e6fa472f [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/**
Jerome Forissier79013242021-07-28 10:24:04 +02002 * \file md.c
Jens Wiklander817466c2018-05-22 13:49:31 +02003 *
4 * \brief Generic message digest wrapper for mbed TLS
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Jerome Forissier79013242021-07-28 10:24:04 +02008 * Copyright The Mbed TLS Contributors
9 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +020010 *
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.
Jens Wiklander817466c2018-05-22 13:49:31 +020022 */
23
Jerome Forissier79013242021-07-28 10:24:04 +020024#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020025
26#if defined(MBEDTLS_MD_C)
27
28#include "mbedtls/md.h"
29#include "mbedtls/md_internal.h"
Jens Wiklander3d3b0592019-03-20 15:30:29 +010030#include "mbedtls/platform_util.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020031#include "mbedtls/error.h"
32
33#include "mbedtls/md2.h"
34#include "mbedtls/md4.h"
35#include "mbedtls/md5.h"
36#include "mbedtls/ripemd160.h"
37#include "mbedtls/sha1.h"
38#include "mbedtls/sha256.h"
39#include "mbedtls/sha512.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020040
41#if defined(MBEDTLS_PLATFORM_C)
42#include "mbedtls/platform.h"
43#else
44#include <stdlib.h>
45#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
49#include <string.h>
50
51#if defined(MBEDTLS_FS_IO)
52#include <stdio.h>
53#endif
54
Jerome Forissier11fa71b2020-04-20 17:17:56 +020055#if defined(MBEDTLS_MD2_C)
56const mbedtls_md_info_t mbedtls_md2_info = {
57 "MD2",
58 MBEDTLS_MD_MD2,
59 16,
60 16,
61};
62#endif
63
64#if defined(MBEDTLS_MD4_C)
65const mbedtls_md_info_t mbedtls_md4_info = {
66 "MD4",
67 MBEDTLS_MD_MD4,
68 16,
69 64,
70};
71#endif
72
73#if defined(MBEDTLS_MD5_C)
74const mbedtls_md_info_t mbedtls_md5_info = {
75 "MD5",
76 MBEDTLS_MD_MD5,
77 16,
78 64,
79};
80#endif
81
82#if defined(MBEDTLS_RIPEMD160_C)
83const mbedtls_md_info_t mbedtls_ripemd160_info = {
84 "RIPEMD160",
85 MBEDTLS_MD_RIPEMD160,
86 20,
87 64,
88};
89#endif
90
91#if defined(MBEDTLS_SHA1_C)
92const mbedtls_md_info_t mbedtls_sha1_info = {
93 "SHA1",
94 MBEDTLS_MD_SHA1,
95 20,
96 64,
97};
98#endif
99
100#if defined(MBEDTLS_SHA256_C)
101const mbedtls_md_info_t mbedtls_sha224_info = {
102 "SHA224",
103 MBEDTLS_MD_SHA224,
104 28,
105 64,
106};
107
108const mbedtls_md_info_t mbedtls_sha256_info = {
109 "SHA256",
110 MBEDTLS_MD_SHA256,
111 32,
112 64,
113};
114#endif
115
116#if defined(MBEDTLS_SHA512_C)
117#if !defined(MBEDTLS_SHA512_NO_SHA384)
118const mbedtls_md_info_t mbedtls_sha384_info = {
119 "SHA384",
120 MBEDTLS_MD_SHA384,
121 48,
122 128,
123};
124#endif
125
126const mbedtls_md_info_t mbedtls_sha512_info = {
127 "SHA512",
128 MBEDTLS_MD_SHA512,
129 64,
130 128,
131};
132#endif
133
Jens Wiklander817466c2018-05-22 13:49:31 +0200134/*
135 * Reminder: update profiles in x509_crt.c when adding a new hash!
136 */
137static const int supported_digests[] = {
138
139#if defined(MBEDTLS_SHA512_C)
140 MBEDTLS_MD_SHA512,
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200141#if !defined(MBEDTLS_SHA512_NO_SHA384)
Jens Wiklander817466c2018-05-22 13:49:31 +0200142 MBEDTLS_MD_SHA384,
143#endif
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200144#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200145
146#if defined(MBEDTLS_SHA256_C)
147 MBEDTLS_MD_SHA256,
148 MBEDTLS_MD_SHA224,
149#endif
150
151#if defined(MBEDTLS_SHA1_C)
152 MBEDTLS_MD_SHA1,
153#endif
154
155#if defined(MBEDTLS_RIPEMD160_C)
156 MBEDTLS_MD_RIPEMD160,
157#endif
158
159#if defined(MBEDTLS_MD5_C)
160 MBEDTLS_MD_MD5,
161#endif
162
163#if defined(MBEDTLS_MD4_C)
164 MBEDTLS_MD_MD4,
165#endif
166
167#if defined(MBEDTLS_MD2_C)
168 MBEDTLS_MD_MD2,
169#endif
170
171 MBEDTLS_MD_NONE
172};
173
174const int *mbedtls_md_list( void )
175{
176 return( supported_digests );
177}
178
179const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
180{
181 if( NULL == md_name )
182 return( NULL );
183
184 /* Get the appropriate digest information */
185#if defined(MBEDTLS_MD2_C)
186 if( !strcmp( "MD2", md_name ) )
187 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
188#endif
189#if defined(MBEDTLS_MD4_C)
190 if( !strcmp( "MD4", md_name ) )
191 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
192#endif
193#if defined(MBEDTLS_MD5_C)
194 if( !strcmp( "MD5", md_name ) )
195 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
196#endif
197#if defined(MBEDTLS_RIPEMD160_C)
198 if( !strcmp( "RIPEMD160", md_name ) )
199 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
200#endif
201#if defined(MBEDTLS_SHA1_C)
202 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
203 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
204#endif
205#if defined(MBEDTLS_SHA256_C)
206 if( !strcmp( "SHA224", md_name ) )
207 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
208 if( !strcmp( "SHA256", md_name ) )
209 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
210#endif
211#if defined(MBEDTLS_SHA512_C)
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200212#if !defined(MBEDTLS_SHA512_NO_SHA384)
Jens Wiklander817466c2018-05-22 13:49:31 +0200213 if( !strcmp( "SHA384", md_name ) )
214 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200215#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200216 if( !strcmp( "SHA512", md_name ) )
217 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
218#endif
219 return( NULL );
220}
221
222const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
223{
224 switch( md_type )
225 {
226#if defined(MBEDTLS_MD2_C)
227 case MBEDTLS_MD_MD2:
228 return( &mbedtls_md2_info );
229#endif
230#if defined(MBEDTLS_MD4_C)
231 case MBEDTLS_MD_MD4:
232 return( &mbedtls_md4_info );
233#endif
234#if defined(MBEDTLS_MD5_C)
235 case MBEDTLS_MD_MD5:
236 return( &mbedtls_md5_info );
237#endif
238#if defined(MBEDTLS_RIPEMD160_C)
239 case MBEDTLS_MD_RIPEMD160:
240 return( &mbedtls_ripemd160_info );
241#endif
242#if defined(MBEDTLS_SHA1_C)
243 case MBEDTLS_MD_SHA1:
244 return( &mbedtls_sha1_info );
245#endif
246#if defined(MBEDTLS_SHA256_C)
247 case MBEDTLS_MD_SHA224:
248 return( &mbedtls_sha224_info );
249 case MBEDTLS_MD_SHA256:
250 return( &mbedtls_sha256_info );
251#endif
252#if defined(MBEDTLS_SHA512_C)
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200253#if !defined(MBEDTLS_SHA512_NO_SHA384)
Jens Wiklander817466c2018-05-22 13:49:31 +0200254 case MBEDTLS_MD_SHA384:
255 return( &mbedtls_sha384_info );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200256#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200257 case MBEDTLS_MD_SHA512:
258 return( &mbedtls_sha512_info );
259#endif
260 default:
261 return( NULL );
262 }
263}
264
265void mbedtls_md_init( mbedtls_md_context_t *ctx )
266{
267 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
268}
269
270void mbedtls_md_free( mbedtls_md_context_t *ctx )
271{
272 if( ctx == NULL || ctx->md_info == NULL )
273 return;
274
275 if( ctx->md_ctx != NULL )
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200276 {
277 switch( ctx->md_info->type )
278 {
279#if defined(MBEDTLS_MD2_C)
280 case MBEDTLS_MD_MD2:
281 mbedtls_md2_free( ctx->md_ctx );
282 break;
283#endif
284#if defined(MBEDTLS_MD4_C)
285 case MBEDTLS_MD_MD4:
286 mbedtls_md4_free( ctx->md_ctx );
287 break;
288#endif
289#if defined(MBEDTLS_MD5_C)
290 case MBEDTLS_MD_MD5:
291 mbedtls_md5_free( ctx->md_ctx );
292 break;
293#endif
294#if defined(MBEDTLS_RIPEMD160_C)
295 case MBEDTLS_MD_RIPEMD160:
296 mbedtls_ripemd160_free( ctx->md_ctx );
297 break;
298#endif
299#if defined(MBEDTLS_SHA1_C)
300 case MBEDTLS_MD_SHA1:
301 mbedtls_sha1_free( ctx->md_ctx );
302 break;
303#endif
304#if defined(MBEDTLS_SHA256_C)
305 case MBEDTLS_MD_SHA224:
306 case MBEDTLS_MD_SHA256:
307 mbedtls_sha256_free( ctx->md_ctx );
308 break;
309#endif
310#if defined(MBEDTLS_SHA512_C)
311#if !defined(MBEDTLS_SHA512_NO_SHA384)
312 case MBEDTLS_MD_SHA384:
313#endif
314 case MBEDTLS_MD_SHA512:
315 mbedtls_sha512_free( ctx->md_ctx );
316 break;
317#endif
318 default:
319 /* Shouldn't happen */
320 break;
321 }
322 mbedtls_free( ctx->md_ctx );
323 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200324
325 if( ctx->hmac_ctx != NULL )
326 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100327 mbedtls_platform_zeroize( ctx->hmac_ctx,
328 2 * ctx->md_info->block_size );
Jens Wiklander817466c2018-05-22 13:49:31 +0200329 mbedtls_free( ctx->hmac_ctx );
330 }
331
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100332 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200333}
334
335int mbedtls_md_clone( mbedtls_md_context_t *dst,
336 const mbedtls_md_context_t *src )
337{
338 if( dst == NULL || dst->md_info == NULL ||
339 src == NULL || src->md_info == NULL ||
340 dst->md_info != src->md_info )
341 {
342 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
343 }
344
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200345 switch( src->md_info->type )
346 {
347#if defined(MBEDTLS_MD2_C)
348 case MBEDTLS_MD_MD2:
349 mbedtls_md2_clone( dst->md_ctx, src->md_ctx );
350 break;
351#endif
352#if defined(MBEDTLS_MD4_C)
353 case MBEDTLS_MD_MD4:
354 mbedtls_md4_clone( dst->md_ctx, src->md_ctx );
355 break;
356#endif
357#if defined(MBEDTLS_MD5_C)
358 case MBEDTLS_MD_MD5:
359 mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
360 break;
361#endif
362#if defined(MBEDTLS_RIPEMD160_C)
363 case MBEDTLS_MD_RIPEMD160:
364 mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
365 break;
366#endif
367#if defined(MBEDTLS_SHA1_C)
368 case MBEDTLS_MD_SHA1:
369 mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
370 break;
371#endif
372#if defined(MBEDTLS_SHA256_C)
373 case MBEDTLS_MD_SHA224:
374 case MBEDTLS_MD_SHA256:
375 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
376 break;
377#endif
378#if defined(MBEDTLS_SHA512_C)
379#if !defined(MBEDTLS_SHA512_NO_SHA384)
380 case MBEDTLS_MD_SHA384:
381#endif
382 case MBEDTLS_MD_SHA512:
383 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
384 break;
385#endif
386 default:
387 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
388 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200389
Edison Ai12484fc2018-12-19 15:36:28 +0800390 if( dst->hmac_ctx != NULL && src->hmac_ctx != NULL )
391 memcpy( dst->hmac_ctx, src->hmac_ctx, 2 * src->md_info->block_size );
392
Jens Wiklander817466c2018-05-22 13:49:31 +0200393 return( 0 );
394}
395
396#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
397int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
398{
399 return mbedtls_md_setup( ctx, md_info, 1 );
400}
401#endif
402
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200403#define ALLOC( type ) \
404 do { \
405 ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
406 if( ctx->md_ctx == NULL ) \
407 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
408 mbedtls_##type##_init( ctx->md_ctx ); \
409 } \
410 while( 0 )
411
Jens Wiklander817466c2018-05-22 13:49:31 +0200412int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
413{
414 if( md_info == NULL || ctx == NULL )
415 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
416
Jerome Forissier79013242021-07-28 10:24:04 +0200417 ctx->md_info = md_info;
418 ctx->md_ctx = NULL;
419 ctx->hmac_ctx = NULL;
420
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200421 switch( md_info->type )
422 {
423#if defined(MBEDTLS_MD2_C)
424 case MBEDTLS_MD_MD2:
425 ALLOC( md2 );
426 break;
427#endif
428#if defined(MBEDTLS_MD4_C)
429 case MBEDTLS_MD_MD4:
430 ALLOC( md4 );
431 break;
432#endif
433#if defined(MBEDTLS_MD5_C)
434 case MBEDTLS_MD_MD5:
435 ALLOC( md5 );
436 break;
437#endif
438#if defined(MBEDTLS_RIPEMD160_C)
439 case MBEDTLS_MD_RIPEMD160:
440 ALLOC( ripemd160 );
441 break;
442#endif
443#if defined(MBEDTLS_SHA1_C)
444 case MBEDTLS_MD_SHA1:
445 ALLOC( sha1 );
446 break;
447#endif
448#if defined(MBEDTLS_SHA256_C)
449 case MBEDTLS_MD_SHA224:
450 case MBEDTLS_MD_SHA256:
451 ALLOC( sha256 );
452 break;
453#endif
454#if defined(MBEDTLS_SHA512_C)
455#if !defined(MBEDTLS_SHA512_NO_SHA384)
456 case MBEDTLS_MD_SHA384:
457#endif
458 case MBEDTLS_MD_SHA512:
459 ALLOC( sha512 );
460 break;
461#endif
462 default:
463 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
464 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200465
466 if( hmac != 0 )
467 {
468 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
469 if( ctx->hmac_ctx == NULL )
470 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200471 mbedtls_md_free( ctx );
Jens Wiklander817466c2018-05-22 13:49:31 +0200472 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
473 }
474 }
475
Jens Wiklander817466c2018-05-22 13:49:31 +0200476 return( 0 );
477}
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200478#undef ALLOC
Jens Wiklander817466c2018-05-22 13:49:31 +0200479
480int mbedtls_md_starts( mbedtls_md_context_t *ctx )
481{
482 if( ctx == NULL || ctx->md_info == NULL )
483 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
484
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200485 switch( ctx->md_info->type )
486 {
487#if defined(MBEDTLS_MD2_C)
488 case MBEDTLS_MD_MD2:
489 return( mbedtls_md2_starts_ret( ctx->md_ctx ) );
490#endif
491#if defined(MBEDTLS_MD4_C)
492 case MBEDTLS_MD_MD4:
493 return( mbedtls_md4_starts_ret( ctx->md_ctx ) );
494#endif
495#if defined(MBEDTLS_MD5_C)
496 case MBEDTLS_MD_MD5:
497 return( mbedtls_md5_starts_ret( ctx->md_ctx ) );
498#endif
499#if defined(MBEDTLS_RIPEMD160_C)
500 case MBEDTLS_MD_RIPEMD160:
501 return( mbedtls_ripemd160_starts_ret( ctx->md_ctx ) );
502#endif
503#if defined(MBEDTLS_SHA1_C)
504 case MBEDTLS_MD_SHA1:
505 return( mbedtls_sha1_starts_ret( ctx->md_ctx ) );
506#endif
507#if defined(MBEDTLS_SHA256_C)
508 case MBEDTLS_MD_SHA224:
509 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 1 ) );
510 case MBEDTLS_MD_SHA256:
511 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 0 ) );
512#endif
513#if defined(MBEDTLS_SHA512_C)
514#if !defined(MBEDTLS_SHA512_NO_SHA384)
515 case MBEDTLS_MD_SHA384:
516 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 1 ) );
517#endif
518 case MBEDTLS_MD_SHA512:
519 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 0 ) );
520#endif
521 default:
522 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
523 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200524}
525
526int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
527{
528 if( ctx == NULL || ctx->md_info == NULL )
529 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
530
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200531 switch( ctx->md_info->type )
532 {
533#if defined(MBEDTLS_MD2_C)
534 case MBEDTLS_MD_MD2:
535 return( mbedtls_md2_update_ret( ctx->md_ctx, input, ilen ) );
536#endif
537#if defined(MBEDTLS_MD4_C)
538 case MBEDTLS_MD_MD4:
539 return( mbedtls_md4_update_ret( ctx->md_ctx, input, ilen ) );
540#endif
541#if defined(MBEDTLS_MD5_C)
542 case MBEDTLS_MD_MD5:
543 return( mbedtls_md5_update_ret( ctx->md_ctx, input, ilen ) );
544#endif
545#if defined(MBEDTLS_RIPEMD160_C)
546 case MBEDTLS_MD_RIPEMD160:
547 return( mbedtls_ripemd160_update_ret( ctx->md_ctx, input, ilen ) );
548#endif
549#if defined(MBEDTLS_SHA1_C)
550 case MBEDTLS_MD_SHA1:
551 return( mbedtls_sha1_update_ret( ctx->md_ctx, input, ilen ) );
552#endif
553#if defined(MBEDTLS_SHA256_C)
554 case MBEDTLS_MD_SHA224:
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200555 case MBEDTLS_MD_SHA256:
556 return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
557#endif
558#if defined(MBEDTLS_SHA512_C)
559#if !defined(MBEDTLS_SHA512_NO_SHA384)
560 case MBEDTLS_MD_SHA384:
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200561#endif
562 case MBEDTLS_MD_SHA512:
563 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
564#endif
565 default:
566 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
567 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200568}
569
570int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
571{
572 if( ctx == NULL || ctx->md_info == NULL )
573 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
574
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200575 switch( ctx->md_info->type )
576 {
577#if defined(MBEDTLS_MD2_C)
578 case MBEDTLS_MD_MD2:
579 return( mbedtls_md2_finish_ret( ctx->md_ctx, output ) );
580#endif
581#if defined(MBEDTLS_MD4_C)
582 case MBEDTLS_MD_MD4:
583 return( mbedtls_md4_finish_ret( ctx->md_ctx, output ) );
584#endif
585#if defined(MBEDTLS_MD5_C)
586 case MBEDTLS_MD_MD5:
587 return( mbedtls_md5_finish_ret( ctx->md_ctx, output ) );
588#endif
589#if defined(MBEDTLS_RIPEMD160_C)
590 case MBEDTLS_MD_RIPEMD160:
591 return( mbedtls_ripemd160_finish_ret( ctx->md_ctx, output ) );
592#endif
593#if defined(MBEDTLS_SHA1_C)
594 case MBEDTLS_MD_SHA1:
595 return( mbedtls_sha1_finish_ret( ctx->md_ctx, output ) );
596#endif
597#if defined(MBEDTLS_SHA256_C)
598 case MBEDTLS_MD_SHA224:
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200599 case MBEDTLS_MD_SHA256:
600 return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
601#endif
602#if defined(MBEDTLS_SHA512_C)
603#if !defined(MBEDTLS_SHA512_NO_SHA384)
604 case MBEDTLS_MD_SHA384:
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200605#endif
606 case MBEDTLS_MD_SHA512:
607 return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
608#endif
609 default:
610 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
611 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200612}
613
614int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
615 unsigned char *output )
616{
617 if( md_info == NULL )
618 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
619
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200620 switch( md_info->type )
621 {
622#if defined(MBEDTLS_MD2_C)
623 case MBEDTLS_MD_MD2:
624 return( mbedtls_md2_ret( input, ilen, output ) );
625#endif
626#if defined(MBEDTLS_MD4_C)
627 case MBEDTLS_MD_MD4:
628 return( mbedtls_md4_ret( input, ilen, output ) );
629#endif
630#if defined(MBEDTLS_MD5_C)
631 case MBEDTLS_MD_MD5:
632 return( mbedtls_md5_ret( input, ilen, output ) );
633#endif
634#if defined(MBEDTLS_RIPEMD160_C)
635 case MBEDTLS_MD_RIPEMD160:
636 return( mbedtls_ripemd160_ret( input, ilen, output ) );
637#endif
638#if defined(MBEDTLS_SHA1_C)
639 case MBEDTLS_MD_SHA1:
640 return( mbedtls_sha1_ret( input, ilen, output ) );
641#endif
642#if defined(MBEDTLS_SHA256_C)
643 case MBEDTLS_MD_SHA224:
644 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
645 case MBEDTLS_MD_SHA256:
646 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
647#endif
648#if defined(MBEDTLS_SHA512_C)
649#if !defined(MBEDTLS_SHA512_NO_SHA384)
650 case MBEDTLS_MD_SHA384:
651 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
652#endif
653 case MBEDTLS_MD_SHA512:
654 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
655#endif
656 default:
657 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
658 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200659}
660
661#if defined(MBEDTLS_FS_IO)
662int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
663{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200665 FILE *f;
666 size_t n;
667 mbedtls_md_context_t ctx;
668 unsigned char buf[1024];
669
670 if( md_info == NULL )
671 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
672
673 if( ( f = fopen( path, "rb" ) ) == NULL )
674 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
675
676 mbedtls_md_init( &ctx );
677
678 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
679 goto cleanup;
680
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200681 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100682 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200683
684 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200685 if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100686 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200687
688 if( ferror( f ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200689 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100690 else
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200691 ret = mbedtls_md_finish( &ctx, output );
Jens Wiklander817466c2018-05-22 13:49:31 +0200692
693cleanup:
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100694 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200695 fclose( f );
696 mbedtls_md_free( &ctx );
697
698 return( ret );
699}
700#endif /* MBEDTLS_FS_IO */
701
702int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
703{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200705 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
706 unsigned char *ipad, *opad;
707 size_t i;
708
709 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
710 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
711
712 if( keylen > (size_t) ctx->md_info->block_size )
713 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200714 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100715 goto cleanup;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200716 if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100717 goto cleanup;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200718 if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100719 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200720
721 keylen = ctx->md_info->size;
722 key = sum;
723 }
724
725 ipad = (unsigned char *) ctx->hmac_ctx;
726 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
727
728 memset( ipad, 0x36, ctx->md_info->block_size );
729 memset( opad, 0x5C, ctx->md_info->block_size );
730
731 for( i = 0; i < keylen; i++ )
732 {
733 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
734 opad[i] = (unsigned char)( opad[i] ^ key[i] );
735 }
736
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200737 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100738 goto cleanup;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200739 if( ( ret = mbedtls_md_update( ctx, ipad,
740 ctx->md_info->block_size ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100741 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200742
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100743cleanup:
744 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200745
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100746 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +0200747}
748
749int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
750{
751 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
752 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
753
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200754 return( mbedtls_md_update( ctx, input, ilen ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200755}
756
757int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
758{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200759 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200760 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
761 unsigned char *opad;
762
763 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
764 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
765
766 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
767
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200768 if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100769 return( ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200770 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100771 return( ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200772 if( ( ret = mbedtls_md_update( ctx, opad,
773 ctx->md_info->block_size ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100774 return( ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200775 if( ( ret = mbedtls_md_update( ctx, tmp,
776 ctx->md_info->size ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100777 return( ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200778 return( mbedtls_md_finish( ctx, output ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200779}
780
781int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
782{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200783 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200784 unsigned char *ipad;
785
786 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
787 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
788
789 ipad = (unsigned char *) ctx->hmac_ctx;
790
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200791 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100792 return( ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200793 return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200794}
795
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100796int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
797 const unsigned char *key, size_t keylen,
798 const unsigned char *input, size_t ilen,
799 unsigned char *output )
Jens Wiklander817466c2018-05-22 13:49:31 +0200800{
801 mbedtls_md_context_t ctx;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200802 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200803
804 if( md_info == NULL )
805 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
806
807 mbedtls_md_init( &ctx );
808
809 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100810 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200811
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100812 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
813 goto cleanup;
814 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
815 goto cleanup;
816 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
817 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200818
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100819cleanup:
Jens Wiklander817466c2018-05-22 13:49:31 +0200820 mbedtls_md_free( &ctx );
821
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100822 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +0200823}
824
825int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
826{
827 if( ctx == NULL || ctx->md_info == NULL )
828 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
829
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200830 switch( ctx->md_info->type )
831 {
832#if defined(MBEDTLS_MD2_C)
833 case MBEDTLS_MD_MD2:
834 return( mbedtls_internal_md2_process( ctx->md_ctx ) );
835#endif
836#if defined(MBEDTLS_MD4_C)
837 case MBEDTLS_MD_MD4:
838 return( mbedtls_internal_md4_process( ctx->md_ctx, data ) );
839#endif
840#if defined(MBEDTLS_MD5_C)
841 case MBEDTLS_MD_MD5:
842 return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
843#endif
844#if defined(MBEDTLS_RIPEMD160_C)
845 case MBEDTLS_MD_RIPEMD160:
846 return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
847#endif
848#if defined(MBEDTLS_SHA1_C)
849 case MBEDTLS_MD_SHA1:
850 return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
851#endif
852#if defined(MBEDTLS_SHA256_C)
853 case MBEDTLS_MD_SHA224:
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200854 case MBEDTLS_MD_SHA256:
855 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
856#endif
857#if defined(MBEDTLS_SHA512_C)
858#if !defined(MBEDTLS_SHA512_NO_SHA384)
859 case MBEDTLS_MD_SHA384:
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200860#endif
861 case MBEDTLS_MD_SHA512:
862 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
863#endif
864 default:
865 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
866 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200867}
868
869unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
870{
871 if( md_info == NULL )
872 return( 0 );
873
874 return md_info->size;
875}
876
877mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
878{
879 if( md_info == NULL )
880 return( MBEDTLS_MD_NONE );
881
882 return md_info->type;
883}
884
885const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
886{
887 if( md_info == NULL )
888 return( NULL );
889
890 return md_info->name;
891}
892
893#endif /* MBEDTLS_MD_C */