本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
5.7KB

  1. // Package brainpool implements Brainpool elliptic curves.
  2. // Implementation of rcurves is from github.com/ebfe/brainpool
  3. // Note that these curves are implemented with naive, non-constant time operations
  4. // and are likely not suitable for enviroments where timing attacks are a concern.
  5. package brainpool
  6. import (
  7. "crypto/elliptic"
  8. "math/big"
  9. "sync"
  10. )
  11. var (
  12. once sync.Once
  13. p256t1, p384t1, p512t1 *elliptic.CurveParams
  14. p256r1, p384r1, p512r1 *rcurve
  15. )
  16. func initAll() {
  17. initP256t1()
  18. initP384t1()
  19. initP512t1()
  20. initP256r1()
  21. initP384r1()
  22. initP512r1()
  23. }
  24. func initP256t1() {
  25. p256t1 = &elliptic.CurveParams{Name: "brainpoolP256t1"}
  26. p256t1.P, _ = new(big.Int).SetString("A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", 16)
  27. p256t1.N, _ = new(big.Int).SetString("A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", 16)
  28. p256t1.B, _ = new(big.Int).SetString("662C61C430D84EA4FE66A7733D0B76B7BF93EBC4AF2F49256AE58101FEE92B04", 16)
  29. p256t1.Gx, _ = new(big.Int).SetString("A3E8EB3CC1CFE7B7732213B23A656149AFA142C47AAFBC2B79A191562E1305F4", 16)
  30. p256t1.Gy, _ = new(big.Int).SetString("2D996C823439C56D7F7B22E14644417E69BCB6DE39D027001DABE8F35B25C9BE", 16)
  31. p256t1.BitSize = 256
  32. }
  33. func initP256r1() {
  34. twisted := p256t1
  35. params := &elliptic.CurveParams{
  36. Name: "brainpoolP256r1",
  37. P: twisted.P,
  38. N: twisted.N,
  39. BitSize: twisted.BitSize,
  40. }
  41. params.Gx, _ = new(big.Int).SetString("8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", 16)
  42. params.Gy, _ = new(big.Int).SetString("547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", 16)
  43. z, _ := new(big.Int).SetString("3E2D4BD9597B58639AE7AA669CAB9837CF5CF20A2C852D10F655668DFC150EF0", 16)
  44. p256r1 = newrcurve(twisted, params, z)
  45. }
  46. func initP384t1() {
  47. p384t1 = &elliptic.CurveParams{Name: "brainpoolP384t1"}
  48. p384t1.P, _ = new(big.Int).SetString("8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC53", 16)
  49. p384t1.N, _ = new(big.Int).SetString("8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425A7CF3AB6AF6B7FC3103B883202E9046565", 16)
  50. p384t1.B, _ = new(big.Int).SetString("7F519EADA7BDA81BD826DBA647910F8C4B9346ED8CCDC64E4B1ABD11756DCE1D2074AA263B88805CED70355A33B471EE", 16)
  51. p384t1.Gx, _ = new(big.Int).SetString("18DE98B02DB9A306F2AFCD7235F72A819B80AB12EBD653172476FECD462AABFFC4FF191B946A5F54D8D0AA2F418808CC", 16)
  52. p384t1.Gy, _ = new(big.Int).SetString("25AB056962D30651A114AFD2755AD336747F93475B7A1FCA3B88F2B6A208CCFE469408584DC2B2912675BF5B9E582928", 16)
  53. p384t1.BitSize = 384
  54. }
  55. func initP384r1() {
  56. twisted := p384t1
  57. params := &elliptic.CurveParams{
  58. Name: "brainpoolP384r1",
  59. P: twisted.P,
  60. N: twisted.N,
  61. BitSize: twisted.BitSize,
  62. }
  63. params.Gx, _ = new(big.Int).SetString("1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10E8E826E03436D646AAEF87B2E247D4AF1E", 16)
  64. params.Gy, _ = new(big.Int).SetString("8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129280E4646217791811142820341263C5315", 16)
  65. z, _ := new(big.Int).SetString("41DFE8DD399331F7166A66076734A89CD0D2BCDB7D068E44E1F378F41ECBAE97D2D63DBC87BCCDDCCC5DA39E8589291C", 16)
  66. p384r1 = newrcurve(twisted, params, z)
  67. }
  68. func initP512t1() {
  69. p512t1 = &elliptic.CurveParams{Name: "brainpoolP512t1"}
  70. p512t1.P, _ = new(big.Int).SetString("AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3", 16)
  71. p512t1.N, _ = new(big.Int).SetString("AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA70330870553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069", 16)
  72. p512t1.B, _ = new(big.Int).SetString("7CBBBCF9441CFAB76E1890E46884EAE321F70C0BCB4981527897504BEC3E36A62BCDFA2304976540F6450085F2DAE145C22553B465763689180EA2571867423E", 16)
  73. p512t1.Gx, _ = new(big.Int).SetString("640ECE5C12788717B9C1BA06CBC2A6FEBA85842458C56DDE9DB1758D39C0313D82BA51735CDB3EA499AA77A7D6943A64F7A3F25FE26F06B51BAA2696FA9035DA", 16)
  74. p512t1.Gy, _ = new(big.Int).SetString("5B534BD595F5AF0FA2C892376C84ACE1BB4E3019B71634C01131159CAE03CEE9D9932184BEEF216BD71DF2DADF86A627306ECFF96DBB8BACE198B61E00F8B332", 16)
  75. p512t1.BitSize = 512
  76. }
  77. func initP512r1() {
  78. twisted := p512t1
  79. params := &elliptic.CurveParams{
  80. Name: "brainpoolP512r1",
  81. P: twisted.P,
  82. N: twisted.N,
  83. BitSize: twisted.BitSize,
  84. }
  85. params.Gx, _ = new(big.Int).SetString("81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D0098EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822", 16)
  86. params.Gy, _ = new(big.Int).SetString("7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F8111B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892", 16)
  87. z, _ := new(big.Int).SetString("12EE58E6764838B69782136F0F2D3BA06E27695716054092E60A80BEDB212B64E585D90BCE13761F85C3F1D2A64E3BE8FEA2220F01EBA5EEB0F35DBD29D922AB", 16)
  88. p512r1 = newrcurve(twisted, params, z)
  89. }
  90. // P256t1 returns a Curve which implements Brainpool P256t1 (see RFC 5639, section 3.4)
  91. func P256t1() elliptic.Curve {
  92. once.Do(initAll)
  93. return p256t1
  94. }
  95. // P256r1 returns a Curve which implements Brainpool P256r1 (see RFC 5639, section 3.4)
  96. func P256r1() elliptic.Curve {
  97. once.Do(initAll)
  98. return p256r1
  99. }
  100. // P384t1 returns a Curve which implements Brainpool P384t1 (see RFC 5639, section 3.6)
  101. func P384t1() elliptic.Curve {
  102. once.Do(initAll)
  103. return p384t1
  104. }
  105. // P384r1 returns a Curve which implements Brainpool P384r1 (see RFC 5639, section 3.6)
  106. func P384r1() elliptic.Curve {
  107. once.Do(initAll)
  108. return p384r1
  109. }
  110. // P512t1 returns a Curve which implements Brainpool P512t1 (see RFC 5639, section 3.7)
  111. func P512t1() elliptic.Curve {
  112. once.Do(initAll)
  113. return p512t1
  114. }
  115. // P512r1 returns a Curve which implements Brainpool P512r1 (see RFC 5639, section 3.7)
  116. func P512r1() elliptic.Curve {
  117. once.Do(initAll)
  118. return p512r1
  119. }
上海开阖软件有限公司 沪ICP备12045867号-1