Back to index

lightning-sunbird  0.9+nobinonly
ecp_384.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  *   Douglas Stebila <douglas@stebila.ca>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "ecp.h"
00040 #include "mpi.h"
00041 #include "mplogic.h"
00042 #include "mpi-priv.h"
00043 #include <stdlib.h>
00044 
00045 /* Fast modular reduction for p384 = 2^384 - 2^128 - 2^96 + 2^32 - 1.  a can be r. 
00046  * Uses algorithm 2.30 from Hankerson, Menezes, Vanstone. Guide to 
00047  * Elliptic Curve Cryptography. */
00048 mp_err
00049 ec_GFp_nistp384_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
00050 {
00051        mp_err res = MP_OKAY;
00052        int a_bits = mpl_significant_bits(a);
00053        int i;
00054 
00055        /* m1, m2 are statically-allocated mp_int of exactly the size we need */
00056        mp_int m[10];
00057 
00058 #ifdef ECL_THIRTY_TWO_BIT
00059        mp_digit s[10][12];
00060        for (i = 0; i < 10; i++) {
00061               MP_SIGN(&m[i]) = MP_ZPOS;
00062               MP_ALLOC(&m[i]) = 12;
00063               MP_USED(&m[i]) = 12;
00064               MP_DIGITS(&m[i]) = s[i];
00065        }
00066 #else
00067        mp_digit s[10][6];
00068        for (i = 0; i < 10; i++) {
00069               MP_SIGN(&m[i]) = MP_ZPOS;
00070               MP_ALLOC(&m[i]) = 6;
00071               MP_USED(&m[i]) = 6;
00072               MP_DIGITS(&m[i]) = s[i];
00073        }
00074 #endif
00075 
00076 #ifdef ECL_THIRTY_TWO_BIT
00077        /* for polynomials larger than twice the field size or polynomials 
00078         * not using all words, use regular reduction */
00079        if ((a_bits > 768) || (a_bits <= 736)) {
00080               MP_CHECKOK(mp_mod(a, &meth->irr, r));
00081        } else {
00082               for (i = 0; i < 12; i++) {
00083                      s[0][i] = MP_DIGIT(a, i);
00084               }
00085               s[1][0] = 0;
00086               s[1][1] = 0;
00087               s[1][2] = 0;
00088               s[1][3] = 0;
00089               s[1][4] = MP_DIGIT(a, 21);
00090               s[1][5] = MP_DIGIT(a, 22);
00091               s[1][6] = MP_DIGIT(a, 23);
00092               s[1][7] = 0;
00093               s[1][8] = 0;
00094               s[1][9] = 0;
00095               s[1][10] = 0;
00096               s[1][11] = 0;
00097               for (i = 0; i < 12; i++) {
00098                      s[2][i] = MP_DIGIT(a, i+12);
00099               }
00100               s[3][0] = MP_DIGIT(a, 21);
00101               s[3][1] = MP_DIGIT(a, 22);
00102               s[3][2] = MP_DIGIT(a, 23);
00103               for (i = 3; i < 12; i++) {
00104                      s[3][i] = MP_DIGIT(a, i+9);
00105               }
00106               s[4][0] = 0;
00107               s[4][1] = MP_DIGIT(a, 23);
00108               s[4][2] = 0;
00109               s[4][3] = MP_DIGIT(a, 20);
00110               for (i = 4; i < 12; i++) {
00111                      s[4][i] = MP_DIGIT(a, i+8);
00112               }
00113               s[5][0] = 0;
00114               s[5][1] = 0;
00115               s[5][2] = 0;
00116               s[5][3] = 0;
00117               s[5][4] = MP_DIGIT(a, 20);
00118               s[5][5] = MP_DIGIT(a, 21);
00119               s[5][6] = MP_DIGIT(a, 22);
00120               s[5][7] = MP_DIGIT(a, 23);
00121               s[5][8] = 0;
00122               s[5][9] = 0;
00123               s[5][10] = 0;
00124               s[5][11] = 0;
00125               s[6][0] = MP_DIGIT(a, 20);
00126               s[6][1] = 0;
00127               s[6][2] = 0;
00128               s[6][3] = MP_DIGIT(a, 21);
00129               s[6][4] = MP_DIGIT(a, 22);
00130               s[6][5] = MP_DIGIT(a, 23);
00131               s[6][6] = 0;
00132               s[6][7] = 0;
00133               s[6][8] = 0;
00134               s[6][9] = 0;
00135               s[6][10] = 0;
00136               s[6][11] = 0;
00137               s[7][0] = MP_DIGIT(a, 23);
00138               for (i = 1; i < 12; i++) {
00139                      s[7][i] = MP_DIGIT(a, i+11);
00140               }
00141               s[8][0] = 0;
00142               s[8][1] = MP_DIGIT(a, 20);
00143               s[8][2] = MP_DIGIT(a, 21);
00144               s[8][3] = MP_DIGIT(a, 22);
00145               s[8][4] = MP_DIGIT(a, 23);
00146               s[8][5] = 0;
00147               s[8][6] = 0;
00148               s[8][7] = 0;
00149               s[8][8] = 0;
00150               s[8][9] = 0;
00151               s[8][10] = 0;
00152               s[8][11] = 0;
00153               s[9][0] = 0;
00154               s[9][1] = 0;
00155               s[9][2] = 0;
00156               s[9][3] = MP_DIGIT(a, 23);
00157               s[9][4] = MP_DIGIT(a, 23);
00158               s[9][5] = 0;
00159               s[9][6] = 0;
00160               s[9][7] = 0;
00161               s[9][8] = 0;
00162               s[9][9] = 0;
00163               s[9][10] = 0;
00164               s[9][11] = 0;
00165 
00166               MP_CHECKOK(mp_add(&m[0], &m[1], r));
00167               MP_CHECKOK(mp_add(r, &m[1], r));
00168               MP_CHECKOK(mp_add(r, &m[2], r));
00169               MP_CHECKOK(mp_add(r, &m[3], r));
00170               MP_CHECKOK(mp_add(r, &m[4], r));
00171               MP_CHECKOK(mp_add(r, &m[5], r));
00172               MP_CHECKOK(mp_add(r, &m[6], r));
00173               MP_CHECKOK(mp_sub(r, &m[7], r));
00174               MP_CHECKOK(mp_sub(r, &m[8], r));
00175               MP_CHECKOK(mp_submod(r, &m[9], &meth->irr, r));
00176               s_mp_clamp(r);
00177        }
00178 #else
00179        /* for polynomials larger than twice the field size or polynomials 
00180         * not using all words, use regular reduction */
00181        if ((a_bits > 768) || (a_bits <= 736)) {
00182               MP_CHECKOK(mp_mod(a, &meth->irr, r));
00183        } else {
00184               for (i = 0; i < 6; i++) {
00185                      s[0][i] = MP_DIGIT(a, i);
00186               }
00187               s[1][0] = 0;
00188               s[1][1] = 0;
00189               s[1][2] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
00190               s[1][3] = MP_DIGIT(a, 11) >> 32;
00191               s[1][4] = 0;
00192               s[1][5] = 0;
00193               for (i = 0; i < 6; i++) {
00194                      s[2][i] = MP_DIGIT(a, i+6);
00195               }
00196               s[3][0] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
00197               s[3][1] = (MP_DIGIT(a, 11) >> 32) | (MP_DIGIT(a, 6) << 32);
00198               for (i = 2; i < 6; i++) {
00199                      s[3][i] = (MP_DIGIT(a, i+4) >> 32) | (MP_DIGIT(a, i+5) << 32);
00200               }
00201               s[4][0] = (MP_DIGIT(a, 11) >> 32) << 32;
00202               s[4][1] = MP_DIGIT(a, 10) << 32;
00203               for (i = 2; i < 6; i++) {
00204                      s[4][i] = MP_DIGIT(a, i+4);
00205               }
00206               s[5][0] = 0;
00207               s[5][1] = 0;
00208               s[5][2] = MP_DIGIT(a, 10);
00209               s[5][3] = MP_DIGIT(a, 11);
00210               s[5][4] = 0;
00211               s[5][5] = 0;
00212               s[6][0] = (MP_DIGIT(a, 10) << 32) >> 32;
00213               s[6][1] = (MP_DIGIT(a, 10) >> 32) << 32;
00214               s[6][2] = MP_DIGIT(a, 11);
00215               s[6][3] = 0;
00216               s[6][4] = 0;
00217               s[6][5] = 0;
00218               s[7][0] = (MP_DIGIT(a, 11) >> 32) | (MP_DIGIT(a, 6) << 32);
00219               for (i = 1; i < 6; i++) {
00220                      s[7][i] = (MP_DIGIT(a, i+5) >> 32) | (MP_DIGIT(a, i+6) << 32);
00221               }
00222               s[8][0] = MP_DIGIT(a, 10) << 32;
00223               s[8][1] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
00224               s[8][2] = MP_DIGIT(a, 11) >> 32;
00225               s[8][3] = 0;
00226               s[8][4] = 0;
00227               s[8][5] = 0;
00228               s[9][0] = 0;
00229               s[9][1] = (MP_DIGIT(a, 11) >> 32) << 32;
00230               s[9][2] = MP_DIGIT(a, 11) >> 32;
00231               s[9][3] = 0;
00232               s[9][4] = 0;
00233               s[9][5] = 0;
00234 
00235               MP_CHECKOK(mp_add(&m[0], &m[1], r));
00236               MP_CHECKOK(mp_add(r, &m[1], r));
00237               MP_CHECKOK(mp_add(r, &m[2], r));
00238               MP_CHECKOK(mp_add(r, &m[3], r));
00239               MP_CHECKOK(mp_add(r, &m[4], r));
00240               MP_CHECKOK(mp_add(r, &m[5], r));
00241               MP_CHECKOK(mp_add(r, &m[6], r));
00242               MP_CHECKOK(mp_sub(r, &m[7], r));
00243               MP_CHECKOK(mp_sub(r, &m[8], r));
00244               MP_CHECKOK(mp_submod(r, &m[9], &meth->irr, r));
00245               s_mp_clamp(r);
00246        }
00247 #endif
00248 
00249   CLEANUP:
00250        return res;
00251 }
00252 
00253 /* Compute the square of polynomial a, reduce modulo p384. Store the
00254  * result in r.  r could be a.  Uses optimized modular reduction for p384. 
00255  */
00256 mp_err
00257 ec_GFp_nistp384_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
00258 {
00259        mp_err res = MP_OKAY;
00260 
00261        MP_CHECKOK(mp_sqr(a, r));
00262        MP_CHECKOK(ec_GFp_nistp384_mod(r, r, meth));
00263   CLEANUP:
00264        return res;
00265 }
00266 
00267 /* Compute the product of two polynomials a and b, reduce modulo p384.
00268  * Store the result in r.  r could be a or b; a could be b.  Uses
00269  * optimized modular reduction for p384. */
00270 mp_err
00271 ec_GFp_nistp384_mul(const mp_int *a, const mp_int *b, mp_int *r,
00272                                    const GFMethod *meth)
00273 {
00274        mp_err res = MP_OKAY;
00275 
00276        MP_CHECKOK(mp_mul(a, b, r));
00277        MP_CHECKOK(ec_GFp_nistp384_mod(r, r, meth));
00278   CLEANUP:
00279        return res;
00280 }
00281 
00282 /* Wire in fast field arithmetic and precomputation of base point for
00283  * named curves. */
00284 mp_err
00285 ec_group_set_gfp384(ECGroup *group, ECCurveName name)
00286 {
00287        if (name == ECCurve_NIST_P384) {
00288               group->meth->field_mod = &ec_GFp_nistp384_mod;
00289               group->meth->field_mul = &ec_GFp_nistp384_mul;
00290               group->meth->field_sqr = &ec_GFp_nistp384_sqr;
00291        }
00292        return MP_OKAY;
00293 }