Skip to content

Commit ee46fe7

Browse files
Fix output size calculations in cipher tests
Some calls to psa_cipher_finish or psa_cipher_update append to a buffer. Several of these calls were not calculating the offset into the buffer or the remaining buffer size correctly. This did not lead to buffer overflows before because the buffer sizes were sufficiently large for our test inputs. This did not lead to incorrect output when the test was designed to append but actually wrote too early because all the existing test cases either have no output from finish (stream cipher) or have no output from update (CBC, with less than one block of input).
1 parent 2b88dc3 commit ee46fe7

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

tests/suites/test_suite_psa_crypto.function

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,8 +2415,8 @@ void cipher_encrypt( int alg_arg, int key_type_arg,
24152415
&function_output_length ) );
24162416
total_output_length += function_output_length;
24172417
status = psa_cipher_finish( &operation,
2418-
output + function_output_length,
2419-
output_buffer_size,
2418+
output + total_output_length,
2419+
output_buffer_size - total_output_length,
24202420
&function_output_length );
24212421
total_output_length += function_output_length;
24222422

@@ -2483,12 +2483,13 @@ void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
24832483
PSA_ASSERT( psa_cipher_update( &operation,
24842484
input->x + first_part_size,
24852485
input->len - first_part_size,
2486-
output, output_buffer_size,
2486+
output + total_output_length,
2487+
output_buffer_size - total_output_length,
24872488
&function_output_length ) );
24882489
total_output_length += function_output_length;
24892490
PSA_ASSERT( psa_cipher_finish( &operation,
2490-
output + function_output_length,
2491-
output_buffer_size,
2491+
output + total_output_length,
2492+
output_buffer_size - total_output_length,
24922493
&function_output_length ) );
24932494
total_output_length += function_output_length;
24942495
PSA_ASSERT( psa_cipher_abort( &operation ) );
@@ -2554,12 +2555,13 @@ void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
25542555
PSA_ASSERT( psa_cipher_update( &operation,
25552556
input->x + first_part_size,
25562557
input->len - first_part_size,
2557-
output, output_buffer_size,
2558+
output + total_output_length,
2559+
output_buffer_size - total_output_length,
25582560
&function_output_length ) );
25592561
total_output_length += function_output_length;
25602562
PSA_ASSERT( psa_cipher_finish( &operation,
2561-
output + function_output_length,
2562-
output_buffer_size,
2563+
output + total_output_length,
2564+
output_buffer_size - total_output_length,
25632565
&function_output_length ) );
25642566
total_output_length += function_output_length;
25652567
PSA_ASSERT( psa_cipher_abort( &operation ) );
@@ -2622,8 +2624,8 @@ void cipher_decrypt( int alg_arg, int key_type_arg,
26222624
&function_output_length ) );
26232625
total_output_length += function_output_length;
26242626
status = psa_cipher_finish( &operation,
2625-
output + function_output_length,
2626-
output_buffer_size,
2627+
output + total_output_length,
2628+
output_buffer_size - total_output_length,
26272629
&function_output_length );
26282630
total_output_length += function_output_length;
26292631
TEST_EQUAL( status, expected_status );
@@ -2689,7 +2691,8 @@ void cipher_verify_output( int alg_arg, int key_type_arg,
26892691
output1, output1_size,
26902692
&output1_length ) );
26912693
PSA_ASSERT( psa_cipher_finish( &operation1,
2692-
output1 + output1_length, output1_size,
2694+
output1 + output1_length,
2695+
output1_size - output1_length,
26932696
&function_output_length ) );
26942697

26952698
output1_length += function_output_length;
@@ -2707,7 +2710,7 @@ void cipher_verify_output( int alg_arg, int key_type_arg,
27072710
function_output_length = 0;
27082711
PSA_ASSERT( psa_cipher_finish( &operation2,
27092712
output2 + output2_length,
2710-
output2_size,
2713+
output2_size - output2_length,
27112714
&function_output_length ) );
27122715

27132716
output2_length += function_output_length;

0 commit comments

Comments
 (0)