diff options
author | Jens Wiklander <jens.wiklander@linaro.org> | 2019-03-11 11:54:41 +0100 |
---|---|---|
committer | Jérôme Forissier <jerome.forissier@linaro.org> | 2019-03-28 14:11:23 +0100 |
commit | eebeb1e282342731574a7f9bf215ed66bbf1b0af (patch) | |
tree | 61d2d94c5596d0da8a033780cd09eade4a8f9f45 | |
parent | 89ed30d14f68d431a66b5d8a3d2e7a1a72e8f72b (diff) |
core: ltc: move dh wrappers to separate file
Moves the DH wrappers in tee_ltc_provider.c to its own file, dh.c.
Acked-by: Jerome Forissier <jerome.forissier@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
-rw-r--r-- | core/lib/libtomcrypt/dh.c | 79 | ||||
-rw-r--r-- | core/lib/libtomcrypt/src/tee_ltc_provider.c | 103 | ||||
-rw-r--r-- | core/lib/libtomcrypt/sub.mk | 1 |
3 files changed, 80 insertions, 103 deletions
diff --git a/core/lib/libtomcrypt/dh.c b/core/lib/libtomcrypt/dh.c new file mode 100644 index 00000000..51b0504c --- /dev/null +++ b/core/lib/libtomcrypt/dh.c @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: BSD-2-Clause +/* + * Copyright (c) 2014-2019, Linaro Limited + */ + +#include <crypto/crypto.h> +#include <stdlib.h> +#include <string.h> +#include <tee_api_types.h> +#include <tomcrypt.h> +#include <trace.h> +#include <utee_defines.h> + +#include "acipher_helpers.h" + +TEE_Result crypto_acipher_alloc_dh_keypair(struct dh_keypair *s, + size_t key_size_bits __unused) +{ + memset(s, 0, sizeof(*s)); + if (!bn_alloc_max(&s->g)) + return TEE_ERROR_OUT_OF_MEMORY; + if (!bn_alloc_max(&s->p)) + goto err; + if (!bn_alloc_max(&s->y)) + goto err; + if (!bn_alloc_max(&s->x)) + goto err; + if (!bn_alloc_max(&s->q)) + goto err; + return TEE_SUCCESS; +err: + crypto_bignum_free(s->g); + crypto_bignum_free(s->p); + crypto_bignum_free(s->y); + crypto_bignum_free(s->x); + return TEE_ERROR_OUT_OF_MEMORY; +} + +TEE_Result crypto_acipher_gen_dh_key(struct dh_keypair *key, struct bignum *q, + size_t xbits) +{ + TEE_Result res; + dh_key ltc_tmp_key; + int ltc_res; + + /* Generate the DH key */ + ltc_tmp_key.g = key->g; + ltc_tmp_key.p = key->p; + ltc_res = dh_make_key(NULL, find_prng("prng_mpa"), q, xbits, + <c_tmp_key); + if (ltc_res != CRYPT_OK) { + res = TEE_ERROR_BAD_PARAMETERS; + } else { + ltc_mp.copy(ltc_tmp_key.y, key->y); + ltc_mp.copy(ltc_tmp_key.x, key->x); + + /* Free the tempory key */ + dh_free(<c_tmp_key); + res = TEE_SUCCESS; + } + return res; +} + +TEE_Result crypto_acipher_dh_shared_secret(struct dh_keypair *private_key, + struct bignum *public_key, + struct bignum *secret) +{ + int err; + dh_key pk = { + .type = PK_PRIVATE, + .g = private_key->g, + .p = private_key->p, + .y = private_key->y, + .x = private_key->x + }; + + err = dh_shared_secret(&pk, public_key, secret); + return ((err == CRYPT_OK) ? TEE_SUCCESS : TEE_ERROR_BAD_PARAMETERS); +} diff --git a/core/lib/libtomcrypt/src/tee_ltc_provider.c b/core/lib/libtomcrypt/src/tee_ltc_provider.c index f3c3ae54..3eeb57c8 100644 --- a/core/lib/libtomcrypt/src/tee_ltc_provider.c +++ b/core/lib/libtomcrypt/src/tee_ltc_provider.c @@ -125,109 +125,6 @@ static void tee_ltc_reg_algs(void) register_prng(&prng_mpa_desc); } - -/****************************************************************************** - * Asymmetric algorithms - ******************************************************************************/ - -#if defined(_CFG_CRYPTO_WITH_ACIPHER) - -static bool bn_alloc_max(struct bignum **s) -{ - *s = crypto_bignum_allocate(CFG_CORE_BIGNUM_MAX_BITS); - - return *s; -} - -static TEE_Result __maybe_unused convert_ltc_verify_status(int ltc_res, - int ltc_stat) -{ - switch (ltc_res) { - case CRYPT_OK: - if (ltc_stat == 1) - return TEE_SUCCESS; - else - return TEE_ERROR_SIGNATURE_INVALID; - case CRYPT_INVALID_PACKET: - return TEE_ERROR_SIGNATURE_INVALID; - default: - return TEE_ERROR_GENERIC; - } -} - -#if defined(CFG_CRYPTO_DH) - -TEE_Result crypto_acipher_alloc_dh_keypair(struct dh_keypair *s, - size_t key_size_bits __unused) -{ - memset(s, 0, sizeof(*s)); - if (!bn_alloc_max(&s->g)) { - return TEE_ERROR_OUT_OF_MEMORY; - } - - if (!bn_alloc_max(&s->p)) - goto err; - if (!bn_alloc_max(&s->y)) - goto err; - if (!bn_alloc_max(&s->x)) - goto err; - if (!bn_alloc_max(&s->q)) - goto err; - return TEE_SUCCESS; -err: - crypto_bignum_free(s->g); - crypto_bignum_free(s->p); - crypto_bignum_free(s->y); - crypto_bignum_free(s->x); - return TEE_ERROR_OUT_OF_MEMORY; -} - -TEE_Result crypto_acipher_gen_dh_key(struct dh_keypair *key, struct bignum *q, - size_t xbits) -{ - TEE_Result res; - dh_key ltc_tmp_key; - int ltc_res; - - /* Generate the DH key */ - ltc_tmp_key.g = key->g; - ltc_tmp_key.p = key->p; - ltc_res = dh_make_key(NULL, find_prng("prng_mpa"), q, xbits, - <c_tmp_key); - if (ltc_res != CRYPT_OK) { - res = TEE_ERROR_BAD_PARAMETERS; - } else { - ltc_mp.copy(ltc_tmp_key.y, key->y); - ltc_mp.copy(ltc_tmp_key.x, key->x); - - /* Free the tempory key */ - dh_free(<c_tmp_key); - res = TEE_SUCCESS; - } - return res; -} - -TEE_Result crypto_acipher_dh_shared_secret(struct dh_keypair *private_key, - struct bignum *public_key, - struct bignum *secret) -{ - int err; - dh_key pk = { - .type = PK_PRIVATE, - .g = private_key->g, - .p = private_key->p, - .y = private_key->y, - .x = private_key->x - }; - - err = dh_shared_secret(&pk, public_key, secret); - return ((err == CRYPT_OK) ? TEE_SUCCESS : TEE_ERROR_BAD_PARAMETERS); -} - -#endif /* CFG_CRYPTO_DH */ -#endif /* _CFG_CRYPTO_WITH_ACIPHER */ - - TEE_Result crypto_init(void) { init_mp_tomcrypt(); diff --git a/core/lib/libtomcrypt/sub.mk b/core/lib/libtomcrypt/sub.mk index b04e2dbb..50333ac1 100644 --- a/core/lib/libtomcrypt/sub.mk +++ b/core/lib/libtomcrypt/sub.mk @@ -18,3 +18,4 @@ srcs-$(CFG_CRYPTO_AES_GCM_FROM_CRYPTOLIB) += gcm.c srcs-$(CFG_CRYPTO_DSA) += dsa.c srcs-$(CFG_CRYPTO_ECC) += ecc.c srcs-$(CFG_CRYPTO_RSA) += rsa.c +srcs-$(CFG_CRYPTO_DH) += dh.c |