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