@@ -6,7 +6,7 @@ use crate::{
66use aws_sdk_dynamodb:: { primitives:: Blob , types:: AttributeValue } ;
77use cipherstash_client:: {
88 credentials:: { service_credentials:: ServiceToken , Credentials } ,
9- encryption:: Encryption ,
9+ encryption:: { Encryption , Plaintext } ,
1010} ;
1111use std:: { borrow:: Cow , collections:: HashMap , ops:: Deref } ;
1212
@@ -66,21 +66,28 @@ impl SealedTableEntry {
6666 let mut decryptable_items = Vec :: with_capacity ( items. len ( ) * protected_attributes. len ( ) ) ;
6767
6868 for item in items. iter ( ) {
69- let ciphertexts = protected_attributes
70- . iter ( )
71- . map ( |name| {
72- let attribute = item. inner ( ) . attributes . get ( match name. deref ( ) {
73- "pk" => "__pk" ,
74- "sk" => "__sk" ,
75- _ => name,
76- } ) ;
77-
78- attribute
79- . ok_or_else ( || SealError :: MissingAttribute ( name. to_string ( ) ) ) ?
80- . as_encrypted_record ( )
81- . ok_or_else ( || SealError :: InvalidCiphertext ( name. to_string ( ) ) )
82- } )
83- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
69+ if !protected_attributes. is_empty ( ) {
70+ let ciphertexts = protected_attributes
71+ . iter ( )
72+ . map ( |name| {
73+ let attribute = item. inner ( ) . attributes . get ( match name. deref ( ) {
74+ "pk" => "__pk" ,
75+ "sk" => "__sk" ,
76+ _ => name,
77+ } ) ;
78+
79+ attribute
80+ . ok_or_else ( || SealError :: MissingAttribute ( name. to_string ( ) ) ) ?
81+ . as_encrypted_record ( )
82+ . ok_or_else ( || SealError :: InvalidCiphertext ( name. to_string ( ) ) )
83+ } )
84+ . collect :: < Result < Vec < _ > , _ > > ( ) ?;
85+
86+ // Create a list of all ciphertexts so that they can all be decrypted in one go.
87+ // The decrypted version of this list will be chunked up and zipped with the plaintext
88+ // fields once the decryption succeeds.
89+ decryptable_items. extend ( ciphertexts) ;
90+ }
8491
8592 let unprotected = plaintext_attributes
8693 . iter ( )
@@ -98,17 +105,18 @@ impl SealedTableEntry {
98105 . collect :: < Result < Vec < & TableAttribute > , SealError > > ( ) ?;
99106
100107 plaintext_items. push ( unprotected) ;
101-
102- // Create a list of all ciphertexts so that they can all be decrypted in one go.
103- // The decrypted version of this list will be chunked up and zipped with the plaintext
104- // fields once the decryption succeeds.
105- decryptable_items. extend ( ciphertexts) ;
106108 }
107109
108110 let decrypted = cipher. decrypt ( decryptable_items) . await ?;
109111
110- let unsealed = decrypted
111- . chunks_exact ( protected_attributes. len ( ) )
112+ let decrypted_iter: & mut dyn Iterator < Item = & [ Plaintext ] > =
113+ if protected_attributes. len ( ) > 0 {
114+ & mut decrypted. chunks_exact ( protected_attributes. len ( ) )
115+ } else {
116+ & mut std:: iter:: repeat_with :: < & [ Plaintext ] , _ > ( || & [ ] ) . take ( plaintext_items. len ( ) )
117+ } ;
118+
119+ let unsealed = decrypted_iter
112120 . zip ( plaintext_items)
113121 . map ( |( decrypted_plaintext, plaintext_items) | {
114122 let mut unsealed = Unsealed :: new ( ) ;
0 commit comments