Skip to content

Commit

Permalink
Expose Ed25519Seed
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed Oct 24, 2023
1 parent bfc51da commit e820e14
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
24 changes: 19 additions & 5 deletions aws-lc-rs/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::fmt::{Debug, Formatter};
use std::mem::MaybeUninit;
use std::ptr::null_mut;

use crate::buffer::Buffer;
#[cfg(feature = "ring-sig-verify")]
use untrusted::Input;
use zeroize::Zeroize;
Expand Down Expand Up @@ -99,12 +100,25 @@ impl Drop for Ed25519KeyPair {

#[derive(Clone)]
#[allow(clippy::module_name_repetitions)]
/// The seed value for the `EdDSA` signature scheme using Curve25519
pub struct Seed<'a>(&'a Ed25519KeyPair);

impl AsRef<[u8]> for Seed<'_> {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0.private_key[..ED25519_PRIVATE_KEY_SEED_LEN]
/// Elliptic curve private key data encoded as a big-endian fixed-length integer.
#[allow(clippy::module_name_repetitions)]
pub struct Ed25519SeedBuffer {
_priv: (),
}

impl Seed<'_> {
/// Exposes the seed encoded as a big-endian fixed-length integer.
///
/// For most use-cases, `EcdsaKeyPair::to_pkcs8()` should be preferred.
///
/// # Errors
/// `error::Unspecified` if serialization failed.
pub fn to_buffer(&self) -> Result<Buffer<'static, Ed25519SeedBuffer>, Unspecified> {
let buffer = Vec::from(&self.0.private_key[..ED25519_PRIVATE_KEY_SEED_LEN]);
Ok(Buffer::<Ed25519SeedBuffer>::new(buffer))
}
}

Expand Down Expand Up @@ -383,7 +397,7 @@ impl Ed25519KeyPair {
}))
}

/// Provides the private key "seed" for this Ed25519 key pair.
/// Provides the private key "seed" for this `Ed25519` key pair.
///
/// For serialization of the key pair, `Ed25519KeyPair::to_pkcs8()` is preferred.
///
Expand Down
4 changes: 3 additions & 1 deletion aws-lc-rs/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ use crate::ec::EcdsaSignatureFormat;
pub use crate::ec::{
EcPublicKeyDer, EcdsaSigningAlgorithm, EcdsaVerificationAlgorithm, PublicKey as EcdsaPublicKey,
};
pub use crate::ed25519::{Ed25519KeyPair, EdDSAParameters, ED25519_PUBLIC_KEY_LEN};
pub use crate::ed25519::{
Ed25519KeyPair, EdDSAParameters, Seed as Ed25519Seed, ED25519_PUBLIC_KEY_LEN,
};

/// The longest signature is an ASN.1 P-384 signature where *r* and *s* are of
/// maximum length with the leading high bit set on each. Then each component
Expand Down
3 changes: 2 additions & 1 deletion aws-lc-rs/tests/ed25519_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,12 @@ fn test_seed() {

let key_pair = Ed25519KeyPair::from_pkcs8(key_pair_doc.as_ref()).unwrap();
let seed = key_pair.seed().unwrap();
let seed_buffer = seed.to_buffer().unwrap();

let pub_key = key_pair.public_key();

let key_pair_copy =
Ed25519KeyPair::from_seed_and_public_key(seed.as_ref(), pub_key.as_ref()).unwrap();
Ed25519KeyPair::from_seed_and_public_key(seed_buffer.as_ref(), pub_key.as_ref()).unwrap();
let key_pair_copy_doc = key_pair_copy.to_pkcs8().unwrap();

assert_eq!(key_pair_doc.as_ref(), key_pair_copy_doc.as_ref());
Expand Down

0 comments on commit e820e14

Please sign in to comment.