package crypto
import (
)
type ecdsaSignature struct {
R, S *big.Int
}
func ( elliptic.Curve, []byte) *ecdsa.PrivateKey {
return ECDSAKeyFromPoint(, (&big.Int{}).SetBytes())
}
func ( elliptic.Curve, *big.Int) *ecdsa.PrivateKey {
, := .ScalarBaseMult(.Bytes())
:= &ecdsa.PrivateKey{
PublicKey: ecdsa.PublicKey{
Curve: ,
X: ,
Y: ,
},
D: ,
}
return
}
func ( elliptic.Curve, , []byte) (*ecdsa.PublicKey, error) {
:= (&big.Int{}).SetBytes()
:= (&big.Int{}).SetBytes()
if !.IsOnCurve(, ) {
return nil, fmt.Errorf("point(%v, %v) is not on the given curve", .String(), .String())
}
return &ecdsa.PublicKey{
Curve: ,
X: ,
Y: ,
}, nil
}
func ( *ecdsa.PublicKey, []byte, []byte) (bool, error) {
var ecdsaSignature
, := asn1.Unmarshal(, &)
if != nil {
return false,
}
return ecdsa.Verify(, , .R, .S), nil
}
func ( func() hash.Hash, int, []byte, , []byte) ([]byte, error) {
:= int64(math.Ceil((float64() / 8) / float64(().Size())))
if > 0x7FFFFFFF {
return nil, fmt.Errorf("unable to derive key of size %d using 32-bit counter", )
}
if int64() > 0x7FFFFFFF {
return nil, fmt.Errorf("bitLen is greater than 32-bits")
}
:= bytes.NewBuffer(nil)
.Write()
.WriteByte(0x00)
.Write()
if := binary.Write(, binary.BigEndian, int32()); != nil {
return nil, fmt.Errorf("failed to write bit length to fixed input string: %v", )
}
var []byte
:= hmac.New(, )
for := int64(1); <= ; ++ {
.Reset()
if := binary.Write(, binary.BigEndian, int32()); != nil {
return nil,
}
, := .Write(.Bytes())
if != nil {
return nil,
}
= append(, .Sum(nil)...)
}
return [:/8], nil
}