/*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*

sec_main.c

GENERAL DESCRIPTION
 Contains implementation of Authhat Module

*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include "sec_main.h"
#include "sec_common.h"
#include "sec_templ.h"

int32_t authhat_pre_enroll(UINT32 tz_type, uint64_t* challenge, UINT8* encryptedChallenge, UINT32* encryptedChallengeLen) {
    int32_t result = AUTHHAT_RESULT_SUCCESS;
    UINT8 challengeArray[AUTHHAT_SUBMODULE_CHALLENGE_LEN] = {0, };

    PAL_DbgLog("authhat_pre_enroll");

    result = PAL_generate_randBytes(challengeArray, AUTHHAT_SUBMODULE_CHALLENGE_LEN);
    if (result != AUTHHAT_RESULT_SUCCESS) {
        PAL_DbgLog("authhat_pre_enroll - PAL_generate_randBytes failed");
        return AUTHHAT_RESULT_FAIL_PRE_ENROLL_COMMAND;
    }

    array_to_integer64(challenge, challengeArray, 0);
    if ((*challenge == 0) || (*challenge == 1)) {
        PAL_DbgLog("authhat_pre_enroll - generated challenge : %u", (int) *challenge);
    }

    /* encrypt challenge */
    result = authhat_encode_metadata(tz_type, (UINT8 *) challenge, AUTHHAT_SUBMODULE_META_DATA_CHALLENGE_LEN, encryptedChallenge, encryptedChallengeLen);
    if (result != AUTHHAT_RESULT_SUCCESS) {
        PAL_DbgLog("authhat_pre_enroll failed");
        result = AUTHHAT_RESULT_FAIL_PRE_ENROLL_COMMAND;
    }

    return result;
}

