blob: c035ae97107bf7d396cb1e1bb23ab2e9b6691280 [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 */
Azim Khan5fcca462018-06-29 11:05:32 +010069void sha224( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +000070{
Paul Bakker367dae42009-06-28 21:50:27 +000071 unsigned char output[57];
72
Paul Bakker367dae42009-06-28 21:50:27 +000073 memset(output, 0x00, 57);
74
Paul Bakker367dae42009-06-28 21:50:27 +000075
Azim Khand30ca132017-06-09 04:32:58 +010076 TEST_ASSERT( mbedtls_sha256_ret( src_str->x, src_str->len, output, 1 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000077
Azim Khand30ca132017-06-09 04:32:58 +010078 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 28, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000079}
Paul Bakker33b43f12013-08-20 11:48:36 +020080/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
Azim Khan5fcca462018-06-29 11:05:32 +010083void mbedtls_sha256( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +000084{
Paul Bakker367dae42009-06-28 21:50:27 +000085 unsigned char output[65];
86
Paul Bakker367dae42009-06-28 21:50:27 +000087 memset(output, 0x00, 65);
88
Paul Bakker367dae42009-06-28 21:50:27 +000089
Azim Khand30ca132017-06-09 04:32:58 +010090 TEST_ASSERT( mbedtls_sha256_ret( src_str->x, src_str->len, output, 0 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000091
Azim Khand30ca132017-06-09 04:32:58 +010092 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 32, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000093}
Paul Bakker33b43f12013-08-20 11:48:36 +020094/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
Azim Khan5fcca462018-06-29 11:05:32 +010097void sha384( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +000098{
Paul Bakker367dae42009-06-28 21:50:27 +000099 unsigned char output[97];
100
Paul Bakker367dae42009-06-28 21:50:27 +0000101 memset(output, 0x00, 97);
102
Paul Bakker367dae42009-06-28 21:50:27 +0000103
Azim Khand30ca132017-06-09 04:32:58 +0100104 TEST_ASSERT( mbedtls_sha512_ret( src_str->x, src_str->len, output, 1 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000105
Azim Khand30ca132017-06-09 04:32:58 +0100106 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 48, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000107}
Paul Bakker33b43f12013-08-20 11:48:36 +0200108/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100111void mbedtls_sha512( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000112{
Paul Bakker367dae42009-06-28 21:50:27 +0000113 unsigned char output[129];
114
Paul Bakker367dae42009-06-28 21:50:27 +0000115 memset(output, 0x00, 129);
116
Paul Bakker367dae42009-06-28 21:50:27 +0000117
Azim Khand30ca132017-06-09 04:32:58 +0100118 TEST_ASSERT( mbedtls_sha512_ret( src_str->x, src_str->len, output, 0 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000119
Azim Khand30ca132017-06-09 04:32:58 +0100120 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 64, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000121}
Paul Bakker33b43f12013-08-20 11:48:36 +0200122/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100125void sha1_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000126{
Andres AG93012e82016-09-09 09:10:28 +0100127 TEST_ASSERT( mbedtls_sha1_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000128}
Paul Bakker33b43f12013-08-20 11:48:36 +0200129/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100132void sha256_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000133{
Andres AG93012e82016-09-09 09:10:28 +0100134 TEST_ASSERT( mbedtls_sha256_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000135}
Paul Bakker33b43f12013-08-20 11:48:36 +0200136/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100139void sha512_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000140{
Andres AG93012e82016-09-09 09:10:28 +0100141 TEST_ASSERT( mbedtls_sha512_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000142}
Paul Bakker33b43f12013-08-20 11:48:36 +0200143/* END_CASE */