Skip to content

Commit 979ca1e

Browse files
author
itayzafrir
committed
Do not allocate zero sized buffers - hash
1 parent 7b35e76 commit 979ca1e

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

components/TARGET_PSA/services/crypto/COMPONENT_SPE/psa_crypto_partition.c

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -447,27 +447,32 @@ static void psa_hash_operation(void)
447447
size_t size_to_read = 0;
448448
size_t allocation_size = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
449449

450-
input_buffer = mbedtls_calloc(1, allocation_size);
451-
if (input_buffer == NULL) {
452-
psa_hash_abort(msg.rhandle);
453-
status = PSA_ERROR_INSUFFICIENT_MEMORY;
454-
} else {
455-
while (data_remaining > 0) {
456-
size_to_read = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
457-
bytes_read = psa_read(msg.handle, 1, input_buffer, size_to_read);
450+
if (allocation_size > 0) {
451+
input_buffer = mbedtls_calloc(1, allocation_size);
452+
if (input_buffer == NULL) {
453+
psa_hash_abort(msg.rhandle);
454+
status = PSA_ERROR_INSUFFICIENT_MEMORY;
455+
} else {
456+
while (data_remaining > 0) {
457+
size_to_read = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
458458

459-
if (bytes_read != size_to_read) {
460-
SPM_PANIC("SPM read length mismatch");
461-
}
459+
bytes_read = psa_read(msg.handle, 1, input_buffer, size_to_read);
460+
if (bytes_read != size_to_read) {
461+
SPM_PANIC("SPM read length mismatch");
462+
}
462463

463-
status = psa_hash_update(msg.rhandle, input_buffer, bytes_read);
464-
// stop on error
465-
if (status != PSA_SUCCESS) {
466-
break;
464+
status = psa_hash_update(msg.rhandle, input_buffer, bytes_read);
465+
// stop on error
466+
if (status != PSA_SUCCESS) {
467+
break;
468+
}
469+
data_remaining = data_remaining - bytes_read;
467470
}
468-
data_remaining = data_remaining - bytes_read;
471+
472+
mbedtls_free(input_buffer);
469473
}
470-
mbedtls_free(input_buffer);
474+
} else {
475+
status = psa_hash_update(msg.rhandle, input_buffer, allocation_size);
471476
}
472477

473478
if (status != PSA_SUCCESS) {
@@ -479,25 +484,30 @@ static void psa_hash_operation(void)
479484
}
480485

481486
case PSA_HASH_FINISH: {
482-
size_t hash_size = 0;
483-
bytes_read = psa_read(msg.handle, 1, &hash_size,
484-
msg.in_size[1]);
487+
uint8_t *hash = NULL;
488+
size_t hash_size = 0, hash_length = 0;
489+
490+
bytes_read = psa_read(msg.handle, 1, &hash_size, msg.in_size[1]);
485491
if (bytes_read != msg.in_size[1]) {
486492
SPM_PANIC("SPM read length mismatch");
487493
}
488494

489-
size_t hash_length = 0;
490-
uint8_t *hash = mbedtls_calloc(1, hash_size);
491-
if (hash == NULL) {
492-
psa_hash_abort(msg.rhandle);
493-
status = PSA_ERROR_INSUFFICIENT_MEMORY;
494-
} else {
495+
if (hash_size > 0) {
496+
hash = mbedtls_calloc(1, hash_size);
497+
if (hash == NULL) {
498+
status = PSA_ERROR_INSUFFICIENT_MEMORY;
499+
}
500+
}
501+
502+
if (status == PSA_SUCCESS) {
495503
status = psa_hash_finish(msg.rhandle, hash, hash_size, &hash_length);
496504
if (status == PSA_SUCCESS) {
497505
psa_write(msg.handle, 0, hash, hash_length);
498506
psa_write(msg.handle, 1, &hash_length, sizeof(hash_length));
499507
}
500508
mbedtls_free(hash);
509+
} else {
510+
psa_hash_abort(msg.rhandle);
501511
}
502512

503513
destroy_hash_clone(msg.rhandle);
@@ -507,26 +517,31 @@ static void psa_hash_operation(void)
507517
}
508518

509519
case PSA_HASH_VERIFY: {
520+
uint8_t *hash = NULL;
510521
size_t hash_length = 0;
511-
bytes_read = psa_read(msg.handle, 1, &hash_length,
512-
msg.in_size[1]);
513-
if (bytes_read != msg.in_size[1] ||
514-
hash_length != msg.in_size[2]) {
522+
523+
bytes_read = psa_read(msg.handle, 1, &hash_length, msg.in_size[1]);
524+
if (bytes_read != msg.in_size[1] || hash_length != msg.in_size[2]) {
515525
SPM_PANIC("SPM read length mismatch");
516526
}
517527

518-
uint8_t *hash = mbedtls_calloc(1, hash_length);
519-
if (hash == NULL) {
520-
psa_hash_abort(msg.rhandle);
521-
status = PSA_ERROR_INSUFFICIENT_MEMORY;
522-
} else {
523-
bytes_read = psa_read(msg.handle, 2, hash, msg.in_size[2]);
524-
if (bytes_read != msg.in_size[2]) {
525-
SPM_PANIC("SPM read length mismatch");
528+
if (hash_length > 0) {
529+
hash = mbedtls_calloc(1, hash_length);
530+
if (hash == NULL) {
531+
status = PSA_ERROR_INSUFFICIENT_MEMORY;
532+
} else {
533+
bytes_read = psa_read(msg.handle, 2, hash, hash_length);
534+
if (bytes_read != hash_length) {
535+
SPM_PANIC("SPM read length mismatch");
536+
}
526537
}
538+
}
527539

540+
if (status == PSA_SUCCESS) {
528541
status = psa_hash_verify(msg.rhandle, hash, hash_length);
529542
mbedtls_free(hash);
543+
} else {
544+
psa_hash_abort(msg.rhandle);
530545
}
531546

532547
destroy_hash_clone(msg.rhandle);

0 commit comments

Comments
 (0)