Struct PrivateKey

Source
pub struct PrivateKey<S: Suite> {
    pub scalar: NonZero<SecretScalar<S::E>>,
}
Expand description

Private key is a scalar of the elliptic curve in the chosen suite.

You can obtain a private key by generating it with PrivateKey::generate, or by reading it from bytes with PrivateKey::from_bytes.

The scalars are stored as bytes in big-endian format, which might not always be compatible with other software working with this elliptic curve. For example, for EdDSA compatability we provide a method PrivateKey::from_eddsa_pkey_bytes

Fields§

§scalar: NonZero<SecretScalar<S::E>>

d in the standard

Implementations§

Source§

impl<S> PrivateKey<S>
where S: Suite<E = Ed25519>,

Source

pub fn from_eddsa_pkey_bytes(bytes: &[u8; 32]) -> Option<Self>

Since eddsa secret key is not a scalar, and most tools that call themselves ed25519 are actually eddsa, we need to convert from eddsa key to a scalar.

Returns None if the bytes hash to zero (this has a vanishing probability of occuring)

Source§

impl PrivateKey<Curve25519Aes128cbcHmacsha256>

Source

pub fn decrypt_in_place<'m>( &self, message: EncryptedMessage<'m>, ) -> Result<&'m mut [u8], DecError>

Decrypt the message bytes in place; specialization for curve25519aes128_cbchmac. Uses PKCS7 padding.

When you have a buffer of bytes to decrypt, you first need to parse it with EncryptedMessage::from_bytes, and then decrypt the structure using this funciton. It will modify the bytes in the buffer and return a slice to them.

Convenient alias for PrivateKey::block_decrypt_in_place

Source

pub fn decrypt( &self, message: &EncryptedMessage<'_>, ) -> Result<Vec<u8>, DecError>

Decrypt the message bytes into a new buffer; specialization for curve25519aes128_cbchmac. Uses PKCS7 padding.

When you have a buffer of bytes to decrypt, you first need to parse it with EncryptedMessage::from_bytes, and then decrypt the structure using this funciton. It will copy the message bytes into a new buffer and return a Vec containing them.

Convenient alias for PrivateKey::block_decrypt

Source§

impl PrivateKey<Curve25519Xsalsa20Hmacsha256>

Source

pub fn decrypt_in_place<'m>( &self, message: EncryptedMessage<'m>, ) -> Result<&'m mut [u8], DecError>

Decrypt the message bytes in place; specialization for curve25519xsalsa20hmac

When you have a buffer of bytes to decrypt, you first need to parse it with EncryptedMessage::from_bytes, and then decrypt the structure using this funciton. It will modify the bytes in the buffer and return a slice to them.

Convenient alias to PrivateKey::stream_decrypt_in_place

Source

pub fn decrypt( &self, message: &EncryptedMessage<'_>, ) -> Result<Vec<u8>, DecError>

Decrypt the message bytes into a new buffer; specialization for curve25519xsalsa20hmac

When you have a buffer of bytes to decrypt, you first need to parse it with EncryptedMessage::from_bytes, and then decrypt the structure using this funciton. It will copy the message bytes into a new buffer and return a Vec containing them.

Convenient alias to PrivateKey::decrypt_in_place

Source§

impl<S: Suite> PrivateKey<S>

Source

pub fn generate(rng: &mut (impl RngCore + CryptoRng)) -> Self

Generate random key using the provided CryptoRng

Source

pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Option<Self>

Read the bytes as a big-endian number. This might not necessarily be compatible with other software for working with elliptic curves.

Source

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

Stores the scalar as a big-endian number. This might not necessarily be compatible with other software for working with elliptic curves.

Source

pub fn public_key(&self) -> PublicKey<S>

Compute the associated public key Q = g * d

Source§

impl<S: Suite> PrivateKey<S>

Source

pub fn stream_decrypt_in_place<'m>( &self, message: EncryptedMessage<'m, S>, ) -> Result<&'m mut [u8], DecError>
where S::Mac: Mac + KeyInit, S::Dec: KeyIvInit + StreamCipher,

Decrypt the message bytes in place. Variant for suites with stream ciphers.

When you have a buffer of bytes to decrypt, you first need to parse it with EncryptedMessage::from_bytes, and then decrypt the structure using this funciton. It will modify the bytes in the buffer and return a slice to them.

Source

pub fn stream_decrypt( &self, message: &EncryptedMessage<'_, S>, ) -> Result<Vec<u8>, DecError>
where S::Mac: Mac + KeyInit, S::Dec: KeyIvInit + StreamCipher,

Decrypt the message bytes into a new buffer. Variant for suites with stream ciphers.

When you have a buffer of bytes to decrypt, you first need to parse it with EncryptedMessage::from_bytes, and then decrypt the structure using this funciton. It will copy the message bytes into a new buffer and return a Vec containing them.

Source

pub fn block_decrypt_in_place<'m>( &self, message: EncryptedMessage<'m, S>, ) -> Result<&'m mut [u8], DecError>

Decrypt the message bytes in place. Variant for suites with block ciphers. Uses PKCS7 padding.

When you have a buffer of bytes to decrypt, you first need to parse it with EncryptedMessage::from_bytes, and then decrypt the structure using this funciton. It will modify the bytes in the buffer and return a slice to them.

Source

pub fn block_decrypt( &self, message: &EncryptedMessage<'_, S>, ) -> Result<Vec<u8>, DecError>

Decrypt the message bytes into a new buffer. Variant for suites with block ciphers. Uses PKCS7 padding.

When you have a buffer of bytes to decrypt, you first need to parse it with EncryptedMessage::from_bytes, and then decrypt the structure using this funciton. It will copy the message bytes into a new buffer and return a Vec containing them.

Trait Implementations§

Source§

impl<S: Clone + Suite> Clone for PrivateKey<S>
where S::E: Clone,

Source§

fn clone(&self) -> PrivateKey<S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + Suite> Debug for PrivateKey<S>
where S::E: Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> Freeze for PrivateKey<S>

§

impl<S> RefUnwindSafe for PrivateKey<S>
where <<S as Suite>::E as Curve>::Scalar: RefUnwindSafe,

§

impl<S> Send for PrivateKey<S>

§

impl<S> Sync for PrivateKey<S>

§

impl<S> Unpin for PrivateKey<S>

§

impl<S> UnwindSafe for PrivateKey<S>
where <<S as Suite>::E as Curve>::Scalar: RefUnwindSafe,

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 = 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.