Skip to content

Commit

Permalink
Add tests for {Public,Private}Key::from_ed25519*
Browse files Browse the repository at this point in the history
Change-Id: I46d64bd2cc81f708bf538a00b8b84b02801e3828
  • Loading branch information
erickt committed Jan 23, 2020
1 parent 2fdd334 commit 60f2b3a
Showing 1 changed file with 67 additions and 3 deletions.
70 changes: 67 additions & 3 deletions src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,40 @@ mod test {
#[test]
fn parse_public_ed25519() {
let key = PublicKey::from_ed25519(ED25519_1_PUBLIC_KEY).unwrap();
assert_eq!(
key.key_id(),
&KeyId::from_str("a9f3ebc9b138762563a9c27b6edd439959e559709babd123e8d449ba2c18c61a")
.unwrap()
);
assert_eq!(key.typ, KeyType::Ed25519);
assert_eq!(key.scheme, SignatureScheme::Ed25519);
}

#[test]
fn parse_public_ed25519_without_keyid_hash_algo() {
let key =
PublicKey::from_ed25519_with_keyid_hash_algorithms(ED25519_1_PUBLIC_KEY, None).unwrap();
assert_eq!(
key.key_id(),
&KeyId::from_str("e0294a3f17cc8563c3ed5fceb3bd8d3f6bfeeaca499b5c9572729ae015566554")
.unwrap()
);
assert_eq!(key.typ, KeyType::Ed25519);
assert_eq!(key.scheme, SignatureScheme::Ed25519);
}

#[test]
fn parse_public_ed25519_with_keyid_hash_algo() {
let key = PublicKey::from_ed25519_with_keyid_hash_algorithms(
ED25519_1_PUBLIC_KEY,
python_tuf_compatibility_keyid_hash_algorithms(),
)
.unwrap();
assert_eq!(
key.key_id(),
&KeyId::from_str("a9f3ebc9b138762563a9c27b6edd439959e559709babd123e8d449ba2c18c61a")
.unwrap(),
);
assert_eq!(key.typ, KeyType::Ed25519);
assert_eq!(key.scheme, SignatureScheme::Ed25519);
}
Expand Down Expand Up @@ -1095,13 +1129,43 @@ mod test {
#[test]
fn ed25519_read_keypair_and_sign() {
let key = PrivateKey::from_ed25519(ED25519_1_PRIVATE_KEY).unwrap();
let msg = b"test";
let pub_key = PublicKey::from_ed25519(ED25519_1_PUBLIC_KEY).unwrap();
assert_eq!(key.public(), &pub_key);

let msg = b"test";
let sig = key.sign(msg).unwrap();
assert_eq!(pub_key.verify(msg, &sig), Ok(()));

let pub_key =
PublicKey::from_spki(&key.public.as_spki().unwrap(), SignatureScheme::Ed25519).unwrap();
// Make sure we match what ring expects.
let ring_key = ring::signature::Ed25519KeyPair::from_pkcs8(ED25519_1_PK8).unwrap();
assert_eq!(key.public().as_bytes(), ring_key.public_key().as_ref());
assert_eq!(sig.value().as_bytes(), ring_key.sign(msg).as_ref());

// Make sure verification fails with the wrong key.
let bad_pub_key = PrivateKey::from_pkcs8(ED25519_2_PK8, SignatureScheme::Ed25519)
.unwrap()
.public()
.clone();

assert_eq!(bad_pub_key.verify(msg, &sig), Err(Error::BadSignature));
}

#[test]
fn ed25519_read_keypair_and_sign_with_keyid_hash_algorithms() {
let key = PrivateKey::from_ed25519_with_keyid_hash_algorithms(
ED25519_1_PRIVATE_KEY,
python_tuf_compatibility_keyid_hash_algorithms(),
)
.unwrap();
let pub_key = PublicKey::from_ed25519_with_keyid_hash_algorithms(
ED25519_1_PUBLIC_KEY,
python_tuf_compatibility_keyid_hash_algorithms(),
)
.unwrap();
assert_eq!(key.public(), &pub_key);

let msg = b"test";
let sig = key.sign(msg).unwrap();
assert_eq!(pub_key.verify(msg, &sig), Ok(()));

// Make sure we match what ring expects.
Expand Down

0 comments on commit 60f2b3a

Please sign in to comment.