(der []byte)
| 186 | } |
| 187 | |
| 188 | func ParseSm2PrivateKey(der []byte) (*sm2.PrivateKey, error) { |
| 189 | var privKey sm2PrivateKey |
| 190 | |
| 191 | if _, err := asn1.Unmarshal(der, &privKey); err != nil { |
| 192 | return nil, errors.New("x509: failed to parse SM2 private key: " + err.Error()) |
| 193 | } |
| 194 | curve := sm2.P256Sm2() |
| 195 | k := new(big.Int).SetBytes(privKey.PrivateKey) |
| 196 | curveOrder := curve.Params().N |
| 197 | if k.Cmp(curveOrder) >= 0 { |
| 198 | return nil, errors.New("x509: invalid elliptic curve private key value") |
| 199 | } |
| 200 | priv := new(sm2.PrivateKey) |
| 201 | priv.Curve = curve |
| 202 | priv.D = k |
| 203 | privateKey := make([]byte, (curveOrder.BitLen()+7)/8) |
| 204 | for len(privKey.PrivateKey) > len(privateKey) { |
| 205 | if privKey.PrivateKey[0] != 0 { |
| 206 | return nil, errors.New("x509: invalid private key length") |
| 207 | } |
| 208 | privKey.PrivateKey = privKey.PrivateKey[1:] |
| 209 | } |
| 210 | copy(privateKey[len(privateKey)-len(privKey.PrivateKey):], privKey.PrivateKey) |
| 211 | priv.X, priv.Y = curve.ScalarBaseMult(privateKey) |
| 212 | return priv, nil |
| 213 | } |
| 214 | |
| 215 | func ParsePKCS8UnecryptedPrivateKey(der []byte) (*sm2.PrivateKey, error) { |
| 216 | var privKey pkcs8 |
no test coverage detected
searching dependent graphs…