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