#include "client_store.h"
#include "digest.h"
#include "log.h"
#include "ta_sa.h"
sa_status ta_sa_key_digest(
void* out,
size_t* out_length,
sa_key key,
sa_digest_algorithm digest_algorithm,
ta_client client_slot,
const sa_uuid* caller_uuid) {
if (caller_uuid == NULL) {
ERROR("NULL caller_uuid");
return SA_STATUS_NULL_PARAMETER;
}
if (out_length == NULL) {
ERROR("NULL out_length");
return SA_STATUS_NULL_PARAMETER;
}
sa_status status;
client_store_t* client_store = client_store_global();
client_t* client = NULL;
stored_key_t* stored_key = NULL;
do {
status = client_store_acquire(&client, client_store, client_slot, caller_uuid);
if (status != SA_STATUS_OK) {
ERROR("client_store_acquire failed");
break;
}
key_store_t* key_store = client_get_key_store(client);
status = key_store_unwrap(&stored_key, key_store, key, caller_uuid);
if (status != SA_STATUS_OK) {
ERROR("key_store_unwrap failed");
break;
}
const sa_header* header = stored_key_get_header(stored_key);
if (header == NULL) {
ERROR("stored_key_get_header failed");
status = SA_STATUS_NULL_PARAMETER;
break;
}
if (header->type != SA_KEY_TYPE_SYMMETRIC) {
ERROR("key type is not symmetric");
status = SA_STATUS_INVALID_PARAMETER;
break;
}
status = digest_key(out, out_length, digest_algorithm, stored_key);
if (status != SA_STATUS_OK) {
ERROR("digest_key failed");
status = SA_STATUS_INTERNAL_ERROR;
break;
}
} while (false);
stored_key_free(stored_key);
client_store_release(client_store, client_slot, client, caller_uuid);
return status;
}