1 /* ecparam-1.0.0.js (c) 2013 Kenji Urushima | kjur.github.com/jsrsasign/license
  2  */
  3 /*
  4  * ecparam.js - Elliptic Curve Cryptography Curve Parameter Definition class
  5  *
  6  * Copyright (c) 2013 Kenji Urushima (kenji.urushima@gmail.com)
  7  *
  8  * This software is licensed under the terms of the MIT License.
  9  * https://kjur.github.io/jsrsasign/license
 10  *
 11  * The above copyright and license notice shall be 
 12  * included in all copies or substantial portions of the Software.
 13  */
 14 
 15 /**
 16  * @fileOverview
 17  * @name ecparam-1.1.js
 18  * @author Kenji Urushima kenji.urushima@gmail.com
 19  * @version 1.0.0 (2013-Jul-17)
 20  * @since jsrsasign 4.0
 21  * @license <a href="https://kjur.github.io/jsrsasign/license/">MIT License</a>
 22  */
 23 
 24 if (typeof KJUR == "undefined" || !KJUR) KJUR = {};
 25 if (typeof KJUR.crypto == "undefined" || !KJUR.crypto) KJUR.crypto = {};
 26 
 27 /**
 28  * static object for elliptic curve names and parameters
 29  * @name KJUR.crypto.ECParameterDB
 30  * @class static object for elliptic curve names and parameters
 31  * @description
 32  * This class provides parameters for named elliptic curves.
 33  * Currently it supoprts following curve names and aliases however 
 34  * the name marked (*) are available for {@link KJUR.crypto.ECDSA} and
 35  * {@link KJUR.crypto.Signature} classes.
 36  * <ul>
 37  * <li>secp128r1</li>
 38  * <li>secp160r1</li>
 39  * <li>secp160k1</li>
 40  * <li>secp192r1</li>
 41  * <li>secp192k1</li>
 42  * <li>secp224r1</li>
 43  * <li>secp256r1, NIST P-256, P-256, prime256v1 (*)</li>
 44  * <li>secp256k1 (*)</li>
 45  * <li>secp384r1, NIST P-384, P-384 (*)</li>
 46  * <li>secp521r1, NIST P-521, P-521</li>
 47  * </ul>
 48  * You can register new curves by using 'register' method.
 49  */
 50 KJUR.crypto.ECParameterDB = new function() {
 51     var db = {};
 52     var aliasDB = {};
 53 
 54     function hex2bi(hex) {
 55         return new BigInteger(hex, 16);
 56     }
 57     
 58     /**
 59      * get curve inforamtion associative array for curve name or alias
 60      * @name getByName
 61      * @memberOf KJUR.crypto.ECParameterDB
 62      * @function
 63      * @param {String} nameOrAlias curve name or alias name
 64      * @return {Array} associative array of curve parameters
 65      * @example
 66      * var param = KJUR.crypto.ECParameterDB.getByName('prime256v1');
 67      * var keylen = param['keylen'];
 68      * var n = param['n'];
 69      */
 70     this.getByName = function(nameOrAlias) {
 71 	var name = nameOrAlias;
 72 	if (typeof aliasDB[name] != "undefined") {
 73 	    name = aliasDB[nameOrAlias];
 74         }
 75 	if (typeof db[name] != "undefined") {
 76 	    return db[name];
 77 	}
 78 	throw "unregistered EC curve name: " + name;
 79     };
 80 
 81     /**
 82      * register new curve
 83      * @name regist
 84      * @memberOf KJUR.crypto.ECParameterDB
 85      * @function
 86      * @param {String} name name of curve
 87      * @param {Integer} keylen key length
 88      * @param {String} pHex hexadecimal value of p
 89      * @param {String} aHex hexadecimal value of a
 90      * @param {String} bHex hexadecimal value of b
 91      * @param {String} nHex hexadecimal value of n
 92      * @param {String} hHex hexadecimal value of h
 93      * @param {String} gxHex hexadecimal value of Gx
 94      * @param {String} gyHex hexadecimal value of Gy
 95      * @param {Array} aliasList array of string for curve names aliases
 96      * @param {String} oid Object Identifier for the curve
 97      * @param {String} info information string for the curve
 98      */
 99     this.regist = function(name, keylen, pHex, aHex, bHex, nHex, hHex, gxHex, gyHex, aliasList, oid, info) {
100         db[name] = {};
101 	var p = hex2bi(pHex);
102 	var a = hex2bi(aHex);
103 	var b = hex2bi(bHex);
104 	var n = hex2bi(nHex);
105 	var h = hex2bi(hHex);
106         var curve = new ECCurveFp(p, a, b);
107         var G = curve.decodePointHex("04" + gxHex + gyHex);
108 	db[name]['name'] = name;
109 	db[name]['keylen'] = keylen;
110         db[name]['curve'] = curve;
111         db[name]['G'] = G;
112         db[name]['n'] = n;
113         db[name]['h'] = h;
114         db[name]['oid'] = oid;
115         db[name]['info'] = info;
116 
117         for (var i = 0; i < aliasList.length; i++) {
118 	    aliasDB[aliasList[i]] = name;
119         }
120     };
121 };
122 
123 KJUR.crypto.ECParameterDB.regist(
124   "secp128r1", // name / p = 2^128 - 2^97 - 1
125   128,
126   "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF", // p
127   "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC", // a
128   "E87579C11079F43DD824993C2CEE5ED3", // b
129   "FFFFFFFE0000000075A30D1B9038A115", // n
130   "1", // h
131   "161FF7528B899B2D0C28607CA52C5B86", // gx
132   "CF5AC8395BAFEB13C02DA292DDED7A83", // gy
133   [], // alias
134   "", // oid (underconstruction)
135   "secp128r1 : SECG curve over a 128 bit prime field"); // info
136 
137 KJUR.crypto.ECParameterDB.regist(
138   "secp160k1", // name / p = 2^160 - 2^32 - 2^14 - 2^12 - 2^9 - 2^8 - 2^7 - 2^3 - 2^2 - 1
139   160,
140   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73", // p
141   "0", // a
142   "7", // b
143   "0100000000000000000001B8FA16DFAB9ACA16B6B3", // n
144   "1", // h
145   "3B4C382CE37AA192A4019E763036F4F5DD4D7EBB", // gx
146   "938CF935318FDCED6BC28286531733C3F03C4FEE", // gy
147   [], // alias
148   "", // oid
149   "secp160k1 : SECG curve over a 160 bit prime field"); // info
150 
151 KJUR.crypto.ECParameterDB.regist(
152   "secp160r1", // name / p = 2^160 - 2^31 - 1
153   160,
154   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF", // p
155   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC", // a
156   "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45", // b
157   "0100000000000000000001F4C8F927AED3CA752257", // n
158   "1", // h
159   "4A96B5688EF573284664698968C38BB913CBFC82", // gx
160   "23A628553168947D59DCC912042351377AC5FB32", // gy
161   [], // alias
162   "", // oid
163   "secp160r1 : SECG curve over a 160 bit prime field"); // info
164 
165 KJUR.crypto.ECParameterDB.regist(
166   "secp192k1", // name / p = 2^192 - 2^32 - 2^12 - 2^8 - 2^7 - 2^6 - 2^3 - 1
167   192,
168   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37", // p
169   "0", // a
170   "3", // b
171   "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D", // n
172   "1", // h
173   "DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D", // gx
174   "9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D", // gy
175   []); // alias
176 
177 KJUR.crypto.ECParameterDB.regist(
178   "secp192r1", // name / p = 2^192 - 2^64 - 1
179   192,
180   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", // p
181   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC", // a
182   "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1", // b
183   "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", // n
184   "1", // h
185   "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012", // gx
186   "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811", // gy
187   []); // alias
188 
189 KJUR.crypto.ECParameterDB.regist(
190   "secp224r1", // name / p = 2^224 - 2^96 + 1
191   224,
192   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", // p
193   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", // a
194   "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4", // b
195   "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D", // n
196   "1", // h
197   "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21", // gx
198   "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34", // gy
199   []); // alias
200 
201 KJUR.crypto.ECParameterDB.regist(
202   "secp256k1", // name / p = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1
203   256,
204   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", // p
205   "0", // a
206   "7", // b
207   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", // n
208   "1", // h
209   "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", // gx
210   "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", // gy
211   []); // alias
212 
213 KJUR.crypto.ECParameterDB.regist(
214   "secp256r1", // name / p = 2^224 (2^32 - 1) + 2^192 + 2^96 - 1
215   256,
216   "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", // p
217   "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC", // a
218   "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B", // b
219   "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551", // n
220   "1", // h
221   "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296", // gx
222   "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5", // gy
223   ["NIST P-256", "P-256", "prime256v1"]); // alias
224 
225 KJUR.crypto.ECParameterDB.regist(
226   "secp384r1", // name
227   384,
228   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF", // p
229   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC", // a
230   "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF", // b
231   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973", // n
232   "1", // h
233   "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7", // gx
234   "3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f", // gy
235   ["NIST P-384", "P-384"]); // alias
236 
237 KJUR.crypto.ECParameterDB.regist(
238   "secp521r1", // name
239   521,
240   "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", // p
241   "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", // a
242   "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00", // b
243   "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409", // n
244   "1", // h
245   "C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66", // gx
246   "011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", // gy
247   ["NIST P-521", "P-521"]); // alias
248 
249