blob: d918ce3b960de74c4042b57710ec2578b18eeb96 [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 );
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020019
20 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021 * Very minimal testing of mbedtls_md_process, just make sure the various
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020022 * xxx_process_wrap() function pointers are valid. (Testing that they
23 * indeed do the right thing whould require messing with the internal
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024 * state of the underlying mbedtls_md/sha context.)
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020025 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026 * Also tests that mbedtls_md_list() only returns valid MDs.
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 info = mbedtls_md_info_from_type( *md_type_ptr );
Paul Bakker94b916c2014-04-17 16:07:20 +020031 TEST_ASSERT( info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 TEST_ASSERT( mbedtls_md_setup( &ctx, info, 0 ) == 0 );
33 TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == 0 );
34 mbedtls_md_free( &ctx );
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020035 }
Paul Bakkerbd51b262014-07-10 15:26:12 +020036
37exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038 mbedtls_md_free( &ctx );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010039}
40/* END_CASE */
41
42/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010043void md_null_args( )
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020044{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 mbedtls_md_context_t ctx;
46 const mbedtls_md_info_t *info = mbedtls_md_info_from_type( *( mbedtls_md_list() ) );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020047 unsigned char buf[1] = { 0 };
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 mbedtls_md_init( &ctx );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 TEST_ASSERT( mbedtls_md_get_size( NULL ) == 0 );
52 TEST_ASSERT( mbedtls_md_get_type( NULL ) == MBEDTLS_MD_NONE );
53 TEST_ASSERT( mbedtls_md_get_name( NULL ) == NULL );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 TEST_ASSERT( mbedtls_md_info_from_string( NULL ) == NULL );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 TEST_ASSERT( mbedtls_md_setup( &ctx, NULL, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
58 TEST_ASSERT( mbedtls_md_setup( NULL, info, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 TEST_ASSERT( mbedtls_md_starts( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
61 TEST_ASSERT( mbedtls_md_starts( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 TEST_ASSERT( mbedtls_md_update( NULL, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
64 TEST_ASSERT( mbedtls_md_update( &ctx, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 TEST_ASSERT( mbedtls_md_finish( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
67 TEST_ASSERT( mbedtls_md_finish( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 TEST_ASSERT( mbedtls_md( NULL, buf, 1, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020070
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020071#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 TEST_ASSERT( mbedtls_md_file( NULL, "", buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +020073#endif
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 TEST_ASSERT( mbedtls_md_hmac_starts( NULL, buf, 1 )
76 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
77 TEST_ASSERT( mbedtls_md_hmac_starts( &ctx, buf, 1 )
78 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 TEST_ASSERT( mbedtls_md_hmac_update( NULL, buf, 1 )
81 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
82 TEST_ASSERT( mbedtls_md_hmac_update( &ctx, buf, 1 )
83 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 TEST_ASSERT( mbedtls_md_hmac_finish( NULL, buf )
86 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
87 TEST_ASSERT( mbedtls_md_hmac_finish( &ctx, buf )
88 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 TEST_ASSERT( mbedtls_md_hmac_reset( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
91 TEST_ASSERT( mbedtls_md_hmac_reset( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 TEST_ASSERT( mbedtls_md_hmac( NULL, buf, 1, buf, 1, buf )
94 == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +020095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 TEST_ASSERT( mbedtls_md_process( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
97 TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard19d644b2015-03-26 12:42:35 +010098
99 /* Ok, this is not NULL arg but NULL return... */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 TEST_ASSERT( mbedtls_md_info_from_type( MBEDTLS_MD_NONE ) == NULL );
101 TEST_ASSERT( mbedtls_md_info_from_string( "no such md" ) == NULL );
Manuel Pégourié-Gonnardb25f8162014-06-13 16:34:30 +0200102}
103/* END_CASE */
104
105/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100106void md_info( int md_type, char * md_name, int md_size )
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100109 const int *md_type_ptr;
110 int found;
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakker94b916c2014-04-17 16:07:20 +0200113 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 TEST_ASSERT( md_info == mbedtls_md_info_from_string( md_name ) );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 TEST_ASSERT( mbedtls_md_get_type( md_info ) == (mbedtls_md_type_t) md_type );
117 TEST_ASSERT( mbedtls_md_get_size( md_info ) == (unsigned char) md_size );
118 TEST_ASSERT( strcmp( mbedtls_md_get_name( md_info ), md_name ) == 0 );
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100119
120 found = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +0100122 if( *md_type_ptr == md_type )
123 found = 1;
124 TEST_ASSERT( found == 1 );
125}
126/* END_CASE */
127
128/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100129void md_text( char * text_md_name, char * text_src_string,
Ronald Cronac6ae352020-06-26 14:33:03 +0200130 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000131{
132 char md_name[100];
133 unsigned char src_str[1000];
Paul Bakker17373852011-01-06 14:20:01 +0000134 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000136
Paul Bakker5c57e022016-07-19 13:31:41 +0100137 memset( md_name, 0x00, 100 );
138 memset( src_str, 0x00, 1000 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100139 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000140
Paul Bakker5c57e022016-07-19 13:31:41 +0100141 strncpy( (char *) src_str, text_src_string, sizeof( src_str ) - 1 );
142 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 md_info = mbedtls_md_info_from_string(md_name);
Paul Bakker17373852011-01-06 14:20:01 +0000144 TEST_ASSERT( md_info != NULL );
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, strlen( (char *) src_str ), output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000147
Ronald Cronac6ae352020-06-26 14:33:03 +0200148 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200149 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200150 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000151}
Paul Bakker33b43f12013-08-20 11:48:36 +0200152/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000153
Paul Bakker33b43f12013-08-20 11:48:36 +0200154/* BEGIN_CASE */
Ronald Cronac6ae352020-06-26 14:33:03 +0200155void md_hex( char * text_md_name, data_t * src_str, data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000156{
157 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000158 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000160
Paul Bakker5c57e022016-07-19 13:31:41 +0100161 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100162 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000163
Paul Bakker5c57e022016-07-19 13:31:41 +0100164 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
165 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000166 TEST_ASSERT( md_info != NULL );
167
Azim Khand30ca132017-06-09 04:32:58 +0100168 TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str->x, src_str->len, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000169
Paul Bakker17373852011-01-06 14:20:01 +0000170
Ronald Cronac6ae352020-06-26 14:33:03 +0200171 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200172 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200173 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000174}
Paul Bakker33b43f12013-08-20 11:48:36 +0200175/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000176
Paul Bakker33b43f12013-08-20 11:48:36 +0200177/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100178void md_text_multi( char * text_md_name, char * text_src_string,
Ronald Cronac6ae352020-06-26 14:33:03 +0200179 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000180{
181 char md_name[100];
182 unsigned char src_str[1000];
Paul Bakker17373852011-01-06 14:20:01 +0000183 unsigned char output[100];
Paul Bakkere35afa22016-07-13 17:09:14 +0100184 int halfway, len;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100187 mbedtls_md_context_t ctx, ctx_copy;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_md_init( &ctx );
Paul Bakker97c53c22016-07-13 17:20:22 +0100190 mbedtls_md_init( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000191
Paul Bakker5c57e022016-07-19 13:31:41 +0100192 memset( md_name, 0x00, 100 );
193 memset( src_str, 0x00, 1000 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100194 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000195
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200196 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
197 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakkere35afa22016-07-13 17:09:14 +0100198 len = strlen( (char *) src_str );
199 halfway = len / 2;
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 md_info = mbedtls_md_info_from_string(md_name);
Paul Bakker17373852011-01-06 14:20:01 +0000202 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100204 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000207 TEST_ASSERT ( ctx.md_ctx != NULL );
Paul Bakkere35afa22016-07-13 17:09:14 +0100208 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, halfway ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100209 TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
210
Paul Bakkere35afa22016-07-13 17:09:14 +0100211 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str + halfway, len - halfway ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200213 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200214 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200215 hash->len) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000216
Paul Bakker97c53c22016-07-13 17:20:22 +0100217 /* Test clone */
Paul Bakker5c57e022016-07-19 13:31:41 +0100218 memset( output, 0x00, 100 );
Paul Bakker97c53c22016-07-13 17:20:22 +0100219
220 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str + halfway, len - halfway ) );
221 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200222 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200223 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200224 hash->len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200225
226exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_md_free( &ctx );
Andres AG99b257c2016-08-26 17:21:14 +0100228 mbedtls_md_free( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000229}
Paul Bakker33b43f12013-08-20 11:48:36 +0200230/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000231
Paul Bakker33b43f12013-08-20 11:48:36 +0200232/* BEGIN_CASE */
Ronald Cronac6ae352020-06-26 14:33:03 +0200233void md_hex_multi( char * text_md_name, data_t * src_str, data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000234{
235 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000236 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker97c53c22016-07-13 17:20:22 +0100238 mbedtls_md_context_t ctx, ctx_copy;
Azim Khanf1aaec92017-05-30 14:23:15 +0100239 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_md_init( &ctx );
Paul Bakker97c53c22016-07-13 17:20:22 +0100242 mbedtls_md_init( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000243
Paul Bakker5c57e022016-07-19 13:31:41 +0100244 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100245 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000246
Paul Bakker5c57e022016-07-19 13:31:41 +0100247 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 md_info = mbedtls_md_info_from_string(md_name);
Paul Bakker17373852011-01-06 14:20:01 +0000249 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100251 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000252
Azim Khand30ca132017-06-09 04:32:58 +0100253 halfway = src_str->len / 2;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000256 TEST_ASSERT ( ctx.md_ctx != NULL );
Azim Khand30ca132017-06-09 04:32:58 +0100257 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str->x, halfway ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100258 TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
259
Azim Khand30ca132017-06-09 04:32:58 +0100260 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str->x + halfway, src_str->len - halfway) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200262 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200263 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200264 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000265
Paul Bakker97c53c22016-07-13 17:20:22 +0100266 /* Test clone */
Paul Bakker5c57e022016-07-19 13:31:41 +0100267 memset( output, 0x00, 100 );
Paul Bakker97c53c22016-07-13 17:20:22 +0100268
Azim Khand30ca132017-06-09 04:32:58 +0100269 TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str->x + halfway, src_str->len - halfway ) );
Paul Bakker97c53c22016-07-13 17:20:22 +0100270 TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
Ronald Cronac6ae352020-06-26 14:33:03 +0200271 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200272 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200273 hash->len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200274
275exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_md_free( &ctx );
Andres AG99b257c2016-08-26 17:21:14 +0100277 mbedtls_md_free( &ctx_copy );
Paul Bakker17373852011-01-06 14:20:01 +0000278}
Paul Bakker33b43f12013-08-20 11:48:36 +0200279/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000280
Paul Bakker33b43f12013-08-20 11:48:36 +0200281/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100282void mbedtls_md_hmac( char * text_md_name, int trunc_size,
Azim Khan5fcca462018-06-29 11:05:32 +0100283 data_t * key_str, data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200284 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000285{
286 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000287 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000289
Paul Bakker5c57e022016-07-19 13:31:41 +0100290 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100291 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000292
Paul Bakker5c57e022016-07-19 13:31:41 +0100293 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000295 TEST_ASSERT( md_info != NULL );
296
Paul Bakker17373852011-01-06 14:20:01 +0000297
Azim Khand30ca132017-06-09 04:32:58 +0100298 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 +0000299
Ronald Cronac6ae352020-06-26 14:33:03 +0200300 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
301 trunc_size, hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000302}
Paul Bakker33b43f12013-08-20 11:48:36 +0200303/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000304
Paul Bakker33b43f12013-08-20 11:48:36 +0200305/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100306void md_hmac_multi( char * text_md_name, int trunc_size, data_t * key_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200307 data_t * src_str, data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000308{
309 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000310 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 const mbedtls_md_info_t *md_info = NULL;
312 mbedtls_md_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100313 int halfway;
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_md_init( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000316
Paul Bakker5c57e022016-07-19 13:31:41 +0100317 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100318 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000319
Paul Bakker5c57e022016-07-19 13:31:41 +0100320 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000322 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) );
Paul Bakker17373852011-01-06 14:20:01 +0000324
Azim Khand30ca132017-06-09 04:32:58 +0100325 halfway = src_str->len / 2;
Paul Bakker17373852011-01-06 14:20:01 +0000326
Azim Khand30ca132017-06-09 04:32:58 +0100327 TEST_ASSERT ( 0 == mbedtls_md_hmac_starts( &ctx, key_str->x, key_str->len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000328 TEST_ASSERT ( ctx.md_ctx != NULL );
Azim Khand30ca132017-06-09 04:32:58 +0100329 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x, halfway ) );
330 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 +0200331 TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100332
Ronald Cronac6ae352020-06-26 14:33:03 +0200333 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
334 trunc_size, hash->len ) == 0 );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100335
336 /* Test again, for reset() */
Paul Bakker5c57e022016-07-19 13:31:41 +0100337 memset( output, 0x00, 100 );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 TEST_ASSERT ( 0 == mbedtls_md_hmac_reset( &ctx ) );
Azim Khand30ca132017-06-09 04:32:58 +0100340 TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x, halfway ) );
341 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 +0200342 TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200343
Ronald Cronac6ae352020-06-26 14:33:03 +0200344 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
345 trunc_size, hash->len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200346
347exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_md_free( &ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000349}
Paul Bakker33b43f12013-08-20 11:48:36 +0200350/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100353void mbedtls_md_file( char * text_md_name, char * filename,
Ronald Cronac6ae352020-06-26 14:33:03 +0200354 data_t * hash )
Paul Bakker17373852011-01-06 14:20:01 +0000355{
356 char md_name[100];
Paul Bakker17373852011-01-06 14:20:01 +0000357 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 const mbedtls_md_info_t *md_info = NULL;
Paul Bakker17373852011-01-06 14:20:01 +0000359
Paul Bakker5c57e022016-07-19 13:31:41 +0100360 memset( md_name, 0x00, 100 );
Paul Bakker5c57e022016-07-19 13:31:41 +0100361 memset( output, 0x00, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000362
Paul Bakker5c57e022016-07-19 13:31:41 +0100363 strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 md_info = mbedtls_md_info_from_string( md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000365 TEST_ASSERT( md_info != NULL );
366
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200367 TEST_ASSERT( mbedtls_md_file( md_info, filename, output ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000368
Ronald Cronac6ae352020-06-26 14:33:03 +0200369 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200370 mbedtls_md_get_size( md_info ),
Ronald Cronac6ae352020-06-26 14:33:03 +0200371 hash->len ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000372}
Paul Bakker33b43f12013-08-20 11:48:36 +0200373/* END_CASE */