2016-07-27 14:15:07 +00:00
|
|
|
/*
|
|
|
|
* Hardware entropy collector for NUC472's RNGA
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-09-22 10:41:58 +00:00
|
|
|
#if DEVICE_TRNG
|
2016-07-27 14:15:07 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2016-08-01 07:40:08 +00:00
|
|
|
#include <string.h>
|
2016-07-27 14:15:07 +00:00
|
|
|
#include "cmsis.h"
|
|
|
|
#include "NUC472_442.h"
|
2016-08-01 07:40:08 +00:00
|
|
|
#include "us_ticker_api.h"
|
2016-09-21 15:44:19 +00:00
|
|
|
#include "trng_api.h"
|
2016-07-27 14:15:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get Random number generator.
|
|
|
|
*/
|
|
|
|
static volatile int g_PRNG_done;
|
2017-03-21 03:14:58 +00:00
|
|
|
volatile int g_AES_done;
|
2016-07-27 14:15:07 +00:00
|
|
|
|
|
|
|
void CRYPTO_IRQHandler()
|
|
|
|
{
|
|
|
|
if (PRNG_GET_INT_FLAG()) {
|
|
|
|
g_PRNG_done = 1;
|
|
|
|
PRNG_CLR_INT_FLAG();
|
2017-03-30 01:17:35 +00:00
|
|
|
} else if (AES_GET_INT_FLAG()) {
|
2017-03-21 03:14:58 +00:00
|
|
|
g_AES_done = 1;
|
2017-03-30 01:17:35 +00:00
|
|
|
AES_CLR_INT_FLAG();
|
2016-07-27 14:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 15:44:19 +00:00
|
|
|
static void trng_get(unsigned char *pConversionData)
|
2016-07-27 14:15:07 +00:00
|
|
|
{
|
2016-09-21 12:05:04 +00:00
|
|
|
uint32_t *p32ConversionData;
|
2016-08-01 07:40:08 +00:00
|
|
|
|
2016-09-21 12:05:04 +00:00
|
|
|
p32ConversionData = (uint32_t *)pConversionData;
|
2016-09-19 13:33:06 +00:00
|
|
|
|
2016-09-21 12:05:04 +00:00
|
|
|
PRNG_Open(PRNG_KEY_SIZE_256, 1, us_ticker_read());
|
2016-07-27 14:15:07 +00:00
|
|
|
PRNG_Start();
|
|
|
|
while (!g_PRNG_done);
|
|
|
|
|
|
|
|
PRNG_Read(p32ConversionData);
|
2016-09-19 13:33:06 +00:00
|
|
|
}
|
2016-07-27 14:15:07 +00:00
|
|
|
|
2016-09-21 15:44:19 +00:00
|
|
|
void trng_init(trng_t *obj)
|
2016-09-19 13:33:06 +00:00
|
|
|
{
|
2016-09-21 12:00:43 +00:00
|
|
|
(void)obj;
|
2016-09-19 13:33:06 +00:00
|
|
|
/* Unlock protected registers */
|
|
|
|
SYS_UnlockReg();
|
|
|
|
/* Enable IP clock */
|
|
|
|
CLK_EnableModuleClock(CRPT_MODULE);
|
|
|
|
|
|
|
|
/* Lock protected registers */
|
|
|
|
SYS_LockReg();
|
|
|
|
|
|
|
|
NVIC_EnableIRQ(CRPT_IRQn);
|
|
|
|
PRNG_ENABLE_INT();
|
|
|
|
}
|
|
|
|
|
2016-09-21 15:44:19 +00:00
|
|
|
void trng_free(trng_t *obj)
|
2016-09-19 13:33:06 +00:00
|
|
|
{
|
2016-09-21 12:00:43 +00:00
|
|
|
(void)obj;
|
2016-07-27 14:15:07 +00:00
|
|
|
PRNG_DISABLE_INT();
|
|
|
|
NVIC_DisableIRQ(CRPT_IRQn);
|
|
|
|
}
|
|
|
|
|
2016-09-21 15:44:19 +00:00
|
|
|
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
|
2016-07-27 14:15:07 +00:00
|
|
|
{
|
2016-09-21 12:00:43 +00:00
|
|
|
(void)obj;
|
|
|
|
|
2016-09-19 13:33:06 +00:00
|
|
|
*output_length = 0;
|
|
|
|
if (length < 32) {
|
|
|
|
unsigned char tmpBuff[32];
|
2016-09-21 15:44:19 +00:00
|
|
|
trng_get(tmpBuff);
|
2016-09-21 12:05:04 +00:00
|
|
|
memcpy(output, &tmpBuff, length);
|
2016-09-19 13:33:06 +00:00
|
|
|
*output_length = length;
|
|
|
|
} else {
|
2017-03-21 03:14:58 +00:00
|
|
|
for (int i = 0; i < (length/32); i++) {
|
2016-09-21 15:44:19 +00:00
|
|
|
trng_get(output);
|
2016-09-19 13:33:06 +00:00
|
|
|
*output_length += 32;
|
|
|
|
output += 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2016-07-27 14:15:07 +00:00
|
|
|
}
|
|
|
|
|
2016-09-22 10:41:58 +00:00
|
|
|
#endif
|
2016-07-27 14:15:07 +00:00
|
|
|
|