package ecdsa
import (
)
func ( elliptic.Curve, io.Reader) (*PrivateKey, error) {
if fips140only.Enabled {
return nil, errors.New("crypto/ecdsa: use of custom curves is not allowed in FIPS 140-only mode")
}
, := randFieldElement(, )
if != nil {
return nil,
}
:= new(PrivateKey)
.PublicKey.Curve =
.D =
.PublicKey.X, .PublicKey.Y = .ScalarBaseMult(.Bytes())
return , nil
}
func ( []byte, elliptic.Curve) *big.Int {
:= .Params().N.BitLen()
:= ( + 7) / 8
if len() > {
= [:]
}
:= new(big.Int).SetBytes()
:= len()*8 -
if > 0 {
.Rsh(, uint())
}
return
}
var errZeroParam = errors.New("zero parameter")
func ( io.Reader, *PrivateKey, []byte) (, *big.Int, error) {
, := SignASN1(, , )
if != nil {
return nil, nil,
}
, = new(big.Int), new(big.Int)
var cryptobyte.String
:= cryptobyte.String()
if !.ReadASN1(&, asn1.SEQUENCE) ||
!.Empty() ||
!.ReadASN1Integer() ||
!.ReadASN1Integer() ||
!.Empty() {
return nil, nil, errors.New("invalid ASN.1 from SignASN1")
}
return , , nil
}
func ( *PrivateKey, io.Reader, []byte) ( []byte, error) {
if fips140only.Enabled {
return nil, errors.New("crypto/ecdsa: use of custom curves is not allowed in FIPS 140-only mode")
}
:= .Curve
var [32]byte
if , := io.ReadFull(, [:]); != nil {
return nil,
}
for , := range .D.Bytes() {
[%32] ^=
}
for , := range {
[%32] ^=
}
= rand.NewChaCha8()
:= .Params().N
if .Sign() == 0 {
return nil, errZeroParam
}
var , , , *big.Int
for {
for {
, = randFieldElement(, )
if != nil {
return nil,
}
= new(big.Int).ModInverse(, )
, _ = .ScalarBaseMult(.Bytes())
.Mod(, )
if .Sign() != 0 {
break
}
}
:= hashToInt(, )
= new(big.Int).Mul(.D, )
.Add(, )
.Mul(, )
.Mod(, )
if .Sign() != 0 {
break
}
}
return encodeSignature(.Bytes(), .Bytes())
}
func ( *PublicKey, []byte, , *big.Int) bool {
if .Sign() <= 0 || .Sign() <= 0 {
return false
}
, := encodeSignature(.Bytes(), .Bytes())
if != nil {
return false
}
return VerifyASN1(, , )
}
func ( *PublicKey, []byte, []byte) bool {
if fips140only.Enabled {
panic("crypto/ecdsa: use of custom curves is not allowed in FIPS 140-only mode")
}
, , := parseSignature()
if != nil {
return false
}
, := new(big.Int).SetBytes(), new(big.Int).SetBytes()
:= .Curve
:= .Params().N
if .Sign() <= 0 || .Sign() <= 0 {
return false
}
if .Cmp() >= 0 || .Cmp() >= 0 {
return false
}
:= hashToInt(, )
:= new(big.Int).ModInverse(, )
:= .Mul(, )
.Mod(, )
:= .Mul(, )
.Mod(, )
, := .ScalarBaseMult(.Bytes())
, := .ScalarMult(.X, .Y, .Bytes())
, := .Add(, , , )
if .Sign() == 0 && .Sign() == 0 {
return false
}
.Mod(, )
return .Cmp() == 0
}
var one = new(big.Int).SetInt64(1)
func ( elliptic.Curve, io.Reader) ( *big.Int, error) {
for {
:= .Params().N
:= make([]byte, (.BitLen()+7)/8)
if _, = io.ReadFull(, ); != nil {
return
}
if := len()*8 - .BitLen(); > 0 {
[0] >>=
}
= new(big.Int).SetBytes()
if .Sign() != 0 && .Cmp() < 0 {
return
}
}
}