Skip to main content

PublicKey

Struct PublicKey 

Source
#[non_exhaustive]
pub struct PublicKey { /* private fields */ }
Expand description

Public recipient key for FerroCrypt public-key encryption.

Today, public recipient keys are native X25519 public keys. A PublicKey can be read from a public.key file, wrap raw 32-byte X25519 public material, or be decoded from a Bech32 fcr1… recipient string. Every constructor resolves its source immediately, so the value holds key material and never re-reads where that material came from.

That single resolution point is what makes an identity check meaningful: a fingerprint taken from a PublicKey describes the key that same value will encrypt to. Verify the fingerprint out of band, then keep the value itself through the encryption call — passing the path again instead would re-read a file that may have changed in between.

Once constructed, a PublicKey can be:

Cloning retains the resolved key material and suite; it does not read the source again. Checksums validate the encoding, not the key owner’s identity.

This type implements Clone but deliberately not Copy: future recipient types may hold key material that is not Copy.

Implementations§

Source§

impl PublicKey

Source

pub fn from_key_file(path: impl AsRef<Path>) -> Result<Self, CryptoError>

Reads and parses a FerroCrypt public.key file under KeyReadLimits::default.

The file is read exactly once, by this call. Every later method uses the key material resolved here, so a change to the file afterwards cannot alter what this value fingerprints as or encrypts to.

§Errors

Returns CryptoError::InputPath if the file does not exist, and CryptoError::Io for other read failures. Returns CryptoError::InvalidFormat or CryptoError::RecipientStringCapExceeded if the file is not a valid public.key file, CryptoError::UnsupportedVersion for a public key from an unsupported keypair suite, and CryptoError::UnsupportedKeyType for a valid public key of a key type this build does not support.

Source

pub fn from_key_file_with_limits( path: impl AsRef<Path>, limits: KeyReadLimits, ) -> Result<Self, CryptoError>

Reads a FerroCrypt public.key file under caller-supplied limits.

Same single read as PublicKey::from_key_file, with limits applied to the parse.

§Errors

Same as PublicKey::from_key_file.

Source

pub fn from_x25519_bytes(bytes: [u8; 32]) -> Result<Self, CryptoError>

Wraps raw 32-byte X25519 public-key material directly.

Assigns the current writer’s key-pair suite. Raw bytes carry no suite marker; use PublicKey::from_recipient_string or PublicKey::from_key_file when loading an existing encoded key so its suite is preserved. This constructor cannot verify that a separately stored private key belongs to a supported suite.

Rejects the all-zero point and any non-canonical encoding structurally. Per FORMAT.md §2.4 the accepted material is a 32-byte little-endian integer strictly below the Curve25519 field prime; the X25519 math would accept an alias of a valid key (for example with the top bit set), but the raw bytes are bound into fingerprints, recipient strings, and the wrap-key derivation, so an alias is rejected rather than normalized. Other degenerate inputs are caught at the ECDH site by wrap / unwrap’s shared-secret check.

§Errors

Returns CryptoError::InvalidFormat with FormatDefect::MalformedPublicKey if bytes is the all-zero X25519 public key or a non-canonical encoding.

Source

pub fn from_recipient_string(recipient: &str) -> Result<Self, CryptoError>

Decodes a canonical lowercase Bech32 fcr1… recipient string into a PublicKey. The accepted grammar is the string produced by PublicKey::to_recipient_string, which is also the content of a public.key file without the optional trailing newline. Validates HRP, BIP 173 checksum, internal SHA3-256 checksum, payload structural fields, type-name grammar, and, for X25519 recipients, type_name == "x25519" with exactly 32 bytes of non-zero, canonically encoded key material (FORMAT.md §2.4 / §7).

The keypair suite recovered from the wire-version byte is preserved on the resulting PublicKey. Re-encoding via PublicKey::to_recipient_string uses the parsed suite, not the current writer suite, so a recipient string round-trips byte-identically as long as the original suite is still supported by this build.

§Errors

Returns CryptoError::InvalidFormat with FormatDefect::MalformedPublicKey for text that is not a canonical recipient string and for a malformed payload (an invalid payload type name reports FormatDefect::MalformedTypeName instead), CryptoError::UnsupportedVersion for a recipient string from an unsupported keypair suite, CryptoError::UnsupportedKeyType for a valid recipient string of a key type this build does not support, and CryptoError::RecipientStringCapExceeded when the input exceeds the KeyReadLimits::default recipient-string cap.

Source

pub fn from_recipient_string_with_limits( recipient: &str, limits: KeyReadLimits, ) -> Result<Self, CryptoError>

Decodes a recipient string under caller-supplied length limits.

Same validation as PublicKey::from_recipient_string, with the recipient-string cap taken from limits.

§Errors

Same as PublicKey::from_recipient_string.

Source

pub fn fingerprint(&self) -> Result<String, CryptoError>

Computes the public-key fingerprint.

Returns 64 lowercase hexadecimal characters: SHA3-256 over type_name || 0x00 || key_material, using the "x25519" type name. The type name and zero-byte delimiter distinguish hash inputs for different recipient types. Matches the ferrocrypt fingerprint subcommand. Verify this fingerprint through a trusted channel to establish the recipient’s identity.

§Errors

Returns the same errors as PublicKey::to_x25519_bytes.

Source

pub fn to_recipient_string(&self) -> Result<String, CryptoError>

Encodes the key as the canonical lowercase Bech32 fcr1… recipient string.

Re-encodes using the keypair suite the key was originally constructed with (preserved on every PublicKey ingress path), not the current writer suite. A PublicKey parsed from a fcr1… string round-trips byte-identically; a PublicKey built from raw bytes via PublicKey::from_x25519_bytes re-encodes using the writer suite (the suite from_x25519_bytes pins).

§Errors

Returns the same errors as PublicKey::to_x25519_bytes, plus CryptoError::InternalInvariant if canonical Bech32 encoding fails for already-validated X25519 bytes.

Source

pub fn to_x25519_bytes(&self) -> Result<[u8; 32], CryptoError>

Returns the raw 32-byte X25519 public-key material as an owned array.

§Errors

Reserved for a future recipient type that carries no X25519 material. Every PublicKey this build can construct holds a validated X25519 key, so the call currently always succeeds.

Trait Implementations§

Source§

impl Clone for PublicKey

Source§

fn clone(&self) -> Self

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 Debug for PublicKey

Source§

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

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

impl FromStr for PublicKey

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a Bech32 fcr1… recipient string into a PublicKey. Equivalent to PublicKey::from_recipient_string, enabling "fcr1…".parse::<PublicKey>().

Source§

type Err = CryptoError

The associated error which can be returned from parsing.

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> 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> Same for T

Source§

type Output = T

Should always be Self
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 = !

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.