Skip to content

Commit 8ea9ca0

Browse files
committed
Add test of sha256_clone function
1 parent 021b84a commit 8ea9ca0

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

TESTS/mbedtls/multi/main.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ void test_case_sha256_split() {
6767
/* Tests that treating 2 sha256 objects in // does not impact the result */
6868
void test_case_sha256_multi() {
6969
const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"};
70+
const unsigned char test_buf2[] = {"abcdefghijklmnopqrstuvwxyz012345678901234567890123456789"};
71+
7072
// sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
7173
const unsigned char test_sum1[] =
7274
{ 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
@@ -79,15 +81,23 @@ void test_case_sha256_multi() {
7981
0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93,
8082
0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0,
8183
0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C};
82-
unsigned char outsum1[32], outsum2[32];
84+
// sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdefghijklmnopqrstuvwxyz012345678901234567890123456789
85+
const unsigned char test_sum3[] =
86+
{ 0x6D, 0x5D, 0xDB, 0x5F, 0x4A, 0x94, 0xAB, 0x7E,
87+
0x5C, 0xF7, 0x9A, 0xD8, 0x3F, 0x58, 0xD3, 0x97,
88+
0xFE, 0x79, 0xFB, 0x0D, 0x79, 0xB2, 0x0D, 0x22,
89+
0xFF, 0x95, 0x9F, 0x04, 0xA2, 0xE4, 0x6C, 0x68};
90+
unsigned char outsum1[32], outsum2[32], outsum3[32];
8391
int i;
8492

8593
mbedtls_sha256_context ctx1;
8694
mbedtls_sha256_context ctx2;
95+
mbedtls_sha256_context ctx3;
8796
printf("test sha256_multi\n");
8897
//Init both contexts
89-
mbedtls_sha256_init( &ctx1 );
90-
mbedtls_sha256_init( &ctx2 );
98+
mbedtls_sha256_init( &ctx1);
99+
mbedtls_sha256_init( &ctx2);
100+
mbedtls_sha256_init( &ctx3);
91101
//Start both contexts
92102
mbedtls_sha256_starts( &ctx1, 0);
93103
mbedtls_sha256_starts( &ctx2, 0);
@@ -100,25 +110,36 @@ void test_case_sha256_multi() {
100110
mbedtls_sha256_finish( &ctx1, outsum1 );
101111
printf("upd ctx2\n");
102112
mbedtls_sha256_update( &ctx2, test_buf+66, 46 );
113+
printf("clone ctx2 in ctx3\n");
114+
mbedtls_sha256_clone(&ctx3, (const mbedtls_sha256_context *)&ctx2);
103115
printf("free ctx1\n");
104116
mbedtls_sha256_free( &ctx1 );
105117
printf("upd ctx2\n");
106118
mbedtls_sha256_update( &ctx2, test_buf+112, 56 );
119+
printf("upd ctx3 with different values than ctx2\n");
120+
mbedtls_sha256_update( &ctx3, test_buf2, 56 );
107121
printf("finish ctx2\n");
108122
mbedtls_sha256_finish( &ctx2, outsum2 );
123+
printf("finish ctx3\n");
124+
mbedtls_sha256_finish( &ctx3, outsum3 );
109125
printf("free ctx2\n");
110126
mbedtls_sha256_free( &ctx2 );
111-
127+
printf("free ctx3\n");
128+
mbedtls_sha256_free( &ctx3 );
129+
112130
printf("\nreceived result ctx1 : ");
113131
for (i=0;i<32;i++) { printf("%02X",outsum1[i]);}
114132
printf("\nawaited result : 248D6A61D20638B8E5C026930C3E6039A33CE45964FF216F6ECEDD19DB06C1\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
115133
printf("\nreceived result ctx2 : ");
116134
for (i=0;i<32;i++) { printf("%02X",outsum2[i]);}
117135
printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
118-
136+
printf("\nreceived result ctx3 : ");
137+
for (i=0;i<32;i++) { printf("%02X",outsum3[i]);}
138+
printf("\nawaited result : 6D5DDB5F4A94AB7E5CF79AD83F58D397FE79FB0D79B20D22FF959F04A2E46C68\n"); // for 2*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq+3*0123456789
119139
printf("\nend of test sha256\n");
120140
TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum1, test_sum1,32);
121141
TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum2, test_sum2,32);
142+
TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum3, test_sum3,32);
122143
}
123144
#endif //MBEDTLS_SHA256_C
124145

0 commit comments

Comments
 (0)