mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_NUC472/trng_api.c@163:74e0ce7f98e8, 2017-04-28 (annotated)
- Committer:
- Anna Bridge
- Date:
- Fri Apr 28 14:04:18 2017 +0100
- Revision:
- 163:74e0ce7f98e8
- Parent:
- 153:fa9ff456f731
- Child:
- 179:b0033dcd6934
This updates the lib to the mbed lib v141
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 | */ |
<> | 149:156823d33999 | 33 | static volatile int g_PRNG_done; |
Anna Bridge |
163:74e0ce7f98e8 | 34 | volatile int g_AES_done; |
<> | 149:156823d33999 | 35 | |
<> | 149:156823d33999 | 36 | void CRYPTO_IRQHandler() |
<> | 149:156823d33999 | 37 | { |
<> | 149:156823d33999 | 38 | if (PRNG_GET_INT_FLAG()) { |
<> | 149:156823d33999 | 39 | g_PRNG_done = 1; |
<> | 149:156823d33999 | 40 | PRNG_CLR_INT_FLAG(); |
Anna Bridge |
163:74e0ce7f98e8 | 41 | } else if (AES_GET_INT_FLAG()) { |
Anna Bridge |
163:74e0ce7f98e8 | 42 | g_AES_done = 1; |
Anna Bridge |
163:74e0ce7f98e8 | 43 | AES_CLR_INT_FLAG(); |
<> | 149:156823d33999 | 44 | } |
<> | 149:156823d33999 | 45 | } |
<> | 149:156823d33999 | 46 | |
<> | 149:156823d33999 | 47 | static void trng_get(unsigned char *pConversionData) |
<> | 149:156823d33999 | 48 | { |
<> | 149:156823d33999 | 49 | uint32_t *p32ConversionData; |
<> | 149:156823d33999 | 50 | |
<> | 149:156823d33999 | 51 | p32ConversionData = (uint32_t *)pConversionData; |
<> | 149:156823d33999 | 52 | |
<> | 149:156823d33999 | 53 | PRNG_Open(PRNG_KEY_SIZE_256, 1, us_ticker_read()); |
<> | 149:156823d33999 | 54 | PRNG_Start(); |
<> | 149:156823d33999 | 55 | while (!g_PRNG_done); |
<> | 149:156823d33999 | 56 | |
<> | 149:156823d33999 | 57 | PRNG_Read(p32ConversionData); |
<> | 149:156823d33999 | 58 | } |
<> | 149:156823d33999 | 59 | |
<> | 149:156823d33999 | 60 | void trng_init(trng_t *obj) |
<> | 149:156823d33999 | 61 | { |
<> | 149:156823d33999 | 62 | (void)obj; |
<> | 149:156823d33999 | 63 | /* Unlock protected registers */ |
<> | 149:156823d33999 | 64 | SYS_UnlockReg(); |
<> | 149:156823d33999 | 65 | /* Enable IP clock */ |
<> | 149:156823d33999 | 66 | CLK_EnableModuleClock(CRPT_MODULE); |
<> | 149:156823d33999 | 67 | |
<> | 149:156823d33999 | 68 | /* Lock protected registers */ |
<> | 149:156823d33999 | 69 | SYS_LockReg(); |
<> | 149:156823d33999 | 70 | |
<> | 149:156823d33999 | 71 | NVIC_EnableIRQ(CRPT_IRQn); |
<> | 149:156823d33999 | 72 | PRNG_ENABLE_INT(); |
<> | 149:156823d33999 | 73 | } |
<> | 149:156823d33999 | 74 | |
<> | 149:156823d33999 | 75 | void trng_free(trng_t *obj) |
<> | 149:156823d33999 | 76 | { |
<> | 149:156823d33999 | 77 | (void)obj; |
<> | 149:156823d33999 | 78 | PRNG_DISABLE_INT(); |
<> | 149:156823d33999 | 79 | NVIC_DisableIRQ(CRPT_IRQn); |
<> | 149:156823d33999 | 80 | } |
<> | 149:156823d33999 | 81 | |
<> | 149:156823d33999 | 82 | int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) |
<> | 149:156823d33999 | 83 | { |
<> | 149:156823d33999 | 84 | (void)obj; |
<> | 149:156823d33999 | 85 | |
<> | 149:156823d33999 | 86 | *output_length = 0; |
<> | 149:156823d33999 | 87 | if (length < 32) { |
<> | 149:156823d33999 | 88 | unsigned char tmpBuff[32]; |
<> | 149:156823d33999 | 89 | trng_get(tmpBuff); |
<> | 149:156823d33999 | 90 | memcpy(output, &tmpBuff, length); |
<> | 149:156823d33999 | 91 | *output_length = length; |
<> | 149:156823d33999 | 92 | } else { |
Anna Bridge |
163:74e0ce7f98e8 | 93 | for (int i = 0; i < (length/32); i++) { |
<> | 149:156823d33999 | 94 | trng_get(output); |
<> | 149:156823d33999 | 95 | *output_length += 32; |
<> | 149:156823d33999 | 96 | output += 32; |
<> | 149:156823d33999 | 97 | } |
<> | 149:156823d33999 | 98 | } |
<> | 149:156823d33999 | 99 | |
<> | 149:156823d33999 | 100 | return 0; |
<> | 149:156823d33999 | 101 | } |
<> | 149:156823d33999 | 102 | |
<> | 149:156823d33999 | 103 | #endif |
<> | 149:156823d33999 | 104 |