ecdh

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Curve deprecated

func Curve() stdecdh.Curve

Curve returns the underlying X25519 curve instance.

Deprecated: Use CurveX25519 instead.

func CurveP256

func CurveP256() stdecdh.Curve

CurveP256 returns the underlying P-256 curve instance.

func CurveP384

func CurveP384() stdecdh.Curve

CurveP384 returns the underlying P-384 curve instance.

func CurveX25519 added in v1.0.1

func CurveX25519() stdecdh.Curve

CurveX25519 returns the underlying X25519 curve instance.

func SharedSecret deprecated

func SharedSecret(p PrivateKey, peer PublicKey) ([]byte, error)

SharedSecret performs the X25519 Diffie-Hellman operation between private and peer.

Deprecated: Use SharedSecretX25519 instead.

func SharedSecretP256

func SharedSecretP256(p PrivateKey, peer PublicKey) ([]byte, error)

SharedSecretP256 performs the ECDH operation between private and peer.

func SharedSecretP384

func SharedSecretP384(p PrivateKey, peer PublicKey) ([]byte, error)

SharedSecretP384 performs the ECDH operation between private and peer.

func SharedSecretX448 added in v1.0.1

func SharedSecretX448(p PrivateKey, peer PublicKey) ([]byte, error)

SharedSecretX448 performs the X448 Diffie-Hellman operation between private and peer.

func SharedSecretX25519 added in v1.0.1

func SharedSecretX25519(p PrivateKey, peer PublicKey) ([]byte, error)

SharedSecretX25519 performs the X25519 Diffie-Hellman operation between private and peer.

Types

type KeyExchange

type KeyExchange interface {
	// Curve returns the underlying crypto/ecdh curve implementation when
	// available. For custom curves without a crypto/ecdh counterpart this may
	// return nil.
	Curve() stdecdh.Curve
	// GenerateKey creates a new private key using crypto/rand.
	GenerateKey() (PrivateKey, error)
	// NewPrivateKey constructs a private key from scalar bytes.
	NewPrivateKey(d []byte) (PrivateKey, error)
	// NewPublicKey parses a peer public key in the format required by the curve.
	NewPublicKey(b []byte) (PublicKey, error)
	// SharedSecret performs the ECDH operation between private and peer.
	SharedSecret(p PrivateKey, peer PublicKey) ([]byte, error)
}

KeyExchange describes the minimal API shared by ECDH helpers exposed by the library. Implementations may wrap crypto/ecdh curves or provide custom curve-specific logic while presenting a uniform surface to callers.

func New deprecated

func New() KeyExchange

New returns a KeyExchange helper bound to the X25519 curve.

Deprecated: Use NewX25519 instead.

func NewKeyExchange

func NewKeyExchange(curve stdecdh.Curve) KeyExchange

NewKeyExchange wraps curve in a KeyExchange implementation.

func NewP256

func NewP256() KeyExchange

NewP256 returns a KeyExchange helper bound to the P-256 curve.

func NewP384

func NewP384() KeyExchange

NewP384 returns a KeyExchange helper bound to the P-384 curve.

func NewX448 added in v1.0.1

func NewX448() KeyExchange

NewX448 returns a KeyExchange helper implementing the RFC 7748 X448 Diffie-Hellman primitive. The implementation is self-contained because crypto/ecdh currently does not expose Curve448.

func NewX25519 added in v1.0.1

func NewX25519() KeyExchange

NewX25519 returns a KeyExchange helper bound to the X25519 curve.

type PrivateKey added in v1.0.1

type PrivateKey interface {
	// Bytes returns the canonical encoding of the private key.
	Bytes() []byte
	// PublicKey returns the corresponding public key instance.
	PublicKey() PublicKey
	// ECDH computes the shared secret with the peer public key.
	ECDH(peer PublicKey) ([]byte, error)
	// Equal reports whether the provided key matches this private key.
	Equal(x crypto.PrivateKey) bool
}

PrivateKey represents an ECDH private key backed either by the Go standard library implementation or by a custom curve implementation (such as X448).

The interface intentionally mirrors the small subset of methods exposed by crypto/ecdh.PrivateKey that are required across the repository. This allows callers to operate on keys uniformly without leaking the concrete implementation details or exposing mutable internal buffers.

func GenerateKey deprecated

func GenerateKey() (PrivateKey, error)

GenerateKey creates a new private key using crypto/rand.

Deprecated: Use GenerateKeyX25519 instead.

func GenerateKeyP256

func GenerateKeyP256() (PrivateKey, error)

GenerateKeyP256 creates a new private key using crypto/rand.

func GenerateKeyP384

func GenerateKeyP384() (PrivateKey, error)

GenerateKeyP384 creates a new private key using crypto/rand.

func GenerateKeyX448 added in v1.0.1

func GenerateKeyX448() (PrivateKey, error)

GenerateKeyX448 creates a new X448 private key using crypto/rand.

func GenerateKeyX25519 added in v1.0.1

func GenerateKeyX25519() (PrivateKey, error)

GenerateKeyX25519 creates a new private key using crypto/rand.

func NewPrivateKey deprecated

func NewPrivateKey(d []byte) (PrivateKey, error)

NewPrivateKey constructs a private key from scalar bytes.

Deprecated: Use NewPrivateKeyX25519 instead.

func NewPrivateKeyP256

func NewPrivateKeyP256(d []byte) (PrivateKey, error)

NewPrivateKeyP256 constructs a private key from scalar bytes.

func NewPrivateKeyP384

func NewPrivateKeyP384(d []byte) (PrivateKey, error)

NewPrivateKeyP384 constructs a private key from scalar bytes.

func NewPrivateKeyX448 added in v1.0.1

func NewPrivateKeyX448(d []byte) (PrivateKey, error)

NewPrivateKeyX448 constructs an X448 private key from scalar bytes.

func NewPrivateKeyX25519 added in v1.0.1

func NewPrivateKeyX25519(d []byte) (PrivateKey, error)

NewPrivateKeyX25519 constructs a private key from scalar bytes.

type PublicKey added in v1.0.1

type PublicKey interface {
	// Bytes returns the canonical encoding of the public key.
	Bytes() []byte
	// Equal reports whether the provided key matches this public key.
	Equal(x crypto.PublicKey) bool
}

PublicKey represents an ECDH public key suitable for the associated PrivateKey type.

func NewPublicKey deprecated

func NewPublicKey(b []byte) (PublicKey, error)

NewPublicKey parses a 32-byte Montgomery u-coordinate public key.

Deprecated: Use NewPublicKeyX25519 instead.

func NewPublicKeyP256

func NewPublicKeyP256(b []byte) (PublicKey, error)

NewPublicKeyP256 parses an uncompressed public key.

func NewPublicKeyP384

func NewPublicKeyP384(b []byte) (PublicKey, error)

NewPublicKeyP384 parses an uncompressed public key.

func NewPublicKeyX448 added in v1.0.1

func NewPublicKeyX448(b []byte) (PublicKey, error)

NewPublicKeyX448 parses a 56-byte X448 public key.

func NewPublicKeyX25519 added in v1.0.1

func NewPublicKeyX25519(b []byte) (PublicKey, error)

NewPublicKeyX25519 parses a 32-byte Montgomery u-coordinate public key.

Jump to

Keyboard shortcuts

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