blob: e621f49cdbaee6551cb1f7488dce0780a75458bc [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"
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00006
Hanno Beckerd22df582018-12-18 16:39:45 +00007/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C */
Hanno Becker0e244732018-12-18 11:22:55 +00008void sha1_valid_param( )
9{
10 TEST_VALID_PARAM( mbedtls_sha1_free( NULL ) );
11}
12/* END_CASE */
13
Hanno Beckerd22df582018-12-18 16:39:45 +000014/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
Hanno Becker0e244732018-12-18 11:22:55 +000015void sha1_invalid_param( )
16{
17 mbedtls_sha1_context ctx;
18 unsigned char buf[64] = { 0 };
19 size_t const buflen = sizeof( buf );
20
21 TEST_INVALID_PARAM( mbedtls_sha1_init( NULL ) );
22
23 TEST_INVALID_PARAM( mbedtls_sha1_clone( NULL, &ctx ) );
24 TEST_INVALID_PARAM( mbedtls_sha1_clone( &ctx, NULL ) );
25
26 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA1_BAD_INPUT_DATA,
27 mbedtls_sha1_starts_ret( NULL ) );
28
29 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA1_BAD_INPUT_DATA,
30 mbedtls_sha1_update_ret( NULL, buf, buflen ) );
31 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA1_BAD_INPUT_DATA,
32 mbedtls_sha1_update_ret( &ctx, NULL, buflen ) );
33
34 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA1_BAD_INPUT_DATA,
35 mbedtls_sha1_finish_ret( NULL, buf ) );
36 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA1_BAD_INPUT_DATA,
37 mbedtls_sha1_finish_ret( &ctx, NULL ) );
38
39 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA1_BAD_INPUT_DATA,
40 mbedtls_internal_sha1_process( NULL, buf ) );
41 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA1_BAD_INPUT_DATA,
42 mbedtls_internal_sha1_process( &ctx, NULL ) );
43
44 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA1_BAD_INPUT_DATA,
45 mbedtls_sha1_ret( NULL, buflen, buf ) );
46 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA1_BAD_INPUT_DATA,
47 mbedtls_sha1_ret( buf, buflen, NULL ) );
48
49exit:
50 return;
51}
52/* END_CASE */
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C */
Azim Khan5fcca462018-06-29 11:05:32 +010055void mbedtls_sha1( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +000056{
Paul Bakker367dae42009-06-28 21:50:27 +000057 unsigned char output[41];
58
Paul Bakker367dae42009-06-28 21:50:27 +000059 memset(output, 0x00, 41);
60
Paul Bakker367dae42009-06-28 21:50:27 +000061
Azim Khand30ca132017-06-09 04:32:58 +010062 TEST_ASSERT( mbedtls_sha1_ret( src_str->x, src_str->len, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000063
Azim Khand30ca132017-06-09 04:32:58 +010064 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 20, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000065}
Paul Bakker33b43f12013-08-20 11:48:36 +020066/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
Hanno Becker36beb042018-12-18 14:58:02 +000069void sha256_valid_param( )
70{
71 TEST_VALID_PARAM( mbedtls_sha256_free( NULL ) );
72}
73/* END_CASE */
74
75/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
76void sha256_invalid_param( )
77{
78 mbedtls_sha256_context ctx;
79 unsigned char buf[64] = { 0 };
80 size_t const buflen = sizeof( buf );
81 int valid_type = 0;
82 int invalid_type = 42;
83
84 TEST_INVALID_PARAM( mbedtls_sha256_init( NULL ) );
85
86 TEST_INVALID_PARAM( mbedtls_sha256_clone( NULL, &ctx ) );
87 TEST_INVALID_PARAM( mbedtls_sha256_clone( &ctx, NULL ) );
88
89 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
90 mbedtls_sha256_starts_ret( NULL, valid_type ) );
91 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
92 mbedtls_sha256_starts_ret( &ctx, invalid_type ) );
93
94 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
95 mbedtls_sha256_update_ret( NULL, buf, buflen ) );
96 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
97 mbedtls_sha256_update_ret( &ctx, NULL, buflen ) );
98
99 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
100 mbedtls_sha256_finish_ret( NULL, buf ) );
101 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
102 mbedtls_sha256_finish_ret( &ctx, NULL ) );
103
104 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
105 mbedtls_internal_sha256_process( NULL, buf ) );
106 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
107 mbedtls_internal_sha256_process( &ctx, NULL ) );
108
109 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
110 mbedtls_sha256_ret( NULL, buflen,
111 buf, valid_type ) );
112 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
113 mbedtls_sha256_ret( buf, buflen,
114 NULL, valid_type ) );
115 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
116 mbedtls_sha256_ret( buf, buflen,
117 buf, invalid_type ) );
118
119exit:
120 return;
121}
122/* END_CASE */
123
124/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100125void sha224( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000126{
Paul Bakker367dae42009-06-28 21:50:27 +0000127 unsigned char output[57];
128
Paul Bakker367dae42009-06-28 21:50:27 +0000129 memset(output, 0x00, 57);
130
Paul Bakker367dae42009-06-28 21:50:27 +0000131
Azim Khand30ca132017-06-09 04:32:58 +0100132 TEST_ASSERT( mbedtls_sha256_ret( src_str->x, src_str->len, output, 1 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000133
Azim Khand30ca132017-06-09 04:32:58 +0100134 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 28, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000135}
Paul Bakker33b43f12013-08-20 11:48:36 +0200136/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100139void mbedtls_sha256( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000140{
Paul Bakker367dae42009-06-28 21:50:27 +0000141 unsigned char output[65];
142
Paul Bakker367dae42009-06-28 21:50:27 +0000143 memset(output, 0x00, 65);
144
Paul Bakker367dae42009-06-28 21:50:27 +0000145
Azim Khand30ca132017-06-09 04:32:58 +0100146 TEST_ASSERT( mbedtls_sha256_ret( src_str->x, src_str->len, output, 0 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000147
Azim Khand30ca132017-06-09 04:32:58 +0100148 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 32, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000149}
Paul Bakker33b43f12013-08-20 11:48:36 +0200150/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
Hanno Becker686c9a02018-12-18 15:33:14 +0000153void sha512_valid_param( )
154{
155 TEST_VALID_PARAM( mbedtls_sha512_free( NULL ) );
156}
157/* END_CASE */
158
159/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
160void sha512_invalid_param( )
161{
162 mbedtls_sha512_context ctx;
163 unsigned char buf[64] = { 0 };
164 size_t const buflen = sizeof( buf );
165 int valid_type = 0;
166 int invalid_type = 42;
167
168 TEST_INVALID_PARAM( mbedtls_sha512_init( NULL ) );
169
170 TEST_INVALID_PARAM( mbedtls_sha512_clone( NULL, &ctx ) );
171 TEST_INVALID_PARAM( mbedtls_sha512_clone( &ctx, NULL ) );
172
173 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
174 mbedtls_sha512_starts_ret( NULL, valid_type ) );
175 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
176 mbedtls_sha512_starts_ret( &ctx, invalid_type ) );
177
178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
179 mbedtls_sha512_update_ret( NULL, buf, buflen ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
181 mbedtls_sha512_update_ret( &ctx, NULL, buflen ) );
182
183 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
184 mbedtls_sha512_finish_ret( NULL, buf ) );
185 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
186 mbedtls_sha512_finish_ret( &ctx, NULL ) );
187
188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
189 mbedtls_internal_sha512_process( NULL, buf ) );
190 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
191 mbedtls_internal_sha512_process( &ctx, NULL ) );
192
193 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
194 mbedtls_sha512_ret( NULL, buflen,
195 buf, valid_type ) );
196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
197 mbedtls_sha512_ret( buf, buflen,
198 NULL, valid_type ) );
199 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
200 mbedtls_sha512_ret( buf, buflen,
201 buf, invalid_type ) );
202
203exit:
204 return;
205}
206/* END_CASE */
207
Paul Bakker68a4fce2013-08-20 12:42:31 +0200208/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100209void sha384( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000210{
Paul Bakker367dae42009-06-28 21:50:27 +0000211 unsigned char output[97];
212
Paul Bakker367dae42009-06-28 21:50:27 +0000213 memset(output, 0x00, 97);
214
Paul Bakker367dae42009-06-28 21:50:27 +0000215
Azim Khand30ca132017-06-09 04:32:58 +0100216 TEST_ASSERT( mbedtls_sha512_ret( src_str->x, src_str->len, output, 1 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000217
Azim Khand30ca132017-06-09 04:32:58 +0100218 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 48, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000219}
Paul Bakker33b43f12013-08-20 11:48:36 +0200220/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100223void mbedtls_sha512( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000224{
Paul Bakker367dae42009-06-28 21:50:27 +0000225 unsigned char output[129];
226
Paul Bakker367dae42009-06-28 21:50:27 +0000227 memset(output, 0x00, 129);
228
Paul Bakker367dae42009-06-28 21:50:27 +0000229
Azim Khand30ca132017-06-09 04:32:58 +0100230 TEST_ASSERT( mbedtls_sha512_ret( src_str->x, src_str->len, output, 0 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000231
Azim Khand30ca132017-06-09 04:32:58 +0100232 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 64, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000233}
Paul Bakker33b43f12013-08-20 11:48:36 +0200234/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100237void sha1_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000238{
Andres AG93012e82016-09-09 09:10:28 +0100239 TEST_ASSERT( mbedtls_sha1_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000240}
Paul Bakker33b43f12013-08-20 11:48:36 +0200241/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100244void sha256_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000245{
Andres AG93012e82016-09-09 09:10:28 +0100246 TEST_ASSERT( mbedtls_sha256_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000247}
Paul Bakker33b43f12013-08-20 11:48:36 +0200248/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100251void sha512_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000252{
Andres AG93012e82016-09-09 09:10:28 +0100253 TEST_ASSERT( mbedtls_sha512_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000254}
Paul Bakker33b43f12013-08-20 11:48:36 +0200255/* END_CASE */