security/nss/lib/freebl/ecl/README

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 ***** BEGIN LICENSE BLOCK *****
michael@0 2 Version: MPL 1.1/GPL 2.0/LGPL 2.1
michael@0 3
michael@0 4 The contents of this file are subject to the Mozilla Public License Version
michael@0 5 1.1 (the "License"); you may not use this file except in compliance with
michael@0 6 the License. You may obtain a copy of the License at
michael@0 7 http://www.mozilla.org/MPL/
michael@0 8
michael@0 9 Software distributed under the License is distributed on an "AS IS" basis,
michael@0 10 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
michael@0 11 for the specific language governing rights and limitations under the
michael@0 12 License.
michael@0 13
michael@0 14 The Original Code is the elliptic curve math library.
michael@0 15
michael@0 16 The Initial Developer of the Original Code is Sun Microsystems, Inc.
michael@0 17 Portions created by Sun Microsystems, Inc. are Copyright (C) 2003
michael@0 18 Sun Microsystems, Inc. All Rights Reserved.
michael@0 19
michael@0 20 Contributor(s):
michael@0 21 Stephen Fung <fungstep@hotmail.com> and
michael@0 22 Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
michael@0 23
michael@0 24 Alternatively, the contents of this file may be used under the terms of
michael@0 25 either the GNU General Public License Version 2 or later (the "GPL"), or
michael@0 26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
michael@0 27 in which case the provisions of the GPL or the LGPL are applicable instead
michael@0 28 of those above. If you wish to allow use of your version of this file only
michael@0 29 under the terms of either the GPL or the LGPL, and not to allow others to
michael@0 30 use your version of this file under the terms of the MPL, indicate your
michael@0 31 decision by deleting the provisions above and replace them with the notice
michael@0 32 and other provisions required by the GPL or the LGPL. If you do not delete
michael@0 33 the provisions above, a recipient may use your version of this file under
michael@0 34 the terms of any one of the MPL, the GPL or the LGPL.
michael@0 35
michael@0 36 ***** END LICENSE BLOCK *****
michael@0 37
michael@0 38 The ECL exposes routines for constructing and converting curve
michael@0 39 parameters for internal use.
michael@0 40
michael@0 41
michael@0 42 HEADER FILES
michael@0 43 ============
michael@0 44
michael@0 45 ecl-exp.h - Exports data structures and curve names. For use by code
michael@0 46 that does not have access to mp_ints.
michael@0 47
michael@0 48 ecl-curve.h - Provides hex encodings (in the form of ECCurveParams
michael@0 49 structs) of standardizes elliptic curve domain parameters and mappings
michael@0 50 from ECCurveName to ECCurveParams. For use by code that does not have
michael@0 51 access to mp_ints.
michael@0 52
michael@0 53 ecl.h - Interface to constructors for curve parameters and group object,
michael@0 54 and point multiplication operations. Used by higher level algorithms
michael@0 55 (like ECDH and ECDSA) to actually perform elliptic curve cryptography.
michael@0 56
michael@0 57 ecl-priv.h - Data structures and functions for internal use within the
michael@0 58 library.
michael@0 59
michael@0 60 ec2.h - Internal header file that contains all functions for point
michael@0 61 arithmetic over binary polynomial fields.
michael@0 62
michael@0 63 ecp.h - Internal header file that contains all functions for point
michael@0 64 arithmetic over prime fields.
michael@0 65
michael@0 66 DATA STRUCTURES AND TYPES
michael@0 67 =========================
michael@0 68
michael@0 69 ECCurveName (from ecl-exp.h) - Opaque name for standardized elliptic
michael@0 70 curve domain parameters.
michael@0 71
michael@0 72 ECCurveParams (from ecl-exp.h) - Provides hexadecimal encoding
michael@0 73 of elliptic curve domain parameters. Can be generated by a user
michael@0 74 and passed to ECGroup_fromHex or can be generated from a name by
michael@0 75 EC_GetNamedCurveParams. ecl-curve.h contains ECCurveParams structs for
michael@0 76 the standardized curves defined by ECCurveName.
michael@0 77
michael@0 78 ECGroup (from ecl.h and ecl-priv.h) - Opaque data structure that
michael@0 79 represents a group of elliptic curve points for a particular set of
michael@0 80 elliptic curve domain parameters. Contains all domain parameters (curve
michael@0 81 a and b, field, base point) as well as pointers to the functions that
michael@0 82 should be used for point arithmetic and the underlying field GFMethod.
michael@0 83 Generated by either ECGroup_fromHex or ECGroup_fromName.
michael@0 84
michael@0 85 GFMethod (from ecl-priv.h) - Represents a field underlying a set of
michael@0 86 elliptic curve domain parameters. Contains the irreducible that defines
michael@0 87 the field (either the prime or the binary polynomial) as well as
michael@0 88 pointers to the functions that should be used for field arithmetic.
michael@0 89
michael@0 90 ARITHMETIC FUNCTIONS
michael@0 91 ====================
michael@0 92
michael@0 93 Higher-level algorithms (like ECDH and ECDSA) should call ECPoint_mul
michael@0 94 or ECPoints_mul (from ecl.h) to do point arithmetic. These functions
michael@0 95 will choose which underlying algorithms to use, based on the ECGroup
michael@0 96 structure.
michael@0 97
michael@0 98 Point Multiplication
michael@0 99 --------------------
michael@0 100
michael@0 101 ecl_mult.c provides the ECPoints_mul and ECPoint_mul wrappers.
michael@0 102 It also provides two implementations for the pts_mul operation -
michael@0 103 ec_pts_mul_basic (which computes kP, lQ, and then adds kP + lQ) and
michael@0 104 ec_pts_mul_simul_w2 (which does a simultaneous point multiplication
michael@0 105 using a table with window size 2*2).
michael@0 106
michael@0 107 ec_naf.c provides an implementation of an algorithm to calculate a
michael@0 108 non-adjacent form of a scalar, minimizing the number of point
michael@0 109 additions that need to be done in a point multiplication.
michael@0 110
michael@0 111 Point Arithmetic over Prime Fields
michael@0 112 ----------------------------------
michael@0 113
michael@0 114 ecp_aff.c provides point arithmetic using affine coordinates.
michael@0 115
michael@0 116 ecp_jac.c provides point arithmetic using Jacobian projective
michael@0 117 coordinates and mixed Jacobian-affine coordinates. (Jacobian projective
michael@0 118 coordinates represent a point (x, y) as (X, Y, Z), where x=X/Z^2,
michael@0 119 y=Y/Z^3).
michael@0 120
michael@0 121 ecp_jm.c provides point arithmetic using Modified Jacobian
michael@0 122 coordinates and mixed Modified_Jacobian-affine coordinates.
michael@0 123 (Modified Jacobian coordinates represent a point (x, y)
michael@0 124 as (X, Y, Z, a*Z^4), where x=X/Z^2, y=Y/Z^3, and a is
michael@0 125 the linear coefficient in the curve defining equation).
michael@0 126
michael@0 127 ecp_192.c and ecp_224.c provide optimized field arithmetic.
michael@0 128
michael@0 129 Point Arithmetic over Binary Polynomial Fields
michael@0 130 ----------------------------------------------
michael@0 131
michael@0 132 ec2_aff.c provides point arithmetic using affine coordinates.
michael@0 133
michael@0 134 ec2_proj.c provides point arithmetic using projective coordinates.
michael@0 135 (Projective coordinates represent a point (x, y) as (X, Y, Z), where
michael@0 136 x=X/Z, y=Y/Z^2).
michael@0 137
michael@0 138 ec2_mont.c provides point multiplication using Montgomery projective
michael@0 139 coordinates.
michael@0 140
michael@0 141 ec2_163.c, ec2_193.c, and ec2_233.c provide optimized field arithmetic.
michael@0 142
michael@0 143 Field Arithmetic
michael@0 144 ----------------
michael@0 145
michael@0 146 ecl_gf.c provides constructors for field objects (GFMethod) with the
michael@0 147 functions GFMethod_cons*. It also provides wrappers around the basic
michael@0 148 field operations.
michael@0 149
michael@0 150 Prime Field Arithmetic
michael@0 151 ----------------------
michael@0 152
michael@0 153 The mpi library provides the basic prime field arithmetic.
michael@0 154
michael@0 155 ecp_mont.c provides wrappers around the Montgomery multiplication
michael@0 156 functions from the mpi library and adds encoding and decoding functions.
michael@0 157 It also provides the function to construct a GFMethod object using
michael@0 158 Montgomery multiplication.
michael@0 159
michael@0 160 ecp_192.c and ecp_224.c provide optimized modular reduction for the
michael@0 161 fields defined by nistp192 and nistp224 primes.
michael@0 162
michael@0 163 ecl_gf.c provides wrappers around the basic field operations.
michael@0 164
michael@0 165 Binary Polynomial Field Arithmetic
michael@0 166 ----------------------------------
michael@0 167
michael@0 168 ../mpi/mp_gf2m.c provides basic binary polynomial field arithmetic,
michael@0 169 including addition, multiplication, squaring, mod, and division, as well
michael@0 170 as conversion ob polynomial representations between bitstring and int[].
michael@0 171
michael@0 172 ec2_163.c, ec2_193.c, and ec2_233.c provide optimized field mod, mul,
michael@0 173 and sqr operations.
michael@0 174
michael@0 175 ecl_gf.c provides wrappers around the basic field operations.
michael@0 176
michael@0 177 Field Encoding
michael@0 178 --------------
michael@0 179
michael@0 180 By default, field elements are encoded in their basic form. It is
michael@0 181 possible to use an alternative encoding, however. For example, it is
michael@0 182 possible to Montgomery representation of prime field elements and
michael@0 183 take advantage of the fast modular multiplication that Montgomery
michael@0 184 representation provides. The process of converting from basic form to
michael@0 185 Montgomery representation is called field encoding, and the opposite
michael@0 186 process would be field decoding. All internal point operations assume
michael@0 187 that the operands are field encoded as appropriate. By rewiring the
michael@0 188 underlying field arithmetic to perform operations on these encoded
michael@0 189 values, the same overlying point arithmetic operations can be used
michael@0 190 regardless of field representation.
michael@0 191
michael@0 192 ALGORITHM WIRING
michael@0 193 ================
michael@0 194
michael@0 195 The EC library allows point and field arithmetic algorithms to be
michael@0 196 substituted ("wired-in") on a fine-grained basis. This allows for
michael@0 197 generic algorithms and algorithms that are optimized for a particular
michael@0 198 curve, field, or architecture, to coexist and to be automatically
michael@0 199 selected at runtime.
michael@0 200
michael@0 201 Wiring Mechanism
michael@0 202 ----------------
michael@0 203
michael@0 204 The ECGroup and GFMethod structure contain pointers to the point and
michael@0 205 field arithmetic functions, respectively, that are to be used in
michael@0 206 operations.
michael@0 207
michael@0 208 The selection of algorithms to use is handled in the function
michael@0 209 ecgroup_fromNameAndHex in ecl.c.
michael@0 210
michael@0 211 Default Wiring
michael@0 212 --------------
michael@0 213
michael@0 214 Curves over prime fields by default use montgomery field arithmetic,
michael@0 215 point multiplication using 5-bit window non-adjacent-form with
michael@0 216 Modified Jacobian coordinates, and 2*2-bit simultaneous point
michael@0 217 multiplication using Jacobian coordinates.
michael@0 218 (Wiring in function ECGroup_consGFp_mont in ecl.c.)
michael@0 219
michael@0 220 Curves over prime fields that have optimized modular reduction (i.e.,
michael@0 221 secp160r1, nistp192, and nistp224) do not use Montgomery field
michael@0 222 arithmetic. Instead, they use basic field arithmetic with their
michael@0 223 optimized reduction (as in ecp_192.c and ecp_224.c). They
michael@0 224 use the same point multiplication and simultaneous point multiplication
michael@0 225 algorithms as other curves over prime fields.
michael@0 226
michael@0 227 Curves over binary polynomial fields by default use generic field
michael@0 228 arithmetic with montgomery point multiplication and basic kP + lQ
michael@0 229 computation (multiply, multiply, and add). (Wiring in function
michael@0 230 ECGroup_cons_GF2m in ecl.c.)
michael@0 231
michael@0 232 Curves over binary polynomial fields that have optimized field
michael@0 233 arithmetic (i.e., any 163-, 193, or 233-bit field) use their optimized
michael@0 234 field arithmetic. They use the same point multiplication and
michael@0 235 simultaneous point multiplication algorithms as other curves over binary
michael@0 236 fields.
michael@0 237
michael@0 238 Example
michael@0 239 -------
michael@0 240
michael@0 241 We provide an example for plugging in an optimized implementation for
michael@0 242 the Koblitz curve nistk163.
michael@0 243
michael@0 244 Suppose the file ec2_k163.c contains the optimized implementation. In
michael@0 245 particular it contains a point multiplication function:
michael@0 246
michael@0 247 mp_err ec_GF2m_nistk163_pt_mul(const mp_int *n, const mp_int *px,
michael@0 248 const mp_int *py, mp_int *rx, mp_int *ry, const ECGroup *group);
michael@0 249
michael@0 250 Since only a pt_mul function is provided, the generic pt_add function
michael@0 251 will be used.
michael@0 252
michael@0 253 There are two options for handling the optimized field arithmetic used
michael@0 254 by the ..._pt_mul function. Say the optimized field arithmetic includes
michael@0 255 the following functions:
michael@0 256
michael@0 257 mp_err ec_GF2m_nistk163_add(const mp_int *a, const mp_int *b,
michael@0 258 mp_int *r, const GFMethod *meth);
michael@0 259 mp_err ec_GF2m_nistk163_mul(const mp_int *a, const mp_int *b,
michael@0 260 mp_int *r, const GFMethod *meth);
michael@0 261 mp_err ec_GF2m_nistk163_sqr(const mp_int *a, const mp_int *b,
michael@0 262 mp_int *r, const GFMethod *meth);
michael@0 263 mp_err ec_GF2m_nistk163_div(const mp_int *a, const mp_int *b,
michael@0 264 mp_int *r, const GFMethod *meth);
michael@0 265
michael@0 266 First, the optimized field arithmetic could simply be called directly
michael@0 267 by the ..._pt_mul function. This would be accomplished by changing
michael@0 268 the ecgroup_fromNameAndHex function in ecl.c to include the following
michael@0 269 statements:
michael@0 270
michael@0 271 if (name == ECCurve_NIST_K163) {
michael@0 272 group = ECGroup_consGF2m(&irr, NULL, &curvea, &curveb, &genx,
michael@0 273 &geny, &order, params->cofactor);
michael@0 274 if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
michael@0 275 MP_CHECKOK( ec_group_set_nistk163(group) );
michael@0 276 }
michael@0 277
michael@0 278 and including in ec2_k163.c the following function:
michael@0 279
michael@0 280 mp_err ec_group_set_nistk163(ECGroup *group) {
michael@0 281 group->point_mul = &ec_GF2m_nistk163_pt_mul;
michael@0 282 return MP_OKAY;
michael@0 283 }
michael@0 284
michael@0 285 As a result, ec_GF2m_pt_add and similar functions would use the
michael@0 286 basic binary polynomial field arithmetic ec_GF2m_add, ec_GF2m_mul,
michael@0 287 ec_GF2m_sqr, and ec_GF2m_div.
michael@0 288
michael@0 289 Alternatively, the optimized field arithmetic could be wired into the
michael@0 290 group's GFMethod. This would be accomplished by putting the following
michael@0 291 function in ec2_k163.c:
michael@0 292
michael@0 293 mp_err ec_group_set_nistk163(ECGroup *group) {
michael@0 294 group->meth->field_add = &ec_GF2m_nistk163_add;
michael@0 295 group->meth->field_mul = &ec_GF2m_nistk163_mul;
michael@0 296 group->meth->field_sqr = &ec_GF2m_nistk163_sqr;
michael@0 297 group->meth->field_div = &ec_GF2m_nistk163_div;
michael@0 298 group->point_mul = &ec_GF2m_nistk163_pt_mul;
michael@0 299 return MP_OKAY;
michael@0 300 }
michael@0 301
michael@0 302 For an example of functions that use special field encodings, take a
michael@0 303 look at ecp_mont.c.
michael@0 304
michael@0 305 TESTING
michael@0 306 =======
michael@0 307
michael@0 308 The ecl/tests directory contains a collection of standalone tests that
michael@0 309 verify the correctness of the elliptic curve library.
michael@0 310
michael@0 311 Both ecp_test and ec2_test take the following arguments:
michael@0 312
michael@0 313 --print Print out results of each point arithmetic test.
michael@0 314 --time Benchmark point operations and print results.
michael@0 315
michael@0 316 The set of curves over which ecp_test and ec2_test run is coded into the
michael@0 317 program, but can be changed by editing the source files.
michael@0 318
michael@0 319 BUILDING
michael@0 320 ========
michael@0 321
michael@0 322 The ecl can be built as a standalone library, separate from NSS,
michael@0 323 dependent only on the mpi library. To build the library:
michael@0 324
michael@0 325 > cd ../mpi
michael@0 326 > make libs
michael@0 327 > cd ../ecl
michael@0 328 > make libs
michael@0 329 > make tests # to build test files
michael@0 330 > make test # to run automated tests

mercurial