Skip to content

Commit f913940

Browse files
author
Krzysztof Stachowiak
committed
Prepare test program to work in different MD implementations
1 parent 8c412ed commit f913940

File tree

1 file changed

+77
-15
lines changed

1 file changed

+77
-15
lines changed

TESTS/mbedtls/multi/main.cpp

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,68 @@
2323

2424
#include "mbedtls/sha256.h"
2525

26+
/**
27+
* \name SECTION: Compatibility code
28+
*
29+
* Depending on whether the alternative (hatdware accelerated) hashing
30+
* functions are provided or not, different API should be used for hashing.
31+
* \{
32+
*/
33+
34+
#if defined(MBEDTLS_SHA256_ALT)
35+
36+
/**
37+
* \brief This function starts a SHA-256 checksum calculation.
38+
*
39+
* \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
40+
*
41+
* \param ctx The SHA-256 context to initialize.
42+
* \param is224 Determines which function to use.
43+
* <ul><li>0: Use SHA-256.</li>
44+
* <li>1: Use SHA-224.</li></ul>
45+
*/
46+
void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
47+
int is224 )
48+
{
49+
mbedtls_sha256_starts_ret( ctx, is224 );
50+
}
51+
52+
/**
53+
* \brief This function feeds an input buffer into an ongoing
54+
* SHA-256 checksum calculation.
55+
*
56+
* \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
57+
*
58+
* \param ctx The SHA-256 context to initialize.
59+
* \param input The buffer holding the data.
60+
* \param ilen The length of the input data.
61+
*/
62+
void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
63+
const unsigned char *input,
64+
size_t ilen )
65+
{
66+
mbedtls_sha256_update_ret( ctx, input, ilen );
67+
}
68+
69+
/**
70+
* \brief This function finishes the SHA-256 operation, and writes
71+
* the result to the output buffer.
72+
*
73+
* \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
74+
*
75+
* \param ctx The SHA-256 context.
76+
* \param output The SHA-224or SHA-256 checksum result.
77+
*/
78+
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
79+
unsigned char output[32] )
80+
{
81+
mbedtls_sha256_finish_ret( ctx, output );
82+
}
83+
84+
#endif /* defined(MBEDTLS_SHA256_ALT) */
85+
86+
/* \} name SECTION: Compatibility code */
87+
2688
using namespace utest::v1;
2789

2890
#if defined(MBEDTLS_SHA256_C)
@@ -41,18 +103,18 @@ void test_case_sha256_split() {
41103
mbedtls_sha256_context ctx;
42104
printf("test sha256\n");
43105
mbedtls_sha256_init( &ctx );
44-
mbedtls_sha256_starts_ret( &ctx, 0);
106+
mbedtls_sha256_starts( &ctx, 0);
45107
#if 0
46108
printf("test not splitted\n");
47109
mbedtls_sha256_update( &ctx, test_buf, 168 );
48110
#else
49111
printf("test splitted into 3 pieces\n");
50-
mbedtls_sha256_update_ret( &ctx, test_buf, 2 );
51-
mbedtls_sha256_update_ret( &ctx, test_buf+2, 66 );
52-
mbedtls_sha256_update_ret( &ctx, test_buf+68, 100 );
112+
mbedtls_sha256_update( &ctx, test_buf, 2 );
113+
mbedtls_sha256_update( &ctx, test_buf+2, 66 );
114+
mbedtls_sha256_update( &ctx, test_buf+68, 100 );
53115
#endif
54116

55-
mbedtls_sha256_finish_ret( &ctx, outsum );
117+
mbedtls_sha256_finish( &ctx, outsum );
56118
mbedtls_sha256_free( &ctx );
57119

58120
printf("\nreceived result : ");
@@ -98,29 +160,29 @@ void test_case_sha256_multi() {
98160
mbedtls_sha256_init( &ctx2);
99161
mbedtls_sha256_init( &ctx3);
100162
//Start both contexts
101-
mbedtls_sha256_starts_ret( &ctx1, 0);
102-
mbedtls_sha256_starts_ret( &ctx2, 0);
163+
mbedtls_sha256_starts( &ctx1, 0);
164+
mbedtls_sha256_starts( &ctx2, 0);
103165

104166
printf("upd ctx1\n");
105-
mbedtls_sha256_update_ret( &ctx1, test_buf, 56 );
167+
mbedtls_sha256_update( &ctx1, test_buf, 56 );
106168
printf("upd ctx2\n");
107-
mbedtls_sha256_update_ret( &ctx2, test_buf, 66 );
169+
mbedtls_sha256_update( &ctx2, test_buf, 66 );
108170
printf("finish ctx1\n");
109-
mbedtls_sha256_finish_ret( &ctx1, outsum1 );
171+
mbedtls_sha256_finish( &ctx1, outsum1 );
110172
printf("upd ctx2\n");
111-
mbedtls_sha256_update_ret( &ctx2, test_buf+66, 46 );
173+
mbedtls_sha256_update( &ctx2, test_buf+66, 46 );
112174
printf("clone ctx2 in ctx3\n");
113175
mbedtls_sha256_clone(&ctx3, (const mbedtls_sha256_context *)&ctx2);
114176
printf("free ctx1\n");
115177
mbedtls_sha256_free( &ctx1 );
116178
printf("upd ctx2\n");
117-
mbedtls_sha256_update_ret( &ctx2, test_buf+112, 56 );
179+
mbedtls_sha256_update( &ctx2, test_buf+112, 56 );
118180
printf("upd ctx3 with different values than ctx2\n");
119-
mbedtls_sha256_update_ret( &ctx3, test_buf2, 56 );
181+
mbedtls_sha256_update( &ctx3, test_buf2, 56 );
120182
printf("finish ctx2\n");
121-
mbedtls_sha256_finish_ret( &ctx2, outsum2 );
183+
mbedtls_sha256_finish( &ctx2, outsum2 );
122184
printf("finish ctx3\n");
123-
mbedtls_sha256_finish_ret( &ctx3, outsum3 );
185+
mbedtls_sha256_finish( &ctx3, outsum3 );
124186
printf("free ctx2\n");
125187
mbedtls_sha256_free( &ctx2 );
126188
printf("free ctx3\n");

0 commit comments

Comments
 (0)