blob: 1d646d12424b41b3a3a1e9c355aa48739220d1e7 [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 */
Hanno Becker36beb042018-12-18 14:58:02 +000022void sha256_valid_param( )
23{
24 TEST_VALID_PARAM( mbedtls_sha256_free( NULL ) );
25}
26/* END_CASE */
27
28/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
29void sha256_invalid_param( )
30{
31 mbedtls_sha256_context ctx;
32 unsigned char buf[64] = { 0 };
33 size_t const buflen = sizeof( buf );
34 int valid_type = 0;
35 int invalid_type = 42;
36
37 TEST_INVALID_PARAM( mbedtls_sha256_init( NULL ) );
38
39 TEST_INVALID_PARAM( mbedtls_sha256_clone( NULL, &ctx ) );
40 TEST_INVALID_PARAM( mbedtls_sha256_clone( &ctx, NULL ) );
41
42 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
43 mbedtls_sha256_starts_ret( NULL, valid_type ) );
44 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
45 mbedtls_sha256_starts_ret( &ctx, invalid_type ) );
46
47 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
48 mbedtls_sha256_update_ret( NULL, buf, buflen ) );
49 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
50 mbedtls_sha256_update_ret( &ctx, NULL, buflen ) );
51
52 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
53 mbedtls_sha256_finish_ret( NULL, buf ) );
54 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
55 mbedtls_sha256_finish_ret( &ctx, NULL ) );
56
57 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
58 mbedtls_internal_sha256_process( NULL, buf ) );
59 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
60 mbedtls_internal_sha256_process( &ctx, NULL ) );
61
62 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
63 mbedtls_sha256_ret( NULL, buflen,
64 buf, valid_type ) );
65 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
66 mbedtls_sha256_ret( buf, buflen,
67 NULL, valid_type ) );
68 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
69 mbedtls_sha256_ret( buf, buflen,
70 buf, invalid_type ) );
71
72exit:
73 return;
74}
75/* END_CASE */
76
77/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
Azim Khan5fcca462018-06-29 11:05:32 +010078void sha224( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +000079{
Paul Bakker367dae42009-06-28 21:50:27 +000080 unsigned char output[57];
81
Paul Bakker367dae42009-06-28 21:50:27 +000082 memset(output, 0x00, 57);
83
Paul Bakker367dae42009-06-28 21:50:27 +000084
Azim Khand30ca132017-06-09 04:32:58 +010085 TEST_ASSERT( mbedtls_sha256_ret( src_str->x, src_str->len, output, 1 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000086
Azim Khand30ca132017-06-09 04:32:58 +010087 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 28, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000088}
Paul Bakker33b43f12013-08-20 11:48:36 +020089/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
Azim Khan5fcca462018-06-29 11:05:32 +010092void mbedtls_sha256( data_t * src_str, data_t * hex_hash_string )
Paul Bakker367dae42009-06-28 21:50:27 +000093{
Paul Bakker367dae42009-06-28 21:50:27 +000094 unsigned char output[65];
95
Paul Bakker367dae42009-06-28 21:50:27 +000096 memset(output, 0x00, 65);
97
Paul Bakker367dae42009-06-28 21:50:27 +000098
Azim Khand30ca132017-06-09 04:32:58 +010099 TEST_ASSERT( mbedtls_sha256_ret( src_str->x, src_str->len, output, 0 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000100
Azim Khand30ca132017-06-09 04:32:58 +0100101 TEST_ASSERT( hexcmp( output, hex_hash_string->x, 32, hex_hash_string->len ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000102}
Paul Bakker33b43f12013-08-20 11:48:36 +0200103/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000104
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 */