1
0

p256.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. Copyright Suzhou Tongji Fintech Research Institute 2017 All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sm2
  14. import (
  15. "crypto/elliptic"
  16. "math/big"
  17. "sync"
  18. )
  19. var initonce sync.Once
  20. var p256Sm2Params *elliptic.CurveParams
  21. // 取自elliptic的p256.go文件,修改曲线参数为sm2
  22. // See FIPS 186-3, section D.2.3
  23. func initP256Sm2() {
  24. p256Sm2Params = &elliptic.CurveParams{Name: "SM2-P-256"} // 注明为SM2
  25. //SM2椭 椭 圆 曲 线 公 钥 密 码 算 法 推 荐 曲 线 参 数
  26. p256Sm2Params.P, _ = new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16)
  27. p256Sm2Params.N, _ = new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16)
  28. p256Sm2Params.B, _ = new(big.Int).SetString("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16)
  29. p256Sm2Params.Gx, _ = new(big.Int).SetString("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", 16)
  30. p256Sm2Params.Gy, _ = new(big.Int).SetString("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", 16)
  31. p256Sm2Params.BitSize = 256
  32. }
  33. func P256Sm2() elliptic.Curve {
  34. initonce.Do(initP256Sm2)
  35. return p256Sm2Params
  36. }