11
11
//! [3]: https://en.wikipedia.org/wiki/Symmetric-key_algorithm
12
12
13
13
use crate :: { ParBlocks , ParBlocksSizeUser } ;
14
+ #[ cfg( all( feature = "block-padding" , feature = "alloc" ) ) ]
15
+ use alloc:: { vec, vec:: Vec } ;
14
16
#[ cfg( feature = "block-padding" ) ]
15
17
use inout:: {
16
18
block_padding:: { Padding , UnpadError } ,
@@ -173,6 +175,20 @@ pub trait BlockEncrypt: BlockSizeUser + Sized {
173
175
let buf = InOutBufReserved :: from_slices ( msg, out_buf) . map_err ( |_| PadError ) ?;
174
176
self . encrypt_padded_inout :: < P > ( buf)
175
177
}
178
+
179
+ /// Pad input and encrypt into a newly allocated Vec. Returns resulting ciphertext Vec.
180
+ #[ cfg( all( feature = "block-padding" , feature = "alloc" ) ) ]
181
+ #[ cfg_attr( docsrs, doc( cfg( all( feature = "block-padding" , feature = "alloc" ) ) ) ) ]
182
+ #[ inline]
183
+ fn encrypt_padded_vec < P : Padding < Self :: BlockSize > > ( & self , msg : & [ u8 ] ) -> Vec < u8 > {
184
+ let mut out = allocate_out_vec :: < Self > ( msg. len ( ) ) ;
185
+ let len = self
186
+ . encrypt_padded_b2b :: < P > ( msg, & mut out)
187
+ . expect ( "enough space for encrypting is allocated" )
188
+ . len ( ) ;
189
+ out. truncate ( len) ;
190
+ out
191
+ }
176
192
}
177
193
178
194
/// Decrypt-only functionality for block ciphers.
@@ -281,6 +297,24 @@ pub trait BlockDecrypt: BlockSizeUser {
281
297
let buf = InOutBuf :: new ( in_buf, & mut out_buf[ ..n] ) . map_err ( |_| UnpadError ) ?;
282
298
self . decrypt_padded_inout :: < P > ( buf)
283
299
}
300
+
301
+ /// Decrypt input and unpad it in a newly allocated Vec. Returns resulting
302
+ /// ciphertext Vec.
303
+ ///
304
+ /// Returns [`UnpadError`] if padding is malformed or if input length is
305
+ /// not multiple of `Self::BlockSize`.
306
+ #[ cfg( all( feature = "block-padding" , feature = "alloc" ) ) ]
307
+ #[ cfg_attr( docsrs, doc( cfg( all( feature = "block-padding" , feature = "alloc" ) ) ) ) ]
308
+ #[ inline]
309
+ fn decrypt_padded_vec < P : Padding < Self :: BlockSize > > (
310
+ & self ,
311
+ buf : & [ u8 ] ,
312
+ ) -> Result < Vec < u8 > , UnpadError > {
313
+ let mut out = vec ! [ 0 ; buf. len( ) ] ;
314
+ let len = self . decrypt_padded_b2b :: < P > ( buf, & mut out) ?. len ( ) ;
315
+ out. truncate ( len) ;
316
+ Ok ( out)
317
+ }
284
318
}
285
319
286
320
/// Encrypt-only functionality for block ciphers and modes with mutable access to `self`.
@@ -363,11 +397,11 @@ pub trait BlockEncryptMut: BlockSizeUser + Sized {
363
397
#[ cfg( feature = "block-padding" ) ]
364
398
#[ cfg_attr( docsrs, doc( cfg( feature = "block-padding" ) ) ) ]
365
399
#[ inline]
366
- fn encrypt_padded_mut < ' a , P : Padding < Self :: BlockSize > > (
400
+ fn encrypt_padded_mut < P : Padding < Self :: BlockSize > > (
367
401
self ,
368
- buf : & ' a mut [ u8 ] ,
402
+ buf : & mut [ u8 ] ,
369
403
msg_len : usize ,
370
- ) -> Result < & ' a [ u8 ] , PadError > {
404
+ ) -> Result < & [ u8 ] , PadError > {
371
405
let buf = InOutBufReserved :: from_mut_slice ( buf, msg_len) . map_err ( |_| PadError ) ?;
372
406
self . encrypt_padded_inout_mut :: < P > ( buf)
373
407
}
@@ -386,6 +420,20 @@ pub trait BlockEncryptMut: BlockSizeUser + Sized {
386
420
let buf = InOutBufReserved :: from_slices ( msg, out_buf) . map_err ( |_| PadError ) ?;
387
421
self . encrypt_padded_inout_mut :: < P > ( buf)
388
422
}
423
+
424
+ /// Pad input and encrypt into a newly allocated Vec. Returns resulting ciphertext Vec.
425
+ #[ cfg( all( feature = "block-padding" , feature = "alloc" ) ) ]
426
+ #[ cfg_attr( docsrs, doc( cfg( all( feature = "block-padding" , feature = "alloc" ) ) ) ) ]
427
+ #[ inline]
428
+ fn encrypt_padded_vec_mut < P : Padding < Self :: BlockSize > > ( self , msg : & [ u8 ] ) -> Vec < u8 > {
429
+ let mut out = allocate_out_vec :: < Self > ( msg. len ( ) ) ;
430
+ let len = self
431
+ . encrypt_padded_b2b_mut :: < P > ( msg, & mut out)
432
+ . expect ( "enough space for encrypting is allocated" )
433
+ . len ( ) ;
434
+ out. truncate ( len) ;
435
+ out
436
+ }
389
437
}
390
438
391
439
/// Decrypt-only functionality for block ciphers and modes with mutable access to `self`.
@@ -470,10 +518,10 @@ pub trait BlockDecryptMut: BlockSizeUser + Sized {
470
518
#[ cfg( feature = "block-padding" ) ]
471
519
#[ cfg_attr( docsrs, doc( cfg( feature = "block-padding" ) ) ) ]
472
520
#[ inline]
473
- fn decrypt_padded_mut < ' a , P : Padding < Self :: BlockSize > > (
521
+ fn decrypt_padded_mut < P : Padding < Self :: BlockSize > > (
474
522
self ,
475
- buf : & ' a mut [ u8 ] ,
476
- ) -> Result < & ' a [ u8 ] , UnpadError > {
523
+ buf : & mut [ u8 ] ,
524
+ ) -> Result < & [ u8 ] , UnpadError > {
477
525
self . decrypt_padded_inout_mut :: < P > ( buf. into ( ) )
478
526
}
479
527
@@ -498,6 +546,24 @@ pub trait BlockDecryptMut: BlockSizeUser + Sized {
498
546
let buf = InOutBuf :: new ( in_buf, & mut out_buf[ ..n] ) . map_err ( |_| UnpadError ) ?;
499
547
self . decrypt_padded_inout_mut :: < P > ( buf)
500
548
}
549
+
550
+ /// Decrypt input and unpad it in a newly allocated Vec. Returns resulting
551
+ /// ciphertext Vec.
552
+ ///
553
+ /// Returns [`UnpadError`] if padding is malformed or if input length is
554
+ /// not multiple of `Self::BlockSize`.
555
+ #[ cfg( all( feature = "block-padding" , feature = "alloc" ) ) ]
556
+ #[ cfg_attr( docsrs, doc( cfg( all( feature = "block-padding" , feature = "alloc" ) ) ) ) ]
557
+ #[ inline]
558
+ fn decrypt_padded_vec < P : Padding < Self :: BlockSize > > (
559
+ self ,
560
+ buf : & [ u8 ] ,
561
+ ) -> Result < Vec < u8 > , UnpadError > {
562
+ let mut out = vec ! [ 0 ; buf. len( ) ] ;
563
+ let len = self . decrypt_padded_b2b_mut :: < P > ( buf, & mut out) ?. len ( ) ;
564
+ out. truncate ( len) ;
565
+ Ok ( out)
566
+ }
501
567
}
502
568
503
569
impl < Alg : BlockEncrypt > BlockEncryptMut for Alg {
@@ -568,6 +634,12 @@ impl<'inp, 'out, BS: ArrayLength<u8>> BlockClosure for BlocksCtx<'inp, 'out, BS>
568
634
}
569
635
}
570
636
637
+ #[ cfg( all( feature = "block-padding" , feature = "alloc" ) ) ]
638
+ fn allocate_out_vec < BS : BlockSizeUser > ( len : usize ) -> Vec < u8 > {
639
+ let bs = BS :: BlockSize :: USIZE ;
640
+ vec ! [ 0 ; bs * ( len / bs + 1 ) ]
641
+ }
642
+
571
643
/// Implement simple block backend
572
644
#[ macro_export]
573
645
macro_rules! impl_simple_block_encdec {
0 commit comments