#include <platform.h>
#ifdef PLATFORM_NO_KERNEL
#include "common/interface/random.h"
#endif /* PLATFORM_NO_KERNEL */
#include "mbedtls_config.h"

// Platform-specific implementations

#ifdef __cplusplus
extern "C" {
#endif
    void * innovaphone_mbedtls_calloc(size_t num, size_t size)
    {
#if defined(MODULE_TEST) | defined(PLATFORM_NO_BUFMAN)
        void * ptr = calloc(num, size);
        memset(ptr, 0, num * size);
        return ptr;
#else /* PLATFORM_NO_BUFMAN */
        void * ptr = bufman->alloc(num * size);
        memset(ptr, 0, num * size);
        return ptr;
#endif /* PLATFORM_NO_BUFMAN */
    }

    void innovaphone_mbedtls_free(void * ptr)
    {
#if defined(MODULE_TEST) | defined(PLATFORM_NO_BUFMAN)
        free(ptr);
#else /* PLATFORM_NO_BUFMAN */
        bufman->free_secure(ptr);
#endif /* PLATFORM_NO_BUFMAN */
    }

    int innovaphone_mbedtls_get_random_bytes(void * data, unsigned char * output, size_t len)
    {
#if defined(MODULE_TEST)
        platform_random_bytes_secure((byte *)output, len);
#elif defined(PLATFORM_NO_KERNEL)
        IRandom::GetBytes((byte *)output, (dword)len);
#else  /* PLATFORM_NO_KERNEL */
        kernel->get_random_bytes((byte *)output, len);
#endif /* PLATFORM_NO_KERNEL */
        return 0;
    }

    void innovaphone_mbedtls_zeroize(void * data, size_t len)
    {
        memset(data, 0, len);
    }

#ifdef __cplusplus
}
#endif
