Skip to main content

SecretKey

Struct SecretKey 

Source
pub struct SecretKey(/* private fields */);
Expand description

P-224 (secp224r1) ECDSA secret key.

Supports signing and ECDH key agreement. Messages are hashed with SHA-256, truncated to the leftmost 224 bits as specified by FIPS 186-4.

§Signing

ⓘ
use crypto::p224::SecretKey;

let key = SecretKey::generate().unwrap();
let signature = key.sign(b"message").unwrap();

§ECDH key exchange

ⓘ
use crypto::p224::SecretKey;

let alice = SecretKey::generate().unwrap();
let bob = SecretKey::generate().unwrap();
let alice_shared = alice.ecdh(&bob.public_key()).unwrap();
let bob_shared = bob.ecdh(&alice.public_key()).unwrap();
assert_eq!(alice_shared, bob_shared);

§Security

The raw shared secret from ecdh must not be used directly as an encryption key. Apply a KDF (e.g. HKDF) first.

Implementations§

Source§

impl SecretKey

Source

pub fn generate() -> Result<SecretKey, EllipticCurveError>

Generates a fresh random secret key.

Source

pub fn from_bytes(key: &[u8; 28]) -> Result<SecretKey, EllipticCurveError>

Builds a secret key from its 28-byte big-endian scalar.

Returns EllipticCurveError::InvalidKey when the scalar is zero or greater than or equal to the group order.

Source

pub fn public_key(&self) -> PublicKey

Returns the corresponding public key.

Source

pub fn sign(&self, message: &[u8]) -> Result<[u8; 56], EllipticCurveError>

Signs message with ECDSA using SHA-256 (truncated to the leftmost 224 bits) and deterministic (RFC 6979) nonces.

The emitted signature is always in canonical low-s form (s <= n / 2), so a given key and message always produce the exact same signature bytes. This prevents signature malleability for systems that hash the signature bytes (e.g. transaction ids, replay caches).

Verification remains permissive by default (both s and n - s are accepted); use PublicKey::verify_strict if high-s signatures must be rejected.

Source

pub fn ecdh( &self, peer_public: &PublicKey, ) -> Result<[u8; 28], EllipticCurveError>

Computes the ECDH shared secret with peer_public.

§Security

The raw shared secret must not be used directly as an encryption key. Apply a KDF (e.g. HKDF) first.

Source

pub fn to_bytes(&self) -> [u8; 28]

Returns the secret scalar as 28 big-endian bytes.

Trait Implementations§

Source§

impl Clone for SecretKey

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 Drop for SecretKey

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for SecretKey

Source§

impl PartialEq for SecretKey

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SecretKey

Source§

impl Zeroize for SecretKey

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

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> 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.
Source§

impl<T> Zeroize for T
where T: Zeroize,