@@ -24,7 +24,7 @@ LOG_MODULE_REGISTER(suit_decrypt_filter, CONFIG_SUIT_LOG_LEVEL);
2424struct decrypt_ctx {
2525 mbedtls_svc_key_id_t cek_key_id ;
2626 psa_aead_operation_t operation ;
27- struct stream_sink enc_sink ;
27+ struct stream_sink out_sink ;
2828 size_t tag_size ;
2929 size_t stored_tag_bytes ;
3030 uint8_t tag [PSA_AEAD_TAG_MAX_SIZE ];
@@ -67,8 +67,8 @@ static suit_plat_err_t erase(void *ctx)
6767 decrypt_ctx -> stored_tag_bytes = 0 ;
6868 memset (decrypt_ctx -> tag , 0 , sizeof (decrypt_ctx -> tag ));
6969
70- if (decrypt_ctx -> enc_sink .erase != NULL ) {
71- res = decrypt_ctx -> enc_sink .erase (decrypt_ctx -> enc_sink .ctx );
70+ if (decrypt_ctx -> out_sink .erase != NULL ) {
71+ res = decrypt_ctx -> out_sink .erase (decrypt_ctx -> out_sink .ctx );
7272 }
7373 } else {
7474 res = SUIT_PLAT_ERR_INVAL ;
@@ -131,7 +131,7 @@ static suit_plat_err_t write(void *ctx, const uint8_t *buf, size_t size)
131131 goto cleanup ;
132132 }
133133
134- err = decrypt_ctx -> enc_sink .write (decrypt_ctx -> enc_sink .ctx , decrypted_buf ,
134+ err = decrypt_ctx -> out_sink .write (decrypt_ctx -> out_sink .ctx , decrypted_buf ,
135135 decrypted_len );
136136
137137 if (err != SUIT_PLAT_SUCCESS ) {
@@ -195,13 +195,17 @@ static suit_plat_err_t flush(void *ctx)
195195 } else {
196196 LOG_INF ("Firmware decryption successful" );
197197
198- /* Using enc_sink without a write API is blocked by the filter constructor.
198+ /* Using out_sink without a write API is blocked by the filter constructor.
199199 */
200200 if (decrypted_len > 0 ) {
201- res = decrypt_ctx -> enc_sink .write (decrypt_ctx -> enc_sink .ctx ,
201+ res = decrypt_ctx -> out_sink .write (decrypt_ctx -> out_sink .ctx ,
202202 decrypted_buf , decrypted_len );
203203 if (res != SUIT_PLAT_SUCCESS ) {
204204 LOG_ERR ("Failed to write decrypted data: %d" , res );
205+ /* Revert all the changes so that
206+ * no decrypted data remains
207+ */
208+ erase (decrypt_ctx );
205209 }
206210 }
207211 }
@@ -236,16 +240,16 @@ static suit_plat_err_t release(void *ctx)
236240
237241 suit_plat_err_t res = flush (ctx );
238242
239- if (decrypt_ctx -> enc_sink .release != NULL ) {
243+ if (decrypt_ctx -> out_sink .release != NULL ) {
240244 suit_plat_err_t release_ret =
241- decrypt_ctx -> enc_sink .release (decrypt_ctx -> enc_sink .ctx );
245+ decrypt_ctx -> out_sink .release (decrypt_ctx -> out_sink .ctx );
242246
243247 if (res == SUIT_SUCCESS ) {
244248 res = release_ret ;
245249 }
246250 }
247251
248- zeroize (& decrypt_ctx -> enc_sink , sizeof (struct stream_sink ));
252+ zeroize (& decrypt_ctx -> out_sink , sizeof (struct stream_sink ));
249253
250254 decrypt_ctx -> in_use = false;
251255
@@ -261,8 +265,8 @@ static suit_plat_err_t used_storage(void *ctx, size_t *size)
261265 return SUIT_PLAT_ERR_INVAL ;
262266 }
263267
264- if (decrypt_ctx -> enc_sink .used_storage != NULL ) {
265- return decrypt_ctx -> enc_sink .used_storage (decrypt_ctx -> enc_sink .ctx , size );
268+ if (decrypt_ctx -> out_sink .used_storage != NULL ) {
269+ return decrypt_ctx -> out_sink .used_storage (decrypt_ctx -> out_sink .ctx , size );
266270 }
267271
268272 return SUIT_PLAT_ERR_UNSUPPORTED ;
@@ -344,10 +348,10 @@ static suit_plat_err_t get_psa_alg_info(enum suit_cose_alg cose_alg_id, psa_algo
344348 return SUIT_PLAT_SUCCESS ;
345349}
346350
347- suit_plat_err_t suit_decrypt_filter_get (struct stream_sink * dec_sink ,
351+ suit_plat_err_t suit_decrypt_filter_get (struct stream_sink * in_sink ,
348352 struct suit_encryption_info * enc_info ,
349353 const suit_manifest_class_id_t * class_id ,
350- struct stream_sink * enc_sink )
354+ struct stream_sink * out_sink )
351355{
352356 suit_plat_err_t ret = SUIT_PLAT_SUCCESS ;
353357
@@ -356,8 +360,8 @@ suit_plat_err_t suit_decrypt_filter_get(struct stream_sink *dec_sink,
356360 return SUIT_PLAT_ERR_BUSY ;
357361 }
358362
359- if ((enc_info == NULL ) || (enc_sink == NULL ) || (dec_sink == NULL ) ||
360- (enc_sink -> write == NULL ) || class_id == NULL ) {
363+ if ((enc_info == NULL ) || (out_sink == NULL ) || (in_sink == NULL ) ||
364+ (out_sink -> write == NULL ) || class_id == NULL ) {
361365 return SUIT_PLAT_ERR_INVAL ;
362366 }
363367
@@ -403,23 +407,30 @@ suit_plat_err_t suit_decrypt_filter_get(struct stream_sink *dec_sink,
403407
404408 status = psa_aead_update_ad (& ctx .operation , enc_info -> aad .value , enc_info -> aad .len );
405409
410+ if (status != PSA_SUCCESS ) {
411+ LOG_ERR ("Failed to pass additional data for authentication operation: %d" , status );
412+ psa_aead_abort (& ctx .operation );
413+ ctx .in_use = false;
414+ return SUIT_PLAT_ERR_CRASH ;
415+ }
416+
406417 ctx .stored_tag_bytes = 0 ;
407- memcpy (& ctx .enc_sink , enc_sink , sizeof (struct stream_sink ));
418+ memcpy (& ctx .out_sink , out_sink , sizeof (struct stream_sink ));
408419
409- dec_sink -> ctx = & ctx ;
420+ in_sink -> ctx = & ctx ;
410421
411- dec_sink -> write = write ;
412- dec_sink -> erase = erase ;
413- dec_sink -> release = release ;
414- dec_sink -> flush = flush ;
415- if (enc_sink -> used_storage != NULL ) {
416- dec_sink -> used_storage = used_storage ;
422+ in_sink -> write = write ;
423+ in_sink -> erase = erase ;
424+ in_sink -> release = release ;
425+ in_sink -> flush = flush ;
426+ if (out_sink -> used_storage != NULL ) {
427+ in_sink -> used_storage = used_storage ;
417428 } else {
418- dec_sink -> used_storage = NULL ;
429+ in_sink -> used_storage = NULL ;
419430 }
420431
421432 /* Seeking is not possible on encrypted payload. */
422- dec_sink -> seek = NULL ;
433+ in_sink -> seek = NULL ;
423434
424435 return SUIT_PLAT_SUCCESS ;
425436}
0 commit comments