Skip to main content

KeyPair

Struct KeyPair 

Source
pub struct KeyPair { /* private fields */ }
Expand description

A signature key pair, held once: the module’s parsed key and its raw public key, never the key’s encoding (decision 0012). Not Clone.

Implementations§

Source§

impl KeyPair

Source

pub fn generate(admitted: &Admitted, a: S) -> Result<KeyPair, CryptoError>

A new key pair of algorithm a. Refuses an algorithm admitted’s profile does not admit outside TLS, or its module line lacks (Admitted::admit_signature).

Source

pub fn from_pkcs8_der( admitted: &Admitted, a: S, der: &[u8], ) -> Result<KeyPair, CryptoError>

Imports a DER RFC 5958 §2 OneAsymmetricKey of algorithm a. Refuses, in this order, an algorithm admitted’s profile does not admit outside TLS, or its module line lacks (Admitted::admit_signature); then any structure its RFCs do not define, before the module parses the key; then a key the module refuses, or one whose carried publicKey or expandedKey is not its own (CryptoError::SignaturePrivateKey). The structure:

  • ECDSA: version v1, id-ecPublicKey with a’s namedCurve (RFC 5480 §2.1.1), privateKey an RFC 5915 §3 ECPrivateKey with a scalar of the curve’s length, optional parameters naming the same curve and an optional uncompressed publicKey;
  • Ed25519: id-Ed25519 without parameters, privateKey the 32-byte CurvePrivateKey (RFC 8410 §3, §7), version v1, or v2 with the 32-byte publicKey;
  • RSA: version v1, rsaEncryption with NULL parameters, privateKey a two-prime RFC 8017 A.1.2 RSAPrivateKey of version 0, then eight positive, minimal INTEGERs, the modulus exactly a’s bit length and the public exponent 65537;
  • ML-DSA-87: id-ml-dsa-87 without parameters, privateKey the seed or both the seed and the expanded key (RFC 9881 §6), version v1, or v2 with the 2,592-byte publicKey. The expanded key alone is refused: the module cannot export such a key again. In the both form the expanded key must be byte for byte the seed’s (RFC 9881 §8.2), and the key held is the seed’s.

Each takes optional [0] attributes in X.690 §11.6 order, and nothing after the key. The key is held parsed; KeyPair::to_pkcs8_der encodes it again.

Source

pub fn algorithm(&self) -> S

The key’s algorithm.

Source

pub fn to_pkcs8_der(&self) -> Result<Zeroizing<Vec<u8>>, CryptoError>

The key’s PKCS#8 encoding, made by the module on each call into a buffer the caller owns and that is wiped on drop: the canonical encoding, not the bytes the key was imported from. ECDSA, RSA and ML-DSA-87 (the seed form) are version v1; Ed25519 is version v2 with its public key (RFC 8410 §7).

Source

pub fn public_key(&self) -> &[u8] ⓘ

The raw public key: the bytes of the SubjectPublicKeyInfo subjectPublicKey BIT STRING.

Source

pub fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, CryptoError>

Signs msg with the held key. RSA keys sign RSASSA-PKCS1-v1_5 with SHA-384, or, for an RSA-PSS variant, RSASSA-PSS with SHA-384, MGF1 with SHA-384 and a 48-byte salt.

Trait Implementations§

Source§

impl Debug for KeyPair

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PublicKeyData for KeyPair

Source§

fn der_bytes(&self) -> &[u8] ⓘ

The public key in DER format
Source§

fn algorithm(&self) -> &'static SignatureAlgorithm

The algorithm used by the key pair
Source§

fn subject_public_key_info(&self) -> Vec<u8> ⓘ

The public key data in DER format Read more
Source§

impl SigningKey for KeyPair

Source§

fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, Error>

Signs msg using the selected algorithm

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.