1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, 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 */
prng_crypto_start(prng_state * prng __unused)21 static int prng_crypto_start(prng_state *prng __unused)
22 {
23 	return CRYPT_OK;
24 }
25 
prng_crypto_add_entropy(const unsigned char * in __unused,unsigned long inlen __unused,prng_state * prng __unused)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 
prng_crypto_ready(prng_state * prng __unused)34 static int prng_crypto_ready(prng_state *prng __unused)
35 {
36 	return CRYPT_OK;
37 }
38 
prng_crypto_read(unsigned char * out,unsigned long outlen,prng_state * prng __unused)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 
prng_crypto_done(prng_state * prng __unused)48 static int prng_crypto_done(prng_state *prng __unused)
49 {
50 	return CRYPT_OK;
51 }
52 
prng_crypto_export(unsigned char * out __unused,unsigned long * outlen __unused,prng_state * prng __unused)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 
prng_crypto_import(const unsigned char * in __unused,unsigned long inlen __unused,prng_state * prng __unused)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 
prng_crypto_test(void)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 
tee_ltc_reg_algs(void)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)
103 	register_hash(&md5_desc);
104 #endif
105 #if defined(_CFG_CORE_LTC_SHA1)
106 	register_hash(&sha1_desc);
107 #endif
108 #if defined(_CFG_CORE_LTC_SHA224)
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_ACIPHER) || defined(_CFG_CORE_LTC_EC25519)
121 	register_prng(&prng_crypto_desc);
122 #endif
123 }
124 
ltc_init(void)125 static void ltc_init(void)
126 {
127 #if defined(_CFG_CORE_LTC_ACIPHER)
128 	init_mp_tomcrypt();
129 #endif
130 	tee_ltc_reg_algs();
131 }
132 
133 #if defined(CFG_CRYPTOLIB_NAME_tomcrypt)
crypto_init(void)134 TEE_Result crypto_init(void)
135 {
136 	ltc_init();
137 
138 	return TEE_SUCCESS;
139 }
140 #else
tomcrypt_init(void)141 void tomcrypt_init(void)
142 {
143 	ltc_init();
144 }
145 #endif
146 
147 #if defined(CFG_WITH_VFP)
tomcrypt_arm_neon_enable(struct tomcrypt_arm_neon_state * state)148 void tomcrypt_arm_neon_enable(struct tomcrypt_arm_neon_state *state)
149 {
150 	state->state = thread_kernel_enable_vfp();
151 }
152 
tomcrypt_arm_neon_disable(struct tomcrypt_arm_neon_state * state)153 void tomcrypt_arm_neon_disable(struct tomcrypt_arm_neon_state *state)
154 {
155 	thread_kernel_disable_vfp(state->state);
156 }
157 #endif
158