core/crypto/ecdh
ecdh
Types
3Curve
Curve :: enum int {
Invalid = 0,
SECP256R1 = 1,
SECP384R1 = 2,
X25519 = 3,
X448 = 4,
}SourceCurve the curve identifier associated with a given Private_Key or Public_Key
Private_Key
Private_Key :: struct {
// WARNING: All of the members are to be treated as internal (ie:
// the Private_Key structure is intended to be opaque).
_curve: Curve,
_impl: union {
secec.Scalar_p256r1,
secec.Scalar_p384r1,
X25519_Buf,
X448_Buf,
},
_pub_key: Public_Key,
}SourcePrivate_Key is an ECDH private key.
Public_Key
Public_Key :: struct {
// WARNING: All of the members are to be treated as internal (ie:
// the Public_Key structure is intended to be opaque).
_curve: Curve,
_impl: union {
secec.Point_p256r1,
secec.Point_p384r1,
X25519_Buf,
X448_Buf,
},
}SourcePublic_Key is an ECDH public key.
Variables
4CURVE_NAMES
CURVE_NAMES :: [5]string = [Curve]string {
.Invalid = "Invalid",
.SECP256R1 = "secp256r1",
.SECP384R1 = "secp384r1",
.X25519 = "X25519",
.X448 = "X448",
}SourceCURVE_NAMES is the Curve to curve name string.
PRIVATE_KEY_SIZES
PRIVATE_KEY_SIZES :: [5]int = [Curve]int {
.Invalid = 0,
.SECP256R1 = secec.SC_SIZE_P256R1,
.SECP384R1 = secec.SC_SIZE_P384R1,
.X25519 = x25519.SCALAR_SIZE,
.X448 = x448.SCALAR_SIZE,
}SourcePRIVATE_KEY_SIZES is the Curve to private key size in bytes.
PUBLIC_KEY_SIZES
PUBLIC_KEY_SIZES :: [5]int = [Curve]int {
.Invalid = 0,
.SECP256R1 = 1 + 2 * secec.FE_SIZE_P256R1,
.SECP384R1 = 1 + 2 * secec.FE_SIZE_P384R1,
.X25519 = x25519.POINT_SIZE,
.X448 = x448.POINT_SIZE,
}SourcePUBLIC_KEY_SIZES is the Curve to public key size in bytes.
SHARED_SECRET_SIZES
SHARED_SECRET_SIZES :: [5]int = [Curve]int {
.Invalid = 0,
.SECP256R1 = secec.FE_SIZE_P256R1,
.SECP384R1 = secec.FE_SIZE_P384R1,
.X25519 = x25519.POINT_SIZE,
.X448 = x448.POINT_SIZE,
}SourceSHARED_SECRET_SIZES is the Curve to shared secret size in bytes.
Procedures
17curve
curve :: proc(k: ^T) -> (Curve)Sourcecurve returns the Curve used by a Private_Key or Public_Key instance.
ecdh
ecdh :: proc(priv_key: ^Private_Key, pub_key: ^Public_Key, dst: []u8) -> (bool)Sourceecdh performs an Elliptic Curve Diffie-Hellman key exchange betwween the Private_Key and Public_Key, writing the shared secret to dst.
The neutral element is rejected as an error.
key_size
key_size :: proc(k: ^T) -> (int)Sourcekey_size returns the key size of a Private_Key or Public_Key in bytes.
private_key_bytes
private_key_bytes :: proc(priv_key: ^Private_Key, dst: []u8)Sourceprivate_key_bytes sets dst to byte-encoding of priv_key.
private_key_clear
private_key_clear :: proc(priv_key: ^Private_Key)Sourceprivate_key_clear clears priv_key to the uninitialized state.
private_key_equal
private_key_equal :: proc(p: ^Private_Key, q: ^Private_Key) -> (bool)Sourceprivate_key_equal returns true if and only if (⟺) the private keys are equal, in constant time.
private_key_generate
private_key_generate :: proc(priv_key: ^Private_Key, curve: Curve) -> (bool)Sourceprivate_key_generate uses the system entropy source to generate a new Private_Key. This will only fail if and only if (⟺) the system entropy source is missing or broken.
private_key_public_bytes
private_key_public_bytes :: proc(priv_key: ^Private_Key, dst: []u8)Sourceprivate_key_public_bytes sets dst to the byte-encoding of the public key corresponding to priv_key.
private_key_set
private_key_set :: proc(priv_key: ^Private_Key, src: ^Private_Key)Sourceprivate_key_set sets priv_key to src.
private_key_set_bytes
private_key_set_bytes :: proc(priv_key: ^Private_Key, curve: Curve, b: []u8) -> (bool)Sourceprivate_key_set_bytes decodes a byte-encoded private key, and returns true if and only if (⟺) the operation was successful.
public_key_bytes
public_key_bytes :: proc(pub_key: ^Public_Key, dst: []u8)Sourcepublic_key_bytes sets dst to byte-encoding of pub_key.
public_key_clear
public_key_clear :: proc(pub_key: ^Public_Key)Sourcepublic_key_clear clears pub_key to the uninitialized state.
public_key_equal
public_key_equal :: proc(p: ^Public_Key, q: ^Public_Key) -> (bool)Sourcepublic_key_equal returns true if and only if (⟺) the public keys are equal, in constant time.
public_key_set
public_key_set :: proc(pub_key: ^Public_Key, src: ^Public_Key)Sourcepublic_key_set sets pub_key to src.
public_key_set_bytes
public_key_set_bytes :: proc(pub_key: ^Public_Key, curve: Curve, b: []u8) -> (bool)Sourcepublic_key_set_bytes decodes a byte-encoded public key, and returns true if and only if (⟺) the operation was successful.
public_key_set_priv
public_key_set_priv :: proc(pub_key: ^Public_Key, priv_key: ^Private_Key)Sourcepublic_key_set_priv sets pub_key to the public component of priv_key.
shared_secret_size
shared_secret_size :: proc(k: ^T) -> (int)Sourceshared_secret_size returns the shared secret size of a key exchange in bytes.