#include "crypto_module.h"

#include <string.h>
#include <tee_internal_api.h>
#include <tees_kdf.h>

#ifndef NULL
#define NULL '\0'
#endif

#if AP_ID_SIZE * 2 != SHA256_DIGEST_SIZE
#   error Bad apId derivation algorithm
#endif

CRYPTO_STATUS crypto_gen_random(uint8_t *out, uint32_t size) {
    uint32_t desired;

    while (size != 0) {
        desired = size;
        TEE_GenerateRandom( out, desired );

        out += desired;
        size -= desired;
    }

    return CRYPTO_STATUS_SUCCESS;
}

#if !defined(USE_TEEGRIS) && !defined(USE_TEEGRIS_V4)
char* strtok(const char *string, const char *strCharSet) {
    static char *nextTokenPos;
    char *curTokenPos;
    char *curStrPos;
 
    if (string != NULL) {
        curTokenPos = string;
        curStrPos = string;
        nextTokenPos = (char*)-1;
    } else {
        curTokenPos = nextTokenPos;
        curStrPos = nextTokenPos;
    }
 
    if (nextTokenPos == NULL) return NULL;
    while (1) {
        if (*curStrPos == 0) {
            nextTokenPos = NULL;
            break;
        }
        if (*curStrPos == *strCharSet) {
            *curStrPos = 0;
            nextTokenPos = curStrPos + 1;
            break;
        }
        curStrPos++;
    }
    return curTokenPos;
}
#endif
