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.
  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  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
  38  *
  39  * Alternatively, the contents of this file may be used under the terms of
  40  * either the GNU General Public License Version 2 or later (the "GPL"), or
  41  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  42  * in which case the provisions of the GPL or the LGPL are applicable instead
  43  * of those above. If you wish to allow use of your version of this file only
  44  * under the terms of either the GPL or the LGPL, and not to allow others to
  45  * use your version of this file under the terms of the MPL, indicate your
  46  * decision by deleting the provisions above and replace them with the notice
  47  * and other provisions required by the GPL or the LGPL. If you do not delete
  48  * the provisions above, a recipient may use your version of this file under
  49  * the terms of any one of the MPL, the GPL or the LGPL.
  50  *
  51  *********************************************************************** */
  52 /*
  53  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
  54  * Use is subject to license terms.
  55  */
  56 
  57 /* Uses Montgomery reduction for field arithmetic.  See mpi/mpmontg.c for
  58  * code implementation. */
  59 
  60 #include "mpi.h"
  61 #include "mplogic.h"
  62 #include "mpi-priv.h"
  63 #include "ecl-priv.h"
  64 #include "ecp.h"
  65 #ifndef _KERNEL
  66 #include <stdlib.h>
  67 #include <stdio.h>
  68 #endif
  69 
  70 /* Construct a generic GFMethod for arithmetic over prime fields with
  71  * irreducible irr. */
  72 GFMethod *
  73 GFMethod_consGFp_mont(const mp_int *irr)
  74 {
  75         mp_err res = MP_OKAY;
  76         int i;
  77         GFMethod *meth = NULL;
  78         mp_mont_modulus *mmm;
  79 
  80         meth = GFMethod_consGFp(irr);
  81         if (meth == NULL)
  82                 return NULL;
  83 
  84 #ifdef _KERNEL
  85         mmm = (mp_mont_modulus *) kmem_alloc(sizeof(mp_mont_modulus),
  86             FLAG(irr));
  87 #else
  88         mmm = (mp_mont_modulus *) malloc(sizeof(mp_mont_modulus));
  89 #endif
  90         if (mmm == NULL) {
  91                 res = MP_MEM;
  92                 goto CLEANUP;
  93         }
  94 
  95         meth->field_mul = &ec_GFp_mul_mont;
  96         meth->field_sqr = &ec_GFp_sqr_mont;
  97         meth->field_div = &ec_GFp_div_mont;
  98         meth->field_enc = &ec_GFp_enc_mont;
  99         meth->field_dec = &ec_GFp_dec_mont;
 100         meth->extra1 = mmm;
 101         meth->extra2 = NULL;
 102         meth->extra_free = &ec_GFp_extra_free_mont;
 103 
 104         mmm->N = meth->irr;
 105         i = mpl_significant_bits(&meth->irr);
 106         i += MP_DIGIT_BIT - 1;
 107         mmm->b = i - i % MP_DIGIT_BIT;
 108         mmm->n0prime = 0 - s_mp_invmod_radix(MP_DIGIT(&meth->irr, 0));
 109 
 110   CLEANUP:
 111         if (res != MP_OKAY) {
 112                 GFMethod_free(meth);
 113                 return NULL;
 114         }
 115         return meth;
 116 }
 117 
 118 /* Wrapper functions for generic prime field arithmetic. */
 119 
 120 /* Field multiplication using Montgomery reduction. */
 121 mp_err
 122 ec_GFp_mul_mont(const mp_int *a, const mp_int *b, mp_int *r,
 123                                 const GFMethod *meth)
 124 {
 125         mp_err res = MP_OKAY;
 126 
 127 #ifdef MP_MONT_USE_MP_MUL
 128         /* if MP_MONT_USE_MP_MUL is defined, then the function s_mp_mul_mont
 129          * is not implemented and we have to use mp_mul and s_mp_redc directly
 130          */
 131         MP_CHECKOK(mp_mul(a, b, r));
 132         MP_CHECKOK(s_mp_redc(r, (mp_mont_modulus *) meth->extra1));
 133 #else
 134         mp_int s;
 135 
 136         MP_DIGITS(&s) = 0;
 137         /* s_mp_mul_mont doesn't allow source and destination to be the same */
 138         if ((a == r) || (b == r)) {
 139                 MP_CHECKOK(mp_init(&s, FLAG(a)));
 140                 MP_CHECKOK(s_mp_mul_mont
 141                                    (a, b, &s, (mp_mont_modulus *) meth->extra1));
 142                 MP_CHECKOK(mp_copy(&s, r));
 143                 mp_clear(&s);
 144         } else {
 145                 return s_mp_mul_mont(a, b, r, (mp_mont_modulus *) meth->extra1);
 146         }
 147 #endif
 148   CLEANUP:
 149         return res;
 150 }
 151 
 152 /* Field squaring using Montgomery reduction. */
 153 mp_err
 154 ec_GFp_sqr_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
 155 {
 156         return ec_GFp_mul_mont(a, a, r, meth);
 157 }
 158 
 159 /* Field division using Montgomery reduction. */
 160 mp_err
 161 ec_GFp_div_mont(const mp_int *a, const mp_int *b, mp_int *r,
 162                                 const GFMethod *meth)
 163 {
 164         mp_err res = MP_OKAY;
 165 
 166         /* if A=aZ represents a encoded in montgomery coordinates with Z and #
 167          * and \ respectively represent multiplication and division in
 168          * montgomery coordinates, then A\B = (a/b)Z = (A/B)Z and Binv =
 169          * (1/b)Z = (1/B)(Z^2) where B # Binv = Z */
 170         MP_CHECKOK(ec_GFp_div(a, b, r, meth));
 171         MP_CHECKOK(ec_GFp_enc_mont(r, r, meth));
 172         if (a == NULL) {
 173                 MP_CHECKOK(ec_GFp_enc_mont(r, r, meth));
 174         }
 175   CLEANUP:
 176         return res;
 177 }
 178 
 179 /* Encode a field element in Montgomery form. See s_mp_to_mont in
 180  * mpi/mpmontg.c */
 181 mp_err
 182 ec_GFp_enc_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
 183 {
 184         mp_mont_modulus *mmm;
 185         mp_err res = MP_OKAY;
 186 
 187         mmm = (mp_mont_modulus *) meth->extra1;
 188         MP_CHECKOK(mpl_lsh(a, r, mmm->b));
 189         MP_CHECKOK(mp_mod(r, &mmm->N, r));
 190   CLEANUP:
 191         return res;
 192 }
 193 
 194 /* Decode a field element from Montgomery form. */
 195 mp_err
 196 ec_GFp_dec_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
 197 {
 198         mp_err res = MP_OKAY;
 199 
 200         if (a != r) {
 201                 MP_CHECKOK(mp_copy(a, r));
 202         }
 203         MP_CHECKOK(s_mp_redc(r, (mp_mont_modulus *) meth->extra1));
 204   CLEANUP:
 205         return res;
 206 }
 207 
 208 /* Free the memory allocated to the extra fields of Montgomery GFMethod
 209  * object. */
 210 void
 211 ec_GFp_extra_free_mont(GFMethod *meth)
 212 {
 213         if (meth->extra1 != NULL) {
 214 #ifdef _KERNEL
 215                 kmem_free(meth->extra1, sizeof(mp_mont_modulus));
 216 #else
 217                 free(meth->extra1);
 218 #endif
 219                 meth->extra1 = NULL;
 220         }
 221 }