Documentation
¶
Overview ¶
Package ecdsa implements the Elliptic Curve Digital Signature Algorithm, as defined in FIPS 186-3.
This implementation derives the nonce from an AES-CTR CSPRNG keyed by:
SHA2-512(priv.D || entropy || hash)[:32]
The CSPRNG key is indifferentiable from a random oracle as shown in [Coron], the AES-CTR stream is indifferentiable from a random oracle under standard cryptographic assumptions (see [Larsson] for examples).
References:
[Coron] https://cs.nyu.edu/~dodis/ps/merkle.pdf [Larsson] https://www.nada.kth.se/kurser/kth/2D1441/semteo03/lecturenotes/assump.pdf
Package elliptic implements several standard elliptic curves over prime fields.
Package randutil contains internal randomness utilities for various crypto packages.
Index ¶
- Constants
- Variables
- func MarshalCompressed(curve elliptic.Curve, x, y *big.Int) []byte
- func MaybeReadByte(r io.Reader)
- func P256k1() elliptic.Curve
- func P384() elliptic.Curve
- func P521() elliptic.Curve
- func RecoverEthereum(hash, sig []byte) ([]byte, error)
- func RecoverPubkey(name string, hash, sig []byte) (*ecdsa.PublicKey, error)
- func Sign(rand io.Reader, priv *ecdsa.PrivateKey, hash []byte) (r, s *big.Int, recid byte, err error)
- func SignASN1(rand io.Reader, priv *ecdsa.PrivateKey, hash []byte) ([]byte, error)
- func SignBytes(priv *ecdsa.PrivateKey, hash []byte, flag byte) ([]byte, error)
- func SignEthereum(hash []byte, priv *ecdsa.PrivateKey) ([]byte, error)
- func UnmarshalCompressed(curve elliptic.Curve, data []byte) (x, y *big.Int)
- func Verify(pub *ecdsa.PublicKey, hash []byte, r, s *big.Int) bool
- func VerifyASN1(pub *ecdsa.PublicKey, hash, sig []byte) bool
- func VerifyBytes(pub *ecdsa.PublicKey, hash, sig []byte, flag byte) bool
- func VerifyEthereum(pubkey, hash, sig []byte, isHomestead bool) bool
- type CurveParams
- func (curve *CurveParams) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int)
- func (curve *CurveParams) Double(x1, y1 *big.Int) (*big.Int, *big.Int)
- func (curve *CurveParams) IsOnCurve(x, y *big.Int) bool
- func (curve *CurveParams) Params() *elliptic.CurveParams
- func (curve *CurveParams) ScalarBaseMult(k []byte) (*big.Int, *big.Int)
- func (curve *CurveParams) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int)
Constants ¶
const ( Normal byte = 0 LowerS byte = 1 // return (r, s) with s <= N/2 RecID byte = 2 // return recovery id in addition to (r, s) )
signing options
Variables ¶
var (
ErrInvalidLength = errors.New("Invalid hash or signature length")
)
errors
Functions ¶
func MarshalCompressed ¶
MarshalCompressed converts a point on the curve into the compressed form specified in section 4.3.6 of ANSI X9.62.
func MaybeReadByte ¶
MaybeReadByte reads a single byte from r with ~50% probability. This is used to ensure that callers do not depend on non-guaranteed behaviour, e.g. assuming that rsa.GenerateKey is deterministic w.r.t. a given random stream.
This does not affect tests that pass a stream of fixed bytes as the random source (e.g. a zeroReader).
func P256k1 ¶
P256k1 returns a Curve which implements secp256k1 (https://www.secg.org/sec2-v2.pdf, section 2.4.1), also known as secp521k1. The CurveParams.Name of this Curve is "P-256k1".
Multiple invocations of this function will return the same value, so it can be used for equality checks and switch statements.
The cryptographic operations do not use constant-time algorithms.
func P384 ¶
P384 returns a Curve which implements NIST P-384 (FIPS 186-3, section D.2.4), also known as secp384r1. The CurveParams.Name of this Curve is "P-384".
Multiple invocations of this function will return the same value, so it can be used for equality checks and switch statements.
The cryptographic operations do not use constant-time algorithms.
func P521 ¶
P521 returns a Curve which implements NIST P-521 (FIPS 186-3, section D.2.5), also known as secp521r1. The CurveParams.Name of this Curve is "P-521".
Multiple invocations of this function will return the same value, so it can be used for equality checks and switch statements.
The cryptographic operations do not use constant-time algorithms.
func RecoverEthereum ¶
RecoverEthereum returns the public key of the signer
signature must be the 65-byte [R || S || V] format with recovery id as the last byte
func RecoverPubkey ¶
RecoverPubkey recovers the public key from the signature
func Sign ¶
func Sign(rand io.Reader, priv *ecdsa.PrivateKey, hash []byte) (r, s *big.Int, recid byte, err error)
Sign signs a hash (which should be the result of hashing a larger message) using the private key, priv. If the hash is longer than the bit-length of the private key's curve order, the hash will be truncated to that length. It returns the signature as a pair of integers. The security of the private key depends on the entropy of rand.
func SignASN1 ¶
SignASN1 signs a hash (which should be the result of hashing a larger message) using the private key, priv. If the hash is longer than the bit-length of the private key's curve order, the hash will be truncated to that length. It returns the ASN.1 encoded signature. The security of the private key depends on the entropy of rand.
func SignEthereum ¶
func SignEthereum(hash []byte, priv *ecdsa.PrivateKey) ([]byte, error)
SignEthereum returns an Ethereum-compatible signature The produced signature is in the 65-byte [R || S || V] format
This function is susceptible to chosen plaintext attackes. The caller is responsible to ensure that the given hash cannot be chosen directly by an attacker. Common solution is to hash any input before calculating the signature
func UnmarshalCompressed ¶
UnmarshalCompressed converts a point, serialized by MarshalCompressed, into an x, y pair. It is an error if the point is not in compressed form or is not on the curve. On error, x = nil.
func Verify ¶
Verify verifies the signature in r, s of hash using the public key, pub. Its return value records whether the signature is valid.
func VerifyASN1 ¶
VerifyASN1 verifies the ASN.1 encoded signature, sig, of hash using the public key, pub. Its return value records whether the signature is valid.
func VerifyBytes ¶
VerifyBytes verifies the signature in bytes
func VerifyEthereum ¶
VerifyEthereum verifies an Ethereum signature The public key is either compressed (33-byte) or uncompressed (65-byte) format, and the signature should have the 64-byte [R || S] format
ECDSA malleability issue: For an ECDSA signature (r, s, v), it can be shown that (r, N-s, v^1) is also a valid signature that can correctly recover the public key. Mathematically, this is not a problem but could cause issue where the uniqueness of signature is required for better security
Ethereum handled this issue by requiring the s value to be in the lower half of N (the order of the curve) starting the Homestead hard-fork see https://eips.ethereum.org/EIPS/eip-2 To be specific, the value of s needs to satisfy: 1 <= s <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0 signature with a higher s value will be rejected
For signature before the Homestead hard-fork, call with isHomestead = false
Types ¶
type CurveParams ¶
type CurveParams struct { elliptic.CurveParams A *big.Int // the linear coefficient of the curve equation }
CurveParams contains the parameters of an elliptic curve y² = x³ + ax + b, and also provides a generic, non-constant time implementation of Curve.
func (*CurveParams) IsOnCurve ¶
func (curve *CurveParams) IsOnCurve(x, y *big.Int) bool
IsOnCurve returns whether the point (x, y) lies on the curve or not
func (*CurveParams) Params ¶
func (curve *CurveParams) Params() *elliptic.CurveParams
Params returns the curve params
func (*CurveParams) ScalarBaseMult ¶
ScalarBaseMult computes scalar multiplication of the base point
func (*CurveParams) ScalarMult ¶
ScalarMult computes scalar multiplication of a given point