// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

// Package elliptic provides elliptic curve cryptography for DTLS
package elliptic import ( ) var errInvalidNamedCurve = errors.New("invalid named curve") // CurvePointFormat is used to represent the IANA registered curve points // // https://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-9 type CurvePointFormat byte // CurvePointFormat enums const ( CurvePointFormatUncompressed CurvePointFormat = 0 ) // Keypair is a Curve with a Private/Public Keypair type Keypair struct { Curve Curve PublicKey []byte PrivateKey []byte } // CurveType is used to represent the IANA registered curve types for TLS // // https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-10 type CurveType byte // CurveType enums const ( CurveTypeNamedCurve CurveType = 0x03 ) // CurveTypes returns all known curves func () map[CurveType]struct{} { return map[CurveType]struct{}{ CurveTypeNamedCurve: {}, } } // Curve is used to represent the IANA registered curves for TLS // // https://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-8 type Curve uint16 // Curve enums const ( P256 Curve = 0x0017 P384 Curve = 0x0018 X25519 Curve = 0x001d ) func ( Curve) () string { switch { case P256: return "P-256" case P384: return "P-384" case X25519: return "X25519" } return fmt.Sprintf("%#x", uint16()) } // Curves returns all curves we implement func () map[Curve]bool { return map[Curve]bool{ X25519: true, P256: true, P384: true, } } // GenerateKeypair generates a keypair for the given Curve func ( Curve) (*Keypair, error) { switch { //nolint:revive case X25519: := make([]byte, 32) if , := rand.Read(); != nil { return nil, } var , [32]byte copy([:], ) curve25519.ScalarBaseMult(&, &) return &Keypair{X25519, [:], [:]}, nil case P256: return ellipticCurveKeypair(P256, elliptic.P256(), elliptic.P256()) case P384: return ellipticCurveKeypair(P384, elliptic.P384(), elliptic.P384()) default: return nil, errInvalidNamedCurve } } func ellipticCurveKeypair( Curve, , elliptic.Curve) (*Keypair, error) { , , , := elliptic.GenerateKey(, rand.Reader) if != nil { return nil, } return &Keypair{, elliptic.Marshal(, , ), }, nil }