CustomNamedCurves.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using Org.BouncyCastle.Asn1;
  5. using Org.BouncyCastle.Asn1.Sec;
  6. using Org.BouncyCastle.Asn1.X9;
  7. using Org.BouncyCastle.Math;
  8. using Org.BouncyCastle.Math.EC;
  9. using Org.BouncyCastle.Math.EC.Custom.Djb;
  10. using Org.BouncyCastle.Math.EC.Custom.Sec;
  11. using Org.BouncyCastle.Math.EC.Endo;
  12. using Org.BouncyCastle.Utilities;
  13. using Org.BouncyCastle.Utilities.Collections;
  14. using Org.BouncyCastle.Utilities.Encoders;
  15. namespace Org.BouncyCastle.Crypto.EC
  16. {
  17. public sealed class CustomNamedCurves
  18. {
  19. private CustomNamedCurves()
  20. {
  21. }
  22. private static BigInteger FromHex(string hex)
  23. {
  24. return new BigInteger(1, Hex.Decode(hex));
  25. }
  26. private static ECCurve ConfigureCurve(ECCurve curve)
  27. {
  28. return curve;
  29. }
  30. private static ECCurve ConfigureCurveGlv(ECCurve c, GlvTypeBParameters p)
  31. {
  32. return c.Configure().SetEndomorphism(new GlvTypeBEndomorphism(c, p)).Create();
  33. }
  34. /*
  35. * curve25519
  36. */
  37. internal class Curve25519Holder
  38. : X9ECParametersHolder
  39. {
  40. private Curve25519Holder() { }
  41. internal static readonly X9ECParametersHolder Instance = new Curve25519Holder();
  42. protected override X9ECParameters CreateParameters()
  43. {
  44. byte[] S = null;
  45. ECCurve curve = ConfigureCurve(new Curve25519());
  46. /*
  47. * NOTE: Curve25519 was specified in Montgomery form. Rewriting in Weierstrass form
  48. * involves substitution of variables, so the base-point x coordinate is 9 + (486662 / 3).
  49. *
  50. * The Curve25519 paper doesn't say which of the two possible y values the base
  51. * point has. The choice here is guided by language in the Ed25519 paper.
  52. *
  53. * (The other possible y value is 5F51E65E475F794B1FE122D388B72EB36DC2B28192839E4DD6163A5D81312C14)
  54. */
  55. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  56. + "2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD245A"
  57. + "20AE19A1B8A086B4E01EDD2C7748D14C923D4D7E6D7C61B229E9C5A27ECED3D9"));
  58. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  59. }
  60. }
  61. /*
  62. * secp128r1
  63. */
  64. internal class SecP128R1Holder
  65. : X9ECParametersHolder
  66. {
  67. private SecP128R1Holder() { }
  68. internal static readonly X9ECParametersHolder Instance = new SecP128R1Holder();
  69. protected override X9ECParameters CreateParameters()
  70. {
  71. byte[] S = Hex.Decode("000E0D4D696E6768756151750CC03A4473D03679");
  72. ECCurve curve = ConfigureCurve(new SecP128R1Curve());
  73. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  74. + "161FF7528B899B2D0C28607CA52C5B86"
  75. + "CF5AC8395BAFEB13C02DA292DDED7A83"));
  76. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  77. }
  78. };
  79. /*
  80. * secp160k1
  81. */
  82. internal class SecP160K1Holder
  83. : X9ECParametersHolder
  84. {
  85. private SecP160K1Holder() { }
  86. internal static readonly X9ECParametersHolder Instance = new SecP160K1Holder();
  87. protected override X9ECParameters CreateParameters()
  88. {
  89. byte[] S = null;
  90. GlvTypeBParameters glv = new GlvTypeBParameters(
  91. new BigInteger("9ba48cba5ebcb9b6bd33b92830b2a2e0e192f10a", 16),
  92. new BigInteger("c39c6c3b3a36d7701b9c71a1f5804ae5d0003f4", 16),
  93. new BigInteger[]{
  94. new BigInteger("9162fbe73984472a0a9e", 16),
  95. new BigInteger("-96341f1138933bc2f505", 16) },
  96. new BigInteger[]{
  97. new BigInteger("127971af8721782ecffa3", 16),
  98. new BigInteger("9162fbe73984472a0a9e", 16) },
  99. new BigInteger("9162fbe73984472a0a9d0590", 16),
  100. new BigInteger("96341f1138933bc2f503fd44", 16),
  101. 176);
  102. ECCurve curve = ConfigureCurveGlv(new SecP160K1Curve(), glv);
  103. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  104. + "3B4C382CE37AA192A4019E763036F4F5DD4D7EBB"
  105. + "938CF935318FDCED6BC28286531733C3F03C4FEE"));
  106. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  107. }
  108. };
  109. /*
  110. * secp160r1
  111. */
  112. internal class SecP160R1Holder
  113. : X9ECParametersHolder
  114. {
  115. private SecP160R1Holder() { }
  116. internal static readonly X9ECParametersHolder Instance = new SecP160R1Holder();
  117. protected override X9ECParameters CreateParameters()
  118. {
  119. byte[] S = Hex.Decode("1053CDE42C14D696E67687561517533BF3F83345");
  120. ECCurve curve = ConfigureCurve(new SecP160R1Curve());
  121. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  122. + "4A96B5688EF573284664698968C38BB913CBFC82"
  123. + "23A628553168947D59DCC912042351377AC5FB32"));
  124. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  125. }
  126. };
  127. /*
  128. * secp160r2
  129. */
  130. internal class SecP160R2Holder
  131. : X9ECParametersHolder
  132. {
  133. private SecP160R2Holder() { }
  134. internal static readonly X9ECParametersHolder Instance = new SecP160R2Holder();
  135. protected override X9ECParameters CreateParameters()
  136. {
  137. byte[] S = Hex.Decode("B99B99B099B323E02709A4D696E6768756151751");
  138. ECCurve curve = ConfigureCurve(new SecP160R2Curve());
  139. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  140. + "52DCB034293A117E1F4FF11B30F7199D3144CE6D"
  141. + "FEAFFEF2E331F296E071FA0DF9982CFEA7D43F2E"));
  142. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  143. }
  144. };
  145. /*
  146. * secp192k1
  147. */
  148. internal class SecP192K1Holder
  149. : X9ECParametersHolder
  150. {
  151. private SecP192K1Holder() { }
  152. internal static readonly X9ECParametersHolder Instance = new SecP192K1Holder();
  153. protected override X9ECParameters CreateParameters()
  154. {
  155. byte[] S = null;
  156. GlvTypeBParameters glv = new GlvTypeBParameters(
  157. new BigInteger("bb85691939b869c1d087f601554b96b80cb4f55b35f433c2", 16),
  158. new BigInteger("3d84f26c12238d7b4f3d516613c1759033b1a5800175d0b1", 16),
  159. new BigInteger[]{
  160. new BigInteger("71169be7330b3038edb025f1", 16),
  161. new BigInteger("-b3fb3400dec5c4adceb8655c", 16) },
  162. new BigInteger[]{
  163. new BigInteger("12511cfe811d0f4e6bc688b4d", 16),
  164. new BigInteger("71169be7330b3038edb025f1", 16) },
  165. new BigInteger("71169be7330b3038edb025f1d0f9", 16),
  166. new BigInteger("b3fb3400dec5c4adceb8655d4c94", 16),
  167. 208);
  168. ECCurve curve = ConfigureCurveGlv(new SecP192K1Curve(), glv);
  169. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  170. + "DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D"
  171. + "9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D"));
  172. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  173. }
  174. }
  175. /*
  176. * secp192r1
  177. */
  178. internal class SecP192R1Holder
  179. : X9ECParametersHolder
  180. {
  181. private SecP192R1Holder() { }
  182. internal static readonly X9ECParametersHolder Instance = new SecP192R1Holder();
  183. protected override X9ECParameters CreateParameters()
  184. {
  185. byte[] S = Hex.Decode("3045AE6FC8422F64ED579528D38120EAE12196D5");
  186. ECCurve curve = ConfigureCurve(new SecP192R1Curve());
  187. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  188. + "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
  189. + "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"));
  190. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  191. }
  192. }
  193. /*
  194. * secp224k1
  195. */
  196. internal class SecP224K1Holder
  197. : X9ECParametersHolder
  198. {
  199. private SecP224K1Holder() { }
  200. internal static readonly X9ECParametersHolder Instance = new SecP224K1Holder();
  201. protected override X9ECParameters CreateParameters()
  202. {
  203. byte[] S = null;
  204. GlvTypeBParameters glv = new GlvTypeBParameters(
  205. new BigInteger("fe0e87005b4e83761908c5131d552a850b3f58b749c37cf5b84d6768", 16),
  206. new BigInteger("60dcd2104c4cbc0be6eeefc2bdd610739ec34e317f9b33046c9e4788", 16),
  207. new BigInteger[]{
  208. new BigInteger("6b8cf07d4ca75c88957d9d670591", 16),
  209. new BigInteger("-b8adf1378a6eb73409fa6c9c637d", 16) },
  210. new BigInteger[]{
  211. new BigInteger("1243ae1b4d71613bc9f780a03690e", 16),
  212. new BigInteger("6b8cf07d4ca75c88957d9d670591", 16) },
  213. new BigInteger("6b8cf07d4ca75c88957d9d67059037a4", 16),
  214. new BigInteger("b8adf1378a6eb73409fa6c9c637ba7f5", 16),
  215. 240);
  216. ECCurve curve = ConfigureCurveGlv(new SecP224K1Curve(), glv);
  217. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  218. + "A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C"
  219. + "7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5"));
  220. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  221. }
  222. }
  223. /*
  224. * secp224r1
  225. */
  226. internal class SecP224R1Holder
  227. : X9ECParametersHolder
  228. {
  229. private SecP224R1Holder() { }
  230. internal static readonly X9ECParametersHolder Instance = new SecP224R1Holder();
  231. protected override X9ECParameters CreateParameters()
  232. {
  233. byte[] S = Hex.Decode("BD71344799D5C7FCDC45B59FA3B9AB8F6A948BC5");
  234. ECCurve curve = ConfigureCurve(new SecP224R1Curve());
  235. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  236. + "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
  237. + "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"));
  238. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  239. }
  240. }
  241. /*
  242. * secp256k1
  243. */
  244. internal class SecP256K1Holder
  245. : X9ECParametersHolder
  246. {
  247. private SecP256K1Holder() {}
  248. internal static readonly X9ECParametersHolder Instance = new SecP256K1Holder();
  249. protected override X9ECParameters CreateParameters()
  250. {
  251. byte[] S = null;
  252. GlvTypeBParameters glv = new GlvTypeBParameters(
  253. new BigInteger("7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee", 16),
  254. new BigInteger("5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72", 16),
  255. new BigInteger[]{
  256. new BigInteger("3086d221a7d46bcde86c90e49284eb15", 16),
  257. new BigInteger("-e4437ed6010e88286f547fa90abfe4c3", 16) },
  258. new BigInteger[]{
  259. new BigInteger("114ca50f7a8e2f3f657c1108d9d44cfd8", 16),
  260. new BigInteger("3086d221a7d46bcde86c90e49284eb15", 16) },
  261. new BigInteger("3086d221a7d46bcde86c90e49284eb153dab", 16),
  262. new BigInteger("e4437ed6010e88286f547fa90abfe4c42212", 16),
  263. 272);
  264. ECCurve curve = ConfigureCurveGlv(new SecP256K1Curve(), glv);
  265. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  266. + "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"
  267. + "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"));
  268. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  269. }
  270. }
  271. /*
  272. * secp256r1
  273. */
  274. internal class SecP256R1Holder
  275. : X9ECParametersHolder
  276. {
  277. private SecP256R1Holder() {}
  278. internal static readonly X9ECParametersHolder Instance = new SecP256R1Holder();
  279. protected override X9ECParameters CreateParameters()
  280. {
  281. byte[] S = Hex.Decode("C49D360886E704936A6678E1139D26B7819F7E90");
  282. ECCurve curve = ConfigureCurve(new SecP256R1Curve());
  283. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  284. + "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
  285. + "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"));
  286. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  287. }
  288. }
  289. /*
  290. * secp384r1
  291. */
  292. internal class SecP384R1Holder
  293. : X9ECParametersHolder
  294. {
  295. private SecP384R1Holder() { }
  296. internal static readonly X9ECParametersHolder Instance = new SecP384R1Holder();
  297. protected override X9ECParameters CreateParameters()
  298. {
  299. byte[] S = Hex.Decode("A335926AA319A27A1D00896A6773A4827ACDAC73");
  300. ECCurve curve = ConfigureCurve(new SecP384R1Curve());
  301. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  302. + "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7"
  303. + "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"));
  304. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  305. }
  306. }
  307. /*
  308. * secp521r1
  309. */
  310. internal class SecP521R1Holder
  311. : X9ECParametersHolder
  312. {
  313. private SecP521R1Holder() { }
  314. internal static readonly X9ECParametersHolder Instance = new SecP521R1Holder();
  315. protected override X9ECParameters CreateParameters()
  316. {
  317. byte[] S = Hex.Decode("D09E8800291CB85396CC6717393284AAA0DA64BA");
  318. ECCurve curve = ConfigureCurve(new SecP521R1Curve());
  319. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  320. + "00C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
  321. + "011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650"));
  322. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  323. }
  324. }
  325. /*
  326. * sect113r1
  327. */
  328. internal class SecT113R1Holder
  329. : X9ECParametersHolder
  330. {
  331. private SecT113R1Holder() { }
  332. internal static readonly X9ECParametersHolder Instance = new SecT113R1Holder();
  333. protected override X9ECParameters CreateParameters()
  334. {
  335. byte[] S = Hex.Decode("10E723AB14D696E6768756151756FEBF8FCB49A9");
  336. ECCurve curve = ConfigureCurve(new SecT113R1Curve());
  337. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  338. + "009D73616F35F4AB1407D73562C10F"
  339. + "00A52830277958EE84D1315ED31886"));
  340. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  341. }
  342. };
  343. /*
  344. * sect113r2
  345. */
  346. internal class SecT113R2Holder
  347. : X9ECParametersHolder
  348. {
  349. private SecT113R2Holder() { }
  350. internal static readonly X9ECParametersHolder Instance = new SecT113R2Holder();
  351. protected override X9ECParameters CreateParameters()
  352. {
  353. byte[] S = Hex.Decode("10C0FB15760860DEF1EEF4D696E676875615175D");
  354. ECCurve curve = ConfigureCurve(new SecT113R2Curve());
  355. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  356. + "01A57A6A7B26CA5EF52FCDB8164797"
  357. + "00B3ADC94ED1FE674C06E695BABA1D"));
  358. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  359. }
  360. };
  361. /*
  362. * sect131r1
  363. */
  364. internal class SecT131R1Holder
  365. : X9ECParametersHolder
  366. {
  367. private SecT131R1Holder() { }
  368. internal static readonly X9ECParametersHolder Instance = new SecT131R1Holder();
  369. protected override X9ECParameters CreateParameters()
  370. {
  371. byte[] S = Hex.Decode("4D696E676875615175985BD3ADBADA21B43A97E2");
  372. ECCurve curve = ConfigureCurve(new SecT131R1Curve());
  373. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  374. + "0081BAF91FDF9833C40F9C181343638399"
  375. + "078C6E7EA38C001F73C8134B1B4EF9E150"));
  376. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  377. }
  378. };
  379. /*
  380. * sect131r2
  381. */
  382. internal class SecT131R2Holder
  383. : X9ECParametersHolder
  384. {
  385. private SecT131R2Holder() { }
  386. internal static readonly X9ECParametersHolder Instance = new SecT131R2Holder();
  387. protected override X9ECParameters CreateParameters()
  388. {
  389. byte[] S = Hex.Decode("985BD3ADBAD4D696E676875615175A21B43A97E3");
  390. ECCurve curve = ConfigureCurve(new SecT131R2Curve());
  391. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  392. + "0356DCD8F2F95031AD652D23951BB366A8"
  393. + "0648F06D867940A5366D9E265DE9EB240F"));
  394. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  395. }
  396. };
  397. /*
  398. * sect163k1
  399. */
  400. internal class SecT163K1Holder
  401. : X9ECParametersHolder
  402. {
  403. private SecT163K1Holder() { }
  404. internal static readonly X9ECParametersHolder Instance = new SecT163K1Holder();
  405. protected override X9ECParameters CreateParameters()
  406. {
  407. byte[] S = null;
  408. ECCurve curve = ConfigureCurve(new SecT163K1Curve());
  409. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  410. + "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8"
  411. + "0289070FB05D38FF58321F2E800536D538CCDAA3D9"));
  412. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  413. }
  414. };
  415. /*
  416. * sect163r1
  417. */
  418. internal class SecT163R1Holder
  419. : X9ECParametersHolder
  420. {
  421. private SecT163R1Holder() { }
  422. internal static readonly X9ECParametersHolder Instance = new SecT163R1Holder();
  423. protected override X9ECParameters CreateParameters()
  424. {
  425. byte[] S = Hex.Decode("24B7B137C8A14D696E6768756151756FD0DA2E5C");
  426. ECCurve curve = ConfigureCurve(new SecT163R1Curve());
  427. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  428. + "0369979697AB43897789566789567F787A7876A654"
  429. + "00435EDB42EFAFB2989D51FEFCE3C80988F41FF883"));
  430. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  431. }
  432. };
  433. /*
  434. * sect163r2
  435. */
  436. internal class SecT163R2Holder
  437. : X9ECParametersHolder
  438. {
  439. private SecT163R2Holder() { }
  440. internal static readonly X9ECParametersHolder Instance = new SecT163R2Holder();
  441. protected override X9ECParameters CreateParameters()
  442. {
  443. byte[] S = Hex.Decode("85E25BFE5C86226CDB12016F7553F9D0E693A268");
  444. ECCurve curve = ConfigureCurve(new SecT163R2Curve());
  445. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  446. + "03F0EBA16286A2D57EA0991168D4994637E8343E36"
  447. + "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1"));
  448. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  449. }
  450. };
  451. /*
  452. * sect193r1
  453. */
  454. internal class SecT193R1Holder
  455. : X9ECParametersHolder
  456. {
  457. private SecT193R1Holder() { }
  458. internal static readonly X9ECParametersHolder Instance = new SecT193R1Holder();
  459. protected override X9ECParameters CreateParameters()
  460. {
  461. byte[] S = Hex.Decode("103FAEC74D696E676875615175777FC5B191EF30");
  462. ECCurve curve = ConfigureCurve(new SecT193R1Curve());
  463. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  464. + "01F481BC5F0FF84A74AD6CDF6FDEF4BF6179625372D8C0C5E1"
  465. + "0025E399F2903712CCF3EA9E3A1AD17FB0B3201B6AF7CE1B05"));
  466. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  467. }
  468. };
  469. /*
  470. * sect193r2
  471. */
  472. internal class SecT193R2Holder
  473. : X9ECParametersHolder
  474. {
  475. private SecT193R2Holder() { }
  476. internal static readonly X9ECParametersHolder Instance = new SecT193R2Holder();
  477. protected override X9ECParameters CreateParameters()
  478. {
  479. byte[] S = Hex.Decode("10B7B4D696E676875615175137C8A16FD0DA2211");
  480. ECCurve curve = ConfigureCurve(new SecT193R2Curve());
  481. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  482. + "00D9B67D192E0367C803F39E1A7E82CA14A651350AAE617E8F"
  483. + "01CE94335607C304AC29E7DEFBD9CA01F596F927224CDECF6C"));
  484. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  485. }
  486. };
  487. /*
  488. * sect233k1
  489. */
  490. internal class SecT233K1Holder
  491. : X9ECParametersHolder
  492. {
  493. private SecT233K1Holder() { }
  494. internal static readonly X9ECParametersHolder Instance = new SecT233K1Holder();
  495. protected override X9ECParameters CreateParameters()
  496. {
  497. byte[] S = null;
  498. ECCurve curve = ConfigureCurve(new SecT233K1Curve());
  499. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  500. + "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126"
  501. + "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3"));
  502. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  503. }
  504. };
  505. /*
  506. * sect233r1
  507. */
  508. internal class SecT233R1Holder
  509. : X9ECParametersHolder
  510. {
  511. private SecT233R1Holder() { }
  512. internal static readonly X9ECParametersHolder Instance = new SecT233R1Holder();
  513. protected override X9ECParameters CreateParameters()
  514. {
  515. byte[] S = Hex.Decode("74D59FF07F6B413D0EA14B344B20A2DB049B50C3");
  516. ECCurve curve = ConfigureCurve(new SecT233R1Curve());
  517. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  518. + "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B"
  519. + "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052"));
  520. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  521. }
  522. };
  523. /*
  524. * sect239k1
  525. */
  526. internal class SecT239K1Holder
  527. : X9ECParametersHolder
  528. {
  529. private SecT239K1Holder() { }
  530. internal static readonly X9ECParametersHolder Instance = new SecT239K1Holder();
  531. protected override X9ECParameters CreateParameters()
  532. {
  533. byte[] S = null;
  534. ECCurve curve = ConfigureCurve(new SecT239K1Curve());
  535. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  536. + "29A0B6A887A983E9730988A68727A8B2D126C44CC2CC7B2A6555193035DC"
  537. + "76310804F12E549BDB011C103089E73510ACB275FC312A5DC6B76553F0CA"));
  538. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  539. }
  540. };
  541. /*
  542. * sect283k1
  543. */
  544. internal class SecT283K1Holder
  545. : X9ECParametersHolder
  546. {
  547. private SecT283K1Holder() { }
  548. internal static readonly X9ECParametersHolder Instance = new SecT283K1Holder();
  549. protected override X9ECParameters CreateParameters()
  550. {
  551. byte[] S = null;
  552. ECCurve curve = ConfigureCurve(new SecT283K1Curve());
  553. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  554. + "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836"
  555. + "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259"));
  556. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  557. }
  558. };
  559. /*
  560. * sect283r1
  561. */
  562. internal class SecT283R1Holder
  563. : X9ECParametersHolder
  564. {
  565. private SecT283R1Holder() { }
  566. internal static readonly X9ECParametersHolder Instance = new SecT283R1Holder();
  567. protected override X9ECParameters CreateParameters()
  568. {
  569. byte[] S = Hex.Decode("77E2B07370EB0F832A6DD5B62DFC88CD06BB84BE");
  570. ECCurve curve = ConfigureCurve(new SecT283R1Curve());
  571. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  572. + "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053"
  573. + "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4"));
  574. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  575. }
  576. };
  577. /*
  578. * sect409k1
  579. */
  580. internal class SecT409K1Holder
  581. : X9ECParametersHolder
  582. {
  583. private SecT409K1Holder() { }
  584. internal static readonly X9ECParametersHolder Instance = new SecT409K1Holder();
  585. protected override X9ECParameters CreateParameters()
  586. {
  587. byte[] S = null;
  588. ECCurve curve = ConfigureCurve(new SecT409K1Curve());
  589. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  590. + "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746"
  591. + "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B"));
  592. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  593. }
  594. };
  595. /*
  596. * sect409r1
  597. */
  598. internal class SecT409R1Holder
  599. : X9ECParametersHolder
  600. {
  601. private SecT409R1Holder() { }
  602. internal static readonly X9ECParametersHolder Instance = new SecT409R1Holder();
  603. protected override X9ECParameters CreateParameters()
  604. {
  605. byte[] S = Hex.Decode("4099B5A457F9D69F79213D094C4BCD4D4262210B");
  606. ECCurve curve = ConfigureCurve(new SecT409R1Curve());
  607. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  608. + "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7"
  609. + "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706"));
  610. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  611. }
  612. };
  613. /*
  614. * sect571k1
  615. */
  616. internal class SecT571K1Holder
  617. : X9ECParametersHolder
  618. {
  619. private SecT571K1Holder() { }
  620. internal static readonly X9ECParametersHolder Instance = new SecT571K1Holder();
  621. protected override X9ECParameters CreateParameters()
  622. {
  623. byte[] S = null;
  624. ECCurve curve = ConfigureCurve(new SecT571K1Curve());
  625. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  626. + "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972"
  627. + "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3"));
  628. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  629. }
  630. };
  631. /*
  632. * sect571r1
  633. */
  634. internal class SecT571R1Holder
  635. : X9ECParametersHolder
  636. {
  637. private SecT571R1Holder() { }
  638. internal static readonly X9ECParametersHolder Instance = new SecT571R1Holder();
  639. protected override X9ECParameters CreateParameters()
  640. {
  641. byte[] S = Hex.Decode("2AA058F73A0E33AB486B0F610410C53A7F132310");
  642. ECCurve curve = ConfigureCurve(new SecT571R1Curve());
  643. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  644. + "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19"
  645. + "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B"));
  646. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  647. }
  648. };
  649. private static readonly IDictionary nameToCurve = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  650. private static readonly IDictionary nameToOid = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  651. private static readonly IDictionary oidToCurve = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  652. private static readonly IDictionary oidToName = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  653. private static readonly IList names = Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  654. private static void DefineCurve(string name, X9ECParametersHolder holder)
  655. {
  656. names.Add(name);
  657. name = Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name);
  658. nameToCurve.Add(name, holder);
  659. }
  660. private static void DefineCurveWithOid(string name, DerObjectIdentifier oid, X9ECParametersHolder holder)
  661. {
  662. names.Add(name);
  663. oidToName.Add(oid, name);
  664. oidToCurve.Add(oid, holder);
  665. name = Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name);
  666. nameToOid.Add(name, oid);
  667. nameToCurve.Add(name, holder);
  668. }
  669. private static void DefineCurveAlias(string name, DerObjectIdentifier oid)
  670. {
  671. object curve = oidToCurve[oid];
  672. if (curve == null)
  673. throw new InvalidOperationException();
  674. name = Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name);
  675. nameToOid.Add(name, oid);
  676. nameToCurve.Add(name, curve);
  677. }
  678. static CustomNamedCurves()
  679. {
  680. DefineCurve("curve25519", Curve25519Holder.Instance);
  681. //DefineCurveWithOid("secp112r1", SecObjectIdentifiers.SecP112r1, SecP112R1Holder.Instance);
  682. //DefineCurveWithOid("secp112r2", SecObjectIdentifiers.SecP112r2, SecP112R2Holder.Instance);
  683. DefineCurveWithOid("secp128r1", SecObjectIdentifiers.SecP128r1, SecP128R1Holder.Instance);
  684. //DefineCurveWithOid("secp128r2", SecObjectIdentifiers.SecP128r2, SecP128R2Holder.Instance);
  685. DefineCurveWithOid("secp160k1", SecObjectIdentifiers.SecP160k1, SecP160K1Holder.Instance);
  686. DefineCurveWithOid("secp160r1", SecObjectIdentifiers.SecP160r1, SecP160R1Holder.Instance);
  687. DefineCurveWithOid("secp160r2", SecObjectIdentifiers.SecP160r2, SecP160R2Holder.Instance);
  688. DefineCurveWithOid("secp192k1", SecObjectIdentifiers.SecP192k1, SecP192K1Holder.Instance);
  689. DefineCurveWithOid("secp192r1", SecObjectIdentifiers.SecP192r1, SecP192R1Holder.Instance);
  690. DefineCurveWithOid("secp224k1", SecObjectIdentifiers.SecP224k1, SecP224K1Holder.Instance);
  691. DefineCurveWithOid("secp224r1", SecObjectIdentifiers.SecP224r1, SecP224R1Holder.Instance);
  692. DefineCurveWithOid("secp256k1", SecObjectIdentifiers.SecP256k1, SecP256K1Holder.Instance);
  693. DefineCurveWithOid("secp256r1", SecObjectIdentifiers.SecP256r1, SecP256R1Holder.Instance);
  694. DefineCurveWithOid("secp384r1", SecObjectIdentifiers.SecP384r1, SecP384R1Holder.Instance);
  695. DefineCurveWithOid("secp521r1", SecObjectIdentifiers.SecP521r1, SecP521R1Holder.Instance);
  696. DefineCurveWithOid("sect113r1", SecObjectIdentifiers.SecT113r1, SecT113R1Holder.Instance);
  697. DefineCurveWithOid("sect113r2", SecObjectIdentifiers.SecT113r2, SecT113R2Holder.Instance);
  698. DefineCurveWithOid("sect131r1", SecObjectIdentifiers.SecT131r1, SecT131R1Holder.Instance);
  699. DefineCurveWithOid("sect131r2", SecObjectIdentifiers.SecT131r2, SecT131R2Holder.Instance);
  700. DefineCurveWithOid("sect163k1", SecObjectIdentifiers.SecT163k1, SecT163K1Holder.Instance);
  701. DefineCurveWithOid("sect163r1", SecObjectIdentifiers.SecT163r1, SecT163R1Holder.Instance);
  702. DefineCurveWithOid("sect163r2", SecObjectIdentifiers.SecT163r2, SecT163R2Holder.Instance);
  703. DefineCurveWithOid("sect193r1", SecObjectIdentifiers.SecT193r1, SecT193R1Holder.Instance);
  704. DefineCurveWithOid("sect193r2", SecObjectIdentifiers.SecT193r2, SecT193R2Holder.Instance);
  705. DefineCurveWithOid("sect233k1", SecObjectIdentifiers.SecT233k1, SecT233K1Holder.Instance);
  706. DefineCurveWithOid("sect233r1", SecObjectIdentifiers.SecT233r1, SecT233R1Holder.Instance);
  707. DefineCurveWithOid("sect239k1", SecObjectIdentifiers.SecT239k1, SecT239K1Holder.Instance);
  708. DefineCurveWithOid("sect283k1", SecObjectIdentifiers.SecT283k1, SecT283K1Holder.Instance);
  709. DefineCurveWithOid("sect283r1", SecObjectIdentifiers.SecT283r1, SecT283R1Holder.Instance);
  710. DefineCurveWithOid("sect409k1", SecObjectIdentifiers.SecT409k1, SecT409K1Holder.Instance);
  711. DefineCurveWithOid("sect409r1", SecObjectIdentifiers.SecT409r1, SecT409R1Holder.Instance);
  712. DefineCurveWithOid("sect571k1", SecObjectIdentifiers.SecT571k1, SecT571K1Holder.Instance);
  713. DefineCurveWithOid("sect571r1", SecObjectIdentifiers.SecT571r1, SecT571R1Holder.Instance);
  714. DefineCurveAlias("B-163", SecObjectIdentifiers.SecT163r2);
  715. DefineCurveAlias("B-233", SecObjectIdentifiers.SecT233r1);
  716. DefineCurveAlias("B-283", SecObjectIdentifiers.SecT283r1);
  717. DefineCurveAlias("B-409", SecObjectIdentifiers.SecT409r1);
  718. DefineCurveAlias("B-571", SecObjectIdentifiers.SecT571r1);
  719. DefineCurveAlias("K-163", SecObjectIdentifiers.SecT163k1);
  720. DefineCurveAlias("K-233", SecObjectIdentifiers.SecT233k1);
  721. DefineCurveAlias("K-283", SecObjectIdentifiers.SecT283k1);
  722. DefineCurveAlias("K-409", SecObjectIdentifiers.SecT409k1);
  723. DefineCurveAlias("K-571", SecObjectIdentifiers.SecT571k1);
  724. DefineCurveAlias("P-192", SecObjectIdentifiers.SecP192r1);
  725. DefineCurveAlias("P-224", SecObjectIdentifiers.SecP224r1);
  726. DefineCurveAlias("P-256", SecObjectIdentifiers.SecP256r1);
  727. DefineCurveAlias("P-384", SecObjectIdentifiers.SecP384r1);
  728. DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1);
  729. }
  730. public static X9ECParameters GetByName(string name)
  731. {
  732. X9ECParametersHolder holder = (X9ECParametersHolder)nameToCurve[Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name)];
  733. return holder == null ? null : holder.Parameters;
  734. }
  735. /**
  736. * return the X9ECParameters object for the named curve represented by
  737. * the passed in object identifier. Null if the curve isn't present.
  738. *
  739. * @param oid an object identifier representing a named curve, if present.
  740. */
  741. public static X9ECParameters GetByOid(DerObjectIdentifier oid)
  742. {
  743. X9ECParametersHolder holder = (X9ECParametersHolder)oidToCurve[oid];
  744. return holder == null ? null : holder.Parameters;
  745. }
  746. /**
  747. * return the object identifier signified by the passed in name. Null
  748. * if there is no object identifier associated with name.
  749. *
  750. * @return the object identifier associated with name, if present.
  751. */
  752. public static DerObjectIdentifier GetOid(string name)
  753. {
  754. return (DerObjectIdentifier)nameToOid[Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name)];
  755. }
  756. /**
  757. * return the named curve name represented by the given object identifier.
  758. */
  759. public static string GetName(DerObjectIdentifier oid)
  760. {
  761. return (string)oidToName[oid];
  762. }
  763. /**
  764. * returns an enumeration containing the name strings for curves
  765. * contained in this structure.
  766. */
  767. public static IEnumerable Names
  768. {
  769. get { return new EnumerableProxy(names); }
  770. }
  771. }
  772. }
  773. #endif