mbed

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Wed Jan 17 15:23:54 2018 +0000
Revision:
181:96ed750bd169
Parent:
180:b0033dcd6934
mbed-dev libray. Release version 158

Who changed what in which revision?

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