Fix crypto service abort functionality - mac

pull/10469/head
itayzafrir 2019-03-20 18:32:39 +02:00 committed by adbridge
parent 3085e7188c
commit 28775e1c7a
2 changed files with 67 additions and 50 deletions

View File

@ -120,6 +120,10 @@ static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
psa_algorithm_t alg, psa_algorithm_t alg,
psa_sec_function_t func) psa_sec_function_t func)
{ {
if (operation->handle != PSA_NULL_HANDLE) {
return (PSA_ERROR_BAD_STATE);
}
psa_crypto_ipc_t psa_crypto_ipc = { psa_crypto_ipc_t psa_crypto_ipc = {
.func = func, .func = func,
.handle = key_handle, .handle = key_handle,
@ -133,6 +137,9 @@ static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
return (status); return (status);
} }
status = ipc_call(&operation->handle, &in_vec, 1, NULL, 0, false); status = ipc_call(&operation->handle, &in_vec, 1, NULL, 0, false);
if (status != PSA_SUCCESS) {
ipc_close(&operation->handle);
}
return (status); return (status);
} }
@ -168,6 +175,9 @@ psa_status_t psa_mac_update(psa_mac_operation_t *operation,
}; };
psa_status_t status = ipc_call(&operation->handle, in_vec, 2, NULL, 0, false); psa_status_t status = ipc_call(&operation->handle, in_vec, 2, NULL, 0, false);
if (status != PSA_SUCCESS) {
ipc_close(&operation->handle);
}
return (status); return (status);
} }

View File

