ecc_dsa.c 10 KB

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