mbed
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_M480/trng_api.c@180:b0033dcd6934, 2017-12-07 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Dec 07 14:01:42 2017 +0000
- Revision:
- 180:b0033dcd6934
- Parent:
- 173:7d866c31b3c5
- Child:
- 181:96ed750bd169
mbed-dev library. Release version 157
Who changed what in which revision?
User | Revision | Line number | New 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 | 180:b0033dcd6934 | 28 | |
AnnaBridge | 180:b0033dcd6934 | 29 | #define PRNG_KEY_SIZE (0x20UL) |
AnnaBridge | 180:b0033dcd6934 | 30 | |
AnnaBridge | 173:7d866c31b3c5 | 31 | static volatile int g_PRNG_done; |
AnnaBridge | 173:7d866c31b3c5 | 32 | volatile int g_AES_done; |
AnnaBridge | 173:7d866c31b3c5 | 33 | |
AnnaBridge | 180:b0033dcd6934 | 34 | /* Implementation that should never be optimized out by the compiler */ |
AnnaBridge | 180:b0033dcd6934 | 35 | static void trng_zeroize( void *v, size_t n ) { |
AnnaBridge | 180:b0033dcd6934 | 36 | volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
AnnaBridge | 180:b0033dcd6934 | 37 | } |
AnnaBridge | 180:b0033dcd6934 | 38 | |
AnnaBridge | 173:7d866c31b3c5 | 39 | void CRYPTO_IRQHandler() |
AnnaBridge | 173:7d866c31b3c5 | 40 | { |
AnnaBridge | 173:7d866c31b3c5 | 41 | if (PRNG_GET_INT_FLAG()) { |
AnnaBridge | 173:7d866c31b3c5 | 42 | g_PRNG_done = 1; |
AnnaBridge | 173:7d866c31b3c5 | 43 | PRNG_CLR_INT_FLAG(); |
AnnaBridge | 173:7d866c31b3c5 | 44 | } else if (AES_GET_INT_FLAG()) { |
AnnaBridge | 173:7d866c31b3c5 | 45 | g_AES_done = 1; |
AnnaBridge | 173:7d866c31b3c5 | 46 | AES_CLR_INT_FLAG(); |
AnnaBridge | 173:7d866c31b3c5 | 47 | } |
AnnaBridge | 173:7d866c31b3c5 | 48 | } |
AnnaBridge | 173:7d866c31b3c5 | 49 | |
AnnaBridge | 173:7d866c31b3c5 | 50 | static void trng_get(unsigned char *pConversionData) |
AnnaBridge | 173:7d866c31b3c5 | 51 | { |
AnnaBridge | 173:7d866c31b3c5 | 52 | uint32_t *p32ConversionData; |
AnnaBridge | 173:7d866c31b3c5 | 53 | |
AnnaBridge | 173:7d866c31b3c5 | 54 | p32ConversionData = (uint32_t *)pConversionData; |
AnnaBridge | 173:7d866c31b3c5 | 55 | |
AnnaBridge | 173:7d866c31b3c5 | 56 | PRNG_Open(PRNG_KEY_SIZE_256, 1, us_ticker_read()); |
AnnaBridge | 173:7d866c31b3c5 | 57 | PRNG_Start(); |
AnnaBridge | 173:7d866c31b3c5 | 58 | while (!g_PRNG_done); |
AnnaBridge | 173:7d866c31b3c5 | 59 | |
AnnaBridge | 173:7d866c31b3c5 | 60 | PRNG_Read(p32ConversionData); |
AnnaBridge | 173:7d866c31b3c5 | 61 | } |
AnnaBridge | 173:7d866c31b3c5 | 62 | |
AnnaBridge | 173:7d866c31b3c5 | 63 | void trng_init(trng_t *obj) |
AnnaBridge | 173:7d866c31b3c5 | 64 | { |
AnnaBridge | 173:7d866c31b3c5 | 65 | (void)obj; |
AnnaBridge | 173:7d866c31b3c5 | 66 | /* Unlock protected registers */ |
AnnaBridge | 173:7d866c31b3c5 | 67 | SYS_UnlockReg(); |
AnnaBridge | 173:7d866c31b3c5 | 68 | /* Enable IP clock */ |
AnnaBridge | 173:7d866c31b3c5 | 69 | CLK_EnableModuleClock(CRPT_MODULE); |
AnnaBridge | 173:7d866c31b3c5 | 70 | |
AnnaBridge | 173:7d866c31b3c5 | 71 | /* Lock protected registers */ |
AnnaBridge | 173:7d866c31b3c5 | 72 | SYS_LockReg(); |
AnnaBridge | 173:7d866c31b3c5 | 73 | |
AnnaBridge | 173:7d866c31b3c5 | 74 | NVIC_EnableIRQ(CRPT_IRQn); |
AnnaBridge | 173:7d866c31b3c5 | 75 | PRNG_ENABLE_INT(); |
AnnaBridge | 173:7d866c31b3c5 | 76 | } |
AnnaBridge | 173:7d866c31b3c5 | 77 | |
AnnaBridge | 173:7d866c31b3c5 | 78 | void trng_free(trng_t *obj) |
AnnaBridge | 173:7d866c31b3c5 | 79 | { |
AnnaBridge | 173:7d866c31b3c5 | 80 | (void)obj; |
AnnaBridge | 173:7d866c31b3c5 | 81 | PRNG_DISABLE_INT(); |
AnnaBridge | 173:7d866c31b3c5 | 82 | NVIC_DisableIRQ(CRPT_IRQn); |
AnnaBridge | 173:7d866c31b3c5 | 83 | } |
AnnaBridge | 173:7d866c31b3c5 | 84 | |
AnnaBridge | 173:7d866c31b3c5 | 85 | int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) |
AnnaBridge | 173:7d866c31b3c5 | 86 | { |
AnnaBridge | 173:7d866c31b3c5 | 87 | (void)obj; |
AnnaBridge | 180:b0033dcd6934 | 88 | unsigned char tmpBuff[PRNG_KEY_SIZE]; |
AnnaBridge | 180:b0033dcd6934 | 89 | size_t cur_length = 0; |
AnnaBridge | 180:b0033dcd6934 | 90 | |
AnnaBridge | 180:b0033dcd6934 | 91 | while (length >= sizeof(tmpBuff)) { |
AnnaBridge | 180:b0033dcd6934 | 92 | trng_get(output); |
AnnaBridge | 180:b0033dcd6934 | 93 | output += sizeof(tmpBuff); |
AnnaBridge | 180:b0033dcd6934 | 94 | cur_length += sizeof(tmpBuff); |
AnnaBridge | 180:b0033dcd6934 | 95 | length -= sizeof(tmpBuff); |
AnnaBridge | 180:b0033dcd6934 | 96 | } |
AnnaBridge | 180:b0033dcd6934 | 97 | if (length > 0) { |
AnnaBridge | 173:7d866c31b3c5 | 98 | trng_get(tmpBuff); |
AnnaBridge | 180:b0033dcd6934 | 99 | memcpy(output, tmpBuff, length); |
AnnaBridge | 180:b0033dcd6934 | 100 | cur_length += length; |
AnnaBridge | 180:b0033dcd6934 | 101 | trng_zeroize(tmpBuff, sizeof(tmpBuff)); |
AnnaBridge | 173:7d866c31b3c5 | 102 | } |
AnnaBridge | 180:b0033dcd6934 | 103 | *output_length = cur_length; |
AnnaBridge | 173:7d866c31b3c5 | 104 | return 0; |
AnnaBridge | 173:7d866c31b3c5 | 105 | } |
AnnaBridge | 173:7d866c31b3c5 | 106 | |
AnnaBridge | 173:7d866c31b3c5 | 107 | #endif |
AnnaBridge | 173:7d866c31b3c5 | 108 |