mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
fix remaining crypto tests
This commit is contained in:
parent
3647c38cb0
commit
d995912a97
4 changed files with 117 additions and 42 deletions
86
Cargo.lock
generated
86
Cargo.lock
generated
|
|
@ -248,6 +248,15 @@ dependencies = [
|
|||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "block-buffer"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "block-padding"
|
||||
version = "0.3.2"
|
||||
|
|
@ -680,6 +689,17 @@ dependencies = [
|
|||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crypto-bigint"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78a4e0fb04deabeb711eb20bd1179f1524c06f7e6975ebccc495f678a635887b"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"subtle",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.3"
|
||||
|
|
@ -737,6 +757,7 @@ dependencies = [
|
|||
"rand_chacha 0.3.1",
|
||||
"rand_core 0.6.3",
|
||||
"regex",
|
||||
"rfc6979 0.2.0",
|
||||
"ring",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
|
@ -768,7 +789,7 @@ version = "3.2.1"
|
|||
source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#b8f71e85d1e251aeeea4fe0da8ff8c8815e51c69"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"digest",
|
||||
"digest 0.9.0",
|
||||
"engine-25519",
|
||||
"engine25519-as",
|
||||
"log",
|
||||
|
|
@ -784,7 +805,7 @@ dependencies = [
|
|||
"bincode",
|
||||
"byteorder",
|
||||
"criterion",
|
||||
"digest",
|
||||
"digest 0.9.0",
|
||||
"engine-25519",
|
||||
"engine25519-as",
|
||||
"fiat-crypto",
|
||||
|
|
@ -875,6 +896,17 @@ dependencies = [
|
|||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.10.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506"
|
||||
dependencies = [
|
||||
"block-buffer 0.10.2",
|
||||
"crypto-common",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dlib"
|
||||
version = "0.5.0"
|
||||
|
|
@ -916,7 +948,7 @@ checksum = "d0d69ae62e0ce582d56380743515fefaf1a8c70cec685d9677636d7e30ae9dc9"
|
|||
dependencies = [
|
||||
"der",
|
||||
"elliptic-curve",
|
||||
"rfc6979",
|
||||
"rfc6979 0.1.0",
|
||||
"signature",
|
||||
]
|
||||
|
||||
|
|
@ -976,7 +1008,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "25b477563c2bfed38a3b7a60964c49e058b2510ad3f12ba3483fd8f62c2306d6"
|
||||
dependencies = [
|
||||
"base16ct",
|
||||
"crypto-bigint",
|
||||
"crypto-bigint 0.3.2",
|
||||
"der",
|
||||
"ff",
|
||||
"generic-array",
|
||||
|
|
@ -1544,8 +1576,8 @@ version = "0.11.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01706d578d5c281058480e673ae4086a9f4710d8df1ad80a5b03e39ece5f886b"
|
||||
dependencies = [
|
||||
"digest",
|
||||
"hmac",
|
||||
"digest 0.9.0",
|
||||
"hmac 0.11.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1555,7 +1587,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "2a2a2320eb7ec0ebe8da8f744d7812d9fc4cb4d09344ac01898dbcb6a20ae69b"
|
||||
dependencies = [
|
||||
"crypto-mac",
|
||||
"digest",
|
||||
"digest 0.9.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hmac"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
|
||||
dependencies = [
|
||||
"digest 0.10.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -2309,7 +2350,7 @@ dependencies = [
|
|||
"bitflags",
|
||||
"blowfish 0.9.1",
|
||||
"cipher 0.4.3",
|
||||
"digest",
|
||||
"digest 0.9.0",
|
||||
"gam",
|
||||
"hkdf",
|
||||
"llio",
|
||||
|
|
@ -2683,8 +2724,19 @@ version = "0.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96ef608575f6392792f9ecf7890c00086591d29a83910939d430753f7c050525"
|
||||
dependencies = [
|
||||
"crypto-bigint",
|
||||
"hmac",
|
||||
"crypto-bigint 0.3.2",
|
||||
"hmac 0.11.0",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rfc6979"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c0788437d5ee113c49af91d3594ebc4fcdcc962f8b6df5aa1c3eeafd8ad95de"
|
||||
dependencies = [
|
||||
"crypto-bigint 0.4.3",
|
||||
"hmac 0.12.1",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
|
|
@ -2764,7 +2816,7 @@ dependencies = [
|
|||
"cipher 0.4.3",
|
||||
"com",
|
||||
"curve25519-dalek",
|
||||
"digest",
|
||||
"digest 0.9.0",
|
||||
"ed25519-dalek",
|
||||
"engine-25519",
|
||||
"gam",
|
||||
|
|
@ -3013,8 +3065,8 @@ dependencies = [
|
|||
name = "sha2"
|
||||
version = "0.9.8"
|
||||
dependencies = [
|
||||
"block-buffer",
|
||||
"digest",
|
||||
"block-buffer 0.9.0",
|
||||
"digest 0.9.0",
|
||||
"log",
|
||||
"log-server",
|
||||
"num-derive",
|
||||
|
|
@ -3034,8 +3086,8 @@ dependencies = [
|
|||
name = "sha2-loader"
|
||||
version = "0.9.5"
|
||||
dependencies = [
|
||||
"block-buffer",
|
||||
"digest",
|
||||
"block-buffer 0.9.0",
|
||||
"digest 0.9.0",
|
||||
"opaque-debug",
|
||||
"utralib",
|
||||
]
|
||||
|
|
@ -3053,7 +3105,7 @@ dependencies = [
|
|||
"content-plugin-api",
|
||||
"curve25519-dalek",
|
||||
"debug-here",
|
||||
"digest",
|
||||
"digest 0.9.0",
|
||||
"dns",
|
||||
"ed25519-dalek",
|
||||
"engine-25519",
|
||||
|
|
@ -3099,7 +3151,7 @@ version = "1.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02658e48d89f2bec991f9a78e69cfa4c316f8d6a6c4ec12fae1aeb263d486788"
|
||||
dependencies = [
|
||||
"digest",
|
||||
"digest 0.9.0",
|
||||
"rand_core 0.6.3",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -31,4 +31,5 @@ hex = { version = "0.3.2", default-features = false }
|
|||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = { version = "1.0" }
|
||||
ring = { version = "0.16.11" }
|
||||
byteorder = { version = "1", default-features = false }
|
||||
byteorder = { version = "1", default-features = false }
|
||||
rfc6979 = "0.2.0"
|
||||
|
|
@ -151,6 +151,7 @@ mod test {
|
|||
let encoded = pk.p.to_encoded_point(false);
|
||||
let maybe_affine = AffinePoint::from_encoded_point(&encoded);
|
||||
assert!(bool::from(maybe_affine.is_some()));
|
||||
// assert!(pk.p.is_valid_vartime());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -208,19 +208,17 @@ impl PubKey {
|
|||
_ => None
|
||||
}
|
||||
}
|
||||
/*
|
||||
#[cfg(test)]
|
||||
//#[cfg(test)]
|
||||
fn to_bytes_uncompressed(&self, bytes: &mut [u8; 65]) {
|
||||
// not sure if this is correct -- we'll find out when we run the tests
|
||||
// this generates a SEC1 EncodedPoint, but I'm not sure if that's the same thing as
|
||||
// a SEC1-encoded public key.
|
||||
bytes.copy_from_slice(
|
||||
self.p.to_encoded_point(false)
|
||||
.to_bytes()
|
||||
.as_slice()
|
||||
.as_bytes()
|
||||
)
|
||||
// self.p.to_bytes_uncompressed(bytes);
|
||||
}*/
|
||||
}
|
||||
|
||||
#[cfg(feature = "with_ctap1")]
|
||||
pub fn to_uncompressed(&self) -> [u8; PubKey::UNCOMPRESSED_LENGTH] {
|
||||
|
|
@ -301,13 +299,13 @@ impl PubKey {
|
|||
*/
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::super::rng256::ThreadRng256;
|
||||
use super::super::sha256::Sha256;
|
||||
use super::*;
|
||||
use arrayref::array_ref;
|
||||
use arrayref::{array_ref, array_mut_ref};
|
||||
use byteorder::{BigEndian, ByteOrder};
|
||||
use p256::SecretKey;
|
||||
|
||||
|
|
@ -315,6 +313,7 @@ mod test {
|
|||
const BYTES_PER_DIGIT: usize = BITS_PER_DIGIT >> 3;
|
||||
const NDIGITS: usize = 8;
|
||||
pub const NBYTES: usize = NDIGITS * BYTES_PER_DIGIT;
|
||||
pub type Digit = u32;
|
||||
#[derive(Default)]
|
||||
pub struct Int256 {
|
||||
digits: [Digit; NDIGITS],
|
||||
|
|
@ -336,6 +335,11 @@ mod test {
|
|||
}
|
||||
Int256 { digits }
|
||||
}
|
||||
pub fn to_bin(&self, dst: &mut [u8; NBYTES]) {
|
||||
for i in 0..NDIGITS {
|
||||
BigEndian::write_u32(array_mut_ref![dst, 4 * i, 4], self.digits[NDIGITS - 1 - i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -348,12 +352,17 @@ mod test {
|
|||
/** Test that key generation creates valid keys **/
|
||||
#[test]
|
||||
fn test_genpk_is_valid_random() {
|
||||
use p256::AffinePoint;
|
||||
use p256::elliptic_curve::sec1::FromEncodedPoint;
|
||||
let mut rng = ThreadRng256 {};
|
||||
|
||||
for _ in 0..ITERATIONS {
|
||||
let sk = SecKey::gensk(&mut rng);
|
||||
let pk = sk.genpk();
|
||||
assert!(pk.p.is_valid_vartime());
|
||||
let encoded = pk.p.to_encoded_point(false);
|
||||
let maybe_affine = AffinePoint::from_encoded_point(&encoded);
|
||||
assert!(bool::from(maybe_affine.is_some()));
|
||||
// assert!(pk.p.is_valid_vartime());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -366,8 +375,10 @@ mod test {
|
|||
let sk = SecKey::gensk(&mut rng);
|
||||
let mut bytes = [0; 32];
|
||||
sk.to_bytes(&mut bytes);
|
||||
let decoded_sk = SecKey::from_bytes(&bytes);
|
||||
assert_eq!(decoded_sk, Some(sk));
|
||||
let decoded_sk = SecKey::from_bytes(&bytes).unwrap();
|
||||
let mut checkbytes = [0; 32];
|
||||
decoded_sk.to_bytes(&mut checkbytes);
|
||||
assert_eq!(checkbytes, bytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -409,26 +420,31 @@ mod test {
|
|||
#[test]
|
||||
fn test_rfc6979_keypair() {
|
||||
let sk = SecKey {
|
||||
k: SecretKey::from_be_bytes(&hex::decode(RFC6979_X).unwrap()),
|
||||
k: SigningKey::from_bytes(&hex::decode(RFC6979_X).unwrap()).unwrap(),
|
||||
// k: NonZeroExponentP256::from_int_checked(int256_from_hex(RFC6979_X)).unwrap(),
|
||||
};
|
||||
let pk = sk.genpk();
|
||||
assert_eq!(pk.p.getx().to_int(), int256_from_hex(RFC6979_UX));
|
||||
assert_eq!(pk.p.gety().to_int(), int256_from_hex(RFC6979_UY));
|
||||
let encoded = pk.p.to_encoded_point(false);
|
||||
assert_eq!(encoded.x().unwrap().as_slice(), &hex::decode(RFC6979_UX).unwrap());
|
||||
assert_eq!(encoded.y().unwrap().as_slice(), &hex::decode(RFC6979_UY).unwrap());
|
||||
}
|
||||
|
||||
fn test_rfc6979(msg: &str, k: &str, r: &str, s: &str) {
|
||||
let key = SigningKey::from_bytes(&hex::decode(RFC6979_X).unwrap()).unwrap();
|
||||
let sk = SecKey {
|
||||
k: SecretKey::from_be_bytes(&hex::decode(RFC6979_X).unwrap()),
|
||||
k: key,
|
||||
// k: NonZeroExponentP256::from_int_checked(int256_from_hex(RFC6979_X)).unwrap(),
|
||||
};
|
||||
/* // unfortunately we don't have a routine to extract `k` in the RustCrypto libraries
|
||||
assert_eq!(
|
||||
sk.get_k_rfc6979::<Sha256>(msg.as_bytes()).to_int(),
|
||||
int256_from_hex(k)
|
||||
);
|
||||
); */
|
||||
let sign = sk.sign_rfc6979::<Sha256>(msg.as_bytes());
|
||||
assert_eq!(sign.r.to_int(), int256_from_hex(r));
|
||||
assert_eq!(sign.s.to_int(), int256_from_hex(s));
|
||||
let mut rs = [0u8; 64];
|
||||
sign.to_bytes(&mut rs);
|
||||
assert_eq!(&rs[..32], &hex::decode(r).unwrap());
|
||||
assert_eq!(&rs[32..], &hex::decode(s).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -584,14 +600,17 @@ mod test {
|
|||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x01,
|
||||
];
|
||||
let r = NonZeroExponentP256::from_int_checked(Int256::from_bin(&r_bytes)).unwrap();
|
||||
//let r = NonZeroExponentP256::from_int_checked(Int256::from_bin(&r_bytes)).unwrap();
|
||||
let s_bytes = [
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0xFF,
|
||||
];
|
||||
let s = NonZeroExponentP256::from_int_checked(Int256::from_bin(&s_bytes)).unwrap();
|
||||
let signature = Signature { r, s };
|
||||
//let s = NonZeroExponentP256::from_int_checked(Int256::from_bin(&s_bytes)).unwrap();
|
||||
//let signature = Signature { r, s };
|
||||
let signature = Signature::from_bytes(
|
||||
&[r_bytes, s_bytes].concat()
|
||||
).unwrap();
|
||||
let expected_encoding = vec![0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x02, 0x00, 0xFF];
|
||||
|
||||
assert_eq!(signature.to_asn1_der(), expected_encoding);
|
||||
|
|
@ -604,14 +623,17 @@ mod test {
|
|||
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
|
||||
0xAA, 0xAA, 0xAA, 0xAA,
|
||||
];
|
||||
let r = NonZeroExponentP256::from_int_checked(Int256::from_bin(&r_bytes)).unwrap();
|
||||
//let r = NonZeroExponentP256::from_int_checked(Int256::from_bin(&r_bytes)).unwrap();
|
||||
let s_bytes = [
|
||||
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
|
||||
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
|
||||
0xBB, 0xBB, 0xBB, 0xBB,
|
||||
];
|
||||
let s = NonZeroExponentP256::from_int_checked(Int256::from_bin(&s_bytes)).unwrap();
|
||||
let signature = Signature { r, s };
|
||||
//let s = NonZeroExponentP256::from_int_checked(Int256::from_bin(&s_bytes)).unwrap();
|
||||
//let signature = Signature { r, s };
|
||||
let signature = Signature::from_bytes(
|
||||
&[r_bytes, s_bytes].concat()
|
||||
).unwrap();
|
||||
let expected_encoding = vec![
|
||||
0x30, 0x46, 0x02, 0x21, 0x00, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
|
||||
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
|
||||
|
|
@ -628,4 +650,3 @@ mod test {
|
|||
// - Invalid public key (at infinity, values not less than the prime p), but ring doesn't
|
||||
// directly exposes key validation in its API.
|
||||
}
|
||||
*/
|
||||
Loading…
Reference in a new issue