2016-06-23 00:53:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 ARM Limited.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2016-11-20 07:46:22 +08:00
|
|
|
#include <device.h>
|
2017-10-14 07:30:55 +08:00
|
|
|
#include <entropy.h>
|
2017-10-14 06:45:02 +08:00
|
|
|
#include <random/rand32.h>
|
2016-12-07 05:58:49 +08:00
|
|
|
#include <init.h>
|
2016-06-23 00:53:28 +08:00
|
|
|
|
|
|
|
#include "fsl_rnga.h"
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static u8_t entropy_mcux_rnga_get_uint8(void)
|
2016-11-20 07:46:22 +08:00
|
|
|
{
|
2017-04-21 23:03:20 +08:00
|
|
|
u32_t random;
|
|
|
|
u8_t output = 0;
|
2016-11-20 07:46:22 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
RNGA_SetMode(RNG, kRNGA_ModeNormal);
|
|
|
|
/* The Reference manual states that back to back reads from
|
|
|
|
* the RNGA deliver one or two bits of entropy per 32-bit
|
|
|
|
* word, therefore to deliver 8 bits of entropy we need
|
|
|
|
* between 4 and 8 samples. Conservatively, we take 8.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
status = RNGA_GetRandomData(RNG, &random, sizeof(random));
|
|
|
|
__ASSERT_NO_MSG(!status);
|
|
|
|
output ^= random;
|
|
|
|
}
|
|
|
|
|
|
|
|
RNGA_SetMode(RNG, kRNGA_ModeSleep);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_mcux_rnga_get_entropy(struct device *dev, u8_t *buffer,
|
2017-04-21 23:03:20 +08:00
|
|
|
u16_t length)
|
2016-11-20 07:46:22 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++) {
|
2017-10-14 07:30:55 +08:00
|
|
|
buffer[i] = entropy_mcux_rnga_get_uint8();
|
2016-11-20 07:46:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static const struct entropy_driver_api entropy_mcux_rnga_api_funcs = {
|
|
|
|
.get_entropy = entropy_mcux_rnga_get_entropy
|
2016-11-20 07:46:22 +08:00
|
|
|
};
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_mcux_rnga_init(struct device *);
|
2016-11-20 07:46:22 +08:00
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
DEVICE_AND_API_INIT(entropy_mcux_rnga, CONFIG_ENTROPY_NAME,
|
|
|
|
entropy_mcux_rnga_init, NULL, NULL,
|
2016-11-20 07:46:22 +08:00
|
|
|
PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
|
2017-10-14 07:30:55 +08:00
|
|
|
&entropy_mcux_rnga_api_funcs);
|
2016-06-23 00:53:28 +08:00
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_mcux_rnga_init(struct device *dev)
|
2016-06-23 00:53:28 +08:00
|
|
|
{
|
2017-04-21 23:03:20 +08:00
|
|
|
u32_t seed = k_cycle_get_32();
|
2016-06-23 00:53:28 +08:00
|
|
|
|
2016-12-07 05:58:49 +08:00
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2016-06-23 00:53:28 +08:00
|
|
|
RNGA_Init(RNG);
|
|
|
|
|
|
|
|
/* The range of seed values acquired by this method is likely
|
|
|
|
* to be relatively small. The RNGA hardware uses two free
|
|
|
|
* running oscillators to add entropy to the seed value, we
|
|
|
|
* take care below to ensure the read rate is lower than the
|
|
|
|
* rate at which the hardware can add entropy.
|
|
|
|
*/
|
|
|
|
RNGA_Seed(RNG, seed);
|
|
|
|
RNGA_SetMode(RNG, kRNGA_ModeSleep);
|
2016-12-07 05:58:49 +08:00
|
|
|
return 0;
|
2016-06-23 00:53:28 +08:00
|
|
|
}
|