blob: f0e85f0ccfdadfbe937781efb1b26439b413b299 [file] [log] [blame]
Edison Ai7b079202018-02-28 15:01:47 +08001// SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02002/**
3 * \file mbedtls_md.c
4 *
5 * \brief Generic message digest wrapper for mbed TLS
6 *
7 * \author Adriaan de Jong <dejong@fox-it.com>
8 *
9 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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.
22 *
23 * This file is part of mbed TLS (https://tls.mbed.org)
24 */
25
26#if !defined(MBEDTLS_CONFIG_FILE)
27#include "mbedtls/config.h"
28#else
29#include MBEDTLS_CONFIG_FILE
30#endif
31
32#if defined(MBEDTLS_MD_C)
33
34#include "mbedtls/md.h"
35#include "mbedtls/md_internal.h"
Jens Wiklander50a57cf2019-03-13 10:41:54 +010036#include "mbedtls/platform_util.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020037
38#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
44#endif
45
46#include <string.h>
47
48#if defined(MBEDTLS_FS_IO)
49#include <stdio.h>
50#endif
51
Jens Wiklander817466c2018-05-22 13:49:31 +020052/*
53 * Reminder: update profiles in x509_crt.c when adding a new hash!
54 */
55static const int supported_digests[] = {
56
57#if defined(MBEDTLS_SHA512_C)
58 MBEDTLS_MD_SHA512,
59 MBEDTLS_MD_SHA384,
60#endif
61
62#if defined(MBEDTLS_SHA256_C)
63 MBEDTLS_MD_SHA256,
64 MBEDTLS_MD_SHA224,
65#endif
66
67#if defined(MBEDTLS_SHA1_C)
68 MBEDTLS_MD_SHA1,
69#endif
70
71#if defined(MBEDTLS_RIPEMD160_C)
72 MBEDTLS_MD_RIPEMD160,
73#endif
74
75#if defined(MBEDTLS_MD5_C)
76 MBEDTLS_MD_MD5,
77#endif
78
79#if defined(MBEDTLS_MD4_C)
80 MBEDTLS_MD_MD4,
81#endif
82
83#if defined(MBEDTLS_MD2_C)
84 MBEDTLS_MD_MD2,
85#endif
86
87 MBEDTLS_MD_NONE
88};
89
90const int *mbedtls_md_list( void )
91{
92 return( supported_digests );
93}
94
95const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
96{
97 if( NULL == md_name )
98 return( NULL );
99
100 /* Get the appropriate digest information */
101#if defined(MBEDTLS_MD2_C)
102 if( !strcmp( "MD2", md_name ) )
103 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
104#endif
105#if defined(MBEDTLS_MD4_C)
106 if( !strcmp( "MD4", md_name ) )
107 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
108#endif
109#if defined(MBEDTLS_MD5_C)
110 if( !strcmp( "MD5", md_name ) )
111 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
112#endif
113#if defined(MBEDTLS_RIPEMD160_C)
114 if( !strcmp( "RIPEMD160", md_name ) )
115 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
116#endif
117#if defined(MBEDTLS_SHA1_C)
118 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
119 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
120#endif
121#if defined(MBEDTLS_SHA256_C)
122 if( !strcmp( "SHA224", md_name ) )
123 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
124 if( !strcmp( "SHA256", md_name ) )
125 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
126#endif
127#if defined(MBEDTLS_SHA512_C)
128 if( !strcmp( "SHA384", md_name ) )
129 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
130 if( !strcmp( "SHA512", md_name ) )
131 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
132#endif
133 return( NULL );
134}
135
136const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
137{
138 switch( md_type )
139 {
140#if defined(MBEDTLS_MD2_C)
141 case MBEDTLS_MD_MD2:
142 return( &mbedtls_md2_info );
143#endif
144#if defined(MBEDTLS_MD4_C)
145 case MBEDTLS_MD_MD4:
146 return( &mbedtls_md4_info );
147#endif
148#if defined(MBEDTLS_MD5_C)
149 case MBEDTLS_MD_MD5:
150 return( &mbedtls_md5_info );
151#endif
152#if defined(MBEDTLS_RIPEMD160_C)
153 case MBEDTLS_MD_RIPEMD160:
154 return( &mbedtls_ripemd160_info );
155#endif
156#if defined(MBEDTLS_SHA1_C)
157 case MBEDTLS_MD_SHA1:
158 return( &mbedtls_sha1_info );
159#endif
160#if defined(MBEDTLS_SHA256_C)
161 case MBEDTLS_MD_SHA224:
162 return( &mbedtls_sha224_info );
163 case MBEDTLS_MD_SHA256:
164 return( &mbedtls_sha256_info );
165#endif
166#if defined(MBEDTLS_SHA512_C)
167 case MBEDTLS_MD_SHA384:
168 return( &mbedtls_sha384_info );
169 case MBEDTLS_MD_SHA512:
170 return( &mbedtls_sha512_info );
171#endif
172 default:
173 return( NULL );
174 }
175}
176
177void mbedtls_md_init( mbedtls_md_context_t *ctx )
178{
179 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
180}
181
182void mbedtls_md_free( mbedtls_md_context_t *ctx )
183{
184 if( ctx == NULL || ctx->md_info == NULL )
185 return;
186
187 if( ctx->md_ctx != NULL )
188 ctx->md_info->ctx_free_func( ctx->md_ctx );
189
190 if( ctx->hmac_ctx != NULL )
191 {
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100192 mbedtls_platform_zeroize( ctx->hmac_ctx,
193 2 * ctx->md_info->block_size );
Jens Wiklander817466c2018-05-22 13:49:31 +0200194 mbedtls_free( ctx->hmac_ctx );
195 }
196
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100197 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200198}
199
200int mbedtls_md_clone( mbedtls_md_context_t *dst,
201 const mbedtls_md_context_t *src )
202{
203 if( dst == NULL || dst->md_info == NULL ||
204 src == NULL || src->md_info == NULL ||
205 dst->md_info != src->md_info )
206 {
207 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
208 }
209
210 dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
211
Edison Aiac347342018-12-19 15:36:28 +0800212 if( dst->hmac_ctx != NULL && src->hmac_ctx != NULL )
213 memcpy( dst->hmac_ctx, src->hmac_ctx, 2 * src->md_info->block_size );
214
Jens Wiklander817466c2018-05-22 13:49:31 +0200215 return( 0 );
216}
217
218#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
219int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
220{
221 return mbedtls_md_setup( ctx, md_info, 1 );
222}
223#endif
224
225int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
226{
227 if( md_info == NULL || ctx == NULL )
228 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
229
230 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
231 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
232
233 if( hmac != 0 )
234 {
235 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
236 if( ctx->hmac_ctx == NULL )
237 {
238 md_info->ctx_free_func( ctx->md_ctx );
239 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
240 }
241 }
242
243 ctx->md_info = md_info;
244
245 return( 0 );
246}
247
248int mbedtls_md_starts( mbedtls_md_context_t *ctx )
249{
250 if( ctx == NULL || ctx->md_info == NULL )
251 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
252
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100253 return( ctx->md_info->starts_func( ctx->md_ctx ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200254}
255
256int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
257{
258 if( ctx == NULL || ctx->md_info == NULL )
259 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
260
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100261 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200262}
263
264int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
265{
266 if( ctx == NULL || ctx->md_info == NULL )
267 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
268
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100269 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200270}
271
272int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
273 unsigned char *output )
274{
275 if( md_info == NULL )
276 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
277
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100278 return( md_info->digest_func( input, ilen, output ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200279}
280
281#if defined(MBEDTLS_FS_IO)
282int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
283{
284 int ret;
285 FILE *f;
286 size_t n;
287 mbedtls_md_context_t ctx;
288 unsigned char buf[1024];
289
290 if( md_info == NULL )
291 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
292
293 if( ( f = fopen( path, "rb" ) ) == NULL )
294 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
295
296 mbedtls_md_init( &ctx );
297
298 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
299 goto cleanup;
300
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100301 if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
302 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200303
304 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100305 if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
306 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200307
308 if( ferror( f ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200309 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100310 else
311 ret = md_info->finish_func( ctx.md_ctx, output );
Jens Wiklander817466c2018-05-22 13:49:31 +0200312
313cleanup:
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100314 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200315 fclose( f );
316 mbedtls_md_free( &ctx );
317
318 return( ret );
319}
320#endif /* MBEDTLS_FS_IO */
321
322int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
323{
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100324 int ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200325 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
326 unsigned char *ipad, *opad;
327 size_t i;
328
329 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
330 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
331
332 if( keylen > (size_t) ctx->md_info->block_size )
333 {
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100334 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
335 goto cleanup;
336 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
337 goto cleanup;
338 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
339 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200340
341 keylen = ctx->md_info->size;
342 key = sum;
343 }
344
345 ipad = (unsigned char *) ctx->hmac_ctx;
346 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
347
348 memset( ipad, 0x36, ctx->md_info->block_size );
349 memset( opad, 0x5C, ctx->md_info->block_size );
350
351 for( i = 0; i < keylen; i++ )
352 {
353 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
354 opad[i] = (unsigned char)( opad[i] ^ key[i] );
355 }
356
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100357 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
358 goto cleanup;
359 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
360 ctx->md_info->block_size ) ) != 0 )
361 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200362
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100363cleanup:
364 mbedtls_platform_zeroize( sum, sizeof( sum ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200365
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100366 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +0200367}
368
369int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
370{
371 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
372 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
373
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100374 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200375}
376
377int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
378{
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100379 int ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200380 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
381 unsigned char *opad;
382
383 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
384 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
385
386 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
387
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100388 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
389 return( ret );
390 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
391 return( ret );
392 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
393 ctx->md_info->block_size ) ) != 0 )
394 return( ret );
395 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
396 ctx->md_info->size ) ) != 0 )
397 return( ret );
398 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200399}
400
401int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
402{
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100403 int ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200404 unsigned char *ipad;
405
406 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
407 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
408
409 ipad = (unsigned char *) ctx->hmac_ctx;
410
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100411 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
412 return( ret );
413 return( ctx->md_info->update_func( ctx->md_ctx, ipad,
414 ctx->md_info->block_size ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200415}
416
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100417int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
418 const unsigned char *key, size_t keylen,
419 const unsigned char *input, size_t ilen,
420 unsigned char *output )
Jens Wiklander817466c2018-05-22 13:49:31 +0200421{
422 mbedtls_md_context_t ctx;
423 int ret;
424
425 if( md_info == NULL )
426 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
427
428 mbedtls_md_init( &ctx );
429
430 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100431 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200432
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100433 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
434 goto cleanup;
435 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
436 goto cleanup;
437 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
438 goto cleanup;
Jens Wiklander817466c2018-05-22 13:49:31 +0200439
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100440cleanup:
Jens Wiklander817466c2018-05-22 13:49:31 +0200441 mbedtls_md_free( &ctx );
442
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100443 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +0200444}
445
446int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
447{
448 if( ctx == NULL || ctx->md_info == NULL )
449 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
450
Jens Wiklander50a57cf2019-03-13 10:41:54 +0100451 return( ctx->md_info->process_func( ctx->md_ctx, data ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200452}
453
454unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
455{
456 if( md_info == NULL )
457 return( 0 );
458
459 return md_info->size;
460}
461
462mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
463{
464 if( md_info == NULL )
465 return( MBEDTLS_MD_NONE );
466
467 return md_info->type;
468}
469
470const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
471{
472 if( md_info == NULL )
473 return( NULL );
474
475 return md_info->name;
476}
477
478#endif /* MBEDTLS_MD_C */