Skip to content

Commit e8e6677

Browse files
committed
improvements to KeyMaterial. Closes #38
1 parent d82041b commit e8e6677

1 file changed

Lines changed: 75 additions & 18 deletions

File tree

crypto/core/tests/key_material_tests.rs

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mod test_key_material {
6565
key.allow_hazardous_operations();
6666
key.set_bytes_as_type(&key_bytes, KeyType::BytesLowEntropy).unwrap();
6767
assert_eq!(key.key_type(), KeyType::BytesLowEntropy);
68-
68+
key.drop_hazardous_operations();
6969
// nothing else requires setting hazardous operations.
7070
}
7171

@@ -94,6 +94,7 @@ mod test_key_material {
9494
key.set_key_len(32).unwrap();
9595
assert_eq!(key.ref_to_bytes(), &[2u8; 32]);
9696
assert_eq!(key.key_len(), 32);
97+
key.drop_hazardous_operations();
9798
}
9899

99100
#[test]
@@ -173,15 +174,28 @@ mod test_key_material {
173174
#[test]
174175
fn zeroize() {
175176
let mut key = KeyMaterial256::from_bytes(&DUMMY_KEY[..32]).unwrap();
177+
let capacity = key.capacity();
178+
179+
// Sanity check: the backing buffer actually holds non-zero key material before it is wiped.
180+
// Without this, the post-zeroize assertion below could pass vacuously.
181+
key.allow_hazardous_operations();
182+
assert!(key.mut_ref_to_bytes().unwrap().iter().any(|&b| b != 0));
183+
key.drop_hazardous_operations();
184+
176185
key.zeroize();
177186
let key_len = key.key_len();
178187
assert_eq!(key_len, 0);
179188
assert_eq!(key.key_type(), KeyType::Zeroized);
180189

190+
// zeroize() must wipe the entire backing buffer.
191+
// Full capacity must be inspected to confirm the previously-set bytes were
192+
// actually overwritten with zeros.
193+
// Note: key_len is now 0, so ref_to_bytes() returns an empty slice.
181194
key.allow_hazardous_operations();
182-
let mut buf = vec![0u8; key_len];
183-
buf.copy_from_slice(key.ref_to_bytes());
184-
assert!(buf.iter().all(|&b| b == 0));
195+
let full_buf = key.mut_ref_to_bytes().unwrap();
196+
assert_eq!(full_buf.len(), capacity);
197+
assert!(full_buf.iter().all(|&b| b == 0));
198+
key.drop_hazardous_operations();
185199
}
186200

187201
#[test]
@@ -263,8 +277,8 @@ mod test_key_material {
263277
key.convert_key_type(KeyType::BytesFullEntropy).unwrap();
264278
assert_eq!(key.key_type(), KeyType::BytesFullEntropy);
265279
assert!(key.is_full_entropy());
266-
267280
key.drop_hazardous_operations();
281+
268282
match key.convert_key_type(KeyType::SymmetricCipherKey) {
269283
Ok(()) => { /* good */ }
270284
_ => panic!("Expected Ok(())"),
@@ -277,6 +291,7 @@ mod test_key_material {
277291
let mut key = KeyMaterial256::from_bytes(&DUMMY_KEY[..32]).unwrap();
278292
key.allow_hazardous_operations();
279293
key.convert_key_type(KeyType::BytesFullEntropy).unwrap();
294+
key.drop_hazardous_operations();
280295
match key.convert_key_type(KeyType::Seed) {
281296
Ok(()) => { /* good */ }
282297
_ => panic!("Expected Ok(())"),
@@ -406,18 +421,22 @@ mod test_key_material {
406421
key = KeyMaterial256::from_bytes(&DUMMY_KEY[..32]).unwrap();
407422
key.allow_hazardous_operations();
408423
key.convert_key_type(KeyType::BytesFullEntropy).unwrap();
424+
key.drop_hazardous_operations();
409425

410426
key = KeyMaterial256::from_bytes(&DUMMY_KEY[..32]).unwrap();
411427
key.allow_hazardous_operations();
412428
key.convert_key_type(KeyType::MACKey).unwrap();
429+
key.drop_hazardous_operations();
413430

414431
key = KeyMaterial256::from_bytes(&DUMMY_KEY[..32]).unwrap();
415432
key.allow_hazardous_operations();
416433
key.convert_key_type(KeyType::SymmetricCipherKey).unwrap();
434+
key.drop_hazardous_operations();
417435

418436
key = KeyMaterial256::from_bytes(&DUMMY_KEY[..32]).unwrap();
419437
key.allow_hazardous_operations();
420438
key.convert_key_type(KeyType::Seed).unwrap();
439+
key.drop_hazardous_operations();
421440
}
422441

423442
#[test]
@@ -493,6 +512,7 @@ mod test_key_material {
493512
// should work if you allow hazardous conversions.
494513
key.allow_hazardous_operations();
495514
key.convert_key_type(KeyType::SymmetricCipherKey).unwrap();
515+
key.drop_hazardous_operations();
496516
}
497517

498518
#[test]
@@ -570,6 +590,7 @@ mod test_key_material {
570590
// now it should work
571591
key.set_security_strength(SecurityStrength::_128bit).unwrap();
572592
assert_eq!(key.security_strength(), SecurityStrength::_128bit);
593+
key.drop_hazardous_operations();
573594

574595
// BytesLowEntropy keys cannot have a security strength other than None.
575596
// success
@@ -583,12 +604,14 @@ mod test_key_material {
583604
Err(KeyMaterialError::SecurityStrength(_)) => { /* good */ }
584605
_ => panic!("Expected KeyMaterialError::SecurityStrength"),
585606
}
607+
key.drop_hazardous_operations();
586608

587609
// Zeroized keys cannot have a security strength other than None.
588610
// success
589611
let mut key = KeyMaterial256::new();
590612
key.allow_hazardous_operations();
591613
key.set_key_len(32).unwrap(); // still zeroized
614+
key.drop_hazardous_operations();
592615
assert_eq!(key.key_type(), KeyType::Zeroized);
593616
// setting to ::None should work .. even without setting .allow_hazardous_operations()
594617
key.set_security_strength(SecurityStrength::None).unwrap();
@@ -598,6 +621,7 @@ mod test_key_material {
598621
Err(KeyMaterialError::SecurityStrength(_)) => { /* good */ }
599622
_ => panic!("Expected KeyMaterialError::SecurityStrength"),
600623
}
624+
key.drop_hazardous_operations();
601625
}
602626

603627
#[test]
@@ -678,22 +702,55 @@ mod test_key_material {
678702

679703
#[test]
680704
fn eq() {
681-
// On instances of the same exact type (size).
682-
let key1 = KeyMaterial256::from_bytes(
683-
b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
684-
)
685-
.unwrap();
686-
let key2 = KeyMaterial256::from_bytes(
687-
b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
688-
)
689-
.unwrap();
705+
// For context:
706+
// DUMMY_KEY: &[u8; 64] = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\
707+
// \x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\
708+
// \x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\
709+
// \x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F";
710+
711+
// Same bytes, full capacity. Should be equal.
712+
let key1 = KeyMaterial256::from_bytes(&DUMMY_KEY[..32]).unwrap();
713+
let key2 = KeyMaterial256::from_bytes(&DUMMY_KEY[..32]).unwrap();
690714
assert_eq!(key1, key2);
691715

692-
let key3 = KeyMaterial256::from_bytes(
693-
b"\x0F\x0E\x0D\x0C\x0B\x0A\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00",
694-
)
695-
.unwrap();
716+
// Same length, different content. Should NOT be equal.
717+
let key3 = KeyMaterial256::from_bytes(&[0xFFu8; 32]).unwrap();
696718
assert_ne!(key1, key3);
719+
720+
// Different length, overlapping prefix. Should NOT be equal.
721+
let key_short = KeyMaterial256::from_bytes(&DUMMY_KEY[..16]).unwrap();
722+
assert_ne!(key1, key_short);
723+
724+
// PartialEq ignores key_type: same bytes, different KeyType. Should be equal.
725+
let key_low =
726+
KeyMaterial256::from_bytes_as_type(&DUMMY_KEY[..32], KeyType::BytesLowEntropy).unwrap();
727+
let key_mac =
728+
KeyMaterial256::from_bytes_as_type(&DUMMY_KEY[..32], KeyType::MACKey).unwrap();
729+
assert_eq!(key_low, key_mac);
730+
731+
// PartialEq ignores security_strength: same bytes, different strength. Should be equal.
732+
let key_strong =
733+
KeyMaterial256::from_bytes_as_type(&DUMMY_KEY[..32], KeyType::BytesFullEntropy)
734+
.unwrap();
735+
let mut key_weak =
736+
KeyMaterial256::from_bytes_as_type(&DUMMY_KEY[..32], KeyType::BytesFullEntropy)
737+
.unwrap();
738+
key_weak.set_security_strength(SecurityStrength::_128bit).unwrap();
739+
assert_ne!(key_strong.security_strength(), key_weak.security_strength()); // strengths differ
740+
assert_eq!(key_strong, key_weak); // but keys are still equal
741+
742+
// Partially-filled buffers with identical content. Should be equal.
743+
let key_half1 = KeyMaterial256::from_bytes(&DUMMY_KEY[..16]).unwrap();
744+
let key_half2 = KeyMaterial256::from_bytes(&DUMMY_KEY[..16]).unwrap();
745+
assert_eq!(key_half1, key_half2);
746+
747+
// Verify with a second size (KeyMaterial512) to cover the generic impl.
748+
let key512_a = KeyMaterial512::from_bytes(&DUMMY_KEY[..64]).unwrap();
749+
let key512_b = KeyMaterial512::from_bytes(&DUMMY_KEY[..64]).unwrap();
750+
assert_eq!(key512_a, key512_b);
751+
752+
let key512_c = KeyMaterial512::from_bytes(&[0xFFu8; 64]).unwrap();
753+
assert_ne!(key512_a, key512_c);
697754
}
698755

699756
#[test]

0 commit comments

Comments
 (0)