@ -227,28 +227,30 @@ static void psa_mac_operation(void)
switch (psa_crypto.func) { switch (psa_crypto.func) {
case PSA_MAC_SIGN_SETUP: { case PSA_MAC_SIGN_SETUP: {
if (!psa_crypto_access_control_is_handle_permitted(psa_crypto.handle, if (psa_crypto_access_control_is_handle_permitted(psa_crypto.handle, msg.client_id)) {
msg.client_id)) { status = psa_mac_sign_setup(msg.rhandle, psa_crypto.handle, psa_crypto.alg);
} else {
status = PSA_ERROR_INVALID_HANDLE; status = PSA_ERROR_INVALID_HANDLE;
break;
} }
status = psa_mac_sign_setup(msg.rhandle, if (status != PSA_SUCCESS) {
psa_crypto.handle, mbedtls_free(msg.rhandle);
psa_crypto.alg); psa_set_rhandle(msg.handle, NULL);
}
break; break;
} }
case PSA_MAC_VERIFY_SETUP: { case PSA_MAC_VERIFY_SETUP: {
if (!psa_crypto_access_control_is_handle_permitted(psa_crypto.handle, if (psa_crypto_access_control_is_handle_permitted(psa_crypto.handle, msg.client_id)) {
msg.client_id)) { status = psa_mac_verify_setup(msg.rhandle, psa_crypto.handle, psa_crypto.alg);
} else {
status = PSA_ERROR_INVALID_HANDLE; status = PSA_ERROR_INVALID_HANDLE;
break;
} }
status = psa_mac_verify_setup(msg.rhandle, if (status != PSA_SUCCESS) {
psa_crypto.handle, mbedtls_free(msg.rhandle);
psa_crypto.alg); psa_set_rhandle(msg.handle, NULL);
}
break; break;
} }
@ -261,23 +263,18 @@ static void psa_mac_operation(void)
input_buffer = mbedtls_calloc(1, allocation_size); input_buffer = mbedtls_calloc(1, allocation_size);
if (input_buffer == NULL) { if (input_buffer == NULL) {
psa_mac_abort(msg.rhandle);
status = PSA_ERROR_INSUFFICIENT_MEMORY; status = PSA_ERROR_INSUFFICIENT_MEMORY;
break; } else {
}
while (data_remaining > 0) { while (data_remaining > 0) {
size_to_read = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES); size_to_read = MIN(data_remaining, MAX_DATA_CHUNK_SIZE_IN_BYTES);
bytes_read = psa_read(msg.handle, 1, input_buffer, bytes_read = psa_read(msg.handle, 1, input_buffer, size_to_read);
size_to_read);
if (bytes_read != size_to_read) { if (bytes_read != size_to_read) {
SPM_PANIC("SPM read length mismatch"); SPM_PANIC("SPM read length mismatch");
} }
status = psa_mac_update(msg.rhandle, status = psa_mac_update(msg.rhandle, input_buffer, bytes_read);
input_buffer,
bytes_read);
// stop on error // stop on error
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
break; break;
@ -286,7 +283,12 @@ static void psa_mac_operation(void)
} }
mbedtls_free(input_buffer); mbedtls_free(input_buffer);
}
if (status != PSA_SUCCESS) {
mbedtls_free(msg.rhandle);
psa_set_rhandle(msg.handle, NULL);
}
break; break;
} }
@ -301,19 +303,19 @@ static void psa_mac_operation(void)
size_t mac_length = 0; size_t mac_length = 0;
uint8_t *mac = mbedtls_calloc(1, mac_size); uint8_t *mac = mbedtls_calloc(1, mac_size);
if (mac == NULL) { if (mac == NULL) {
psa_mac_abort(msg.rhandle);
status = PSA_ERROR_INSUFFICIENT_MEMORY; status = PSA_ERROR_INSUFFICIENT_MEMORY;
break; } else {
} status = psa_mac_sign_finish(msg.rhandle, mac, mac_size, &mac_length);
status = psa_mac_sign_finish(msg.rhandle, mac, mac_size,
&mac_length);
if (status == PSA_SUCCESS) { if (status == PSA_SUCCESS) {
psa_write(msg.handle, 0, mac, mac_length); psa_write(msg.handle, 0, mac, mac_length);
psa_write(msg.handle, 1, &mac_length, psa_write(msg.handle, 1, &mac_length, sizeof(mac_length));
sizeof(mac_length)); }
mbedtls_free(mac);
} }
mbedtls_free(mac); mbedtls_free(msg.rhandle);
psa_set_rhandle(msg.handle, NULL);
break; break;
} }
@ -328,10 +330,9 @@ static void psa_mac_operation(void)
uint8_t *mac = mbedtls_calloc(1, mac_length); uint8_t *mac = mbedtls_calloc(1, mac_length);
if (mac == NULL) { if (mac == NULL) {
psa_mac_abort(msg.rhandle);
status = PSA_ERROR_INSUFFICIENT_MEMORY; status = PSA_ERROR_INSUFFICIENT_MEMORY;
break; } else {
}
bytes_read = psa_read(msg.handle, 2, mac, msg.in_size[2]); bytes_read = psa_read(msg.handle, 2, mac, msg.in_size[2]);
if (bytes_read != msg.in_size[2]) { if (bytes_read != msg.in_size[2]) {
SPM_PANIC("SPM read length mismatch"); SPM_PANIC("SPM read length mismatch");
@ -339,11 +340,17 @@ static void psa_mac_operation(void)
status = psa_mac_verify_finish(msg.rhandle, mac, mac_length); status = psa_mac_verify_finish(msg.rhandle, mac, mac_length);
mbedtls_free(mac); mbedtls_free(mac);
}
mbedtls_free(msg.rhandle);
psa_set_rhandle(msg.handle, NULL);
break; break;
} }
case PSA_MAC_ABORT: { case PSA_MAC_ABORT: {
status = psa_mac_abort(msg.rhandle); status = psa_mac_abort(msg.rhandle);
mbedtls_free(msg.rhandle);
psa_set_rhandle(msg.handle, NULL);
break; break;
} }
@ -359,8 +366,8 @@ static void psa_mac_operation(void)
} }
case PSA_IPC_DISCONNECT: { case PSA_IPC_DISCONNECT: {
psa_mac_abort(msg.rhandle);
if (msg.rhandle != NULL) { if (msg.rhandle != NULL) {
psa_mac_abort(msg.rhandle);
mbedtls_free(msg.rhandle); mbedtls_free(msg.rhandle);
} }