/security/nss/lib/freebl/ecl/README

http://github.com/zpao/v8monkey · #! · 330 lines · 248 code · 82 blank · 0 comment · 0 complexity · 9cda4e12e40e17bf74a682a09be5a207 MD5 · raw file

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