ecc_dsa.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* ec_dsa.c - TinyCrypt implementation of EC-DSA */
  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. #include <tinycrypt/constants.h>
  54. #include <tinycrypt/ecc.h>
  55. #include <tinycrypt/ecc_dsa.h>
  56. #if default_RNG_defined
  57. static uECC_RNG_Function g_rng_function = &default_CSPRNG;
  58. #else
  59. static uECC_RNG_Function g_rng_function = 0;
  60. #endif
  61. static void bits2int(uECC_word_t *native, const uint8_t *bits,
  62. unsigned bits_size, uECC_Curve curve)
  63. {
  64. unsigned num_n_bytes = BITS_TO_BYTES(curve->num_n_bits);
  65. unsigned num_n_words = BITS_TO_WORDS(curve->num_n_bits);
  66. int shift;
  67. uECC_word_t carry;
  68. uECC_word_t *ptr;
  69. if (bits_size > num_n_bytes) {
  70. bits_size = num_n_bytes;
  71. }
  72. uECC_vli_clear(native, num_n_words);
  73. uECC_vli_bytesToNative(native, bits, bits_size);
  74. if (bits_size * 8 <= (unsigned)curve->num_n_bits) {
  75. return;
  76. }
  77. shift = bits_size * 8 - curve->num_n_bits;
  78. carry = 0;
  79. ptr = native + num_n_words;
  80. while (ptr-- > native) {
  81. uECC_word_t temp = *ptr;
  82. *ptr = (temp >> shift) | carry;
  83. carry = temp << (uECC_WORD_BITS - shift);
  84. }
  85. /* Reduce mod curve_n */
  86. if (uECC_vli_cmp_unsafe(curve->n, native, num_n_words) != 1) {
  87. uECC_vli_sub(native, native, curve->n, num_n_words);
  88. }
  89. }
  90. int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
  91. unsigned hash_size, uECC_word_t *k, uint8_t *signature,
  92. uECC_Curve curve)
  93. {
  94. uECC_word_t tmp[NUM_ECC_WORDS];
  95. uECC_word_t s[NUM_ECC_WORDS];
  96. uECC_word_t *k2[2] = {tmp, s};
  97. uECC_word_t p[NUM_ECC_WORDS * 2];
  98. uECC_word_t carry;
  99. wordcount_t num_words = curve->num_words;
  100. wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
  101. bitcount_t num_n_bits = curve->num_n_bits;
  102. /* Make sure 0 < k < curve_n */
  103. if (uECC_vli_isZero(k, num_words) ||
  104. uECC_vli_cmp(curve->n, k, num_n_words) != 1) {
  105. return 0;
  106. }
  107. carry = regularize_k(k, tmp, s, curve);
  108. EccPoint_mult(p, curve->G, k2[!carry], 0, num_n_bits + 1, curve);
  109. if (uECC_vli_isZero(p, num_words)) {
  110. return 0;
  111. }
  112. /* If an RNG function was specified, get a random number
  113. to prevent side channel analysis of k. */
  114. if (!g_rng_function) {
  115. uECC_vli_clear(tmp, num_n_words);
  116. tmp[0] = 1;
  117. }
  118. else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
  119. return 0;
  120. }
  121. /* Prevent side channel analysis of uECC_vli_modInv() to determine
  122. bits of k / the private key by premultiplying by a random number */
  123. uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */
  124. uECC_vli_modInv(k, k, curve->n, num_n_words); /* k = 1 / k' */
  125. uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */
  126. uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
  127. /* tmp = d: */
  128. uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
  129. s[num_n_words - 1] = 0;
  130. uECC_vli_set(s, p, num_words);
  131. uECC_vli_modMult(s, tmp, s, curve->n, num_n_words); /* s = r*d */
  132. bits2int(tmp, message_hash, hash_size, curve);
  133. uECC_vli_modAdd(s, tmp, s, curve->n, num_n_words); /* s = e + r*d */
  134. uECC_vli_modMult(s, s, k, curve->n, num_n_words); /* s = (e + r*d) / k */
  135. if (uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) {
  136. return 0;
  137. }
  138. uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s);
  139. return 1;
  140. }
  141. int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
  142. unsigned hash_size, uint8_t *signature, uECC_Curve curve)
  143. {
  144. uECC_word_t _random[2*NUM_ECC_WORDS];
  145. uECC_word_t k[NUM_ECC_WORDS];
  146. uECC_word_t tries;
  147. for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
  148. /* Generating _random uniformly at random: */
  149. uECC_RNG_Function rng_function = uECC_get_rng();
  150. if (!rng_function ||
  151. !rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE)) {
  152. return 0;
  153. }
  154. // computing k as modular reduction of _random (see FIPS 186.4 B.5.1):
  155. uECC_vli_mmod(k, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
  156. if (uECC_sign_with_k(private_key, message_hash, hash_size, k, signature,
  157. curve)) {
  158. return 1;
  159. }
  160. }
  161. return 0;
  162. }
  163. static bitcount_t smax(bitcount_t a, bitcount_t b)
  164. {
  165. return (a > b ? a : b);
  166. }
  167. int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
  168. unsigned hash_size, const uint8_t *signature,
  169. uECC_Curve curve)
  170. {
  171. uECC_word_t u1[NUM_ECC_WORDS], u2[NUM_ECC_WORDS];
  172. uECC_word_t z[NUM_ECC_WORDS];
  173. uECC_word_t sum[NUM_ECC_WORDS * 2];
  174. uECC_word_t rx[NUM_ECC_WORDS];
  175. uECC_word_t ry[NUM_ECC_WORDS];
  176. uECC_word_t tx[NUM_ECC_WORDS];
  177. uECC_word_t ty[NUM_ECC_WORDS];
  178. uECC_word_t tz[NUM_ECC_WORDS];
  179. const uECC_word_t *points[4];
  180. const uECC_word_t *point;
  181. bitcount_t num_bits;
  182. bitcount_t i;
  183. uECC_word_t _public[NUM_ECC_WORDS * 2];
  184. uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
  185. wordcount_t num_words = curve->num_words;
  186. wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
  187. rx[num_n_words - 1] = 0;
  188. r[num_n_words - 1] = 0;
  189. s[num_n_words - 1] = 0;
  190. uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
  191. uECC_vli_bytesToNative(_public + num_words, public_key + curve->num_bytes,
  192. curve->num_bytes);
  193. uECC_vli_bytesToNative(r, signature, curve->num_bytes);
  194. uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
  195. /* r, s must not be 0. */
  196. if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
  197. return 0;
  198. }
  199. /* r, s must be < n. */
  200. if (uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 ||
  201. uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) {
  202. return 0;
  203. }
  204. /* Calculate u1 and u2. */
  205. uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
  206. u1[num_n_words - 1] = 0;
  207. bits2int(u1, message_hash, hash_size, curve);
  208. uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
  209. uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */
  210. /* Calculate sum = G + Q. */
  211. uECC_vli_set(sum, _public, num_words);
  212. uECC_vli_set(sum + num_words, _public + num_words, num_words);
  213. uECC_vli_set(tx, curve->G, num_words);
  214. uECC_vli_set(ty, curve->G + num_words, num_words);
  215. uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
  216. XYcZ_add(tx, ty, sum, sum + num_words, curve);
  217. uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
  218. apply_z(sum, sum + num_words, z, curve);
  219. /* Use Shamir's trick to calculate u1*G + u2*Q */
  220. points[0] = 0;
  221. points[1] = curve->G;
  222. points[2] = _public;
  223. points[3] = sum;
  224. num_bits = smax(uECC_vli_numBits(u1, num_n_words),
  225. uECC_vli_numBits(u2, num_n_words));
  226. point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
  227. ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
  228. uECC_vli_set(rx, point, num_words);
  229. uECC_vli_set(ry, point + num_words, num_words);
  230. uECC_vli_clear(z, num_words);
  231. z[0] = 1;
  232. for (i = num_bits - 2; i >= 0; --i) {
  233. uECC_word_t index;
  234. curve->double_jacobian(rx, ry, z, curve);
  235. index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
  236. point = points[index];
  237. if (point) {
  238. uECC_vli_set(tx, point, num_words);
  239. uECC_vli_set(ty, point + num_words, num_words);
  240. apply_z(tx, ty, z, curve);
  241. uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
  242. XYcZ_add(tx, ty, rx, ry, curve);
  243. uECC_vli_modMult_fast(z, z, tz, curve);
  244. }
  245. }
  246. uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
  247. apply_z(rx, ry, z, curve);
  248. /* v = x1 (mod n) */
  249. if (uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) {
  250. uECC_vli_sub(rx, rx, curve->n, num_n_words);
  251. }
  252. /* Accept only if v == r. */
  253. return (int)(uECC_vli_equal(rx, r, num_words) == 0);
  254. }