ecc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /* ecc.c - TinyCrypt implementation of common ECC functions */
  2. /*
  3. * Copyright (c) 2014, Kenneth MacKay
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE 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/ecc.h>
  54. #include <tinycrypt/ecc_platform_specific.h>
  55. #include <string.h>
  56. /* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform
  57. * has access to enough entropy in order to feed the PRNG regularly. */
  58. #if default_RNG_defined
  59. static uECC_RNG_Function g_rng_function = &default_CSPRNG;
  60. #else
  61. static uECC_RNG_Function g_rng_function = 0;
  62. #endif
  63. void uECC_set_rng(uECC_RNG_Function rng_function)
  64. {
  65. g_rng_function = rng_function;
  66. }
  67. uECC_RNG_Function uECC_get_rng(void)
  68. {
  69. return g_rng_function;
  70. }
  71. int uECC_curve_private_key_size(uECC_Curve curve)
  72. {
  73. return BITS_TO_BYTES(curve->num_n_bits);
  74. }
  75. int uECC_curve_public_key_size(uECC_Curve curve)
  76. {
  77. return 2 * curve->num_bytes;
  78. }
  79. void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words)
  80. {
  81. wordcount_t i;
  82. for (i = 0; i < num_words; ++i) {
  83. vli[i] = 0;
  84. }
  85. }
  86. uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words)
  87. {
  88. uECC_word_t bits = 0;
  89. wordcount_t i;
  90. for (i = 0; i < num_words; ++i) {
  91. bits |= vli[i];
  92. }
  93. return (bits == 0);
  94. }
  95. uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit)
  96. {
  97. return (vli[bit >> uECC_WORD_BITS_SHIFT] &
  98. ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
  99. }
  100. /* Counts the number of words in vli. */
  101. static wordcount_t vli_numDigits(const uECC_word_t *vli,
  102. const wordcount_t max_words)
  103. {
  104. wordcount_t i;
  105. /* Search from the end until we find a non-zero digit. We do it in reverse
  106. * because we expect that most digits will be nonzero. */
  107. for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) {
  108. }
  109. return (i + 1);
  110. }
  111. bitcount_t uECC_vli_numBits(const uECC_word_t *vli,
  112. const wordcount_t max_words)
  113. {
  114. uECC_word_t i;
  115. uECC_word_t digit;
  116. wordcount_t num_digits = vli_numDigits(vli, max_words);
  117. if (num_digits == 0) {
  118. return 0;
  119. }
  120. digit = vli[num_digits - 1];
  121. for (i = 0; digit; ++i) {
  122. digit >>= 1;
  123. }
  124. return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
  125. }
  126. void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src,
  127. wordcount_t num_words)
  128. {
  129. wordcount_t i;
  130. for (i = 0; i < num_words; ++i) {
  131. dest[i] = src[i];
  132. }
  133. }
  134. cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
  135. const uECC_word_t *right,
  136. wordcount_t num_words)
  137. {
  138. wordcount_t i;
  139. for (i = num_words - 1; i >= 0; --i) {
  140. if (left[i] > right[i]) {
  141. return 1;
  142. } else if (left[i] < right[i]) {
  143. return -1;
  144. }
  145. }
  146. return 0;
  147. }
  148. uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right,
  149. wordcount_t num_words)
  150. {
  151. uECC_word_t diff = 0;
  152. wordcount_t i;
  153. for (i = num_words - 1; i >= 0; --i) {
  154. diff |= (left[i] ^ right[i]);
  155. }
  156. return !(diff == 0);
  157. }
  158. uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond)
  159. {
  160. return (p_true*(cond)) | (p_false*(!cond));
  161. }
  162. /* Computes result = left - right, returning borrow, in constant time.
  163. * Can modify in place. */
  164. uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
  165. const uECC_word_t *right, wordcount_t num_words)
  166. {
  167. uECC_word_t borrow = 0;
  168. wordcount_t i;
  169. for (i = 0; i < num_words; ++i) {
  170. uECC_word_t diff = left[i] - right[i] - borrow;
  171. uECC_word_t val = (diff > left[i]);
  172. borrow = cond_set(val, borrow, (diff != left[i]));
  173. result[i] = diff;
  174. }
  175. return borrow;
  176. }
  177. /* Computes result = left + right, returning carry, in constant time.
  178. * Can modify in place. */
  179. static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left,
  180. const uECC_word_t *right, wordcount_t num_words)
  181. {
  182. uECC_word_t carry = 0;
  183. wordcount_t i;
  184. for (i = 0; i < num_words; ++i) {
  185. uECC_word_t sum = left[i] + right[i] + carry;
  186. uECC_word_t val = (sum < left[i]);
  187. carry = cond_set(val, carry, (sum != left[i]));
  188. result[i] = sum;
  189. }
  190. return carry;
  191. }
  192. cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right,
  193. wordcount_t num_words)
  194. {
  195. uECC_word_t tmp[NUM_ECC_WORDS];
  196. uECC_word_t neg = !!uECC_vli_sub(tmp, left, right, num_words);
  197. uECC_word_t equal = uECC_vli_isZero(tmp, num_words);
  198. return (!equal - 2 * neg);
  199. }
  200. /* Computes vli = vli >> 1. */
  201. static void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words)
  202. {
  203. uECC_word_t *end = vli;
  204. uECC_word_t carry = 0;
  205. vli += num_words;
  206. while (vli-- > end) {
  207. uECC_word_t temp = *vli;
  208. *vli = (temp >> 1) | carry;
  209. carry = temp << (uECC_WORD_BITS - 1);
  210. }
  211. }
  212. static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0,
  213. uECC_word_t *r1, uECC_word_t *r2)
  214. {
  215. uECC_dword_t p = (uECC_dword_t)a * b;
  216. uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
  217. r01 += p;
  218. *r2 += (r01 < p);
  219. *r1 = r01 >> uECC_WORD_BITS;
  220. *r0 = (uECC_word_t)r01;
  221. }
  222. /* Computes result = left * right. Result must be 2 * num_words long. */
  223. static void uECC_vli_mult(uECC_word_t *result, const uECC_word_t *left,
  224. const uECC_word_t *right, wordcount_t num_words)
  225. {
  226. uECC_word_t r0 = 0;
  227. uECC_word_t r1 = 0;
  228. uECC_word_t r2 = 0;
  229. wordcount_t i, k;
  230. /* Compute each digit of result in sequence, maintaining the carries. */
  231. for (k = 0; k < num_words; ++k) {
  232. for (i = 0; i <= k; ++i) {
  233. muladd(left[i], right[k - i], &r0, &r1, &r2);
  234. }
  235. result[k] = r0;
  236. r0 = r1;
  237. r1 = r2;
  238. r2 = 0;
  239. }
  240. for (k = num_words; k < num_words * 2 - 1; ++k) {
  241. for (i = (k + 1) - num_words; i < num_words; ++i) {
  242. muladd(left[i], right[k - i], &r0, &r1, &r2);
  243. }
  244. result[k] = r0;
  245. r0 = r1;
  246. r1 = r2;
  247. r2 = 0;
  248. }
  249. result[num_words * 2 - 1] = r0;
  250. }
  251. void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
  252. const uECC_word_t *right, const uECC_word_t *mod,
  253. wordcount_t num_words)
  254. {
  255. uECC_word_t carry = uECC_vli_add(result, left, right, num_words);
  256. if (carry || uECC_vli_cmp_unsafe(mod, result, num_words) != 1) {
  257. /* result > mod (result = mod + remainder), so subtract mod to get
  258. * remainder. */
  259. uECC_vli_sub(result, result, mod, num_words);
  260. }
  261. }
  262. void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
  263. const uECC_word_t *right, const uECC_word_t *mod,
  264. wordcount_t num_words)
  265. {
  266. uECC_word_t l_borrow = uECC_vli_sub(result, left, right, num_words);
  267. if (l_borrow) {
  268. /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
  269. * we can get the correct result from result + mod (with overflow). */
  270. uECC_vli_add(result, result, mod, num_words);
  271. }
  272. }
  273. /* Computes result = product % mod, where product is 2N words long. */
  274. /* Currently only designed to work for curve_p or curve_n. */
  275. void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
  276. const uECC_word_t *mod, wordcount_t num_words)
  277. {
  278. uECC_word_t mod_multiple[2 * NUM_ECC_WORDS];
  279. uECC_word_t tmp[2 * NUM_ECC_WORDS];
  280. uECC_word_t *v[2] = {tmp, product};
  281. uECC_word_t index;
  282. /* Shift mod so its highest set bit is at the maximum position. */
  283. bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) -
  284. uECC_vli_numBits(mod, num_words);
  285. wordcount_t word_shift = shift / uECC_WORD_BITS;
  286. wordcount_t bit_shift = shift % uECC_WORD_BITS;
  287. uECC_word_t carry = 0;
  288. uECC_vli_clear(mod_multiple, word_shift);
  289. if (bit_shift > 0) {
  290. for(index = 0; index < (uECC_word_t)num_words; ++index) {
  291. mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry;
  292. carry = mod[index] >> (uECC_WORD_BITS - bit_shift);
  293. }
  294. } else {
  295. uECC_vli_set(mod_multiple + word_shift, mod, num_words);
  296. }
  297. for (index = 1; shift >= 0; --shift) {
  298. uECC_word_t borrow = 0;
  299. wordcount_t i;
  300. for (i = 0; i < num_words * 2; ++i) {
  301. uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow;
  302. if (diff != v[index][i]) {
  303. borrow = (diff > v[index][i]);
  304. }
  305. v[1 - index][i] = diff;
  306. }
  307. /* Swap the index if there was no borrow */
  308. index = !(index ^ borrow);
  309. uECC_vli_rshift1(mod_multiple, num_words);
  310. mod_multiple[num_words - 1] |= mod_multiple[num_words] <<
  311. (uECC_WORD_BITS - 1);
  312. uECC_vli_rshift1(mod_multiple + num_words, num_words);
  313. }
  314. uECC_vli_set(result, v[index], num_words);
  315. }
  316. void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
  317. const uECC_word_t *right, const uECC_word_t *mod,
  318. wordcount_t num_words)
  319. {
  320. uECC_word_t product[2 * NUM_ECC_WORDS];
  321. uECC_vli_mult(product, left, right, num_words);
  322. uECC_vli_mmod(result, product, mod, num_words);
  323. }
  324. void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
  325. const uECC_word_t *right, uECC_Curve curve)
  326. {
  327. uECC_word_t product[2 * NUM_ECC_WORDS];
  328. uECC_vli_mult(product, left, right, curve->num_words);
  329. curve->mmod_fast(result, product);
  330. }
  331. static void uECC_vli_modSquare_fast(uECC_word_t *result,
  332. const uECC_word_t *left,
  333. uECC_Curve curve)
  334. {
  335. uECC_vli_modMult_fast(result, left, left, curve);
  336. }
  337. #define EVEN(vli) (!(vli[0] & 1))
  338. static void vli_modInv_update(uECC_word_t *uv,
  339. const uECC_word_t *mod,
  340. wordcount_t num_words)
  341. {
  342. uECC_word_t carry = 0;
  343. if (!EVEN(uv)) {
  344. carry = uECC_vli_add(uv, uv, mod, num_words);
  345. }
  346. uECC_vli_rshift1(uv, num_words);
  347. if (carry) {
  348. uv[num_words - 1] |= HIGH_BIT_SET;
  349. }
  350. }
  351. void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
  352. const uECC_word_t *mod, wordcount_t num_words)
  353. {
  354. uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
  355. uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS];
  356. cmpresult_t cmpResult;
  357. if (uECC_vli_isZero(input, num_words)) {
  358. uECC_vli_clear(result, num_words);
  359. return;
  360. }
  361. uECC_vli_set(a, input, num_words);
  362. uECC_vli_set(b, mod, num_words);
  363. uECC_vli_clear(u, num_words);
  364. u[0] = 1;
  365. uECC_vli_clear(v, num_words);
  366. while ((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) {
  367. if (EVEN(a)) {
  368. uECC_vli_rshift1(a, num_words);
  369. vli_modInv_update(u, mod, num_words);
  370. } else if (EVEN(b)) {
  371. uECC_vli_rshift1(b, num_words);
  372. vli_modInv_update(v, mod, num_words);
  373. } else if (cmpResult > 0) {
  374. uECC_vli_sub(a, a, b, num_words);
  375. uECC_vli_rshift1(a, num_words);
  376. if (uECC_vli_cmp_unsafe(u, v, num_words) < 0) {
  377. uECC_vli_add(u, u, mod, num_words);
  378. }
  379. uECC_vli_sub(u, u, v, num_words);
  380. vli_modInv_update(u, mod, num_words);
  381. } else {
  382. uECC_vli_sub(b, b, a, num_words);
  383. uECC_vli_rshift1(b, num_words);
  384. if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) {
  385. uECC_vli_add(v, v, mod, num_words);
  386. }
  387. uECC_vli_sub(v, v, u, num_words);
  388. vli_modInv_update(v, mod, num_words);
  389. }
  390. }
  391. uECC_vli_set(result, u, num_words);
  392. }
  393. /* ------ Point operations ------ */
  394. void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
  395. uECC_word_t * Z1, uECC_Curve curve)
  396. {
  397. /* t1 = X, t2 = Y, t3 = Z */
  398. uECC_word_t t4[NUM_ECC_WORDS];
  399. uECC_word_t t5[NUM_ECC_WORDS];
  400. wordcount_t num_words = curve->num_words;
  401. if (uECC_vli_isZero(Z1, num_words)) {
  402. return;
  403. }
  404. uECC_vli_modSquare_fast(t4, Y1, curve); /* t4 = y1^2 */
  405. uECC_vli_modMult_fast(t5, X1, t4, curve); /* t5 = x1*y1^2 = A */
  406. uECC_vli_modSquare_fast(t4, t4, curve); /* t4 = y1^4 */
  407. uECC_vli_modMult_fast(Y1, Y1, Z1, curve); /* t2 = y1*z1 = z3 */
  408. uECC_vli_modSquare_fast(Z1, Z1, curve); /* t3 = z1^2 */
  409. uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = x1 + z1^2 */
  410. uECC_vli_modAdd(Z1, Z1, Z1, curve->p, num_words); /* t3 = 2*z1^2 */
  411. uECC_vli_modSub(Z1, X1, Z1, curve->p, num_words); /* t3 = x1 - z1^2 */
  412. uECC_vli_modMult_fast(X1, X1, Z1, curve); /* t1 = x1^2 - z1^4 */
  413. uECC_vli_modAdd(Z1, X1, X1, curve->p, num_words); /* t3 = 2*(x1^2 - z1^4) */
  414. uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = 3*(x1^2 - z1^4) */
  415. if (uECC_vli_testBit(X1, 0)) {
  416. uECC_word_t l_carry = uECC_vli_add(X1, X1, curve->p, num_words);
  417. uECC_vli_rshift1(X1, num_words);
  418. X1[num_words - 1] |= l_carry << (uECC_WORD_BITS - 1);
  419. } else {
  420. uECC_vli_rshift1(X1, num_words);
  421. }
  422. /* t1 = 3/2*(x1^2 - z1^4) = B */
  423. uECC_vli_modSquare_fast(Z1, X1, curve); /* t3 = B^2 */
  424. uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - A */
  425. uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - 2A = x3 */
  426. uECC_vli_modSub(t5, t5, Z1, curve->p, num_words); /* t5 = A - x3 */
  427. uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = B * (A - x3) */
  428. /* t4 = B * (A - x3) - y1^4 = y3: */
  429. uECC_vli_modSub(t4, X1, t4, curve->p, num_words);
  430. uECC_vli_set(X1, Z1, num_words);
  431. uECC_vli_set(Z1, Y1, num_words);
  432. uECC_vli_set(Y1, t4, num_words);
  433. }
  434. void x_side_default(uECC_word_t *result,
  435. const uECC_word_t *x,
  436. uECC_Curve curve)
  437. {
  438. uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */
  439. wordcount_t num_words = curve->num_words;
  440. uECC_vli_modSquare_fast(result, x, curve); /* r = x^2 */
  441. uECC_vli_modSub(result, result, _3, curve->p, num_words); /* r = x^2 - 3 */
  442. uECC_vli_modMult_fast(result, result, x, curve); /* r = x^3 - 3x */
  443. /* r = x^3 - 3x + b: */
  444. uECC_vli_modAdd(result, result, curve->b, curve->p, num_words);
  445. }
  446. uECC_Curve uECC_secp256r1(void)
  447. {
  448. return &curve_secp256r1;
  449. }
  450. void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product)
  451. {
  452. unsigned int tmp[NUM_ECC_WORDS];
  453. int carry;
  454. /* t */
  455. uECC_vli_set(result, product, NUM_ECC_WORDS);
  456. /* s1 */
  457. tmp[0] = tmp[1] = tmp[2] = 0;
  458. tmp[3] = product[11];
  459. tmp[4] = product[12];
  460. tmp[5] = product[13];
  461. tmp[6] = product[14];
  462. tmp[7] = product[15];
  463. carry = uECC_vli_add(tmp, tmp, tmp, NUM_ECC_WORDS);
  464. carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
  465. /* s2 */
  466. tmp[3] = product[12];
  467. tmp[4] = product[13];
  468. tmp[5] = product[14];
  469. tmp[6] = product[15];
  470. tmp[7] = 0;
  471. carry += uECC_vli_add(tmp, tmp, tmp, NUM_ECC_WORDS);
  472. carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
  473. /* s3 */
  474. tmp[0] = product[8];
  475. tmp[1] = product[9];
  476. tmp[2] = product[10];
  477. tmp[3] = tmp[4] = tmp[5] = 0;
  478. tmp[6] = product[14];
  479. tmp[7] = product[15];
  480. carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
  481. /* s4 */
  482. tmp[0] = product[9];
  483. tmp[1] = product[10];
  484. tmp[2] = product[11];
  485. tmp[3] = product[13];
  486. tmp[4] = product[14];
  487. tmp[5] = product[15];
  488. tmp[6] = product[13];
  489. tmp[7] = product[8];
  490. carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
  491. /* d1 */
  492. tmp[0] = product[11];
  493. tmp[1] = product[12];
  494. tmp[2] = product[13];
  495. tmp[3] = tmp[4] = tmp[5] = 0;
  496. tmp[6] = product[8];
  497. tmp[7] = product[10];
  498. carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
  499. /* d2 */
  500. tmp[0] = product[12];
  501. tmp[1] = product[13];
  502. tmp[2] = product[14];
  503. tmp[3] = product[15];
  504. tmp[4] = tmp[5] = 0;
  505. tmp[6] = product[9];
  506. tmp[7] = product[11];
  507. carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
  508. /* d3 */
  509. tmp[0] = product[13];
  510. tmp[1] = product[14];
  511. tmp[2] = product[15];
  512. tmp[3] = product[8];
  513. tmp[4] = product[9];
  514. tmp[5] = product[10];
  515. tmp[6] = 0;
  516. tmp[7] = product[12];
  517. carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
  518. /* d4 */
  519. tmp[0] = product[14];
  520. tmp[1] = product[15];
  521. tmp[2] = 0;
  522. tmp[3] = product[9];
  523. tmp[4] = product[10];
  524. tmp[5] = product[11];
  525. tmp[6] = 0;
  526. tmp[7] = product[13];
  527. carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
  528. if (carry < 0) {
  529. do {
  530. carry += uECC_vli_add(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
  531. }
  532. while (carry < 0);
  533. } else {
  534. while (carry ||
  535. uECC_vli_cmp_unsafe(curve_secp256r1.p, result, NUM_ECC_WORDS) != 1) {
  536. carry -= uECC_vli_sub(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
  537. }
  538. }
  539. }
  540. uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve)
  541. {
  542. return uECC_vli_isZero(point, curve->num_words * 2);
  543. }
  544. void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z,
  545. uECC_Curve curve)
  546. {
  547. uECC_word_t t1[NUM_ECC_WORDS];
  548. uECC_vli_modSquare_fast(t1, Z, curve); /* z^2 */
  549. uECC_vli_modMult_fast(X1, X1, t1, curve); /* x1 * z^2 */
  550. uECC_vli_modMult_fast(t1, t1, Z, curve); /* z^3 */
  551. uECC_vli_modMult_fast(Y1, Y1, t1, curve); /* y1 * z^3 */
  552. }
  553. /* P = (x1, y1) => 2P, (x2, y2) => P' */
  554. static void XYcZ_initial_double(uECC_word_t * X1, uECC_word_t * Y1,
  555. uECC_word_t * X2, uECC_word_t * Y2,
  556. const uECC_word_t * const initial_Z,
  557. uECC_Curve curve)
  558. {
  559. uECC_word_t z[NUM_ECC_WORDS];
  560. wordcount_t num_words = curve->num_words;
  561. if (initial_Z) {
  562. uECC_vli_set(z, initial_Z, num_words);
  563. } else {
  564. uECC_vli_clear(z, num_words);
  565. z[0] = 1;
  566. }
  567. uECC_vli_set(X2, X1, num_words);
  568. uECC_vli_set(Y2, Y1, num_words);
  569. apply_z(X1, Y1, z, curve);
  570. curve->double_jacobian(X1, Y1, z, curve);
  571. apply_z(X2, Y2, z, curve);
  572. }
  573. void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1,
  574. uECC_word_t * X2, uECC_word_t * Y2,
  575. uECC_Curve curve)
  576. {
  577. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  578. uECC_word_t t5[NUM_ECC_WORDS];
  579. wordcount_t num_words = curve->num_words;
  580. uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
  581. uECC_vli_modSquare_fast(t5, t5, curve); /* t5 = (x2 - x1)^2 = A */
  582. uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = x1*A = B */
  583. uECC_vli_modMult_fast(X2, X2, t5, curve); /* t3 = x2*A = C */
  584. uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
  585. uECC_vli_modSquare_fast(t5, Y2, curve); /* t5 = (y2 - y1)^2 = D */
  586. uECC_vli_modSub(t5, t5, X1, curve->p, num_words); /* t5 = D - B */
  587. uECC_vli_modSub(t5, t5, X2, curve->p, num_words); /* t5 = D - B - C = x3 */
  588. uECC_vli_modSub(X2, X2, X1, curve->p, num_words); /* t3 = C - B */
  589. uECC_vli_modMult_fast(Y1, Y1, X2, curve); /* t2 = y1*(C - B) */
  590. uECC_vli_modSub(X2, X1, t5, curve->p, num_words); /* t3 = B - x3 */
  591. uECC_vli_modMult_fast(Y2, Y2, X2, curve); /* t4 = (y2 - y1)*(B - x3) */
  592. uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y3 */
  593. uECC_vli_set(X2, t5, num_words);
  594. }
  595. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  596. Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
  597. or P => P - Q, Q => P + Q
  598. */
  599. static void XYcZ_addC(uECC_word_t * X1, uECC_word_t * Y1,
  600. uECC_word_t * X2, uECC_word_t * Y2,
  601. uECC_Curve curve)
  602. {
  603. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  604. uECC_word_t t5[NUM_ECC_WORDS];
  605. uECC_word_t t6[NUM_ECC_WORDS];
  606. uECC_word_t t7[NUM_ECC_WORDS];
  607. wordcount_t num_words = curve->num_words;
  608. uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
  609. uECC_vli_modSquare_fast(t5, t5, curve); /* t5 = (x2 - x1)^2 = A */
  610. uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = x1*A = B */
  611. uECC_vli_modMult_fast(X2, X2, t5, curve); /* t3 = x2*A = C */
  612. uECC_vli_modAdd(t5, Y2, Y1, curve->p, num_words); /* t5 = y2 + y1 */
  613. uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
  614. uECC_vli_modSub(t6, X2, X1, curve->p, num_words); /* t6 = C - B */
  615. uECC_vli_modMult_fast(Y1, Y1, t6, curve); /* t2 = y1 * (C - B) = E */
  616. uECC_vli_modAdd(t6, X1, X2, curve->p, num_words); /* t6 = B + C */
  617. uECC_vli_modSquare_fast(X2, Y2, curve); /* t3 = (y2 - y1)^2 = D */
  618. uECC_vli_modSub(X2, X2, t6, curve->p, num_words); /* t3 = D - (B + C) = x3 */
  619. uECC_vli_modSub(t7, X1, X2, curve->p, num_words); /* t7 = B - x3 */
  620. uECC_vli_modMult_fast(Y2, Y2, t7, curve); /* t4 = (y2 - y1)*(B - x3) */
  621. /* t4 = (y2 - y1)*(B - x3) - E = y3: */
  622. uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words);
  623. uECC_vli_modSquare_fast(t7, t5, curve); /* t7 = (y2 + y1)^2 = F */
  624. uECC_vli_modSub(t7, t7, t6, curve->p, num_words); /* t7 = F - (B + C) = x3' */
  625. uECC_vli_modSub(t6, t7, X1, curve->p, num_words); /* t6 = x3' - B */
  626. uECC_vli_modMult_fast(t6, t6, t5, curve); /* t6 = (y2+y1)*(x3' - B) */
  627. /* t2 = (y2+y1)*(x3' - B) - E = y3': */
  628. uECC_vli_modSub(Y1, t6, Y1, curve->p, num_words);
  629. uECC_vli_set(X1, t7, num_words);
  630. }
  631. void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
  632. const uECC_word_t * scalar,
  633. const uECC_word_t * initial_Z,
  634. bitcount_t num_bits, uECC_Curve curve)
  635. {
  636. /* R0 and R1 */
  637. uECC_word_t Rx[2][NUM_ECC_WORDS];
  638. uECC_word_t Ry[2][NUM_ECC_WORDS];
  639. uECC_word_t z[NUM_ECC_WORDS];
  640. bitcount_t i;
  641. uECC_word_t nb;
  642. wordcount_t num_words = curve->num_words;
  643. uECC_vli_set(Rx[1], point, num_words);
  644. uECC_vli_set(Ry[1], point + num_words, num_words);
  645. XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve);
  646. for (i = num_bits - 2; i > 0; --i) {
  647. nb = !uECC_vli_testBit(scalar, i);
  648. XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve);
  649. XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve);
  650. }
  651. nb = !uECC_vli_testBit(scalar, 0);
  652. XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve);
  653. /* Find final 1/Z value. */
  654. uECC_vli_modSub(z, Rx[1], Rx[0], curve->p, num_words); /* X1 - X0 */
  655. uECC_vli_modMult_fast(z, z, Ry[1 - nb], curve); /* Yb * (X1 - X0) */
  656. uECC_vli_modMult_fast(z, z, point, curve); /* xP * Yb * (X1 - X0) */
  657. uECC_vli_modInv(z, z, curve->p, num_words); /* 1 / (xP * Yb * (X1 - X0))*/
  658. /* yP / (xP * Yb * (X1 - X0)) */
  659. uECC_vli_modMult_fast(z, z, point + num_words, curve);
  660. /* Xb * yP / (xP * Yb * (X1 - X0)) */
  661. uECC_vli_modMult_fast(z, z, Rx[1 - nb], curve);
  662. /* End 1/Z calculation */
  663. XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve);
  664. apply_z(Rx[0], Ry[0], z, curve);
  665. uECC_vli_set(result, Rx[0], num_words);
  666. uECC_vli_set(result + num_words, Ry[0], num_words);
  667. }
  668. uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
  669. uECC_word_t *k1, uECC_Curve curve)
  670. {
  671. wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
  672. bitcount_t num_n_bits = curve->num_n_bits;
  673. uECC_word_t carry = uECC_vli_add(k0, k, curve->n, num_n_words) ||
  674. (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
  675. uECC_vli_testBit(k0, num_n_bits));
  676. uECC_vli_add(k1, k0, curve->n, num_n_words);
  677. return carry;
  678. }
  679. uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
  680. uECC_word_t *private_key,
  681. uECC_Curve curve)
  682. {
  683. uECC_word_t tmp1[NUM_ECC_WORDS];
  684. uECC_word_t tmp2[NUM_ECC_WORDS];
  685. uECC_word_t *p2[2] = {tmp1, tmp2};
  686. uECC_word_t carry;
  687. /* Regularize the bitcount for the private key so that attackers cannot
  688. * use a side channel attack to learn the number of leading zeros. */
  689. carry = regularize_k(private_key, tmp1, tmp2, curve);
  690. EccPoint_mult(result, curve->G, p2[!carry], 0, curve->num_n_bits + 1, curve);
  691. if (EccPoint_isZero(result, curve)) {
  692. return 0;
  693. }
  694. return 1;
  695. }
  696. /* Converts an integer in uECC native format to big-endian bytes. */
  697. void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
  698. const unsigned int *native)
  699. {
  700. wordcount_t i;
  701. for (i = 0; i < num_bytes; ++i) {
  702. unsigned b = num_bytes - 1 - i;
  703. bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE));
  704. }
  705. }
  706. /* Converts big-endian bytes to an integer in uECC native format. */
  707. void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
  708. int num_bytes)
  709. {
  710. wordcount_t i;
  711. uECC_vli_clear(native, (num_bytes + (uECC_WORD_SIZE - 1)) / uECC_WORD_SIZE);
  712. for (i = 0; i < num_bytes; ++i) {
  713. unsigned b = num_bytes - 1 - i;
  714. native[b / uECC_WORD_SIZE] |=
  715. (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE));
  716. }
  717. }
  718. int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
  719. wordcount_t num_words)
  720. {
  721. uECC_word_t mask = (uECC_word_t)-1;
  722. uECC_word_t tries;
  723. bitcount_t num_bits = uECC_vli_numBits(top, num_words);
  724. if (!g_rng_function) {
  725. return 0;
  726. }
  727. for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
  728. if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
  729. return 0;
  730. }
  731. random[num_words - 1] &=
  732. mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
  733. if (!uECC_vli_isZero(random, num_words) &&
  734. uECC_vli_cmp(top, random, num_words) == 1) {
  735. return 1;
  736. }
  737. }
  738. return 0;
  739. }
  740. int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve)
  741. {
  742. uECC_word_t tmp1[NUM_ECC_WORDS];
  743. uECC_word_t tmp2[NUM_ECC_WORDS];
  744. wordcount_t num_words = curve->num_words;
  745. /* The point at infinity is invalid. */
  746. if (EccPoint_isZero(point, curve)) {
  747. return -1;
  748. }
  749. /* x and y must be smaller than p. */
  750. if (uECC_vli_cmp_unsafe(curve->p, point, num_words) != 1 ||
  751. uECC_vli_cmp_unsafe(curve->p, point + num_words, num_words) != 1) {
  752. return -2;
  753. }
  754. uECC_vli_modSquare_fast(tmp1, point + num_words, curve);
  755. curve->x_side(tmp2, point, curve); /* tmp2 = x^3 + ax + b */
  756. /* Make sure that y^2 == x^3 + ax + b */
  757. if (uECC_vli_equal(tmp1, tmp2, num_words) != 0)
  758. return -3;
  759. return 0;
  760. }
  761. int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve)
  762. {
  763. uECC_word_t _public[NUM_ECC_WORDS * 2];
  764. uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
  765. uECC_vli_bytesToNative(
  766. _public + curve->num_words,
  767. public_key + curve->num_bytes,
  768. curve->num_bytes);
  769. if (uECC_vli_cmp_unsafe(_public, curve->G, NUM_ECC_WORDS * 2) == 0) {
  770. return -4;
  771. }
  772. return uECC_valid_point(_public, curve);
  773. }
  774. int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
  775. uECC_Curve curve)
  776. {
  777. uECC_word_t _private[NUM_ECC_WORDS];
  778. uECC_word_t _public[NUM_ECC_WORDS * 2];
  779. uECC_vli_bytesToNative(
  780. _private,
  781. private_key,
  782. BITS_TO_BYTES(curve->num_n_bits));
  783. /* Make sure the private key is in the range [1, n-1]. */
  784. if (uECC_vli_isZero(_private, BITS_TO_WORDS(curve->num_n_bits))) {
  785. return 0;
  786. }
  787. if (uECC_vli_cmp(curve->n, _private, BITS_TO_WORDS(curve->num_n_bits)) != 1) {
  788. return 0;
  789. }
  790. /* Compute public key. */
  791. if (!EccPoint_compute_public_key(_public, _private, curve)) {
  792. return 0;
  793. }
  794. uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public);
  795. uECC_vli_nativeToBytes(
  796. public_key +
  797. curve->num_bytes, curve->num_bytes, _public + curve->num_words);
  798. return 1;
  799. }