1/* BEGIN_HEADER */ 2#include "mbedtls/sha1.h" 3#include "mbedtls/sha256.h" 4#include "mbedtls/sha512.h" 5/* END_HEADER */ 6 7/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C */ 8void sha1_valid_param( ) 9{ 10 TEST_VALID_PARAM( mbedtls_sha1_free( NULL ) ); 11} 12/* END_CASE */ 13 14/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ 15void 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 54/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C */ 55void mbedtls_sha1( data_t * src_str, data_t * hash ) 56{ 57 unsigned char output[41]; 58 59 memset(output, 0x00, 41); 60 61 62 TEST_ASSERT( mbedtls_sha1_ret( src_str->x, src_str->len, output ) == 0 ); 63 64 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x, 20, hash->len ) == 0 ); 65} 66/* END_CASE */ 67 68/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ 69void 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 */ 125void sha224( data_t * src_str, data_t * hash ) 126{ 127 unsigned char output[57]; 128 129 memset(output, 0x00, 57); 130 131 132 TEST_ASSERT( mbedtls_sha256_ret( src_str->x, src_str->len, output, 1 ) == 0 ); 133 134 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x, 28, hash->len ) == 0 ); 135} 136/* END_CASE */ 137 138/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ 139void mbedtls_sha256( data_t * src_str, data_t * hash ) 140{ 141 unsigned char output[65]; 142 143 memset(output, 0x00, 65); 144 145 146 TEST_ASSERT( mbedtls_sha256_ret( src_str->x, src_str->len, output, 0 ) == 0 ); 147 148 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x, 32, hash->len ) == 0 ); 149} 150/* END_CASE */ 151 152/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */ 153void 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 208/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */ 209void sha384( data_t * src_str, data_t * hash ) 210{ 211 unsigned char output[97]; 212 213 memset(output, 0x00, 97); 214 215 216 TEST_ASSERT( mbedtls_sha512_ret( src_str->x, src_str->len, output, 1 ) == 0 ); 217 218 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x, 48, hash->len ) == 0 ); 219} 220/* END_CASE */ 221 222/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */ 223void mbedtls_sha512( data_t * src_str, data_t * hash ) 224{ 225 unsigned char output[129]; 226 227 memset(output, 0x00, 129); 228 229 230 TEST_ASSERT( mbedtls_sha512_ret( src_str->x, src_str->len, output, 0 ) == 0 ); 231 232 TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x, 64, hash->len ) == 0 ); 233} 234/* END_CASE */ 235 236/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C:MBEDTLS_SELF_TEST */ 237void sha1_selftest( ) 238{ 239 TEST_ASSERT( mbedtls_sha1_self_test( 1 ) == 0 ); 240} 241/* END_CASE */ 242 243/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_SELF_TEST */ 244void sha256_selftest( ) 245{ 246 TEST_ASSERT( mbedtls_sha256_self_test( 1 ) == 0 ); 247} 248/* END_CASE */ 249 250/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:MBEDTLS_SELF_TEST */ 251void sha512_selftest( ) 252{ 253 TEST_ASSERT( mbedtls_sha512_self_test( 1 ) == 0 ); 254} 255/* END_CASE */ 256