Skip to main content

RsaPublicKey

Struct RsaPublicKey 

Source
pub struct RsaPublicKey<const LIMBS: usize> { /* private fields */ }
Expand description

An RSA public key (n, e).

Implementations§

Source§

impl<const LIMBS: usize> RsaPublicKey<LIMBS>

Source

pub fn new(n: Uint<LIMBS>, e: Uint<LIMBS>) -> Self

Creates a public key from a modulus and exponent.

Source

pub fn modulus(&self) -> &Uint<LIMBS>

The modulus n.

Source

pub fn exponent(&self) -> &Uint<LIMBS>

The public exponent e.

Source

pub fn raw(&self, m: &Uint<LIMBS>) -> Uint<LIMBS>

The raw RSA public operation m^e mod n (encryption / signature verification primitive). m must be less than n.

Source§

impl<const LIMBS: usize> RsaPublicKey<LIMBS>

Source

pub fn to_pkcs1_der(&self) -> Vec<u8>

Encodes the key as a PKCS#1 RSAPublicKey DER structure.

Source

pub fn from_pkcs1_der(der: &[u8]) -> Result<Self, Error>

Decodes a PKCS#1 RSAPublicKey DER structure. Rejects moduli below MIN_RSA_BITS and degenerate public exponents (even/zero n; e < 3, e even, e ≥ n) per the structural shape check derived from RFC 8017 §3.1. The size floor mirrors the boxed parser so the two import paths refuse the same attacker-injected moduli.

Source

pub fn to_pkcs1_pem(&self) -> String

Encodes the key as a PKCS#1 PEM document (-----BEGIN RSA PUBLIC KEY-----).

Source

pub fn from_pkcs1_pem(pem: &str) -> Result<Self, Error>

Decodes a PKCS#1 PEM public key.

Source

pub fn to_spki_der(&self) -> Vec<u8>

Encodes the key as an X.509 SubjectPublicKeyInfo (SPKI) DER structure (RFC 5280 §4.1.2.7). The AlgorithmIdentifier is rsaEncryption (OID 1.2.840.113549.1.1.1) with explicit NULL parameters (RFC 3279 §2.3.1); the BIT STRING body is the PKCS#1 RSAPublicKey DER produced by to_pkcs1_der.

Source

pub fn to_spki_pem(&self) -> String

Encodes the key as a PEM -----BEGIN PUBLIC KEY----- document (RFC 7468). Distinct from the legacy RSA PUBLIC KEY label which carries a bare PKCS#1 body.

Source

pub fn from_spki_der(der: &[u8]) -> Result<Self, Error>

Parses an X.509 SubjectPublicKeyInfo (SPKI) DER structure for an RSA public key. Validates that the algorithm OID is rsaEncryption, the parameters field is an explicit NULL (strict per RFC 3279 §2.3.1 / fix H-7), and the inner BIT STRING decodes as a valid PKCS#1 RSAPublicKey.

Source

pub fn from_spki_pem(pem: &str) -> Result<Self, Error>

Parses an SPKI PEM document (-----BEGIN PUBLIC KEY-----, RFC 7468). The legacy RSA PUBLIC KEY PKCS#1 label is not accepted here — use from_pkcs1_pem for that form.

Source§

impl<const LIMBS: usize> RsaPublicKey<LIMBS>

Source

pub fn encrypt_pkcs1v15<R: RngCore + CryptoRng>( &self, msg: &[u8], rng: &mut R, ) -> Result<Vec<u8>, Error>

Encrypts msg with PKCS#1 v1.5 (RFC 8017 §7.2.1). Returns the LIMBS*8-byte ciphertext.

§Errors

Error::MessageTooLong if msg.len() > k - 11, where k = LIMBS*8.

Source

pub fn encrypt_oaep<D: Digest, R: RngCore + CryptoRng>( &self, msg: &[u8], label: &[u8], rng: &mut R, ) -> Result<Vec<u8>, Error>

Encrypts msg with RSAES-OAEP (RFC 8017 §7.1.1), using hash D for both the label hash and MGF1, and the empty label by default — pass label to bind context. Returns the LIMBS*8-byte ciphertext.

rng must be a cryptographically secure CSPRNG (see CryptoRng) — OAEP’s security reduction depends on the seed being unpredictable.

§Errors

Error::MessageTooLong if msg.len() > k - 2·hLen - 2.

Source

pub fn verify_pkcs1v15<D: Pkcs1Digest>( &self, msg: &[u8], sig: &[u8], ) -> Result<(), Error>

Verifies a PKCS#1 v1.5 signature over msg, hashing with D.

§Errors

Error::Verification if the signature is invalid; Error::InvalidLength if sig is not LIMBS*8 bytes.

Source§

impl<const LIMBS: usize> RsaPublicKey<LIMBS>

Source

pub fn verify_pss<D: Digest>(&self, msg: &[u8], sig: &[u8]) -> Result<(), Error>

Verifies an RSA-PSS signature over msg, hashing with D.

Trait Implementations§

Source§

impl<const LIMBS: usize> Clone for RsaPublicKey<LIMBS>

Source§

fn clone(&self) -> RsaPublicKey<LIMBS>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const LIMBS: usize> Debug for RsaPublicKey<LIMBS>

Source§

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

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

impl<const LIMBS: usize> Eq for RsaPublicKey<LIMBS>

Source§

impl<const LIMBS: usize> PartialEq for RsaPublicKey<LIMBS>

Source§

fn eq(&self, other: &RsaPublicKey<LIMBS>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const LIMBS: usize> StructuralPartialEq for RsaPublicKey<LIMBS>

Auto Trait Implementations§

§

impl<const LIMBS: usize> Freeze for RsaPublicKey<LIMBS>

§

impl<const LIMBS: usize> RefUnwindSafe for RsaPublicKey<LIMBS>

§

impl<const LIMBS: usize> Send for RsaPublicKey<LIMBS>

§

impl<const LIMBS: usize> Sync for RsaPublicKey<LIMBS>

§

impl<const LIMBS: usize> Unpin for RsaPublicKey<LIMBS>

§

impl<const LIMBS: usize> UnsafeUnpin for RsaPublicKey<LIMBS>

§

impl<const LIMBS: usize> UnwindSafe for RsaPublicKey<LIMBS>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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

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.