mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
features/cryptocell/FEATURE_CRYPTOCELL310/trng.c@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
- Parent:
- 0:5b88d5760320
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:5b88d5760320 | 1 | /* |
kenjiArai | 0:5b88d5760320 | 2 | * trng.c |
kenjiArai | 0:5b88d5760320 | 3 | * |
kenjiArai | 0:5b88d5760320 | 4 | * Copyright (C) 2017, Arm Limited, All Rights Reserved |
kenjiArai | 0:5b88d5760320 | 5 | * SPDX-License-Identifier: Apache-2.0 |
kenjiArai | 0:5b88d5760320 | 6 | * |
kenjiArai | 0:5b88d5760320 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
kenjiArai | 0:5b88d5760320 | 8 | * not use this file except in compliance with the License. |
kenjiArai | 0:5b88d5760320 | 9 | * You may obtain a copy of the License at |
kenjiArai | 0:5b88d5760320 | 10 | * |
kenjiArai | 0:5b88d5760320 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 0:5b88d5760320 | 12 | * |
kenjiArai | 0:5b88d5760320 | 13 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 0:5b88d5760320 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
kenjiArai | 0:5b88d5760320 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 0:5b88d5760320 | 16 | * See the License for the specific language governing permissions and |
kenjiArai | 0:5b88d5760320 | 17 | * limitations under the License. |
kenjiArai | 0:5b88d5760320 | 18 | * |
kenjiArai | 0:5b88d5760320 | 19 | */ |
kenjiArai | 0:5b88d5760320 | 20 | |
kenjiArai | 0:5b88d5760320 | 21 | #if DEVICE_TRNG |
kenjiArai | 0:5b88d5760320 | 22 | |
kenjiArai | 0:5b88d5760320 | 23 | #include <string.h> |
kenjiArai | 0:5b88d5760320 | 24 | #include "trng_api.h" |
kenjiArai | 0:5b88d5760320 | 25 | #include "mbedtls/platform.h" |
kenjiArai | 0:5b88d5760320 | 26 | |
kenjiArai | 0:5b88d5760320 | 27 | extern mbedtls_platform_context plat_ctx; |
kenjiArai | 0:5b88d5760320 | 28 | static CRYS_RND_WorkBuff_t rndWorkBuff = { { 0 } }; |
kenjiArai | 0:5b88d5760320 | 29 | |
kenjiArai | 0:5b88d5760320 | 30 | /* Implementation that should never be optimized out by the compiler */ |
kenjiArai | 0:5b88d5760320 | 31 | static void mbedtls_zeroize( void *v, size_t n ) { |
kenjiArai | 0:5b88d5760320 | 32 | volatile unsigned char *p = (unsigned char*)v; |
kenjiArai | 0:5b88d5760320 | 33 | while( n-- ) *p++ = 0; |
kenjiArai | 0:5b88d5760320 | 34 | } |
kenjiArai | 0:5b88d5760320 | 35 | |
kenjiArai | 0:5b88d5760320 | 36 | CRYSError_t RNG_PLAT_SetUserRngParameters( |
kenjiArai | 0:5b88d5760320 | 37 | CRYS_RND_State_t *pRndState, |
kenjiArai | 0:5b88d5760320 | 38 | CRYS_RND_Params_t *pTrngParams); |
kenjiArai | 0:5b88d5760320 | 39 | |
kenjiArai | 0:5b88d5760320 | 40 | CRYSError_t LLF_RND_GetTrngSource( |
kenjiArai | 0:5b88d5760320 | 41 | CRYS_RND_State_t *rndState_ptr, |
kenjiArai | 0:5b88d5760320 | 42 | CRYS_RND_Params_t *trngParams_ptr, |
kenjiArai | 0:5b88d5760320 | 43 | SaSiBool_t isContinued, |
kenjiArai | 0:5b88d5760320 | 44 | uint32_t *entropySize_ptr, |
kenjiArai | 0:5b88d5760320 | 45 | uint32_t **sourceOut_ptr_ptr, |
kenjiArai | 0:5b88d5760320 | 46 | uint32_t *sourceOutSize_ptr, |
kenjiArai | 0:5b88d5760320 | 47 | uint32_t *rndWorkBuff_ptr); |
kenjiArai | 0:5b88d5760320 | 48 | |
kenjiArai | 0:5b88d5760320 | 49 | |
kenjiArai | 0:5b88d5760320 | 50 | void trng_init(trng_t *obj) |
kenjiArai | 0:5b88d5760320 | 51 | { |
kenjiArai | 0:5b88d5760320 | 52 | RNG_PLAT_SetUserRngParameters(&plat_ctx.platform_impl_ctx.rndState, obj); |
kenjiArai | 0:5b88d5760320 | 53 | } |
kenjiArai | 0:5b88d5760320 | 54 | |
kenjiArai | 0:5b88d5760320 | 55 | void trng_free(trng_t *obj) |
kenjiArai | 0:5b88d5760320 | 56 | { |
kenjiArai | 0:5b88d5760320 | 57 | (void)obj; |
kenjiArai | 0:5b88d5760320 | 58 | } |
kenjiArai | 0:5b88d5760320 | 59 | |
kenjiArai | 0:5b88d5760320 | 60 | |
kenjiArai | 0:5b88d5760320 | 61 | int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *outputLength) |
kenjiArai | 0:5b88d5760320 | 62 | { |
kenjiArai | 0:5b88d5760320 | 63 | (void)obj; |
kenjiArai | 0:5b88d5760320 | 64 | int ret; |
kenjiArai | 0:5b88d5760320 | 65 | uint32_t entropySizeBits; |
kenjiArai | 0:5b88d5760320 | 66 | uint32_t *entrSource_ptr; |
kenjiArai | 0:5b88d5760320 | 67 | uint32_t actualLength; |
kenjiArai | 0:5b88d5760320 | 68 | |
kenjiArai | 0:5b88d5760320 | 69 | ret = LLF_RND_GetTrngSource( |
kenjiArai | 0:5b88d5760320 | 70 | &plat_ctx.platform_impl_ctx.rndState , /*in/out*/ |
kenjiArai | 0:5b88d5760320 | 71 | obj, /*in/out*/ |
kenjiArai | 0:5b88d5760320 | 72 | 0, /*in*/ |
kenjiArai | 0:5b88d5760320 | 73 | &entropySizeBits, /*in/out*/ |
kenjiArai | 0:5b88d5760320 | 74 | &entrSource_ptr, /*out*/ |
kenjiArai | 0:5b88d5760320 | 75 | &actualLength, /*out*/ |
kenjiArai | 0:5b88d5760320 | 76 | (uint32_t*)&rndWorkBuff.crysRndWorkBuff /*in*/); |
kenjiArai | 0:5b88d5760320 | 77 | if ( ret != 0 ) |
kenjiArai | 0:5b88d5760320 | 78 | return -1; |
kenjiArai | 0:5b88d5760320 | 79 | |
kenjiArai | 0:5b88d5760320 | 80 | if ( length < actualLength ) |
kenjiArai | 0:5b88d5760320 | 81 | actualLength = length; |
kenjiArai | 0:5b88d5760320 | 82 | |
kenjiArai | 0:5b88d5760320 | 83 | *outputLength = actualLength; |
kenjiArai | 0:5b88d5760320 | 84 | |
kenjiArai | 0:5b88d5760320 | 85 | memcpy( output, entrSource_ptr + CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS, *outputLength ); |
kenjiArai | 0:5b88d5760320 | 86 | mbedtls_zeroize( entrSource_ptr + CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS, *outputLength ); |
kenjiArai | 0:5b88d5760320 | 87 | return 0; |
kenjiArai | 0:5b88d5760320 | 88 | } |
kenjiArai | 0:5b88d5760320 | 89 | |
kenjiArai | 0:5b88d5760320 | 90 | #endif //DEVICE_TRNG |