p256-x86_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* Copyright (c) 2014, Intel Corporation.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. // Developers and authors:
  15. // Shay Gueron (1, 2), and Vlad Krasnov (1)
  16. // (1) Intel Corporation, Israel Development Center
  17. // (2) University of Haifa
  18. // Reference:
  19. // S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with
  20. // 256 Bit Primes"
  21. #include <openssl/ec.h>
  22. #include <assert.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <openssl/bn.h>
  26. #include <openssl/crypto.h>
  27. #include <openssl/err.h>
  28. #include "../bn/internal.h"
  29. #include "../delocate.h"
  30. #include "../../internal.h"
  31. #include "internal.h"
  32. #include "p256-x86_64.h"
  33. #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
  34. !defined(OPENSSL_SMALL)
  35. typedef P256_POINT_AFFINE PRECOMP256_ROW[64];
  36. // One converted into the Montgomery domain
  37. static const BN_ULONG ONE[P256_LIMBS] = {
  38. TOBN(0x00000000, 0x00000001), TOBN(0xffffffff, 0x00000000),
  39. TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0xfffffffe),
  40. };
  41. // Precomputed tables for the default generator
  42. #include "p256-x86_64-table.h"
  43. // Recode window to a signed digit, see util-64.c for details
  44. static unsigned booth_recode_w5(unsigned in) {
  45. unsigned s, d;
  46. s = ~((in >> 5) - 1);
  47. d = (1 << 6) - in - 1;
  48. d = (d & s) | (in & ~s);
  49. d = (d >> 1) + (d & 1);
  50. return (d << 1) + (s & 1);
  51. }
  52. static unsigned booth_recode_w7(unsigned in) {
  53. unsigned s, d;
  54. s = ~((in >> 7) - 1);
  55. d = (1 << 8) - in - 1;
  56. d = (d & s) | (in & ~s);
  57. d = (d >> 1) + (d & 1);
  58. return (d << 1) + (s & 1);
  59. }
  60. // copy_conditional copies |src| to |dst| if |move| is one and leaves it as-is
  61. // if |move| is zero.
  62. //
  63. // WARNING: this breaks the usual convention of constant-time functions
  64. // returning masks.
  65. static void copy_conditional(BN_ULONG dst[P256_LIMBS],
  66. const BN_ULONG src[P256_LIMBS], BN_ULONG move) {
  67. BN_ULONG mask1 = ((BN_ULONG)0) - move;
  68. BN_ULONG mask2 = ~mask1;
  69. dst[0] = (src[0] & mask1) ^ (dst[0] & mask2);
  70. dst[1] = (src[1] & mask1) ^ (dst[1] & mask2);
  71. dst[2] = (src[2] & mask1) ^ (dst[2] & mask2);
  72. dst[3] = (src[3] & mask1) ^ (dst[3] & mask2);
  73. if (P256_LIMBS == 8) {
  74. dst[4] = (src[4] & mask1) ^ (dst[4] & mask2);
  75. dst[5] = (src[5] & mask1) ^ (dst[5] & mask2);
  76. dst[6] = (src[6] & mask1) ^ (dst[6] & mask2);
  77. dst[7] = (src[7] & mask1) ^ (dst[7] & mask2);
  78. }
  79. }
  80. // is_not_zero returns one iff in != 0 and zero otherwise.
  81. //
  82. // WARNING: this breaks the usual convention of constant-time functions
  83. // returning masks.
  84. //
  85. // (define-fun is_not_zero ((in (_ BitVec 64))) (_ BitVec 64)
  86. // (bvlshr (bvor in (bvsub #x0000000000000000 in)) #x000000000000003f)
  87. // )
  88. //
  89. // (declare-fun x () (_ BitVec 64))
  90. //
  91. // (assert (and (= x #x0000000000000000) (= (is_not_zero x) #x0000000000000001)))
  92. // (check-sat)
  93. //
  94. // (assert (and (not (= x #x0000000000000000)) (= (is_not_zero x) #x0000000000000000)))
  95. // (check-sat)
  96. //
  97. static BN_ULONG is_not_zero(BN_ULONG in) {
  98. in |= (0 - in);
  99. in >>= BN_BITS2 - 1;
  100. return in;
  101. }
  102. // ecp_nistz256_mod_inverse_mont sets |r| to (|in| * 2^-256)^-1 * 2^256 mod p.
  103. // That is, |r| is the modular inverse of |in| for input and output in the
  104. // Montgomery domain.
  105. static void ecp_nistz256_mod_inverse_mont(BN_ULONG r[P256_LIMBS],
  106. const BN_ULONG in[P256_LIMBS]) {
  107. /* The poly is ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff
  108. ffffffff
  109. We use FLT and used poly-2 as exponent */
  110. BN_ULONG p2[P256_LIMBS];
  111. BN_ULONG p4[P256_LIMBS];
  112. BN_ULONG p8[P256_LIMBS];
  113. BN_ULONG p16[P256_LIMBS];
  114. BN_ULONG p32[P256_LIMBS];
  115. BN_ULONG res[P256_LIMBS];
  116. int i;
  117. ecp_nistz256_sqr_mont(res, in);
  118. ecp_nistz256_mul_mont(p2, res, in); // 3*p
  119. ecp_nistz256_sqr_mont(res, p2);
  120. ecp_nistz256_sqr_mont(res, res);
  121. ecp_nistz256_mul_mont(p4, res, p2); // f*p
  122. ecp_nistz256_sqr_mont(res, p4);
  123. ecp_nistz256_sqr_mont(res, res);
  124. ecp_nistz256_sqr_mont(res, res);
  125. ecp_nistz256_sqr_mont(res, res);
  126. ecp_nistz256_mul_mont(p8, res, p4); // ff*p
  127. ecp_nistz256_sqr_mont(res, p8);
  128. for (i = 0; i < 7; i++) {
  129. ecp_nistz256_sqr_mont(res, res);
  130. }
  131. ecp_nistz256_mul_mont(p16, res, p8); // ffff*p
  132. ecp_nistz256_sqr_mont(res, p16);
  133. for (i = 0; i < 15; i++) {
  134. ecp_nistz256_sqr_mont(res, res);
  135. }
  136. ecp_nistz256_mul_mont(p32, res, p16); // ffffffff*p
  137. ecp_nistz256_sqr_mont(res, p32);
  138. for (i = 0; i < 31; i++) {
  139. ecp_nistz256_sqr_mont(res, res);
  140. }
  141. ecp_nistz256_mul_mont(res, res, in);
  142. for (i = 0; i < 32 * 4; i++) {
  143. ecp_nistz256_sqr_mont(res, res);
  144. }
  145. ecp_nistz256_mul_mont(res, res, p32);
  146. for (i = 0; i < 32; i++) {
  147. ecp_nistz256_sqr_mont(res, res);
  148. }
  149. ecp_nistz256_mul_mont(res, res, p32);
  150. for (i = 0; i < 16; i++) {
  151. ecp_nistz256_sqr_mont(res, res);
  152. }
  153. ecp_nistz256_mul_mont(res, res, p16);
  154. for (i = 0; i < 8; i++) {
  155. ecp_nistz256_sqr_mont(res, res);
  156. }
  157. ecp_nistz256_mul_mont(res, res, p8);
  158. ecp_nistz256_sqr_mont(res, res);
  159. ecp_nistz256_sqr_mont(res, res);
  160. ecp_nistz256_sqr_mont(res, res);
  161. ecp_nistz256_sqr_mont(res, res);
  162. ecp_nistz256_mul_mont(res, res, p4);
  163. ecp_nistz256_sqr_mont(res, res);
  164. ecp_nistz256_sqr_mont(res, res);
  165. ecp_nistz256_mul_mont(res, res, p2);
  166. ecp_nistz256_sqr_mont(res, res);
  167. ecp_nistz256_sqr_mont(res, res);
  168. ecp_nistz256_mul_mont(r, res, in);
  169. }
  170. // ecp_nistz256_bignum_to_field_elem copies the contents of |in| to |out| and
  171. // returns one if it fits. Otherwise it returns zero.
  172. static int ecp_nistz256_bignum_to_field_elem(BN_ULONG out[P256_LIMBS],
  173. const BIGNUM *in) {
  174. if (in->top > P256_LIMBS) {
  175. return 0;
  176. }
  177. OPENSSL_memset(out, 0, sizeof(BN_ULONG) * P256_LIMBS);
  178. OPENSSL_memcpy(out, in->d, sizeof(BN_ULONG) * in->top);
  179. return 1;
  180. }
  181. // r = p * p_scalar
  182. static int ecp_nistz256_windowed_mul(const EC_GROUP *group, P256_POINT *r,
  183. const EC_POINT *p,
  184. const EC_SCALAR *p_scalar) {
  185. assert(p != NULL);
  186. assert(p_scalar != NULL);
  187. static const unsigned kWindowSize = 5;
  188. static const unsigned kMask = (1 << (5 /* kWindowSize */ + 1)) - 1;
  189. // A |P256_POINT| is (3 * 32) = 96 bytes, and the 64-byte alignment should
  190. // add no more than 63 bytes of overhead. Thus, |table| should require
  191. // ~1599 ((96 * 16) + 63) bytes of stack space.
  192. alignas(64) P256_POINT table[16];
  193. uint8_t p_str[33];
  194. OPENSSL_memcpy(p_str, p_scalar->bytes, 32);
  195. p_str[32] = 0;
  196. // table[0] is implicitly (0,0,0) (the point at infinity), therefore it is
  197. // not stored. All other values are actually stored with an offset of -1 in
  198. // table.
  199. P256_POINT *row = table;
  200. if (!ecp_nistz256_bignum_to_field_elem(row[1 - 1].X, &p->X) ||
  201. !ecp_nistz256_bignum_to_field_elem(row[1 - 1].Y, &p->Y) ||
  202. !ecp_nistz256_bignum_to_field_elem(row[1 - 1].Z, &p->Z)) {
  203. OPENSSL_PUT_ERROR(EC, EC_R_COORDINATES_OUT_OF_RANGE);
  204. return 0;
  205. }
  206. ecp_nistz256_point_double(&row[2 - 1], &row[1 - 1]);
  207. ecp_nistz256_point_add(&row[3 - 1], &row[2 - 1], &row[1 - 1]);
  208. ecp_nistz256_point_double(&row[4 - 1], &row[2 - 1]);
  209. ecp_nistz256_point_double(&row[6 - 1], &row[3 - 1]);
  210. ecp_nistz256_point_double(&row[8 - 1], &row[4 - 1]);
  211. ecp_nistz256_point_double(&row[12 - 1], &row[6 - 1]);
  212. ecp_nistz256_point_add(&row[5 - 1], &row[4 - 1], &row[1 - 1]);
  213. ecp_nistz256_point_add(&row[7 - 1], &row[6 - 1], &row[1 - 1]);
  214. ecp_nistz256_point_add(&row[9 - 1], &row[8 - 1], &row[1 - 1]);
  215. ecp_nistz256_point_add(&row[13 - 1], &row[12 - 1], &row[1 - 1]);
  216. ecp_nistz256_point_double(&row[14 - 1], &row[7 - 1]);
  217. ecp_nistz256_point_double(&row[10 - 1], &row[5 - 1]);
  218. ecp_nistz256_point_add(&row[15 - 1], &row[14 - 1], &row[1 - 1]);
  219. ecp_nistz256_point_add(&row[11 - 1], &row[10 - 1], &row[1 - 1]);
  220. ecp_nistz256_point_double(&row[16 - 1], &row[8 - 1]);
  221. BN_ULONG tmp[P256_LIMBS];
  222. alignas(32) P256_POINT h;
  223. unsigned index = 255;
  224. unsigned wvalue = p_str[(index - 1) / 8];
  225. wvalue = (wvalue >> ((index - 1) % 8)) & kMask;
  226. ecp_nistz256_select_w5(r, table, booth_recode_w5(wvalue) >> 1);
  227. while (index >= 5) {
  228. if (index != 255) {
  229. unsigned off = (index - 1) / 8;
  230. wvalue = p_str[off] | p_str[off + 1] << 8;
  231. wvalue = (wvalue >> ((index - 1) % 8)) & kMask;
  232. wvalue = booth_recode_w5(wvalue);
  233. ecp_nistz256_select_w5(&h, table, wvalue >> 1);
  234. ecp_nistz256_neg(tmp, h.Y);
  235. copy_conditional(h.Y, tmp, (wvalue & 1));
  236. ecp_nistz256_point_add(r, r, &h);
  237. }
  238. index -= kWindowSize;
  239. ecp_nistz256_point_double(r, r);
  240. ecp_nistz256_point_double(r, r);
  241. ecp_nistz256_point_double(r, r);
  242. ecp_nistz256_point_double(r, r);
  243. ecp_nistz256_point_double(r, r);
  244. }
  245. // Final window
  246. wvalue = p_str[0];
  247. wvalue = (wvalue << 1) & kMask;
  248. wvalue = booth_recode_w5(wvalue);
  249. ecp_nistz256_select_w5(&h, table, wvalue >> 1);
  250. ecp_nistz256_neg(tmp, h.Y);
  251. copy_conditional(h.Y, tmp, wvalue & 1);
  252. ecp_nistz256_point_add(r, r, &h);
  253. return 1;
  254. }
  255. static int ecp_nistz256_points_mul(const EC_GROUP *group, EC_POINT *r,
  256. const EC_SCALAR *g_scalar,
  257. const EC_POINT *p_,
  258. const EC_SCALAR *p_scalar, BN_CTX *ctx) {
  259. assert((p_ != NULL) == (p_scalar != NULL));
  260. static const unsigned kWindowSize = 7;
  261. static const unsigned kMask = (1 << (7 /* kWindowSize */ + 1)) - 1;
  262. alignas(32) union {
  263. P256_POINT p;
  264. P256_POINT_AFFINE a;
  265. } t, p;
  266. if (g_scalar != NULL) {
  267. uint8_t p_str[33];
  268. OPENSSL_memcpy(p_str, g_scalar->bytes, 32);
  269. p_str[32] = 0;
  270. // First window
  271. unsigned wvalue = (p_str[0] << 1) & kMask;
  272. unsigned index = kWindowSize;
  273. wvalue = booth_recode_w7(wvalue);
  274. const PRECOMP256_ROW *const precomputed_table =
  275. (const PRECOMP256_ROW *)ecp_nistz256_precomputed;
  276. ecp_nistz256_select_w7(&p.a, precomputed_table[0], wvalue >> 1);
  277. ecp_nistz256_neg(p.p.Z, p.p.Y);
  278. copy_conditional(p.p.Y, p.p.Z, wvalue & 1);
  279. // Convert |p| from affine to Jacobian coordinates. We set Z to zero if |p|
  280. // is infinity and |ONE| otherwise. |p| was computed from the table, so it
  281. // is infinity iff |wvalue >> 1| is zero.
  282. OPENSSL_memset(p.p.Z, 0, sizeof(p.p.Z));
  283. copy_conditional(p.p.Z, ONE, is_not_zero(wvalue >> 1));
  284. for (int i = 1; i < 37; i++) {
  285. unsigned off = (index - 1) / 8;
  286. wvalue = p_str[off] | p_str[off + 1] << 8;
  287. wvalue = (wvalue >> ((index - 1) % 8)) & kMask;
  288. index += kWindowSize;
  289. wvalue = booth_recode_w7(wvalue);
  290. ecp_nistz256_select_w7(&t.a, precomputed_table[i], wvalue >> 1);
  291. ecp_nistz256_neg(t.p.Z, t.a.Y);
  292. copy_conditional(t.a.Y, t.p.Z, wvalue & 1);
  293. ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);
  294. }
  295. }
  296. const int p_is_infinity = g_scalar == NULL;
  297. if (p_scalar != NULL) {
  298. P256_POINT *out = &t.p;
  299. if (p_is_infinity) {
  300. out = &p.p;
  301. }
  302. if (!ecp_nistz256_windowed_mul(group, out, p_, p_scalar)) {
  303. return 0;
  304. }
  305. if (!p_is_infinity) {
  306. ecp_nistz256_point_add(&p.p, &p.p, out);
  307. }
  308. }
  309. // Not constant-time, but we're only operating on the public output.
  310. if (!bn_set_words(&r->X, p.p.X, P256_LIMBS) ||
  311. !bn_set_words(&r->Y, p.p.Y, P256_LIMBS) ||
  312. !bn_set_words(&r->Z, p.p.Z, P256_LIMBS)) {
  313. return 0;
  314. }
  315. return 1;
  316. }
  317. static int ecp_nistz256_get_affine(const EC_GROUP *group, const EC_POINT *point,
  318. BIGNUM *x, BIGNUM *y, BN_CTX *ctx) {
  319. BN_ULONG z_inv2[P256_LIMBS];
  320. BN_ULONG z_inv3[P256_LIMBS];
  321. BN_ULONG point_x[P256_LIMBS], point_y[P256_LIMBS], point_z[P256_LIMBS];
  322. if (EC_POINT_is_at_infinity(group, point)) {
  323. OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
  324. return 0;
  325. }
  326. if (!ecp_nistz256_bignum_to_field_elem(point_x, &point->X) ||
  327. !ecp_nistz256_bignum_to_field_elem(point_y, &point->Y) ||
  328. !ecp_nistz256_bignum_to_field_elem(point_z, &point->Z)) {
  329. OPENSSL_PUT_ERROR(EC, EC_R_COORDINATES_OUT_OF_RANGE);
  330. return 0;
  331. }
  332. ecp_nistz256_mod_inverse_mont(z_inv3, point_z);
  333. ecp_nistz256_sqr_mont(z_inv2, z_inv3);
  334. // Instead of using |ecp_nistz256_from_mont| to convert the |x| coordinate
  335. // and then calling |ecp_nistz256_from_mont| again to convert the |y|
  336. // coordinate below, convert the common factor |z_inv2| once now, saving one
  337. // reduction.
  338. ecp_nistz256_from_mont(z_inv2, z_inv2);
  339. if (x != NULL) {
  340. BN_ULONG x_aff[P256_LIMBS];
  341. ecp_nistz256_mul_mont(x_aff, z_inv2, point_x);
  342. if (!bn_set_words(x, x_aff, P256_LIMBS)) {
  343. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  344. return 0;
  345. }
  346. }
  347. if (y != NULL) {
  348. BN_ULONG y_aff[P256_LIMBS];
  349. ecp_nistz256_mul_mont(z_inv3, z_inv3, z_inv2);
  350. ecp_nistz256_mul_mont(y_aff, z_inv3, point_y);
  351. if (!bn_set_words(y, y_aff, P256_LIMBS)) {
  352. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  353. return 0;
  354. }
  355. }
  356. return 1;
  357. }
  358. DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistz256_method) {
  359. out->group_init = ec_GFp_mont_group_init;
  360. out->group_finish = ec_GFp_mont_group_finish;
  361. out->group_set_curve = ec_GFp_mont_group_set_curve;
  362. out->point_get_affine_coordinates = ecp_nistz256_get_affine;
  363. out->mul = ecp_nistz256_points_mul;
  364. out->field_mul = ec_GFp_mont_field_mul;
  365. out->field_sqr = ec_GFp_mont_field_sqr;
  366. out->field_encode = ec_GFp_mont_field_encode;
  367. out->field_decode = ec_GFp_mont_field_decode;
  368. };
  369. #endif /* !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
  370. !defined(OPENSSL_SMALL) */