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