@@ -286,6 +286,19 @@ impl Cipher<Encryption, Authenticated, AdditionalData> {
286
286
self . change_state ( ) ,
287
287
) )
288
288
}
289
+
290
+ pub fn encrypt_auth_inplace (
291
+ mut self ,
292
+ ad : & [ u8 ] ,
293
+ data : & mut [ u8 ] ,
294
+ tag : & mut [ u8 ] ,
295
+ ) -> Result < ( usize , Cipher < Encryption , Authenticated , Finished > ) > {
296
+ Ok ( (
297
+ self . raw_cipher
298
+ . encrypt_auth_inplace ( ad, data, tag) ?,
299
+ self . change_state ( ) ,
300
+ ) )
301
+ }
289
302
}
290
303
291
304
impl Cipher < Decryption , Authenticated , AdditionalData > {
@@ -302,6 +315,19 @@ impl Cipher<Decryption, Authenticated, AdditionalData> {
302
315
self . change_state ( ) ,
303
316
) )
304
317
}
318
+
319
+ pub fn decrypt_auth_inplace (
320
+ mut self ,
321
+ ad : & [ u8 ] ,
322
+ data : & mut [ u8 ] ,
323
+ tag : & [ u8 ] ,
324
+ ) -> Result < ( usize , Cipher < Decryption , Authenticated , Finished > ) > {
325
+ Ok ( (
326
+ self . raw_cipher
327
+ . decrypt_auth_inplace ( ad, data, tag) ?,
328
+ self . change_state ( ) ,
329
+ ) )
330
+ }
305
331
}
306
332
307
333
impl < O : Operation , T : Type > Cipher < O , T , CipherData > {
@@ -401,6 +427,44 @@ fn ccm() {
401
427
assert_eq ! ( p, p_out) ;
402
428
}
403
429
430
+ #[ test]
431
+ fn ccm_inplace ( ) {
432
+ // Example vector C.1
433
+ let k = [
434
+ 0x40 , 0x41 , 0x42 , 0x43 , 0x44 , 0x45 , 0x46 , 0x47 , 0x48 , 0x49 , 0x4a , 0x4b , 0x4c , 0x4d , 0x4e ,
435
+ 0x4f ,
436
+ ] ;
437
+ let iv = [ 0x10 , 0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 ] ;
438
+ let ad = [ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ] ;
439
+ let mut c = [ 0x20 , 0x21 , 0x22 , 0x23 , 0x0 , 0x0 , 0x0 , 0x0 ] ;
440
+ let validate_cipher = [ 0x71 , 0x62 , 0x01 , 0x5b , 0x4d , 0xac , 0x25 , 0x5d ] ;
441
+ let validate_plain = [ 0x20 , 0x21 , 0x22 , 0x23 ] ;
442
+
443
+ let cipher = Cipher :: < _ , Authenticated , _ > :: new (
444
+ raw:: CipherId :: Aes ,
445
+ raw:: CipherMode :: CCM ,
446
+ ( k. len ( ) * 8 ) as _ ,
447
+ )
448
+ . unwrap ( ) ;
449
+ let cipher = cipher. set_key_iv ( & k, & iv) . unwrap ( ) ;
450
+ let ( data, tag) = c. split_at_mut ( 4 ) ;
451
+ cipher
452
+ . encrypt_auth_inplace ( & ad, data, tag)
453
+ . unwrap ( ) ;
454
+ assert_eq ! ( c, validate_cipher) ;
455
+
456
+ let cipher = Cipher :: < _ , Authenticated , _ > :: new (
457
+ raw:: CipherId :: Aes ,
458
+ raw:: CipherMode :: CCM ,
459
+ ( k. len ( ) * 8 ) as _ ,
460
+ )
461
+ . unwrap ( ) ;
462
+ let cipher = cipher. set_key_iv ( & k, & iv) . unwrap ( ) ;
463
+ let ( data, tag) = c. split_at_mut ( 4 ) ;
464
+ cipher. decrypt_auth_inplace ( & ad, data, tag) . unwrap ( ) ;
465
+ assert_eq ! ( validate_plain, data) ;
466
+ }
467
+
404
468
#[ test]
405
469
fn aes_kw ( ) {
406
470
let k = [ 0x75 , 0x75 , 0xda , 0x3a , 0x93 , 0x60 , 0x7c , 0xc2 , 0xbf , 0xd8 , 0xce , 0xc7 , 0xaa , 0xdf , 0xd9 , 0xa6 ] ;
0 commit comments