brainpool

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: BSD-3-Clause Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package brainpool implements the elliptic curves brainpoolP256r1, brainpoolP384r1 and brainpoolP512r1 (RFC 5639) for use by our fork of crypto/tls: ECDHE with the TLS named curves of RFC 7027 (26, 27, 28 in TLS 1.2; the TLS 1.3 groups 31, 32, 33 use the same curves) and ECDSA with brainpool keys, including DER key encodings and X.509 certificates whose keys or issuers are on these curves (crypto/x509 only knows the NIST curves).

Arithmetic

Field and scalar arithmetic use the constant-time package internal/bigmod; field elements are kept in Montgomery form. Points are in homogeneous projective coordinates and are added with the complete addition law of Renes, Costello and Batina (EUROCRYPT 2016, algorithm 1, valid for any a); the same law is used for doubling, so no input — the identity, P+P or P+(-P) — takes a different code path. The brainpool r1 curves have prime order (cofactor 1), which the complete law requires.

Scalar multiplication uses a fixed 4-bit window: 64/96/128 windows of four doublings and one addition each, with the table entry chosen by scanning all 16 entries through bigmod's masked select.

What is constant time, and what is not

  • Constant time with respect to secret scalars: scalar multiplication, ECDH, key generation after the rejection-sampling loop, ECDSA signing (k*G, k^-1 by Fermat exponentiation with bigmod.Exp, s = k^-1(e + r*d)).
  • Rejection sampling of private keys and of ECDSA nonces leaks how many candidates were rejected (never anything about the accepted value).
  • Conversions between bytes and bigmod values leak only lengths.
  • Converting a result to affine form inverts Z with a fixed-exponent exponentiation (constant time).
  • Verification, public-key validation, DER parsing and marshalling, and every method taking or returning *big.Int (the elliptic.Curve methods, *ecdsa.PublicKey and ecdsa.PrivateKey conversion) are variable time and must only see public data. crypto/ecdsa's generic code path for non-NIST curves (used when an *ecdsa.PrivateKey from ECDSA() is passed to ecdsa.Sign) uses math/big and is not constant time; sign with PrivateKey.Sign instead.

ECDSA nonces

PrivateKey.Sign derives k deterministically as in RFC 6979 (HMAC-DRBG with SHA-256/384/512 matching the curve size) and feeds 32 bytes from the caller's random source in as additional input (the hedged variant of RFC 6979 section 3.6): a broken RNG does not reveal the key, and a good one makes signatures non-deterministic.

elliptic.CurveParams

Params() returns the curve's p, n, b, G and name. CurveParams' own arithmetic methods assume a = -3 and give wrong answers for these curves; always call the methods on the curve returned by P256r1() and friends.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateCertificate

func CreateCertificate(rand io.Reader, template, parent *x509.Certificate, pub crypto.PublicKey, signer crypto.Signer) ([]byte, error)

CreateCertificate is x509.CreateCertificate for templates where pub and/or signer are brainpool keys. When neither is, it calls x509.CreateCertificate directly. A brainpool signer signs with ECDSA and SHA-256, SHA-384 or SHA-512 for 256-, 384- and 512-bit keys unless template.SignatureAlgorithm names another ECDSA algorithm.

func CreateCertificateRequest

func CreateCertificateRequest(rand io.Reader, template *x509.CertificateRequest, signer crypto.Signer) ([]byte, error)

CreateCertificateRequest is x509.CreateCertificateRequest for a brainpool signer; any other signer goes to crypto/x509 unchanged. The request is signed with ECDSA and SHA-256, SHA-384 or SHA-512 for 256-, 384- and 512-bit keys unless template.SignatureAlgorithm names another ECDSA algorithm.

func CurveForOID

func CurveForOID(oid asn1.ObjectIdentifier) (elliptic.Curve, bool)

CurveForOID returns the brainpool curve with the given named-curve OID.

func ECDH

func ECDH(c elliptic.Curve, priv, peer []byte) ([]byte, error)

ECDH computes the shared secret for TLS: the x-coordinate of priv*peer, always c.size bytes. priv must be c.size bytes in [1, n-1]; peer must be an uncompressed point on the curve.

func GenerateECDHKey

func GenerateECDHKey(c elliptic.Curve, rand io.Reader) (priv []byte, pub []byte, err error)

GenerateECDHKey returns a new private scalar (c.size bytes, big-endian) and its public point in uncompressed form (0x04 || X || Y).

func IsBrainpool

func IsBrainpool(c elliptic.Curve) bool

IsBrainpool reports whether c is one of this package's curves.

func IsBrainpoolCertificate

func IsBrainpoolCertificate(der []byte) bool

