2017-05-06 10:34:50 -10:00
|
|
|
// Copyright 2015-2017 Brian Smith.
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
|
|
|
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2018-12-08 13:16:12 -10:00
|
|
|
use ring::{
|
2023-10-01 19:38:23 -07:00
|
|
|
error, rand,
|
2018-12-21 15:29:30 -10:00
|
|
|
signature::{self, Ed25519KeyPair, KeyPair},
|
2019-02-02 12:11:19 -10:00
|
|
|
test, test_file,
|
2018-12-08 13:16:12 -10:00
|
|
|
};
|
2017-05-06 10:34:50 -10:00
|
|
|
|
2021-12-08 15:44:03 -08:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
|
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
wasm_bindgen_test_configure!(run_in_browser);
|
|
|
|
|
2017-05-06 10:34:50 -10:00
|
|
|
/// Test vectors from BoringSSL.
|
|
|
|
#[test]
|
|
|
|
fn test_signature_ed25519() {
|
2019-02-02 12:11:19 -10:00
|
|
|
test::run(test_file!("ed25519_tests.txt"), |section, test_case| {
|
2017-05-06 10:34:50 -10:00
|
|
|
assert_eq!(section, "");
|
2017-05-07 09:22:03 -10:00
|
|
|
let seed = test_case.consume_bytes("SEED");
|
|
|
|
assert_eq!(32, seed.len());
|
|
|
|
|
2017-05-06 10:34:50 -10:00
|
|
|
let public_key = test_case.consume_bytes("PUB");
|
|
|
|
assert_eq!(32, public_key.len());
|
2017-05-07 09:22:03 -10:00
|
|
|
|
2017-05-06 10:34:50 -10:00
|
|
|
let msg = test_case.consume_bytes("MESSAGE");
|
2017-05-07 09:22:03 -10:00
|
|
|
|
2017-05-06 10:34:50 -10:00
|
|
|
let expected_sig = test_case.consume_bytes("SIG");
|
|
|
|
|
2017-05-07 09:22:03 -10:00
|
|
|
{
|
2019-04-05 14:08:09 -10:00
|
|
|
let key_pair = Ed25519KeyPair::from_seed_and_public_key(&seed, &public_key).unwrap();
|
2017-05-07 09:22:03 -10:00
|
|
|
let actual_sig = key_pair.sign(&msg);
|
|
|
|
assert_eq!(&expected_sig[..], actual_sig.as_ref());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test PKCS#8 generation, parsing, and private-to-public calculations.
|
2019-04-05 14:08:09 -10:00
|
|
|
let rng = test::rand::FixedSliceRandom { bytes: &seed };
|
2017-05-07 09:22:03 -10:00
|
|
|
let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
|
2019-04-05 14:08:09 -10:00
|
|
|
let key_pair = Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).unwrap();
|
2019-04-05 12:46:58 -10:00
|
|
|
assert_eq!(public_key, key_pair.public_key().as_ref());
|
2017-05-07 09:22:03 -10:00
|
|
|
|
|
|
|
// Test Signature generation.
|
2017-05-06 10:34:50 -10:00
|
|
|
let actual_sig = key_pair.sign(&msg);
|
|
|
|
assert_eq!(&expected_sig[..], actual_sig.as_ref());
|
|
|
|
|
2017-05-07 09:22:03 -10:00
|
|
|
// Test Signature verification.
|
2020-12-17 18:42:44 -08:00
|
|
|
test_signature_verification(&public_key, &msg, &expected_sig, Ok(()));
|
2019-04-05 12:46:58 -10:00
|
|
|
|
2019-04-05 16:25:51 -10:00
|
|
|
let mut tampered_sig = expected_sig;
|
|
|
|
tampered_sig[0] ^= 1;
|
|
|
|
|
2020-12-17 18:42:44 -08:00
|
|
|
test_signature_verification(&public_key, &msg, &tampered_sig, Err(error::Unspecified));
|
2019-04-05 16:25:51 -10:00
|
|
|
|
2017-05-06 10:34:50 -10:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:49:07 -08:00
|
|
|
/// Test vectors from BoringSSL.
|
|
|
|
#[test]
|
|
|
|
fn test_signature_ed25519_verify() {
|
|
|
|
test::run(
|
|
|
|
test_file!("ed25519_verify_tests.txt"),
|
|
|
|
|section, test_case| {
|
|
|
|
assert_eq!(section, "");
|
|
|
|
|
|
|
|
let public_key = test_case.consume_bytes("PUB");
|
|
|
|
let msg = test_case.consume_bytes("MESSAGE");
|
|
|
|
let sig = test_case.consume_bytes("SIG");
|
|
|
|
let expected_result = match test_case.consume_string("Result").as_str() {
|
|
|
|
"P" => Ok(()),
|
|
|
|
"F" => Err(error::Unspecified),
|
|
|
|
s => panic!("{:?} is not a valid result", s),
|
|
|
|
};
|
|
|
|
test_signature_verification(&public_key, &msg, &sig, expected_result);
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:42:44 -08:00
|
|
|
fn test_signature_verification(
|
|
|
|
public_key: &[u8],
|
|
|
|
msg: &[u8],
|
|
|
|
sig: &[u8],
|
|
|
|
expected_result: Result<(), error::Unspecified>,
|
|
|
|
) {
|
|
|
|
assert_eq!(
|
|
|
|
expected_result,
|
|
|
|
signature::UnparsedPublicKey::new(&signature::ED25519, public_key).verify(msg, sig)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-06 10:34:50 -10:00
|
|
|
#[test]
|
2017-05-07 09:28:28 -10:00
|
|
|
fn test_ed25519_from_seed_and_public_key_misuse() {
|
2017-05-07 09:22:03 -10:00
|
|
|
const PRIVATE_KEY: &[u8] = include_bytes!("ed25519_test_private_key.bin");
|
|
|
|
const PUBLIC_KEY: &[u8] = include_bytes!("ed25519_test_public_key.bin");
|
2017-05-06 10:34:50 -10:00
|
|
|
|
2019-04-05 14:08:09 -10:00
|
|
|
assert!(Ed25519KeyPair::from_seed_and_public_key(PRIVATE_KEY, PUBLIC_KEY).is_ok());
|
2017-05-06 10:34:50 -10:00
|
|
|
|
|
|
|
// Truncated private key.
|
2019-04-05 14:08:09 -10:00
|
|
|
assert!(Ed25519KeyPair::from_seed_and_public_key(&PRIVATE_KEY[..31], PUBLIC_KEY).is_err());
|
2017-05-06 10:34:50 -10:00
|
|
|
|
|
|
|
// Truncated public key.
|
2019-04-05 14:08:09 -10:00
|
|
|
assert!(Ed25519KeyPair::from_seed_and_public_key(PRIVATE_KEY, &PUBLIC_KEY[..31]).is_err());
|
2017-05-06 10:34:50 -10:00
|
|
|
|
|
|
|
// Swapped public and private key.
|
2019-04-05 14:08:09 -10:00
|
|
|
assert!(Ed25519KeyPair::from_seed_and_public_key(PUBLIC_KEY, PRIVATE_KEY).is_err());
|
2017-05-06 10:34:50 -10:00
|
|
|
}
|
2017-05-07 11:42:22 -10:00
|
|
|
|
2022-02-20 14:34:44 -08:00
|
|
|
enum FromPkcs8Variant {
|
|
|
|
Checked,
|
|
|
|
MaybeUnchecked,
|
|
|
|
}
|
|
|
|
|
2017-05-07 11:42:22 -10:00
|
|
|
#[test]
|
|
|
|
fn test_ed25519_from_pkcs8_unchecked() {
|
2022-02-20 13:19:22 -08:00
|
|
|
test_ed25519_from_pkcs8_(
|
2022-02-20 14:34:44 -08:00
|
|
|
FromPkcs8Variant::MaybeUnchecked,
|
2022-02-20 13:19:22 -08:00
|
|
|
Ed25519KeyPair::from_pkcs8_maybe_unchecked,
|
|
|
|
)
|
2017-05-07 11:42:22 -10:00
|
|
|
}
|
2017-05-07 13:17:57 -10:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ed25519_from_pkcs8() {
|
2022-02-20 14:34:44 -08:00
|
|
|
test_ed25519_from_pkcs8_(FromPkcs8Variant::Checked, Ed25519KeyPair::from_pkcs8)
|
2022-02-20 13:19:22 -08:00
|
|
|
}
|
2018-11-27 15:13:26 -10:00
|
|
|
|
2022-02-20 13:19:22 -08:00
|
|
|
fn test_ed25519_from_pkcs8_(
|
2022-02-20 14:34:44 -08:00
|
|
|
variant: FromPkcs8Variant,
|
2022-02-20 13:19:22 -08:00
|
|
|
f: impl Fn(&[u8]) -> Result<Ed25519KeyPair, error::KeyRejected>,
|
|
|
|
) {
|
|
|
|
// Just test that we can parse the input.
|
2022-02-20 14:34:44 -08:00
|
|
|
test::run(
|
|
|
|
test_file!("ed25519_from_pkcs8_tests.txt"),
|
|
|
|
|section, test_case| {
|
|
|
|
assert_eq!(section, "");
|
|
|
|
let input = test_case.consume_bytes("Input");
|
|
|
|
let expected_error = {
|
|
|
|
let expected_checked = test_case.consume_string("Result-Checked");
|
|
|
|
let expected_maybe_unchecked = test_case.consume_string("Result-Maybe-Unchecked");
|
|
|
|
let expected_result = match variant {
|
|
|
|
FromPkcs8Variant::Checked => expected_checked,
|
|
|
|
FromPkcs8Variant::MaybeUnchecked => expected_maybe_unchecked,
|
|
|
|
};
|
|
|
|
if expected_result == "OK" {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(expected_result)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let expected_public = {
|
|
|
|
let expected_if_no_error = test_case.consume_optional_bytes("Public");
|
|
|
|
if expected_error.is_none() {
|
|
|
|
Some(expected_if_no_error.unwrap())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match f(&input) {
|
|
|
|
Ok(keypair) => {
|
|
|
|
assert_eq!(expected_error, None);
|
|
|
|
assert_eq!(
|
|
|
|
expected_public.as_deref(),
|
|
|
|
Some(keypair.public_key().as_ref())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Err(actual_error) => {
|
|
|
|
assert_eq!(expected_error, Some(format!("{}", actual_error)));
|
|
|
|
assert_eq!(expected_public, None);
|
|
|
|
}
|
2022-02-20 13:12:15 -08:00
|
|
|
}
|
|
|
|
|
2022-02-20 14:34:44 -08:00
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
);
|
2017-05-07 13:17:57 -10:00
|
|
|
}
|
2019-01-19 18:17:45 -10:00
|
|
|
|
2023-10-01 19:38:23 -07:00
|
|
|
#[test]
|
|
|
|
fn ed25519_test_generate_pkcs8() {
|
|
|
|
let rng = rand::SystemRandom::new();
|
|
|
|
let generated = signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
|
|
|
|
let generated = generated.as_ref();
|
|
|
|
|
2023-10-01 21:35:47 -07:00
|
|
|
let _ronudtripped = signature::Ed25519KeyPair::from_pkcs8(generated).unwrap();
|
2023-10-01 19:38:23 -07:00
|
|
|
|
|
|
|
// Regression test: Verify we're generating the correct encoding, as
|
|
|
|
// `Ed25519KeyPair::from_pkcs8` also accepts our old wrong encoding.
|
|
|
|
assert_eq!(generated.len(), 19 + 32 + 32);
|
|
|
|
assert_eq!(&generated[..2], &[0x30, 0x51]);
|
|
|
|
}
|
|
|
|
|
2019-01-19 18:17:45 -10:00
|
|
|
#[test]
|
|
|
|
fn ed25519_test_public_key_coverage() {
|
2019-03-06 13:21:18 -10:00
|
|
|
const PRIVATE_KEY: &[u8] = include_bytes!("ed25519_test_private_key.p8");
|
|
|
|
const PUBLIC_KEY: &[u8] = include_bytes!("ed25519_test_public_key.der");
|
2020-11-11 12:51:17 -08:00
|
|
|
const PUBLIC_KEY_DEBUG: &str =
|
2019-01-19 18:17:45 -10:00
|
|
|
"PublicKey(\"5809e9fef6dcec58f0f2e3b0d67e9880a11957e083ace85835c3b6c8fbaf6b7d\")";
|
|
|
|
|
2019-04-05 14:08:09 -10:00
|
|
|
let key_pair = signature::Ed25519KeyPair::from_pkcs8(PRIVATE_KEY).unwrap();
|
2019-01-19 18:17:45 -10:00
|
|
|
|
|
|
|
// Test `AsRef<[u8]>`
|
|
|
|
assert_eq!(key_pair.public_key().as_ref(), PUBLIC_KEY);
|
|
|
|
|
|
|
|
// Test `Clone`.
|
2020-11-11 13:04:39 -08:00
|
|
|
#[allow(clippy::clone_on_copy)]
|
|
|
|
let _: <Ed25519KeyPair as KeyPair>::PublicKey = key_pair.public_key().clone();
|
|
|
|
|
|
|
|
// Test `Copy`.
|
|
|
|
let _: <Ed25519KeyPair as KeyPair>::PublicKey = *key_pair.public_key();
|
2019-01-19 18:17:45 -10:00
|
|
|
|
|
|
|
// Test `Debug`.
|
|
|
|
assert_eq!(PUBLIC_KEY_DEBUG, format!("{:?}", key_pair.public_key()));
|
|
|
|
assert_eq!(
|
|
|
|
format!(
|
|
|
|
"Ed25519KeyPair {{ public_key: {:?} }}",
|
|
|
|
key_pair.public_key()
|
|
|
|
),
|
|
|
|
format!("{:?}", key_pair)
|
|
|
|
);
|
|
|
|
}
|