pub struct KeyPair<PublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, SecretKey: ByteArray<CRYPTO_BOX_SECRETKEYBYTES> + Zeroize> {
pub public_key: PublicKey,
pub secret_key: SecretKey,
}Expand description
Public/secret keypair for use with crate::dryocbox::DryocBox and
libsodium-compatible public-key encryption.
Fields§
§public_key: PublicKeyPublic key
secret_key: SecretKeySecret key
Implementations§
Source§impl KeyPair<Locked<HeapByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>, Locked<HeapByteArray<CRYPTO_BOX_SECRETKEYBYTES>>>
impl KeyPair<Locked<HeapByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>, Locked<HeapByteArray<CRYPTO_BOX_SECRETKEYBYTES>>>
Sourcepub fn new_locked_keypair() -> Result<Self, Error>
Available on crate feature protected only.
pub fn new_locked_keypair() -> Result<Self, Error>
protected only.Sourcepub fn generate_locked_keypair() -> Result<Self, Error>
Available on crate feature protected only.
pub fn generate_locked_keypair() -> Result<Self, Error>
protected only.Sourcepub fn gen_locked_keypair() -> Result<Self, Error>
👎Deprecated: use generate_locked_keypair() instead
Available on crate feature protected only.
pub fn gen_locked_keypair() -> Result<Self, Error>
use generate_locked_keypair() instead
protected only.Returns a new randomly generated locked keypair.
Prefer generate_locked_keypair.
This method is retained for compatibility.
§Errors
Returns the same errors as
generate_locked_keypair.
§Panics
Panics under the same conditions as
generate_locked_keypair.
Sourcepub fn precalculate_locked<OtherPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>(
&self,
third_party_public_key: &OtherPublicKey,
) -> Result<PrecalcSecretKey<Locked<HeapByteArray<CRYPTO_BOX_BEFORENMBYTES>>>, Error>
Available on crate feature protected only.
pub fn precalculate_locked<OtherPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>( &self, third_party_public_key: &OtherPublicKey, ) -> Result<PrecalcSecretKey<Locked<HeapByteArray<CRYPTO_BOX_BEFORENMBYTES>>>, Error>
protected only.Computes a heap-allocated, page-aligned, locked shared secret key
using a secret key from this keypair and
third_party_public_key.
Compatible with libsodium’s crypto_box_beforenm.
§Errors
Returns an error if third_party_public_key is an unacceptable
low-order point or the shared-key allocation cannot be locked.
§Panics
Panics if the page-aligned shared-key allocation cannot be created or its size cannot be represented with guard pages.
Source§impl KeyPair<LockedRO<HeapByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>, LockedRO<HeapByteArray<CRYPTO_BOX_SECRETKEYBYTES>>>
impl KeyPair<LockedRO<HeapByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>, LockedRO<HeapByteArray<CRYPTO_BOX_SECRETKEYBYTES>>>
Sourcepub fn generate_readonly_locked_keypair() -> Result<Self, Error>
Available on crate feature protected only.
pub fn generate_readonly_locked_keypair() -> Result<Self, Error>
protected only.Returns a new randomly generated locked, read-only keypair.
§Errors
Returns Error::Io if either allocation cannot be locked or its
page permissions cannot be changed to read-only.
§Panics
Panics if either page-aligned allocation cannot be created, its size cannot be represented with guard pages, or the operating system’s random number generator fails.
Sourcepub fn gen_readonly_locked_keypair() -> Result<Self, Error>
👎Deprecated: use generate_readonly_locked_keypair() instead
Available on crate feature protected only.
pub fn gen_readonly_locked_keypair() -> Result<Self, Error>
use generate_readonly_locked_keypair() instead
protected only.Returns a new randomly generated locked, read-only keypair.
Prefer
generate_readonly_locked_keypair.
This method is retained for compatibility.
§Errors
Returns the same errors as
generate_readonly_locked_keypair.
§Panics
Panics under the same conditions as
generate_readonly_locked_keypair.
Sourcepub fn precalculate_readonly_locked<OtherPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>(
&self,
third_party_public_key: &OtherPublicKey,
) -> Result<PrecalcSecretKey<LockedRO<HeapByteArray<CRYPTO_BOX_BEFORENMBYTES>>>, Error>
Available on crate feature protected only.
pub fn precalculate_readonly_locked<OtherPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>( &self, third_party_public_key: &OtherPublicKey, ) -> Result<PrecalcSecretKey<LockedRO<HeapByteArray<CRYPTO_BOX_BEFORENMBYTES>>>, Error>
protected only.Computes a heap-allocated, page-aligned, locked, read-only shared
secret key using a secret key from this keypair and
third_party_public_key.
Compatible with libsodium’s crypto_box_beforenm.
§Errors
Returns an error if third_party_public_key is an unacceptable
low-order point, the shared-key allocation cannot be locked, or its
page permissions cannot be changed to read-only.
§Panics
Panics if the page-aligned shared-key allocation cannot be created or its size cannot be represented with guard pages.
Source§impl<PublicKey: NewByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, SecretKey: NewByteArray<CRYPTO_BOX_SECRETKEYBYTES> + Zeroize> KeyPair<PublicKey, SecretKey>
impl<PublicKey: NewByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, SecretKey: NewByteArray<CRYPTO_BOX_SECRETKEYBYTES> + Zeroize> KeyPair<PublicKey, SecretKey>
Sourcepub fn gen() -> Self
👎Deprecated: use generate() instead
pub fn gen() -> Self
use generate() instead
Generates a random keypair.
Prefer generate. gen is retained for compatibility
with older Rust editions.
Sourcepub fn from_secret_key(secret_key: SecretKey) -> Self
pub fn from_secret_key(secret_key: SecretKey) -> Self
Derives the public key for secret_key and returns the complete
keypair, consuming the secret key.
Source§impl KeyPair<StackByteArray<CRYPTO_BOX_PUBLICKEYBYTES>, StackByteArray<CRYPTO_BOX_SECRETKEYBYTES>>
impl KeyPair<StackByteArray<CRYPTO_BOX_PUBLICKEYBYTES>, StackByteArray<CRYPTO_BOX_SECRETKEYBYTES>>
Sourcepub fn generate_with_defaults() -> Self
pub fn generate_with_defaults() -> Self
Randomly generates a new keypair, using default types (stack-allocated byte arrays). Provided for convenience.
Sourcepub fn gen_with_defaults() -> Self
👎Deprecated: use generate_with_defaults() instead
pub fn gen_with_defaults() -> Self
use generate_with_defaults() instead
Randomly generates a new keypair, using default types (stack-allocated byte arrays). Provided for convenience.
Prefer generate_with_defaults. This
method is retained for compatibility.
Source§impl<'a, PublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + TryFrom<&'a [u8]> + Zeroize, SecretKey: ByteArray<CRYPTO_BOX_SECRETKEYBYTES> + TryFrom<&'a [u8]> + Zeroize> KeyPair<PublicKey, SecretKey>
impl<'a, PublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + TryFrom<&'a [u8]> + Zeroize, SecretKey: ByteArray<CRYPTO_BOX_SECRETKEYBYTES> + TryFrom<&'a [u8]> + Zeroize> KeyPair<PublicKey, SecretKey>
Sourcepub fn from_slices(
public_key: &'a [u8],
secret_key: &'a [u8],
) -> Result<Self, Error>
pub fn from_slices( public_key: &'a [u8], secret_key: &'a [u8], ) -> Result<Self, Error>
Constructs a new keypair from key slices, consuming them. Does not check validity or authenticity of keypair.
§Errors
Returns an error if either slice does not have the required key length, or if the target key type rejects the key bytes.
Source§impl<PublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, SecretKey: ByteArray<CRYPTO_BOX_SECRETKEYBYTES> + Zeroize> KeyPair<PublicKey, SecretKey>
impl<PublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, SecretKey: ByteArray<CRYPTO_BOX_SECRETKEYBYTES> + Zeroize> KeyPair<PublicKey, SecretKey>
Sourcepub fn is_valid_public_key<PK: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>(
key: &PK,
) -> bool
pub fn is_valid_public_key<PK: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>( key: &PK, ) -> bool
Checks if the given public key is valid according to X25519 rules.
For X25519 (crypto_box,
DryocBox), this performs a trial scalar
multiplication and rejects public keys that produce an all-zero shared
secret, including low-order inputs rejected by libsodium. As required by
RFC 7748, the high bit of the encoded public key is ignored.
§Validating Protected Keys
You can validate keys stored in protected memory directly, as the validation functions operate on references.
use dryoc::constants::{CRYPTO_BOX_PUBLICKEYBYTES, CRYPTO_BOX_SECRETKEYBYTES};
use dryoc::keypair::protected::{HeapByteArray, LockedRO};
use dryoc::keypair::{KeyPair, PublicKey, SecretKey};
// Generate a keypair stored in locked, read-only memory
let protected_kp: KeyPair<
LockedRO<HeapByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>,
LockedRO<HeapByteArray<CRYPTO_BOX_SECRETKEYBYTES>>,
> = KeyPair::generate_readonly_locked_keypair().expect("Failed to generate locked keypair");
// Validate the X25519 public key.
let is_x25519_valid = KeyPair::<
LockedRO<HeapByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>,
LockedRO<HeapByteArray<CRYPTO_BOX_SECRETKEYBYTES>>,
>::is_valid_public_key(&protected_kp.public_key);
assert!(is_x25519_valid, "Protected X25519 key should be valid");Sourcepub fn is_valid_ed25519_key<PK: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>(
key: &PK,
) -> bool
pub fn is_valid_ed25519_key<PK: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>( key: &PK, ) -> bool
Checks if the given key is a valid prime-order Ed25519 public key.
The canonical compressed encoding is required. The high bit, which encodes the sign of the x-coordinate, may legitimately be set.
This is a strict prime-subgroup policy, not a generic Ed25519 signature
validity predicate. Use it when an application or point-arithmetic
protocol requires canonical, nonidentity, prime-order keys. Verify
signatures with
crypto_sign_verify_detached
instead; some signature profiles intentionally define different
point-acceptance rules.
is_valid_public_key should be used for X25519 keys used in crypto_box.
Sourcepub fn kx_new_client_session<SessionKey: NewByteArray<CRYPTO_KX_SESSIONKEYBYTES> + Zeroize + ZeroizeOnDrop>(
&self,
server_public_key: &PublicKey,
) -> Result<Session<SessionKey>, Error>
pub fn kx_new_client_session<SessionKey: NewByteArray<CRYPTO_KX_SESSIONKEYBYTES> + Zeroize + ZeroizeOnDrop>( &self, server_public_key: &PublicKey, ) -> Result<Session<SessionKey>, Error>
Creates new client session keys using this keypair and
server_public_key, assuming this keypair is for the client.
§Errors
Returns an error if server_public_key is unacceptable, including a
low-order point that would produce an all-zero shared secret.
Sourcepub fn kx_new_server_session<SessionKey: NewByteArray<CRYPTO_KX_SESSIONKEYBYTES> + Zeroize + ZeroizeOnDrop>(
&self,
client_public_key: &PublicKey,
) -> Result<Session<SessionKey>, Error>
pub fn kx_new_server_session<SessionKey: NewByteArray<CRYPTO_KX_SESSIONKEYBYTES> + Zeroize + ZeroizeOnDrop>( &self, client_public_key: &PublicKey, ) -> Result<Session<SessionKey>, Error>
Creates new server session keys using this keypair and
client_public_key, assuming this keypair is for the server.
§Errors
Returns an error if client_public_key is unacceptable, including a
low-order point that would produce an all-zero shared secret.
Sourcepub fn precalculate(
&self,
third_party_public_key: &PublicKey,
) -> Result<PrecalcSecretKey<StackByteArray<CRYPTO_BOX_BEFORENMBYTES>>, Error>
pub fn precalculate( &self, third_party_public_key: &PublicKey, ) -> Result<PrecalcSecretKey<StackByteArray<CRYPTO_BOX_BEFORENMBYTES>>, Error>
Computes a stack-allocated shared secret key using a secret key from
this keypair and third_party_public_key.
Compatible with libsodium’s crypto_box_beforenm.
§Errors
Returns an error if third_party_public_key is an unacceptable
low-order point.