xref: /optee_os/core/crypto/rng_hw.c (revision 1a5f154b26bc2cd59a1c3532eb0a76f96e857380)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /* Copyright (c) 2018, Linaro Limited */
3 
4 #include <compiler.h>
5 #include <crypto/crypto.h>
6 #include <kernel/panic.h>
7 #include <rng_support.h>
8 #include <tee/tee_cryp_utl.h>
9 #include <types_ext.h>
10 
11 /*
12  * This is only here to keep the compiler happy while we convert over
13  * platforms. Either hw_get_random_bytes() is overridden or this
14  * function is, in either case this is never called.
15  */
16 uint8_t __weak hw_get_random_byte(void)
17 {
18 	panic();
19 	return 4; // chosen by fair dice roll.
20 		  // guaranteed to be random.
21 }
22 
23 TEE_Result __weak hw_get_random_bytes(void *buf, size_t blen)
24 {
25 	uint8_t *b = buf;
26 	size_t n = 0;
27 
28 	for (n = 0; n < blen; n++)
29 		b[n] = hw_get_random_byte();
30 
31 	return TEE_SUCCESS;
32 }
33 
34 /* This is a HW RNG, no need for seeding */
35 TEE_Result __weak crypto_rng_init(const void *data __unused,
36 				  size_t dlen __unused)
37 {
38 	return TEE_SUCCESS;
39 }
40 
41 /* This is a HW RNG, no need to add entropy */
42 void __weak crypto_rng_add_event(enum crypto_rng_src sid __unused,
43 				 unsigned int *pnum __unused,
44 				 const void *data __unused,
45 				 size_t dlen __unused)
46 {
47 }
48 
49 TEE_Result __weak crypto_rng_read(void *buf, size_t blen)
50 {
51 	if (!buf)
52 		return TEE_ERROR_BAD_PARAMETERS;
53 
54 	return hw_get_random_bytes(buf, blen);
55 }
56