blob: 8cbdbe4c75e7826c8c73873af1c6713f56610b34 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/md.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker17373852011-01-06 14:20:01 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_MD_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010011void mbedtls_md_process( )
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010012{
13 const int *md_type_ptr;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014 const mbedtls_md_info_t *info;
15 mbedtls_md_context_t ctx;
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020016 unsigned char buf[150];
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018 mbedtls_md_init( &ctx );
Thomas Daubney0bd08e72022-02-17 13:38:26 +000019 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020020
21 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022 * Very minimal testing of mbedtls_md_process, just make sure the various
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020023 * xxx_process_wrap() function pointers are valid. (Testing that they
24 * indeed do the right thing whould require messing with the internal
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025 * state of the underlying mbedtls_md/sha context.)
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020026 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027 * Also tests that mbedtls_md_list() only returns valid MDs.
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031 info = mbedtls_md_info_from_type( *md_type_ptr );
Paul Bakker94b916c2014-04-17 16:07:20 +020032 TEST_ASSERT( info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 TEST_ASSERT( mbedtls_md_setup( &ctx, info, 0 ) == 0 );
34 TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == 0 );
35 mbedtls_md_free( &ctx );
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020036 }
Paul Bakkerbd51b262014-07-10 15:26:12 +020037
38exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_md_free( &ctx );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010040}
41/* END_CASE */
42
43/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010044void md_null_args( )
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_md_context_t ctx;
47 const mbedtls_md_info_t *info = mbedtls_md_info_from_type( *( mbedtls_md_list() ) );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020048 unsigned char buf[1] = { 0 };
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 TEST_ASSERT( mbedtls_md_get_size( NULL ) == 0 );
53 TEST_ASSERT( mbedtls_md_get_type( NULL ) == MBEDTLS_MD_NONE );
54 TEST_ASSERT( mbedtls_md_get_name( NULL ) == NULL );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 TEST_ASSERT( mbedtls_md_info_from_string( NULL ) == NULL );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 TEST_ASSERT( mbedtls_md_setup( &ctx, NULL, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
59 TEST_ASSERT( mbedtls_md_setup( NULL, info, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 TEST_ASSERT( mbedtls_md_starts( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
62 TEST_ASSERT( mbedtls_md_starts( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 TEST_ASSERT( mbedtls_md_update( NULL, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
65 TEST_ASSERT( mbedtls_md_update( &ctx, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 TEST_ASSERT( mbedtls_md_finish( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
68 TEST_ASSERT( mbedtls_md_finish( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 TEST_ASSERT( mbedtls_md( NULL, buf, 1, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020071
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020072#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 TEST_ASSERT( mbedtls_md_file( NULL, "", buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020074#endif
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 TEST_ASSERT( mbedtls_md_hmac_starts( NULL, buf, 1 )
77 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
78 TEST_ASSERT( mbedtls_md_hmac_starts( &ctx, buf, 1 )
79 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 TEST_ASSERT( mbedtls_md_hmac_update( NULL, buf, 1 )
82 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
83 TEST_ASSERT( mbedtls_md_hmac_update( &ctx, buf, 1 )
84 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 TEST_ASSERT( mbedtls_md_hmac_finish( NULL, buf )
87 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
88 TEST_ASSERT( mbedtls_md_hmac_finish( &ctx, buf )
89 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 TEST_ASSERT( mbedtls_md_hmac_reset( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
92 TEST_ASSERT( mbedtls_md_hmac_reset( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 TEST_ASSERT( mbedtls_md_hmac( NULL, buf, 1, buf, 1, buf )
95 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 TEST_ASSERT( mbedtls_md_process( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
98 TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard19d644b2015-03-26 12:42:35 +010099
100 /* Ok, this is not NULL arg but NULL return... */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 TEST_ASSERT( mbedtls_md_info_from_type( MBEDTLS_MD_NONE ) == NULL );
102 TEST_ASSERT( mbedtls_md_info_from_string( "no such md" ) == NULL );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +0200103}
104/* END_CASE */
105
106/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100107void md_info( int md_type, char * md_name, int md_size )
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100110 const int *md_type_ptr;
111 int found;
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakker94b916c2014-04-17 16:07:20 +0200114 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 TEST_ASSERT( md_info == mbedtls_md_info_from_string( md_name ) );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 TEST_ASSERT( mbedtls_md_get_type( md_info ) == (mbedtls_md_type_t) md_type );
118 TEST_ASSERT( mbedtls_md_get_size( md_info ) == (unsigned char) md_size );
119 TEST_ASSERT( strcmp( mbedtls_md_get_name( md_info ), md_name ) == 0 );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100120
121 found = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100123 if( *md_type_ptr == md_type )
124 found = 1;
125 TEST_ASSERT( found == 1 );
126}
127/* END_CASE */
128
129/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100130void md_text( char * text_md_name, char * text_src_string,
Ronald Cronac6ae352020-06-26 14:33:03 +0200131 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000132{
133 char md_name[100];
134 unsigned char src_str[1000];
Paul Bakker17373852011-01-06 14:20:01 +0000135 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000137
Paul Bakker5c57e022016-07-19 13:31:41 +0100138 memset( md_name, 0x00, 100 );
139 memset( src_str, 0x00, 1000 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100140 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000141
Paul Bakker5c57e022016-07-19 13:31:41 +0100142 strncpy( (char *) src_str, text_src_string, sizeof( src_str ) - 1 );
143 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 md_info = mbedtls_md_info_from_string(md_name);
Paul Bakker17373852011-01-06 14:20:01 +0000145 TEST_ASSERT( md_info != NULL );
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, strlen( (char *) src_str ), output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000148
Ronald Cronac6ae352020-06-26 14:33:03 +0200149 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200150 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200151 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000152}
Paul Bakker33b43f12013-08-20 11:48:36 +0200153/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000154
Paul Bakker33b43f12013-08-20 11:48:36 +0200155/* BEGIN_CASE */
Ronald Cronac6ae352020-06-26 14:33:03 +0200156void md_hex( char * text_md_name, data_t * src_str, data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000157{
158 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000159 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000161
Paul Bakker5c57e022016-07-19 13:31:41 +0100162 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100163 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000164
Paul Bakker5c57e022016-07-19 13:31:41 +0100165 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
166 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000167 TEST_ASSERT( md_info != NULL );
168
Azim Khand30ca132017-06-09 04:32:58 +0100169 TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str->x, src_str->len, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000170
Paul Bakker17373852011-01-06 14:20:01 +0000171
Ronald Cronac6ae352020-06-26 14:33:03 +0200172 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200173 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200174 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000175}
Paul Bakker33b43f12013-08-20 11:48:36 +0200176/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000177
Paul Bakker33b43f12013-08-20 11:48:36 +0200178/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100179void md_text_multi( char * text_md_name, char * text_src_string,
Ronald Cronac6ae352020-06-26 14:33:03 +0200180 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000181{
182 char md_name[100];
183 unsigned char src_str[1000];
Paul Bakker17373852011-01-06 14:20:01 +0000184 unsigned char output[100];
Paul Bakkere35afa22016-07-13 17:09:14 +0100185 int halfway, len;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100188 mbedtls_md_context_t ctx, ctx_copy;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_md_init( &ctx );
Paul Bakker97c53c22016-07-13 17:20:22 +0100191 mbedtls_md_init( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000192
Paul Bakker5c57e022016-07-19 13:31:41 +0100193 memset( md_name, 0x00, 100 );
194 memset( src_str, 0x00, 1000 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100195 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000196
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200197 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
198 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakkere35afa22016-07-13 17:09:14 +0100199 len = strlen( (char *) src_str );
200 halfway = len / 2;
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 md_info = mbedtls_md_info_from_string(md_name);
Paul Bakker17373852011-01-06 14:20:01 +0000203 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100205 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000208 TEST_ASSERT ( ctx.md_ctx != NULL );
Paul Bakkere35afa22016-07-13 17:09:14 +0100209 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, halfway ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100210 TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
211
Paul Bakkere35afa22016-07-13 17:09:14 +0100212 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str + halfway, len - halfway ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200214 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200215 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200216 hash->len) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000217
Paul Bakker97c53c22016-07-13 17:20:22 +0100218 /* Test clone */
Paul Bakker5c57e022016-07-19 13:31:41 +0100219 memset( output, 0x00, 100 );
Paul Bakker97c53c22016-07-13 17:20:22 +0100220
221 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str + halfway, len - halfway ) );
222 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200223 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200224 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200225 hash->len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200226
227exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_md_free( &ctx );
Andres AG99b257c2016-08-26 17:21:14 +0100229 mbedtls_md_free( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000230}
Paul Bakker33b43f12013-08-20 11:48:36 +0200231/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000232
Paul Bakker33b43f12013-08-20 11:48:36 +0200233/* BEGIN_CASE */
Ronald Cronac6ae352020-06-26 14:33:03 +0200234void md_hex_multi( char * text_md_name, data_t * src_str, data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000235{
236 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000237 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100239 mbedtls_md_context_t ctx, ctx_copy;
Azim Khanf1aaec92017-05-30 14:23:15 +0100240 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_md_init( &ctx );
Paul Bakker97c53c22016-07-13 17:20:22 +0100243 mbedtls_md_init( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000244
Paul Bakker5c57e022016-07-19 13:31:41 +0100245 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100246 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000247
Paul Bakker5c57e022016-07-19 13:31:41 +0100248 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 md_info = mbedtls_md_info_from_string(md_name);
Paul Bakker17373852011-01-06 14:20:01 +0000250 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100252 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000253
Azim Khand30ca132017-06-09 04:32:58 +0100254 halfway = src_str->len / 2;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000257 TEST_ASSERT ( ctx.md_ctx != NULL );
Azim Khand30ca132017-06-09 04:32:58 +0100258 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str->x, halfway ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100259 TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
260
Azim Khand30ca132017-06-09 04:32:58 +0100261 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str->x + halfway, src_str->len - halfway) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200263 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200264 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200265 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000266
Paul Bakker97c53c22016-07-13 17:20:22 +0100267 /* Test clone */
Paul Bakker5c57e022016-07-19 13:31:41 +0100268 memset( output, 0x00, 100 );
Paul Bakker97c53c22016-07-13 17:20:22 +0100269
Azim Khand30ca132017-06-09 04:32:58 +0100270 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str->x + halfway, src_str->len - halfway ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100271 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200272 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200273 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200274 hash->len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200275
276exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_md_free( &ctx );
Andres AG99b257c2016-08-26 17:21:14 +0100278 mbedtls_md_free( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000279}
Paul Bakker33b43f12013-08-20 11:48:36 +0200280/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000281
Paul Bakker33b43f12013-08-20 11:48:36 +0200282/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100283void mbedtls_md_hmac( char * text_md_name, int trunc_size,
Azim Khan5fcca462018-06-29 11:05:32 +0100284 data_t * key_str, data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200285 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000286{
287 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000288 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000290
Paul Bakker5c57e022016-07-19 13:31:41 +0100291 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100292 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000293
Paul Bakker5c57e022016-07-19 13:31:41 +0100294 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000296 TEST_ASSERT( md_info != NULL );
297
Paul Bakker17373852011-01-06 14:20:01 +0000298
Azim Khand30ca132017-06-09 04:32:58 +0100299 TEST_ASSERT ( mbedtls_md_hmac( md_info, key_str->x, key_str->len, src_str->x, src_str->len, output ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000300
Ronald Cronac6ae352020-06-26 14:33:03 +0200301 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
302 trunc_size, hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000303}
Paul Bakker33b43f12013-08-20 11:48:36 +0200304/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000305
Paul Bakker33b43f12013-08-20 11:48:36 +0200306/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100307void md_hmac_multi( char * text_md_name, int trunc_size, data_t * key_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200308 data_t * src_str, data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000309{
310 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000311 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 const mbedtls_md_info_t *md_info = NULL;
313 mbedtls_md_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100314 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000317
Paul Bakker5c57e022016-07-19 13:31:41 +0100318 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100319 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000320
Paul Bakker5c57e022016-07-19 13:31:41 +0100321 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000323 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000325
Azim Khand30ca132017-06-09 04:32:58 +0100326 halfway = src_str->len / 2;
Paul Bakker17373852011-01-06 14:20:01 +0000327
Azim Khand30ca132017-06-09 04:32:58 +0100328 TEST_ASSERT ( 0 == mbedtls_md_hmac_starts( &ctx, key_str->x, key_str->len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000329 TEST_ASSERT ( ctx.md_ctx != NULL );
Azim Khand30ca132017-06-09 04:32:58 +0100330 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x, halfway ) );
331 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x + halfway, src_str->len - halfway ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100333
Ronald Cronac6ae352020-06-26 14:33:03 +0200334 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
335 trunc_size, hash->len ) == 0 );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100336
337 /* Test again, for reset() */
Paul Bakker5c57e022016-07-19 13:31:41 +0100338 memset( output, 0x00, 100 );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 TEST_ASSERT ( 0 == mbedtls_md_hmac_reset( &ctx ) );
Azim Khand30ca132017-06-09 04:32:58 +0100341 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x, halfway ) );
342 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x + halfway, src_str->len - halfway ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200344
Ronald Cronac6ae352020-06-26 14:33:03 +0200345 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
346 trunc_size, hash->len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200347
348exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000350}
Paul Bakker33b43f12013-08-20 11:48:36 +0200351/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100354void mbedtls_md_file( char * text_md_name, char * filename,
Ronald Cronac6ae352020-06-26 14:33:03 +0200355 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000356{
357 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000358 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000360
Paul Bakker5c57e022016-07-19 13:31:41 +0100361 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100362 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000363
Paul Bakker5c57e022016-07-19 13:31:41 +0100364 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000366 TEST_ASSERT( md_info != NULL );
367
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200368 TEST_ASSERT( mbedtls_md_file( md_info, filename, output ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000369
Ronald Cronac6ae352020-06-26 14:33:03 +0200370 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200371 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200372 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000373}
Paul Bakker33b43f12013-08-20 11:48:36 +0200374/* END_CASE */