blob: a5a7b68d55533e69b793be7f5e97fad4a7c1b081 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C */
Azim Khan5fcca462018-06-29 11:05:32 +01008void mbedtls_sha1( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +00009{
Paul Bakker367dae42009-06-28 21:50:27 +000010 unsigned char output[41];
11
Paul Bakker367dae42009-06-28 21:50:27 +000012 memset(output, 0x00, 41);
13
Paul Bakker367dae42009-06-28 21:50:27 +000014
Azim Khand30ca132017-06-09 04:32:58 +010015 TEST_ASSERT( mbedtls_sha1_ret( src_str->x, src_str->len, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000016
Azim Khand30ca132017-06-09 04:32:58 +010017 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 20, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000018}
Paul Bakker33b43f12013-08-20 11:48:36 +020019/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
Azim Khan5fcca462018-06-29 11:05:32 +010022void sha224( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +000023{
Paul Bakker367dae42009-06-28 21:50:27 +000024 unsigned char output[57];
25
Paul Bakker367dae42009-06-28 21:50:27 +000026 memset(output, 0x00, 57);
27
Paul Bakker367dae42009-06-28 21:50:27 +000028
Azim Khand30ca132017-06-09 04:32:58 +010029 TEST_ASSERT( mbedtls_sha256_ret( src_str->x, src_str->len, output, 1 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000030
Azim Khand30ca132017-06-09 04:32:58 +010031 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 28, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000032}
Paul Bakker33b43f12013-08-20 11:48:36 +020033/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
Azim Khan5fcca462018-06-29 11:05:32 +010036void mbedtls_sha256( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +000037{
Paul Bakker367dae42009-06-28 21:50:27 +000038 unsigned char output[65];
39
Paul Bakker367dae42009-06-28 21:50:27 +000040 memset(output, 0x00, 65);
41
Paul Bakker367dae42009-06-28 21:50:27 +000042
Azim Khand30ca132017-06-09 04:32:58 +010043 TEST_ASSERT( mbedtls_sha256_ret( src_str->x, src_str->len, output, 0 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000044
Azim Khand30ca132017-06-09 04:32:58 +010045 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 32, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000046}
Paul Bakker33b43f12013-08-20 11:48:36 +020047/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000048
Hanno Becker686c9a02018-12-18 15:33:14 +000049/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
50void sha512_valid_param( )
51{
52 TEST_VALID_PARAM( mbedtls_sha512_free( NULL ) );
53}
54/* END_CASE */
55
56/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
57void sha512_invalid_param( )
58{
59 mbedtls_sha512_context ctx;
60 unsigned char buf[64] = { 0 };
61 size_t const buflen = sizeof( buf );
62 int valid_type = 0;
63 int invalid_type = 42;
64
65 TEST_INVALID_PARAM( mbedtls_sha512_init( NULL ) );
66
67 TEST_INVALID_PARAM( mbedtls_sha512_clone( NULL, &ctx ) );
68 TEST_INVALID_PARAM( mbedtls_sha512_clone( &ctx, NULL ) );
69
70 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
71 mbedtls_sha512_starts_ret( NULL, valid_type ) );
72 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
73 mbedtls_sha512_starts_ret( &ctx, invalid_type ) );
74
75 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
76 mbedtls_sha512_update_ret( NULL, buf, buflen ) );
77 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
78 mbedtls_sha512_update_ret( &ctx, NULL, buflen ) );
79
80 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
81 mbedtls_sha512_finish_ret( NULL, buf ) );
82 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
83 mbedtls_sha512_finish_ret( &ctx, NULL ) );
84
85 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
86 mbedtls_internal_sha512_process( NULL, buf ) );
87 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
88 mbedtls_internal_sha512_process( &ctx, NULL ) );
89
90 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
91 mbedtls_sha512_ret( NULL, buflen,
92 buf, valid_type ) );
93 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
94 mbedtls_sha512_ret( buf, buflen,
95 NULL, valid_type ) );
96 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
97 mbedtls_sha512_ret( buf, buflen,
98 buf, invalid_type ) );
99
100exit:
101 return;
102}
103/* END_CASE */
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100106void sha384( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000107{
Paul Bakker367dae42009-06-28 21:50:27 +0000108 unsigned char output[97];
109
Paul Bakker367dae42009-06-28 21:50:27 +0000110 memset(output, 0x00, 97);
111
Paul Bakker367dae42009-06-28 21:50:27 +0000112
Azim Khand30ca132017-06-09 04:32:58 +0100113 TEST_ASSERT( mbedtls_sha512_ret( src_str->x, src_str->len, output, 1 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000114
Azim Khand30ca132017-06-09 04:32:58 +0100115 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 48, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000116}
Paul Bakker33b43f12013-08-20 11:48:36 +0200117/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100120void mbedtls_sha512( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000121{
Paul Bakker367dae42009-06-28 21:50:27 +0000122 unsigned char output[129];
123
Paul Bakker367dae42009-06-28 21:50:27 +0000124 memset(output, 0x00, 129);
125
Paul Bakker367dae42009-06-28 21:50:27 +0000126
Azim Khand30ca132017-06-09 04:32:58 +0100127 TEST_ASSERT( mbedtls_sha512_ret( src_str->x, src_str->len, output, 0 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000128
Azim Khand30ca132017-06-09 04:32:58 +0100129 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 64, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000130}
Paul Bakker33b43f12013-08-20 11:48:36 +0200131/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100134void sha1_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000135{
Andres AG93012e82016-09-09 09:10:28 +0100136 TEST_ASSERT( mbedtls_sha1_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000137}
Paul Bakker33b43f12013-08-20 11:48:36 +0200138/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100141void sha256_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000142{
Andres AG93012e82016-09-09 09:10:28 +0100143 TEST_ASSERT( mbedtls_sha256_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000144}
Paul Bakker33b43f12013-08-20 11:48:36 +0200145/* END_CASE */
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100148void sha512_selftest( )
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000149{
Andres AG93012e82016-09-09 09:10:28 +0100150 TEST_ASSERT( mbedtls_sha512_self_test( 1 ) == 0 );
Paul Bakkerf3eedce2009-07-05 11:30:16 +0000151}
Paul Bakker33b43f12013-08-20 11:48:36 +0200152/* END_CASE */