helpfor studient
Dependents: STM32_F103-C8T6basecanblink_led
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_NUC472/trng_api.c@180:b0033dcd6934, 2017-12-07 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Dec 07 14:01:42 2017 +0000
- Revision:
- 180:b0033dcd6934
- Parent:
- 163:74e0ce7f98e8
- Child:
- 181:96ed750bd169
mbed-dev library. Release version 157
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* |
<> | 149:156823d33999 | 2 | * Hardware entropy collector for NUC472's RNGA |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
<> | 149:156823d33999 | 5 | * SPDX-License-Identifier: Apache-2.0 |
<> | 149:156823d33999 | 6 | * |
<> | 149:156823d33999 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
<> | 149:156823d33999 | 8 | * not use this file except in compliance with the License. |
<> | 149:156823d33999 | 9 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 10 | * |
<> | 149:156823d33999 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 12 | * |
<> | 149:156823d33999 | 13 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
<> | 149:156823d33999 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 16 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 17 | * limitations under the License. |
<> | 149:156823d33999 | 18 | * |
<> | 149:156823d33999 | 19 | */ |
<> | 149:156823d33999 | 20 | |
<> | 149:156823d33999 | 21 | #if DEVICE_TRNG |
<> | 149:156823d33999 | 22 | |
<> | 149:156823d33999 | 23 | #include <stdlib.h> |
<> | 149:156823d33999 | 24 | #include <string.h> |
<> | 149:156823d33999 | 25 | #include "cmsis.h" |
<> | 149:156823d33999 | 26 | #include "NUC472_442.h" |
<> | 149:156823d33999 | 27 | #include "us_ticker_api.h" |
<> | 149:156823d33999 | 28 | #include "trng_api.h" |
<> | 149:156823d33999 | 29 | |
<> | 149:156823d33999 | 30 | /* |
<> | 149:156823d33999 | 31 | * Get Random number generator. |
<> | 149:156823d33999 | 32 | */ |
AnnaBridge | 180:b0033dcd6934 | 33 | |
AnnaBridge | 180:b0033dcd6934 | 34 | #define PRNG_KEY_SIZE (0x20UL) |
AnnaBridge | 180:b0033dcd6934 | 35 | |
<> | 149:156823d33999 | 36 | static volatile int g_PRNG_done; |
Anna Bridge |
163:74e0ce7f98e8 | 37 | volatile int g_AES_done; |
<> | 149:156823d33999 | 38 | |
AnnaBridge | 180:b0033dcd6934 | 39 | /* Implementation that should never be optimized out by the compiler */ |
AnnaBridge | 180:b0033dcd6934 | 40 | static void trng_zeroize( void *v, size_t n ) { |
AnnaBridge | 180:b0033dcd6934 | 41 | volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
AnnaBridge | 180:b0033dcd6934 | 42 | } |
AnnaBridge | 180:b0033dcd6934 | 43 | |
<> | 149:156823d33999 | 44 | void CRYPTO_IRQHandler() |
<> | 149:156823d33999 | 45 | { |
<> | 149:156823d33999 | 46 | if (PRNG_GET_INT_FLAG()) { |
<> | 149:156823d33999 | 47 | g_PRNG_done = 1; |
<> | 149:156823d33999 | 48 | PRNG_CLR_INT_FLAG(); |
Anna Bridge |
163:74e0ce7f98e8 | 49 | } else if (AES_GET_INT_FLAG()) { |
Anna Bridge |
163:74e0ce7f98e8 | 50 | g_AES_done = 1; |
Anna Bridge |
163:74e0ce7f98e8 | 51 | AES_CLR_INT_FLAG(); |
<> | 149:156823d33999 | 52 | } |
<> | 149:156823d33999 | 53 | } |
<> | 149:156823d33999 | 54 | |
<> | 149:156823d33999 | 55 | static void trng_get(unsigned char *pConversionData) |
<> | 149:156823d33999 | 56 | { |
<> | 149:156823d33999 | 57 | uint32_t *p32ConversionData; |
<> | 149:156823d33999 | 58 | |
<> | 149:156823d33999 | 59 | p32ConversionData = (uint32_t *)pConversionData; |
<> | 149:156823d33999 | 60 | |
<> | 149:156823d33999 | 61 | PRNG_Open(PRNG_KEY_SIZE_256, 1, us_ticker_read()); |
<> | 149:156823d33999 | 62 | PRNG_Start(); |
<> | 149:156823d33999 | 63 | while (!g_PRNG_done); |
<> | 149:156823d33999 | 64 | |
<> | 149:156823d33999 | 65 | PRNG_Read(p32ConversionData); |
<> | 149:156823d33999 | 66 | } |
<> | 149:156823d33999 | 67 | |
<> | 149:156823d33999 | 68 | void trng_init(trng_t *obj) |
<> | 149:156823d33999 | 69 | { |
<> | 149:156823d33999 | 70 | (void)obj; |
<> | 149:156823d33999 | 71 | /* Unlock protected registers */ |
<> | 149:156823d33999 | 72 | SYS_UnlockReg(); |
<> | 149:156823d33999 | 73 | /* Enable IP clock */ |
<> | 149:156823d33999 | 74 | CLK_EnableModuleClock(CRPT_MODULE); |
<> | 149:156823d33999 | 75 | |
<> | 149:156823d33999 | 76 | /* Lock protected registers */ |
<> | 149:156823d33999 | 77 | SYS_LockReg(); |
<> | 149:156823d33999 | 78 | |
<> | 149:156823d33999 | 79 | NVIC_EnableIRQ(CRPT_IRQn); |
<> | 149:156823d33999 | 80 | PRNG_ENABLE_INT(); |
<> | 149:156823d33999 | 81 | } |
<> | 149:156823d33999 | 82 | |
<> | 149:156823d33999 | 83 | void trng_free(trng_t *obj) |
<> | 149:156823d33999 | 84 | { |
<> | 149:156823d33999 | 85 | (void)obj; |
<> | 149:156823d33999 | 86 | PRNG_DISABLE_INT(); |
<> | 149:156823d33999 | 87 | NVIC_DisableIRQ(CRPT_IRQn); |
<> | 149:156823d33999 | 88 | } |
<> | 149:156823d33999 | 89 | |
<> | 149:156823d33999 | 90 | int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) |
<> | 149:156823d33999 | 91 | { |
<> | 149:156823d33999 | 92 | (void)obj; |
AnnaBridge | 180:b0033dcd6934 | 93 | unsigned char tmpBuff[PRNG_KEY_SIZE]; |
AnnaBridge | 180:b0033dcd6934 | 94 | size_t cur_length = 0; |
AnnaBridge | 180:b0033dcd6934 | 95 | |
AnnaBridge | 180:b0033dcd6934 | 96 | while (length >= sizeof(tmpBuff)) { |
AnnaBridge | 180:b0033dcd6934 | 97 | trng_get(output); |
AnnaBridge | 180:b0033dcd6934 | 98 | output += sizeof(tmpBuff); |
AnnaBridge | 180:b0033dcd6934 | 99 | cur_length += sizeof(tmpBuff); |
AnnaBridge | 180:b0033dcd6934 | 100 | length -= sizeof(tmpBuff); |
AnnaBridge | 180:b0033dcd6934 | 101 | } |
AnnaBridge | 180:b0033dcd6934 | 102 | if (length > 0) { |
<> | 149:156823d33999 | 103 | trng_get(tmpBuff); |
AnnaBridge | 180:b0033dcd6934 | 104 | memcpy(output, tmpBuff, length); |
AnnaBridge | 180:b0033dcd6934 | 105 | cur_length += length; |
AnnaBridge | 180:b0033dcd6934 | 106 | trng_zeroize(tmpBuff, sizeof(tmpBuff)); |
<> | 149:156823d33999 | 107 | } |
AnnaBridge | 180:b0033dcd6934 | 108 | *output_length = cur_length; |
<> | 149:156823d33999 | 109 | return 0; |
<> | 149:156823d33999 | 110 | } |
<> | 149:156823d33999 | 111 | |
<> | 149:156823d33999 | 112 | #endif |
<> | 149:156823d33999 | 113 |