Struct orion::hazardous::kem::x25519_hkdf_sha256::SharedSecret
source · pub struct SharedSecret { /* private fields */ }
safe_api
only.Expand description
A type to represent the SharedSecret
that DH-KEM(X25519, HKDF-SHA256) produces.
This type simply holds bytes. Creating an instance from slices or similar, performs no checks whatsoever.
Errors:
An error will be returned if:
slice
is not 32 bytes.
Security:
-
Avoid using
unprotected_as_bytes()
whenever possible, as it breaks all protections that the type implements. -
The trait
PartialEq<&'_ [u8]>
is implemented for this type so that users are not tempted to callunprotected_as_bytes
to compare this sensitive value to a byte slice. The trait is implemented in such a way that the comparison happens in constant time. Thus, users should preferSecretType == &[u8]
overSecretType.unprotected_as_bytes() == &[u8]
. Examples are shown below. The examples apply to any type that implementsPartialEq<&'_ [u8]>
.
use orion::hazardous::stream::chacha20::SecretKey;
// Initialize a secret key with random bytes.
let secret_key = SecretKey::generate();
// Secure, constant-time comparison with a byte slice
assert_ne!(secret_key, &[0; 32][..]);
// Secure, constant-time comparison with another SecretKey
assert_ne!(secret_key, SecretKey::generate());
Implementations§
sourcepub fn from_slice(slice: &[u8]) -> Result<SharedSecret, UnknownCryptoError>
pub fn from_slice(slice: &[u8]) -> Result<SharedSecret, UnknownCryptoError>
Construct from a given byte slice.
sourcepub fn unprotected_as_bytes(&self) -> &[u8] ⓘ
pub fn unprotected_as_bytes(&self) -> &[u8] ⓘ
Return the object as byte slice. Warning: Should not be used unless strictly needed. This breaks protections that the type implements.
Trait Implementations§
source§fn eq(&self, other: &SharedSecret) -> bool
fn eq(&self, other: &SharedSecret) -> bool
self
and other
values to be equal, and is used
by ==
.