xref: /optee_os/lib/libmbedtls/core/mbed_helpers.h (revision 9fc2442cc66c279cb962c90c4375746fc9b28bb9)
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/ctr_drbg.h>
13 
14 static inline int mbd_rand(void *rng_state __unused, unsigned char *output,
15 			size_t len)
16 {
17 	if (crypto_rng_read(output, len))
18 		return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
19 	return 0;
20 }
21 
22 static inline void mbed_copy_mbedtls_aes_context(mbedtls_aes_context *dst,
23 						 mbedtls_aes_context *src)
24 {
25 	*dst = *src;
26 #if !defined(MBEDTLS_AES_ALT)
27 #if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_PADLOCK_ALIGN16)
28 	/*
29 	 * This build configuration should not occur, but just in case error out
30 	 * here. It needs special handling of the rk pointer, see
31 	 * mbedtls_aes_setkey_enc().
32 	 */
33 #error Do not know how to copy mbedtls_aes_context::rk
34 #endif
35 	dst->rk = dst->buf;
36 #endif
37 }
38 
39 #endif /*MBED_HELPERS_H*/
40