11//! Development-related functionality
22
3- #![ allow( clippy:: missing_errors_doc, reason = "dev module" ) ]
4- #![ allow( clippy:: missing_panics_doc, reason = "dev module" ) ]
5- #![ allow( clippy:: unwrap_in_result, reason = "dev module" ) ]
6-
7- use crate :: {
8- Aead , AeadInOut , Payload , Tag , TagPosition , array:: typenum:: Unsigned , inout:: InOutBuf ,
9- } ;
103pub use blobby;
4+
5+ use crate :: Aead ;
116use common:: KeyInit ;
127
138/// AEAD test vector
@@ -26,8 +21,11 @@ pub struct TestVector {
2621}
2722
2823/// Run AEAD test for the provided passing test vector
29- #[ allow( clippy:: cast_possible_truncation) ]
30- pub fn pass_test < C : AeadInOut + KeyInit > (
24+ ///
25+ /// # Errors
26+ /// - If the cipher has failed initialization with the provided key.
27+ /// - If the AEAD mode has failed to pass the test vector.
28+ pub fn pass_test < C : Aead + KeyInit > (
3129 & TestVector {
3230 key,
3331 nonce,
@@ -36,76 +34,31 @@ pub fn pass_test<C: AeadInOut + KeyInit>(
3634 ciphertext,
3735 } : & TestVector ,
3836) -> Result < ( ) , & ' static str > {
39- let nonce = nonce. try_into ( ) . expect ( "wrong nonce size" ) ;
40- let cipher = <C as KeyInit >:: new_from_slice ( key) . expect ( "failed to initialize the cipher" ) ;
37+ let cipher: C = KeyInit :: new_from_slice ( key) . map_err ( |_| "failed to initialize the cipher" ) ?;
4138
4239 let res = cipher
43- . encrypt (
44- nonce,
45- Payload {
46- aad,
47- msg : plaintext,
48- } ,
49- )
40+ . encrypt_into_vec ( nonce, aad, plaintext)
5041 . map_err ( |_| "encryption failure" ) ?;
5142 if res != ciphertext {
5243 return Err ( "encrypted data is different from target ciphertext" ) ;
5344 }
5445
5546 let res = cipher
56- . decrypt (
57- nonce,
58- Payload {
59- aad,
60- msg : ciphertext,
61- } ,
62- )
47+ . decrypt_into_vec ( nonce, aad, ciphertext)
6348 . map_err ( |_| "decryption failure" ) ?;
6449 if res != plaintext {
6550 return Err ( "decrypted data is different from target plaintext" ) ;
6651 }
6752
68- let ( ct, tag) = match C :: TAG_POSITION {
69- TagPosition :: Prefix => {
70- let ( tag, ct) = ciphertext. split_at ( C :: TagSize :: USIZE ) ;
71- ( ct, tag)
72- }
73- TagPosition :: Postfix => ciphertext. split_at ( plaintext. len ( ) ) ,
74- } ;
75- let tag: & Tag < C > = tag. try_into ( ) . expect ( "tag has correct length" ) ;
76-
77- // Fill output buffer with "garbage" to test that its data does not get read during encryption
78- let mut buf: alloc:: vec:: Vec < u8 > = ( 0 ..plaintext. len ( ) ) . map ( |i| i as u8 ) . collect ( ) ;
79- let inout_buf = InOutBuf :: new ( plaintext, & mut buf) . expect ( "pt and buf have the same length" ) ;
80-
81- let calc_tag = cipher
82- . encrypt_inout_detached ( nonce, aad, inout_buf)
83- . map_err ( |_| "encrypt_inout_detached: encryption failure" ) ?;
84- if tag != & calc_tag {
85- return Err ( "encrypt_inout_detached: tag mismatch" ) ;
86- }
87- if ct != buf {
88- return Err ( "encrypt_inout_detached: ciphertext mismatch" ) ;
89- }
90-
91- // Fill output buffer with "garbage"
92- buf. iter_mut ( )
93- . enumerate ( )
94- . for_each ( |( i, v) : ( usize , & mut u8 ) | * v = i as u8 ) ;
95-
96- let inout_buf = InOutBuf :: new ( ct, & mut buf) . expect ( "ct and buf have the same length" ) ;
97- cipher
98- . decrypt_inout_detached ( nonce, aad, inout_buf, tag)
99- . map_err ( |_| "decrypt_inout_detached: decryption failure" ) ?;
100- if plaintext != buf {
101- return Err ( "decrypt_inout_detached: plaintext mismatch" ) ;
102- }
103-
10453 Ok ( ( ) )
10554}
10655
10756/// Run AEAD test for the provided failing test vector
108- pub fn fail_test < C : AeadInOut + KeyInit > (
57+ ///
58+ /// # Errors
59+ /// - If the cipher has failed initialization with the provided key.
60+ /// - If the cipher has passed the test vector.
61+ pub fn fail_test < C : Aead + KeyInit > (
10962 & TestVector {
11063 key,
11164 nonce,
@@ -114,16 +67,9 @@ pub fn fail_test<C: AeadInOut + KeyInit>(
11467 ..
11568 } : & TestVector ,
11669) -> Result < ( ) , & ' static str > {
117- let nonce = nonce. try_into ( ) . expect ( "wrong nonce size" ) ;
118- let cipher = <C as KeyInit >:: new_from_slice ( key) . expect ( "failed to initialize the cipher" ) ;
70+ let cipher: C = KeyInit :: new_from_slice ( key) . map_err ( |_| "failed to initialize the cipher" ) ?;
11971
120- let res = cipher. decrypt (
121- nonce,
122- Payload {
123- aad,
124- msg : ciphertext,
125- } ,
126- ) ;
72+ let res = cipher. decrypt_into_vec ( nonce, aad, ciphertext) ;
12773 if res. is_ok ( ) {
12874 Err ( "decryption must return error" )
12975 } else {
@@ -134,6 +80,9 @@ pub fn fail_test<C: AeadInOut + KeyInit>(
13480/// Define AEAD test for passing test vectors
13581#[ macro_export]
13682macro_rules! new_pass_test {
83+ ( $name: ident, $cipher: ty $( , ) ?) => {
84+ $crate:: new_pass_test!( $name, stringify!( $name) , $cipher) ;
85+ } ;
13786 ( $name: ident, $test_name: expr, $cipher: ty $( , ) ?) => {
13887 #[ test]
13988 fn $name( ) {
@@ -165,6 +114,9 @@ macro_rules! new_pass_test {
165114/// Define AEAD test for failing test vectors
166115#[ macro_export]
167116macro_rules! new_fail_test {
117+ ( $name: ident, $cipher: ty $( , ) ?) => {
118+ $crate:: new_fail_test!( $name, stringify!( $name) , $cipher) ;
119+ } ;
168120 ( $name: ident, $test_name: expr, $cipher: ty $( , ) ?) => {
169121 #[ test]
170122 fn $name( ) {
0 commit comments