ecc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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_secp192r1(void)
  447. {
  448. return &curve_secp192r1;
  449. }
  450. void vli_mmod_fast_secp192r1(uint32_t *result, uint32_t *product) {
  451. uint32_t tmp[NUM_WORDS_SECP192R1];
  452. int carry;
  453. uECC_vli_set(result, product, NUM_WORDS_SECP192R1);
  454. uECC_vli_set(tmp, &product[6], NUM_WORDS_SECP192R1);
  455. carry = uECC_vli_add(result, result, tmp, NUM_WORDS_SECP192R1);
  456. tmp[0] = tmp[1] = 0;
  457. tmp[2] = product[6];
  458. tmp[3] = product[7];
  459. tmp[4] = product[8];
  460. tmp[5] = product[9];
  461. carry += uECC_vli_add(result, result, tmp, NUM_WORDS_SECP192R1);
  462. tmp[0] = tmp[2] = product[10];
  463. tmp[1] = tmp[3] = product[11];
  464. tmp[4] = tmp[5] = 0;
  465. carry += uECC_vli_add(result, result, tmp, NUM_WORDS_SECP192R1);
  466. while (carry || uECC_vli_cmp_unsafe(curve_secp192r1.p, result, NUM_WORDS_SECP192R1) != 1) {
  467. carry -= uECC_vli_sub(result, result, curve_secp192r1.p, NUM_WORDS_SECP192R1);
  468. }
  469. }
  470. uECC_Curve uECC_secp256r1(void)
  471. {
  472. return &curve_secp256r1;
  473. }
  474. void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product)
  475. {
  476. unsigned int tmp[NUM_ECC_WORDS];
  477. int carry;
  478. /* t */
  479. uECC_vli_set(result, product, NUM_ECC_WORDS);
  480. /* s1 */
  481. tmp[0] = tmp[1] = tmp[2] = 0;
  482. tmp[3] = product[11];
  483. tmp[4] = product[12];
  484. tmp[5] = product[13];
  485. tmp[6] = product[14];
  486. tmp[7] = product[15];
  487. carry = uECC_vli_add(tmp, tmp, tmp, NUM_ECC_WORDS);
  488. carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
  489. /* s2 */
  490. tmp[3] = product[12];
  491. tmp[4] = product[13];
  492. tmp[5] = product[14];
  493. tmp[6] = product[15];
  494. tmp[7] = 0;
  495. carry += uECC_vli_add(tmp, tmp, tmp, NUM_ECC_WORDS);
  496. carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
  497. /* s3 */
  498. tmp[0] = product[8];
  499. tmp[1] = product[9];
  500. tmp[2] = product[10];
  501. tmp[3] = tmp[4] = tmp[5] = 0;
  502. tmp[6] = product[14];
  503. tmp[7] = product[15];
  504. carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
  505. /* s4 */
  506. tmp[0] = product[9];
  507. tmp[1] = product[10];
  508. tmp[2] = product[11];
  509. tmp[3] = product[13];
  510. tmp[4] = product[14];
  511. tmp[5] = product[15];
  512. tmp[6] = product[13];
  513. tmp[7] = product[8];
  514. carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
  515. /* d1 */
  516. tmp[0] = product[11];
  517. tmp[1] = product[12];
  518. tmp[2] = product[13];
  519. tmp[3] = tmp[4] = tmp[5] = 0;
  520. tmp[6] = product[8];
  521. tmp[7] = product[10];
  522. carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
  523. /* d2 */
  524. tmp[0] = product[12];
  525. tmp[1] = product[13];
  526. tmp[2] = product[14];
  527. tmp[3] = product[15];
  528. tmp[4] = tmp[5] = 0;
  529. tmp[6] = product[9];
  530. tmp[7] = product[11];
  531. carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
  532. /* d3 */
  533. tmp[0] = product[13];
  534. tmp[1] = product[14];
  535. tmp[2] = product[15];
  536. tmp[3] = product[8];
  537. tmp[4] = product[9];
  538. tmp[5] = product[10];
  539. tmp[6] = 0;
  540. tmp[7] = product[12];
  541. carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
  542. /* d4 */
  543. tmp[0] = product[14];
  544. tmp[1] = product[15];
  545. tmp[2] = 0;
  546. tmp[3] = product[9];
  547. tmp[4] = product[10];
  548. tmp[5] = product[11];
  549. tmp[6] = 0;
  550. tmp[7] = product[13];
  551. carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
  552. if (carry < 0) {
  553. do {
  554. carry += uECC_vli_add(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
  555. }
  556. while (carry < 0);
  557. } else {
  558. while (carry ||
  559. uECC_vli_cmp_unsafe(curve_secp256r1.p, result, NUM_ECC_WORDS) != 1) {
  560. carry -= uECC_vli_sub(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
  561. }
  562. }
  563. }
  564. uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve)
  565. {
  566. return uECC_vli_isZero(point, curve->num_words * 2);
  567. }
  568. void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z,
  569. uECC_Curve curve)
  570. {
  571. uECC_word_t t1[NUM_ECC_WORDS];
  572. uECC_vli_modSquare_fast(t1, Z, curve); /* z^2 */
  573. uECC_vli_modMult_fast(X1, X1, t1, curve); /* x1 * z^2 */
  574. uECC_vli_modMult_fast(t1, t1, Z, curve); /* z^3 */
  575. uECC_vli_modMult_fast(Y1, Y1, t1, curve); /* y1 * z^3 */
  576. }
  577. /* P = (x1, y1) => 2P, (x2, y2) => P' */
  578. static void XYcZ_initial_double(uECC_word_t * X1, uECC_word_t * Y1,
  579. uECC_word_t * X2, uECC_word_t * Y2,
  580. const uECC_word_t * const initial_Z,
  581. uECC_Curve curve)
  582. {
  583. uECC_word_t z[NUM_ECC_WORDS];
  584. wordcount_t num_words = curve->num_words;
  585. if (initial_Z) {
  586. uECC_vli_set(z, initial_Z, num_words);
  587. } else {
  588. uECC_vli_clear(z, num_words);
  589. z[0] = 1;
  590. }
  591. uECC_vli_set(X2, X1, num_words);
  592. uECC_vli_set(Y2, Y1, num_words);
  593. apply_z(X1, Y1, z, curve);
  594. curve->double_jacobian(X1, Y1, z, curve);
  595. apply_z(X2, Y2, z, curve);
  596. }
  597. void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1,
  598. uECC_word_t * X2, uECC_word_t * Y2,
  599. uECC_Curve curve)
  600. {
  601. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  602. uECC_word_t t5[NUM_ECC_WORDS];
  603. wordcount_t num_words = curve->num_words;
  604. uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
  605. uECC_vli_modSquare_fast(t5, t5, curve); /* t5 = (x2 - x1)^2 = A */
  606. uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = x1*A = B */
  607. uECC_vli_modMult_fast(X2, X2, t5, curve); /* t3 = x2*A = C */
  608. uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
  609. uECC_vli_modSquare_fast(t5, Y2, curve); /* t5 = (y2 - y1)^2 = D */
  610. uECC_vli_modSub(t5, t5, X1, curve->p, num_words); /* t5 = D - B */
  611. uECC_vli_modSub(t5, t5, X2, curve->p, num_words); /* t5 = D - B - C = x3 */
  612. uECC_vli_modSub(X2, X2, X1, curve->p, num_words); /* t3 = C - B */
  613. uECC_vli_modMult_fast(Y1, Y1, X2, curve); /* t2 = y1*(C - B) */
  614. uECC_vli_modSub(X2, X1, t5, curve->p, num_words); /* t3 = B - x3 */
  615. uECC_vli_modMult_fast(Y2, Y2, X2, curve); /* t4 = (y2 - y1)*(B - x3) */
  616. uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y3 */
  617. uECC_vli_set(X2, t5, num_words);
  618. }
  619. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  620. Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
  621. or P => P - Q, Q => P + Q
  622. */
  623. static void XYcZ_addC(uECC_word_t * X1, uECC_word_t * Y1,
  624. uECC_word_t * X2, uECC_word_t * Y2,
  625. uECC_Curve curve)
  626. {
  627. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  628. uECC_word_t t5[NUM_ECC_WORDS];
  629. uECC_word_t t6[NUM_ECC_WORDS];
  630. uECC_word_t t7[NUM_ECC_WORDS];
  631. wordcount_t num_words = curve->num_words;
  632. uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
  633. uECC_vli_modSquare_fast(t5, t5, curve); /* t5 = (x2 - x1)^2 = A */
  634. uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = x1*A = B */
  635. uECC_vli_modMult_fast(X2, X2, t5, curve); /* t3 = x2*A = C */
  636. uECC_vli_modAdd(t5, Y2, Y1, curve->p, num_words); /* t5 = y2 + y1 */
  637. uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
  638. uECC_vli_modSub(t6, X2, X1, curve->p, num_words); /* t6 = C - B */
  639. uECC_vli_modMult_fast(Y1, Y1, t6, curve); /* t2 = y1 * (C - B) = E */
  640. uECC_vli_modAdd(t6, X1, X2, curve->p, num_words); /* t6 = B + C */
  641. uECC_vli_modSquare_fast(X2, Y2, curve); /* t3 = (y2 - y1)^2 = D */
  642. uECC_vli_modSub(X2, X2, t6, curve->p, num_words); /* t3 = D - (B + C) = x3 */
  643. uECC_vli_modSub(t7, X1, X2, curve->p, num_words); /* t7 = B - x3 */
  644. uECC_vli_modMult_fast(Y2, Y2, t7, curve); /* t4 = (y2 - y1)*(B - x3) */
  645. /* t4 = (y2 - y1)*(B - x3) - E = y3: */
  646. uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words);
  647. uECC_vli_modSquare_fast(t7, t5, curve); /* t7 = (y2 + y1)^2 = F */
  648. uECC_vli_modSub(t7, t7, t6, curve->p, num_words); /* t7 = F - (B + C) = x3' */
  649. uECC_vli_modSub(t6, t7, X1, curve->p, num_words); /* t6 = x3' - B */
  650. uECC_vli_modMult_fast(t6, t6, t5, curve); /* t6 = (y2+y1)*(x3' - B) */
  651. /* t2 = (y2+y1)*(x3' - B) - E = y3': */
  652. uECC_vli_modSub(Y1, t6, Y1, curve->p, num_words);
  653. uECC_vli_set(X1, t7, num_words);
  654. }
  655. void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
  656. const uECC_word_t * scalar,
  657. const uECC_word_t * initial_Z,
  658. bitcount_t num_bits, uECC_Curve curve)
  659. {
  660. /* R0 and R1 */
  661. uECC_word_t Rx[2][NUM_ECC_WORDS];
  662. uECC_word_t Ry[2][NUM_ECC_WORDS];
  663. uECC_word_t z[NUM_ECC_WORDS];
  664. bitcount_t i;
  665. uECC_word_t nb;
  666. wordcount_t num_words = curve->num_words;
  667. uECC_vli_set(Rx[1], point, num_words);
  668. uECC_vli_set(Ry[1], point + num_words, num_words);
  669. XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve);
  670. for (i = num_bits - 2; i > 0; --i) {
  671. nb = !uECC_vli_testBit(scalar, i);
  672. XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve);
  673. XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve);
  674. }
  675. nb = !uECC_vli_testBit(scalar, 0);
  676. XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve);
  677. /* Find final 1/Z value. */
  678. uECC_vli_modSub(z, Rx[1], Rx[0], curve->p, num_words); /* X1 - X0 */
  679. uECC_vli_modMult_fast(z, z, Ry[1 - nb], curve); /* Yb * (X1 - X0) */
  680. uECC_vli_modMult_fast(z, z, point, curve); /* xP * Yb * (X1 - X0) */
  681. uECC_vli_modInv(z, z, curve->p, num_words); /* 1 / (xP * Yb * (X1 - X0))*/
  682. /* yP / (xP * Yb * (X1 - X0)) */
  683. uECC_vli_modMult_fast(z, z, point + num_words, curve);
  684. /* Xb * yP / (xP * Yb * (X1 - X0)) */
  685. uECC_vli_modMult_fast(z, z, Rx[1 - nb], curve);
  686. /* End 1/Z calculation */
  687. XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve);
  688. apply_z(Rx[0], Ry[0], z, curve);
  689. uECC_vli_set(result, Rx[0], num_words);
  690. uECC_vli_set(result + num_words, Ry[0], num_words);
  691. }
  692. uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
  693. uECC_word_t *k1, uECC_Curve curve)
  694. {
  695. wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
  696. bitcount_t num_n_bits = curve->num_n_bits;
  697. uECC_word_t carry = uECC_vli_add(k0, k, curve->n, num_n_words) ||
  698. (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
  699. uECC_vli_testBit(k0, num_n_bits));
  700. uECC_vli_add(k1, k0, curve->n, num_n_words);
  701. return carry;
  702. }
  703. uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
  704. uECC_word_t *private_key,
  705. uECC_Curve curve)
  706. {
  707. uECC_word_t tmp1[NUM_ECC_WORDS];
  708. uECC_word_t tmp2[NUM_ECC_WORDS];
  709. uECC_word_t *p2[2] = {tmp1, tmp2};
  710. uECC_word_t carry;
  711. /* Regularize the bitcount for the private key so that attackers cannot
  712. * use a side channel attack to learn the number of leading zeros. */
  713. carry = regularize_k(private_key, tmp1, tmp2, curve);
  714. EccPoint_mult(result, curve->G, p2[!carry], 0, curve->num_n_bits + 1, curve);
  715. if (EccPoint_isZero(result, curve)) {
  716. return 0;
  717. }
  718. return 1;
  719. }
  720. /* Converts an integer in uECC native format to big-endian bytes. */
  721. void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
  722. const unsigned int *native)
  723. {
  724. wordcount_t i;
  725. for (i = 0; i < num_bytes; ++i) {
  726. unsigned b = num_bytes - 1 - i;
  727. bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE));
  728. }
  729. }
  730. /* Converts big-endian bytes to an integer in uECC native format. */
  731. void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
  732. int num_bytes)
  733. {
  734. wordcount_t i;
  735. uECC_vli_clear(native, (num_bytes + (uECC_WORD_SIZE - 1)) / uECC_WORD_SIZE);
  736. for (i = 0; i < num_bytes; ++i) {
  737. unsigned b = num_bytes - 1 - i;
  738. native[b / uECC_WORD_SIZE] |=
  739. (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE));
  740. }
  741. }
  742. int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
  743. wordcount_t num_words)
  744. {
  745. uECC_word_t mask = (uECC_word_t)-1;
  746. uECC_word_t tries;
  747. bitcount_t num_bits = uECC_vli_numBits(top, num_words);
  748. if (!g_rng_function) {
  749. return 0;
  750. }
  751. for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
  752. if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
  753. return 0;
  754. }
  755. random[num_words - 1] &=
  756. mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
  757. if (!uECC_vli_isZero(random, num_words) &&
  758. uECC_vli_cmp(top, random, num_words) == 1) {
  759. return 1;
  760. }
  761. }
  762. return 0;
  763. }
  764. int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve)
  765. {
  766. uECC_word_t tmp1[NUM_ECC_WORDS];
  767. uECC_word_t tmp2[NUM_ECC_WORDS];
  768. wordcount_t num_words = curve->num_words;
  769. /* The point at infinity is invalid. */
  770. if (EccPoint_isZero(point, curve)) {
  771. return -1;
  772. }
  773. /* x and y must be smaller than p. */
  774. if (uECC_vli_cmp_unsafe(curve->p, point, num_words) != 1 ||
  775. uECC_vli_cmp_unsafe(curve->p, point + num_words, num_words) != 1) {
  776. return -2;
  777. }
  778. uECC_vli_modSquare_fast(tmp1, point + num_words, curve);
  779. curve->x_side(tmp2, point, curve); /* tmp2 = x^3 + ax + b */
  780. /* Make sure that y^2 == x^3 + ax + b */
  781. if (uECC_vli_equal(tmp1, tmp2, num_words) != 0)
  782. return -3;
  783. return 0;
  784. }
  785. int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve)
  786. {
  787. uECC_word_t _public[NUM_ECC_WORDS * 2];
  788. uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
  789. uECC_vli_bytesToNative(
  790. _public + curve->num_words,
  791. public_key + curve->num_bytes,
  792. curve->num_bytes);
  793. if (uECC_vli_cmp_unsafe(_public, curve->G, NUM_ECC_WORDS * 2) == 0) {
  794. return -4;
  795. }
  796. return uECC_valid_point(_public, curve);
  797. }
  798. int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
  799. uECC_Curve curve)
  800. {
  801. uECC_word_t _private[NUM_ECC_WORDS];
  802. uECC_word_t _public[NUM_ECC_WORDS * 2];
  803. uECC_vli_bytesToNative(
  804. _private,
  805. private_key,
  806. BITS_TO_BYTES(curve->num_n_bits));
  807. /* Make sure the private key is in the range [1, n-1]. */
  808. if (uECC_vli_isZero(_private, BITS_TO_WORDS(curve->num_n_bits))) {
  809. return 0;
  810. }
  811. if (uECC_vli_cmp(curve->n, _private, BITS_TO_WORDS(curve->num_n_bits)) != 1) {
  812. return 0;
  813. }
  814. /* Compute public key. */
  815. if (!EccPoint_compute_public_key(_public, _private, curve)) {
  816. return 0;
  817. }
  818. uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public);
  819. uECC_vli_nativeToBytes(
  820. public_key +
  821. curve->num_bytes, curve->num_bytes, _public + curve->num_words);
  822. return 1;
  823. }