main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /**
  2. * Copyright (c) 2016 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "integration_test_plat_defs.h"
  41. #include "ssi_pal_mutex_plat.h"
  42. #include "nrf_cc310_bl_init.h"
  43. #include "nrf_cc310_bl_ecdsa_verify_secp256r1.h"
  44. #include "nrf_cc310_bl_ecdsa_verify_secp224r1.h"
  45. #include "nrf_cc310_bl_hash_common.h"
  46. #include "nrf_cc310_bl_hash_sha256.h"
  47. #include <string.h>
  48. #include "nrf.h"
  49. #define NUM_ELEMENTS(x) (sizeof(x) / sizeof((x)[0]))
  50. // Simple implementation of HardFault handler in case of unaligned access or null
  51. void HardFault_Handler(void)
  52. {
  53. INTEG_TEST_PRINT("hardfault");
  54. while(1);
  55. }
  56. static nrf_cc310_bl_hash_context_sha256_t hash_context;
  57. static nrf_cc310_bl_hash_digest_sha256_t hash_digest;
  58. //
  59. // Input-data to the ecdsa verify function: Hash of the term "sample"
  60. // According to RFC6979, the input data "sample" is described as a
  61. // utf-8 string with 6 octets (which means disregarding the null termination).
  62. // Ref: rfc6979 A.1.2
  63. //
  64. static uint8_t hash_input[6] = "sample";
  65. // Expected hash
  66. static nrf_cc310_bl_hash_digest_sha256_t hash_digest_expected =
  67. {
  68. 0xaf, 0x2b, 0xdb, 0xe1, 0xaa, 0x9b, 0x6e, 0xc1, 0xe2, 0xad, 0xe1, 0xd6, 0x94, 0xf4, 0x1f, 0xc7,
  69. 0x1a, 0x83, 0x1d, 0x02, 0x68, 0xe9, 0x89, 0x15, 0x62, 0x11, 0x3d, 0x8a, 0x62, 0xad, 0xd1, 0xbf
  70. };
  71. // Invalid hash with some bytes in the middle set to zero
  72. static nrf_cc310_bl_hash_digest_sha256_t hash_digest_faulty =
  73. {
  74. 0xaf, 0x2b, 0xdb, 0xe1, 0xaa, 0x9b, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd6, 0x94, 0xf4, 0x1f, 0xc7,
  75. 0x1a, 0x83, 0x1d, 0x02, 0x68, 0xe9, 0x89, 0x15, 0x62, 0x11, 0x3d, 0x8a, 0x62, 0xad, 0xd1, 0xbf
  76. };
  77. //
  78. // For ECDSA verify using secp256r1 and SHA-256
  79. //
  80. static nrf_cc310_bl_ecdsa_verify_context_secp256r1_t ecdsa_context_secp256r1;
  81. // Public key according to rfc6979 for secp256r1 in A.2.5
  82. // Ux = 60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6
  83. // Uy = 7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299
  84. static nrf_cc310_bl_ecc_public_key_secp256r1_t ecc_public_key_secp256r1 =
  85. {
  86. .x = { 0x60, 0xFE, 0xD4, 0xBA, 0x25, 0x5A, 0x9D, 0x31, 0xC9, 0x61, 0xEB, 0x74, 0xC6, 0x35, 0x6D, 0x68,
  87. 0xC0, 0x49, 0xB8, 0x92, 0x3B, 0x61, 0xFA, 0x6C, 0xE6, 0x69, 0x62, 0x2E, 0x60, 0xF2, 0x9F, 0xB6 },
  88. .y = { 0x79, 0x03, 0xFE, 0x10, 0x08, 0xB8, 0xBC, 0x99, 0xA4, 0x1A, 0xE9, 0xE9, 0x56, 0x28, 0xBC, 0x64,
  89. 0xF2, 0xF1, 0xB2, 0x0C, 0x2D, 0x7E, 0x9F, 0x51, 0x77, 0xA3, 0xC2, 0x94, 0xD4, 0x46, 0x22, 0x99 }
  90. };
  91. // Signature according to rfc6979 for secp256r1 in A.2.5
  92. // r = EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716
  93. // s = F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8
  94. static nrf_cc310_bl_ecc_signature_secp256r1_t ecdsa_signature_secp256r1 =
  95. {
  96. .r = { 0xEF, 0xD4, 0x8B, 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD, 0x9C, 0xD4, 0x5E, 0x81, 0xD6,
  97. 0x9D, 0x2C, 0x87, 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E, 0xA8, 0x4E, 0xAF, 0x37, 0x16},
  98. .s = { 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41, 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  99. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06, 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8}
  100. };
  101. // Invalid signature with some data set to zero.
  102. static nrf_cc310_bl_ecc_signature_secp256r1_t ecdsa_signature_secp256r1_faulty =
  103. {
  104. .r = { 0xEF, 0xD4, 0x8B, 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD, 0x9C, 0xD4, 0x5E, 0x81, 0xD6,
  105. 0x9D, 0x2C, 0x87, 0x00, 0x00, 0xAA, 0x00, 0x91, 0xC3, 0x4D, 0x0E, 0xA8, 0x4E, 0xAF, 0x37, 0x16},
  106. .s = { 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x00, 0xD4, 0x00, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  107. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06, 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8}
  108. };
  109. //
  110. // For ECDSA verify using secp224r1 and SHA-256
  111. //
  112. static nrf_cc310_bl_ecdsa_verify_context_secp224r1_t ecdsa_context_secp224r1;
  113. // Public key according to rfc6979 for secp224r1 in A.2.4
  114. // Ux = 00CF08DA5AD719E42707FA431292DEA11244D64FC51610D94B130D6C
  115. // Uy = EEAB6F3DEBE455E3DBF85416F7030CBD94F34F2D6F232C69F3C1385A
  116. static nrf_cc310_bl_ecc_public_key_secp224r1_t ecc_public_key_secp224r1 =
  117. {
  118. .x = { 0x00, 0xCF, 0x08, 0xDA, 0x5A, 0xD7, 0x19, 0xE4, 0x27, 0x07, 0xFA, 0x43, 0x12, 0x92,
  119. 0xDE, 0xA1, 0x12, 0x44, 0xD6, 0x4F, 0xC5, 0x16, 0x10, 0xD9, 0x4B, 0x13, 0x0D, 0x6C },
  120. .y = { 0xEE, 0xAB, 0x6F, 0x3D, 0xEB, 0xE4, 0x55, 0xE3, 0xDB, 0xF8, 0x54, 0x16, 0xF7, 0x03,
  121. 0x0C, 0xBD, 0x94, 0xF3, 0x4F, 0x2D, 0x6F, 0x23, 0x2C, 0x69, 0xF3, 0xC1, 0x38, 0x5A }
  122. };
  123. // Signature according to rfc6979 for secp224r1 in A.2.4
  124. // r = 61AA3DA010E8E8406C656BC477A7A7189895E7E840CDFE8FF42307BA
  125. // s = BC814050DAB5D23770879494F9E0A680DC1AF7161991BDE692B10101
  126. static nrf_cc310_bl_ecc_signature_secp224r1_t ecdsa_signature_secp224r1 =
  127. {
  128. .r = { 0x61, 0xAA, 0x3D, 0xA0, 0x10, 0xE8, 0xE8, 0x40, 0x6C, 0x65, 0x6B, 0xC4, 0x77, 0xA7,
  129. 0xA7, 0x18, 0x98, 0x95, 0xE7, 0xE8, 0x40, 0xCD, 0xFE, 0x8F, 0xF4, 0x23, 0x07, 0xBA },
  130. .s = { 0xBC, 0x81, 0x40, 0x50, 0xDA, 0xB5, 0xD2, 0x37, 0x70, 0x87, 0x94, 0x94, 0xF9, 0xE0,
  131. 0xA6, 0x80, 0xDC, 0x1A, 0xF7, 0x16, 0x19, 0x91, 0xBD, 0xE6, 0x92, 0xB1, 0x01, 0x01 }
  132. };
  133. // Invalid signature with some data set to zero.
  134. static nrf_cc310_bl_ecc_signature_secp224r1_t ecdsa_signature_secp224r1_faulty =
  135. {
  136. .r = { 0x61, 0xAA, 0x3D, 0xA0, 0x10, 0xE8, 0xE8, 0x40, 0x6C, 0x65, 0x6B, 0xC4, 0x77, 0xA7,
  137. 0xA7, 0x18, 0x98, 0x95, 0xE7, 0xE8, 0x40, 0xCD, 0xFE, 0x8F, 0xF0, 0x23, 0x07, 0xBA },
  138. .s = { 0xBC, 0x81, 0x40, 0x50, 0xDA, 0xB5, 0xD2, 0x37, 0x70, 0x87, 0x94, 0x94, 0xF9, 0xE0,
  139. 0xA6, 0x80, 0xDC, 0x1A, 0xF7, 0x16, 0x19, 0x91, 0xBD, 0xE6, 0x92, 0xB1, 0x01, 0x01 }
  140. };
  141. void print_array(char const * const name, uint8_t const * const p_src, size_t size)
  142. {
  143. INTEG_TEST_PRINT("Array %s:\r\n", name);
  144. for(int i = 0; i < size - 1; i++)
  145. INTEG_TEST_PRINT("0x%x, ", p_src[i]);
  146. INTEG_TEST_PRINT("0x%x\r\n\r\n", p_src[size-1]);
  147. }
  148. void test_check(CRYSError_t error)
  149. {
  150. if(error != SASI_SUCCESS)
  151. {
  152. INTEG_TEST_PRINT("Failure: 0x%08x\r\n", error);
  153. while(1);
  154. }
  155. }
  156. int main(void)
  157. {
  158. int ret = 0;
  159. CRYSError_t err_code;
  160. // Enables log and sets CryptoCell HW to enabled.
  161. ret = integration_tests_setup();
  162. test_check(ret);
  163. // Initialize the thin version of the run-time library (no RNG support).
  164. INTEG_TEST_PRINT("Initializing nrf_cc310_bl\r\n");
  165. err_code = nrf_cc310_bl_init();
  166. test_check(err_code);
  167. //
  168. // Hash test
  169. //
  170. INTEG_TEST_PRINT("\r\n\r\n===================\r\n");
  171. INTEG_TEST_PRINT("Testing SHA-256 hash\r\n\r\n");
  172. INTEG_TEST_PRINT("Initializing SHA-256 context\r\n");
  173. err_code = nrf_cc310_bl_hash_sha256_init(&hash_context);
  174. test_check(err_code);
  175. INTEG_TEST_PRINT("Running SHA-256 update\r\n");
  176. err_code = nrf_cc310_bl_hash_sha256_update(&hash_context, hash_input, 6);
  177. test_check(err_code);
  178. INTEG_TEST_PRINT("Running SHA-256 finalize\r\n");
  179. err_code = nrf_cc310_bl_hash_sha256_finalize(&hash_context, &hash_digest);
  180. test_check(err_code);
  181. print_array("expected", hash_digest, 32);
  182. print_array("calculated", hash_digest_expected, 32);
  183. INTEG_TEST_PRINT("Comparing result\r\n");
  184. if( memcmp(&hash_digest, &hash_digest_expected, sizeof(nrf_cc310_bl_hash_digest_sha256_t)) != 0)
  185. {
  186. APP_ERROR_CHECK(false);
  187. }
  188. INTEG_TEST_PRINT("Hash generation was successful!\r\n");
  189. //
  190. // Valid ECDSA secp256r1 verify test
  191. //
  192. INTEG_TEST_PRINT("\r\n\r\n====================================\r\n");
  193. INTEG_TEST_PRINT("Testing valid ECDSA secp256r1 verify\r\n\r\n");
  194. INTEG_TEST_PRINT("Initializing ecdsa verify context secp256r1\r\n");
  195. err_code = nrf_cc310_bl_ecdsa_verify_init_secp256r1(&ecdsa_context_secp256r1, &ecc_public_key_secp256r1);
  196. test_check(err_code);
  197. INTEG_TEST_PRINT("Executing ecdsa verify secp256r1\r\n");
  198. err_code = nrf_cc310_bl_ecdsa_verify_hash_secp256r1(&ecdsa_context_secp256r1,
  199. &ecdsa_signature_secp256r1,
  200. hash_digest,
  201. sizeof(nrf_cc310_bl_hash_digest_sha256_t));
  202. if (err_code == SASI_SUCCESS)
  203. {
  204. INTEG_TEST_PRINT("Verification was successful!\r\n");
  205. }
  206. else
  207. {
  208. INTEG_TEST_PRINT("Verification failed!\r\n");
  209. test_check(err_code);
  210. }
  211. //
  212. // Invalid ECDSA secp256r1 verify test (wrong signature)
  213. //
  214. INTEG_TEST_PRINT("\r\n\r\n=======================================================\r\n");
  215. INTEG_TEST_PRINT("Testing ECDSA secp256r1 verify failure scenario (wrong signature)\r\n\r\n");
  216. INTEG_TEST_PRINT("Initializing ecdsa verify context secp256r1\r\n");
  217. err_code = nrf_cc310_bl_ecdsa_verify_init_secp256r1(&ecdsa_context_secp256r1, &ecc_public_key_secp256r1);
  218. test_check(err_code);
  219. INTEG_TEST_PRINT("Executing ecdsa verify secp256r1\r\n");
  220. err_code = nrf_cc310_bl_ecdsa_verify_hash_secp256r1(&ecdsa_context_secp256r1,
  221. &ecdsa_signature_secp256r1_faulty,
  222. hash_digest,
  223. sizeof(nrf_cc310_bl_hash_digest_sha256_t));
  224. if (err_code == SASI_SUCCESS)
  225. {
  226. INTEG_TEST_PRINT("Verification not supposed to be successful!\r\n");
  227. test_check(err_code);
  228. }
  229. else
  230. {
  231. INTEG_TEST_PRINT("Verification failed as it should!\r\n");
  232. }
  233. //
  234. // Invalid ECDSA secp256r1 verify test (wrong hash)
  235. //
  236. INTEG_TEST_PRINT("\r\n\r\n==================================================\r\n");
  237. INTEG_TEST_PRINT("Testing ECDSA secp256r1 verify failure scenario (wrong hash)\r\n\r\n");
  238. INTEG_TEST_PRINT("Initializing ecdsa verify context secp256r1\r\n");
  239. err_code = nrf_cc310_bl_ecdsa_verify_init_secp256r1(&ecdsa_context_secp256r1, &ecc_public_key_secp256r1);
  240. test_check(err_code);
  241. INTEG_TEST_PRINT("Executing ecdsa verify secp256r1 with wrong hash\r\n");
  242. err_code = nrf_cc310_bl_ecdsa_verify_hash_secp256r1(&ecdsa_context_secp256r1,
  243. &ecdsa_signature_secp256r1,
  244. hash_digest_faulty,
  245. sizeof(nrf_cc310_bl_hash_digest_sha256_t));
  246. if (err_code == SASI_SUCCESS)
  247. {
  248. INTEG_TEST_PRINT("Verification not supposed to be successful!\r\n");
  249. test_check(err_code);
  250. }
  251. else
  252. {
  253. INTEG_TEST_PRINT("Verification failed as it should!\r\n");
  254. }
  255. //
  256. // Valid ECDSA secp224r1 verify test
  257. //
  258. INTEG_TEST_PRINT("\r\n\r\n====================================\r\n");
  259. INTEG_TEST_PRINT("Testing valid ECDSA secp224r1 verify\r\n\r\n");
  260. INTEG_TEST_PRINT("Initializing ecdsa verify context secp224r1\r\n");
  261. err_code = nrf_cc310_bl_ecdsa_verify_init_secp224r1(&ecdsa_context_secp224r1, &ecc_public_key_secp224r1);
  262. test_check(err_code);
  263. INTEG_TEST_PRINT("Executing ecdsa verify secp224r1\r\n");
  264. err_code = nrf_cc310_bl_ecdsa_verify_hash_secp224r1(&ecdsa_context_secp224r1,
  265. &ecdsa_signature_secp224r1,
  266. hash_digest,
  267. sizeof(nrf_cc310_bl_hash_digest_sha256_t));
  268. if (err_code == SASI_SUCCESS)
  269. {
  270. INTEG_TEST_PRINT("Verification was successful!\r\n");
  271. }
  272. else
  273. {
  274. INTEG_TEST_PRINT("Verification failed!\r\n");
  275. test_check(err_code);
  276. }
  277. //
  278. // Invalid ECDSA secp224r1 verify test (wrong signature)
  279. //
  280. INTEG_TEST_PRINT("\r\n\r\n=======================================================\r\n");
  281. INTEG_TEST_PRINT("Testing ECDSA secp224r1 verify failure scenario (wrong signature)\r\n\r\n");
  282. INTEG_TEST_PRINT("Initializing ecdsa verify context secp224r1\r\n");
  283. err_code = nrf_cc310_bl_ecdsa_verify_init_secp224r1(&ecdsa_context_secp224r1, &ecc_public_key_secp224r1);
  284. test_check(err_code);
  285. INTEG_TEST_PRINT("Executing ecdsa verify secp224r1\r\n");
  286. err_code = nrf_cc310_bl_ecdsa_verify_hash_secp224r1(&ecdsa_context_secp224r1,
  287. &ecdsa_signature_secp224r1_faulty,
  288. hash_digest,
  289. sizeof(nrf_cc310_bl_hash_digest_sha256_t));
  290. if (err_code == SASI_SUCCESS)
  291. {
  292. INTEG_TEST_PRINT("Verification not supposed to be successful!\r\n");
  293. test_check(err_code);
  294. }
  295. else
  296. {
  297. INTEG_TEST_PRINT("Verification failed as it should!\r\n");
  298. }
  299. //
  300. // Invalid ECDSA secp224r1 verify test (wrong hash)
  301. //
  302. INTEG_TEST_PRINT("\r\n\r\n==================================================\r\n");
  303. INTEG_TEST_PRINT("Testing ECDSA secp224r1 verify failure scenario (wrong hash)\r\n\r\n");
  304. INTEG_TEST_PRINT("Initializing ecdsa verify context secp224r1\r\n");
  305. err_code = nrf_cc310_bl_ecdsa_verify_init_secp224r1(&ecdsa_context_secp224r1, &ecc_public_key_secp224r1);
  306. test_check(err_code);
  307. INTEG_TEST_PRINT("Executing ecdsa verify secp224r1 with wrong hash\r\n");
  308. err_code = nrf_cc310_bl_ecdsa_verify_hash_secp224r1(&ecdsa_context_secp224r1,
  309. &ecdsa_signature_secp224r1,
  310. hash_digest_faulty,
  311. sizeof(nrf_cc310_bl_hash_digest_sha256_t));
  312. if (err_code == SASI_SUCCESS)
  313. {
  314. INTEG_TEST_PRINT("Verification not supposed to be successful!\r\n");
  315. test_check(err_code);
  316. }
  317. else
  318. {
  319. INTEG_TEST_PRINT("Verification failed as it should!\r\n");
  320. }
  321. INTEG_TEST_PRINT("\r\n\r\n=================================\r\n");
  322. INTEG_TEST_PRINT("All tests passed!\r\n");
  323. INTEG_TEST_PRINT("=================================\r\n");
  324. while(1);
  325. }