Struct mbedtls::pk::Pk

source ·
#[repr(C)]
pub struct Pk { /* private fields */ }

Implementations§

source§

impl Pk

source

pub fn from_private_key(key: &[u8], password: Option<&[u8]>) -> Result<Pk>

Takes both DER and PEM forms of PKCS#1 or PKCS#8 encoded keys.

When calling on PEM-encoded data, key must be NULL-terminated

source

pub fn from_public_key(key: &[u8]) -> Result<Pk>

Takes both DER and PEM encoded SubjectPublicKeyInfo keys.

When calling on PEM-encoded data, key must be NULL-terminated

source

pub fn generate_rsa<F: Random>( rng: &mut F, bits: u32, exponent: u32 ) -> Result<Pk>

source

pub fn generate_ec<F: Random, C: TryInto<EcGroup, Error = impl Into<Error>>>( rng: &mut F, curve: C ) -> Result<Pk>

source

pub fn private_from_ec_components( curve: EcGroup, private_key: Mpi ) -> Result<Pk>

👎Deprecated since 0.12.3: This function does not accept an RNG so it’s vulnerable to side channel attacks. Please use private_from_ec_scalar_with_rng instead.
source

pub fn private_from_ec_scalar_with_rng<F: Random>( curve: EcGroup, private_key: Mpi, rng: &mut F ) -> Result<Pk>

Constructs a private key from elliptic curve components.

This function requires a random number generator (RNG) because it uses the EcPoint::mul function which requires an RNG for blinding.

It initializes a Pk context, generates the public key point by multiplying the curve generator with the private key, and sets up the private key context with the generated curve, private key, and public point.

§Arguments
  • rng - The RNG used for blinding in the EcPoint::mul function.
  • curve - The elliptic curve group to use for key generation.
  • private_key - The private key as an MPI (Multiple Precision Integer).
§Returns
  • Result<Pk> - A Pk context filled with the generated private key on success.
§Errors

This function will return an error if:

  • Fails to generate EcPoint from given EcGroup in curve.
  • The underlying C mbedtls_pk_setup function fails to set up the Pk context.
  • The EcPoint::mul_with_rng function fails to generate the public key point.
source

pub fn public_from_ec_components( curve: EcGroup, public_point: EcPoint ) -> Result<Pk>

source

pub fn private_from_rsa_components( components: RsaPrivateComponents<'_> ) -> Result<Pk>

Construct a private key from RSA components.

source

pub fn public_from_rsa_components( components: RsaPublicComponents<'_> ) -> Result<Pk>

Construct a public key from RSA components.

source

pub fn public_custom_algo(algo_id: &[u64], pk: &[u8]) -> Result<Pk>

source

pub fn private_custom_algo(algo_id: &[u64], pk: &[u8], sk: &[u8]) -> Result<Pk>

source

pub fn custom_algo_id(&self) -> Result<&[u64]>

source

pub fn custom_public_key(&self) -> Result<&[u8]>

source

pub fn custom_private_key(&self) -> Result<&[u8]>

source

pub fn set_options(&mut self, options: Options)

Panics if the options are not valid for this key type.

source

pub fn can_do(&self, t: Type) -> bool

source

pub fn check_pair(public: &Self, private: &Self) -> bool

source

pub fn len(&self) -> usize

Key length in bits

source

pub fn pk_type(&self) -> Type

source

pub fn curve(&self) -> Result<EcGroupId>

source

pub fn curve_oid(&self) -> Result<Vec<u64>>

source

pub fn ec_group(&self) -> Result<EcGroup>

source

pub fn ec_public(&self) -> Result<EcPoint>

source

pub fn ec_private(&self) -> Result<Mpi>

source

pub fn rsa_public_modulus(&self) -> Result<Mpi>

source

pub fn rsa_private_prime1(&self) -> Result<Mpi>

source

pub fn rsa_private_prime2(&self) -> Result<Mpi>

source

pub fn rsa_private_exponent(&self) -> Result<Mpi>

source

pub fn rsa_crt_dp(&self) -> Result<Mpi>

source

pub fn rsa_crt_dq(&self) -> Result<Mpi>

source

pub fn rsa_crt_qp(&self) -> Result<Mpi>

source

pub fn rsa_public_exponent(&self) -> Result<u32>

source

pub fn name(&self) -> Result<&str>

source

pub fn decrypt<F: Random>( &mut self, cipher: &[u8], plain: &mut [u8], rng: &mut F ) -> Result<usize>

source

pub fn decrypt_with_label<F: Random>( &mut self, cipher: &[u8], plain: &mut [u8], rng: &mut F, label: &[u8] ) -> Result<usize>

Decrypt using a custom label.

This function may only be called on an RSA key with its padding set to RSA_PKCS_V21.

source

pub fn encrypt<F: Random>( &mut self, plain: &[u8], cipher: &mut [u8], rng: &mut F ) -> Result<usize>

source

pub fn encrypt_with_label<F: Random>( &mut self, plain: &[u8], cipher: &mut [u8], rng: &mut F, label: &[u8] ) -> Result<usize>

Encrypt using a custom label.

This function may only be called on an RSA key with its padding set to RSA_PKCS_V21.

source

pub fn sign<F: Random>( &mut self, md: MdType, hash: &[u8], sig: &mut [u8], rng: &mut F ) -> Result<usize>

Sign the hash hash of type md, placing the signature in sig. rng must be a cryptographically secure RNG.

For RSA signatures, the length of sig must be greater than or equal to the RSA modulus length, otherwise sign() fails with Error::PkSigLenMismatch.

For EC signatures, the length of sig must be greater than or equal to ECDSA_MAX_LEN, otherwise sign() fails with Error::PkSigLenMismatch.

On success, returns the actual number of bytes written to sig.

source

pub fn sign_deterministic<F: Random>( &mut self, md: MdType, hash: &[u8], sig: &mut [u8], rng: &mut F ) -> Result<usize>

source

pub fn verify(&mut self, md: MdType, hash: &[u8], sig: &[u8]) -> Result<()>

source

pub fn agree<F: Random>( &mut self, other: &Pk, shared: &mut [u8], rng: &mut F ) -> Result<usize>

Agree on a shared secret with another public key.

source

pub fn write_private_der<'buf>( &mut self, buf: &'buf mut [u8] ) -> Result<Option<&'buf [u8]>>

source

pub fn write_private_der_vec(&mut self) -> Result<Vec<u8>>

source

pub fn write_private_pem<'buf>( &mut self, buf: &'buf mut [u8] ) -> Result<Option<&'buf [u8]>>

source

pub fn write_private_pem_string(&mut self) -> Result<String>

source

pub fn write_public_der<'buf>( &mut self, buf: &'buf mut [u8] ) -> Result<Option<&'buf [u8]>>

source

pub fn write_public_der_vec(&mut self) -> Result<Vec<u8>>

source

pub fn write_public_pem<'buf>( &mut self, buf: &'buf mut [u8] ) -> Result<Option<&'buf [u8]>>

source

pub fn write_public_pem_string(&mut self) -> Result<String>

Trait Implementations§

source§

impl Drop for Pk

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a> Into<*const pk_context> for &'a Pk

source§

fn into(self) -> *const pk_context

Converts this type into the (usually inferred) input type.
source§

impl<'a> Into<*mut pk_context> for &'a mut Pk

source§

fn into(self) -> *mut pk_context

Converts this type into the (usually inferred) input type.
source§

impl Send for Pk

source§

impl Sync for Pk

Auto Trait Implementations§

§

impl RefUnwindSafe for Pk

§

impl Unpin for Pk

§

impl UnwindSafe for Pk

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>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.