p256-x86_64.c 14 KB

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