Skip to content

Commit 3396c54

Browse files
committed
Adds a tests against a inoutbuf with two backing buffers
1 parent 48707a2 commit 3396c54

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

deoxys/src/lib.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,115 @@ where
324324
B: DeoxysBcType,
325325
{
326326
}
327+
328+
#[cfg(all(feature = "alloc", 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+
extern crate alloc;
339+
340+
use alloc::{vec, vec::Vec};
341+
use hex_literal::hex;
342+
343+
use super::*;
344+
345+
struct MockBuffer {
346+
in_buf: Vec<u8>,
347+
out_buf: Vec<u8>,
348+
}
349+
350+
impl From<&[u8]> for MockBuffer {
351+
fn from(buf: &[u8]) -> Self {
352+
Self {
353+
in_buf: buf.to_vec(),
354+
out_buf: vec![0u8; buf.len()],
355+
}
356+
}
357+
}
358+
359+
impl MockBuffer {
360+
/// Get an [`InOutBuf`] from a [`MockBuffer`]
361+
pub fn to_in_out_buf(&mut self) -> InOutBuf<'_, '_, u8> {
362+
InOutBuf::new(self.in_buf.as_slice(), self.out_buf.as_mut_slice())
363+
.expect("Invariant violation")
364+
}
365+
}
366+
367+
impl AsRef<[u8]> for MockBuffer {
368+
fn as_ref(&self) -> &[u8] {
369+
&self.out_buf
370+
}
371+
}
372+
373+
#[test]
374+
fn test_deoxys_i_128_5() {
375+
let plaintext = hex!("5a4c652cb880808707230679224b11799b5883431292973215e9bd03cf3bc32fe4");
376+
let mut buffer = MockBuffer::from(&plaintext[..]);
377+
378+
let aad = Vec::new();
379+
380+
let key = hex!("101112131415161718191a1b1c1d1e1f");
381+
let key = Array(key);
382+
383+
let nonce = hex!("202122232425262728292a2b2c2d2e2f");
384+
let nonce = Array::try_from(&nonce[..8]).unwrap();
385+
386+
let ciphertext_expected =
387+
hex!("cded5a43d3c76e942277c2a1517530ad66037897c985305ede345903ed7585a626");
388+
389+
let tag_expected: [u8; 16] = hex!("cbf5faa6b8398c47f4278d2019161776");
390+
391+
type M = modes::DeoxysI<deoxys_bc::DeoxysBc256>;
392+
let cipher = DeoxysI128::new(&key);
393+
let tag: Tag = M::encrypt_inout(&nonce, &aad, buffer.to_in_out_buf(), &cipher.subkeys);
394+
395+
let ciphertext = buffer.as_ref();
396+
assert_eq!(ciphertext, ciphertext_expected);
397+
assert_eq!(tag, tag_expected);
398+
399+
let mut buffer = MockBuffer::from(buffer.as_ref());
400+
M::decrypt_inout(&nonce, &aad, buffer.to_in_out_buf(), &tag, &cipher.subkeys)
401+
.expect("decryption failed");
402+
403+
assert_eq!(&plaintext[..], buffer.as_ref());
404+
}
405+
406+
#[test]
407+
fn test_deoxys_ii_128_5() {
408+
let plaintext = hex!("06ac1756eccece62bd743fa80c299f7baa3872b556130f52265919494bdc136db3");
409+
let mut buffer = MockBuffer::from(&plaintext[..]);
410+
411+
let aad = Vec::new();
412+
413+
let key = hex!("101112131415161718191a1b1c1d1e1f");
414+
let key = Array(key);
415+
416+
let nonce = hex!("202122232425262728292a2b2c2d2e2f");
417+
let nonce = Array::try_from(&nonce[..15]).unwrap();
418+
419+
let ciphertext_expected =
420+
hex!("82bf241958b324ed053555d23315d3cc20935527fc970ff34a9f521a95e302136d");
421+
422+
let tag_expected: [u8; 16] = hex!("0eadc8612d5208c491e93005195e9769");
423+
424+
type M = modes::DeoxysII<deoxys_bc::DeoxysBc256>;
425+
let cipher = DeoxysII128::new(&key);
426+
let tag: Tag = M::encrypt_inout(&nonce, &aad, buffer.to_in_out_buf(), &cipher.subkeys);
427+
428+
let ciphertext = buffer.as_ref();
429+
assert_eq!(ciphertext, ciphertext_expected);
430+
assert_eq!(tag, tag_expected);
431+
432+
let mut buffer = MockBuffer::from(buffer.as_ref());
433+
M::decrypt_inout(&nonce, &aad, buffer.to_in_out_buf(), &tag, &cipher.subkeys)
434+
.expect("decryption failed");
435+
436+
assert_eq!(&plaintext[..], buffer.as_ref());
437+
}
438+
}

0 commit comments

Comments
 (0)