Back to index

lightning-sunbird  0.9+nobinonly
ecp_aff.c
Go to the documentation of this file.
00001 /* 
00002  * ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the elliptic curve math library for prime field curves.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Sun Microsystems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Sheueling Chang-Shantz <sheueling.chang@sun.com>,
00024  *   Stephen Fung <fungstep@hotmail.com>, and
00025  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
00026  *   Bodo Moeller <moeller@cdc.informatik.tu-darmstadt.de>,
00027  *   Nils Larsch <nla@trustcenter.de>, and
00028  *   Lenka Fibikova <fibikova@exp-math.uni-essen.de>, the OpenSSL Project
00029  *
00030  * Alternatively, the contents of this file may be used under the terms of
00031  * either the GNU General Public License Version 2 or later (the "GPL"), or
00032  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00033  * in which case the provisions of the GPL or the LGPL are applicable instead
00034  * of those above. If you wish to allow use of your version of this file only
00035  * under the terms of either the GPL or the LGPL, and not to allow others to
00036  * use your version of this file under the terms of the MPL, indicate your
00037  * decision by deleting the provisions above and replace them with the notice
00038  * and other provisions required by the GPL or the LGPL. If you do not delete
00039  * the provisions above, a recipient may use your version of this file under
00040  * the terms of any one of the MPL, the GPL or the LGPL.
00041  *
00042  * ***** END LICENSE BLOCK ***** */
00043 
00044 #include "ecp.h"
00045 #include "mplogic.h"
00046 #include <stdlib.h>
00047 
00048 /* Checks if point P(px, py) is at infinity.  Uses affine coordinates. */
00049 mp_err
00050 ec_GFp_pt_is_inf_aff(const mp_int *px, const mp_int *py)
00051 {
00052 
00053        if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
00054               return MP_YES;
00055        } else {
00056               return MP_NO;
00057        }
00058 
00059 }
00060 
00061 /* Sets P(px, py) to be the point at infinity.  Uses affine coordinates. */
00062 mp_err
00063 ec_GFp_pt_set_inf_aff(mp_int *px, mp_int *py)
00064 {
00065        mp_zero(px);
00066        mp_zero(py);
00067        return MP_OKAY;
00068 }
00069 
00070 /* Computes R = P + Q based on IEEE P1363 A.10.1. Elliptic curve points P, 
00071  * Q, and R can all be identical. Uses affine coordinates. Assumes input
00072  * is already field-encoded using field_enc, and returns output that is
00073  * still field-encoded. */
00074 mp_err
00075 ec_GFp_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
00076                               const mp_int *qy, mp_int *rx, mp_int *ry,
00077                               const ECGroup *group)
00078 {
00079        mp_err res = MP_OKAY;
00080        mp_int lambda, temp, tempx, tempy;
00081 
00082        MP_DIGITS(&lambda) = 0;
00083        MP_DIGITS(&temp) = 0;
00084        MP_DIGITS(&tempx) = 0;
00085        MP_DIGITS(&tempy) = 0;
00086        MP_CHECKOK(mp_init(&lambda));
00087        MP_CHECKOK(mp_init(&temp));
00088        MP_CHECKOK(mp_init(&tempx));
00089        MP_CHECKOK(mp_init(&tempy));
00090        /* if P = inf, then R = Q */
00091        if (ec_GFp_pt_is_inf_aff(px, py) == 0) {
00092               MP_CHECKOK(mp_copy(qx, rx));
00093               MP_CHECKOK(mp_copy(qy, ry));
00094               res = MP_OKAY;
00095               goto CLEANUP;
00096        }
00097        /* if Q = inf, then R = P */
00098        if (ec_GFp_pt_is_inf_aff(qx, qy) == 0) {
00099               MP_CHECKOK(mp_copy(px, rx));
00100               MP_CHECKOK(mp_copy(py, ry));
00101               res = MP_OKAY;
00102               goto CLEANUP;
00103        }
00104        /* if px != qx, then lambda = (py-qy) / (px-qx) */
00105        if (mp_cmp(px, qx) != 0) {
00106               MP_CHECKOK(group->meth->field_sub(py, qy, &tempy, group->meth));
00107               MP_CHECKOK(group->meth->field_sub(px, qx, &tempx, group->meth));
00108               MP_CHECKOK(group->meth->
00109                                field_div(&tempy, &tempx, &lambda, group->meth));
00110        } else {
00111               /* if py != qy or qy = 0, then R = inf */
00112               if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qy) == 0)) {
00113                      mp_zero(rx);
00114                      mp_zero(ry);
00115                      res = MP_OKAY;
00116                      goto CLEANUP;
00117               }
00118               /* lambda = (3qx^2+a) / (2qy) */
00119               MP_CHECKOK(group->meth->field_sqr(qx, &tempx, group->meth));
00120               MP_CHECKOK(mp_set_int(&temp, 3));
00121               if (group->meth->field_enc) {
00122                      MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
00123               }
00124               MP_CHECKOK(group->meth->
00125                                field_mul(&tempx, &temp, &tempx, group->meth));
00126               MP_CHECKOK(group->meth->
00127                                field_add(&tempx, &group->curvea, &tempx, group->meth));
00128               MP_CHECKOK(mp_set_int(&temp, 2));
00129               if (group->meth->field_enc) {
00130                      MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
00131               }
00132               MP_CHECKOK(group->meth->field_mul(qy, &temp, &tempy, group->meth));
00133               MP_CHECKOK(group->meth->
00134                                field_div(&tempx, &tempy, &lambda, group->meth));
00135        }
00136        /* rx = lambda^2 - px - qx */
00137        MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
00138        MP_CHECKOK(group->meth->field_sub(&tempx, px, &tempx, group->meth));
00139        MP_CHECKOK(group->meth->field_sub(&tempx, qx, &tempx, group->meth));
00140        /* ry = (x1-x2) * lambda - y1 */
00141        MP_CHECKOK(group->meth->field_sub(qx, &tempx, &tempy, group->meth));
00142        MP_CHECKOK(group->meth->
00143                         field_mul(&tempy, &lambda, &tempy, group->meth));
00144        MP_CHECKOK(group->meth->field_sub(&tempy, qy, &tempy, group->meth));
00145        MP_CHECKOK(mp_copy(&tempx, rx));
00146        MP_CHECKOK(mp_copy(&tempy, ry));
00147 
00148   CLEANUP:
00149        mp_clear(&lambda);
00150        mp_clear(&temp);
00151        mp_clear(&tempx);
00152        mp_clear(&tempy);
00153        return res;
00154 }
00155 
00156 /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
00157  * identical. Uses affine coordinates. Assumes input is already
00158  * field-encoded using field_enc, and returns output that is still
00159  * field-encoded. */
00160 mp_err
00161 ec_GFp_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
00162                               const mp_int *qy, mp_int *rx, mp_int *ry,
00163                               const ECGroup *group)
00164 {
00165        mp_err res = MP_OKAY;
00166        mp_int nqy;
00167 
00168        MP_DIGITS(&nqy) = 0;
00169        MP_CHECKOK(mp_init(&nqy));
00170        /* nqy = -qy */
00171        MP_CHECKOK(group->meth->field_neg(qy, &nqy, group->meth));
00172        res = group->point_add(px, py, qx, &nqy, rx, ry, group);
00173   CLEANUP:
00174        mp_clear(&nqy);
00175        return res;
00176 }
00177 
00178 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
00179  * affine coordinates. Assumes input is already field-encoded using
00180  * field_enc, and returns output that is still field-encoded. */
00181 mp_err
00182 ec_GFp_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
00183                               mp_int *ry, const ECGroup *group)
00184 {
00185        return ec_GFp_pt_add_aff(px, py, px, py, rx, ry, group);
00186 }
00187 
00188 /* by default, this routine is unused and thus doesn't need to be compiled */
00189 #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
00190 /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and 
00191  * R can be identical. Uses affine coordinates. Assumes input is already
00192  * field-encoded using field_enc, and returns output that is still
00193  * field-encoded. */
00194 mp_err
00195 ec_GFp_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
00196                               mp_int *rx, mp_int *ry, const ECGroup *group)
00197 {
00198        mp_err res = MP_OKAY;
00199        mp_int k, k3, qx, qy, sx, sy;
00200        int b1, b3, i, l;
00201 
00202        MP_DIGITS(&k) = 0;
00203        MP_DIGITS(&k3) = 0;
00204        MP_DIGITS(&qx) = 0;
00205        MP_DIGITS(&qy) = 0;
00206        MP_DIGITS(&sx) = 0;
00207        MP_DIGITS(&sy) = 0;
00208        MP_CHECKOK(mp_init(&k));
00209        MP_CHECKOK(mp_init(&k3));
00210        MP_CHECKOK(mp_init(&qx));
00211        MP_CHECKOK(mp_init(&qy));
00212        MP_CHECKOK(mp_init(&sx));
00213        MP_CHECKOK(mp_init(&sy));
00214 
00215        /* if n = 0 then r = inf */
00216        if (mp_cmp_z(n) == 0) {
00217               mp_zero(rx);
00218               mp_zero(ry);
00219               res = MP_OKAY;
00220               goto CLEANUP;
00221        }
00222        /* Q = P, k = n */
00223        MP_CHECKOK(mp_copy(px, &qx));
00224        MP_CHECKOK(mp_copy(py, &qy));
00225        MP_CHECKOK(mp_copy(n, &k));
00226        /* if n < 0 then Q = -Q, k = -k */
00227        if (mp_cmp_z(n) < 0) {
00228               MP_CHECKOK(group->meth->field_neg(&qy, &qy, group->meth));
00229               MP_CHECKOK(mp_neg(&k, &k));
00230        }
00231 #ifdef ECL_DEBUG                          /* basic double and add method */
00232        l = mpl_significant_bits(&k) - 1;
00233        MP_CHECKOK(mp_copy(&qx, &sx));
00234        MP_CHECKOK(mp_copy(&qy, &sy));
00235        for (i = l - 1; i >= 0; i--) {
00236               /* S = 2S */
00237               MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
00238               /* if k_i = 1, then S = S + Q */
00239               if (mpl_get_bit(&k, i) != 0) {
00240                      MP_CHECKOK(group->
00241                                       point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
00242               }
00243        }
00244 #else                                            /* double and add/subtract method from
00245                                                          * standard */
00246        /* k3 = 3 * k */
00247        MP_CHECKOK(mp_set_int(&k3, 3));
00248        MP_CHECKOK(mp_mul(&k, &k3, &k3));
00249        /* S = Q */
00250        MP_CHECKOK(mp_copy(&qx, &sx));
00251        MP_CHECKOK(mp_copy(&qy, &sy));
00252        /* l = index of high order bit in binary representation of 3*k */
00253        l = mpl_significant_bits(&k3) - 1;
00254        /* for i = l-1 downto 1 */
00255        for (i = l - 1; i >= 1; i--) {
00256               /* S = 2S */
00257               MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
00258               b3 = MP_GET_BIT(&k3, i);
00259               b1 = MP_GET_BIT(&k, i);
00260               /* if k3_i = 1 and k_i = 0, then S = S + Q */
00261               if ((b3 == 1) && (b1 == 0)) {
00262                      MP_CHECKOK(group->
00263                                       point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
00264                      /* if k3_i = 0 and k_i = 1, then S = S - Q */
00265               } else if ((b3 == 0) && (b1 == 1)) {
00266                      MP_CHECKOK(group->
00267                                       point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
00268               }
00269        }
00270 #endif
00271        /* output S */
00272        MP_CHECKOK(mp_copy(&sx, rx));
00273        MP_CHECKOK(mp_copy(&sy, ry));
00274 
00275   CLEANUP:
00276        mp_clear(&k);
00277        mp_clear(&k3);
00278        mp_clear(&qx);
00279        mp_clear(&qy);
00280        mp_clear(&sx);
00281        mp_clear(&sy);
00282        return res;
00283 }
00284 #endif
00285 
00286 /* Validates a point on a GFp curve. */
00287 mp_err 
00288 ec_GFp_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
00289 {
00290        mp_err res = MP_NO;
00291        mp_int accl, accr, tmp, pxt, pyt;
00292 
00293        MP_DIGITS(&accl) = 0;
00294        MP_DIGITS(&accr) = 0;
00295        MP_DIGITS(&tmp) = 0;
00296        MP_DIGITS(&pxt) = 0;
00297        MP_DIGITS(&pyt) = 0;
00298        MP_CHECKOK(mp_init(&accl));
00299        MP_CHECKOK(mp_init(&accr));
00300        MP_CHECKOK(mp_init(&tmp));
00301        MP_CHECKOK(mp_init(&pxt));
00302        MP_CHECKOK(mp_init(&pyt));
00303 
00304     /* 1: Verify that publicValue is not the point at infinity */
00305        if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) {
00306               res = MP_NO;
00307               goto CLEANUP;
00308        }
00309     /* 2: Verify that the coordinates of publicValue are elements 
00310      *    of the field.
00311      */
00312        if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) || 
00313               (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
00314               res = MP_NO;
00315               goto CLEANUP;
00316        }
00317     /* 3: Verify that publicValue is on the curve. */
00318        if (group->meth->field_enc) {
00319               group->meth->field_enc(px, &pxt, group->meth);
00320               group->meth->field_enc(py, &pyt, group->meth);
00321        } else {
00322               mp_copy(px, &pxt);
00323               mp_copy(py, &pyt);
00324        }
00325        /* left-hand side: y^2  */
00326        MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
00327        /* right-hand side: x^3 + a*x + b */
00328        MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
00329        MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
00330        MP_CHECKOK( group->meth->field_mul(&group->curvea, &pxt, &tmp, group->meth) );
00331        MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
00332        MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
00333        /* check LHS - RHS == 0 */
00334        MP_CHECKOK( group->meth->field_sub(&accl, &accr, &accr, group->meth) );
00335        if (mp_cmp_z(&accr) != 0) {
00336               res = MP_NO;
00337               goto CLEANUP;
00338        }
00339     /* 4: Verify that the order of the curve times the publicValue
00340      *    is the point at infinity.
00341      */
00342        MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
00343        if (ec_GFp_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
00344               res = MP_NO;
00345               goto CLEANUP;
00346        }
00347 
00348        res = MP_YES;
00349 
00350 CLEANUP:
00351        mp_clear(&accl);
00352        mp_clear(&accr);
00353        mp_clear(&tmp);
00354        mp_clear(&pxt);
00355        mp_clear(&pyt);
00356        return res;
00357 }