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.
|
|
|
|
|
2017-08-27 12:31:17 -10:00
|
|
|
#![forbid(
|
|
|
|
anonymous_parameters,
|
|
|
|
box_pointers,
|
|
|
|
legacy_directory_ownership,
|
|
|
|
missing_copy_implementations,
|
|
|
|
missing_debug_implementations,
|
|
|
|
missing_docs,
|
|
|
|
trivial_casts,
|
|
|
|
trivial_numeric_casts,
|
|
|
|
unsafe_code,
|
|
|
|
unstable_features,
|
|
|
|
unused_extern_crates,
|
|
|
|
unused_import_braces,
|
|
|
|
unused_qualifications,
|
|
|
|
unused_results,
|
|
|
|
variant_size_differences,
|
2018-11-15 15:39:12 -10:00
|
|
|
warnings
|
2017-08-27 12:31:17 -10:00
|
|
|
)]
|
|
|
|
|
2017-05-06 10:34:50 -10:00
|
|
|
extern crate ring;
|
|
|
|
extern crate untrusted;
|
|
|
|
|
2017-05-07 09:22:03 -10:00
|
|
|
use ring::{signature, test};
|
2017-05-06 10:34:50 -10:00
|
|
|
use signature::Ed25519KeyPair;
|
|
|
|
|
|
|
|
/// Test vectors from BoringSSL.
|
|
|
|
#[test]
|
|
|
|
fn test_signature_ed25519() {
|
|
|
|
test::from_file("tests/ed25519_tests.txt", |section, test_case| {
|
|
|
|
assert_eq!(section, "");
|
2017-05-07 09:22:03 -10:00
|
|
|
let seed = test_case.consume_bytes("SEED");
|
|
|
|
assert_eq!(32, seed.len());
|
|
|
|
let seed = untrusted::Input::from(&seed);
|
|
|
|
|
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
|
|
|
let public_key = untrusted::Input::from(&public_key);
|
|
|
|
|
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
|
|
|
{
|
2018-11-15 15:39:12 -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.
|
|
|
|
let rng = test::rand::FixedSliceRandom {
|
2018-11-15 15:39:12 -10:00
|
|
|
bytes: seed.as_slice_less_safe(),
|
2017-05-07 09:22:03 -10:00
|
|
|
};
|
|
|
|
let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
|
2018-11-15 15:39:12 -10:00
|
|
|
let key_pair = Ed25519KeyPair::from_pkcs8(untrusted::Input::from(&pkcs8)).unwrap();
|
2017-05-07 09:22:03 -10:00
|
|
|
assert_eq!(public_key, key_pair.public_key_bytes());
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
assert!(signature::verify(
|
2018-11-15 15:39:12 -10:00
|
|
|
&signature::ED25519,
|
|
|
|
public_key,
|
|
|
|
untrusted::Input::from(&msg),
|
|
|
|
untrusted::Input::from(&expected_sig)
|
|
|
|
)
|
|
|
|
.is_ok());
|
2017-05-06 10:34:50 -10:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
2017-05-07 09:28:28 -10:00
|
|
|
assert!(Ed25519KeyPair::from_seed_and_public_key(
|
2017-05-07 09:22:03 -10:00
|
|
|
untrusted::Input::from(PRIVATE_KEY),
|
2018-11-15 15:39:12 -10:00
|
|
|
untrusted::Input::from(PUBLIC_KEY)
|
|
|
|
)
|
|
|
|
.is_ok());
|
2017-05-06 10:34:50 -10:00
|
|
|
|
|
|
|
// Truncated private key.
|
2017-05-07 09:28:28 -10:00
|
|
|
assert!(Ed25519KeyPair::from_seed_and_public_key(
|
2017-05-07 09:22:03 -10:00
|
|
|
untrusted::Input::from(&PRIVATE_KEY[..31]),
|
2018-11-15 15:39:12 -10:00
|
|
|
untrusted::Input::from(PUBLIC_KEY)
|
|
|
|
)
|
|
|
|
.is_err());
|
2017-05-06 10:34:50 -10:00
|
|
|
|
|
|
|
// Truncated public key.
|
2017-05-07 09:28:28 -10:00
|
|
|
assert!(Ed25519KeyPair::from_seed_and_public_key(
|
2017-05-07 09:22:03 -10:00
|
|
|
untrusted::Input::from(PRIVATE_KEY),
|
2018-11-15 15:39:12 -10:00
|
|
|
untrusted::Input::from(&PUBLIC_KEY[..31])
|
|
|
|
)
|
|
|
|
.is_err());
|
2017-05-06 10:34:50 -10:00
|
|
|
|
|
|
|
// Swapped public and private key.
|
2017-05-07 09:28:28 -10:00
|
|
|
assert!(Ed25519KeyPair::from_seed_and_public_key(
|
2017-05-07 09:22:03 -10:00
|
|
|
untrusted::Input::from(PUBLIC_KEY),
|
2018-11-15 15:39:12 -10:00
|
|
|
untrusted::Input::from(PRIVATE_KEY)
|
|
|
|
)
|
|
|
|
.is_err());
|
2017-05-06 10:34:50 -10:00
|
|
|
}
|
2017-05-07 11:42:22 -10:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ed25519_from_pkcs8_unchecked() {
|
|
|
|
// Just test that we can parse the input.
|
2018-11-15 15:39:12 -10:00
|
|
|
test::from_file(
|
|
|
|
"tests/ed25519_from_pkcs8_unchecked_tests.txt",
|
|
|
|
|section, test_case| {
|
2018-11-27 15:13:26 -10:00
|
|
|
use std::error::Error;
|
|
|
|
|
2018-11-15 15:39:12 -10:00
|
|
|
assert_eq!(section, "");
|
|
|
|
let input = test_case.consume_bytes("Input");
|
|
|
|
let error = test_case.consume_optional_string("Error");
|
2018-11-27 15:13:26 -10:00
|
|
|
|
|
|
|
match (
|
|
|
|
Ed25519KeyPair::from_pkcs8_maybe_unchecked(untrusted::Input::from(&input)),
|
|
|
|
error.clone(),
|
|
|
|
) {
|
|
|
|
(Ok(_), None) => (),
|
|
|
|
(Err(e), None) => panic!("Failed with error \"{}\", but expected to succeed", e),
|
|
|
|
(Ok(_), Some(e)) => panic!("Succeeded, but expected error \"{}\"", e),
|
|
|
|
(Err(actual), Some(expected)) => assert_eq!(actual.description(), expected),
|
|
|
|
};
|
|
|
|
|
2018-11-15 15:39:12 -10:00
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
);
|
2017-05-07 11:42:22 -10:00
|
|
|
}
|
2017-05-07 13:17:57 -10:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ed25519_from_pkcs8() {
|
|
|
|
// Just test that we can parse the input.
|
2018-11-15 15:39:12 -10:00
|
|
|
test::from_file(
|
|
|
|
"tests/ed25519_from_pkcs8_tests.txt",
|
|
|
|
|section, test_case| {
|
2018-11-27 15:13:26 -10:00
|
|
|
use std::error::Error;
|
|
|
|
|
2018-11-15 15:39:12 -10:00
|
|
|
assert_eq!(section, "");
|
|
|
|
let input = test_case.consume_bytes("Input");
|
|
|
|
let error = test_case.consume_optional_string("Error");
|
2018-11-27 15:13:26 -10:00
|
|
|
|
|
|
|
match (
|
|
|
|
Ed25519KeyPair::from_pkcs8(untrusted::Input::from(&input)),
|
|
|
|
error.clone(),
|
|
|
|
) {
|
|
|
|
(Ok(_), None) => (),
|
|
|
|
(Err(e), None) => panic!("Failed with error \"{}\", but expected to succeed", e),
|
|
|
|
(Ok(_), Some(e)) => panic!("Succeeded, but expected error \"{}\"", e),
|
|
|
|
(Err(actual), Some(expected)) => assert_eq!(actual.description(), expected),
|
|
|
|
};
|
|
|
|
|
2018-11-15 15:39:12 -10:00
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
);
|
2017-05-07 13:17:57 -10:00
|
|
|
}
|