@@ -28,13 +28,13 @@ const uint8_t dummy_data_output_sha256[] = {
28
28
};
29
29
30
30
/* Input valid lzma2 compressed data whereby the output is larger than the dictionary size */
31
- const uint8_t dummy_data_too_large_input [] = {
32
- #include "dummy_data_input_too_large .inc"
31
+ const uint8_t dummy_data_large_input [] = {
32
+ #include "dummy_data_input_large .inc"
33
33
};
34
34
35
- /* File size and sha256 hash of decompressed data for too large an output */
36
- const uint32_t dummy_data_too_large_output_size = 134061 ;
37
- const uint8_t dummy_data_too_large_output_sha256 [] = {
35
+ /* File size and sha256 hash of decompressed data for an output larger than dictionary size */
36
+ const uint32_t dummy_data_large_output_size = 134061 ;
37
+ const uint8_t dummy_data_large_output_sha256 [] = {
38
38
0xc0 , 0xc4 , 0xac , 0xc7 , 0xac , 0x69 , 0x37 , 0x4b ,
39
39
0x60 , 0xb4 , 0x87 , 0xe9 , 0x3d , 0x65 , 0xcf , 0xa2 ,
40
40
0x4b , 0x2b , 0xef , 0xd0 , 0xb9 , 0xbf , 0xf9 , 0xc9 ,
@@ -205,7 +205,6 @@ ZTEST(nrf_compress_decompression, test_valid_data_decompression)
205
205
rc = implementation -> decompress (inst , & dummy_data_input [pos ],
206
206
(sizeof (dummy_data_input ) - pos ), true,
207
207
& offset , & output , & output_size );
208
- pos += 1 ;
209
208
} else {
210
209
rc = implementation -> decompress (inst , & dummy_data_input [pos ], rc , false,
211
210
& offset , & output , & output_size );
@@ -257,14 +256,17 @@ ZTEST(nrf_compress_decompression, test_valid_data_decompression)
257
256
#endif
258
257
}
259
258
260
- ZTEST (nrf_compress_decompression , test_valid_data_too_large_decompression )
259
+ ZTEST (nrf_compress_decompression , test_valid_data_large_decompression )
261
260
{
262
261
int rc ;
263
262
uint32_t pos ;
264
263
uint32_t offset ;
265
264
uint8_t * output ;
266
265
uint32_t output_size ;
266
+ uint32_t total_output_size = 0 ;
267
+ uint8_t output_sha [SHA256_SIZE ] = { 0 };
267
268
struct nrf_compress_implementation * implementation ;
269
+ mbedtls_sha256_context ctx ;
268
270
#if defined(CONFIG_NRF_COMPRESS_EXTERNAL_DICTIONARY )
269
271
void * inst = & lzma_inst ;
270
272
@@ -273,6 +275,10 @@ ZTEST(nrf_compress_decompression, test_valid_data_too_large_decompression)
273
275
void * inst = NULL ;
274
276
#endif
275
277
278
+ mbedtls_sha256_init (& ctx );
279
+ rc = mbedtls_sha256_starts (& ctx , false);
280
+ zassert_ok (rc , "Expected mbedtls sha256 start to be successful" );
281
+
276
282
implementation = nrf_compress_implementation_find (NRF_COMPRESS_TYPE_LZMA );
277
283
278
284
pos = 0 ;
@@ -283,12 +289,12 @@ ZTEST(nrf_compress_decompression, test_valid_data_too_large_decompression)
283
289
rc = implementation -> decompress_bytes_needed (inst );
284
290
zassert_equal (rc , 2 , "Expected to need 2 bytes for LZMA header" );
285
291
286
- rc = implementation -> decompress (inst , & dummy_data_too_large_input [pos ], rc , false, & offset ,
292
+ rc = implementation -> decompress (inst , & dummy_data_large_input [pos ], rc , false, & offset ,
287
293
& output , & output_size );
288
294
zassert_ok (rc , "Expected header decompress to be successful" );
289
295
pos += offset ;
290
296
291
- while (pos < sizeof (dummy_data_too_large_input )) {
297
+ while (pos < sizeof (dummy_data_large_input )) {
292
298
rc = implementation -> decompress_bytes_needed (inst );
293
299
zassert_equal (rc , CONFIG_NRF_COMPRESS_CHUNK_SIZE ,
294
300
"Expected to need chunk size bytes for LZMA data" );
@@ -298,26 +304,43 @@ ZTEST(nrf_compress_decompression, test_valid_data_too_large_decompression)
298
304
rc = REDUCED_BUFFER_SIZE ;
299
305
}
300
306
301
- if ((pos + rc ) > sizeof (dummy_data_too_large_input )) {
302
- rc = implementation -> decompress (inst , & dummy_data_too_large_input [pos ],
303
- (sizeof (dummy_data_too_large_input ) - pos ),
307
+ if ((pos + rc ) > sizeof (dummy_data_large_input )) {
308
+ rc = implementation -> decompress (inst , & dummy_data_large_input [pos ],
309
+ (sizeof (dummy_data_large_input ) - pos ),
304
310
true, & offset , & output , & output_size );
305
- pos += 1 ;
306
311
} else {
307
- rc = implementation -> decompress (inst , & dummy_data_too_large_input [pos ], rc ,
312
+ rc = implementation -> decompress (inst , & dummy_data_large_input [pos ], rc ,
308
313
false, & offset , & output , & output_size );
309
314
}
310
315
311
- if (rc != - EINVAL ) {
312
- zassert_ok (rc , "Expected data decompress to be successful" );
316
+ zassert_ok (rc , "Expected data decompress to be successful" );
317
+
318
+ total_output_size += output_size ;
319
+
320
+ if (output_size > 0 ) {
321
+ #if defined(CONFIG_NRF_COMPRESS_EXTERNAL_DICTIONARY )
322
+ rc = mbedtls_sha256_update (& ctx , local_dictionary , output_size );
323
+ #else
324
+ rc = mbedtls_sha256_update (& ctx , output , output_size );
325
+ #endif
326
+ zassert_ok (rc , "Expected hash update to be successful" );
313
327
}
314
328
315
329
pos += offset ;
316
330
}
317
331
318
332
(void )implementation -> deinit (inst );
333
+ zassert_ok (rc , "Expected deinit to be successful" );
319
334
320
- zassert_equal (rc , - EINVAL , "Expected data decompress with too large an output to fail" );
335
+ zassert_equal (total_output_size , dummy_data_large_output_size ,
336
+ "Expected decompressed data size to match" );
337
+
338
+ rc = mbedtls_sha256_finish (& ctx , output_sha );
339
+ mbedtls_sha256_free (& ctx );
340
+ zassert_ok (rc , "Expected mbedtls sha256 finish to be successful" );
341
+
342
+ zassert_mem_equal (output_sha , dummy_data_large_output_sha256 , SHA256_SIZE ,
343
+ "Expected hash to match" );
321
344
322
345
#if defined(CONFIG_NRF_COMPRESS_EXTERNAL_DICTIONARY )
323
346
zassert_equal (open_dict_cnt , 1 ,
@@ -394,7 +417,7 @@ ZTEST(nrf_compress_decompression, test_invalid_data_data)
394
417
rc = implementation -> decompress_bytes_needed (inst );
395
418
zassert_equal (rc , 2 , "Expected to need 2 bytes for LZMA header" );
396
419
397
- rc = implementation -> decompress (inst , & dummy_data_too_large_input [pos ], rc , false, & offset ,
420
+ rc = implementation -> decompress (inst , & dummy_data_large_input [pos ], rc , false, & offset ,
398
421
& output , & output_size );
399
422
zassert_ok (rc , "Expected header decompress to be successful" );
400
423
pos += offset ;
@@ -413,7 +436,6 @@ ZTEST(nrf_compress_decompression, test_invalid_data_data)
413
436
rc = implementation -> decompress (NULL , & dummy_data_input [pos ],
414
437
(sizeof (dummy_data_input ) - pos ), true,
415
438
& offset , & output , & output_size );
416
- pos += 1 ;
417
439
} else if (pos >= REDUCED_BUFFER_SIZE ) {
418
440
/* Read in manipulated bad data */
419
441
uint8_t bad_data [REDUCED_BUFFER_SIZE ];
@@ -508,7 +530,6 @@ ZTEST(nrf_compress_decompression, test_valid_data_decompression_random_sizes)
508
530
rc = implementation -> decompress (inst , & dummy_data_input [pos ],
509
531
(sizeof (dummy_data_input ) - pos ), true,
510
532
& offset , & output , & output_size );
511
- pos += 1 ;
512
533
} else {
513
534
rc = implementation -> decompress (inst , & dummy_data_input [pos ], rc , false,
514
535
& offset , & output , & output_size );
@@ -627,7 +648,6 @@ ZTEST(nrf_compress_decompression, test_valid_data_decompression_reset)
627
648
rc = implementation -> decompress (inst , & dummy_data_input [pos ],
628
649
(sizeof (dummy_data_input ) - pos ), true,
629
650
& offset , & output , & output_size );
630
- pos += 1 ;
631
651
} else {
632
652
rc = implementation -> decompress (inst , & dummy_data_input [pos ], rc , false,
633
653
& offset , & output , & output_size );
0 commit comments