sec.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Named EC curves
  2. // Requires ec.js, jsbn.js, and jsbn2.js
  3. var BigInteger = require('jsbn').BigInteger
  4. var ECCurveFp = require('./ec.js').ECCurveFp
  5. // ----------------
  6. // X9ECParameters
  7. // constructor
  8. function X9ECParameters(curve,g,n,h) {
  9. this.curve = curve;
  10. this.g = g;
  11. this.n = n;
  12. this.h = h;
  13. }
  14. function x9getCurve() {
  15. return this.curve;
  16. }
  17. function x9getG() {
  18. return this.g;
  19. }
  20. function x9getN() {
  21. return this.n;
  22. }
  23. function x9getH() {
  24. return this.h;
  25. }
  26. X9ECParameters.prototype.getCurve = x9getCurve;
  27. X9ECParameters.prototype.getG = x9getG;
  28. X9ECParameters.prototype.getN = x9getN;
  29. X9ECParameters.prototype.getH = x9getH;
  30. // ----------------
  31. // SECNamedCurves
  32. function fromHex(s) { return new BigInteger(s, 16); }
  33. function secp128r1() {
  34. // p = 2^128 - 2^97 - 1
  35. var p = fromHex("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF");
  36. var a = fromHex("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC");
  37. var b = fromHex("E87579C11079F43DD824993C2CEE5ED3");
  38. //byte[] S = Hex.decode("000E0D4D696E6768756151750CC03A4473D03679");
  39. var n = fromHex("FFFFFFFE0000000075A30D1B9038A115");
  40. var h = BigInteger.ONE;
  41. var curve = new ECCurveFp(p, a, b);
  42. var G = curve.decodePointHex("04"
  43. + "161FF7528B899B2D0C28607CA52C5B86"
  44. + "CF5AC8395BAFEB13C02DA292DDED7A83");
  45. return new X9ECParameters(curve, G, n, h);
  46. }
  47. function secp160k1() {
  48. // p = 2^160 - 2^32 - 2^14 - 2^12 - 2^9 - 2^8 - 2^7 - 2^3 - 2^2 - 1
  49. var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73");
  50. var a = BigInteger.ZERO;
  51. var b = fromHex("7");
  52. //byte[] S = null;
  53. var n = fromHex("0100000000000000000001B8FA16DFAB9ACA16B6B3");
  54. var h = BigInteger.ONE;
  55. var curve = new ECCurveFp(p, a, b);
  56. var G = curve.decodePointHex("04"
  57. + "3B4C382CE37AA192A4019E763036F4F5DD4D7EBB"
  58. + "938CF935318FDCED6BC28286531733C3F03C4FEE");
  59. return new X9ECParameters(curve, G, n, h);
  60. }
  61. function secp160r1() {
  62. // p = 2^160 - 2^31 - 1
  63. var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF");
  64. var a = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC");
  65. var b = fromHex("1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45");
  66. //byte[] S = Hex.decode("1053CDE42C14D696E67687561517533BF3F83345");
  67. var n = fromHex("0100000000000000000001F4C8F927AED3CA752257");
  68. var h = BigInteger.ONE;
  69. var curve = new ECCurveFp(p, a, b);
  70. var G = curve.decodePointHex("04"
  71. + "4A96B5688EF573284664698968C38BB913CBFC82"
  72. + "23A628553168947D59DCC912042351377AC5FB32");
  73. return new X9ECParameters(curve, G, n, h);
  74. }
  75. function secp192k1() {
  76. // p = 2^192 - 2^32 - 2^12 - 2^8 - 2^7 - 2^6 - 2^3 - 1
  77. var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37");
  78. var a = BigInteger.ZERO;
  79. var b = fromHex("3");
  80. //byte[] S = null;
  81. var n = fromHex("FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D");
  82. var h = BigInteger.ONE;
  83. var curve = new ECCurveFp(p, a, b);
  84. var G = curve.decodePointHex("04"
  85. + "DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D"
  86. + "9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D");
  87. return new X9ECParameters(curve, G, n, h);
  88. }
  89. function secp192r1() {
  90. // p = 2^192 - 2^64 - 1
  91. var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF");
  92. var a = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC");
  93. var b = fromHex("64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1");
  94. //byte[] S = Hex.decode("3045AE6FC8422F64ED579528D38120EAE12196D5");
  95. var n = fromHex("FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831");
  96. var h = BigInteger.ONE;
  97. var curve = new ECCurveFp(p, a, b);
  98. var G = curve.decodePointHex("04"
  99. + "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
  100. + "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811");
  101. return new X9ECParameters(curve, G, n, h);
  102. }
  103. function secp224r1() {
  104. // p = 2^224 - 2^96 + 1
  105. var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001");
  106. var a = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE");
  107. var b = fromHex("B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4");
  108. //byte[] S = Hex.decode("BD71344799D5C7FCDC45B59FA3B9AB8F6A948BC5");
  109. var n = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D");
  110. var h = BigInteger.ONE;
  111. var curve = new ECCurveFp(p, a, b);
  112. var G = curve.decodePointHex("04"
  113. + "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
  114. + "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34");
  115. return new X9ECParameters(curve, G, n, h);
  116. }
  117. function secp256r1() {
  118. // p = 2^224 (2^32 - 1) + 2^192 + 2^96 - 1
  119. var p = fromHex("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
  120. var a = fromHex("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
  121. var b = fromHex("5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
  122. //byte[] S = Hex.decode("C49D360886E704936A6678E1139D26B7819F7E90");
  123. var n = fromHex("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
  124. var h = BigInteger.ONE;
  125. var curve = new ECCurveFp(p, a, b);
  126. var G = curve.decodePointHex("04"
  127. + "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
  128. + "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
  129. return new X9ECParameters(curve, G, n, h);
  130. }
  131. // TODO: make this into a proper hashtable
  132. function getSECCurveByName(name) {
  133. if(name == "secp128r1") return secp128r1();
  134. if(name == "secp160k1") return secp160k1();
  135. if(name == "secp160r1") return secp160r1();
  136. if(name == "secp192k1") return secp192k1();
  137. if(name == "secp192r1") return secp192r1();
  138. if(name == "secp224r1") return secp224r1();
  139. if(name == "secp256r1") return secp256r1();
  140. return null;
  141. }
  142. module.exports = {
  143. "secp128r1":secp128r1,
  144. "secp160k1":secp160k1,
  145. "secp160r1":secp160r1,
  146. "secp192k1":secp192k1,
  147. "secp192r1":secp192r1,
  148. "secp224r1":secp224r1,
  149. "secp256r1":secp256r1
  150. }