/security/nss/lib/freebl/ecl/ecp_aff.c

http://github.com/zpao/v8monkey · C · 357 lines · 243 code · 20 blank · 94 comment · 48 complexity · 7c0dea250bf1d02623ed5d23e7c34032 MD5 · raw file

  1. /*
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is the elliptic curve math library for prime field curves.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Sun Microsystems, Inc.
  19. * Portions created by the Initial Developer are Copyright (C) 2003
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Sheueling Chang-Shantz <sheueling.chang@sun.com>,
  24. * Stephen Fung <fungstep@hotmail.com>, and
  25. * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
  26. * Bodo Moeller <moeller@cdc.informatik.tu-darmstadt.de>,
  27. * Nils Larsch <nla@trustcenter.de>, and
  28. * Lenka Fibikova <fibikova@exp-math.uni-essen.de>, the OpenSSL Project
  29. *
  30. * Alternatively, the contents of this file may be used under the terms of
  31. * either the GNU General Public License Version 2 or later (the "GPL"), or
  32. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  33. * in which case the provisions of the GPL or the LGPL are applicable instead
  34. * of those above. If you wish to allow use of your version of this file only
  35. * under the terms of either the GPL or the LGPL, and not to allow others to
  36. * use your version of this file under the terms of the MPL, indicate your
  37. * decision by deleting the provisions above and replace them with the notice
  38. * and other provisions required by the GPL or the LGPL. If you do not delete
  39. * the provisions above, a recipient may use your version of this file under
  40. * the terms of any one of the MPL, the GPL or the LGPL.
  41. *
  42. * ***** END LICENSE BLOCK ***** */
  43. #include "ecp.h"
  44. #include "mplogic.h"
  45. #include <stdlib.h>
  46. /* Checks if point P(px, py) is at infinity. Uses affine coordinates. */
  47. mp_err
  48. ec_GFp_pt_is_inf_aff(const mp_int *px, const mp_int *py)
  49. {
  50. if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
  51. return MP_YES;
  52. } else {
  53. return MP_NO;
  54. }
  55. }
  56. /* Sets P(px, py) to be the point at infinity. Uses affine coordinates. */
  57. mp_err
  58. ec_GFp_pt_set_inf_aff(mp_int *px, mp_int *py)
  59. {
  60. mp_zero(px);
  61. mp_zero(py);
  62. return MP_OKAY;
  63. }
  64. /* Computes R = P + Q based on IEEE P1363 A.10.1. Elliptic curve points P,
  65. * Q, and R can all be identical. Uses affine coordinates. Assumes input
  66. * is already field-encoded using field_enc, and returns output that is
  67. * still field-encoded. */
  68. mp_err
  69. ec_GFp_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
  70. const mp_int *qy, mp_int *rx, mp_int *ry,
  71. const ECGroup *group)
  72. {
  73. mp_err res = MP_OKAY;
  74. mp_int lambda, temp, tempx, tempy;
  75. MP_DIGITS(&lambda) = 0;
  76. MP_DIGITS(&temp) = 0;
  77. MP_DIGITS(&tempx) = 0;
  78. MP_DIGITS(&tempy) = 0;
  79. MP_CHECKOK(mp_init(&lambda));
  80. MP_CHECKOK(mp_init(&temp));
  81. MP_CHECKOK(mp_init(&tempx));
  82. MP_CHECKOK(mp_init(&tempy));
  83. /* if P = inf, then R = Q */
  84. if (ec_GFp_pt_is_inf_aff(px, py) == 0) {
  85. MP_CHECKOK(mp_copy(qx, rx));
  86. MP_CHECKOK(mp_copy(qy, ry));
  87. res = MP_OKAY;
  88. goto CLEANUP;
  89. }
  90. /* if Q = inf, then R = P */
  91. if (ec_GFp_pt_is_inf_aff(qx, qy) == 0) {
  92. MP_CHECKOK(mp_copy(px, rx));
  93. MP_CHECKOK(mp_copy(py, ry));
  94. res = MP_OKAY;
  95. goto CLEANUP;
  96. }
  97. /* if px != qx, then lambda = (py-qy) / (px-qx) */
  98. if (mp_cmp(px, qx) != 0) {
  99. MP_CHECKOK(group->meth->field_sub(py, qy, &tempy, group->meth));
  100. MP_CHECKOK(group->meth->field_sub(px, qx, &tempx, group->meth));
  101. MP_CHECKOK(group->meth->
  102. field_div(&tempy, &tempx, &lambda, group->meth));
  103. } else {
  104. /* if py != qy or qy = 0, then R = inf */
  105. if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qy) == 0)) {
  106. mp_zero(rx);
  107. mp_zero(ry);
  108. res = MP_OKAY;
  109. goto CLEANUP;
  110. }
  111. /* lambda = (3qx^2+a) / (2qy) */
  112. MP_CHECKOK(group->meth->field_sqr(qx, &tempx, group->meth));
  113. MP_CHECKOK(mp_set_int(&temp, 3));
  114. if (group->meth->field_enc) {
  115. MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
  116. }
  117. MP_CHECKOK(group->meth->
  118. field_mul(&tempx, &temp, &tempx, group->meth));
  119. MP_CHECKOK(group->meth->
  120. field_add(&tempx, &group->curvea, &tempx, group->meth));
  121. MP_CHECKOK(mp_set_int(&temp, 2));
  122. if (group->meth->field_enc) {
  123. MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
  124. }
  125. MP_CHECKOK(group->meth->field_mul(qy, &temp, &tempy, group->meth));
  126. MP_CHECKOK(group->meth->
  127. field_div(&tempx, &tempy, &lambda, group->meth));
  128. }
  129. /* rx = lambda^2 - px - qx */
  130. MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
  131. MP_CHECKOK(group->meth->field_sub(&tempx, px, &tempx, group->meth));
  132. MP_CHECKOK(group->meth->field_sub(&tempx, qx, &tempx, group->meth));
  133. /* ry = (x1-x2) * lambda - y1 */
  134. MP_CHECKOK(group->meth->field_sub(qx, &tempx, &tempy, group->meth));
  135. MP_CHECKOK(group->meth->
  136. field_mul(&tempy, &lambda, &tempy, group->meth));
  137. MP_CHECKOK(group->meth->field_sub(&tempy, qy, &tempy, group->meth));
  138. MP_CHECKOK(mp_copy(&tempx, rx));
  139. MP_CHECKOK(mp_copy(&tempy, ry));
  140. CLEANUP:
  141. mp_clear(&lambda);
  142. mp_clear(&temp);
  143. mp_clear(&tempx);
  144. mp_clear(&tempy);
  145. return res;
  146. }
  147. /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
  148. * identical. Uses affine coordinates. Assumes input is already
  149. * field-encoded using field_enc, and returns output that is still
  150. * field-encoded. */
  151. mp_err
  152. ec_GFp_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
  153. const mp_int *qy, mp_int *rx, mp_int *ry,
  154. const ECGroup *group)
  155. {
  156. mp_err res = MP_OKAY;
  157. mp_int nqy;
  158. MP_DIGITS(&nqy) = 0;
  159. MP_CHECKOK(mp_init(&nqy));
  160. /* nqy = -qy */
  161. MP_CHECKOK(group->meth->field_neg(qy, &nqy, group->meth));
  162. res = group->point_add(px, py, qx, &nqy, rx, ry, group);
  163. CLEANUP:
  164. mp_clear(&nqy);
  165. return res;
  166. }
  167. /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
  168. * affine coordinates. Assumes input is already field-encoded using
  169. * field_enc, and returns output that is still field-encoded. */
  170. mp_err
  171. ec_GFp_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
  172. mp_int *ry, const ECGroup *group)
  173. {
  174. return ec_GFp_pt_add_aff(px, py, px, py, rx, ry, group);
  175. }
  176. /* by default, this routine is unused and thus doesn't need to be compiled */
  177. #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
  178. /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and
  179. * R can be identical. Uses affine coordinates. Assumes input is already
  180. * field-encoded using field_enc, and returns output that is still
  181. * field-encoded. */
  182. mp_err
  183. ec_GFp_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
  184. mp_int *rx, mp_int *ry, const ECGroup *group)
  185. {
  186. mp_err res = MP_OKAY;
  187. mp_int k, k3, qx, qy, sx, sy;
  188. int b1, b3, i, l;
  189. MP_DIGITS(&k) = 0;
  190. MP_DIGITS(&k3) = 0;
  191. MP_DIGITS(&qx) = 0;
  192. MP_DIGITS(&qy) = 0;
  193. MP_DIGITS(&sx) = 0;
  194. MP_DIGITS(&sy) = 0;
  195. MP_CHECKOK(mp_init(&k));
  196. MP_CHECKOK(mp_init(&k3));
  197. MP_CHECKOK(mp_init(&qx));
  198. MP_CHECKOK(mp_init(&qy));
  199. MP_CHECKOK(mp_init(&sx));
  200. MP_CHECKOK(mp_init(&sy));
  201. /* if n = 0 then r = inf */
  202. if (mp_cmp_z(n) == 0) {
  203. mp_zero(rx);
  204. mp_zero(ry);
  205. res = MP_OKAY;
  206. goto CLEANUP;
  207. }
  208. /* Q = P, k = n */
  209. MP_CHECKOK(mp_copy(px, &qx));
  210. MP_CHECKOK(mp_copy(py, &qy));
  211. MP_CHECKOK(mp_copy(n, &k));
  212. /* if n < 0 then Q = -Q, k = -k */
  213. if (mp_cmp_z(n) < 0) {
  214. MP_CHECKOK(group->meth->field_neg(&qy, &qy, group->meth));
  215. MP_CHECKOK(mp_neg(&k, &k));
  216. }
  217. #ifdef ECL_DEBUG /* basic double and add method */
  218. l = mpl_significant_bits(&k) - 1;
  219. MP_CHECKOK(mp_copy(&qx, &sx));
  220. MP_CHECKOK(mp_copy(&qy, &sy));
  221. for (i = l - 1; i >= 0; i--) {
  222. /* S = 2S */
  223. MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
  224. /* if k_i = 1, then S = S + Q */
  225. if (mpl_get_bit(&k, i) != 0) {
  226. MP_CHECKOK(group->
  227. point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
  228. }
  229. }
  230. #else /* double and add/subtract method from
  231. * standard */
  232. /* k3 = 3 * k */
  233. MP_CHECKOK(mp_set_int(&k3, 3));
  234. MP_CHECKOK(mp_mul(&k, &k3, &k3));
  235. /* S = Q */
  236. MP_CHECKOK(mp_copy(&qx, &sx));
  237. MP_CHECKOK(mp_copy(&qy, &sy));
  238. /* l = index of high order bit in binary representation of 3*k */
  239. l = mpl_significant_bits(&k3) - 1;
  240. /* for i = l-1 downto 1 */
  241. for (i = l - 1; i >= 1; i--) {
  242. /* S = 2S */
  243. MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
  244. b3 = MP_GET_BIT(&k3, i);
  245. b1 = MP_GET_BIT(&k, i);
  246. /* if k3_i = 1 and k_i = 0, then S = S + Q */
  247. if ((b3 == 1) && (b1 == 0)) {
  248. MP_CHECKOK(group->
  249. point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
  250. /* if k3_i = 0 and k_i = 1, then S = S - Q */
  251. } else if ((b3 == 0) && (b1 == 1)) {
  252. MP_CHECKOK(group->
  253. point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
  254. }
  255. }
  256. #endif
  257. /* output S */
  258. MP_CHECKOK(mp_copy(&sx, rx));
  259. MP_CHECKOK(mp_copy(&sy, ry));
  260. CLEANUP:
  261. mp_clear(&k);
  262. mp_clear(&k3);
  263. mp_clear(&qx);
  264. mp_clear(&qy);
  265. mp_clear(&sx);
  266. mp_clear(&sy);
  267. return res;
  268. }
  269. #endif
  270. /* Validates a point on a GFp curve. */
  271. mp_err
  272. ec_GFp_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
  273. {
  274. mp_err res = MP_NO;
  275. mp_int accl, accr, tmp, pxt, pyt;
  276. MP_DIGITS(&accl) = 0;
  277. MP_DIGITS(&accr) = 0;
  278. MP_DIGITS(&tmp) = 0;
  279. MP_DIGITS(&pxt) = 0;
  280. MP_DIGITS(&pyt) = 0;
  281. MP_CHECKOK(mp_init(&accl));
  282. MP_CHECKOK(mp_init(&accr));
  283. MP_CHECKOK(mp_init(&tmp));
  284. MP_CHECKOK(mp_init(&pxt));
  285. MP_CHECKOK(mp_init(&pyt));
  286. /* 1: Verify that publicValue is not the point at infinity */
  287. if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) {
  288. res = MP_NO;
  289. goto CLEANUP;
  290. }
  291. /* 2: Verify that the coordinates of publicValue are elements
  292. * of the field.
  293. */
  294. if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) ||
  295. (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
  296. res = MP_NO;
  297. goto CLEANUP;
  298. }
  299. /* 3: Verify that publicValue is on the curve. */
  300. if (group->meth->field_enc) {
  301. group->meth->field_enc(px, &pxt, group->meth);
  302. group->meth->field_enc(py, &pyt, group->meth);
  303. } else {
  304. mp_copy(px, &pxt);
  305. mp_copy(py, &pyt);
  306. }
  307. /* left-hand side: y^2 */
  308. MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
  309. /* right-hand side: x^3 + a*x + b */
  310. MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
  311. MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
  312. MP_CHECKOK( group->meth->field_mul(&group->curvea, &pxt, &tmp, group->meth) );
  313. MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
  314. MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
  315. /* check LHS - RHS == 0 */
  316. MP_CHECKOK( group->meth->field_sub(&accl, &accr, &accr, group->meth) );
  317. if (mp_cmp_z(&accr) != 0) {
  318. res = MP_NO;
  319. goto CLEANUP;
  320. }
  321. /* 4: Verify that the order of the curve times the publicValue
  322. * is the point at infinity.
  323. */
  324. MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
  325. if (ec_GFp_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
  326. res = MP_NO;
  327. goto CLEANUP;
  328. }
  329. res = MP_YES;
  330. CLEANUP:
  331. mp_clear(&accl);
  332. mp_clear(&accr);
  333. mp_clear(&tmp);
  334. mp_clear(&pxt);
  335. mp_clear(&pyt);
  336. return res;
  337. }