aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / ed25519-dalek / tests / validation_criteria.rs
| 1 | use ed25519::signature::Verifier; |
| 2 | use ed25519_dalek::{Signature, VerifyingKey}; |
| 3 | |
| 4 | use serde::{de::Error as SError, Deserialize, Deserializer}; |
| 5 | use std::{collections::BTreeSet as Set, fs::File}; |
| 6 | |
| 7 | /// The set of edge cases that [`VerifyingKey::verify()`] permits. |
| 8 | const VERIFY_ALLOWED_EDGECASES: &[Flag] = &[ |
| 9 | Flag::LowOrderA, |
| 10 | Flag::LowOrderR, |
| 11 | Flag::NonCanonicalA, |
| 12 | Flag::LowOrderComponentA, |
| 13 | Flag::LowOrderComponentR, |
| 14 | // `ReencodedK` is not actually permitted by `verify()`, but it looks that way in the tests |
| 15 | // because it sometimes occurs with a low-order A. 1/8 of the time, the resulting signature |
| 16 | // will be identical the one made with a normal k. find_validation_criteria shows that indeed |
| 17 | // this occurs 10/58 of the time |
| 18 | Flag::ReencodedK, |
| 19 | ]; |
| 20 | |
| 21 | /// The set of edge cases that [`VerifyingKey::verify_strict()`] permits |
| 22 | const VERIFY_STRICT_ALLOWED_EDGECASES: &[Flag] = |
| 23 | &[Flag::LowOrderComponentA, Flag::LowOrderComponentR]; |
| 24 | |
| 25 | /// Each variant describes a specific edge case that can occur in an Ed25519 signature. Refer to |
| 26 | /// the test vector [README][] for more info. |
| 27 | /// |
| 28 | /// [README]: https://github.com/C2SP/CCTV/blob/5ea85644bd035c555900a2f707f7e4c31ea65ced/ed25519vectors/README.md |
| 29 | #[derive(Deserialize, Debug, Copy, Clone, PartialOrd, Ord, Eq, PartialEq)] |
| 30 | enum Flag { |
| 31 | #[serde(rename = "low_order")] |
| 32 | LowOrder, |
| 33 | #[serde(rename = "low_order_A")] |
| 34 | LowOrderA, |
| 35 | #[serde(rename = "low_order_R")] |
| 36 | LowOrderR, |
| 37 | #[serde(rename = "non_canonical_A")] |
| 38 | NonCanonicalA, |
| 39 | #[serde(rename = "non_canonical_R")] |
| 40 | NonCanonicalR, |
| 41 | #[serde(rename = "low_order_component_A")] |
| 42 | LowOrderComponentA, |
| 43 | #[serde(rename = "low_order_component_R")] |
| 44 | LowOrderComponentR, |
| 45 | #[serde(rename = "low_order_residue")] |
| 46 | LowOrderResidue, |
| 47 | #[serde(rename = "reencoded_k")] |
| 48 | ReencodedK, |
| 49 | } |
| 50 | |
| 51 | /// This is an intermediate representation between JSON and TestVector |
| 52 | #[derive(Deserialize)] |
| 53 | struct IntermediateTestVector { |
| 54 | number: usize, |
| 55 | #[serde(deserialize_with = "bytes_from_hex", rename = "key")] |
| 56 | pubkey: Vec<u8>, |
| 57 | #[serde(deserialize_with = "bytes_from_hex")] |
| 58 | sig: Vec<u8>, |
| 59 | msg: String, |
| 60 | flags: Option<Set<Flag>>, |
| 61 | } |
| 62 | |
| 63 | /// The test vector struct from [CCTV][]. `sig` may or may not be a valid signature of `msg` with |
| 64 | /// respect to `pubkey`, depending on the verification function's validation criteria. `flags` |
| 65 | /// describes all the edge cases which this test vector falls into. |
| 66 | /// |
| 67 | /// [CCTV]: https://github.com/C2SP/CCTV/tree/5ea85644bd035c555900a2f707f7e4c31ea65ced/ed25519vectors |
| 68 | struct TestVector { |
| 69 | number: usize, |
| 70 | pubkey: VerifyingKey, |
| 71 | sig: Signature, |
| 72 | msg: Vec<u8>, |
| 73 | flags: Set<Flag>, |
| 74 | } |
| 75 | |
| 76 | impl From<IntermediateTestVector> for TestVector { |
| 77 | fn from(tv: IntermediateTestVector) -> Self { |
| 78 | let number = tv.number; |
| 79 | let pubkey = { |
| 80 | let mut buf = [0u8; 32]; |
| 81 | buf.copy_from_slice(&tv.pubkey); |
| 82 | VerifyingKey::from_bytes(&buf).unwrap() |
| 83 | }; |
| 84 | let sig = { |
| 85 | let mut buf = [0u8; 64]; |
| 86 | buf.copy_from_slice(&tv.sig); |
| 87 | Signature::from_bytes(&buf) |
| 88 | }; |
| 89 | let msg = tv.msg.as_bytes().to_vec(); |
| 90 | |
| 91 | // Unwrap the Option<Set<Flag>> |
| 92 | let flags = tv.flags.unwrap_or_default(); |
| 93 | |
| 94 | Self { |
| 95 | number, |
| 96 | pubkey, |
| 97 | sig, |
| 98 | msg, |
| 99 | flags, |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Tells serde how to deserialize bytes from hex |
| 105 | fn bytes_from_hex<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error> |
| 106 | where |
| 107 | D: Deserializer<'de>, |
| 108 | { |
| 109 | let mut hex_str = String::deserialize(deserializer)?; |
| 110 | // Prepend a 0 if it's not even length |
| 111 | if hex_str.len() % 2 == 1 { |
| 112 | hex_str.insert(0, '0'); |
| 113 | } |
| 114 | hex::decode(hex_str).map_err(|e| SError::custom(format!("{:?}", e))) |
| 115 | } |
| 116 | |
| 117 | fn get_test_vectors() -> impl Iterator<Item = TestVector> { |
| 118 | let f = File::open("VALIDATIONVECTORS").expect( |
| 119 | "This test is only available when the code has been cloned from the git repository, since |
| 120 | the VALIDATIONVECTORS file is large and is therefore not included within the distributed \ |
| 121 | crate.", |
| 122 | ); |
| 123 | |
| 124 | serde_json::from_reader::<_, Vec<IntermediateTestVector>>(f) |
| 125 | .unwrap() |
| 126 | .into_iter() |
| 127 | .map(TestVector::from) |
| 128 | } |
| 129 | |
| 130 | /// Tests that the verify() and verify_strict() functions succeed only on test cases whose flags |
| 131 | /// (i.e., edge cases it falls into) are a subset of VERIFY_ALLOWED_EDGECASES and |
| 132 | /// VERIFY_STRICT_ALLOWED_EDGECASES, respectively |
| 133 | #[test] |
| 134 | fn check_validation_criteria() { |
| 135 | let verify_allowed_edgecases = Set::from_iter(VERIFY_ALLOWED_EDGECASES.to_vec()); |
| 136 | let verify_strict_allowed_edgecases = Set::from_iter(VERIFY_STRICT_ALLOWED_EDGECASES.to_vec()); |
| 137 | |
| 138 | for TestVector { |
| 139 | number, |
| 140 | pubkey, |
| 141 | msg, |
| 142 | sig, |
| 143 | flags, |
| 144 | } in get_test_vectors() |
| 145 | { |
| 146 | // If all the verify-permitted flags here are ones we permit, then verify() should succeed. |
| 147 | // Otherwise, it should not. |
| 148 | let success = pubkey.verify(&msg, &sig).is_ok(); |
| 149 | if flags.is_subset(&verify_allowed_edgecases) { |
| 150 | assert!(success, "verify() expected success in testcase #{number}",); |
| 151 | } else { |
| 152 | assert!(!success, "verify() expected failure in testcase #{number}",); |
| 153 | } |
| 154 | |
| 155 | // If all the verify_strict-permitted flags here are ones we permit, then verify_strict() |
| 156 | // should succeed. Otherwise, it should not. |
| 157 | let success = pubkey.verify_strict(&msg, &sig).is_ok(); |
| 158 | if flags.is_subset(&verify_strict_allowed_edgecases) { |
| 159 | assert!( |
| 160 | success, |
| 161 | "verify_strict() expected success in testcase #{number}", |
| 162 | ); |
| 163 | } else { |
| 164 | assert!( |
| 165 | !success, |
| 166 | "verify_strict() expected failure in testcase #{number}", |
| 167 | ); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /// Prints the flags that are consistently permitted by verify() and verify_strict() |
| 173 | #[test] |
| 174 | fn find_validation_criteria() { |
| 175 | let mut verify_allowed_edgecases = Set::new(); |
| 176 | let mut verify_strict_allowed_edgecases = Set::new(); |
| 177 | |
| 178 | // Counts the number of times a signature with a re-encoded k and a low-order A verified. This |
| 179 | // happens with 1/8 probability, assuming the usual verification equation(s). |
| 180 | let mut num_lucky_reencoded_k = 0; |
| 181 | let mut num_reencoded_k = 0; |
| 182 | |
| 183 | for TestVector { |
| 184 | number: _, |
| 185 | pubkey, |
| 186 | msg, |
| 187 | sig, |
| 188 | flags, |
| 189 | } in get_test_vectors() |
| 190 | { |
| 191 | // If verify() was a success, add all the associated flags to verify-permitted set |
| 192 | let success = pubkey.verify(&msg, &sig).is_ok(); |
| 193 | |
| 194 | // If this is ReencodedK && LowOrderA, log some statistics |
| 195 | if flags.contains(&Flag::ReencodedK) && flags.contains(&Flag::LowOrderA) { |
| 196 | num_reencoded_k += 1; |
| 197 | num_lucky_reencoded_k += success as u8; |
| 198 | } |
| 199 | |
| 200 | if success { |
| 201 | for flag in &flags { |
| 202 | // Don't count re-encoded k when A is low-order. This is because the |
| 203 | // re-encoded k might be a multiple of 8 by accident |
| 204 | if *flag == Flag::ReencodedK && flags.contains(&Flag::LowOrderA) { |
| 205 | continue; |
| 206 | } else { |
| 207 | verify_allowed_edgecases.insert(*flag); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // If verify_strict() was a success, add all the associated flags to |
| 213 | // verify_strict-permitted set |
| 214 | let success = pubkey.verify_strict(&msg, &sig).is_ok(); |
| 215 | if success { |
| 216 | for flag in &flags { |
| 217 | verify_strict_allowed_edgecases.insert(*flag); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | println!("VERIFY_ALLOWED_EDGECASES: {:?}", verify_allowed_edgecases); |
| 223 | println!( |
| 224 | "VERIFY_STRICT_ALLOWED_EDGECASES: {:?}", |
| 225 | verify_strict_allowed_edgecases |
| 226 | ); |
| 227 | println!( |
| 228 | "re-encoded k && low-order A yielded a valid signature {}/{} of the time", |
| 229 | num_lucky_reencoded_k, num_reencoded_k |
| 230 | ); |
| 231 | } |