@@ -98,3 +98,125 @@ pub struct CcmTagLen16;
9898impl CcmTagLen for CcmTagLen16 {
9999 const LEN : usize = 16 ;
100100}
101+
102+ pub mod test_helper {
103+ use super :: * ;
104+
105+ pub fn test_aes_ccm_roundtrip < C : Crypto , Tag : CcmTagLen > ( crypto : & mut C ) {
106+ let key: BytesCcmKeyLen = [
107+ 0x26 , 0x51 , 0x1f , 0xb5 , 0x1f , 0xcf , 0xa7 , 0x5c , 0xb4 , 0xb4 , 0x4d , 0xa7 , 0x5a , 0x6e ,
108+ 0x5a , 0x0e ,
109+ ]
110+ . into ( ) ;
111+
112+ let iv: BytesCcmIvLen = [
113+ 0x5a , 0x8a , 0xa4 , 0x85 , 0xc3 , 0x16 , 0xe9 , 0x40 , 0x3a , 0xff , 0x85 , 0x9f , 0xbb ,
114+ ]
115+ . into ( ) ;
116+
117+ let ad = [
118+ 0xa1 , 0x6a , 0x2e , 0x74 , 0x1f , 0x1c , 0xd9 , 0x71 , 0x72 , 0x85 , 0xb6 , 0xd8 , 0x82 , 0xc1 ,
119+ 0xfc , 0x53 , 0x65 , 0x5e , 0x97 , 0x73 , 0x76 , 0x1a , 0xd6 , 0x97 , 0xa7 , 0xee , 0x64 , 0x10 ,
120+ 0x18 , 0x4c , 0x79 , 0x82 ,
121+ ] ;
122+ let plaintext = [
123+ 0x87 , 0x39 , 0xb4 , 0xbe , 0xa1 , 0xa0 , 0x99 , 0xfe , 0x54 , 0x74 , 0x99 , 0xcb , 0xc6 , 0xd1 ,
124+ 0xb1 , 0x3d , 0x84 , 0x9b , 0x80 , 0x84 , 0xc9 , 0xb6 , 0xac , 0xc5 ,
125+ ] ;
126+
127+ let ciphertext = crypto. aes_ccm_encrypt :: < 64 , Tag > ( & key, & iv, & ad, & plaintext) ;
128+ assert_eq ! ( ciphertext. len( ) , plaintext. len( ) + Tag :: LEN ) ;
129+
130+ let decrypted = crypto
131+ . aes_ccm_decrypt :: < 64 , Tag > ( & key, & iv, & ad, ciphertext. as_slice ( ) )
132+ . expect ( "decryption should succeed" ) ;
133+
134+ assert_eq ! ( decrypted. as_slice( ) , & plaintext) ;
135+ }
136+
137+ pub fn test_aes_ccm_tag_8 < C : Crypto > ( crypto : & mut C ) {
138+ type Tag = CcmTagLen8 ;
139+ let key: BytesCcmKeyLen = [
140+ 0x36 , 0x8f , 0x35 , 0xa1 , 0xf8 , 0x0e , 0xaa , 0xac , 0xd6 , 0xbb , 0x13 , 0x66 , 0x09 , 0x38 ,
141+ 0x97 , 0x27 ,
142+ ]
143+ . into ( ) ;
144+
145+ let iv: BytesCcmIvLen = [
146+ 0x84 , 0x2a , 0x84 , 0x45 , 0x84 , 0x75 , 0x02 , 0xea , 0x77 , 0x36 , 0x3a , 0x16 , 0xb6 ,
147+ ]
148+ . into ( ) ;
149+
150+ let ad = [
151+ 0x34 , 0x39 , 0x6d , 0xfc , 0xfa , 0x6f , 0x74 , 0x2a , 0xea , 0x70 , 0x40 , 0x97 , 0x6b , 0xd5 ,
152+ 0x96 , 0x49 , 0x7a , 0x7a , 0x6f , 0xa4 , 0xfb , 0x85 , 0xee , 0x8e , 0x4c , 0xa3 , 0x94 , 0xd0 ,
153+ 0x20 , 0x95 , 0xb7 , 0xbf ,
154+ ] ;
155+ let plaintext = [
156+ 0x1c , 0xcc , 0xd5 , 0x58 , 0x25 , 0x31 , 0x6a , 0x94 , 0xc5 , 0x97 , 0x9e , 0x04 , 0x93 , 0x10 ,
157+ 0xd1 , 0xd7 , 0x17 , 0xcd , 0xfb , 0x76 , 0x24 , 0x28 , 0x9d , 0xac ,
158+ ] ;
159+
160+ let expected_ct: [ u8 ; 32 ] = [
161+ 0x1a , 0x58 , 0x09 , 0x4f , 0x0e , 0x8c , 0x60 , 0x35 , 0xa5 , 0x58 , 0x4b , 0xfa , 0x8d , 0x10 ,
162+ 0x09 , 0xc5 , 0xf7 , 0x8f , 0xd2 , 0xca , 0x48 , 0x7f , 0xf2 , 0x22 , 0xf6 , 0xd1 , 0xd8 , 0x97 ,
163+ 0xd6 , 0x05 , 0x16 , 0x18 ,
164+ ] ;
165+
166+ let ciphertext = crypto. aes_ccm_encrypt :: < 64 , Tag > ( & key, & iv, & ad, & plaintext) ;
167+ assert_eq ! ( ciphertext. len( ) , plaintext. len( ) + Tag :: LEN ) ;
168+ assert_eq ! (
169+ ciphertext,
170+ EdhocBuffer :: new_from_slice( & expected_ct) . expect( "expected_ct.length() <= 64" )
171+ ) ;
172+
173+ let decrypted = crypto
174+ . aes_ccm_decrypt :: < 64 , Tag > ( & key, & iv, & ad, ciphertext. as_slice ( ) )
175+ . expect ( "decryption should succeed" ) ;
176+
177+ assert_eq ! ( decrypted. as_slice( ) , & plaintext) ;
178+ }
179+
180+ pub fn test_aes_ccm_tag_16 < C : Crypto > ( crypto : & mut C ) {
181+ type Tag = CcmTagLen16 ;
182+ let key: BytesCcmKeyLen = [
183+ 0x41 , 0x89 , 0x35 , 0x1b , 0x5c , 0xae , 0xa3 , 0x75 , 0xa0 , 0x29 , 0x9e , 0x81 , 0xc6 , 0x21 ,
184+ 0xbf , 0x43 ,
185+ ]
186+ . into ( ) ;
187+
188+ let iv: BytesCcmIvLen = [
189+ 0x48 , 0xc0 , 0x90 , 0x69 , 0x30 , 0x56 , 0x1e , 0x0a , 0xb0 , 0xef , 0x4c , 0xd9 , 0x72 ,
190+ ]
191+ . into ( ) ;
192+
193+ let ad = [
194+ 0x40 , 0xa2 , 0x7c , 0x1d , 0x1e , 0x23 , 0xea , 0x3d , 0xbe , 0x80 , 0x56 , 0xb2 , 0x77 , 0x48 ,
195+ 0x61 , 0xa4 , 0xa2 , 0x01 , 0xcc , 0xe4 , 0x9f , 0x19 , 0x99 , 0x7d , 0x19 , 0x20 , 0x6d , 0x8c ,
196+ 0x8a , 0x34 , 0x39 , 0x51 ,
197+ ] ;
198+ let plaintext = [
199+ 0x45 , 0x35 , 0xd1 , 0x2b , 0x43 , 0x77 , 0x92 , 0x8a , 0x7c , 0x0a , 0x61 , 0xc9 , 0xf8 , 0x25 ,
200+ 0xa4 , 0x86 , 0x71 , 0xea , 0x05 , 0x91 , 0x07 , 0x48 , 0xc8 , 0xef ,
201+ ] ;
202+
203+ let expected_ct: [ u8 ; 40 ] = [
204+ 0x26 , 0xc5 , 0x69 , 0x61 , 0xc0 , 0x35 , 0xa7 , 0xe4 , 0x52 , 0xcc , 0xe6 , 0x1b , 0xc6 , 0xee ,
205+ 0x22 , 0x0d , 0x77 , 0xb3 , 0xf9 , 0x4d , 0x18 , 0xfd , 0x10 , 0xb6 , 0xd8 , 0x0e , 0x8b , 0xf8 ,
206+ 0x0f , 0x4a , 0x46 , 0xca , 0xb0 , 0x6d , 0x43 , 0x13 , 0xf0 , 0xdb , 0x9b , 0xe9 ,
207+ ] ;
208+
209+ let ciphertext = crypto. aes_ccm_encrypt :: < 64 , Tag > ( & key, & iv, & ad, & plaintext) ;
210+ assert_eq ! ( ciphertext. len( ) , plaintext. len( ) + Tag :: LEN ) ;
211+ assert_eq ! (
212+ ciphertext,
213+ EdhocBuffer :: new_from_slice( & expected_ct) . expect( "expected_ct.length() < 64" )
214+ ) ;
215+
216+ let decrypted = crypto
217+ . aes_ccm_decrypt :: < 64 , Tag > ( & key, & iv, & ad, ciphertext. as_slice ( ) )
218+ . expect ( "decryption should succeed" ) ;
219+
220+ assert_eq ! ( decrypted. as_slice( ) , & plaintext) ;
221+ }
222+ }
0 commit comments