jdk/src/share/native/sun/security/ec/ec2_mont.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 binary polynomial 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  *
       
    41  * Alternatively, the contents of this file may be used under the terms of
       
    42  * either the GNU General Public License Version 2 or later (the "GPL"), or
       
    43  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
       
    44  * in which case the provisions of the GPL or the LGPL are applicable instead
       
    45  * of those above. If you wish to allow use of your version of this file only
       
    46  * under the terms of either the GPL or the LGPL, and not to allow others to
       
    47  * use your version of this file under the terms of the MPL, indicate your
       
    48  * decision by deleting the provisions above and replace them with the notice
       
    49  * and other provisions required by the GPL or the LGPL. If you do not delete
       
    50  * the provisions above, a recipient may use your version of this file under
       
    51  * the terms of any one of the MPL, the GPL or the LGPL.
       
    52  *
       
    53  *********************************************************************** */
       
    54 /*
       
    55  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
       
    56  * Use is subject to license terms.
       
    57  */
       
    58 
       
    59 #pragma ident   "%Z%%M% %I%     %E% SMI"
       
    60 
       
    61 #include "ec2.h"
       
    62 #include "mplogic.h"
       
    63 #include "mp_gf2m.h"
       
    64 #ifndef _KERNEL
       
    65 #include <stdlib.h>
       
    66 #endif
       
    67 
       
    68 /* Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery
       
    69  * projective coordinates. Uses algorithm Mdouble in appendix of Lopez, J.
       
    70  * and Dahab, R.  "Fast multiplication on elliptic curves over GF(2^m)
       
    71  * without precomputation". modified to not require precomputation of
       
    72  * c=b^{2^{m-1}}. */
       
    73 static mp_err
       
    74 gf2m_Mdouble(mp_int *x, mp_int *z, const ECGroup *group, int kmflag)
       
    75 {
       
    76         mp_err res = MP_OKAY;
       
    77         mp_int t1;
       
    78 
       
    79         MP_DIGITS(&t1) = 0;
       
    80         MP_CHECKOK(mp_init(&t1, kmflag));
       
    81 
       
    82         MP_CHECKOK(group->meth->field_sqr(x, x, group->meth));
       
    83         MP_CHECKOK(group->meth->field_sqr(z, &t1, group->meth));
       
    84         MP_CHECKOK(group->meth->field_mul(x, &t1, z, group->meth));
       
    85         MP_CHECKOK(group->meth->field_sqr(x, x, group->meth));
       
    86         MP_CHECKOK(group->meth->field_sqr(&t1, &t1, group->meth));
       
    87         MP_CHECKOK(group->meth->
       
    88                            field_mul(&group->curveb, &t1, &t1, group->meth));
       
    89         MP_CHECKOK(group->meth->field_add(x, &t1, x, group->meth));
       
    90 
       
    91   CLEANUP:
       
    92         mp_clear(&t1);
       
    93         return res;
       
    94 }
       
    95 
       
    96 /* Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in
       
    97  * Montgomery projective coordinates. Uses algorithm Madd in appendix of
       
    98  * Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over
       
    99  * GF(2^m) without precomputation". */
       
   100 static mp_err
       
   101 gf2m_Madd(const mp_int *x, mp_int *x1, mp_int *z1, mp_int *x2, mp_int *z2,
       
   102                   const ECGroup *group, int kmflag)
       
   103 {
       
   104         mp_err res = MP_OKAY;
       
   105         mp_int t1, t2;
       
   106 
       
   107         MP_DIGITS(&t1) = 0;
       
   108         MP_DIGITS(&t2) = 0;
       
   109         MP_CHECKOK(mp_init(&t1, kmflag));
       
   110         MP_CHECKOK(mp_init(&t2, kmflag));
       
   111 
       
   112         MP_CHECKOK(mp_copy(x, &t1));
       
   113         MP_CHECKOK(group->meth->field_mul(x1, z2, x1, group->meth));
       
   114         MP_CHECKOK(group->meth->field_mul(z1, x2, z1, group->meth));
       
   115         MP_CHECKOK(group->meth->field_mul(x1, z1, &t2, group->meth));
       
   116         MP_CHECKOK(group->meth->field_add(z1, x1, z1, group->meth));
       
   117         MP_CHECKOK(group->meth->field_sqr(z1, z1, group->meth));
       
   118         MP_CHECKOK(group->meth->field_mul(z1, &t1, x1, group->meth));
       
   119         MP_CHECKOK(group->meth->field_add(x1, &t2, x1, group->meth));
       
   120 
       
   121   CLEANUP:
       
   122         mp_clear(&t1);
       
   123         mp_clear(&t2);
       
   124         return res;
       
   125 }
       
   126 
       
   127 /* Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
       
   128  * using Montgomery point multiplication algorithm Mxy() in appendix of
       
   129  * Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over
       
   130  * GF(2^m) without precomputation". Returns: 0 on error 1 if return value
       
   131  * should be the point at infinity 2 otherwise */
       
   132 static int
       
   133 gf2m_Mxy(const mp_int *x, const mp_int *y, mp_int *x1, mp_int *z1,
       
   134                  mp_int *x2, mp_int *z2, const ECGroup *group)
       
   135 {
       
   136         mp_err res = MP_OKAY;
       
   137         int ret = 0;
       
   138         mp_int t3, t4, t5;
       
   139 
       
   140         MP_DIGITS(&t3) = 0;
       
   141         MP_DIGITS(&t4) = 0;
       
   142         MP_DIGITS(&t5) = 0;
       
   143         MP_CHECKOK(mp_init(&t3, FLAG(x2)));
       
   144         MP_CHECKOK(mp_init(&t4, FLAG(x2)));
       
   145         MP_CHECKOK(mp_init(&t5, FLAG(x2)));
       
   146 
       
   147         if (mp_cmp_z(z1) == 0) {
       
   148                 mp_zero(x2);
       
   149                 mp_zero(z2);
       
   150                 ret = 1;
       
   151                 goto CLEANUP;
       
   152         }
       
   153 
       
   154         if (mp_cmp_z(z2) == 0) {
       
   155                 MP_CHECKOK(mp_copy(x, x2));
       
   156                 MP_CHECKOK(group->meth->field_add(x, y, z2, group->meth));
       
   157                 ret = 2;
       
   158                 goto CLEANUP;
       
   159         }
       
   160 
       
   161         MP_CHECKOK(mp_set_int(&t5, 1));
       
   162         if (group->meth->field_enc) {
       
   163                 MP_CHECKOK(group->meth->field_enc(&t5, &t5, group->meth));
       
   164         }
       
   165 
       
   166         MP_CHECKOK(group->meth->field_mul(z1, z2, &t3, group->meth));
       
   167 
       
   168         MP_CHECKOK(group->meth->field_mul(z1, x, z1, group->meth));
       
   169         MP_CHECKOK(group->meth->field_add(z1, x1, z1, group->meth));
       
   170         MP_CHECKOK(group->meth->field_mul(z2, x, z2, group->meth));
       
   171         MP_CHECKOK(group->meth->field_mul(z2, x1, x1, group->meth));
       
   172         MP_CHECKOK(group->meth->field_add(z2, x2, z2, group->meth));
       
   173 
       
   174         MP_CHECKOK(group->meth->field_mul(z2, z1, z2, group->meth));
       
   175         MP_CHECKOK(group->meth->field_sqr(x, &t4, group->meth));
       
   176         MP_CHECKOK(group->meth->field_add(&t4, y, &t4, group->meth));
       
   177         MP_CHECKOK(group->meth->field_mul(&t4, &t3, &t4, group->meth));
       
   178         MP_CHECKOK(group->meth->field_add(&t4, z2, &t4, group->meth));
       
   179 
       
   180         MP_CHECKOK(group->meth->field_mul(&t3, x, &t3, group->meth));
       
   181         MP_CHECKOK(group->meth->field_div(&t5, &t3, &t3, group->meth));
       
   182         MP_CHECKOK(group->meth->field_mul(&t3, &t4, &t4, group->meth));
       
   183         MP_CHECKOK(group->meth->field_mul(x1, &t3, x2, group->meth));
       
   184         MP_CHECKOK(group->meth->field_add(x2, x, z2, group->meth));
       
   185 
       
   186         MP_CHECKOK(group->meth->field_mul(z2, &t4, z2, group->meth));
       
   187         MP_CHECKOK(group->meth->field_add(z2, y, z2, group->meth));
       
   188 
       
   189         ret = 2;
       
   190 
       
   191   CLEANUP:
       
   192         mp_clear(&t3);
       
   193         mp_clear(&t4);
       
   194         mp_clear(&t5);
       
   195         if (res == MP_OKAY) {
       
   196                 return ret;
       
   197         } else {
       
   198                 return 0;
       
   199         }
       
   200 }
       
   201 
       
   202 /* Computes R = nP based on algorithm 2P of Lopex, J. and Dahab, R.  "Fast
       
   203  * multiplication on elliptic curves over GF(2^m) without
       
   204  * precomputation". Elliptic curve points P and R can be identical. Uses
       
   205  * Montgomery projective coordinates. */
       
   206 mp_err
       
   207 ec_GF2m_pt_mul_mont(const mp_int *n, const mp_int *px, const mp_int *py,
       
   208                                         mp_int *rx, mp_int *ry, const ECGroup *group)
       
   209 {
       
   210         mp_err res = MP_OKAY;
       
   211         mp_int x1, x2, z1, z2;
       
   212         int i, j;
       
   213         mp_digit top_bit, mask;
       
   214 
       
   215         MP_DIGITS(&x1) = 0;
       
   216         MP_DIGITS(&x2) = 0;
       
   217         MP_DIGITS(&z1) = 0;
       
   218         MP_DIGITS(&z2) = 0;
       
   219         MP_CHECKOK(mp_init(&x1, FLAG(n)));
       
   220         MP_CHECKOK(mp_init(&x2, FLAG(n)));
       
   221         MP_CHECKOK(mp_init(&z1, FLAG(n)));
       
   222         MP_CHECKOK(mp_init(&z2, FLAG(n)));
       
   223 
       
   224         /* if result should be point at infinity */
       
   225         if ((mp_cmp_z(n) == 0) || (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES)) {
       
   226                 MP_CHECKOK(ec_GF2m_pt_set_inf_aff(rx, ry));
       
   227                 goto CLEANUP;
       
   228         }
       
   229 
       
   230         MP_CHECKOK(mp_copy(px, &x1));   /* x1 = px */
       
   231         MP_CHECKOK(mp_set_int(&z1, 1)); /* z1 = 1 */
       
   232         MP_CHECKOK(group->meth->field_sqr(&x1, &z2, group->meth));      /* z2 =
       
   233                                                                                                                                  * x1^2 =
       
   234                                                                                                                                  * px^2 */
       
   235         MP_CHECKOK(group->meth->field_sqr(&z2, &x2, group->meth));
       
   236         MP_CHECKOK(group->meth->field_add(&x2, &group->curveb, &x2, group->meth));      /* x2
       
   237                                                                                                                                                                  * =
       
   238                                                                                                                                                                  * px^4
       
   239                                                                                                                                                                  * +
       
   240                                                                                                                                                                  * b
       
   241                                                                                                                                                                  */
       
   242 
       
   243         /* find top-most bit and go one past it */
       
   244         i = MP_USED(n) - 1;
       
   245         j = MP_DIGIT_BIT - 1;
       
   246         top_bit = 1;
       
   247         top_bit <<= MP_DIGIT_BIT - 1;
       
   248         mask = top_bit;
       
   249         while (!(MP_DIGITS(n)[i] & mask)) {
       
   250                 mask >>= 1;
       
   251                 j--;
       
   252         }
       
   253         mask >>= 1;
       
   254         j--;
       
   255 
       
   256         /* if top most bit was at word break, go to next word */
       
   257         if (!mask) {
       
   258                 i--;
       
   259                 j = MP_DIGIT_BIT - 1;
       
   260                 mask = top_bit;
       
   261         }
       
   262 
       
   263         for (; i >= 0; i--) {
       
   264                 for (; j >= 0; j--) {
       
   265                         if (MP_DIGITS(n)[i] & mask) {
       
   266                                 MP_CHECKOK(gf2m_Madd(px, &x1, &z1, &x2, &z2, group, FLAG(n)));
       
   267                                 MP_CHECKOK(gf2m_Mdouble(&x2, &z2, group, FLAG(n)));
       
   268                         } else {
       
   269                                 MP_CHECKOK(gf2m_Madd(px, &x2, &z2, &x1, &z1, group, FLAG(n)));
       
   270                                 MP_CHECKOK(gf2m_Mdouble(&x1, &z1, group, FLAG(n)));
       
   271                         }
       
   272                         mask >>= 1;
       
   273                 }
       
   274                 j = MP_DIGIT_BIT - 1;
       
   275                 mask = top_bit;
       
   276         }
       
   277 
       
   278         /* convert out of "projective" coordinates */
       
   279         i = gf2m_Mxy(px, py, &x1, &z1, &x2, &z2, group);
       
   280         if (i == 0) {
       
   281                 res = MP_BADARG;
       
   282                 goto CLEANUP;
       
   283         } else if (i == 1) {
       
   284                 MP_CHECKOK(ec_GF2m_pt_set_inf_aff(rx, ry));
       
   285         } else {
       
   286                 MP_CHECKOK(mp_copy(&x2, rx));
       
   287                 MP_CHECKOK(mp_copy(&z2, ry));
       
   288         }
       
   289 
       
   290   CLEANUP:
       
   291         mp_clear(&x1);
       
   292         mp_clear(&x2);
       
   293         mp_clear(&z1);
       
   294         mp_clear(&z2);
       
   295         return res;
       
   296 }