Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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 DEVICE_TRNG
00022 
00023 #include <string.h>
00024 #include "trng_api.h"
00025 #include "mbedtls/platform.h"
00026 
00027 extern mbedtls_platform_context  plat_ctx;
00028 static CRYS_RND_WorkBuff_t   rndWorkBuff = { { 0 } };
00029 
00030 /* Implementation that should never be optimized out by the compiler */
00031 static void mbedtls_zeroize( void *v, size_t n ) {
00032     volatile unsigned char *p = (unsigned char*)v;
00033     while( n-- ) *p++ = 0;
00034 }
00035 
00036 CRYSError_t  RNG_PLAT_SetUserRngParameters(
00037                          CRYS_RND_State_t   *pRndState,
00038                          CRYS_RND_Params_t *pTrngParams);
00039 
00040 CRYSError_t  LLF_RND_GetTrngSource(
00041                          CRYS_RND_State_t   *rndState_ptr,
00042                          CRYS_RND_Params_t *trngParams_ptr,
00043                          SaSiBool_t        isContinued,
00044                          uint32_t          *entropySize_ptr,
00045                          uint32_t          **sourceOut_ptr_ptr,
00046                          uint32_t          *sourceOutSize_ptr,
00047                          uint32_t          *rndWorkBuff_ptr);
00048 
00049 
00050 void trng_init(trng_t *obj)
00051 {
00052     RNG_PLAT_SetUserRngParameters(&plat_ctx.platform_impl_ctx.rndState, obj);
00053 }
00054 
00055 void trng_free(trng_t *obj)
00056 {
00057     (void)obj;
00058 }
00059 
00060 
00061 int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *outputLength)
00062 {
00063     (void)obj;
00064     int ret;
00065     uint32_t entropySizeBits;
00066     uint32_t *entrSource_ptr;
00067     uint32_t actualLength;
00068 
00069     ret = LLF_RND_GetTrngSource(
00070                 &plat_ctx.platform_impl_ctx.rndState ,    /*in/out*/
00071                 obj,       /*in/out*/
00072                 0,       /*in*/
00073                 &entropySizeBits,  /*in/out*/
00074                 &entrSource_ptr,   /*out*/
00075                 &actualLength,  /*out*/
00076                 (uint32_t*)&rndWorkBuff.crysRndWorkBuff  /*in*/);
00077     if ( ret != 0 )
00078         return -1;
00079 
00080     if ( length < actualLength )
00081         actualLength = length;
00082 
00083     *outputLength = actualLength;
00084 
00085     memcpy( output, entrSource_ptr + CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS, *outputLength );
00086     mbedtls_zeroize( entrSource_ptr + CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS, *outputLength );
00087     return 0;
00088 }
00089 
00090 #endif //DEVICE_TRNG