mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
180:96ed750bd169
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew 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 "us_ticker_api.h"
<> 149:156823d33999 27 #include "trng_api.h"
Anna Bridge 180:96ed750bd169 28 #include "crypto-misc.h"
<> 149:156823d33999 29
<> 149:156823d33999 30 /*
<> 149:156823d33999 31 * Get Random number generator.
<> 149:156823d33999 32 */
AnnaBridge 179:b0033dcd6934 33
AnnaBridge 179:b0033dcd6934 34 #define PRNG_KEY_SIZE (0x20UL)
AnnaBridge 179:b0033dcd6934 35
<> 149:156823d33999 36 static void trng_get(unsigned char *pConversionData)
<> 149:156823d33999 37 {
<> 149:156823d33999 38 uint32_t *p32ConversionData;
<> 149:156823d33999 39
<> 149:156823d33999 40 p32ConversionData = (uint32_t *)pConversionData;
<> 149:156823d33999 41
<> 149:156823d33999 42 PRNG_Open(PRNG_KEY_SIZE_256, 1, us_ticker_read());
Anna Bridge 180:96ed750bd169 43 crypto_prng_prestart();
<> 149:156823d33999 44 PRNG_Start();
Anna Bridge 180:96ed750bd169 45 crypto_prng_wait();
<> 149:156823d33999 46
<> 149:156823d33999 47 PRNG_Read(p32ConversionData);
<> 149:156823d33999 48 }
<> 149:156823d33999 49
<> 149:156823d33999 50 void trng_init(trng_t *obj)
<> 149:156823d33999 51 {
<> 149:156823d33999 52 (void)obj;
<> 149:156823d33999 53
Anna Bridge 180:96ed750bd169 54 /* Init crypto module */
Anna Bridge 180:96ed750bd169 55 crypto_init();
<> 149:156823d33999 56
<> 149:156823d33999 57 PRNG_ENABLE_INT();
<> 149:156823d33999 58 }
<> 149:156823d33999 59
<> 149:156823d33999 60 void trng_free(trng_t *obj)
<> 149:156823d33999 61 {
<> 149:156823d33999 62 (void)obj;
Anna Bridge 180:96ed750bd169 63
<> 149:156823d33999 64 PRNG_DISABLE_INT();
Anna Bridge 180:96ed750bd169 65
Anna Bridge 180:96ed750bd169 66 /* Uninit crypto module */
Anna Bridge 180:96ed750bd169 67 crypto_uninit();
<> 149:156823d33999 68 }
<> 149:156823d33999 69
<> 149:156823d33999 70 int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
<> 149:156823d33999 71 {
<> 149:156823d33999 72 (void)obj;
AnnaBridge 179:b0033dcd6934 73 unsigned char tmpBuff[PRNG_KEY_SIZE];
AnnaBridge 179:b0033dcd6934 74 size_t cur_length = 0;
AnnaBridge 179:b0033dcd6934 75
AnnaBridge 179:b0033dcd6934 76 while (length >= sizeof(tmpBuff)) {
AnnaBridge 179:b0033dcd6934 77 trng_get(output);
AnnaBridge 179:b0033dcd6934 78 output += sizeof(tmpBuff);
AnnaBridge 179:b0033dcd6934 79 cur_length += sizeof(tmpBuff);
AnnaBridge 179:b0033dcd6934 80 length -= sizeof(tmpBuff);
AnnaBridge 179:b0033dcd6934 81 }
AnnaBridge 179:b0033dcd6934 82 if (length > 0) {
<> 149:156823d33999 83 trng_get(tmpBuff);
AnnaBridge 179:b0033dcd6934 84 memcpy(output, tmpBuff, length);
AnnaBridge 179:b0033dcd6934 85 cur_length += length;
Anna Bridge 180:96ed750bd169 86 crypto_zeroize(tmpBuff, sizeof(tmpBuff));
<> 149:156823d33999 87 }
AnnaBridge 179:b0033dcd6934 88 *output_length = cur_length;
<> 149:156823d33999 89 return 0;
<> 149:156823d33999 90 }
Anna Bridge 180:96ed750bd169 91
<> 149:156823d33999 92 #endif