jdk/src/share/native/sun/security/ec/ecp_aff.c
changeset 3492 e549cea58864
equal deleted inserted replaced
3480:c197e38bf15a 3492:e549cea58864
       
     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 2007 Sun Microsystems, Inc.  All rights reserved.
       
    59  * Use is subject to license terms.
       
    60  */
       
    61 
       
    62 #pragma ident   "%Z%%M% %I%     %E% SMI"
       
    63 
       
    64 #include "ecp.h"
       
    65 #include "mplogic.h"
       
    66 #ifndef _KERNEL
       
    67 #include <stdlib.h>
       
    68 #endif
       
    69 
       
    70 /* Checks if point P(px, py) is at infinity.  Uses affine coordinates. */
       
    71 mp_err
       
    72 ec_GFp_pt_is_inf_aff(const mp_int *px, const mp_int *py)
       
    73 {
       
    74 
       
    75         if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
       
    76                 return MP_YES;
       
    77         } else {
       
    78                 return MP_NO;
       
    79         }
       
    80 
       
    81 }
       
    82 
       
    83 /* Sets P(px, py) to be the point at infinity.  Uses affine coordinates. */
       
    84 mp_err
       
    85 ec_GFp_pt_set_inf_aff(mp_int *px, mp_int *py)
       
    86 {
       
    87         mp_zero(px);
       
    88         mp_zero(py);
       
    89         return MP_OKAY;
       
    90 }
       
    91 
       
    92 /* Computes R = P + Q based on IEEE P1363 A.10.1. Elliptic curve points P,
       
    93  * Q, and R can all be identical. Uses affine coordinates. Assumes input
       
    94  * is already field-encoded using field_enc, and returns output that is
       
    95  * still field-encoded. */
       
    96 mp_err
       
    97 ec_GFp_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
       
    98                                   const mp_int *qy, mp_int *rx, mp_int *ry,
       
    99                                   const ECGroup *group)
       
   100 {
       
   101         mp_err res = MP_OKAY;
       
   102         mp_int lambda, temp, tempx, tempy;
       
   103 
       
   104         MP_DIGITS(&lambda) = 0;
       
   105         MP_DIGITS(&temp) = 0;
       
   106         MP_DIGITS(&tempx) = 0;
       
   107         MP_DIGITS(&tempy) = 0;
       
   108         MP_CHECKOK(mp_init(&lambda, FLAG(px)));
       
   109         MP_CHECKOK(mp_init(&temp, FLAG(px)));
       
   110         MP_CHECKOK(mp_init(&tempx, FLAG(px)));
       
   111         MP_CHECKOK(mp_init(&tempy, FLAG(px)));
       
   112         /* if P = inf, then R = Q */
       
   113         if (ec_GFp_pt_is_inf_aff(px, py) == 0) {
       
   114                 MP_CHECKOK(mp_copy(qx, rx));
       
   115                 MP_CHECKOK(mp_copy(qy, ry));
       
   116                 res = MP_OKAY;
       
   117                 goto CLEANUP;
       
   118         }
       
   119         /* if Q = inf, then R = P */
       
   120         if (ec_GFp_pt_is_inf_aff(qx, qy) == 0) {
       
   121                 MP_CHECKOK(mp_copy(px, rx));
       
   122                 MP_CHECKOK(mp_copy(py, ry));
       
   123                 res = MP_OKAY;
       
   124                 goto CLEANUP;
       
   125         }
       
   126         /* if px != qx, then lambda = (py-qy) / (px-qx) */
       
   127         if (mp_cmp(px, qx) != 0) {
       
   128                 MP_CHECKOK(group->meth->field_sub(py, qy, &tempy, group->meth));
       
   129                 MP_CHECKOK(group->meth->field_sub(px, qx, &tempx, group->meth));
       
   130                 MP_CHECKOK(group->meth->
       
   131                                    field_div(&tempy, &tempx, &lambda, group->meth));
       
   132         } else {
       
   133                 /* if py != qy or qy = 0, then R = inf */
       
   134                 if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qy) == 0)) {
       
   135                         mp_zero(rx);
       
   136                         mp_zero(ry);
       
   137                         res = MP_OKAY;
       
   138                         goto CLEANUP;
       
   139                 }
       
   140                 /* lambda = (3qx^2+a) / (2qy) */
       
   141                 MP_CHECKOK(group->meth->field_sqr(qx, &tempx, group->meth));
       
   142                 MP_CHECKOK(mp_set_int(&temp, 3));
       
   143                 if (group->meth->field_enc) {
       
   144                         MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
       
   145                 }
       
   146                 MP_CHECKOK(group->meth->
       
   147                                    field_mul(&tempx, &temp, &tempx, group->meth));
       
   148                 MP_CHECKOK(group->meth->
       
   149                                    field_add(&tempx, &group->curvea, &tempx, group->meth));
       
   150                 MP_CHECKOK(mp_set_int(&temp, 2));
       
   151                 if (group->meth->field_enc) {
       
   152                         MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
       
   153                 }
       
   154                 MP_CHECKOK(group->meth->field_mul(qy, &temp, &tempy, group->meth));
       
   155                 MP_CHECKOK(group->meth->
       
   156                                    field_div(&tempx, &tempy, &lambda, group->meth));
       
   157         }
       
   158         /* rx = lambda^2 - px - qx */
       
   159         MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
       
   160         MP_CHECKOK(group->meth->field_sub(&tempx, px, &tempx, group->meth));
       
   161         MP_CHECKOK(group->meth->field_sub(&tempx, qx, &tempx, group->meth));
       
   162         /* ry = (x1-x2) * lambda - y1 */
       
   163         MP_CHECKOK(group->meth->field_sub(qx, &tempx, &tempy, group->meth));
       
   164         MP_CHECKOK(group->meth->
       
   165                            field_mul(&tempy, &lambda, &tempy, group->meth));
       
   166         MP_CHECKOK(group->meth->field_sub(&tempy, qy, &tempy, group->meth));
       
   167         MP_CHECKOK(mp_copy(&tempx, rx));
       
   168         MP_CHECKOK(mp_copy(&tempy, ry));
       
   169 
       
   170   CLEANUP:
       
   171         mp_clear(&lambda);
       
   172         mp_clear(&temp);
       
   173         mp_clear(&tempx);
       
   174         mp_clear(&tempy);
       
   175         return res;
       
   176 }
       
   177 
       
   178 /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
       
   179  * 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_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
       
   184                                   const mp_int *qy, mp_int *rx, mp_int *ry,
       
   185                                   const ECGroup *group)
       
   186 {
       
   187         mp_err res = MP_OKAY;
       
   188         mp_int nqy;
       
   189 
       
   190         MP_DIGITS(&nqy) = 0;
       
   191         MP_CHECKOK(mp_init(&nqy, FLAG(px)));
       
   192         /* nqy = -qy */
       
   193         MP_CHECKOK(group->meth->field_neg(qy, &nqy, group->meth));
       
   194         res = group->point_add(px, py, qx, &nqy, rx, ry, group);
       
   195   CLEANUP:
       
   196         mp_clear(&nqy);
       
   197         return res;
       
   198 }
       
   199 
       
   200 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
       
   201  * affine coordinates. Assumes input is already field-encoded using
       
   202  * field_enc, and returns output that is still field-encoded. */
       
   203 mp_err
       
   204 ec_GFp_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
       
   205                                   mp_int *ry, const ECGroup *group)
       
   206 {
       
   207         return ec_GFp_pt_add_aff(px, py, px, py, rx, ry, group);
       
   208 }
       
   209 
       
   210 /* by default, this routine is unused and thus doesn't need to be compiled */
       
   211 #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
       
   212 /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and
       
   213  * R can be identical. Uses affine coordinates. Assumes input is already
       
   214  * field-encoded using field_enc, and returns output that is still
       
   215  * field-encoded. */
       
   216 mp_err
       
   217 ec_GFp_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
       
   218                                   mp_int *rx, mp_int *ry, const ECGroup *group)
       
   219 {
       
   220         mp_err res = MP_OKAY;
       
   221         mp_int k, k3, qx, qy, sx, sy;
       
   222         int b1, b3, i, l;
       
   223 
       
   224         MP_DIGITS(&k) = 0;
       
   225         MP_DIGITS(&k3) = 0;
       
   226         MP_DIGITS(&qx) = 0;
       
   227         MP_DIGITS(&qy) = 0;
       
   228         MP_DIGITS(&sx) = 0;
       
   229         MP_DIGITS(&sy) = 0;
       
   230         MP_CHECKOK(mp_init(&k));
       
   231         MP_CHECKOK(mp_init(&k3));
       
   232         MP_CHECKOK(mp_init(&qx));
       
   233         MP_CHECKOK(mp_init(&qy));
       
   234         MP_CHECKOK(mp_init(&sx));
       
   235         MP_CHECKOK(mp_init(&sy));
       
   236 
       
   237         /* if n = 0 then r = inf */
       
   238         if (mp_cmp_z(n) == 0) {
       
   239                 mp_zero(rx);
       
   240                 mp_zero(ry);
       
   241                 res = MP_OKAY;
       
   242                 goto CLEANUP;
       
   243         }
       
   244         /* Q = P, k = n */
       
   245         MP_CHECKOK(mp_copy(px, &qx));
       
   246         MP_CHECKOK(mp_copy(py, &qy));
       
   247         MP_CHECKOK(mp_copy(n, &k));
       
   248         /* if n < 0 then Q = -Q, k = -k */
       
   249         if (mp_cmp_z(n) < 0) {
       
   250                 MP_CHECKOK(group->meth->field_neg(&qy, &qy, group->meth));
       
   251                 MP_CHECKOK(mp_neg(&k, &k));
       
   252         }
       
   253 #ifdef ECL_DEBUG                                /* basic double and add method */
       
   254         l = mpl_significant_bits(&k) - 1;
       
   255         MP_CHECKOK(mp_copy(&qx, &sx));
       
   256         MP_CHECKOK(mp_copy(&qy, &sy));
       
   257         for (i = l - 1; i >= 0; i--) {
       
   258                 /* S = 2S */
       
   259                 MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
       
   260                 /* if k_i = 1, then S = S + Q */
       
   261                 if (mpl_get_bit(&k, i) != 0) {
       
   262                         MP_CHECKOK(group->
       
   263                                            point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
       
   264                 }
       
   265         }
       
   266 #else                                                   /* double and add/subtract method from
       
   267                                                                  * standard */
       
   268         /* k3 = 3 * k */
       
   269         MP_CHECKOK(mp_set_int(&k3, 3));
       
   270         MP_CHECKOK(mp_mul(&k, &k3, &k3));
       
   271         /* S = Q */
       
   272         MP_CHECKOK(mp_copy(&qx, &sx));
       
   273         MP_CHECKOK(mp_copy(&qy, &sy));
       
   274         /* l = index of high order bit in binary representation of 3*k */
       
   275         l = mpl_significant_bits(&k3) - 1;
       
   276         /* for i = l-1 downto 1 */
       
   277         for (i = l - 1; i >= 1; i--) {
       
   278                 /* S = 2S */
       
   279                 MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
       
   280                 b3 = MP_GET_BIT(&k3, i);
       
   281                 b1 = MP_GET_BIT(&k, i);
       
   282                 /* if k3_i = 1 and k_i = 0, then S = S + Q */
       
   283                 if ((b3 == 1) && (b1 == 0)) {
       
   284                         MP_CHECKOK(group->
       
   285                                            point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
       
   286                         /* if k3_i = 0 and k_i = 1, then S = S - Q */
       
   287                 } else if ((b3 == 0) && (b1 == 1)) {
       
   288                         MP_CHECKOK(group->
       
   289                                            point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
       
   290                 }
       
   291         }
       
   292 #endif
       
   293         /* output S */
       
   294         MP_CHECKOK(mp_copy(&sx, rx));
       
   295         MP_CHECKOK(mp_copy(&sy, ry));
       
   296 
       
   297   CLEANUP:
       
   298         mp_clear(&k);
       
   299         mp_clear(&k3);
       
   300         mp_clear(&qx);
       
   301         mp_clear(&qy);
       
   302         mp_clear(&sx);
       
   303         mp_clear(&sy);
       
   304         return res;
       
   305 }
       
   306 #endif
       
   307 
       
   308 /* Validates a point on a GFp curve. */
       
   309 mp_err
       
   310 ec_GFp_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
       
   311 {
       
   312         mp_err res = MP_NO;
       
   313         mp_int accl, accr, tmp, pxt, pyt;
       
   314 
       
   315         MP_DIGITS(&accl) = 0;
       
   316         MP_DIGITS(&accr) = 0;
       
   317         MP_DIGITS(&tmp) = 0;
       
   318         MP_DIGITS(&pxt) = 0;
       
   319         MP_DIGITS(&pyt) = 0;
       
   320         MP_CHECKOK(mp_init(&accl, FLAG(px)));
       
   321         MP_CHECKOK(mp_init(&accr, FLAG(px)));
       
   322         MP_CHECKOK(mp_init(&tmp, FLAG(px)));
       
   323         MP_CHECKOK(mp_init(&pxt, FLAG(px)));
       
   324         MP_CHECKOK(mp_init(&pyt, FLAG(px)));
       
   325 
       
   326     /* 1: Verify that publicValue is not the point at infinity */
       
   327         if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) {
       
   328                 res = MP_NO;
       
   329                 goto CLEANUP;
       
   330         }
       
   331     /* 2: Verify that the coordinates of publicValue are elements
       
   332      *    of the field.
       
   333      */
       
   334         if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) ||
       
   335                 (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
       
   336                 res = MP_NO;
       
   337                 goto CLEANUP;
       
   338         }
       
   339     /* 3: Verify that publicValue is on the curve. */
       
   340         if (group->meth->field_enc) {
       
   341                 group->meth->field_enc(px, &pxt, group->meth);
       
   342                 group->meth->field_enc(py, &pyt, group->meth);
       
   343         } else {
       
   344                 mp_copy(px, &pxt);
       
   345                 mp_copy(py, &pyt);
       
   346         }
       
   347         /* left-hand side: y^2  */
       
   348         MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
       
   349         /* right-hand side: x^3 + a*x + b */
       
   350         MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
       
   351         MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
       
   352         MP_CHECKOK( group->meth->field_mul(&group->curvea, &pxt, &tmp, group->meth) );
       
   353         MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
       
   354         MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
       
   355         /* check LHS - RHS == 0 */
       
   356         MP_CHECKOK( group->meth->field_sub(&accl, &accr, &accr, group->meth) );
       
   357         if (mp_cmp_z(&accr) != 0) {
       
   358                 res = MP_NO;
       
   359                 goto CLEANUP;
       
   360         }
       
   361     /* 4: Verify that the order of the curve times the publicValue
       
   362      *    is the point at infinity.
       
   363      */
       
   364         MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
       
   365         if (ec_GFp_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
       
   366                 res = MP_NO;
       
   367                 goto CLEANUP;
       
   368         }
       
   369 
       
   370         res = MP_YES;
       
   371 
       
   372 CLEANUP:
       
   373         mp_clear(&accl);
       
   374         mp_clear(&accr);
       
   375         mp_clear(&tmp);
       
   376         mp_clear(&pxt);
       
   377         mp_clear(&pyt);
       
   378         return res;
       
   379 }