Skip to content

Commit 7b35e76

Browse files
author
itayzafrir
committed
Do not allocate zero sized buffers - mac
1 parent 8044f6d commit 7b35e76

File tree

1 file changed

+54
-41
lines changed

1 file changed

+54
-41
lines changed

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

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -255,34 +255,37 @@ static void psa_mac_operation(void)
255255
}
256256

257257
case PSA_MAC_UPDATE: {
258-
259258
uint8_t *input_buffer = NULL;
260259
size_t data_remaining = msg.in_size[1];
261260
size_t allocation_size = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
262261
size_t size_to_read = 0;
263262

264-
input_buffer = mbedtls_calloc(1, allocation_size);
265-
if (input_buffer == NULL) {
266-
psa_mac_abort(msg.rhandle);
267-
status = PSA_ERROR_INSUFFICIENT_MEMORY;
268-
} else {
269-
while (data_remaining > 0) {
270-
size_to_read = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
271-
bytes_read = psa_read(msg.handle, 1, input_buffer, size_to_read);
272-
273-
if (bytes_read != size_to_read) {
274-
SPM_PANIC("SPM read length mismatch");
263+
if (allocation_size > 0) {
264+
input_buffer = mbedtls_calloc(1, allocation_size);
265+
if (input_buffer == NULL) {
266+
psa_mac_abort(msg.rhandle);
267+
status = PSA_ERROR_INSUFFICIENT_MEMORY;
268+
} else {
269+
while (data_remaining > 0) {
270+
size_to_read = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
271+
272+
bytes_read = psa_read(msg.handle, 1, input_buffer, size_to_read);
273+
if (bytes_read != size_to_read) {
274+
SPM_PANIC("SPM read length mismatch");
275+
}
276+
277+
status = psa_mac_update(msg.rhandle, input_buffer, bytes_read);
278+
// stop on error
279+
if (status != PSA_SUCCESS) {
280+
break;
281+
}
282+
data_remaining = data_remaining - bytes_read;
275283
}
276284

277-
status = psa_mac_update(msg.rhandle, input_buffer, bytes_read);
278-
// stop on error
279-
if (status != PSA_SUCCESS) {
280-
break;
281-
}
282-
data_remaining = data_remaining - bytes_read;
285+
mbedtls_free(input_buffer);
283286
}
284-
285-
mbedtls_free(input_buffer);
287+
} else {
288+
status = psa_mac_update(msg.rhandle, input_buffer, allocation_size);
286289
}
287290

288291
if (status != PSA_SUCCESS) {
@@ -293,25 +296,30 @@ static void psa_mac_operation(void)
293296
}
294297

295298
case PSA_MAC_SIGN_FINISH: {
296-
size_t mac_size = 0;
297-
bytes_read = psa_read(msg.handle, 1, &mac_size,
298-
msg.in_size[1]);
299+
uint8_t *mac = NULL;
300+
size_t mac_size = 0, mac_length = 0;
301+
302+
bytes_read = psa_read(msg.handle, 1, &mac_size, msg.in_size[1]);
299303
if (bytes_read != msg.in_size[1]) {
300304
SPM_PANIC("SPM read length mismatch");
301305
}
302306

303-
size_t mac_length = 0;
304-
uint8_t *mac = mbedtls_calloc(1, mac_size);
305-
if (mac == NULL) {
306-
psa_mac_abort(msg.rhandle);
307-
status = PSA_ERROR_INSUFFICIENT_MEMORY;
308-
} else {
307+
if (mac_size > 0) {
308+
mac = mbedtls_calloc(1, mac_size);
309+
if (mac == NULL) {
310+
status = PSA_ERROR_INSUFFICIENT_MEMORY;
311+
}
312+
}
313+
314+
if (status == PSA_SUCCESS) {
309315
status = psa_mac_sign_finish(msg.rhandle, mac, mac_size, &mac_length);
310316
if (status == PSA_SUCCESS) {
311317
psa_write(msg.handle, 0, mac, mac_length);
312318
psa_write(msg.handle, 1, &mac_length, sizeof(mac_length));
313319
}
314320
mbedtls_free(mac);
321+
} else {
322+
psa_mac_abort(msg.rhandle);
315323
}
316324

317325
mbedtls_free(msg.rhandle);
@@ -320,26 +328,31 @@ static void psa_mac_operation(void)
320328
}
321329

322330
case PSA_MAC_VERIFY_FINISH: {
331+
uint8_t *mac = NULL;
323332
size_t mac_length = 0;
324-
bytes_read = psa_read(msg.handle, 1, &mac_length,
325-
msg.in_size[1]);
326-
if (bytes_read != msg.in_size[1] ||
327-
mac_length != msg.in_size[2]) {
333+
334+
bytes_read = psa_read(msg.handle, 1, &mac_length, msg.in_size[1]);
335+
if (bytes_read != msg.in_size[1] || mac_length != msg.in_size[2]) {
328336
SPM_PANIC("SPM read length mismatch");
329337
}
330338

331-
uint8_t *mac = mbedtls_calloc(1, mac_length);
332-
if (mac == NULL) {
333-
psa_mac_abort(msg.rhandle);
334-
status = PSA_ERROR_INSUFFICIENT_MEMORY;
335-
} else {
336-
bytes_read = psa_read(msg.handle, 2, mac, msg.in_size[2]);
337-
if (bytes_read != msg.in_size[2]) {
338-
SPM_PANIC("SPM read length mismatch");
339+
if (mac_length > 0) {
340+
mac = mbedtls_calloc(1, mac_length);
341+
if (mac == NULL) {
342+
status = PSA_ERROR_INSUFFICIENT_MEMORY;
343+
} else {
344+
bytes_read = psa_read(msg.handle, 2, mac, mac_length);
345+
if (bytes_read != mac_length) {
346+
SPM_PANIC("SPM read length mismatch");
347+
}
339348
}
349+
}
340350

351+
if (status == PSA_SUCCESS) {
341352
status = psa_mac_verify_finish(msg.rhandle, mac, mac_length);
342353
mbedtls_free(mac);
354+
} else {
355+
psa_mac_abort(msg.rhandle);
343356
}
344357

345358
mbedtls_free(msg.rhandle);

0 commit comments

Comments
 (0)