mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*
elessair 0:f269e3021894 2 * Hardware entropy collector for NUC472's RNGA
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
elessair 0:f269e3021894 5 * SPDX-License-Identifier: Apache-2.0
elessair 0:f269e3021894 6 *
elessair 0:f269e3021894 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
elessair 0:f269e3021894 8 * not use this file except in compliance with the License.
elessair 0:f269e3021894 9 * You may obtain a copy of the License at
elessair 0:f269e3021894 10 *
elessair 0:f269e3021894 11 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 12 *
elessair 0:f269e3021894 13 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
elessair 0:f269e3021894 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 16 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 17 * limitations under the License.
elessair 0:f269e3021894 18 *
elessair 0:f269e3021894 19 */
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 #if DEVICE_TRNG
elessair 0:f269e3021894 22
elessair 0:f269e3021894 23 #include <stdlib.h>
elessair 0:f269e3021894 24 #include <string.h>
elessair 0:f269e3021894 25 #include "cmsis.h"
elessair 0:f269e3021894 26 #include "NUC472_442.h"
elessair 0:f269e3021894 27 #include "us_ticker_api.h"
elessair 0:f269e3021894 28 #include "trng_api.h"
elessair 0:f269e3021894 29
elessair 0:f269e3021894 30 /*
elessair 0:f269e3021894 31 * Get Random number generator.
elessair 0:f269e3021894 32 */
elessair 0:f269e3021894 33 static volatile int g_PRNG_done;
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 void CRYPTO_IRQHandler()
elessair 0:f269e3021894 36 {
elessair 0:f269e3021894 37 if (PRNG_GET_INT_FLAG()) {
elessair 0:f269e3021894 38 g_PRNG_done = 1;
elessair 0:f269e3021894 39 PRNG_CLR_INT_FLAG();
elessair 0:f269e3021894 40 }
elessair 0:f269e3021894 41 }
elessair 0:f269e3021894 42
elessair 0:f269e3021894 43 static void trng_get(unsigned char *pConversionData)
elessair 0:f269e3021894 44 {
elessair 0:f269e3021894 45 uint32_t *p32ConversionData;
elessair 0:f269e3021894 46
elessair 0:f269e3021894 47 p32ConversionData = (uint32_t *)pConversionData;
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49 PRNG_Open(PRNG_KEY_SIZE_256, 1, us_ticker_read());
elessair 0:f269e3021894 50 PRNG_Start();
elessair 0:f269e3021894 51 while (!g_PRNG_done);
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53 PRNG_Read(p32ConversionData);
elessair 0:f269e3021894 54 }
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 void trng_init(trng_t *obj)
elessair 0:f269e3021894 57 {
elessair 0:f269e3021894 58 (void)obj;
elessair 0:f269e3021894 59 /* Unlock protected registers */
elessair 0:f269e3021894 60 SYS_UnlockReg();
elessair 0:f269e3021894 61 /* Enable IP clock */
elessair 0:f269e3021894 62 CLK_EnableModuleClock(CRPT_MODULE);
elessair 0:f269e3021894 63
elessair 0:f269e3021894 64 /* Lock protected registers */
elessair 0:f269e3021894 65 SYS_LockReg();
elessair 0:f269e3021894 66
elessair 0:f269e3021894 67 NVIC_EnableIRQ(CRPT_IRQn);
elessair 0:f269e3021894 68 PRNG_ENABLE_INT();
elessair 0:f269e3021894 69 }
elessair 0:f269e3021894 70
elessair 0:f269e3021894 71 void trng_free(trng_t *obj)
elessair 0:f269e3021894 72 {
elessair 0:f269e3021894 73 (void)obj;
elessair 0:f269e3021894 74 PRNG_DISABLE_INT();
elessair 0:f269e3021894 75 NVIC_DisableIRQ(CRPT_IRQn);
elessair 0:f269e3021894 76 }
elessair 0:f269e3021894 77
elessair 0:f269e3021894 78 int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
elessair 0:f269e3021894 79 {
elessair 0:f269e3021894 80 (void)obj;
elessair 0:f269e3021894 81
elessair 0:f269e3021894 82 *output_length = 0;
elessair 0:f269e3021894 83 if (length < 32) {
elessair 0:f269e3021894 84 unsigned char tmpBuff[32];
elessair 0:f269e3021894 85 trng_get(tmpBuff);
elessair 0:f269e3021894 86 memcpy(output, &tmpBuff, length);
elessair 0:f269e3021894 87 *output_length = length;
elessair 0:f269e3021894 88 } else {
elessair 0:f269e3021894 89 for (int i = 0; i < (length/32); i++) {
elessair 0:f269e3021894 90 trng_get(output);
elessair 0:f269e3021894 91 *output_length += 32;
elessair 0:f269e3021894 92 output += 32;
elessair 0:f269e3021894 93 }
elessair 0:f269e3021894 94 }
elessair 0:f269e3021894 95
elessair 0:f269e3021894 96 return 0;
elessair 0:f269e3021894 97 }
elessair 0:f269e3021894 98
elessair 0:f269e3021894 99 #endif
elessair 0:f269e3021894 100