test_ecc_utils.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* test_ecc_utils.c - TinyCrypt common functions for ECC tests */
  2. /* Copyright (c) 2014, Kenneth MacKay
  3. * 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. * * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. * POSSIBILITY OF SUCH DAMAGE.*/
  24. /*
  25. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or without
  28. * modification, are permitted provided that the following conditions are met:
  29. *
  30. * - Redistributions of source code must retain the above copyright notice,
  31. * this list of conditions and the following disclaimer.
  32. *
  33. * - Redistributions in binary form must reproduce the above copyright
  34. * notice, this list of conditions and the following disclaimer in the
  35. * documentation and/or other materials provided with the distribution.
  36. *
  37. * - Neither the name of Intel Corporation nor the names of its contributors
  38. * may be used to endorse or promote products derived from this software
  39. * without specific prior written permission.
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  42. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  45. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  46. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  47. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  48. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  49. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  51. * POSSIBILITY OF SUCH DAMAGE.
  52. *
  53. * test_ecc_utils.c -- Implementation of some common functions for ECC tests.
  54. *
  55. */
  56. #include <test_ecc_utils.h>
  57. #include <tinycrypt/constants.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <unistd.h>
  62. #include <fcntl.h>
  63. #include <stdbool.h>
  64. #include <unistd.h>
  65. int hex2int (char hex)
  66. {
  67. uint8_t dec;
  68. if ('0' <= hex && hex <= '9') dec = hex - '0';
  69. else if ('a' <= hex && hex <= 'f') dec = hex - 'a' + 10;
  70. else if ('A' <= hex && hex <= 'F') dec = hex - 'A' + 10;
  71. else return -1;
  72. return dec;
  73. }
  74. /*
  75. * Convert hex string to byte string
  76. * Return number of bytes written to buf, or 0 on error
  77. */
  78. int hex2bin(uint8_t *buf, const size_t buflen, const char *hex,
  79. const size_t hexlen)
  80. {
  81. int dec;
  82. if (buflen < hexlen / 2 + hexlen % 2)
  83. {
  84. return false;
  85. }
  86. /* if hexlen is uneven, insert leading zero nibble */
  87. if (hexlen % 2)
  88. {
  89. dec = hex2int(hex[0]);
  90. if (dec == -1)
  91. return false;
  92. buf[0] = dec;
  93. buf++;
  94. hex++;
  95. }
  96. /* regular hex conversion */
  97. for (size_t i = 0; i < hexlen / 2; i++)
  98. {
  99. dec = hex2int(hex[2 * i]);
  100. if (dec == -1)
  101. {
  102. return false;
  103. }
  104. buf[i] = dec << 4;
  105. dec = hex2int(hex[ 2 * i + 1]);
  106. if (dec == -1)
  107. {
  108. return false;
  109. }
  110. buf[i] += dec;
  111. }
  112. return hexlen / 2 + hexlen % 2;
  113. }
  114. /*
  115. * Convert hex string to zero-padded nanoECC scalar
  116. */
  117. void string2scalar(unsigned int *scalar, unsigned int num_word32, char *str)
  118. {
  119. unsigned int num_bytes = 4 * num_word32;
  120. uint8_t tmp[num_bytes];
  121. size_t hexlen = strlen(str);
  122. int padding;
  123. if (0 > (padding = 2 * num_bytes - strlen(str)))
  124. {
  125. printf("Error: 2 * num_bytes(%d) < strlen(hex) (%zu)\n",
  126. 2 * num_bytes, strlen(str));
  127. exit(-1);
  128. }
  129. memset(tmp, 0, padding / 2);
  130. if (false == hex2bin(tmp + padding / 2, num_bytes, str, hexlen))
  131. {
  132. exit(-1);
  133. }
  134. uECC_vli_bytesToNative(scalar, tmp, num_bytes);
  135. }
  136. void vli_print_bytes(uint8_t *vli, unsigned int size)
  137. {
  138. for(unsigned i = 0; i < size; ++i)
  139. {
  140. printf("%02X ", (unsigned)vli[i]);
  141. }
  142. }
  143. void print_ecc_scalar(const char *label, const unsigned int * p_vli,
  144. unsigned int num_word32)
  145. {
  146. unsigned int i;
  147. if (label) {
  148. printf("%s = { ", label);
  149. }
  150. for(i = 0; i < num_word32 - 1; ++i) {
  151. printf("0x%08lX, ", (unsigned long)p_vli[i]);
  152. }
  153. printf("0x%08lX", (unsigned long)p_vli[i]);
  154. if (label) {
  155. printf(" };\n");
  156. }
  157. }
  158. int check_ecc_result(const int num, const char *name,
  159. const unsigned int *expected,
  160. const unsigned int *computed,
  161. const unsigned int num_word32, const bool verbose)
  162. {
  163. uint32_t num_bytes = 4 * num_word32;
  164. if (memcmp(computed, expected, num_bytes)) {
  165. TC_PRINT("\n Vector #%02d check %s - FAILURE:\n\n", num, name);
  166. print_ecc_scalar("Expected", expected, num_word32);
  167. print_ecc_scalar("Computed", computed, num_word32);
  168. TC_PRINT("\n");
  169. return TC_FAIL;
  170. }
  171. if (verbose) {
  172. TC_PRINT(" Vector #%02d check %s - success\n", num, name);
  173. }
  174. return TC_PASS;
  175. }
  176. int check_code(const int num, const char *name, const int expected,
  177. const int computed, const int verbose)
  178. {
  179. if (expected != computed) {
  180. TC_ERROR("\n Vector #%02d check %s - FAILURE:\n", num, name);
  181. TC_ERROR("\n Expected: %d, computed: %d\n\n", expected, computed);
  182. return TC_FAIL;
  183. }
  184. if (verbose) {
  185. TC_PRINT(" Vector #%02d check %s - success (%d=%d)\n", num, name,
  186. expected, computed);
  187. }
  188. return TC_PASS;
  189. }
  190. /* Test ecc_make_keys, and also as keygen part of other tests */
  191. int keygen_vectors(char **d_vec, char **qx_vec, char **qy_vec, int tests,
  192. bool verbose)
  193. {
  194. unsigned int pub[2 * NUM_ECC_WORDS];
  195. unsigned int d[NUM_ECC_WORDS];
  196. unsigned int prv[NUM_ECC_WORDS];
  197. unsigned int result = TC_PASS;
  198. /* expected outputs (converted input vectors) */
  199. unsigned int exp_pub[2 * NUM_ECC_WORDS];
  200. unsigned int exp_prv[NUM_ECC_WORDS];
  201. for (int i = 0; i < tests; i++) {
  202. string2scalar(exp_prv, NUM_ECC_WORDS, d_vec[i]);
  203. string2scalar(exp_pub, NUM_ECC_WORDS, qx_vec[i]);
  204. string2scalar(exp_pub + NUM_ECC_WORDS, NUM_ECC_WORDS, qy_vec[i]);
  205. /*
  206. * Feed prvkey vector as padded random seed into ecc_make_key.
  207. * Internal mod-reduction will be zero-op and generate correct prv/pub
  208. */
  209. memset(d, 0, NUM_ECC_WORDS);
  210. string2scalar(d, NUM_ECC_WORDS, d_vec[i]);
  211. uint8_t pub_bytes[2*NUM_ECC_BYTES];
  212. uint8_t prv_bytes[NUM_ECC_BYTES];
  213. uECC_make_key_with_d(pub_bytes, prv_bytes, d, uECC_secp256r1());
  214. uECC_vli_bytesToNative(prv, prv_bytes, NUM_ECC_BYTES);
  215. uECC_vli_bytesToNative(pub, pub_bytes, NUM_ECC_BYTES);
  216. uECC_vli_bytesToNative(pub + NUM_ECC_WORDS, pub_bytes + NUM_ECC_BYTES, NUM_ECC_BYTES);
  217. /* validate correctness of vector conversion and make_key() */
  218. result = check_ecc_result(i, "prv ", exp_prv, prv, NUM_ECC_WORDS, verbose);
  219. if (result == TC_FAIL) {
  220. return result;
  221. }
  222. result = check_ecc_result(i, "pub.x", exp_pub, pub, NUM_ECC_WORDS, verbose);
  223. if (result == TC_FAIL) {
  224. return result;
  225. }
  226. result = check_ecc_result(i, "pub.y", exp_pub + NUM_ECC_WORDS, pub + NUM_ECC_WORDS, NUM_ECC_WORDS, verbose);
  227. if (result == TC_FAIL) {
  228. return result;
  229. }
  230. }
  231. return result;
  232. }