mbed

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Aug 31 17:27:04 2017 +0100
Revision:
173:7d866c31b3c5
Child:
180:b0033dcd6934
This updates the lib to the mbed lib v 150

Who changed what in which revision?

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