1 /*
2  * Copyright (c) 2001-2021, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <string.h>
12 
13 #include <limits.h>
14 
15 /* mbedtls lib */
16 #include "mbedtls/sha1.h"
17 #include "mbedtls/sha256.h"
18 #include "mbedtls/sha512.h"
19 #include "mbedtls/timing.h"
20 
21 /* local */
22 #include "run_integration_pal_log.h"
23 #include "run_integration_test.h"
24 #include "run_integration_helper.h"
25 
26 /************************************************************
27  *
28  * static functions prototypes
29  *
30  ************************************************************/
31 static RunItError_t runIt_sha1Test(void);
32 static RunItError_t runIt_sha224Test(void);
33 static RunItError_t runIt_sha256Test(void);
34 static RunItError_t runIt_sha384Test(void);
35 static RunItError_t runIt_sha512Test(void);
36 
37 /************************************************************
38  *
39  * static functions
40  *
41  ************************************************************/
runIt_sha1Test(void)42 static RunItError_t runIt_sha1Test(void)
43 {
44     RunItError_t rc = RUNIT_ERROR__OK;
45 
46 #if defined(MBEDTLS_SHA1_C)
47     /* https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA1.pdf */
48     static const char* INPUT_MESSAGE = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
49     static const uint8_t DIGEST[] = { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 };
50 
51     unsigned char sha1sum[20];
52     RunItPtr ctxPtr;
53     RunItPtr ctxClonedPtr;
54 
55     mbedtls_sha1_context *pCtx = NULL;
56     mbedtls_sha1_context *pCtxCloned = NULL;
57 
58     const char* TEST_NAME = "SHA1";
59     RUNIT_SUB_TEST_START(TEST_NAME);
60 
61     ALLOC_STRUCT(mbedtls_sha1_context, ctxPtr, pCtx);
62     ALLOC_STRUCT(mbedtls_sha1_context, ctxClonedPtr, pCtxCloned);
63 
64     /* Initialize sha engine */
65     RUNIT_API(mbedtls_sha1_init(pCtx));
66     RUNIT_ASSERT(mbedtls_sha1_starts_ret(pCtx) == 0);
67     RUNIT_ASSERT(mbedtls_sha1_update_ret(pCtx, (const unsigned char*)INPUT_MESSAGE, strlen(INPUT_MESSAGE)) == 0);
68     RUNIT_API(mbedtls_sha1_clone(pCtxCloned, pCtx));
69     RUNIT_ASSERT(mbedtls_sha1_finish_ret(pCtxCloned, sha1sum) == 0);
70 
71     RUNIT_PRINT_BUF(sha1sum, 20, "result");
72 
73     /* compare result */
74     RUNIT_ASSERT(memcmp(sha1sum, DIGEST, 20) == 0);
75 
76     /* test the wrapper function too */
77     memset(sha1sum, 0, sizeof(sha1sum));
78 
79     RUNIT_API(mbedtls_sha1((const unsigned char*)INPUT_MESSAGE, strlen(INPUT_MESSAGE), sha1sum));
80 
81     /* compare result */
82     RUNIT_ASSERT(memcmp(sha1sum, DIGEST, 20) == 0);
83 
84 bail:
85     RUNIT_API(mbedtls_sha1_free(pCtx));
86     RUNIT_API(mbedtls_sha1_free(pCtxCloned));
87 
88     FREE_IF_NOT_NULL(ctxPtr);
89     FREE_IF_NOT_NULL(ctxClonedPtr);
90 
91     RUNIT_SUB_TEST_RESULT(TEST_NAME);
92 #endif /* MBEDTLS_SHA1_C */
93     return rc;
94 }
95 
runIt_sha224Test(void)96 static RunItError_t runIt_sha224Test(void)
97 {
98     RunItError_t rc = RUNIT_ERROR__OK;
99 
100 #if defined(MBEDTLS_SHA256_C)
101     const int IS_SHA_224 = 1;
102 
103     /* https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA224.pdf */
104     static const char* INPUT_MESSAGE = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
105     static const uint8_t DIGEST[] = { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC, 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50, 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19, 0x52, 0x52, 0x25, 0x25 };
106 
107     unsigned char sha224sum[28];
108     RunItPtr ctxPtr;
109     RunItPtr ctxClonedPtr;
110 
111     mbedtls_sha256_context *pCtx = NULL;
112     mbedtls_sha256_context *pCtxCloned = NULL;
113 
114     const char* TEST_NAME = "SHA224";
115     RUNIT_SUB_TEST_START(TEST_NAME);
116 
117     ALLOC_STRUCT(mbedtls_sha256_context, ctxPtr, pCtx);
118     ALLOC_STRUCT(mbedtls_sha256_context, ctxClonedPtr, pCtxCloned);
119 
120     /* Initialize sha engine */
121     RUNIT_API(mbedtls_sha256_init(pCtx));
122     RUNIT_ASSERT(mbedtls_sha256_starts(pCtx, IS_SHA_224) == 0);
123     RUNIT_ASSERT(mbedtls_sha256_update(pCtx, (const unsigned char*)INPUT_MESSAGE, strlen(INPUT_MESSAGE)) == 0);
124     RUNIT_API(mbedtls_sha256_clone(pCtxCloned, pCtx));
125     RUNIT_ASSERT(mbedtls_sha256_finish(pCtxCloned, sha224sum) == 0);
126 
127     RUNIT_PRINT_BUF(sha224sum, 28, "result");
128 
129     /* compare result */
130     RUNIT_ASSERT(memcmp(sha224sum, DIGEST, 28) == 0);
131 
132     /* test the wrapper function too */
133     memset(sha224sum, 0, sizeof(sha224sum));
134 
135     RUNIT_API(mbedtls_sha256((const unsigned char*)INPUT_MESSAGE, strlen(INPUT_MESSAGE), sha224sum, IS_SHA_224));
136 
137     /* compare result */
138     RUNIT_ASSERT(memcmp(sha224sum, DIGEST, 28) == 0);
139 
140 bail:
141     RUNIT_API(mbedtls_sha256_free(pCtx));
142     RUNIT_API(mbedtls_sha256_free(pCtxCloned));
143 
144     FREE_IF_NOT_NULL(ctxPtr);
145     FREE_IF_NOT_NULL(ctxClonedPtr);
146 
147     RUNIT_SUB_TEST_RESULT(TEST_NAME);
148 #endif /* MBEDTLS_SHA256_C */
149     return rc;
150 }
151 
runIt_sha256Test(void)152 static RunItError_t runIt_sha256Test(void)
153 {
154     RunItError_t rc = RUNIT_ERROR__OK;
155 #if defined(MBEDTLS_SHA256_C)
156     const int IS_SHA_224 = 0;
157 
158     /* https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA256.pdf */
159     static const char* INPUT_MESSAGE = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
160     static const uint8_t DIGEST[] = { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 };
161 
162     unsigned char sha256sum[32];
163     RunItPtr ctxPtr;
164     RunItPtr ctxClonedPtr;
165 
166     mbedtls_sha256_context *pCtx = NULL;
167     mbedtls_sha256_context *pCtxCloned = NULL;
168 
169     const char* TEST_NAME = "SHA256";
170     RUNIT_SUB_TEST_START(TEST_NAME);
171 
172     ALLOC_STRUCT(mbedtls_sha256_context, ctxPtr, pCtx);
173     ALLOC_STRUCT(mbedtls_sha256_context, ctxClonedPtr, pCtxCloned);
174 
175     /* Initialize sha engine */
176     RUNIT_API(mbedtls_sha256_init(pCtx));
177     RUNIT_ASSERT(mbedtls_sha256_starts(pCtx, IS_SHA_224) == 0);
178     RUNIT_ASSERT(mbedtls_sha256_update(pCtx, (const unsigned char*)INPUT_MESSAGE, strlen(INPUT_MESSAGE)) == 0);
179     RUNIT_API(mbedtls_sha256_clone(pCtxCloned, pCtx));
180     RUNIT_ASSERT(mbedtls_sha256_finish(pCtxCloned, sha256sum) == 0);
181 
182     RUNIT_PRINT_BUF(sha256sum, 32, "result");
183 
184     /* compare result */
185     RUNIT_ASSERT(memcmp(sha256sum, DIGEST, 32) == 0);
186 
187     /* test the wrapper function too */
188     memset(sha256sum, 0, sizeof(sha256sum));
189     RUNIT_API(mbedtls_sha256((const unsigned char*)INPUT_MESSAGE, strlen(INPUT_MESSAGE), sha256sum, IS_SHA_224));
190 
191     /* compare result */
192     RUNIT_ASSERT(memcmp(sha256sum, DIGEST, 32) == 0);
193 
194 bail:
195     RUNIT_API(mbedtls_sha256_free(pCtx));
196     RUNIT_API(mbedtls_sha256_free(pCtxCloned));
197 
198     FREE_IF_NOT_NULL(ctxPtr);
199     FREE_IF_NOT_NULL(ctxClonedPtr);
200 
201     RUNIT_SUB_TEST_RESULT(TEST_NAME);
202 #endif /* MBEDTLS_SHA256_C */
203     return rc;
204 }
205 
runIt_sha384Test(void)206 static RunItError_t runIt_sha384Test(void)
207 {
208     RunItError_t rc = RUNIT_ERROR__OK;
209 #if defined(MBEDTLS_SHA512_C)
210     const int IS_SHA_384 = 1;
211 
212     /* https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA384.pdf */
213     static const char* INPUT_MESSAGE = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
214     static const uint8_t DIGEST[] = { 0x09,0x33,0x0C,0x33,0xF7,0x11,0x47,0xE8,0x3D,0x19,0x2F,0xC7,0x82,0xCD,0x1B,0x47,0x53,0x11,0x1B,0x17,0x3B,0x3B,0x05,0xD2,0x2F,0xA0,0x80,0x86,0xE3,0xB0,0xF7,0x12,0xFC,0xC7,0xC7,0x1A,0x55,0x7E,0x2D,0xB9,0x66,0xC3,0xE9,0xFA,0x91,0x74,0x60,0x39};
215 
216     unsigned char sha512sum[48];
217     RunItPtr ctxPtr;
218     RunItPtr ctxClonedPtr;
219 
220     mbedtls_sha512_context *pCtx = NULL;
221     mbedtls_sha512_context *pCtxCloned = NULL;
222 
223     const char* TEST_NAME = "SHA384";
224     RUNIT_SUB_TEST_START(TEST_NAME);
225 
226     ALLOC_STRUCT(mbedtls_sha512_context, ctxPtr, pCtx);
227     ALLOC_STRUCT(mbedtls_sha512_context, ctxClonedPtr, pCtxCloned);
228 
229     /* Initialize sha engine */
230     RUNIT_API(mbedtls_sha512_init(pCtx));
231     RUNIT_API(mbedtls_sha512_starts(pCtx, IS_SHA_384));
232     RUNIT_API(mbedtls_sha512_update(pCtx, (const unsigned char*)INPUT_MESSAGE, strlen(INPUT_MESSAGE)));
233     RUNIT_API(mbedtls_sha512_clone(pCtxCloned, pCtx));
234     RUNIT_API(mbedtls_sha512_finish(pCtxCloned, sha512sum));
235 
236     RUNIT_PRINT_BUF(sha512sum, 48, "result");
237 
238     /* compare result */
239     RUNIT_ASSERT(memcmp(sha512sum, DIGEST, 48) == 0);
240 
241     /* test the wrapper function too */
242     memset(sha512sum, 0, sizeof(sha512sum));
243     RUNIT_API(mbedtls_sha512((const unsigned char*)INPUT_MESSAGE, strlen(INPUT_MESSAGE), sha512sum, IS_SHA_384));
244 
245     /* compare result */
246     RUNIT_ASSERT(memcmp(sha512sum, DIGEST, 48) == 0);
247 
248 bail:
249     RUNIT_API(mbedtls_sha512_free(pCtx));
250     RUNIT_API(mbedtls_sha512_free(pCtxCloned));
251 
252     FREE_IF_NOT_NULL(ctxPtr);
253     FREE_IF_NOT_NULL(ctxClonedPtr);
254 
255     RUNIT_SUB_TEST_RESULT(TEST_NAME);
256 #endif /* MBEDTLS_SHA512_C */
257     return rc;
258 }
259 
runIt_sha512Test(void)260 static RunItError_t runIt_sha512Test(void)
261 {
262     RunItError_t rc = RUNIT_ERROR__OK;
263 #if defined(MBEDTLS_SHA512_C)
264     const int IS_SHA_384 = 0;
265 
266     /* https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA256.pdf */
267     static const char* INPUT_MESSAGE = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
268     static const uint8_t DIGEST[] = { 0x8E,0x95,0x9B,0x75,0xDA,0xE3,0x13,0xDA,0x8C,0xF4,0xF7,0x28,0x14,0xFC,0x14,0x3F,0x8F,0x77,0x79,0xC6,0xEB,0x9F,0x7F,0xA1,0x72,0x99,0xAE,0xAD,0xB6,0x88,0x90,0x18,0x50,0x1D,0x28,0x9E,0x49,0x00,0xF7,0xE4,0x33,0x1B,0x99,0xDE,0xC4,0xB5,0x43,0x3A,0xC7,0xD3,0x29,0xEE,0xB6,0xDD,0x26,0x54,0x5E,0x96,0xE5,0x5B,0x87,0x4B,0xE9,0x09 };
269 
270     unsigned char sha512sum[64];
271     RunItPtr ctxPtr;
272     RunItPtr ctxClonedPtr;
273 
274     mbedtls_sha512_context *pCtx = NULL;
275     mbedtls_sha512_context *pCtxCloned = NULL;
276 
277     const char* TEST_NAME = "SHA512";
278     RUNIT_SUB_TEST_START(TEST_NAME);
279 
280     ALLOC_STRUCT(mbedtls_sha512_context, ctxPtr, pCtx);
281     ALLOC_STRUCT(mbedtls_sha512_context, ctxClonedPtr, pCtxCloned);
282 
283     /* Initialize sha engine */
284     RUNIT_API(mbedtls_sha512_init(pCtx));
285     RUNIT_API(mbedtls_sha512_starts(pCtx, IS_SHA_384));
286     RUNIT_API(mbedtls_sha512_update(pCtx, (const unsigned char*)INPUT_MESSAGE, strlen(INPUT_MESSAGE)));
287     RUNIT_API(mbedtls_sha512_clone(pCtxCloned, pCtx));
288     RUNIT_API(mbedtls_sha512_finish(pCtxCloned, sha512sum));
289 
290     RUNIT_PRINT_BUF(sha512sum, 64, "result");
291 
292     /* compare result */
293     RUNIT_ASSERT(memcmp(sha512sum, DIGEST, 64) == 0);
294 
295     /* test the wrapper function too */
296     memset(sha512sum, 0, sizeof(sha512sum));
297     RUNIT_API(mbedtls_sha512((const unsigned char*)INPUT_MESSAGE, strlen(INPUT_MESSAGE), sha512sum, IS_SHA_384));
298 
299     /* compare result */
300     RUNIT_ASSERT(memcmp(sha512sum, DIGEST, 64) == 0);
301 
302 bail:
303     RUNIT_API(mbedtls_sha512_free(pCtx));
304     RUNIT_API(mbedtls_sha512_free(pCtxCloned));
305 
306     FREE_IF_NOT_NULL(ctxPtr);
307     FREE_IF_NOT_NULL(ctxClonedPtr);
308 
309     RUNIT_SUB_TEST_RESULT(TEST_NAME);
310 #endif /* MBEDTLS_SHA512_C */
311     return rc;
312 }
313 /************************************************************
314  *
315  * public functions
316  *
317  ************************************************************/
runIt_shaTest(void)318 RunItError_t runIt_shaTest(void)
319 {
320     RunItError_t rc = RUNIT_ERROR__OK;
321 
322     const char* TEST_NAME = "HASH";
323     RUNIT_TEST_START(TEST_NAME);
324 
325     RUNIT_ASSERT(runIt_sha1Test() == RUNIT_ERROR__OK);
326     RUNIT_ASSERT(runIt_sha224Test() == RUNIT_ERROR__OK);
327     RUNIT_ASSERT(runIt_sha256Test() == RUNIT_ERROR__OK);
328     RUNIT_ASSERT(runIt_sha384Test() == RUNIT_ERROR__OK);
329     RUNIT_ASSERT(runIt_sha512Test() == RUNIT_ERROR__OK);
330 
331 bail:
332     RUNIT_TEST_RESULT(TEST_NAME);
333     return rc;
334 }
335