symmetric.rs
benches/crypto/cases/src/symmetric.rs
1//! All buffers are allocated at setup. Timed in-place cases include restoring2//! the caller buffer; the copy control makes that work explicit.3//! Existing AEAD cases replay fixed key/nonce/AAD/plaintext fixtures across4//! calls. The `*-records` seal cases instead use one key with record-specific5//! input/AAD and nonce counters that advance across the driver's cohorts.6use cdk_rsx_crypto_bench_api::{Case, Operation, Params, Pattern};7use cdk_rsx_crypto_symmetric as s;8use std::hint::black_box;910#[path = "symmetric_chacha.rs"]11mod chacha_candidates;12// The root build owner chooses candidate registration for matched IC runs.13pub(crate) use chacha_candidates::cases as chacha_candidate_cases;14#[path = "symmetric_cohort.rs"]15mod chacha_cohort;16pub(crate) use chacha_cohort::cases as chacha_cohort_cases;1718#[derive(Clone, Copy)]19enum Algorithm {20 ChaCha,21 XChaCha,22 Gcm128,23 Gcm256,24 Siv128,25 Siv256,26 Ascon,27 AsconUnrolled,28}29#[derive(Clone, Copy)]30enum Kind {31 Copy,32 ChaChaBlock,33 ChaChaFresh,34 ChaChaPrepared,35 ChaChaReference,36 HChaCha,37 PolyFresh,38 PolyPrepared,39 PolyVerify,40 Aes128Fresh,41 Aes128Prepared,42 Aes256Fresh,43 Aes256Prepared,44 GHash,45 Polyval,46 AsconHash,47 AsconXof,48 AsconCxof,49 Seal(Algorithm, bool),50 Open(Algorithm, bool),51}52enum Cipher {53 ChaCha(s::ChaCha20Poly1305),54 XChaCha(s::XChaCha20Poly1305),55 Gcm128(s::Aes128Gcm),56 Gcm256(s::Aes256Gcm),57 Siv128(s::Aes128GcmSiv),58 Siv256(s::Aes256GcmSiv),59 Ascon(s::AsconAead128, bool),60}61impl Cipher {62 fn new(algorithm: Algorithm, key: &[u8; 32]) -> Self {63 match algorithm {64 Algorithm::ChaCha => Self::ChaCha(s::ChaCha20Poly1305::new(key)),65 Algorithm::XChaCha => Self::XChaCha(s::XChaCha20Poly1305::new(key)),66 Algorithm::Gcm128 => {67 Self::Gcm128(s::Aes128Gcm::new(key[..16].try_into().expect("key")))68 }69 Algorithm::Gcm256 => Self::Gcm256(s::Aes256Gcm::new(key)),70 Algorithm::Siv128 => {71 Self::Siv128(s::Aes128GcmSiv::new(key[..16].try_into().expect("key")))72 }73 Algorithm::Siv256 => Self::Siv256(s::Aes256GcmSiv::new(key)),74 Algorithm::Ascon | Algorithm::AsconUnrolled => Self::Ascon(75 s::AsconAead128::new(key[..16].try_into().expect("key")),76 matches!(algorithm, Algorithm::AsconUnrolled),77 ),78 }79 }80 fn seal(&self, nonce: &[u8; 24], aad: &[u8], body: &mut [u8]) -> Result<[u8; 16], s::Error> {81 match self {82 Self::ChaCha(c) => c.seal(nonce[..12].try_into().expect("nonce"), aad, body),83 Self::XChaCha(c) => c.seal(nonce, aad, body),84 Self::Gcm128(c) => c.seal(nonce[..12].try_into().expect("nonce"), aad, body),85 Self::Gcm256(c) => c.seal(nonce[..12].try_into().expect("nonce"), aad, body),86 Self::Siv128(c) => c.seal(nonce[..12].try_into().expect("nonce"), aad, body),87 Self::Siv256(c) => c.seal(nonce[..12].try_into().expect("nonce"), aad, body),88 Self::Ascon(c, false) => c.seal(nonce[..16].try_into().expect("nonce"), aad, body),89 Self::Ascon(c, true) => {90 c.seal_unrolled(nonce[..16].try_into().expect("nonce"), aad, body)91 }92 }93 }94 fn open(95 &self,96 nonce: &[u8; 24],97 aad: &[u8],98 body: &mut [u8],99 tag: &[u8; 16],100 ) -> Result<(), s::Error> {101 match self {102 Self::ChaCha(c) => c.open(nonce[..12].try_into().expect("nonce"), aad, body, tag),103 Self::XChaCha(c) => c.open(nonce, aad, body, tag),104 Self::Gcm128(c) => c.open(nonce[..12].try_into().expect("nonce"), aad, body, tag),105 Self::Gcm256(c) => c.open(nonce[..12].try_into().expect("nonce"), aad, body, tag),106 Self::Siv128(c) => c.open(nonce[..12].try_into().expect("nonce"), aad, body, tag),107 Self::Siv256(c) => c.open(nonce[..12].try_into().expect("nonce"), aad, body, tag),108 Self::Ascon(c, false) => c.open(nonce[..16].try_into().expect("nonce"), aad, body, tag),109 Self::Ascon(c, true) => {110 c.open_unrolled(nonce[..16].try_into().expect("nonce"), aad, body, tag)111 }112 }113 }114}115enum Prepared {116 None,117 Cipher(Box<Cipher>),118 ChaCha(s::ChaCha20Key),119 Poly(s::Poly1305Key),120 Aes128(Box<s::Aes128Key>),121 Aes256(Box<s::Aes256Key>),122}123struct Item {124 key: [u8; 32],125 nonce: [u8; 24],126 aad: [u8; 32],127 plain: Vec<u8>,128 ciphertext: Vec<u8>,129 body: Vec<u8>,130 blocks: Vec<[u8; 16]>,131 expected: Vec<u8>,132 tag: [u8; 16],133 expected_tag: [u8; 16],134 output: [u8; 64],135 expected_output: [u8; 64],136 success: bool,137 expected_success: bool,138 prepared: Prepared,139}140struct Fixture {141 common_prepared: Option<Prepared>,142 kind: Kind,143 items: Vec<Item>,144 vary_records: bool,145 ran: bool,146}147fn pattern(seed: u64, n: usize) -> Vec<u8> {148 let mut x = seed;149 (0..n)150 .map(|_| {151 x ^= x << 13;152 x ^= x >> 7;153 x ^= x << 17;154 x as u8155 })156 .collect()157}158159fn prepare(p: &Params, kind: Kind) -> Result<Box<dyn Operation>, String> {160 prepare_mode(p, kind, false)161}162163fn prepare_records(p: &Params, algorithm: Algorithm) -> Result<Box<dyn Operation>, String> {164 if p.distinct {165 return Err(166 "fixed-key record cases require distinct=false; inputs and nonces vary by record"167 .into(),168 );169 }170 prepare_mode(p, Kind::Seal(algorithm, true), true)171}172173fn record_counter(iteration: u64, count: usize, index: usize) -> [u8; 12] {174 // With count <= 500, the largest u64 cohort number needs at most 73 bits.175 let value = iteration as u128 * count as u128 + index as u128;176 value.to_be_bytes()[4..].try_into().expect("96-bit counter")177}178179fn prepare_mode(p: &Params, kind: Kind, vary_records: bool) -> Result<Box<dyn Operation>, String> {180 if p.batch == 0181 || p.batch > 500182 || p.message_len > 1024 * 1024183 || p.message_len184 .checked_mul(p.batch)185 .is_none_or(|n| n > 8 * 1024 * 1024)186 {187 return Err("symmetric fixture exceeds bounded bytes or batch 1..=500".into());188 }189 if matches!(190 kind,191 Kind::Aes128Fresh192 | Kind::Aes128Prepared193 | Kind::Aes256Fresh194 | Kind::Aes256Prepared195 | Kind::GHash196 | Kind::Polyval197 ) && (p.message_len == 0 || p.message_len % 16 != 0)198 {199 return Err(200 "raw AES/universal-hash case needs a positive whole 16-byte block length".into(),201 );202 }203 if matches!(kind, Kind::ChaChaBlock) && (p.message_len == 0 || p.message_len % 64 != 0) {204 return Err("ChaCha block case needs a positive whole 64-byte block length".into());205 }206 let mut items = Vec::with_capacity(p.batch);207 for i in 0..p.batch {208 let seed = p.seed.wrapping_add(if p.distinct { i as u64 } else { 0 });209 let key: [u8; 32] = pattern(seed ^ 0x834df3871, 32).try_into().expect("key");210 let record_seed = if vary_records {211 seed.wrapping_add(i as u64 + 1)212 } else {213 seed214 };215 let mut nonce: [u8; 24] = pattern(record_seed ^ 0xf138daf27, 24)216 .try_into()217 .expect("nonce");218 if vary_records {219 nonce[..12].copy_from_slice(&record_counter(0, p.batch, i));220 }221 let aad: [u8; 32] = pattern(record_seed ^ 0xc9a781e, 32)222 .try_into()223 .expect("aad");224 let mut plain = pattern(record_seed, p.message_len);225 if matches!(kind, Kind::ChaChaBlock) {226 plain.fill(0);227 }228 let mut ciphertext = plain.clone();229 let mut expected = plain.clone();230 let mut expected_output = [0; 64];231 let mut expected_tag = [0; 16];232 let valid = match p.pattern {233 Pattern::Valid => true,234 Pattern::Invalid => false,235 Pattern::Mixed => i % 2 == 0,236 };237 let mut expected_success = true;238 let mut blocks: Vec<[u8; 16]> = plain239 .chunks_exact(16)240 .map(|b| b.try_into().expect("block"))241 .collect();242 match kind {243 Kind::Copy => {}244 Kind::ChaChaBlock245 | Kind::ChaChaFresh246 | Kind::ChaChaPrepared247 | Kind::ChaChaReference => {248 s::chacha20_xor_reference(249 &key,250 nonce[..12].try_into().expect("nonce"),251 1,252 &mut expected,253 )254 .map_err(|e| format!("{e:?}"))?;255 }256 Kind::HChaCha => expected_output[..32]257 .copy_from_slice(&s::hchacha20(&key, nonce[..16].try_into().expect("nonce"))),258 Kind::PolyFresh | Kind::PolyPrepared | Kind::PolyVerify => {259 expected_tag = s::poly1305_tag(&key, &plain);260 if matches!(kind, Kind::PolyVerify) {261 expected_success = valid;262 }263 }264 Kind::Aes128Fresh | Kind::Aes128Prepared => {265 let k = s::Aes128Key::new(key[..16].try_into().expect("key"));266 for b in expected.chunks_exact_mut(16) {267 k.encrypt(b.try_into().expect("block"));268 }269 }270 Kind::Aes256Fresh | Kind::Aes256Prepared => {271 let k = s::Aes256Key::new(&key);272 for b in expected.chunks_exact_mut(16) {273 k.encrypt(b.try_into().expect("block"));274 }275 }276 Kind::GHash => expected_output[..16]277 .copy_from_slice(&s::ghash(key[..16].try_into().expect("key"), &blocks)),278 Kind::Polyval => expected_output[..16]279 .copy_from_slice(&s::polyval(key[..16].try_into().expect("key"), &blocks)),280 Kind::AsconHash => expected_output[..32].copy_from_slice(&s::ascon::hash256(&plain)),281 Kind::AsconXof => s::ascon::xof128(&plain, &mut expected_output),282 Kind::AsconCxof => s::ascon::cxof128(&aad, &plain, &mut expected_output)283 .map_err(|e| format!("{e:?}"))?,284 Kind::Seal(a, _) | Kind::Open(a, _) => {285 // Loop permutation is the independent control for the unrolled candidate.286 let reference = if matches!(a, Algorithm::AsconUnrolled) {287 Algorithm::Ascon288 } else {289 a290 };291 expected_tag = Cipher::new(reference, &key)292 .seal(&nonce, &aad, &mut ciphertext)293 .map_err(|e| format!("{e:?}"))?;294 if matches!(kind, Kind::Seal(..)) {295 expected = ciphertext.clone();296 } else {297 expected_success = valid;298 if !valid {299 expected.fill(0);300 }301 }302 }303 }304 let mut tag = expected_tag;305 if matches!(kind, Kind::Open(..) | Kind::PolyVerify) && !valid {306 tag[if i % 2 == 0 { 0 } else { 15 }] ^= 1;307 }308 let prepared = if p.distinct || i == 0 {309 match kind {310 Kind::Seal(a, true) | Kind::Open(a, true) => {311 Prepared::Cipher(Box::new(Cipher::new(a, &key)))312 }313 Kind::ChaChaPrepared => Prepared::ChaCha(s::ChaCha20Key::new(&key)),314 Kind::PolyPrepared | Kind::PolyVerify => Prepared::Poly(s::Poly1305Key::new(&key)),315 Kind::Aes128Prepared => Prepared::Aes128(Box::new(s::Aes128Key::new(316 key[..16].try_into().expect("key"),317 ))),318 Kind::Aes256Prepared => Prepared::Aes256(Box::new(s::Aes256Key::new(&key))),319 _ => Prepared::None,320 }321 } else {322 Prepared::None323 };324 blocks.shrink_to_fit();325 items.push(Item {326 key,327 nonce,328 aad,329 plain: plain.clone(),330 ciphertext,331 body: plain,332 blocks,333 expected,334 tag,335 expected_tag,336 output: [0; 64],337 expected_output,338 success: false,339 expected_success,340 prepared,341 });342 }343 let common_prepared = if p.distinct {344 None345 } else {346 Some(core::mem::replace(&mut items[0].prepared, Prepared::None))347 };348 Ok(Box::new(Fixture {349 common_prepared,350 kind,351 items,352 vary_records,353 ran: false,354 }))355}356impl Operation for Fixture {357 fn operations_per_run(&self) -> usize {358 self.items.len()359 }360361 fn run(&mut self, iteration: u64) -> u64 {362 let mut checksum = 0u64;363 let count = self.items.len();364 for (i, item) in self.items.iter_mut().enumerate() {365 if self.vary_records {366 item.nonce[..12].copy_from_slice(&record_counter(iteration, count, i));367 }368 let retained = match &mut self.common_prepared {369 Some(context) => context,370 None => &mut item.prepared,371 };372 let key = black_box(&item.key);373 let nonce = black_box(&item.nonce);374 let aad = black_box(&item.aad);375 item.success = true;376 match self.kind {377 Kind::Copy => item.body.copy_from_slice(black_box(&item.plain)),378 Kind::ChaChaBlock => {379 let prepared = s::ChaCha20Key::new(key);380 for (i, b) in item.body.chunks_exact_mut(64).enumerate() {381 b.copy_from_slice(382 &prepared.block(nonce[..12].try_into().expect("nonce"), 1 + i as u32),383 );384 }385 }386 Kind::ChaChaFresh => {387 item.body.copy_from_slice(black_box(&item.plain));388 s::chacha20_xor(389 key,390 nonce[..12].try_into().expect("nonce"),391 1,392 &mut item.body,393 )394 .expect("bounded");395 }396 Kind::ChaChaReference => {397 item.body.copy_from_slice(black_box(&item.plain));398 s::chacha20_xor_reference(399 key,400 nonce[..12].try_into().expect("nonce"),401 1,402 &mut item.body,403 )404 .expect("bounded");405 }406 Kind::ChaChaPrepared => {407 item.body.copy_from_slice(black_box(&item.plain));408 let Prepared::ChaCha(k) = &*retained else {409 unreachable!()410 };411 k.xor(nonce[..12].try_into().expect("nonce"), 1, &mut item.body)412 .expect("bounded");413 }414 Kind::HChaCha => item.output[..32]415 .copy_from_slice(&s::hchacha20(key, nonce[..16].try_into().expect("nonce"))),416 Kind::PolyFresh => {417 item.output[..16].copy_from_slice(&s::poly1305_tag(key, black_box(&item.plain)))418 }419 Kind::PolyPrepared => {420 let Prepared::Poly(k) = &*retained else {421 unreachable!()422 };423 item.output[..16].copy_from_slice(&k.tag(black_box(&item.plain)));424 }425 Kind::PolyVerify => {426 let Prepared::Poly(k) = &*retained else {427 unreachable!()428 };429 item.success = k.verify(black_box(&item.plain), black_box(&item.tag));430 }431 Kind::Aes128Fresh432 | Kind::Aes128Prepared433 | Kind::Aes256Fresh434 | Kind::Aes256Prepared => {435 for (b, plain) in item436 .blocks437 .iter_mut()438 .zip(black_box(item.plain.chunks_exact(16)))439 {440 b.copy_from_slice(plain);441 }442 match self.kind {443 Kind::Aes128Fresh => s::Aes128Key::new(key[..16].try_into().expect("key"))444 .encrypt_blocks(&mut item.blocks),445 Kind::Aes256Fresh => {446 s::Aes256Key::new(key).encrypt_blocks(&mut item.blocks)447 }448 Kind::Aes128Prepared => {449 let Prepared::Aes128(k) = &*retained else {450 unreachable!()451 };452 k.encrypt_blocks(&mut item.blocks);453 }454 _ => {455 let Prepared::Aes256(k) = &*retained else {456 unreachable!()457 };458 k.encrypt_blocks(&mut item.blocks);459 }460 }461 }462 Kind::GHash => item.output[..16].copy_from_slice(&s::ghash(463 key[..16].try_into().expect("key"),464 black_box(&item.blocks),465 )),466 Kind::Polyval => item.output[..16].copy_from_slice(&s::polyval(467 key[..16].try_into().expect("key"),468 black_box(&item.blocks),469 )),470 Kind::AsconHash => {471 item.output[..32].copy_from_slice(&s::ascon::hash256(black_box(&item.plain)))472 }473 Kind::AsconXof => s::ascon::xof128(black_box(&item.plain), &mut item.output),474 Kind::AsconCxof => s::ascon::cxof128(aad, black_box(&item.plain), &mut item.output)475 .expect("customization bound"),476 Kind::Seal(a, prepared) => {477 item.body.copy_from_slice(black_box(&item.plain));478 item.output[..16].copy_from_slice(479 &if prepared {480 let Prepared::Cipher(c) = &*retained else {481 unreachable!()482 };483 c.seal(nonce, aad, &mut item.body)484 } else {485 Cipher::new(a, key).seal(nonce, aad, &mut item.body)486 }487 .expect("bounded"),488 );489 }490 Kind::Open(a, prepared) => {491 item.body.copy_from_slice(black_box(&item.ciphertext));492 item.success = if prepared {493 let Prepared::Cipher(c) = &*retained else {494 unreachable!()495 };496 c.open(nonce, aad, &mut item.body, black_box(&item.tag))497 } else {498 Cipher::new(a, key).open(nonce, aad, &mut item.body, black_box(&item.tag))499 }500 .is_ok();501 }502 }503 // Observe complete outputs, including failure-wiped bodies and every block.504 black_box(item.body.as_slice());505 black_box(item.blocks.as_slice());506 let output = black_box(item.output);507 black_box(item.success);508 checksum = checksum.rotate_left(7)509 ^ u64::from_le_bytes(output[..8].try_into().expect("checksum"))510 ^ item.success as u64;511 if let Some(byte) = item.body.first() {512 checksum ^= *byte as u64;513 }514 if let Some(block) = item.blocks.first() {515 checksum ^= u64::from_le_bytes(block[..8].try_into().expect("checksum"));516 }517 }518 self.ran = true;519 checksum520 }521 fn check(&mut self) -> Result<(), String> {522 if !self.ran {523 return Err("operation has not run".into());524 }525 for (i, item) in self.items.iter_mut().enumerate() {526 if self.vary_records {527 let Kind::Seal(algorithm, true) = self.kind else {528 unreachable!()529 };530 let reference = if matches!(algorithm, Algorithm::AsconUnrolled) {531 Algorithm::Ascon532 } else {533 algorithm534 };535 item.expected.copy_from_slice(&item.plain);536 item.expected_tag = Cipher::new(reference, &item.key)537 .seal(&item.nonce, &item.aad, &mut item.expected)538 .map_err(|e| format!("record reference seal: {e:?}"))?;539 }540 let correct = match self.kind {541 Kind::HChaCha542 | Kind::GHash543 | Kind::Polyval544 | Kind::AsconHash545 | Kind::AsconXof546 | Kind::AsconCxof => item.output == item.expected_output,547 Kind::PolyFresh | Kind::PolyPrepared => item.output[..16] == item.expected_tag,548 Kind::PolyVerify => item.success == item.expected_success,549 Kind::Aes128Fresh550 | Kind::Aes128Prepared551 | Kind::Aes256Fresh552 | Kind::Aes256Prepared => item553 .blocks554 .iter()555 .flatten()556 .copied()557 .eq(item.expected.iter().copied()),558 Kind::Seal(..) => {559 item.body == item.expected && item.output[..16] == item.expected_tag560 }561 Kind::Open(..) => {562 item.body == item.expected && item.success == item.expected_success563 }564 _ => item.body == item.expected,565 };566 if !correct {567 return Err(format!("symmetric complete output mismatch at item {i}"));568 }569 }570 Ok(())571 }572}573macro_rules! definitions {574 ($(($function:ident,$kind:expr,$id:literal,$description:literal)),*$(,)?)=>{575 $(fn $function(p:&Params)->Result<Box<dyn Operation>,String>{prepare(p,$kind)})*576 pub fn cases()->Vec<Case>{577 let mut cases = vec![$(Case{id:$id,description:$description,prepare:$function}),*];578 cases.extend(record_cases());579 cases580 }581 }582}583584fn record_cases() -> Vec<Case> {585 vec![586 Case {587 id: "chachapoly-seal-prepared-records",588 description: "One prepared ChaCha20-Poly1305 key, varying input/AAD and advancing nonce per record",589 prepare: |p| prepare_records(p, Algorithm::ChaCha),590 },591 Case {592 id: "xchachapoly-seal-prepared-records",593 description: "One prepared XChaCha20-Poly1305 key, varying input/AAD and advancing nonce per record",594 prepare: |p| prepare_records(p, Algorithm::XChaCha),595 },596 Case {597 id: "aes128gcm-seal-prepared-records",598 description: "One prepared AES-128-GCM key, varying input/AAD and advancing nonce per record",599 prepare: |p| prepare_records(p, Algorithm::Gcm128),600 },601 Case {602 id: "aes256gcm-seal-prepared-records",603 description: "One prepared AES-256-GCM key, varying input/AAD and advancing nonce per record",604 prepare: |p| prepare_records(p, Algorithm::Gcm256),605 },606 Case {607 id: "aes128gcmsiv-seal-prepared-records",608 description: "One prepared AES-128-GCM-SIV key, varying input/AAD and advancing nonce per record",609 prepare: |p| prepare_records(p, Algorithm::Siv128),610 },611 Case {612 id: "aes256gcmsiv-seal-prepared-records",613 description: "One prepared AES-256-GCM-SIV key, varying input/AAD and advancing nonce per record",614 prepare: |p| prepare_records(p, Algorithm::Siv256),615 },616 Case {617 id: "ascon-seal-prepared-records",618 description: "One prepared final Ascon key, loop permutation, varying records and advancing nonces",619 prepare: |p| prepare_records(p, Algorithm::Ascon),620 },621 Case {622 id: "ascon-seal-unrolled-records",623 description: "Same varying records and advancing nonces with the explicit unrolled Ascon candidate",624 prepare: |p| prepare_records(p, Algorithm::AsconUnrolled),625 },626 ]627}628definitions!(629 (630 copy,631 Kind::Copy,632 "symmetric-buffer-copy",633 "Caller-buffer restore only; matched reset-copy control"634 ),635 (636 chb,637 Kind::ChaChaBlock,638 "chacha20-blocks",639 "RFC 8439 scalar block function, whole 64-byte blocks, includes key-word setup"640 ),641 (642 chf,643 Kind::ChaChaFresh,644 "chacha20-inplace-fresh",645 "Scalar ChaCha20 in-place XOR plus restore, fresh key words"646 ),647 (648 chp,649 Kind::ChaChaPrepared,650 "chacha20-inplace-prepared",651 "Scalar ChaCha20 in-place XOR plus restore, retained key words"652 ),653 (654 chr,655 Kind::ChaChaReference,656 "chacha20-reference",657 "Pinned RustCrypto ChaCha20 XOR plus restore"658 ),659 (660 hch,661 Kind::HChaCha,662 "hchacha20",663 "Established HChaCha20 subkey primitive; fixed 32-byte key and 16-byte nonce"664 ),665 (666 polyf,667 Kind::PolyFresh,668 "poly1305-fresh",669 "RFC 8439 Poly1305 tag including one-time key preparation; public benchmark key"670 ),671 (672 polyp,673 Kind::PolyPrepared,674 "poly1305-prepared",675 "Poly1305 tag with retained context; repeated public test key only"676 ),677 (678 polyv,679 Kind::PolyVerify,680 "poly1305-verify",681 "Poly1305 constant-time verification; valid/invalid/mixed tags"682 ),683 (684 a128f,685 Kind::Aes128Fresh,686 "aes128-blocks-fresh",687 "AES-128 whole-block encryption including schedule and input restore"688 ),689 (690 a128p,691 Kind::Aes128Prepared,692 "aes128-blocks-prepared",693 "AES-128 parallel whole-block encryption with retained schedule plus restore"694 ),695 (696 a256f,697 Kind::Aes256Fresh,698 "aes256-blocks-fresh",699 "AES-256 whole-block encryption including schedule and input restore"700 ),701 (702 a256p,703 Kind::Aes256Prepared,704 "aes256-blocks-prepared",705 "AES-256 parallel whole-block encryption with retained schedule plus restore"706 ),707 (708 gh,709 Kind::GHash,710 "ghash-blocks",711 "Raw GHASH complete 16-byte blocks including key setup, no AEAD framing"712 ),713 (714 pv,715 Kind::Polyval,716 "polyval-blocks",717 "Raw POLYVAL complete 16-byte blocks including key setup, no AEAD framing"718 ),719 (720 ash,721 Kind::AsconHash,722 "ascon-hash256",723 "Final NIST SP 800-232 Ascon-Hash256, 32-byte output"724 ),725 (726 asx,727 Kind::AsconXof,728 "ascon-xof128",729 "Final NIST Ascon-XOF128, 64-byte output"730 ),731 (732 ascx,733 Kind::AsconCxof,734 "ascon-cxof128",735 "Final NIST Ascon-CXOF128, fixed 32-byte customization and 64-byte output"736 ),737 (738 ccsf,739 Kind::Seal(Algorithm::ChaCha, false),740 "chachapoly-seal-fresh",741 "ChaCha20-Poly1305 detached seal including fresh key and buffer restore"742 ),743 (744 ccsp,745 Kind::Seal(Algorithm::ChaCha, true),746 "chachapoly-seal-prepared",747 "ChaCha20-Poly1305 detached seal, retained key, includes buffer restore"748 ),749 (750 ccof,751 Kind::Open(Algorithm::ChaCha, false),752 "chachapoly-open-fresh",753 "ChaCha20-Poly1305 open including fresh key/restore; failed body wiped"754 ),755 (756 ccop,757 Kind::Open(Algorithm::ChaCha, true),758 "chachapoly-open-prepared",759 "ChaCha20-Poly1305 open retained key/restore; valid/invalid/mixed tags"760 ),761 (762 xsf,763 Kind::Seal(Algorithm::XChaCha, false),764 "xchachapoly-seal-fresh",765 "XChaCha20-Poly1305 detached seal including fresh key and restore"766 ),767 (768 xsp,769 Kind::Seal(Algorithm::XChaCha, true),770 "xchachapoly-seal-prepared",771 "XChaCha20-Poly1305 seal retained key, includes restore"772 ),773 (774 xof,775 Kind::Open(Algorithm::XChaCha, false),776 "xchachapoly-open-fresh",777 "XChaCha20-Poly1305 open including key/restore; failed body wiped"778 ),779 (780 xop,781 Kind::Open(Algorithm::XChaCha, true),782 "xchachapoly-open-prepared",783 "XChaCha20-Poly1305 open retained key/restore; valid/invalid/mixed tags"784 ),785 (786 g128sf,787 Kind::Seal(Algorithm::Gcm128, false),788 "aes128gcm-seal-fresh",789 "AES-128-GCM seal including fresh key schedule and restore"790 ),791 (792 g128sp,793 Kind::Seal(Algorithm::Gcm128, true),794 "aes128gcm-seal-prepared",795 "AES-128-GCM seal retained key schedule plus restore"796 ),797 (798 g128of,799 Kind::Open(Algorithm::Gcm128, false),800 "aes128gcm-open-fresh",801 "AES-128-GCM open including key schedule/restore; failed body wiped"802 ),803 (804 g128op,805 Kind::Open(Algorithm::Gcm128, true),806 "aes128gcm-open-prepared",807 "AES-128-GCM open retained key/restore; valid/invalid/mixed tags"808 ),809 (810 g256sf,811 Kind::Seal(Algorithm::Gcm256, false),812 "aes256gcm-seal-fresh",813 "AES-256-GCM seal including fresh key schedule and restore"814 ),815 (816 g256sp,817 Kind::Seal(Algorithm::Gcm256, true),818 "aes256gcm-seal-prepared",819 "AES-256-GCM seal retained key schedule plus restore"820 ),821 (822 g256of,823 Kind::Open(Algorithm::Gcm256, false),824 "aes256gcm-open-fresh",825 "AES-256-GCM open including key schedule/restore; failed body wiped"826 ),827 (828 g256op,829 Kind::Open(Algorithm::Gcm256, true),830 "aes256gcm-open-prepared",831 "AES-256-GCM open retained key/restore; valid/invalid/mixed tags"832 ),833 (834 s128sf,835 Kind::Seal(Algorithm::Siv128, false),836 "aes128gcmsiv-seal-fresh",837 "RFC 8452 AES-128-GCM-SIV seal including key setup and restore"838 ),839 (840 s128sp,841 Kind::Seal(Algorithm::Siv128, true),842 "aes128gcmsiv-seal-prepared",843 "AES-128-GCM-SIV seal retained key schedule plus restore"844 ),845 (846 s128of,847 Kind::Open(Algorithm::Siv128, false),848 "aes128gcmsiv-open-fresh",849 "AES-128-GCM-SIV open including key/restore; failed body wiped"850 ),851 (852 s128op,853 Kind::Open(Algorithm::Siv128, true),854 "aes128gcmsiv-open-prepared",855 "AES-128-GCM-SIV open retained key/restore; valid/invalid/mixed tags"856 ),857 (858 s256sf,859 Kind::Seal(Algorithm::Siv256, false),860 "aes256gcmsiv-seal-fresh",861 "RFC 8452 AES-256-GCM-SIV seal including key setup and restore"862 ),863 (864 s256sp,865 Kind::Seal(Algorithm::Siv256, true),866 "aes256gcmsiv-seal-prepared",867 "AES-256-GCM-SIV seal retained key schedule plus restore"868 ),869 (870 s256of,871 Kind::Open(Algorithm::Siv256, false),872 "aes256gcmsiv-open-fresh",873 "AES-256-GCM-SIV open including key/restore; failed body wiped"874 ),875 (876 s256op,877 Kind::Open(Algorithm::Siv256, true),878 "aes256gcmsiv-open-prepared",879 "AES-256-GCM-SIV open retained key/restore; valid/invalid/mixed tags"880 ),881 (882 assf,883 Kind::Seal(Algorithm::Ascon, false),884 "ascon-seal-fresh",885 "Final NIST Ascon-AEAD128 seal including key-word setup and restore"886 ),887 (888 assp,889 Kind::Seal(Algorithm::Ascon, true),890 "ascon-seal-prepared",891 "Final NIST Ascon-AEAD128 seal retained key plus restore"892 ),893 (894 asof,895 Kind::Open(Algorithm::Ascon, false),896 "ascon-open-fresh",897 "Final NIST Ascon-AEAD128 open including key/restore; failed body wiped"898 ),899 (900 asop,901 Kind::Open(Algorithm::Ascon, true),902 "ascon-open-prepared",903 "Final NIST Ascon-AEAD128 open retained key/restore; valid/invalid/mixed tags"904 ),905 (906 asus,907 Kind::Seal(Algorithm::AsconUnrolled, true),908 "ascon-seal-unrolled",909 "Same final Ascon-AEAD128 with explicit permutation unrolling; retained key/restore"910 ),911 (912 asuo,913 Kind::Open(Algorithm::AsconUnrolled, true),914 "ascon-open-unrolled",915 "Same final Ascon-AEAD128 unrolled open; retained key/restore, mixed tags"916 ),917);918#[cfg(test)]919mod tests {920 use super::*;921 #[test]922 fn all_cases_check_success_failure_and_reuse() {923 for case in cases() {924 for distinct in [false, true] {925 for pattern in [Pattern::Valid, Pattern::Invalid, Pattern::Mixed] {926 let prepared = (case.prepare)(&Params {927 message_len: 128,928 batch: 3,929 seed: 31,930 pattern,931 distinct,932 });933 if case.id.ends_with("-records") && distinct {934 assert!(prepared.is_err());935 continue;936 }937 let mut f = prepared.unwrap();938 f.run(0);939 f.check().unwrap();940 f.run(1);941 f.check().unwrap();942 }943 }944 }945 }946947 #[test]948 fn driver_counts_each_symmetric_cohort_once_through_batch500() {949 for count in [1, 8, 32, 128, 500] {950 for case in cases() {951 let mut fixture = (case.prepare)(&Params {952 message_len: 128,953 batch: count,954 seed: 41,955 pattern: Pattern::Mixed,956 distinct: false,957 })958 .unwrap();959 let group = fixture.operations_per_run();960 assert_eq!(group, count, "{} must consume the whole cohort", case.id);961 assert_eq!(count / group, 1, "{} must run once per cohort", case.id);962 for round in 0..2 {963 for i in 0..count / group {964 fixture.run((round + i) as u64);965 }966 fixture967 .check()968 .unwrap_or_else(|e| panic!("{} batch{count}: {e}", case.id));969 }970 }971 }972 }973974 #[test]975 fn record_nonce_counters_do_not_repeat_between_cohorts() {976 for count in [1, 8, 32, 128, 500] {977 let mut seen = std::collections::BTreeSet::new();978 for iteration in [0, 1, u64::MAX] {979 for index in 0..count {980 assert!(seen.insert(record_counter(iteration, count, index)));981 }982 }983 }984 }985}