sha256-test.cc 13 KB

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