Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers trng.c Source File

trng.c

00001  /*
00002   *  trng.c
00003   *
00004   *  Copyright (C) 2017, Arm Limited, All Rights Reserved
00005   *  SPDX-License-Identifier: Apache-2.0
00006   *
00007   *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008   *  not use this file except in compliance with the License.
00009   *  You may obtain a copy of the License at
00010   *
00011   *  http://www.apache.org/licenses/LICENSE-2.0
00012   *
00013   *  Unless required by applicable law or agreed to in writing, software
00014   *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015   *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   *  See the License for the specific language governing permissions and
00017   *  limitations under the License.
00018   *
00019   */
00020 
00021 #if defined(DEVICE_TRNG)
00022 
00023 #include <string.h>
00024 #include "trng_api.h"
00025 
00026 extern CRYS_RND_State_t      rndState;
00027 extern CRYS_RND_WorkBuff_t   rndWorkBuff;
00028 
00029 /* Implementation that should never be optimized out by the compiler */
00030 static void mbedtls_zeroize( void *v, size_t n ) {
00031     volatile unsigned char *p = (unsigned char*)v;
00032     while( n-- ) *p++ = 0;
00033 }
00034 
00035 CRYSError_t  RNG_PLAT_SetUserRngParameters(
00036                          CRYS_RND_State_t   *pRndState,
00037                          CRYS_RND_Params_t *pTrngParams);
00038 
00039 CRYSError_t  LLF_RND_GetTrngSource(
00040                          CRYS_RND_State_t   *rndState_ptr,
00041                          CRYS_RND_Params_t *trngParams_ptr,
00042                          SaSiBool_t        isContinued,
00043                          uint32_t          *entropySize_ptr,
00044                          uint32_t          **sourceOut_ptr_ptr,
00045                          uint32_t          *sourceOutSize_ptr,
00046                          uint32_t          *rndWorkBuff_ptr);
00047 
00048 
00049 void trng_init(trng_t *obj)
00050 {
00051     RNG_PLAT_SetUserRngParameters(&rndState, obj);
00052 }
00053 
00054 void trng_free(trng_t *obj)
00055 {
00056     (void)obj;
00057 }
00058 
00059 
00060 int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *outputLength)
00061 {
00062     (void)obj;
00063     int ret;
00064     uint32_t entropySizeBits;
00065     uint32_t *entrSource_ptr;
00066     uint32_t actualLength;
00067 
00068     ret = LLF_RND_GetTrngSource(
00069                 &rndState ,    /*in/out*/
00070                 obj,       /*in/out*/
00071                 0,       /*in*/
00072                 &entropySizeBits,  /*in/out*/
00073                 &entrSource_ptr,   /*out*/
00074                 &actualLength,  /*out*/
00075                 (uint32_t*)&rndWorkBuff.crysRndWorkBuff  /*in*/);
00076     if ( ret != 0 )
00077         return -1;
00078 
00079     if ( length < actualLength )
00080         actualLength = length;
00081 
00082     *outputLength = actualLength;
00083 
00084     memcpy( output, entrSource_ptr + CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS, *outputLength );
00085     mbedtls_zeroize( entrSource_ptr + CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS, *outputLength );
00086     return 0;
00087 }
00088 
00089 #endif //DEVICE_TRNG