blob: aee55b9c3d1de59ba81e6566a11d076db7590551 [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/sha1.h"
3#include "mbedtls/sha256.h"
4#include "mbedtls/sha512.h"
Pol Henarejosf6457052022-05-09 01:04:34 +02005#include "mbedtls/sha3.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02006/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C */
Ronald Cronac6ae352020-06-26 14:33:03 +02009void mbedtls_sha1( data_t * src_str, data_t * hash )
Paul Bakker367dae42009-06-28 21:50:27 +000010{
Paul Bakker367dae42009-06-28 21:50:27 +000011 unsigned char output[41];
12
Paul Bakker367dae42009-06-28 21:50:27 +000013 memset(output, 0x00, 41);
14
Paul Bakker367dae42009-06-28 21:50:27 +000015
TRodziewicz26371e42021-06-08 16:45:41 +020016 TEST_ASSERT( mbedtls_sha1( src_str->x, src_str->len, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000017
Ronald Cronac6ae352020-06-26 14:33:03 +020018 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x, 20, hash->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000019}
Paul Bakker33b43f12013-08-20 11:48:36 +020020/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000021
TRodziewicz062f3532021-05-25 15:15:57 +020022/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:NOT_DEFINED */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050023void sha256_invalid_param( )
24{
25 mbedtls_sha256_context ctx;
26 unsigned char buf[64] = { 0 };
27 size_t const buflen = sizeof( buf );
28 int valid_type = 0;
29 int invalid_type = 42;
30
TRodziewicz062f3532021-05-25 15:15:57 +020031 TEST_EQUAL( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
TRodziewicz26371e42021-06-08 16:45:41 +020032 mbedtls_sha256_starts( &ctx, invalid_type ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050033
TRodziewicz062f3532021-05-25 15:15:57 +020034 TEST_EQUAL( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
TRodziewicz26371e42021-06-08 16:45:41 +020035 mbedtls_sha256( buf, buflen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050036 buf, invalid_type ) );
37
38exit:
39 return;
40}
41/* END_CASE */
42
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020043/* BEGIN_CASE depends_on:MBEDTLS_SHA224_C */
Ronald Cronac6ae352020-06-26 14:33:03 +020044void sha224( data_t * src_str, data_t * hash )
Paul Bakker367dae42009-06-28 21:50:27 +000045{
Paul Bakker367dae42009-06-28 21:50:27 +000046 unsigned char output[57];
47
Paul Bakker367dae42009-06-28 21:50:27 +000048 memset(output, 0x00, 57);
49
Paul Bakker367dae42009-06-28 21:50:27 +000050
TRodziewicz26371e42021-06-08 16:45:41 +020051 TEST_ASSERT( mbedtls_sha256( src_str->x, src_str->len, output, 1 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000052
Ronald Cronac6ae352020-06-26 14:33:03 +020053 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x, 28, hash->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000054}
Paul Bakker33b43f12013-08-20 11:48:36 +020055/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
Ronald Cronac6ae352020-06-26 14:33:03 +020058void mbedtls_sha256( data_t * src_str, data_t * hash )
Paul Bakker367dae42009-06-28 21:50:27 +000059{
Paul Bakker367dae42009-06-28 21:50:27 +000060 unsigned char output[65];
61
Paul Bakker367dae42009-06-28 21:50:27 +000062 memset(output, 0x00, 65);
63
Paul Bakker367dae42009-06-28 21:50:27 +000064
TRodziewicz26371e42021-06-08 16:45:41 +020065 TEST_ASSERT( mbedtls_sha256( src_str->x, src_str->len, output, 0 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000066
Ronald Cronac6ae352020-06-26 14:33:03 +020067 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x, 32, hash->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000068}
Paul Bakker33b43f12013-08-20 11:48:36 +020069/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000070
TRodziewicz062f3532021-05-25 15:15:57 +020071/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:NOT_DEFINED */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072void sha512_invalid_param( )
73{
74 mbedtls_sha512_context ctx;
75 unsigned char buf[64] = { 0 };
76 size_t const buflen = sizeof( buf );
77 int valid_type = 0;
78 int invalid_type = 42;
79
TRodziewicz062f3532021-05-25 15:15:57 +020080 TEST_EQUAL( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
TRodziewicz26371e42021-06-08 16:45:41 +020081 mbedtls_sha512_starts( &ctx, invalid_type ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050082
TRodziewicz062f3532021-05-25 15:15:57 +020083 TEST_EQUAL( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
TRodziewicz26371e42021-06-08 16:45:41 +020084 mbedtls_sha512( buf, buflen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050085 buf, invalid_type ) );
86
87exit:
88 return;
89}
90/* END_CASE */
91
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +020092/* BEGIN_CASE depends_on:MBEDTLS_SHA384_C */
Ronald Cronac6ae352020-06-26 14:33:03 +020093void sha384( data_t * src_str, data_t * hash )
Paul Bakker367dae42009-06-28 21:50:27 +000094{
Paul Bakker367dae42009-06-28 21:50:27 +000095 unsigned char output[97];
96
Paul Bakker367dae42009-06-28 21:50:27 +000097 memset(output, 0x00, 97);
98
Paul Bakker367dae42009-06-28 21:50:27 +000099
TRodziewicz26371e42021-06-08 16:45:41 +0200100 TEST_ASSERT( mbedtls_sha512( src_str->x, src_str->len, output, 1 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000101
Ronald Cronac6ae352020-06-26 14:33:03 +0200102 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x, 48, hash->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000103}
Paul Bakker33b43f12013-08-20 11:48:36 +0200104/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
Ronald Cronac6ae352020-06-26 14:33:03 +0200107void mbedtls_sha512( data_t * src_str, data_t * hash )
Paul Bakker367dae42009-06-28 21:50:27 +0000108{
Paul Bakker367dae42009-06-28 21:50:27 +0000109 unsigned char output[129];
110
Paul Bakker367dae42009-06-28 21:50:27 +0000111 memset(output, 0x00, 129);
112
Paul Bakker367dae42009-06-28 21:50:27 +0000113
TRodziewicz26371e42021-06-08 16:45:41 +0200114 TEST_ASSERT( mbedtls_sha512( src_str->x, src_str->len, output, 0 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000115
Ronald Cronac6ae352020-06-26 14:33:03 +0200116 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x, 64, hash->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000117}
Paul Bakker33b43f12013-08-20 11:48:36 +0200118/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100121void sha1_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000122{
Andres AG93012e82016-09-09 09:10:28 +0100123 TEST_ASSERT( mbedtls_sha1_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000124}
Paul Bakker33b43f12013-08-20 11:48:36 +0200125/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100128void sha256_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000129{
Andres AG93012e82016-09-09 09:10:28 +0100130 TEST_ASSERT( mbedtls_sha256_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000131}
Paul Bakker33b43f12013-08-20 11:48:36 +0200132/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100135void sha512_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000136{
Andres AG93012e82016-09-09 09:10:28 +0100137 TEST_ASSERT( mbedtls_sha512_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000138}
Paul Bakker33b43f12013-08-20 11:48:36 +0200139/* END_CASE */
Pol Henarejosf6457052022-05-09 01:04:34 +0200140
141/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
142void mbedtls_sha3( int family, data_t *in, data_t *hash )
143{
144 unsigned char *output = NULL;
145
146 ASSERT_ALLOC( output, hash->len );
147
148 TEST_ASSERT( mbedtls_sha3( family, in->x, in->len, output, hash->len ) == 0 );
149
150 ASSERT_COMPARE( output, hash->len, hash->x, hash->len );
151
152exit:
153 mbedtls_free( output );
154}
155/* END_CASE */
156
157/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
158void mbedtls_sha3_multi( int family, data_t *in, data_t *hash )
159{
160 unsigned char *output = NULL;
161 mbedtls_sha3_context ctx;
162 const unsigned int block_size = 256;
163
164 ASSERT_ALLOC( output, hash->len );
165
166 mbedtls_sha3_init( &ctx );
167 mbedtls_sha3_starts( &ctx, family );
168
169 for( size_t l = 0; l < in->len; l += block_size )
170 TEST_ASSERT( mbedtls_sha3_update( &ctx, in->x + l, MIN( in->len - l, block_size ) ) == 0 );
171
172 TEST_ASSERT( mbedtls_sha3_finish( &ctx, output, hash->len ) == 0 );
173
174 ASSERT_COMPARE( output, hash->len, hash->x, hash->len );
175
176exit:
177 mbedtls_free( output );
178}
179/* END_CASE */
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200180
Pol Henarejos90f803c2022-05-20 20:50:29 +0200181/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
182void sha3_streaming( int type, data_t *input )
183{
184 mbedtls_sha3_context ctx;
185 unsigned char reference_hash[64];
186 unsigned char hash[64];
187 size_t chunk_size;
188 size_t hash_length = ( type == MBEDTLS_SHA3_224 ? 28 :
189 type == MBEDTLS_SHA3_256 ? 32 :
190 type == MBEDTLS_SHA3_384 ? 48 :
191 type == MBEDTLS_SHA3_512 ? 64 :
192 0 );
193
194 mbedtls_sha3_init( &ctx );
195 memset( reference_hash, 0, sizeof( reference_hash ) );
196 memset( hash, 0, sizeof( hash ) );
197 TEST_ASSERT( hash_length != 0 );
198
199 /* Generate a reference hash */
200 mbedtls_sha3( type, input->x, input->len, reference_hash, hash_length );
201
202 /* Repeat each test with increasingly-sized data chunks
203 * E.g. start by processing bytes individual bytes, then 2-byte chunks,
204 * then 3-byte chunks, and so on...
205 * At each test ensure that the same hash is generated.
206 */
207 for( chunk_size = 1; chunk_size < input->len; chunk_size++ )
208 {
209 size_t i;
210 size_t remaining = input->len;
211
212 mbedtls_sha3_init( &ctx );
213 TEST_ASSERT( mbedtls_sha3_starts( &ctx, type ) == 0 );
214
215 for ( i = 0; i < input->len; i += chunk_size )
216 {
217 size_t len = remaining >= chunk_size ? chunk_size : remaining;
218 TEST_ASSERT( mbedtls_sha3_update( &ctx, input->x + i, len ) == 0 );
219 remaining -= len;
220 }
221
222 mbedtls_sha3_finish( &ctx, hash, hash_length );
223 mbedtls_sha3_free( &ctx );
224
225 ASSERT_COMPARE( hash, hash_length, reference_hash, hash_length );
226 }
227
228exit:
229 mbedtls_sha3_free( &ctx );
230}
231/* END_CASE */
232
233/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
234void sha3_reuse( data_t *input1, data_t *hash1,
235 data_t *input2, data_t *hash2 )
236{
237 unsigned char output[64];
238 mbedtls_sha3_context ctx;
239 mbedtls_sha3_id type1, type2;
240
241 mbedtls_sha3_init( &ctx );
242 switch( hash1->len )
243 {
244 case 28: type1 = MBEDTLS_SHA3_224; break;
245 case 32: type1 = MBEDTLS_SHA3_256; break;
246 case 48: type1 = MBEDTLS_SHA3_384; break;
247 case 64: type1 = MBEDTLS_SHA3_512; break;
248 default: TEST_ASSERT( ! "hash1->len validity" ); break;
249 }
250 switch( hash2->len )
251 {
252 case 28: type2 = MBEDTLS_SHA3_224; break;
253 case 32: type2 = MBEDTLS_SHA3_256; break;
254 case 48: type2 = MBEDTLS_SHA3_384; break;
255 case 64: type2 = MBEDTLS_SHA3_512; break;
256 default: TEST_ASSERT( ! "hash2->len validity" ); break;
257 }
258
259 /* Round 1 */
260 TEST_ASSERT( mbedtls_sha3_starts( &ctx, type1 ) == 0 );
261 TEST_ASSERT( mbedtls_sha3_update( &ctx, input1->x, input1->len ) == 0 );
262 TEST_ASSERT( mbedtls_sha3_finish( &ctx, output, sizeof( output ) ) == 0 );
263 ASSERT_COMPARE( output, hash1->len, hash1->x, hash1->len );
264
265 /* Round 2 */
266 TEST_ASSERT( mbedtls_sha3_starts( &ctx, type2 ) == 0 );
267 TEST_ASSERT( mbedtls_sha3_update( &ctx, input2->x, input2->len ) == 0 );
268 TEST_ASSERT( mbedtls_sha3_finish( &ctx, output, sizeof( output ) ) == 0 );
269 ASSERT_COMPARE( output, hash2->len, hash2->x, hash2->len );
270
271exit:
272 mbedtls_sha3_free( &ctx );
273}
274/* END_CASE */
275
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200276/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C:MBEDTLS_SELF_TEST */
277void sha3_selftest()
278{
279 TEST_ASSERT( mbedtls_sha3_self_test( 0 ) == 0 );
280}
281/* END_CASE */