Skip to content

Commit 34abf4d

Browse files
committed
deoxys: migrate inout tests to integration tests
1 parent 0ac3e86 commit 34abf4d

File tree

2 files changed

+82
-111
lines changed

2 files changed

+82
-111
lines changed

deoxys/src/lib.rs

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -324,114 +324,3 @@ where
324324
B: DeoxysBcType,
325325
{
326326
}
327-
328-
#[cfg(test)]
329-
mod tests {
330-
//! this module is here to test the inout behavior which is not currently exposed.
331-
//! it will be once we port over to the API made in RustCrypto/traits#1793.
332-
//!
333-
//! This is to drop once https://github.com/RustCrypto/traits/pull/1797 is made available.
334-
//!
335-
//! It duplicates test vectors from `tests/deoxys_i_128.rs` and provides a mock buffer backing
336-
//! for InOut.
337-
338-
use hex_literal::hex;
339-
340-
use super::*;
341-
342-
struct MockBuffer {
343-
in_buf: [u8; 33],
344-
out_buf: [u8; 33],
345-
}
346-
347-
impl From<&[u8]> for MockBuffer {
348-
fn from(buf: &[u8]) -> Self {
349-
let mut in_buf = [0u8; 33];
350-
in_buf.copy_from_slice(buf);
351-
Self {
352-
in_buf,
353-
out_buf: [0u8; 33],
354-
}
355-
}
356-
}
357-
358-
impl MockBuffer {
359-
/// Get an [`InOutBuf`] from a [`MockBuffer`]
360-
pub fn to_in_out_buf(&mut self) -> InOutBuf<'_, '_, u8> {
361-
InOutBuf::new(self.in_buf.as_slice(), self.out_buf.as_mut_slice())
362-
.expect("Invariant violation")
363-
}
364-
}
365-
366-
impl AsRef<[u8]> for MockBuffer {
367-
fn as_ref(&self) -> &[u8] {
368-
&self.out_buf
369-
}
370-
}
371-
372-
#[test]
373-
fn test_deoxys_i_128_5() {
374-
let plaintext = hex!("5a4c652cb880808707230679224b11799b5883431292973215e9bd03cf3bc32fe4");
375-
let mut buffer = MockBuffer::from(&plaintext[..]);
376-
377-
let aad = [];
378-
379-
let key = hex!("101112131415161718191a1b1c1d1e1f");
380-
let key = Array(key);
381-
382-
let nonce = hex!("202122232425262728292a2b2c2d2e2f");
383-
let nonce = Array::try_from(&nonce[..8]).unwrap();
384-
385-
let ciphertext_expected =
386-
hex!("cded5a43d3c76e942277c2a1517530ad66037897c985305ede345903ed7585a626");
387-
388-
let tag_expected: [u8; 16] = hex!("cbf5faa6b8398c47f4278d2019161776");
389-
390-
type M = modes::DeoxysI<deoxys_bc::DeoxysBc256>;
391-
let cipher = DeoxysI128::new(&key);
392-
let tag: Tag = M::encrypt_inout(&nonce, &aad, buffer.to_in_out_buf(), &cipher.subkeys);
393-
394-
let ciphertext = buffer.as_ref();
395-
assert_eq!(ciphertext, ciphertext_expected);
396-
assert_eq!(tag, tag_expected);
397-
398-
let mut buffer = MockBuffer::from(buffer.as_ref());
399-
M::decrypt_inout(&nonce, &aad, buffer.to_in_out_buf(), &tag, &cipher.subkeys)
400-
.expect("decryption failed");
401-
402-
assert_eq!(&plaintext[..], buffer.as_ref());
403-
}
404-
405-
#[test]
406-
fn test_deoxys_ii_128_5() {
407-
let plaintext = hex!("06ac1756eccece62bd743fa80c299f7baa3872b556130f52265919494bdc136db3");
408-
let mut buffer = MockBuffer::from(&plaintext[..]);
409-
410-
let aad = [];
411-
412-
let key = hex!("101112131415161718191a1b1c1d1e1f");
413-
let key = Array(key);
414-
415-
let nonce = hex!("202122232425262728292a2b2c2d2e2f");
416-
let nonce = Array::try_from(&nonce[..15]).unwrap();
417-
418-
let ciphertext_expected =
419-
hex!("82bf241958b324ed053555d23315d3cc20935527fc970ff34a9f521a95e302136d");
420-
421-
let tag_expected: [u8; 16] = hex!("0eadc8612d5208c491e93005195e9769");
422-
423-
type M = modes::DeoxysII<deoxys_bc::DeoxysBc256>;
424-
let cipher = DeoxysII128::new(&key);
425-
let tag: Tag = M::encrypt_inout(&nonce, &aad, buffer.to_in_out_buf(), &cipher.subkeys);
426-
427-
let ciphertext = buffer.as_ref();
428-
assert_eq!(ciphertext, ciphertext_expected);
429-
assert_eq!(tag, tag_expected);
430-
431-
let mut buffer = MockBuffer::from(buffer.as_ref());
432-
M::decrypt_inout(&nonce, &aad, buffer.to_in_out_buf(), &tag, &cipher.subkeys)
433-
.expect("decryption failed");
434-
435-
assert_eq!(&plaintext[..], buffer.as_ref());
436-
}
437-
}

