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