github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/core/access_contoller/opencrypto/utils/sm2curve.go (about)

     1  /*
     2  Copyright (C) BABEC. All rights reserved.
     3  Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
     4  
     5  SPDX-License-Identifier: Apache-2.0
     6  */
     7  
     8  package utils
     9  
    10  import (
    11  	"crypto/elliptic"
    12  	"encoding/asn1"
    13  	"math/big"
    14  	"sync"
    15  )
    16  
    17  const SM2_DEFAULT_USER_ID = "1234567812345678"
    18  
    19  // curve
    20  var (
    21  	OidSM2 = asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1} // SM2 id
    22  )
    23  
    24  var (
    25  	oidNamedCurveP224 = asn1.ObjectIdentifier{1, 3, 132, 0, 33}
    26  	oidNamedCurveP256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7}
    27  	oidNamedCurveP384 = asn1.ObjectIdentifier{1, 3, 132, 0, 34}
    28  	oidNamedCurveP521 = asn1.ObjectIdentifier{1, 3, 132, 0, 35}
    29  
    30  	OidNamedCurveSm2 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 301}
    31  )
    32  
    33  var initonce sync.Once
    34  
    35  type (
    36  	p256Curve struct {
    37  		*elliptic.CurveParams
    38  	}
    39  )
    40  
    41  var (
    42  	p256 p256Curve
    43  )
    44  
    45  func initSMP256() {
    46  	// See FIPS 186-3, section D.2.3
    47  	p256.CurveParams = &elliptic.CurveParams{Name: "SM2-P-256"}
    48  	var pStr = "115792089210356248756420345214020892766250353991924191454421193933289684991999"
    49  	p256.P, _ = new(big.Int).SetString(pStr, 10)
    50  	var nStr = "115792089210356248756420345214020892766061623724957744567843809356293439045923"
    51  	p256.N, _ = new(big.Int).SetString(nStr, 10)
    52  	var bStr = "18505919022281880113072981827955639221458448578012075254857346196103069175443"
    53  	p256.B, _ = new(big.Int).SetString(bStr, 10)
    54  	var gXStr = "22963146547237050559479531362550074578802567295341616970375194840604139615431"
    55  	p256.Gx, _ = new(big.Int).SetString(gXStr, 10)
    56  	var gYStr = "85132369209828568825618990617112496413088388631904505083283536607588877201568"
    57  	p256.Gy, _ = new(big.Int).SetString(gYStr, 10)
    58  	p256.BitSize = 256
    59  }
    60  
    61  func P256Sm2() elliptic.Curve {
    62  	initonce.Do(initSMP256)
    63  	return p256
    64  }
    65  
    66  func OidFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) {
    67  	switch curve {
    68  	case elliptic.P224():
    69  		return oidNamedCurveP224, true
    70  	case elliptic.P256():
    71  		return oidNamedCurveP256, true
    72  	case elliptic.P384():
    73  		return oidNamedCurveP384, true
    74  	case elliptic.P521():
    75  		return oidNamedCurveP521, true
    76  	case P256Sm2():
    77  		return OidNamedCurveSm2, true
    78  	}
    79  	return nil, false
    80  }
    81  
    82  // nolint
    83  func namedCurveFromOID(oid asn1.ObjectIdentifier) elliptic.Curve {
    84  	switch {
    85  	case oid.Equal(oidNamedCurveP224):
    86  		return elliptic.P224()
    87  	case oid.Equal(oidNamedCurveP256):
    88  		return elliptic.P256()
    89  	case oid.Equal(oidNamedCurveP384):
    90  		return elliptic.P384()
    91  	case oid.Equal(oidNamedCurveP521):
    92  		return elliptic.P521()
    93  	case oid.Equal(OidNamedCurveSm2):
    94  		return P256Sm2()
    95  	}
    96  	return nil
    97  }