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/private keypair for use with crate::dryocbox::DryocBox, aka
libsodium box
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 nightly only.
pub fn new_locked_keypair() -> Result<Self, Error>
nightly only.Returns a new locked keypair.
Sourcepub fn gen_locked_keypair() -> Result<Self, Error>
Available on crate feature nightly only.
pub fn gen_locked_keypair() -> Result<Self, Error>
nightly only.Returns a new randomly generated 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 nightly only.
pub fn precalculate_locked<OtherPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>( &self, third_party_public_key: &OtherPublicKey, ) -> Result<PrecalcSecretKey<Locked<HeapByteArray<CRYPTO_BOX_BEFORENMBYTES>>>, Error>
nightly 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.
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 gen_readonly_locked_keypair() -> Result<Self, Error>
Available on crate feature nightly only.
pub fn gen_readonly_locked_keypair() -> Result<Self, Error>
nightly only.Returns a new randomly generated locked, read-only 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 nightly 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>
nightly 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.
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 from_secret_key(secret_key: SecretKey) -> Self
pub fn from_secret_key(secret_key: SecretKey) -> Self
Derives a keypair from secret_key, and consumes it, and returns a new
keypair.
Source§impl KeyPair<StackByteArray<CRYPTO_BOX_PUBLICKEYBYTES>, StackByteArray<CRYPTO_BOX_SECRETKEYBYTES>>
impl KeyPair<StackByteArray<CRYPTO_BOX_PUBLICKEYBYTES>, StackByteArray<CRYPTO_BOX_SECRETKEYBYTES>>
Sourcepub fn gen_with_defaults() -> Self
pub fn gen_with_defaults() -> Self
Randomly generates a new keypair, using default types (stack-allocated byte arrays). Provided for convenience.
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>
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), a public key is considered
valid if:
- It is not the all-zero point
[0, ..., 0]. - The high bit of the last byte is 0.
This function verifies these conditions.
Note: This validation is specific to X25519 keys used in
Diffie-Hellman key exchange (crypto_box). It primarily aims to
exclude degenerate keys and does not explicitly verify that the
point lies on the underlying curve, unlike stricter Ed25519 point
validation (see
crypto_core_ed25519_is_valid_point).
§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::gen_readonly_locked_keypair().expect("Failed to generate locked keypair");
// Validate the Ed25519 public key using the relaxed rules appropriate for
// keys generated by crypto_sign_keypair (even though this is an X25519 keypair,
// the validation function itself can be called).
// Note: For an actual Ed25519 keypair from crypto_sign, you'd use the
// crypto_core_ed25519_is_valid_point_relaxed function directly.
// Here we demonstrate calling the KeyPair method.
let is_valid = KeyPair::<
LockedRO<HeapByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>,
LockedRO<HeapByteArray<CRYPTO_BOX_SECRETKEYBYTES>>,
>::is_valid_ed25519_key(&protected_kp.public_key);
// For keys generated by crypto_sign_keypair, relaxed validation should pass.
// (This assertion might depend on the specific key generation details,
// but illustrates the call)
// assert!(is_valid, "Protected key should be valid (relaxed check)");
// Similarly, 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 Ed25519 public key, using relaxed validation rules that allow the high bit to be set.
For Ed25519 public keys, generated by crypto_sign_keypair(), we need
to use more permissive validation since these keys can have the high
bit set.
This method should be used for validating Ed25519 keys (used in
signatures), while 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>(
&self,
server_public_key: &PublicKey,
) -> Result<Session<SessionKey>, Error>
pub fn kx_new_client_session<SessionKey: NewByteArray<CRYPTO_KX_SESSIONKEYBYTES> + Zeroize>( &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.
Sourcepub fn kx_new_server_session<SessionKey: NewByteArray<CRYPTO_KX_SESSIONKEYBYTES> + Zeroize>(
&self,
client_public_key: &PublicKey,
) -> Result<Session<SessionKey>, Error>
pub fn kx_new_server_session<SessionKey: NewByteArray<CRYPTO_KX_SESSIONKEYBYTES> + Zeroize>( &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.
Sourcepub fn precalculate(
&self,
third_party_public_key: &PublicKey,
) -> PrecalcSecretKey<StackByteArray<CRYPTO_BOX_BEFORENMBYTES>>
pub fn precalculate( &self, third_party_public_key: &PublicKey, ) -> PrecalcSecretKey<StackByteArray<CRYPTO_BOX_BEFORENMBYTES>>
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.