@@ -290,28 +290,105 @@ START_TEST(test_ram_decrypt_len_bitflip_rejected)
290290}
291291END_TEST
292292
293+ /* The maximum in-bounds payload, matching the branch the bound check uses. */
294+ #ifdef WOLFBOOT_FIXED_PARTITIONS
295+ # define RAM_DECRYPT_MAX_PAYLOAD (WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE)
296+ #elif defined(WOLFBOOT_RAMBOOT_MAX_SIZE )
297+ # define RAM_DECRYPT_MAX_PAYLOAD WOLFBOOT_RAMBOOT_MAX_SIZE
298+ #endif
299+
300+ /* The exact maximum in-bounds length must decrypt successfully. Paired with the
301+ * one-block-over test below this brackets the accept/reject boundary, locking
302+ * the comparison's off-by-one ('>' vs '>=', +/- IMAGE_HEADER_SIZE). */
303+ START_TEST (test_ram_decrypt_max_valid )
304+ {
305+ const uint32_t len = RAM_DECRYPT_MAX_PAYLOAD ;
306+ const uint32_t total = IMAGE_HEADER_SIZE + len ;
307+ uint8_t * plain = malloc (total );
308+ uint8_t * enc = malloc (total );
309+ uint8_t * dst = malloc (WOLFBOOT_PARTITION_SIZE );
310+ uint32_t magic = WOLFBOOT_MAGIC ;
311+ uint32_t i ;
312+ int ret ;
313+
314+ ck_assert_ptr_nonnull (plain );
315+ ck_assert_ptr_nonnull (enc );
316+ ck_assert_ptr_nonnull (dst );
317+ ck_assert_uint_eq (total % ENCRYPT_BLOCK_SIZE , 0 );
318+ ck_assert (total <= WOLFBOOT_PARTITION_SIZE );
319+
320+ for (i = 0 ; i < total ; i ++ )
321+ plain [i ] = (uint8_t )(i * 7 + 3 );
322+ memcpy (plain , & magic , sizeof (magic ));
323+ memcpy (plain + sizeof (uint32_t ), & len , sizeof (len ));
324+
325+ setup_crypto_key ();
326+ encrypt_blob (enc , plain , total );
327+
328+ ret = wolfBoot_ram_decrypt (enc , dst );
329+ ck_assert_int_eq (ret , 0 );
330+ ck_assert_mem_eq (dst , plain , total );
331+
332+ free (plain );
333+ free (enc );
334+ free (dst );
335+ }
336+ END_TEST
337+
338+ /* One block past the maximum in-bounds length must be rejected before the copy
339+ * loop runs (the reject side of the boundary pair). */
340+ START_TEST (test_ram_decrypt_one_over_rejected )
341+ {
342+ const uint32_t len = RAM_DECRYPT_MAX_PAYLOAD + ENCRYPT_BLOCK_SIZE ;
343+ uint8_t * enc = malloc (2 * IMAGE_HEADER_SIZE );
344+ uint8_t * dst = malloc (WOLFBOOT_PARTITION_SIZE );
345+ int ret ;
346+
347+ ck_assert_ptr_nonnull (enc );
348+ ck_assert_ptr_nonnull (dst );
349+ memset (enc , 0 , 2 * IMAGE_HEADER_SIZE );
350+
351+ setup_crypto_key ();
352+ make_encrypted_header (enc , len );
353+
354+ ret = wolfBoot_ram_decrypt (enc , dst );
355+ ck_assert_int_eq (ret , -1 );
356+
357+ free (enc );
358+ free (dst );
359+ }
360+ END_TEST
361+
293362Suite * wolfboot_suite (void )
294363{
295364 Suite * s = suite_create ("wolfboot-ram-decrypt" );
296365 TCase * valid = tcase_create ("ram_decrypt valid image" );
297366 TCase * oversize = tcase_create ("ram_decrypt oversize rejected" );
298367 TCase * overflow = tcase_create ("ram_decrypt length-overflow rejected" );
299368 TCase * bitflip = tcase_create ("ram_decrypt length bit-flip rejected" );
369+ TCase * maxvalid = tcase_create ("ram_decrypt exact-max valid" );
370+ TCase * oneover = tcase_create ("ram_decrypt one block over rejected" );
300371
301372 tcase_add_test (valid , test_ram_decrypt_valid );
302373 tcase_add_test (oversize , test_ram_decrypt_oversize_rejected );
303374 tcase_add_test (overflow , test_ram_decrypt_overflow_len_rejected );
304375 tcase_add_test (bitflip , test_ram_decrypt_len_bitflip_rejected );
376+ tcase_add_test (maxvalid , test_ram_decrypt_max_valid );
377+ tcase_add_test (oneover , test_ram_decrypt_one_over_rejected );
305378
306379 suite_add_tcase (s , valid );
307380 suite_add_tcase (s , oversize );
308381 suite_add_tcase (s , overflow );
309382 suite_add_tcase (s , bitflip );
383+ suite_add_tcase (s , maxvalid );
384+ suite_add_tcase (s , oneover );
310385
311386 tcase_set_timeout (bitflip , 5 );
312387 tcase_set_timeout (valid , 5 );
313388 tcase_set_timeout (oversize , 5 );
314389 tcase_set_timeout (overflow , 5 );
390+ tcase_set_timeout (maxvalid , 5 );
391+ tcase_set_timeout (oneover , 5 );
315392
316393 return s ;
317394}
0 commit comments