Skip to content

Commit 5214cef

Browse files
frkvrlubos
authored andcommitted
nrf_security: CRACEN: Align CBC PKCS#7 methodology
-Align CBC PKCS#7 padding methodology to changes made for Mbed TLS v3.6.4 Signed-off-by: Frank Audun Kvamtrø <[email protected]>
1 parent d4f04b2 commit 5214cef

File tree

1 file changed

+10
-10
lines changed
  • subsys/nrf_security/src/drivers/cracen/cracenpsa/src

1 file changed

+10
-10
lines changed

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/cipher.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,20 @@ static psa_status_t decrypt_cbc(const struct sxkeyref *key, const uint8_t *input
258258
return silex_statuscodes_to_psa(sx_status);
259259
}
260260

261-
uint8_t padding = output[input_length - 1];
261+
size_t padding_length = output[input_length - 1];
262+
size_t padding_index = input_length - padding_length;
263+
uint32_t failure = 0;
262264

263-
if (padding > SX_BLKCIPHER_AES_BLK_SZ || padding == 0) {
264-
return PSA_ERROR_INVALID_PADDING;
265-
}
265+
failure |= (padding_length > SX_BLKCIPHER_AES_BLK_SZ);
266+
failure |= (padding_length == 0);
266267

267-
for (size_t i = input_length - padding; i < input_length; i++) {
268-
if (output[i] != padding) {
269-
return PSA_ERROR_INVALID_PADDING;
270-
}
268+
for (size_t i = 0; i < input_length; i++) {
269+
failure |= (output[i] ^ padding_length) * (i >= padding_index);
271270
}
272271

273-
*output_length = input_length - padding;
274-
return PSA_SUCCESS;
272+
*output_length = padding_index;
273+
274+
return (failure == 0) ? PSA_SUCCESS : PSA_ERROR_INVALID_PADDING;
275275
}
276276

277277
psa_status_t cracen_cipher_encrypt(const psa_key_attributes_t *attributes,

0 commit comments

Comments
 (0)