IsBrainpoolCertificate reports whether der is a certificate whose subject public key is on a brainpool curve.

func MarshalECPrivateKey

func MarshalECPrivateKey(k *PrivateKey) ([]byte, error)

MarshalECPrivateKey returns the RFC 5915 ECPrivateKey DER encoding (with the named curve and the public key).

func MarshalPKCS8PrivateKey

func MarshalPKCS8PrivateKey(k *PrivateKey) ([]byte, error)

MarshalPKCS8PrivateKey returns the PKCS#8 PrivateKeyInfo DER encoding; the curve is named in the AlgorithmIdentifier, not in the inner structure.

func MarshalPKIXPublicKey

func MarshalPKIXPublicKey(pub *ecdsa.PublicKey) ([]byte, error)

MarshalPKIXPublicKey returns the DER SubjectPublicKeyInfo of a brainpool public key.

func OID

OID returns the named-curve object identifier of a brainpool curve.

func P256r1

func P256r1() elliptic.Curve

P256r1 returns the brainpoolP256r1 curve.

func P384r1

func P384r1() elliptic.Curve

P384r1 returns the brainpoolP384r1 curve.

func P512r1

func P512r1() elliptic.Curve

P512r1 returns the brainpoolP512r1 curve.

func ParseCertificate

func ParseCertificate(der []byte) (*x509.Certificate, error)

ParseCertificate parses a DER certificate. Certificates crypto/x509 accepts are returned as it parses them. A certificate with a brainpool subject key is parsed with a placeholder key, after which Raw, RawTBSCertificate and RawSubjectPublicKeyInfo are the original bytes and PublicKey is the brainpool *ecdsa.PublicKey.

func ParseCertificateRequest

func ParseCertificateRequest(der []byte) (*x509.CertificateRequest, error)

ParseCertificateRequest parses a DER certification request and checks its signature. Requests crypto/x509 accepts are returned as it parses them. For a brainpool subject key, Raw, RawTBSCertificateRequest and RawSubjectPublicKeyInfo are the original bytes and PublicKey is the brainpool *ecdsa.PublicKey.

func ParsePKIXPublicKey

func ParsePKIXPublicKey(spki []byte) (*ecdsa.PublicKey, error)

ParsePKIXPublicKey parses a DER SubjectPublicKeyInfo holding an id-ecPublicKey on a brainpool named curve (uncompressed point).

func Verify

func Verify(pub *ecdsa.PublicKey, digest, sig []byte) bool

Verify reports whether sig (ASN.1 DER) is a valid ECDSA signature of digest by pub. It is variable time and handles only public data.

Types

type PrivateKey

type PrivateKey struct {
	// contains filtered or unexported fields
}

PrivateKey is an ECDSA private key on a brainpool curve. It implements crypto.Signer.

func GenerateKey

func GenerateKey(c elliptic.Curve, rand io.Reader) (*PrivateKey, error)

GenerateKey returns a new random key on c.

func NewPrivateKey

func NewPrivateKey(c elliptic.Curve, d []byte) (*PrivateKey, error)

NewPrivateKey returns the key with scalar d (big-endian; shorter encodings are left-padded, longer ones may only carry leading zeros). d must be in [1, n-1].

func ParseECPrivateKey

func ParseECPrivateKey(der []byte) (*PrivateKey, error)

ParseECPrivateKey parses an RFC 5915 (SEC 1) ECPrivateKey, or the same wrapped in PKCS#8, on a brainpool curve.

func ParsePKCS8PrivateKey

func ParsePKCS8PrivateKey(der []byte) (*PrivateKey, error)

ParsePKCS8PrivateKey parses a PKCS#8 PrivateKeyInfo holding a brainpool EC private key.

func (*PrivateKey) Bytes

func (k *PrivateKey) Bytes() []byte

Bytes returns the private scalar as c.size big-endian bytes.

func (*PrivateKey) Curve

func (k *PrivateKey) Curve() elliptic.Curve

Curve returns the key's curve.

func (*PrivateKey) ECDSA

func (k *PrivateKey) ECDSA() *ecdsa.PrivateKey

ECDSA returns the same key as *ecdsa.PrivateKey. Note that ecdsa.Sign with it takes crypto/ecdsa's variable-time generic path; prefer k.Sign.

func (*PrivateKey) Public

func (k *PrivateKey) Public() crypto.PublicKey

Public returns the public key as *ecdsa.PublicKey (Curve is the brainpool curve).

func (*PrivateKey) Sign

func (k *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error)

Sign signs digest (a hash computed by the caller; it is truncated to the order's bit length as ECDSA specifies) and returns an ASN.1 DER Ecdsa-Sig-Value. opts is ignored: any hash (or none) is accepted.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL