test_sha256.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* test_sha256.c - TinyCrypt implementation of some SHA-256 tests */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. DESCRIPTION
  33. This module tests the following SHA256 routines:
  34. Scenarios tested include:
  35. - NIST SHA256 test vectors
  36. */
  37. #include <tinycrypt/sha256.h>
  38. #include <tinycrypt/constants.h>
  39. #include <test_utils.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <stdint.h>
  44. /*
  45. * NIST SHA256 test vector 1.
  46. */
  47. unsigned int test_1(void)
  48. {
  49. unsigned int result = TC_PASS;
  50. TC_PRINT("SHA256 test #1:\n");
  51. const uint8_t expected[32] = {
  52. 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde,
  53. 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
  54. 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
  55. };
  56. const char *m = "abc";
  57. uint8_t digest[32];
  58. struct tc_sha256_state_struct s;
  59. (void)tc_sha256_init(&s);
  60. tc_sha256_update(&s, (const uint8_t *) m, strlen(m));
  61. (void)tc_sha256_final(digest, &s);
  62. result = check_result(1, expected, sizeof(expected),
  63. digest, sizeof(digest));
  64. TC_END_RESULT(result);
  65. return result;
  66. }
  67. /*
  68. * NIST SHA256 test vector 2.
  69. */
  70. unsigned int test_2(void)
  71. {
  72. unsigned int result = TC_PASS;
  73. TC_PRINT("SHA256 test #2:\n");
  74. const uint8_t expected[32] = {
  75. 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93,
  76. 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
  77. 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
  78. };
  79. const char *m = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  80. uint8_t digest[32];
  81. struct tc_sha256_state_struct s;
  82. (void)tc_sha256_init(&s);
  83. tc_sha256_update(&s, (const uint8_t *) m, strlen(m));
  84. (void) tc_sha256_final(digest, &s);
  85. result = check_result(2, expected, sizeof(expected),
  86. digest, sizeof(digest));
  87. TC_END_RESULT(result);
  88. return result;
  89. }
  90. unsigned int test_3(void)
  91. {
  92. unsigned int result = TC_PASS;
  93. TC_PRINT("SHA256 test #3:\n");
  94. const uint8_t expected[32] = {
  95. 0x68, 0x32, 0x57, 0x20, 0xaa, 0xbd, 0x7c, 0x82, 0xf3, 0x0f, 0x55, 0x4b,
  96. 0x31, 0x3d, 0x05, 0x70, 0xc9, 0x5a, 0xcc, 0xbb, 0x7d, 0xc4, 0xb5, 0xaa,
  97. 0xe1, 0x12, 0x04, 0xc0, 0x8f, 0xfe, 0x73, 0x2b
  98. };
  99. const uint8_t m[1] = { 0xbd };
  100. uint8_t digest[32];
  101. struct tc_sha256_state_struct s;
  102. (void)tc_sha256_init(&s);
  103. tc_sha256_update(&s, m, sizeof(m));
  104. (void)tc_sha256_final(digest, &s);
  105. result = check_result(3, expected, sizeof(expected),
  106. digest, sizeof(digest));
  107. TC_END_RESULT(result);
  108. return result;
  109. }
  110. unsigned int test_4(void)
  111. {
  112. unsigned int result = TC_PASS;
  113. TC_PRINT("SHA256 test #4:\n");
  114. const uint8_t expected[32] = {
  115. 0x7a, 0xbc, 0x22, 0xc0, 0xae, 0x5a, 0xf2, 0x6c, 0xe9, 0x3d, 0xbb, 0x94,
  116. 0x43, 0x3a, 0x0e, 0x0b, 0x2e, 0x11, 0x9d, 0x01, 0x4f, 0x8e, 0x7f, 0x65,
  117. 0xbd, 0x56, 0xc6, 0x1c, 0xcc, 0xcd, 0x95, 0x04
  118. };
  119. const uint8_t m[4] = { 0xc9, 0x8c, 0x8e, 0x55 };
  120. uint8_t digest[32];
  121. struct tc_sha256_state_struct s;
  122. (void)tc_sha256_init(&s);
  123. tc_sha256_update(&s, m, sizeof(m));
  124. (void)tc_sha256_final(digest, &s);
  125. result = check_result(4, expected, sizeof(expected),
  126. digest, sizeof(digest));
  127. TC_END_RESULT(result);
  128. return result;
  129. }
  130. unsigned int test_5(void)
  131. {
  132. unsigned int result = TC_PASS;
  133. TC_PRINT("SHA256 test #5:\n");
  134. const uint8_t expected[32] = {
  135. 0x02, 0x77, 0x94, 0x66, 0xcd, 0xec, 0x16, 0x38, 0x11, 0xd0, 0x78, 0x81,
  136. 0x5c, 0x63, 0x3f, 0x21, 0x90, 0x14, 0x13, 0x08, 0x14, 0x49, 0x00, 0x2f,
  137. 0x24, 0xaa, 0x3e, 0x80, 0xf0, 0xb8, 0x8e, 0xf7
  138. };
  139. uint8_t m[55];
  140. uint8_t digest[32];
  141. struct tc_sha256_state_struct s;
  142. (void)memset(m, 0x00, sizeof(m));
  143. (void)tc_sha256_init(&s);
  144. tc_sha256_update(&s, m, sizeof(m));
  145. (void)tc_sha256_final(digest, &s);
  146. result = check_result(5, expected, sizeof(expected),
  147. digest, sizeof(digest));
  148. TC_END_RESULT(result);
  149. return result;
  150. }
  151. unsigned int test_6(void)
  152. {
  153. unsigned int result = TC_PASS;
  154. TC_PRINT("SHA256 test #6:\n");
  155. const uint8_t expected[32] = {
  156. 0xd4, 0x81, 0x7a, 0xa5, 0x49, 0x76, 0x28, 0xe7, 0xc7, 0x7e, 0x6b, 0x60,
  157. 0x61, 0x07, 0x04, 0x2b, 0xbb, 0xa3, 0x13, 0x08, 0x88, 0xc5, 0xf4, 0x7a,
  158. 0x37, 0x5e, 0x61, 0x79, 0xbe, 0x78, 0x9f, 0xbb
  159. };
  160. uint8_t m[56];
  161. uint8_t digest[32];
  162. struct tc_sha256_state_struct s;
  163. (void)memset(m, 0x00, sizeof(m));
  164. (void)tc_sha256_init(&s);
  165. tc_sha256_update(&s, m, sizeof(m));
  166. (void)tc_sha256_final(digest, &s);
  167. result = check_result(6, expected, sizeof(expected),
  168. digest, sizeof(digest));
  169. TC_END_RESULT(result);
  170. return result;
  171. }
  172. unsigned int test_7(void)
  173. {
  174. unsigned int result = TC_PASS;
  175. TC_PRINT("SHA256 test #7:\n");
  176. const uint8_t expected[32] = {
  177. 0x65, 0xa1, 0x6c, 0xb7, 0x86, 0x13, 0x35, 0xd5, 0xac, 0xe3, 0xc6, 0x07,
  178. 0x18, 0xb5, 0x05, 0x2e, 0x44, 0x66, 0x07, 0x26, 0xda, 0x4c, 0xd1, 0x3b,
  179. 0xb7, 0x45, 0x38, 0x1b, 0x23, 0x5a, 0x17, 0x85
  180. };
  181. uint8_t m[57];
  182. uint8_t digest[32];
  183. struct tc_sha256_state_struct s;
  184. (void)memset(m, 0x00, sizeof(m));
  185. (void)tc_sha256_init(&s);
  186. tc_sha256_update(&s, m, sizeof(m));
  187. (void)tc_sha256_final(digest, &s);
  188. result = check_result(7, expected, sizeof(expected),
  189. digest, sizeof(digest));
  190. TC_END_RESULT(result);
  191. return result;
  192. }
  193. unsigned int test_8(void)
  194. {
  195. unsigned int result = TC_PASS;
  196. TC_PRINT("SHA256 test #8:\n");
  197. const uint8_t expected[32] = {
  198. 0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30, 0x27, 0x98, 0xef, 0x6e,
  199. 0xd3, 0x09, 0x97, 0x9b, 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8,
  200. 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b
  201. };
  202. uint8_t m[64];
  203. uint8_t digest[32];
  204. struct tc_sha256_state_struct s;
  205. (void)memset(m, 0x00, sizeof(m));
  206. (void)tc_sha256_init(&s);
  207. tc_sha256_update(&s, m, sizeof(m));
  208. (void)tc_sha256_final(digest, &s);
  209. result = check_result(8, expected, sizeof(expected),
  210. digest, sizeof(digest));
  211. TC_END_RESULT(result);
  212. return result;
  213. }
  214. unsigned int test_9(void)
  215. {
  216. unsigned int result = TC_PASS;
  217. TC_PRINT("SHA256 test #9:\n");
  218. const uint8_t expected[32] = {
  219. 0x54, 0x1b, 0x3e, 0x9d, 0xaa, 0x09, 0xb2, 0x0b, 0xf8, 0x5f, 0xa2, 0x73,
  220. 0xe5, 0xcb, 0xd3, 0xe8, 0x01, 0x85, 0xaa, 0x4e, 0xc2, 0x98, 0xe7, 0x65,
  221. 0xdb, 0x87, 0x74, 0x2b, 0x70, 0x13, 0x8a, 0x53
  222. };
  223. uint8_t m[1000];
  224. uint8_t digest[32];
  225. struct tc_sha256_state_struct s;
  226. (void)memset(m, 0x00, sizeof(m));
  227. (void)tc_sha256_init(&s);
  228. tc_sha256_update(&s, m, sizeof(m));
  229. (void)tc_sha256_final(digest, &s);
  230. result = check_result(9, expected, sizeof(expected),
  231. digest, sizeof(digest));
  232. TC_END_RESULT(result);
  233. return result;
  234. }
  235. unsigned int test_10(void)
  236. {
  237. unsigned int result = TC_PASS;
  238. TC_PRINT("SHA256 test #10:\n");
  239. const uint8_t expected[32] = {
  240. 0xc2, 0xe6, 0x86, 0x82, 0x34, 0x89, 0xce, 0xd2, 0x01, 0x7f, 0x60, 0x59,
  241. 0xb8, 0xb2, 0x39, 0x31, 0x8b, 0x63, 0x64, 0xf6, 0xdc, 0xd8, 0x35, 0xd0,
  242. 0xa5, 0x19, 0x10, 0x5a, 0x1e, 0xad, 0xd6, 0xe4
  243. };
  244. uint8_t m[1000];
  245. uint8_t digest[32];
  246. struct tc_sha256_state_struct s;
  247. (void)memset(m, 0x41, sizeof(m));
  248. (void)tc_sha256_init(&s);
  249. tc_sha256_update(&s, m, sizeof(m));
  250. (void)tc_sha256_final(digest, &s);
  251. result = check_result(10, expected, sizeof(expected),
  252. digest, sizeof(digest));
  253. TC_END_RESULT(result);
  254. return result;
  255. }
  256. unsigned int test_11(void)
  257. {
  258. unsigned int result = TC_PASS;
  259. TC_PRINT("SHA256 test #11:\n");
  260. const uint8_t expected[32] = {
  261. 0xf4, 0xd6, 0x2d, 0xde, 0xc0, 0xf3, 0xdd, 0x90, 0xea, 0x13, 0x80, 0xfa,
  262. 0x16, 0xa5, 0xff, 0x8d, 0xc4, 0xc5, 0x4b, 0x21, 0x74, 0x06, 0x50, 0xf2,
  263. 0x4a, 0xfc, 0x41, 0x20, 0x90, 0x35, 0x52, 0xb0
  264. };
  265. uint8_t m[1005];
  266. uint8_t digest[32];
  267. struct tc_sha256_state_struct s;
  268. (void)memset(m, 0x55, sizeof(m));
  269. (void)tc_sha256_init(&s);
  270. tc_sha256_update(&s, m, sizeof(m));
  271. (void)tc_sha256_final(digest, &s);
  272. result = check_result(11, expected, sizeof(expected),
  273. digest, sizeof(digest));
  274. TC_END_RESULT(result);
  275. return result;
  276. }
  277. unsigned int test_12(void)
  278. {
  279. unsigned int result = TC_PASS;
  280. TC_PRINT("SHA256 test #12:\n");
  281. const uint8_t expected[32] = {
  282. 0xd2, 0x97, 0x51, 0xf2, 0x64, 0x9b, 0x32, 0xff, 0x57, 0x2b, 0x5e, 0x0a,
  283. 0x9f, 0x54, 0x1e, 0xa6, 0x60, 0xa5, 0x0f, 0x94, 0xff, 0x0b, 0xee, 0xdf,
  284. 0xb0, 0xb6, 0x92, 0xb9, 0x24, 0xcc, 0x80, 0x25
  285. };
  286. uint8_t m[1000];
  287. uint8_t digest[32];
  288. struct tc_sha256_state_struct s;
  289. unsigned int i;
  290. (void)memset(m, 0x00, sizeof(m));
  291. (void)tc_sha256_init(&s);
  292. for (i = 0; i < 1000; ++i) {
  293. tc_sha256_update(&s, m, sizeof(m));
  294. }
  295. (void)tc_sha256_final(digest, &s);
  296. result = check_result(12, expected, sizeof(expected),
  297. digest, sizeof(digest));
  298. TC_END_RESULT(result);
  299. return result;
  300. }
  301. unsigned int test_13(void)
  302. {
  303. unsigned int result = TC_PASS;
  304. TC_PRINT("SHA256 test #13:\n");
  305. const uint8_t expected[32] = {
  306. 0x15, 0xa1, 0x86, 0x8c, 0x12, 0xcc, 0x53, 0x95, 0x1e, 0x18, 0x23, 0x44,
  307. 0x27, 0x74, 0x47, 0xcd, 0x09, 0x79, 0x53, 0x6b, 0xad, 0xcc, 0x51, 0x2a,
  308. 0xd2, 0x4c, 0x67, 0xe9, 0xb2, 0xd4, 0xf3, 0xdd
  309. };
  310. uint8_t m[32768];
  311. uint8_t digest[32];
  312. struct tc_sha256_state_struct s;
  313. unsigned int i;
  314. (void)memset(m, 0x5a, sizeof(m));
  315. (void)tc_sha256_init(&s);
  316. for (i = 0; i < 16384; ++i) {
  317. tc_sha256_update(&s, m, sizeof(m));
  318. }
  319. (void)tc_sha256_final(digest, &s);
  320. result = check_result(13, expected, sizeof(expected),
  321. digest, sizeof(digest));
  322. TC_END_RESULT(result);
  323. return result;
  324. }
  325. unsigned int test_14(void)
  326. {
  327. unsigned int result = TC_PASS;
  328. TC_PRINT("SHA256 test #14:\n");
  329. const uint8_t expected[32] = {
  330. 0x46, 0x1c, 0x19, 0xa9, 0x3b, 0xd4, 0x34, 0x4f, 0x92, 0x15, 0xf5, 0xec,
  331. 0x64, 0x35, 0x70, 0x90, 0x34, 0x2b, 0xc6, 0x6b, 0x15, 0xa1, 0x48, 0x31,
  332. 0x7d, 0x27, 0x6e, 0x31, 0xcb, 0xc2, 0x0b, 0x53
  333. };
  334. uint8_t m[32768];
  335. uint8_t digest[32];
  336. struct tc_sha256_state_struct s;
  337. unsigned int i;
  338. (void)memset(m, 0x00, sizeof(m));
  339. (void) tc_sha256_init(&s);
  340. for (i = 0; i < 33280; ++i) {
  341. tc_sha256_update(&s, m, sizeof(m));
  342. }
  343. (void) tc_sha256_final(digest, &s);
  344. result = check_result(14, expected, sizeof(expected),
  345. digest, sizeof(digest));
  346. TC_END_RESULT(result);
  347. return result;
  348. }
  349. /*
  350. * Main task to test AES
  351. */
  352. int main(void)
  353. {
  354. unsigned int result = TC_PASS;
  355. TC_START("Performing SHA256 tests (NIST tests vectors):");
  356. result = test_1();
  357. if (result == TC_FAIL) {
  358. /* terminate test */
  359. TC_ERROR("SHA256 test #1 failed.\n");
  360. goto exitTest;
  361. }
  362. result = test_2();
  363. if (result == TC_FAIL) {
  364. /* terminate test */
  365. TC_ERROR("SHA256 test #2 failed.\n");
  366. goto exitTest;
  367. }
  368. result = test_3();
  369. if (result == TC_FAIL) {
  370. /* terminate test */
  371. TC_ERROR("SHA256 test #3 failed.\n");
  372. goto exitTest;
  373. }
  374. result = test_4();
  375. if (result == TC_FAIL) {
  376. /* terminate test */
  377. TC_ERROR("SHA256 test #4 failed.\n");
  378. goto exitTest;
  379. }
  380. result = test_5();
  381. if (result == TC_FAIL) {
  382. /* terminate test */
  383. TC_ERROR("SHA256 test #5 failed.\n");
  384. goto exitTest;
  385. }
  386. result = test_6();
  387. if (result == TC_FAIL) {
  388. /* terminate test */
  389. TC_ERROR("SHA256 test #6 failed.\n");
  390. goto exitTest;
  391. }
  392. result = test_7();
  393. if (result == TC_FAIL) {
  394. /* terminate test */
  395. TC_ERROR("SHA256 test #7 failed.\n");
  396. goto exitTest;
  397. }
  398. result = test_8();
  399. if (result == TC_FAIL) {
  400. /* terminate test */
  401. TC_ERROR("SHA256 test #8 failed.\n");
  402. goto exitTest;
  403. }
  404. result = test_9();
  405. if (result == TC_FAIL) {
  406. /* terminate test */
  407. TC_ERROR("SHA256 test #9 failed.\n");
  408. goto exitTest;
  409. }
  410. result = test_10();
  411. if (result == TC_FAIL) {
  412. /* terminate test */
  413. TC_ERROR("SHA256 test #10 failed.\n");
  414. goto exitTest;
  415. }
  416. result = test_11();
  417. if (result == TC_FAIL) {
  418. /* terminate test */
  419. TC_ERROR("SHA256 test #11 failed.\n");
  420. goto exitTest;
  421. }
  422. result = test_12();
  423. if (result == TC_FAIL) {
  424. /* terminate test */
  425. TC_ERROR("SHA256 test #12 failed.\n");
  426. goto exitTest;
  427. }
  428. /* memory and computation intensive test cases: */
  429. result = test_13();
  430. if (result == TC_FAIL) {
  431. /* terminate test */
  432. TC_ERROR("SHA256 test #13 failed.\n");
  433. goto exitTest;
  434. }
  435. result = test_14();
  436. if (result == TC_FAIL) {
  437. /* terminate test */
  438. TC_ERROR("SHA256 test #14 failed.\n");
  439. goto exitTest;
  440. }
  441. TC_PRINT("All SHA256 tests succeeded!\n");
  442. exitTest:
  443. TC_END_RESULT(result);
  444. TC_END_REPORT(result);
  445. }