oxedyne/fe2o3/fe2o3_crypto/tests/x25519_of_ed25519.rs
2.6 KiB, 1 run
created by r1870400018:76933, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! One Ed25519 key used for X25519 too: the public key mapped to its Montgomery `u` and the seed |
| 2 | //! to the clamped first half of its SHA-512 must be a pair, and two such pairs must agree. |
| 3 | |
| 4 | use oxedyne_fe2o3_core::prelude::*; |
| 5 | use oxedyne_fe2o3_crypto::agree::AgreementScheme; |
| 6 | |
| 7 | |
| 8 | fn unhex(s: &str) -> Outcome<Vec<u8>> { |
| 9 | let mut out = Vec::with_capacity(s.len() / 2); |
| 10 | for i in (0..s.len()).step_by(2) { |
| 11 | out.push(res!(u8::from_str_radix(&s[i..i + 2], 16), Decode)); |
| 12 | } |
| 13 | Ok(out) |
| 14 | } |
| 15 | |
| 16 | // RFC 8032 §7.1, tests 1 and 2: secret seed and public key. |
| 17 | const KEYS: [(&str, &str); 2] = [ |
| 18 | ("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60", |
| 19 | "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"), |
| 20 | ("4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb", |
| 21 | "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c"), |
| 22 | ]; |
| 23 | |
| 24 | #[test] |
| 25 | fn test_the_converted_secret_and_public_key_are_a_pair_00() -> Outcome<()> { |
| 26 | for (seed, pk) in KEYS { |
| 27 | let sk = res!(AgreementScheme::x25519_secret_of_ed25519(&res!(unhex(seed)))); |
| 28 | if sk[0] & 7 != 0 || sk[31] & 0x80 != 0 || sk[31] & 0x40 == 0 { |
| 29 | return Err(err!("The secret from seed {} is not clamped.", seed; Test, Invalid)); |
| 30 | } |
| 31 | let from_ed = res!(AgreementScheme::x25519_public_of_ed25519(&res!(unhex(pk)))); |
| 32 | let from_sk = res!(AgreementScheme::x25519_public_of(&sk[..])); |
| 33 | req!(from_ed, from_sk); |
| 34 | } |
| 35 | Ok(()) |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 39 | fn test_two_converted_keys_agree_00() -> Outcome<()> { |
| 40 | let a_sk = res!(AgreementScheme::x25519_secret_of_ed25519(&res!(unhex(KEYS[0].0)))); |
| 41 | let b_sk = res!(AgreementScheme::x25519_secret_of_ed25519(&res!(unhex(KEYS[1].0)))); |
| 42 | let a_pk = res!(AgreementScheme::x25519_public_of_ed25519(&res!(unhex(KEYS[0].1)))); |
| 43 | let b_pk = res!(AgreementScheme::x25519_public_of_ed25519(&res!(unhex(KEYS[1].1)))); |
| 44 | let ab = res!(AgreementScheme::x25519_agree(&a_sk[..], &b_pk)); |
| 45 | let ba = res!(AgreementScheme::x25519_agree(&b_sk[..], &a_pk)); |
| 46 | req!(ab, ba); |
| 47 | Ok(()) |
| 48 | } |
| 49 | |
| 50 | #[test] |
| 51 | fn test_keys_that_cannot_be_converted_are_refused_00() -> Outcome<()> { |
| 52 | // The identity point, of small order, and a y with no x on the curve (y = 2). |
| 53 | let mut identity = [0u8; 32]; |
| 54 | identity[0] = 1; |
| 55 | let mut off_curve = [0u8; 32]; |
| 56 | off_curve[0] = 2; |
| 57 | assert!(AgreementScheme::x25519_public_of_ed25519(&identity).is_err()); |
| 58 | assert!(AgreementScheme::x25519_public_of_ed25519(&off_curve).is_err()); |
| 59 | assert!(AgreementScheme::x25519_public_of_ed25519(&[9u8; 31]).is_err()); |
| 60 | assert!(AgreementScheme::x25519_secret_of_ed25519(&[9u8; 33]).is_err()); |
| 61 | Ok(()) |
| 62 | } |