Merge pull request #84 from fweissberg/port-wycheproof-vectors

Port wycheproof vectors
This commit is contained in:
bunnie 2021-09-06 14:38:07 +08:00 committed by GitHub
commit 0c095cced6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 5595 additions and 11 deletions

145
Cargo.lock generated
View file

@ -61,9 +61,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.38"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1"
checksum = "28ae2b3dec75a406790005a200b1bd89785afc02517a00ca99ecfe093ee9e6cf"
[[package]]
name = "atty"
@ -372,7 +372,7 @@ dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"strsim 0.8.0",
"textwrap",
"unicode-width",
"vec_map",
@ -557,6 +557,41 @@ dependencies = [
"zeroize",
]
[[package]]
name = "darling"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "757c0ded2af11d8e739c4daea1ac623dd1624b06c844cf3f5a39f1bdbd99bb12"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c34d8efb62d0c2d7f60ece80f75e5c63c1588ba68032740494b0b9a996466e3"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim 0.10.0",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ade7bff147130fe5e6d39f089c6bd49ec0250f35d70b2eebf72afdfc919f15cc"
dependencies = [
"darling_core",
"quote",
"syn",
]
[[package]]
name = "debug-here"
version = "0.2.2"
@ -676,6 +711,16 @@ dependencies = [
"termcolor",
]
[[package]]
name = "eyre"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "221239d1d5ea86bf5d6f91c9d6bc3646ffe471b08ff9b0f91c44f115ac969d2b"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "failure"
version = "0.1.8"
@ -709,6 +754,12 @@ dependencies = [
"miniz_oxide",
]
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "gam"
version = "0.1.0"
@ -844,6 +895,12 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "ime-frontend"
version = "0.1.0"
@ -897,6 +954,12 @@ dependencies = [
"xous-names",
]
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "itoa"
version = "0.4.7"
@ -1247,9 +1310,9 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
[[package]]
name = "proc-macro2"
version = "1.0.24"
version = "1.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612"
dependencies = [
"unicode-xid",
]
@ -1291,9 +1354,9 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.8"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [
"proc-macro2",
]
@ -1612,6 +1675,12 @@ dependencies = [
"semver",
]
[[package]]
name = "rustversion"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61b3909d758bb75c79f23d4736fac9433868679d3ad2ea7a61e3c25cfda9a088"
[[package]]
name = "ryu"
version = "1.0.5"
@ -1664,9 +1733,23 @@ checksum = "568a8e6258aa33c13358f81fd834adb854c6f7c9468520910a9b1e8fac068012"
[[package]]
name = "serde"
version = "1.0.123"
version = "1.0.129"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae"
checksum = "d1f72836d2aa753853178eda473a3b9d8e4eefdaf20523b919677e6de489f8f1"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.129"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e57ae87ad533d9a56427558b516d0adac283614e347abf85b0dc0cbbf0a249f3"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
@ -1679,6 +1762,30 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_with"
version = "1.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad9fdbb69badc8916db738c25efd04f0a65297d26c2f8de4b62e57b8c12bc72"
dependencies = [
"hex",
"rustversion",
"serde",
"serde_with_macros",
]
[[package]]
name = "serde_with_macros"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1569374bd54623ec8bd592cf22ba6e03c0f177ff55fbc8c29a49e296e7adecf"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "sha2"
version = "0.9.5"
@ -1833,6 +1940,12 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "subtle"
version = "2.4.0"
@ -1876,9 +1989,9 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.67"
version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6498a9efc342871f91cc2d0d694c674368b4ceb40f62b65a7a08c3792935e702"
checksum = "b7f58f7e8eaa0009c5fec437aabf511bd9933e4b2d7407bd05273c01a8906ea7"
dependencies = [
"proc-macro2",
"quote",
@ -2308,6 +2421,16 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "wycheproof-import"
version = "0.1.0"
dependencies = [
"eyre",
"serde",
"serde_json",
"serde_with",
]
[[package]]
name = "x11-dl"
version = "2.18.5"

View file

@ -77,6 +77,7 @@ members = [
"services/spinor",
"services/root-keys",
"services/jtag",
"wycheproof-import",
]
resolver = "2"

View file

@ -205,12 +205,77 @@ pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
xous::destroy_server(sid).unwrap();
}
mod wycheproof {
pub const WYCHEPROOF_NO_TEST_CASES: usize = 518;
pub const WHYCHEPROOF_TEST_CASE_SIZE: usize = 96;
#[export_name = "wycheproof_vectors"]
pub static WYCHEPROOF_VECTORS: &[u8; WYCHEPROOF_NO_TEST_CASES * WHYCHEPROOF_TEST_CASE_SIZE] =
include_bytes!("x25519_test.bin");
pub struct WycheproofTestCase {
// inferred from the index; not read from the binary data
public: [u8; 32],
id: usize,
private: [u8; 32],
shared: [u8; 32],
}
impl WycheproofTestCase {
pub fn read(test_index: usize) -> Self {
use std::io::Read;
let test_offset = test_index * WHYCHEPROOF_TEST_CASE_SIZE;
let mut public = [0u8; 32];
(&WYCHEPROOF_VECTORS[test_offset..test_offset + 32])
.read_exact(&mut public)
.unwrap();
let mut private = [0u8; 32];
(&WYCHEPROOF_VECTORS[test_offset + 32..test_offset + 64])
.read_exact(&mut private)
.unwrap();
let mut shared = [0u8; 32];
(&WYCHEPROOF_VECTORS[test_offset + 64..test_offset + 96])
.read_exact(&mut shared)
.unwrap();
WycheproofTestCase {
id: test_index + 1,
public,
private,
shared,
}
}
pub fn run(&self) -> Option<TestCaseError> {
use x25519_dalek::{PublicKey, StaticSecret};
let private = StaticSecret::from(self.private);
let public = PublicKey::from(self.public);
let shared = private.diffie_hellman(&public).to_bytes();
if shared != self.shared {
Some(TestCaseError {
test_id: self.id,
expected: self.shared,
actual: shared,
})
} else {
None
}
}
}
pub struct TestCaseError {
pub(crate) test_id: usize,
pub(crate) expected: [u8; 32],
pub(crate) actual: [u8; 32],
}
}
#[derive(Debug)]
pub struct Engine {
susres: susres::Susres,
benchmark_cid: xous::CID,
start_time: Option<u64>,
}
impl Engine {
pub fn new(xns: &xous_names::XousNames, env: &mut CommonEnv) -> Self {
let sid = xous::create_server().unwrap();
@ -449,6 +514,22 @@ impl<'a> ShellCmdApi<'a> for Engine {
write!(ret, "Passed ed25519 simple check").unwrap();
}
}
"wycheproof" => {
use wycheproof::*;
use hex::ToHex;
let failures: Vec<TestCaseError> = (0..WYCHEPROOF_NO_TEST_CASES)
.filter_map(|test_index| WycheproofTestCase::read(test_index).run())
.collect();
write!(ret, "Ran {} tests. {} failures.", WYCHEPROOF_NO_TEST_CASES, failures.len()).unwrap();
if failures.len() > 0 {
write!(ret, "\nFailed tests: {:?}", failures.iter().map(|tc| tc.test_id).collect::<Vec<usize>>()).unwrap();
for failure in failures {
log::error!("wycheproof test #{} failed:", failure.test_id);
log::error!("expected: {}", failure.expected.encode_hex::<std::string::String>());
log::error!("actual: {}", failure.actual.encode_hex::<std::string::String>());
}
}
}
_ => {
write!(ret, "{}", helpstring).unwrap();
}

Binary file not shown.

View file

@ -0,0 +1,10 @@
[package]
name = "wycheproof-import"
version = "0.1.0"
edition = "2018"
[dependencies]
eyre = "0.6.5"
serde = { version = "1.0.129", features = ["derive"] }
serde_json = "1.0.66"
serde_with = { version = "1.9.4", features = ["hex"] }

View file

@ -0,0 +1,20 @@
# Wycheproof x25519 Test Vectors
This package contains code to make the Diffie-Hellman Key exchange test vectors for Curve25519
from [Project Wycheproof](https://github.com/google/wycheproof) usable for Xous.
Project Wycheproof is published under the [Apache-2.0 License](../LICENSES/Apache-2.0.txt). The
file [x25519_test.json](x25519_test.json) was imported from Project Wycheproof. See the newest corresponding commit
message to map the local file to the upstream file's version. A subset of the information therein is compiled to binary
using this package.
## Usage
The repository contains the precompiled test vectors. To run them, use the `engine wycheproof` command when running on
Renode or on real hardware.
### (Re-)compiling the test vectors
Run `cargo xtask wychproof-import` in order to compile the test cases in `wycheproof-import/x25519_test.json`
to `services/shellchat/src/cmds/x25519_test.bin` which will be included when compiling
the `services/shellchat/src/cmds/engine.rs`. The `xtask` command runs the local binary crate with the proper arguments.

View file

@ -0,0 +1,75 @@
use std::env;
use std::io::Write;
use std::ops::AddAssign;
use std::path::Path;
use std::process::exit;
use eyre::{bail, Result, WrapErr};
use serde::Deserialize;
#[derive(Deserialize)]
struct TestData {
#[serde(rename(deserialize = "testGroups"))]
test_groups: Vec<TestGroup>,
}
#[derive(Deserialize)]
struct TestGroup {
tests: Vec<TestCase>,
}
#[serde_with::serde_as]
#[derive(Debug, Deserialize)]
struct TestCase {
#[serde(rename(deserialize = "tcId"))]
id: usize,
#[serde_as(as = "serde_with::hex::Hex")]
public: [u8; 32],
#[serde_as(as = "serde_with::hex::Hex")]
private: [u8; 32],
#[serde_as(as = "serde_with::hex::Hex")]
shared: [u8; 32],
result: String,
}
fn main() -> Result<()> {
let args: Vec<String> = env::args().collect();
if args.len() != 3 {
println!("usage: wycheproof-import <input.json> <output.bin>");
exit(0);
}
let input_file_path = Path::new(&args[1]);
let output_file_path = Path::new(&args[2]);
let test_data = std::fs::read_to_string(input_file_path).wrap_err(format!(
"Could not read test data from '{}'",
input_file_path.to_string_lossy()
))?;
let test_data: TestData =
serde_json::from_str(&test_data).wrap_err("Error parsing test vectors")?;
let expected_results = vec!["valid".to_string(), "acceptable".to_string()];
let mut output_file = std::fs::File::create(output_file_path).wrap_err(format!(
"Error creating output file '{}'",
output_file_path.to_string_lossy()
))?;
let mut last_id = 0;
for test_case in &test_data.test_groups[0].tests {
if test_case.id != last_id + 1 {
bail!("Expect test cases to be continuously ascending. Expected next tcId to be {}, was {}", last_id + 1, test_case.id)
}
if !expected_results.contains(&test_case.result) {
bail!(
"Expect test case results to be one of {:?}",
expected_results
);
}
last_id.add_assign(1);
output_file.write_all(&test_case.public)?;
output_file.write_all(&test_case.private)?;
output_file.write_all(&test_case.shared)?;
}
Ok(())
}

File diff suppressed because it is too large Load diff

View file

@ -193,6 +193,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Some("nuke-soc") => update_usb(false, false, true, false)?,
Some("burn-soc") => update_usb(false, false, false, true)?,
Some("generate-locales") => generate_locales()?,
Some("wycheproof-import") => whycheproof_import()?,
_ => print_help(),
}
Ok(())
@ -221,6 +222,7 @@ burn-loader invoke the `usb_update.py` utility to burn the loader
burn-soc invoke the `usb_update.py` utility to stage the SoC gateware, which must then be provisioned with secret material using the Precursor device.
nuke-soc 'Factory reset' - invoke the `usb_update.py` utility to burn the SoC gateware, erasing most secrets. For developers.
generate-locales only generate the locales include for the language selected in xous-rs/src/locale.rs
wycheproof-import generate binary test vectors for engine-25519 from whycheproof-import/x25519.json
Please refer to tools/README_UPDATE.md for instructions on how to set up `usb_update.py`
"
@ -875,6 +877,30 @@ fn generate_locales() -> Result<(), std::io::Error> {
filetime::set_file_mtime("locales/src/lib.rs", ts)
}
fn whycheproof_import() -> Result<(), DynError> {
let input_file = "wycheproof-import/x25519_test.json";
let output_file = "services/shellchat/src/cmds/x25519_test.bin";
let status = Command::new(cargo())
.current_dir(project_root())
.args(&[
"run",
"--package",
"wycheproof-import",
"--",
input_file,
output_file
])
.status()?;
if !status.success() {
return Err("wycheproof-import failed. If any, the output will not be usable.".into());
}
println!();
println!("Wrote wycheproof x25519 testvectors to '{}'.", output_file);
return Ok(())
}
// fn dist_dir() -> PathBuf {
// project_root().join("target/dist")
// }