#include "atca_basic.h"
#include "atca_execution.h"
#include "host/atca_host.h"
ATCA_STATUS atcab_secureboot(uint8_t mode, uint16_t param2, const uint8_t* digest, const uint8_t* signature, uint8_t* mac)
{
ATCAPacket packet;
ATCACommand ca_cmd = _gDevice->mCommands;
ATCA_STATUS status = ATCA_GEN_FAIL;
if (digest == NULL)
{
return ATCA_BAD_PARAM;
}
do
{
packet.param1 = mode;
packet.param2 = param2;
memcpy(packet.data, digest, SECUREBOOT_DIGEST_SIZE);
if (signature)
{
memcpy(&packet.data[SECUREBOOT_DIGEST_SIZE], signature, SECUREBOOT_SIGNATURE_SIZE);
}
if ((status = atSecureBoot(ca_cmd, &packet)) != ATCA_SUCCESS)
{
break;
}
if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS)
{
break;
}
if ((mac != NULL) && (packet.data[ATCA_COUNT_IDX] >= SECUREBOOT_RSP_SIZE_MAC))
{
memcpy(mac, &packet.data[ATCA_RSP_DATA_IDX], SECUREBOOT_MAC_SIZE);
}
}
while (0);
return status;
}
ATCA_STATUS atcab_secureboot_mac(uint8_t mode, const uint8_t* digest, const uint8_t* signature, const uint8_t* num_in, const uint8_t* io_key, bool* is_verified)
{
ATCA_STATUS status = ATCA_GEN_FAIL;
atca_temp_key_t tempkey;
atca_nonce_in_out_t nonce_params;
atca_secureboot_enc_in_out_t sboot_enc_params;
atca_secureboot_mac_in_out_t sboot_mac_params;
uint8_t rand_out[RANDOM_NUM_SIZE];
uint8_t key[ATCA_KEY_SIZE];
uint8_t digest_enc[SECUREBOOT_DIGEST_SIZE];
uint8_t mac[SECUREBOOT_MAC_SIZE];
uint8_t host_mac[SECUREBOOT_MAC_SIZE];
uint8_t buf[2];
do
{
if (is_verified == NULL)
{
return ATCA_BAD_PARAM;
}
*is_verified = false;
if (digest == NULL || num_in == NULL || io_key == NULL)
{
return ATCA_BAD_PARAM;
}
memset(&tempkey, 0, sizeof(tempkey));
memset(&nonce_params, 0, sizeof(nonce_params));
nonce_params.mode = NONCE_MODE_SEED_UPDATE;
nonce_params.zero = 0;
nonce_params.num_in = num_in;
nonce_params.rand_out = rand_out;
nonce_params.temp_key = &tempkey;
status = atcab_nonce_base(nonce_params.mode, nonce_params.zero, nonce_params.num_in, rand_out);
if (status != ATCA_SUCCESS)
{
break;
}
status = atcah_nonce(&nonce_params);
if (status != ATCA_SUCCESS)
{
break;
}
memset(&sboot_enc_params, 0, sizeof(sboot_enc_params));
sboot_enc_params.digest = digest;
sboot_enc_params.io_key = io_key;
sboot_enc_params.temp_key = &tempkey;
sboot_enc_params.hashed_key = key;
sboot_enc_params.digest_enc = digest_enc;
status = atcah_secureboot_enc(&sboot_enc_params);
if (status != ATCA_SUCCESS)
{
break;
}
memset(&sboot_mac_params, 0, sizeof(sboot_mac_params));
sboot_mac_params.mode = mode | SECUREBOOT_MODE_ENC_MAC_FLAG;
sboot_mac_params.param2 = 0;
sboot_mac_params.hashed_key = sboot_enc_params.hashed_key;
sboot_mac_params.digest = digest;
sboot_mac_params.signature = signature;
sboot_mac_params.mac = host_mac;
status = atcab_secureboot(sboot_mac_params.mode, sboot_mac_params.param2, digest_enc, signature, mac);
if (status == ATCA_CHECKMAC_VERIFY_FAILED)
{
*is_verified = false;
status = ATCA_SUCCESS; break;
}
if (status != ATCA_SUCCESS)
{
break;
}
status = atcab_read_bytes_zone(ATCA_ZONE_CONFIG, 0, SECUREBOOTCONFIG_OFFSET, buf, 2);
if (status != ATCA_SUCCESS)
{
break;
}
sboot_mac_params.secure_boot_config = (uint16_t)buf[0] | ((uint16_t)buf[1] << 8);
status = atcah_secureboot_mac(&sboot_mac_params);
if (status != ATCA_SUCCESS)
{
break;
}
*is_verified = (memcmp(host_mac, mac, SECUREBOOT_MAC_SIZE) == 0);
}
while (0);
return status;
}