deoxys/tests/mock_buffer.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//! this module is here to test the inout behavior which is not currently exposed.
2+
//! it will be once we port over to the API made in RustCrypto/traits#1793.
3+
//!
4+
//! This is to drop once https://github.com/RustCrypto/traits/pull/1797 is made available.
5+
//!
6+
//! It duplicates test vectors from `tests/deoxys_i_128.rs` and provides a mock buffer backing
7+
//! for InOut.
8+
9+
use aead::{AeadInOut, array::Array, dev::MockBuffer};
10+
use hex_literal::hex;
11+
12+
use deoxys::*;
13+
14+
#[test]
15+
fn test_deoxys_i_128_5() {
16+
let plaintext = hex!("5a4c652cb880808707230679224b11799b5883431292973215e9bd03cf3bc32fe4");
17+
let mut buffer = MockBuffer::from(&plaintext[..]);
18+
19+
let aad = Vec::new();
20+
21+
let key = hex!("101112131415161718191a1b1c1d1e1f");
22+
let key = Array(key);
23+
24+
let nonce = hex!("202122232425262728292a2b2c2d2e2f");
25+
let nonce = Array::try_from(&nonce[..8]).unwrap();
26+
27+
let ciphertext_expected =
28+
hex!("cded5a43d3c76e942277c2a1517530ad66037897c985305ede345903ed7585a626");
29+
30+
let tag_expected: [u8; 16] = hex!("cbf5faa6b8398c47f4278d2019161776");
31+
32+
let cipher = DeoxysI128::new(&key);
33+
let tag: Tag = cipher
34+
.encrypt_inout_detached(&nonce, &aad, buffer.to_in_out_buf())
35+
.expect("encryption failed");
36+
37+
let ciphertext = buffer.as_ref();
38+
assert_eq!(ciphertext, ciphertext_expected);
39+
assert_eq!(tag, tag_expected);
40+
41+
let mut buffer = MockBuffer::from(buffer.as_ref());
42+
cipher
43+
.decrypt_inout_detached(&nonce, &aad, buffer.to_in_out_buf(), &tag)
44+
.expect("decryption failed");
45+
46+
assert_eq!(&plaintext[..], buffer.as_ref());
47+
}
48+
49+
#[test]
50+
fn test_deoxys_ii_128_5() {
51+
let plaintext = hex!("06ac1756eccece62bd743fa80c299f7baa3872b556130f52265919494bdc136db3");
52+
let mut buffer = MockBuffer::from(&plaintext[..]);
53+
54+
let aad = Vec::new();
55+
56+
let key = hex!("101112131415161718191a1b1c1d1e1f");
57+
let key = Array(key);
58+
59+
let nonce = hex!("202122232425262728292a2b2c2d2e2f");
60+
let nonce = Array::try_from(&nonce[..15]).unwrap();
61+
62+
let ciphertext_expected =
63+
hex!("82bf241958b324ed053555d23315d3cc20935527fc970ff34a9f521a95e302136d");
64+
65+
let tag_expected: [u8; 16] = hex!("0eadc8612d5208c491e93005195e9769");
66+
67+
let cipher = DeoxysII128::new(&key);
68+
let tag: Tag = cipher
69+
.encrypt_inout_detached(&nonce, &aad, buffer.to_in_out_buf())
70+
.expect("encryption failed");
71+
72+
let ciphertext = buffer.as_ref();
73+
assert_eq!(ciphertext, ciphertext_expected);
74+
assert_eq!(tag, tag_expected);
75+
76+
let mut buffer = MockBuffer::from(buffer.as_ref());
77+
cipher
78+
.decrypt_inout_detached(&nonce, &aad, buffer.to_in_out_buf(), &tag)
79+
.expect("decryption failed");
80+
81+
assert_eq!(&plaintext[..], buffer.as_ref());
82+
}

0 commit comments

Comments
 (0)