blob: 7b45bf32d68117c5c2f8af813ef60cfb3b8af039 [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 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009void 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
Gilles Peskine449bd832023-01-11 14:50:10 +010016 TEST_ASSERT(mbedtls_sha1(src_str->x, src_str->len, output) == 0);
Paul Bakker367dae42009-06-28 21:50:27 +000017
Gilles Peskine449bd832023-01-11 14:50:10 +010018 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
Tuvshinzaya Erdenekhuu1db192b2022-07-29 15:44:38 +010022/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010023void sha256_invalid_param()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050024{
25 mbedtls_sha256_context ctx;
26 unsigned char buf[64] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +010027 size_t const buflen = sizeof(buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050028 int invalid_type = 42;
29
Gilles Peskine449bd832023-01-11 14:50:10 +010030 TEST_EQUAL(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
31 mbedtls_sha256_starts(&ctx, invalid_type));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050032
Gilles Peskine449bd832023-01-11 14:50:10 +010033 TEST_EQUAL(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
34 mbedtls_sha256(buf, buflen,
35 buf, invalid_type));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050036
37exit:
38 return;
39}
40/* END_CASE */
41
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020042/* BEGIN_CASE depends_on:MBEDTLS_SHA224_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010043void sha224(data_t *src_str, data_t *hash)
Paul Bakker367dae42009-06-28 21:50:27 +000044{
Paul Bakker367dae42009-06-28 21:50:27 +000045 unsigned char output[57];
46
Paul Bakker367dae42009-06-28 21:50:27 +000047 memset(output, 0x00, 57);
48
Paul Bakker367dae42009-06-28 21:50:27 +000049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 TEST_EQUAL(mbedtls_sha256(src_str->x, src_str->len, output, 1), 0);
Paul Bakker367dae42009-06-28 21:50:27 +000051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 28, hash->len), 0);
Paul Bakker367dae42009-06-28 21:50:27 +000053}
Paul Bakker33b43f12013-08-20 11:48:36 +020054/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010057void mbedtls_sha256(data_t *src_str, data_t *hash)
Paul Bakker367dae42009-06-28 21:50:27 +000058{
Paul Bakker367dae42009-06-28 21:50:27 +000059 unsigned char output[65];
60
Paul Bakker367dae42009-06-28 21:50:27 +000061 memset(output, 0x00, 65);
62
Paul Bakker367dae42009-06-28 21:50:27 +000063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 TEST_EQUAL(mbedtls_sha256(src_str->x, src_str->len, output, 0), 0);
Paul Bakker367dae42009-06-28 21:50:27 +000065
Gilles Peskine449bd832023-01-11 14:50:10 +010066 TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 32, hash->len), 0);
Paul Bakker367dae42009-06-28 21:50:27 +000067}
Paul Bakker33b43f12013-08-20 11:48:36 +020068/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000069
Tuvshinzaya Erdenekhuuca6fde22022-07-29 15:43:04 +010070/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010071void sha512_invalid_param()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072{
73 mbedtls_sha512_context ctx;
74 unsigned char buf[64] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +010075 size_t const buflen = sizeof(buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050076 int invalid_type = 42;
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078 TEST_EQUAL(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
79 mbedtls_sha512_starts(&ctx, invalid_type));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 TEST_EQUAL(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
82 mbedtls_sha512(buf, buflen,
83 buf, invalid_type));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050084
85exit:
86 return;
87}
88/* END_CASE */
89
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +020090/* BEGIN_CASE depends_on:MBEDTLS_SHA384_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010091void sha384(data_t *src_str, data_t *hash)
Paul Bakker367dae42009-06-28 21:50:27 +000092{
Paul Bakker367dae42009-06-28 21:50:27 +000093 unsigned char output[97];
94
Paul Bakker367dae42009-06-28 21:50:27 +000095 memset(output, 0x00, 97);
96
Paul Bakker367dae42009-06-28 21:50:27 +000097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 TEST_EQUAL(mbedtls_sha512(src_str->x, src_str->len, output, 1), 0);
Paul Bakker367dae42009-06-28 21:50:27 +000099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 48, hash->len), 0);
Paul Bakker367dae42009-06-28 21:50:27 +0000101}
Paul Bakker33b43f12013-08-20 11:48:36 +0200102/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105void mbedtls_sha512(data_t *src_str, data_t *hash)
Paul Bakker367dae42009-06-28 21:50:27 +0000106{
Paul Bakker367dae42009-06-28 21:50:27 +0000107 unsigned char output[129];
108
Paul Bakker367dae42009-06-28 21:50:27 +0000109 memset(output, 0x00, 129);
110
Paul Bakker367dae42009-06-28 21:50:27 +0000111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 TEST_EQUAL(mbedtls_sha512(src_str->x, src_str->len, output, 0), 0);
Paul Bakker367dae42009-06-28 21:50:27 +0000113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 64, hash->len), 0);
Paul Bakker367dae42009-06-28 21:50:27 +0000115}
Paul Bakker33b43f12013-08-20 11:48:36 +0200116/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119void sha1_selftest()
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000120{
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 TEST_ASSERT(mbedtls_sha1_self_test(1) == 0);
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000122}
Paul Bakker33b43f12013-08-20 11:48:36 +0200123/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000124
Valerio Setti46e8fd82022-12-14 10:58:02 +0100125/* BEGIN_CASE depends_on:MBEDTLS_SHA224_C:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126void sha224_selftest()
Valerio Setti46e8fd82022-12-14 10:58:02 +0100127{
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 TEST_EQUAL(mbedtls_sha224_self_test(1), 0);
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000129}
130/* END_CASE */
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133void sha256_selftest()
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000134{
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 TEST_EQUAL(mbedtls_sha256_self_test(1), 0);
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000136}
Paul Bakker33b43f12013-08-20 11:48:36 +0200137/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000138
Valerio Setti898e7a32022-12-14 08:55:53 +0100139/* BEGIN_CASE depends_on:MBEDTLS_SHA384_C:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140void sha384_selftest()
Valerio Setti898e7a32022-12-14 08:55:53 +0100141{
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 TEST_EQUAL(mbedtls_sha384_self_test(1), 0);
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000143}
144/* END_CASE */
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147void sha512_selftest()
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000148{
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 TEST_EQUAL(mbedtls_sha512_self_test(1), 0);
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000150}
Paul Bakker33b43f12013-08-20 11:48:36 +0200151/* END_CASE */
Pol Henarejosf6457052022-05-09 01:04:34 +0200152
153/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
154void mbedtls_sha3( int family, data_t *in, data_t *hash )
155{
156 unsigned char *output = NULL;
157
158 ASSERT_ALLOC( output, hash->len );
159
160 TEST_ASSERT( mbedtls_sha3( family, in->x, in->len, output, hash->len ) == 0 );
161
162 ASSERT_COMPARE( output, hash->len, hash->x, hash->len );
163
164exit:
165 mbedtls_free( output );
166}
167/* END_CASE */
168
169/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
170void mbedtls_sha3_multi( int family, data_t *in, data_t *hash )
171{
172 unsigned char *output = NULL;
173 mbedtls_sha3_context ctx;
174 const unsigned int block_size = 256;
175
176 ASSERT_ALLOC( output, hash->len );
177
178 mbedtls_sha3_init( &ctx );
179 mbedtls_sha3_starts( &ctx, family );
180
181 for( size_t l = 0; l < in->len; l += block_size )
182 TEST_ASSERT( mbedtls_sha3_update( &ctx, in->x + l, MIN( in->len - l, block_size ) ) == 0 );
183
184 TEST_ASSERT( mbedtls_sha3_finish( &ctx, output, hash->len ) == 0 );
185
186 ASSERT_COMPARE( output, hash->len, hash->x, hash->len );
187
188exit:
189 mbedtls_free( output );
190}
191/* END_CASE */
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200192
Pol Henarejos90f803c2022-05-20 20:50:29 +0200193/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
194void sha3_streaming( int type, data_t *input )
195{
196 mbedtls_sha3_context ctx;
197 unsigned char reference_hash[64];
198 unsigned char hash[64];
199 size_t chunk_size;
200 size_t hash_length = ( type == MBEDTLS_SHA3_224 ? 28 :
201 type == MBEDTLS_SHA3_256 ? 32 :
202 type == MBEDTLS_SHA3_384 ? 48 :
203 type == MBEDTLS_SHA3_512 ? 64 :
204 0 );
205
206 mbedtls_sha3_init( &ctx );
207 memset( reference_hash, 0, sizeof( reference_hash ) );
208 memset( hash, 0, sizeof( hash ) );
209 TEST_ASSERT( hash_length != 0 );
210
211 /* Generate a reference hash */
212 mbedtls_sha3( type, input->x, input->len, reference_hash, hash_length );
213
214 /* Repeat each test with increasingly-sized data chunks
215 * E.g. start by processing bytes individual bytes, then 2-byte chunks,
216 * then 3-byte chunks, and so on...
217 * At each test ensure that the same hash is generated.
218 */
219 for( chunk_size = 1; chunk_size < input->len; chunk_size++ )
220 {
221 size_t i;
222 size_t remaining = input->len;
223
224 mbedtls_sha3_init( &ctx );
225 TEST_ASSERT( mbedtls_sha3_starts( &ctx, type ) == 0 );
226
227 for ( i = 0; i < input->len; i += chunk_size )
228 {
229 size_t len = remaining >= chunk_size ? chunk_size : remaining;
230 TEST_ASSERT( mbedtls_sha3_update( &ctx, input->x + i, len ) == 0 );
231 remaining -= len;
232 }
233
234 mbedtls_sha3_finish( &ctx, hash, hash_length );
235 mbedtls_sha3_free( &ctx );
236
237 ASSERT_COMPARE( hash, hash_length, reference_hash, hash_length );
238 }
239
240exit:
241 mbedtls_sha3_free( &ctx );
242}
243/* END_CASE */
244
245/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
246void sha3_reuse( data_t *input1, data_t *hash1,
247 data_t *input2, data_t *hash2 )
248{
249 unsigned char output[64];
250 mbedtls_sha3_context ctx;
251 mbedtls_sha3_id type1, type2;
252
253 mbedtls_sha3_init( &ctx );
254 switch( hash1->len )
255 {
256 case 28: type1 = MBEDTLS_SHA3_224; break;
257 case 32: type1 = MBEDTLS_SHA3_256; break;
258 case 48: type1 = MBEDTLS_SHA3_384; break;
259 case 64: type1 = MBEDTLS_SHA3_512; break;
260 default: TEST_ASSERT( ! "hash1->len validity" ); break;
261 }
262 switch( hash2->len )
263 {
264 case 28: type2 = MBEDTLS_SHA3_224; break;
265 case 32: type2 = MBEDTLS_SHA3_256; break;
266 case 48: type2 = MBEDTLS_SHA3_384; break;
267 case 64: type2 = MBEDTLS_SHA3_512; break;
268 default: TEST_ASSERT( ! "hash2->len validity" ); break;
269 }
270
271 /* Round 1 */
272 TEST_ASSERT( mbedtls_sha3_starts( &ctx, type1 ) == 0 );
273 TEST_ASSERT( mbedtls_sha3_update( &ctx, input1->x, input1->len ) == 0 );
274 TEST_ASSERT( mbedtls_sha3_finish( &ctx, output, sizeof( output ) ) == 0 );
275 ASSERT_COMPARE( output, hash1->len, hash1->x, hash1->len );
276
277 /* Round 2 */
278 TEST_ASSERT( mbedtls_sha3_starts( &ctx, type2 ) == 0 );
279 TEST_ASSERT( mbedtls_sha3_update( &ctx, input2->x, input2->len ) == 0 );
280 TEST_ASSERT( mbedtls_sha3_finish( &ctx, output, sizeof( output ) ) == 0 );
281 ASSERT_COMPARE( output, hash2->len, hash2->x, hash2->len );
282
283exit:
284 mbedtls_sha3_free( &ctx );
285}
286/* END_CASE */
287
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200288/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C:MBEDTLS_SELF_TEST */
289void sha3_selftest()
290{
291 TEST_ASSERT( mbedtls_sha3_self_test( 0 ) == 0 );
292}
293/* END_CASE */