@@ -15,19 +15,16 @@ use crate::types::EncryptionContext;
1515use crate :: types:: { SafeRead , SafeWrite } ;
1616
1717use aws_mpl_primitives:: ecdsa_verify_context;
18- use aws_mpl_primitives:: {
19- EcdsaSignatureAlgorithm , aes_encrypt, ecdsa_verify, generate_random_bytes,
20- } ;
18+ use aws_mpl_primitives:: { EcdsaSignatureAlgorithm , aes_encrypt, generate_random_bytes} ;
2119use aws_mpl_rs:: types:: AlgorithmSuiteInfo ;
2220use aws_mpl_rs:: types:: cryptographic_materials_manager:: CryptographicMaterialsManagerRef ;
2321
2422//= compliance/client-apis/encrypt.txt#2.4.6
2523//= type=implication
2624//# This
2725//# value MUST default to 4096 bytes.
28- pub ( crate ) const DEFAULT_FRAME_LENGTH : usize = 4096 ;
26+ pub ( crate ) const DEFAULT_FRAME_LENGTH : u32 = 4096 ;
2927
30- // UTF-8 encoded "aws-crypto-"
3128const RESERVED_ENCRYPTION_CONTEXT : & str = "aws-crypto-" ;
3229
3330pub ( crate ) fn encrypt_and_serialize (
@@ -38,33 +35,34 @@ pub(crate) fn encrypt_and_serialize(
3835 dw : & mut DigestWriter ,
3936) -> Result < ( ) , Error > {
4037 let frame_length = header. body . frame_length ( ) as usize ;
41- // let frames = plaintext.len().div_ceil(frame_length);
4238 let iv_len = get_iv_length ( & header. suite ) as usize ;
4339 let auth_len = get_tag_length ( & header. suite ) as usize ;
4440 let frame_len = frame_length + iv_len + auth_len + 4 ;
45- // let total_size = frames * frame_len + header.raw_header.len() + iv_len + auth_len + 128;
4641 let mut w = Vec :: with_capacity ( frame_len) ;
4742 write_bytes ( & mut w, & header. raw_header ) ?;
4843 write_header_auth_tag ( & mut w, & header. header_auth , & header. suite ) ?;
4944 write_bytes ( out, & w) ?;
5045 write_bytes ( dw, & w) ?;
5146
52- // let mut n: usize = 0;
5347 let mut sequence_number = START_SEQUENCE_NUMBER ;
5448 let alg = get_aes_alg ( & header. suite ) ;
5549
5650 let mut iv = vec ! [ 0 ; iv_len] ;
5751 let mut plaintext_frame = vec ! [ 0 ; frame_length] ;
5852 let mut aad = Vec :: new ( ) ;
5953 let mut in_size: usize ;
60- // let mut foo = 0u8;
54+ let mut next_char: Option < u8 > = None ;
55+
6156 loop {
6257 w. clear ( ) ;
63- // in_size = read_up_to(plaintext, &mut [foo])?;
64- in_size = read_up_to ( plaintext, & mut plaintext_frame) ?;
58+ in_size = read_up_to_peek ( plaintext, & mut plaintext_frame, next_char) ?;
6559 if in_size != frame_length {
6660 break ;
6761 }
62+ next_char = read_opt_u8 ( plaintext) ?;
63+ if next_char. is_none ( ) {
64+ break ;
65+ }
6866 if sequence_number == ENDFRAME_SEQUENCE_NUMBER {
6967 return Err ( "too many frames" . into ( ) ) ;
7068 }
@@ -133,59 +131,7 @@ pub(crate) const fn ecdsa_alg(
133131 }
134132}
135133
136- // consume and verify signature
137- #[ allow( dead_code) ]
138134pub ( crate ) fn verify_signature (
139- r : & mut dyn SafeRead ,
140- msg : & [ u8 ] ,
141- dec_mat : aws_mpl_rs:: types:: DecryptionMaterials ,
142- raw : & mut dyn SafeWrite ,
143- ) -> Result < ( ) , Error > {
144- //= compliance/client-apis/decrypt.txt#2.7
145- //= type=implication
146- //# Otherwise this operation MUST NOT perform this
147- //# step.
148- if dec_mat. verification_key . is_none ( ) {
149- return Ok ( ( ) ) ;
150- }
151-
152- //= compliance/client-apis/decrypt.txt#2.7.5
153- //# If the algorithm suite has a signature algorithm, this operation MUST
154- //# verify the message footer using the specified signature algorithm.
155-
156- //= compliance/client-apis/decrypt.txt#2.7
157- //# ./framework/algorithm-
158- //# suites.md#signature-algorithm), this operation MUST perform
159- //# this step.
160-
161- //= compliance/client-apis/decrypt.txt#2.7.5
162- //# After deserializing the body, this operation MUST deserialize the
163- //# next encrypted message bytes as the message footer (../data-format/
164- //# message-footer.md).
165-
166- let signature = read_seq_u16 ( r, raw) ?;
167- let ecdsa_params = get_ecdsa_alg ( & dec_mat. algorithm_suite . unwrap ( ) . signature . unwrap ( ) ) ?;
168- let data_to_sign = & msg[ 0 ..msg. len ( ) - signature. len ( ) - 2 ] ;
169- //= compliance/client-apis/decrypt.txt#2.7.5
170- //# Once the message footer is deserialized, this operation MUST use the
171- //# signature algorithm (../framework/algorithm-suites.md#signature-
172- //# algorithm) from the algorithm suite (../framework/algorithm-
173- //# suites.md) in the decryption materials to verify the encrypted
174- //# message, with the following inputs:
175- let valid = ecdsa_verify (
176- ecdsa_alg ( ecdsa_params) ,
177- dec_mat. verification_key . unwrap ( ) . as_ref ( ) ,
178- data_to_sign,
179- & signature,
180- ) ?;
181-
182- if !valid {
183- return Err ( "InvalidSignature" . into ( ) ) ;
184- }
185- Ok ( ( ) )
186- }
187-
188- pub ( crate ) fn verify_signature2 (
189135 r : & mut dyn SafeRead ,
190136 context : aws_mpl_primitives:: DigestContext ,
191137 dec_mat : aws_mpl_rs:: types:: DecryptionMaterials ,
@@ -215,7 +161,6 @@ pub(crate) fn verify_signature2(
215161
216162 let signature = read_seq_u16 ( r, raw) ?;
217163 let ecdsa_params = get_ecdsa_alg ( & dec_mat. algorithm_suite . unwrap ( ) . signature . unwrap ( ) ) ?;
218- // let data_to_sign = &msg[0..msg.len() - signature.len() - 2];
219164 //= compliance/client-apis/decrypt.txt#2.7.5
220165 //# Once the message footer is deserialized, this operation MUST use the
221166 //# signature algorithm (../framework/algorithm-suites.md#signature-
@@ -308,14 +253,6 @@ pub(crate) fn build_header_for_encrypt(
308253 frame_length : u32 ,
309254 derived_data_keys : & key_derivation:: ExpandedKeyMaterial ,
310255) -> Result < HeaderInfo , Error > {
311- // requires !suite.commitment.IDENTITY?
312- // requires SerializableTypes.IsESDKEncryptionContext(encryptionContext)
313- // requires suite.commitment.HKDF? ==>
314- // && derivedDataKeys.commitmentKey.Some?
315- // && |derivedDataKeys.commitmentKey.value| == suite.commitment.HKDF.outputKeyLength as int
316-
317- // requires frameLength > 0
318-
319256 //= aws-encryption-sdk-specification/client-apis/encrypt.md#construct-the-header
320257 //# - [AAD](../data-format/message-header.md#aad): MUST be the serialization of the [encryption context](../framework/structures.md#encryption-context)
321258 //# in the [encryption materials](../framework/structures.md#encryption-materials),
@@ -607,8 +544,6 @@ pub(crate) fn validate_suite_data(
607544 header : & HeaderBody ,
608545 expected_suite_data : & [ u8 ] ,
609546) -> Result < ( ) , Error > {
610- // requires suite.commitment.HKDF?
611-
612547 //= compliance/client-apis/decrypt.txt#2.7.2
613548 //# The derived commit key MUST equal the commit key stored in the message
614549 //# header.
@@ -632,23 +567,6 @@ pub(crate) fn validate_suite_data(
632567 Ok ( ( ) )
633568}
634569
635- // pub(crate) fn read_and_decrypt_framed_message_body(
636- // r: &mut dyn SafeRead,
637- // header: &header::HeaderInfo,
638- // key: &[u8],
639- // raw: &mut dyn SafeWrite,
640- // ) -> Result<Vec<u8>, Error> {
641- // //= compliance/client-apis/decrypt.txt#2.7.3
642- // //# The message header MUST be read and parsed as follows.
643- // let message_body = read_framed_message_body(r, header, raw)?;
644-
645- // //= compliance/client-apis/decrypt.txt#2.7.3
646- // //# The message body MUST be read and decrypted as follows.
647- // let plaintext = decrypt_framed_message_body(&message_body, key)?;
648-
649- // Ok(plaintext)
650- // }
651-
652570pub ( crate ) fn read_and_decrypt_non_framed_message_body (
653571 r : & mut dyn SafeRead ,
654572 header : & HeaderInfo ,
0 commit comments