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