1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (C) 2018, ARM Limited
4  * Copyright (C) 2019, Linaro Limited
5  */
6 
7 #ifndef MBED_HELPERS_H
8 #define MBED_HELPERS_H
9 
10 #include <crypto/crypto.h>
11 #include <mbedtls/aes.h>
12 #include <mbedtls/bignum.h>
13 #include <mbedtls/ctr_drbg.h>
14 #include <tee_api_types.h>
15 
mbd_rand(void * rng_state __unused,unsigned char * output,size_t len)16 static inline int mbd_rand(void *rng_state __unused, unsigned char *output,
17 			size_t len)
18 {
19 	if (crypto_rng_read(output, len))
20 		return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
21 	return 0;
22 }
23 
mbed_copy_mbedtls_aes_context(mbedtls_aes_context * dst,mbedtls_aes_context * src)24 static inline void mbed_copy_mbedtls_aes_context(mbedtls_aes_context *dst,
25 						 mbedtls_aes_context *src)
26 {
27 	*dst = *src;
28 #if !defined(MBEDTLS_AES_ALT)
29 #if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_PADLOCK_ALIGN16)
30 	/*
31 	 * This build configuration should not occur, but just in case error out
32 	 * here. It needs special handling of the rk pointer, see
33 	 * mbedtls_aes_setkey_enc().
34 	 */
35 #error Do not know how to copy mbedtls_aes_context::rk
36 #endif
37 	dst->rk = dst->buf;
38 #endif
39 }
40 
41 TEE_Result mbed_gen_random_upto(mbedtls_mpi *n, mbedtls_mpi *max);
42 #endif /*MBED_HELPERS_H*/
43