xref: /optee_os/core/lib/libtomcrypt/tomcrypt.c (revision c7e27e8340c4ce03dad5a92ac7716d147e584715)
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.h>
10 #include "tomcrypt_mp.h"
11 #include <trace.h>
12 
13 #if defined(_CFG_CORE_LTC_VFP)
14 #include <tomcrypt_arm_neon.h>
15 #include <kernel/thread.h>
16 #endif
17 
18 #if defined(_CFG_CORE_LTC_ACIPHER)
19 /* Random generator */
20 static int prng_crypto_start(union Prng_state *prng __unused)
21 {
22 	return CRYPT_OK;
23 }
24 
25 static int prng_crypto_add_entropy(const unsigned char *in __unused,
26 				   unsigned long inlen __unused,
27 				   union Prng_state *prng __unused)
28 {
29 	/* No entropy is required */
30 	return CRYPT_OK;
31 }
32 
33 static int prng_crypto_ready(union Prng_state *prng __unused)
34 {
35 	return CRYPT_OK;
36 }
37 
38 static unsigned long prng_crypto_read(unsigned char *out, unsigned long outlen,
39 				      union Prng_state *prng __unused)
40 {
41 	if (crypto_rng_read(out, outlen))
42 		return 0;
43 
44 	return outlen;
45 }
46 
47 static int prng_crypto_done(union Prng_state *prng __unused)
48 {
49 	return CRYPT_OK;
50 }
51 
52 static int prng_crypto_export(unsigned char *out __unused,
53 			      unsigned long *outlen __unused,
54 			      union Prng_state *prng __unused)
55 {
56 	return CRYPT_OK;
57 }
58 
59 static int prng_crypto_import(const unsigned char *in  __unused,
60 			      unsigned long inlen __unused,
61 			      union Prng_state *prng __unused)
62 {
63 	return CRYPT_OK;
64 }
65 
66 static int prng_crypto_test(void)
67 {
68 	return CRYPT_OK;
69 }
70 
71 static const struct ltc_prng_descriptor prng_crypto_desc = {
72 	.name = "prng_crypto",
73 	.export_size = 64,
74 	.start = &prng_crypto_start,
75 	.add_entropy = &prng_crypto_add_entropy,
76 	.ready = &prng_crypto_ready,
77 	.read = &prng_crypto_read,
78 	.done = &prng_crypto_done,
79 	.pexport = &prng_crypto_export,
80 	.pimport = &prng_crypto_import,
81 	.test = &prng_crypto_test,
82 };
83 #endif /*_CFG_CORE_LTC_ACIPHER*/
84 
85 /*
86  * tee_ltc_reg_algs(): Registers
87  *	- algorithms
88  *	- hash
89  *	- prng (pseudo random generator)
90  */
91 
92 static void tee_ltc_reg_algs(void)
93 {
94 #if defined(_CFG_CORE_LTC_AES) || defined(_CFG_CORE_LTC_AES_DESC)
95 	register_cipher(&aes_desc);
96 #endif
97 #if defined(_CFG_CORE_LTC_DES)
98 	register_cipher(&des_desc);
99 	register_cipher(&des3_desc);
100 #endif
101 #if defined(_CFG_CORE_LTC_MD5)
102 	register_hash(&md5_desc);
103 #endif
104 #if defined(_CFG_CORE_LTC_SHA1)
105 	register_hash(&sha1_desc);
106 #endif
107 #if defined(_CFG_CORE_LTC_SHA224)
108 	register_hash(&sha224_desc);
109 #endif
110 #if defined(_CFG_CORE_LTC_SHA256) || defined(_CFG_CORE_LTC_SHA256_DESC)
111 	register_hash(&sha256_desc);
112 #endif
113 #if defined(_CFG_CORE_LTC_SHA384) || defined(_CFG_CORE_LTC_SHA384_DESC)
114 	register_hash(&sha384_desc);
115 #endif
116 #if defined(_CFG_CORE_LTC_SHA512) || defined(_CFG_CORE_LTC_SHA512_DESC)
117 	register_hash(&sha512_desc);
118 #endif
119 #if defined(_CFG_CORE_LTC_ACIPHER)
120 	register_prng(&prng_crypto_desc);
121 #endif
122 }
123 
124 TEE_Result crypto_init(void)
125 {
126 #if defined(_CFG_CORE_LTC_ACIPHER)
127 	init_mp_tomcrypt();
128 #endif
129 	tee_ltc_reg_algs();
130 
131 	return TEE_SUCCESS;
132 }
133 
134 #if defined(CFG_WITH_VFP)
135 void tomcrypt_arm_neon_enable(struct tomcrypt_arm_neon_state *state)
136 {
137 	state->state = thread_kernel_enable_vfp();
138 }
139 
140 void tomcrypt_arm_neon_disable(struct tomcrypt_arm_neon_state *state)
141 {
142 	thread_kernel_disable_vfp(state->state);
143 }
144 #endif
145