mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Dec 07 14:01:42 2017 +0000
Revision:
179:b0033dcd6934
Parent:
172:7d866c31b3c5
Child:
180:96ed750bd169
mbed-dev library. Release version 157

Who changed what in which revision?

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