xref: /optee_os/core/lib/libtomcrypt/tomcrypt.c (revision 731185b11620a6de1f824278ab3c166c7853ef66)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014-2023, Linaro Limited
4  */
5 
6 #include <crypto/crypto.h>
7 #include <tee_api_types.h>
8 #include <tee_api_defines.h>
9 #include <tomcrypt_private.h>
10 #include <tomcrypt_init.h>
11 #include "tomcrypt_mp.h"
12 #include <trace.h>
13 
14 #if defined(_CFG_CORE_LTC_VFP)
15 #include <tomcrypt_arm_neon.h>
16 #include <kernel/thread.h>
17 #endif
18 
19 #if defined(_CFG_CORE_LTC_ACIPHER) || defined(_CFG_CORE_LTC_EC25519)
20 /* Random generator */
21 static int prng_crypto_start(prng_state *prng __unused)
22 {
23 	return CRYPT_OK;
24 }
25 
26 static int prng_crypto_add_entropy(const unsigned char *in __unused,
27 				   unsigned long inlen __unused,
28 				   prng_state *prng __unused)
29 {
30 	/* No entropy is required */
31 	return CRYPT_OK;
32 }
33 
34 static int prng_crypto_ready(prng_state *prng __unused)
35 {
36 	return CRYPT_OK;
37 }
38 
39 static unsigned long prng_crypto_read(unsigned char *out, unsigned long outlen,
40 				      prng_state *prng __unused)
41 {
42 	if (crypto_rng_read(out, outlen))
43 		return 0;
44 
45 	return outlen;
46 }
47 
48 static int prng_crypto_done(prng_state *prng __unused)
49 {
50 	return CRYPT_OK;
51 }
52 
53 static int prng_crypto_export(unsigned char *out __unused,
54 			      unsigned long *outlen __unused,
55 			      prng_state *prng __unused)
56 {
57 	return CRYPT_OK;
58 }
59 
60 static int prng_crypto_import(const unsigned char *in  __unused,
61 			      unsigned long inlen __unused,
62 			      prng_state *prng __unused)
63 {
64 	return CRYPT_OK;
65 }
66 
67 static int prng_crypto_test(void)
68 {
69 	return CRYPT_OK;
70 }
71 
72 static const struct ltc_prng_descriptor prng_crypto_desc = {
73 	.name = "prng_crypto",
74 	.export_size = 64,
75 	.start = prng_crypto_start,
76 	.add_entropy = prng_crypto_add_entropy,
77 	.ready = prng_crypto_ready,
78 	.read = prng_crypto_read,
79 	.done = prng_crypto_done,
80 	.pexport = prng_crypto_export,
81 	.pimport = prng_crypto_import,
82 	.test = prng_crypto_test,
83 };
84 #endif /*_CFG_CORE_LTC_ACIPHER*/
85 
86 /*
87  * tee_ltc_reg_algs(): Registers
88  *	- algorithms
89  *	- hash
90  *	- prng (pseudo random generator)
91  */
92 
93 static void tee_ltc_reg_algs(void)
94 {
95 #if defined(_CFG_CORE_LTC_AES) || defined(_CFG_CORE_LTC_AES_DESC)
96 	register_cipher(&aes_desc);
97 #endif
98 #if defined(_CFG_CORE_LTC_DES)
99 	register_cipher(&des_desc);
100 	register_cipher(&des3_desc);
101 #endif
102 #if defined(_CFG_CORE_LTC_MD5_DESC)
103 	register_hash(&md5_desc);
104 #endif
105 #if defined(_CFG_CORE_LTC_SHA1) || defined(_CFG_CORE_LTC_SHA1_DESC)
106 	register_hash(&sha1_desc);
107 #endif
108 #if defined(_CFG_CORE_LTC_SHA224) || defined(_CFG_CORE_LTC_SHA224_DESC)
109 	register_hash(&sha224_desc);
110 #endif
111 #if defined(_CFG_CORE_LTC_SHA256) || defined(_CFG_CORE_LTC_SHA256_DESC)
112 	register_hash(&sha256_desc);
113 #endif
114 #if defined(_CFG_CORE_LTC_SHA384) || defined(_CFG_CORE_LTC_SHA384_DESC)
115 	register_hash(&sha384_desc);
116 #endif
117 #if defined(_CFG_CORE_LTC_SHA512) || defined(_CFG_CORE_LTC_SHA512_DESC)
118 	register_hash(&sha512_desc);
119 #endif
120 #if defined(_CFG_CORE_LTC_SHA3_224) || defined(_CFG_CORE_LTC_SHA3_224_DESC)
121 	register_hash(&sha3_224_desc);
122 #endif
123 #if defined(_CFG_CORE_LTC_SHA3_256) || defined(_CFG_CORE_LTC_SHA3_256_DESC)
124 	register_hash(&sha3_256_desc);
125 #endif
126 #if defined(_CFG_CORE_LTC_SHA3_384) || defined(_CFG_CORE_LTC_SHA3_384_DESC)
127 	register_hash(&sha3_384_desc);
128 #endif
129 #if defined(_CFG_CORE_LTC_SHA3_512) || defined(_CFG_CORE_LTC_SHA3_512_DESC)
130 	register_hash(&sha3_512_desc);
131 #endif
132 #if defined(_CFG_CORE_LTC_ACIPHER) || defined(_CFG_CORE_LTC_EC25519)
133 	register_prng(&prng_crypto_desc);
134 #endif
135 }
136 
137 static void ltc_init(void)
138 {
139 #if defined(_CFG_CORE_LTC_ACIPHER)
140 	init_mp_tomcrypt();
141 #endif
142 	tee_ltc_reg_algs();
143 }
144 
145 #if defined(CFG_CRYPTOLIB_NAME_tomcrypt)
146 TEE_Result crypto_init(void)
147 {
148 	ltc_init();
149 
150 	return TEE_SUCCESS;
151 }
152 #else
153 void tomcrypt_init(void)
154 {
155 	ltc_init();
156 }
157 #endif
158 
159 #if defined(CFG_WITH_VFP)
160 void tomcrypt_arm_neon_enable(struct tomcrypt_arm_neon_state *state)
161 {
162 	state->state = thread_kernel_enable_vfp();
163 }
164 
165 void tomcrypt_arm_neon_disable(struct tomcrypt_arm_neon_state *state)
166 {
167 	thread_kernel_disable_vfp(state->state);
168 }
169 #endif
170