1 /*############################################################################
2 # Copyright 2017 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 ############################################################################*/
16 
17 /*
18  *  Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
19  *
20  *  Redistribution and use in source and binary forms, with or without
21  *  modification, are permitted provided that the following conditions are met:
22  *
23  *    - Redistributions of source code must retain the above copyright notice,
24  *     this list of conditions and the following disclaimer.
25  *
26  *    - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in the
28  *    documentation and/or other materials provided with the distribution.
29  *
30  *    - Neither the name of Intel Corporation nor the names of its contributors
31  *    may be used to endorse or promote products derived from this software
32  *    without specific prior written permission.
33  *
34  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
38  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44  *  POSSIBILITY OF SUCH DAMAGE.
45  */
46 
47 /*
48   DESCRIPTION
49   This module tests the following SHA256 routines:
50 
51   Scenarios tested include:
52   - NIST SHA256 test vectors
53 */
54 #include <gtest/gtest.h>
55 
56 namespace {
57 extern "C" {
58 #include "epid/member/tiny/math/sha256.h"
59 }
60 
TEST(TinySha256Test,tc_sha256_update_SucceedsGivenZeroMessageLength)61 TEST(TinySha256Test, tc_sha256_update_SucceedsGivenZeroMessageLength) {
62   const uint8_t expected[32] = {0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
63                                 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
64                                 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
65                                 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};
66   const char* m = "";
67   sha256_state s;
68   uint8_t digest[32];
69   tc_sha256_init(&s);
70   tc_sha256_update(&s, (const uint8_t*)m, 0);
71   tc_sha256_final(digest, &s);
72   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
73       << digest << std::endl
74       << expected;
75 }
76 /*
77  * NIST SHA256 test vector 1.
78  */
TEST(TinySha256Test,tc_sha256_WorksGivenNistTestVector1)79 TEST(TinySha256Test, tc_sha256_WorksGivenNistTestVector1) {
80   const uint8_t expected[32] = {0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
81                                 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
82                                 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
83                                 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad};
84   const char* m = "abc";
85   uint8_t digest[32];
86   sha256_state s;
87 
88   tc_sha256_init(&s);
89   tc_sha256_update(&s, (const uint8_t*)m, strlen(m));
90   tc_sha256_final(digest, &s);
91   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
92       << digest << std::endl
93       << expected;
94 }
95 
96 /*
97  * NIST SHA256 test vector 2.
98  */
TEST(TinySha256Test,tc_sha256_WorksGivenNistTestVector2)99 TEST(TinySha256Test, tc_sha256_WorksGivenNistTestVector2) {
100   const uint8_t expected[32] = {0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
101                                 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
102                                 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
103                                 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1};
104   const char* m = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
105   uint8_t digest[32];
106   sha256_state s;
107 
108   tc_sha256_init(&s);
109   tc_sha256_update(&s, (const uint8_t*)m, strlen(m));
110   tc_sha256_final(digest, &s);
111   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
112       << digest << std::endl
113       << expected;
114 }
115 
TEST(TinySha256Test,tc_sha256_WorksGiven1NonZeroChar)116 TEST(TinySha256Test, tc_sha256_WorksGiven1NonZeroChar) {
117   const uint8_t expected[32] = {0x68, 0x32, 0x57, 0x20, 0xaa, 0xbd, 0x7c, 0x82,
118                                 0xf3, 0x0f, 0x55, 0x4b, 0x31, 0x3d, 0x05, 0x70,
119                                 0xc9, 0x5a, 0xcc, 0xbb, 0x7d, 0xc4, 0xb5, 0xaa,
120                                 0xe1, 0x12, 0x04, 0xc0, 0x8f, 0xfe, 0x73, 0x2b};
121   const uint8_t m[1] = {0xbd};
122   uint8_t digest[32];
123   sha256_state s;
124 
125   tc_sha256_init(&s);
126   tc_sha256_update(&s, m, sizeof(m));
127   tc_sha256_final(digest, &s);
128   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
129       << digest << std::endl
130       << expected;
131 }
132 
TEST(TinySha256Test,tc_sha256_WorksGiven4NonZeroChars)133 TEST(TinySha256Test, tc_sha256_WorksGiven4NonZeroChars) {
134   const uint8_t expected[32] = {0x7a, 0xbc, 0x22, 0xc0, 0xae, 0x5a, 0xf2, 0x6c,
135                                 0xe9, 0x3d, 0xbb, 0x94, 0x43, 0x3a, 0x0e, 0x0b,
136                                 0x2e, 0x11, 0x9d, 0x01, 0x4f, 0x8e, 0x7f, 0x65,
137                                 0xbd, 0x56, 0xc6, 0x1c, 0xcc, 0xcd, 0x95, 0x04};
138   const uint8_t m[4] = {0xc9, 0x8c, 0x8e, 0x55};
139   uint8_t digest[32];
140   sha256_state s;
141 
142   tc_sha256_init(&s);
143   tc_sha256_update(&s, m, sizeof(m));
144   tc_sha256_final(digest, &s);
145   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
146       << digest << std::endl
147       << expected;
148 }
149 
TEST(TinySha256Test,tc_sha256_WorksGiven55ZeroChars)150 TEST(TinySha256Test, tc_sha256_WorksGiven55ZeroChars) {
151   const uint8_t expected[32] = {0x02, 0x77, 0x94, 0x66, 0xcd, 0xec, 0x16, 0x38,
152                                 0x11, 0xd0, 0x78, 0x81, 0x5c, 0x63, 0x3f, 0x21,
153                                 0x90, 0x14, 0x13, 0x08, 0x14, 0x49, 0x00, 0x2f,
154                                 0x24, 0xaa, 0x3e, 0x80, 0xf0, 0xb8, 0x8e, 0xf7};
155   uint8_t m[55];
156   uint8_t digest[32];
157   sha256_state s;
158 
159   (void)memset(m, 0x00, sizeof(m));
160 
161   tc_sha256_init(&s);
162   tc_sha256_update(&s, m, sizeof(m));
163   tc_sha256_final(digest, &s);
164   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
165       << digest << std::endl
166       << expected;
167 }
168 
TEST(TinySha256Test,tc_sha256_WorksGiven56ZeroChars)169 TEST(TinySha256Test, tc_sha256_WorksGiven56ZeroChars) {
170   const uint8_t expected[32] = {0xd4, 0x81, 0x7a, 0xa5, 0x49, 0x76, 0x28, 0xe7,
171                                 0xc7, 0x7e, 0x6b, 0x60, 0x61, 0x07, 0x04, 0x2b,
172                                 0xbb, 0xa3, 0x13, 0x08, 0x88, 0xc5, 0xf4, 0x7a,
173                                 0x37, 0x5e, 0x61, 0x79, 0xbe, 0x78, 0x9f, 0xbb};
174   uint8_t m[56];
175   uint8_t digest[32];
176   sha256_state s;
177 
178   (void)memset(m, 0x00, sizeof(m));
179 
180   tc_sha256_init(&s);
181   tc_sha256_update(&s, m, sizeof(m));
182   tc_sha256_final(digest, &s);
183   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
184       << digest << std::endl
185       << expected;
186 }
187 
TEST(TinySha256Test,tc_sha256_WorksGiven57ZeroChars)188 TEST(TinySha256Test, tc_sha256_WorksGiven57ZeroChars) {
189   const uint8_t expected[32] = {0x65, 0xa1, 0x6c, 0xb7, 0x86, 0x13, 0x35, 0xd5,
190                                 0xac, 0xe3, 0xc6, 0x07, 0x18, 0xb5, 0x05, 0x2e,
191                                 0x44, 0x66, 0x07, 0x26, 0xda, 0x4c, 0xd1, 0x3b,
192                                 0xb7, 0x45, 0x38, 0x1b, 0x23, 0x5a, 0x17, 0x85};
193   uint8_t m[57];
194   uint8_t digest[32];
195   sha256_state s;
196 
197   (void)memset(m, 0x00, sizeof(m));
198 
199   tc_sha256_init(&s);
200   tc_sha256_update(&s, m, sizeof(m));
201   tc_sha256_final(digest, &s);
202   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
203       << digest << std::endl
204       << expected;
205 }
206 
TEST(TinySha256Test,tc_sha256_WorksGiven64ZeroChars)207 TEST(TinySha256Test, tc_sha256_WorksGiven64ZeroChars) {
208   const uint8_t expected[32] = {0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30,
209                                 0x27, 0x98, 0xef, 0x6e, 0xd3, 0x09, 0x97, 0x9b,
210                                 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8,
211                                 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b};
212   uint8_t m[64];
213   uint8_t digest[32];
214   sha256_state s;
215 
216   (void)memset(m, 0x00, sizeof(m));
217 
218   tc_sha256_init(&s);
219   tc_sha256_update(&s, m, sizeof(m));
220   tc_sha256_final(digest, &s);
221   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
222       << digest << std::endl
223       << expected;
224 }
225 
TEST(TinySha256Test,tc_sha256_WorksGiven1000ZeroChars)226 TEST(TinySha256Test, tc_sha256_WorksGiven1000ZeroChars) {
227   const uint8_t expected[32] = {0x54, 0x1b, 0x3e, 0x9d, 0xaa, 0x09, 0xb2, 0x0b,
228                                 0xf8, 0x5f, 0xa2, 0x73, 0xe5, 0xcb, 0xd3, 0xe8,
229                                 0x01, 0x85, 0xaa, 0x4e, 0xc2, 0x98, 0xe7, 0x65,
230                                 0xdb, 0x87, 0x74, 0x2b, 0x70, 0x13, 0x8a, 0x53};
231   uint8_t m[1000];
232   uint8_t digest[32];
233   sha256_state s;
234 
235   (void)memset(m, 0x00, sizeof(m));
236 
237   tc_sha256_init(&s);
238   tc_sha256_update(&s, m, sizeof(m));
239   tc_sha256_final(digest, &s);
240   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
241       << digest << std::endl
242       << expected;
243 }
244 
TEST(TinySha256Test,tc_sha256_WorksGiven1000NonZeroChars)245 TEST(TinySha256Test, tc_sha256_WorksGiven1000NonZeroChars) {
246   const uint8_t expected[32] = {0xc2, 0xe6, 0x86, 0x82, 0x34, 0x89, 0xce, 0xd2,
247                                 0x01, 0x7f, 0x60, 0x59, 0xb8, 0xb2, 0x39, 0x31,
248                                 0x8b, 0x63, 0x64, 0xf6, 0xdc, 0xd8, 0x35, 0xd0,
249                                 0xa5, 0x19, 0x10, 0x5a, 0x1e, 0xad, 0xd6, 0xe4};
250   uint8_t m[1000];
251   uint8_t digest[32];
252   sha256_state s;
253 
254   (void)memset(m, 0x41, sizeof(m));
255 
256   tc_sha256_init(&s);
257   tc_sha256_update(&s, m, sizeof(m));
258   tc_sha256_final(digest, &s);
259   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
260       << digest << std::endl
261       << expected;
262 }
263 
TEST(TinySha256Test,tc_sha256_WorksGiven1005NonZeroChars)264 TEST(TinySha256Test, tc_sha256_WorksGiven1005NonZeroChars) {
265   const uint8_t expected[32] = {0xf4, 0xd6, 0x2d, 0xde, 0xc0, 0xf3, 0xdd, 0x90,
266                                 0xea, 0x13, 0x80, 0xfa, 0x16, 0xa5, 0xff, 0x8d,
267                                 0xc4, 0xc5, 0x4b, 0x21, 0x74, 0x06, 0x50, 0xf2,
268                                 0x4a, 0xfc, 0x41, 0x20, 0x90, 0x35, 0x52, 0xb0};
269   uint8_t m[1005];
270   uint8_t digest[32];
271   sha256_state s;
272 
273   (void)memset(m, 0x55, sizeof(m));
274 
275   tc_sha256_init(&s);
276   tc_sha256_update(&s, m, sizeof(m));
277   tc_sha256_final(digest, &s);
278   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
279       << digest << std::endl
280       << expected;
281 }
282 
TEST(TinySha256Test,tc_sha256_WorksGiven1000ZeroCharsUpdated1000Times)283 TEST(TinySha256Test, tc_sha256_WorksGiven1000ZeroCharsUpdated1000Times) {
284   const uint8_t expected[32] = {0xd2, 0x97, 0x51, 0xf2, 0x64, 0x9b, 0x32, 0xff,
285                                 0x57, 0x2b, 0x5e, 0x0a, 0x9f, 0x54, 0x1e, 0xa6,
286                                 0x60, 0xa5, 0x0f, 0x94, 0xff, 0x0b, 0xee, 0xdf,
287                                 0xb0, 0xb6, 0x92, 0xb9, 0x24, 0xcc, 0x80, 0x25};
288   uint8_t m[1000];
289   uint8_t digest[32];
290   sha256_state s;
291   unsigned int i;
292 
293   (void)memset(m, 0x00, sizeof(m));
294 
295   tc_sha256_init(&s);
296   for (i = 0; i < 1000; ++i) {
297     tc_sha256_update(&s, m, sizeof(m));
298   }
299   tc_sha256_final(digest, &s);
300   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
301       << digest << std::endl
302       << expected;
303 }
304 
TEST(TinySha256Test,DISABLED_SLOW_tc_sha256_WorksGiven32768NonZeroCharsUpdated16384Times)305 TEST(TinySha256Test,
306      DISABLED_SLOW_tc_sha256_WorksGiven32768NonZeroCharsUpdated16384Times) {
307   const uint8_t expected[32] = {0x15, 0xa1, 0x86, 0x8c, 0x12, 0xcc, 0x53, 0x95,
308                                 0x1e, 0x18, 0x23, 0x44, 0x27, 0x74, 0x47, 0xcd,
309                                 0x09, 0x79, 0x53, 0x6b, 0xad, 0xcc, 0x51, 0x2a,
310                                 0xd2, 0x4c, 0x67, 0xe9, 0xb2, 0xd4, 0xf3, 0xdd};
311   uint8_t m[32768];
312   uint8_t digest[32];
313   sha256_state s;
314   unsigned int i;
315 
316   (void)memset(m, 0x5a, sizeof(m));
317 
318   tc_sha256_init(&s);
319   for (i = 0; i < 16384; ++i) {
320     tc_sha256_update(&s, m, sizeof(m));
321   }
322   tc_sha256_final(digest, &s);
323   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
324       << digest << std::endl
325       << expected;
326 }
327 
TEST(TinySha256Test,DISABLED_SLOW_tc_sha256_WorksGiven32768NonZeroCharsUpdated33280Times)328 TEST(TinySha256Test,
329      DISABLED_SLOW_tc_sha256_WorksGiven32768NonZeroCharsUpdated33280Times) {
330   const uint8_t expected[32] = {0x46, 0x1c, 0x19, 0xa9, 0x3b, 0xd4, 0x34, 0x4f,
331                                 0x92, 0x15, 0xf5, 0xec, 0x64, 0x35, 0x70, 0x90,
332                                 0x34, 0x2b, 0xc6, 0x6b, 0x15, 0xa1, 0x48, 0x31,
333                                 0x7d, 0x27, 0x6e, 0x31, 0xcb, 0xc2, 0x0b, 0x53};
334   uint8_t m[32768];
335   uint8_t digest[32];
336   sha256_state s;
337   unsigned int i;
338 
339   (void)memset(m, 0x00, sizeof(m));
340 
341   tc_sha256_init(&s);
342   for (i = 0; i < 33280; ++i) {
343     tc_sha256_update(&s, m, sizeof(m));
344   }
345   tc_sha256_final(digest, &s);
346   EXPECT_TRUE(0 == memcmp(expected, digest, sizeof(digest)))
347       << digest << std::endl
348       << expected;
349 }
350 }  // namespace
351