mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
craft-pkcs7, android-res, jbig2: fewer slow passes
This commit is contained in:
1 parent
0f9802774b
commit
e448589df8
5 files changed
+31
-23
No files matched your search
@@ -13,3 +13,6 @@ workspace = true
|
||||
[dependencies]
|
||||
serde = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
craft-testkit = { workspace = true }
|
||||
@@ -568,9 +568,13 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn shared_offsets_spend_a_pool_byte_budget() {
|
||||
for utf8 in [true, false] {
|
||||
// Each parse keeps 64 MiB of strings: the gate runs the UTF-8 pool with the appended bytes
|
||||
// (the budget and the appended bytes together), nightly all four.
|
||||
let full = craft_testkit::hostile::divisor() == 1;
|
||||
let (encodings, appends): (&[bool], &[bool]) = if full { (&[true, false], &[false, true]) } else { (&[true], &[true]) };
|
||||
for &utf8 in encodings {
|
||||
let mut data = shared_ascii_pool(utf8, 8192);
|
||||
for appended in [false, true] {
|
||||
for &appended in appends {
|
||||
if appended {
|
||||
// Unrelated bytes after the chunk cannot buy more retained copies.
|
||||
data.resize(24 << 20, 0);
|
||||
|
||||
@@ -162,11 +162,16 @@ fn signatures_round_trip_for_every_key_type() {
|
||||
}
|
||||
}
|
||||
|
||||
/// New digital ids are self-signed, survive a `.p12` round trip, and are written with the stronger
|
||||
/// round count for the key and the MAC. Key derivation takes the time here, so one file per key
|
||||
/// type is written and opened once: the wrong password only for the P-256 file (it stops at the
|
||||
/// MAC, which is the same code for both), and the round count read off the P-256 file.
|
||||
#[test]
|
||||
fn new_digital_ids_are_self_signed_and_survive_a_p12_round_trip() {
|
||||
let name = Name::build("Grace Hopper", "Compilers", "Navy", "grace@example.com", "us");
|
||||
let now = Time { year: 2026, month: 10, day: 2, hour: 12, minute: 0, second: 0 };
|
||||
for key in [craft_pkcs7::PrivateKey::generate_rsa(2048).unwrap(), craft_pkcs7::PrivateKey::generate_p256().unwrap()] {
|
||||
let is_p256 = matches!(key.public_key(), PublicKey::P256(_));
|
||||
let cert = Certificate::self_signed(&name, &key, now, 5, &[0x42, 0x01]).unwrap();
|
||||
assert!(cert.is_self_signed());
|
||||
assert_eq!(cert.subject.display(), "C=US, O=Navy, OU=Compilers, CN=Grace Hopper, E=grace@example.com");
|
||||
@@ -177,7 +182,18 @@ fn new_digital_ids_are_self_signed_and_survive_a_p12_round_trip() {
|
||||
let back = pkcs12::open(&p12, "s3cret").unwrap();
|
||||
assert_eq!(back.certificate, id.certificate);
|
||||
assert_eq!(back.friendly_name.as_deref(), Some("Grace Hopper"));
|
||||
assert!(matches!(pkcs12::open(&p12, "wrong"), Err(Error::WrongPassword)));
|
||||
if is_p256 {
|
||||
assert!(matches!(pkcs12::open(&p12, "wrong"), Err(Error::WrongPassword)));
|
||||
// OWASP's figure for PBKDF2-HMAC-SHA-256, spelled out so lowering the constant fails here.
|
||||
const ROUNDS: u64 = 600_000;
|
||||
// MacData's iteration count, the last field of the file.
|
||||
let pfx = Tlv::parse_all(&p12).unwrap().children().unwrap();
|
||||
let mac = pfx[2].children().unwrap();
|
||||
assert_eq!(mac[2].u64().unwrap(), ROUNDS);
|
||||
// The same INTEGER three times: the MAC and the PBKDF2 parameters of both encrypted parts.
|
||||
let rounds = craft_pkcs7::der::int(ROUNDS);
|
||||
assert_eq!(p12.windows(rounds.len()).filter(|w| *w == rounds.as_slice()).count(), 3);
|
||||
}
|
||||
let d = DigestAlg::Sha256.digest(&[b"x"]);
|
||||
let sig = cms::sign_detached(&back.key, &back.certificate, &[], DigestAlg::Sha256, &d).unwrap();
|
||||
let sd = cms::SignedData::parse(&sig).unwrap();
|
||||
@@ -185,26 +201,6 @@ fn new_digital_ids_are_self_signed_and_survive_a_p12_round_trip() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_digital_ids_use_the_stronger_round_count_for_the_key_and_the_mac() {
|
||||
let name = Name::build("Grace Hopper", "", "", "", "");
|
||||
let now = Time { year: 2026, month: 10, day: 14, hour: 12, minute: 0, second: 0 };
|
||||
let key = craft_pkcs7::PrivateKey::generate_p256().unwrap();
|
||||
let certificate = Certificate::self_signed(&name, &key, now, 5, &[0x42]).unwrap();
|
||||
let id = pkcs12::DigitalId { key, certificate, chain: Vec::new(), friendly_name: None };
|
||||
let p12 = pkcs12::write(&id, "s3cret12").unwrap();
|
||||
// OWASP's figure for PBKDF2-HMAC-SHA-256, spelled out so lowering the constant fails here.
|
||||
const ROUNDS: u64 = 600_000;
|
||||
// MacData's iteration count, the last field of the file.
|
||||
let pfx = Tlv::parse_all(&p12).unwrap().children().unwrap();
|
||||
let mac = pfx[2].children().unwrap();
|
||||
assert_eq!(mac[2].u64().unwrap(), ROUNDS);
|
||||
// The same INTEGER three times: the MAC and the PBKDF2 parameters of both encrypted parts.
|
||||
let rounds = craft_pkcs7::der::int(ROUNDS);
|
||||
assert_eq!(p12.windows(rounds.len()).filter(|w| *w == rounds.as_slice()).count(), 3);
|
||||
assert_eq!(pkcs12::open(&p12, "s3cret12").unwrap().certificate, id.certificate);
|
||||
}
|
||||
|
||||
/// A `.p12` whose MAC asks for `rounds` iterations. Its content is empty: opening it must stop
|
||||
/// at the MAC, before the content is read.
|
||||
fn p12_with_mac_rounds(rounds: u64) -> Vec<u8> {
|
||||
|
||||
@@ -21,5 +21,6 @@ hayro-jbig2 = { path = "../../vendor/hayro-jbig2", version = "=0.3.0", default-f
|
||||
thiserror = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
craft-testkit = { workspace = true }
|
||||
proptest = { workspace = true }
|
||||
weezl = "0.2"
|
||||
@@ -942,6 +942,10 @@ mod tests {
|
||||
let past = page(&data, columns).unwrap();
|
||||
assert!(plan(&past, columns, rows, 1, None, None).is_none() && plan(&past, columns, rows, 2, None, None).is_none());
|
||||
assert_eq!(encode_jbig2(&data, columns, rows).unwrap(), encode_jbig2_generic(&data, columns, rows).unwrap());
|
||||
// Each picture below is another 65,536 marks coded and decoded: nightly only.
|
||||
if craft_testkit::hostile::divisor() > 1 {
|
||||
return;
|
||||
}
|
||||
// With two of them repeated, the second plan codes those two.
|
||||
let (data, columns, rows) = codes(MAX_CODED_SYMBOLS + 1, 200);
|
||||
let past = page(&data, columns).unwrap();
|
||||
|
||||
Reference in new issue
Block a user