@@ -102,9 +102,10 @@ void aws_cryptosdk_hdr_clean_up(struct aws_cryptosdk_hdr *hdr) {
102102 // Idempotent cleanup
103103 return ;
104104 }
105-
106- aws_byte_buf_clean_up (& hdr -> iv );
107- aws_byte_buf_clean_up (& hdr -> auth_tag );
105+ if (hdr -> iv .allocator )
106+ aws_byte_buf_clean_up (& hdr -> iv );
107+ if (hdr -> auth_tag .allocator )
108+ aws_byte_buf_clean_up (& hdr -> auth_tag );
108109
109110 aws_cryptosdk_edk_list_clean_up (& hdr -> edk_list );
110111 aws_cryptosdk_enc_ctx_clean_up (& hdr -> enc_ctx );
@@ -288,7 +289,12 @@ int aws_cryptosdk_hdr_size(const struct aws_cryptosdk_hdr *hdr) {
288289
289290 return bytes == SIZE_MAX ? 0 : bytes ;
290291}
291-
292+ static void init_aws_byte_buf_raw (struct aws_byte_buf * buf ){
293+ buf -> allocator = NULL ;
294+ buf -> buffer = NULL ;
295+ buf -> len = 0 ;
296+ buf -> capacity = 0 ;
297+ }
292298int aws_cryptosdk_hdr_write (
293299 const struct aws_cryptosdk_hdr * hdr , size_t * bytes_written , uint8_t * outbuf , size_t outlen ) {
294300 struct aws_byte_buf output = aws_byte_buf_from_array (outbuf , outlen );
@@ -302,6 +308,8 @@ int aws_cryptosdk_hdr_write(
302308 // TODO - unify everything on byte_bufs when the aws-c-common refactor lands
303309 // See: https://github.com/awslabs/aws-c-common/pull/130
304310 struct aws_byte_buf aad_length_field ;
311+ init_aws_byte_buf_raw (& aad_length_field );
312+
305313 if (!aws_byte_buf_advance (& output , & aad_length_field , 2 )) goto WRITE_ERR ;
306314
307315 size_t old_len = output .len ;
@@ -321,13 +329,13 @@ int aws_cryptosdk_hdr_write(
321329 const struct aws_cryptosdk_edk * edk = vp_edk ;
322330
323331 if (!aws_byte_buf_write_be16 (& output , (uint16_t )edk -> provider_id .len )) goto WRITE_ERR ;
324- if (!aws_byte_buf_write_from_whole_buffer (& output , edk -> provider_id )) goto WRITE_ERR ;
332+ if (!aws_byte_buf_write_from_whole_cursor (& output , aws_byte_cursor_from_array ( edk -> provider_id . buffer , edk -> provider_id . len ) )) goto WRITE_ERR ;
325333
326334 if (!aws_byte_buf_write_be16 (& output , (uint16_t )edk -> provider_info .len )) goto WRITE_ERR ;
327- if (!aws_byte_buf_write_from_whole_buffer (& output , edk -> provider_info )) goto WRITE_ERR ;
335+ if (!aws_byte_buf_write_from_whole_cursor (& output , aws_byte_cursor_from_array ( edk -> provider_info . buffer , edk -> provider_info . len ) )) goto WRITE_ERR ;
328336
329337 if (!aws_byte_buf_write_be16 (& output , (uint16_t )edk -> ciphertext .len )) goto WRITE_ERR ;
330- if (!aws_byte_buf_write_from_whole_buffer (& output , edk -> ciphertext )) goto WRITE_ERR ;
338+ if (!aws_byte_buf_write_from_whole_cursor (& output , aws_byte_cursor_from_array ( edk -> ciphertext . buffer , edk -> ciphertext . len ) )) goto WRITE_ERR ;
331339 }
332340
333341 if (!aws_byte_buf_write_u8 (
@@ -339,8 +347,8 @@ int aws_cryptosdk_hdr_write(
339347 if (!aws_byte_buf_write_u8 (& output , (uint8_t )hdr -> iv .len )) goto WRITE_ERR ;
340348 if (!aws_byte_buf_write_be32 (& output , hdr -> frame_len )) goto WRITE_ERR ;
341349
342- if (!aws_byte_buf_write_from_whole_buffer (& output , hdr -> iv )) goto WRITE_ERR ;
343- if (!aws_byte_buf_write_from_whole_buffer (& output , hdr -> auth_tag )) goto WRITE_ERR ;
350+ if (!aws_byte_buf_write_from_whole_cursor (& output , aws_byte_cursor_from_array ( hdr -> iv . buffer , hdr -> iv . len ) )) goto WRITE_ERR ;
351+ if (!aws_byte_buf_write_from_whole_cursor (& output , aws_byte_cursor_from_array ( hdr -> auth_tag . buffer , hdr -> auth_tag . len ) )) goto WRITE_ERR ;
344352
345353 * bytes_written = output .len ;
346354 return AWS_OP_SUCCESS ;
0 commit comments