Skip to content

Commit 6d04ad9

Browse files
author
Valerii Chubar
committed
tmp: add Ed25519 support
Please do not review this patch. The changes originate from: OP-TEE#5486 The changes are not expected to be merged with this pull request. This commit adds Ed25519 support as defined in TEE Internal Core API v1.3.1 Signed-off-by: Sergiy Kibrik <Sergiy_Kibrik@epam.com> Signed-off-by: Valerii Chubar <valerii_chubar@epam.com> Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org> Acked-by: Etienne Carriere <etienne.carriere@linaro.org> libtomcrypt: add ed25519ctx and ed25519ph support Support contextualized extension of the Ed25519 scheme. Signed-off-by: Valerii Chubar <valerii_chubar@epam.com> Signed-off-by: Sergiy Kibrik <Sergiy_Kibrik@epam.com> Acked-by: Jerome Forissier <jerome.forissier@linaro.org> Acked-by: Etienne Carriere <etienne.carriere@linaro.org> core: libtomcrypt: add Ed25519 support Enable Ed25519 implementation of libtomcrypt and add the OP-TEE wrappers. Signed-off-by: Valerii Chubar <valerii_chubar@epam.com> Signed-off-by: Sergiy Kibrik <Sergiy_Kibrik@epam.com> Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org> Acked-by: Etienne Carriere <etienne.carriere@linaro.org> core: crypto: add Ed25519 support Put in place Ed25519 core functionality and support it for OP-TEE crypto syscalls. Signed-off-by: Valerii Chubar <valerii_chubar@epam.com> Signed-off-by: Sergiy Kibrik <Sergiy_Kibrik@epam.com> Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org> Acked-by: Etienne Carriere <etienne.carriere@linaro.org>
1 parent 8411e6a commit 6d04ad9

File tree

14 files changed

+486
-5
lines changed

14 files changed

+486
-5
lines changed

core/crypto.mk

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ CFG_CRYPTO_ECC ?= y
4747
CFG_CRYPTO_SM2_PKE ?= y
4848
CFG_CRYPTO_SM2_DSA ?= y
4949
CFG_CRYPTO_SM2_KEP ?= y
50-
# X25519 is only supported by libtomcrypt
50+
# X25519 and Ed25519 are only supported by libtomcrypt
5151
ifeq ($(CFG_CRYPTOLIB_NAME),tomcrypt)
52+
CFG_CRYPTO_ED25519 ?= y
5253
CFG_CRYPTO_X25519 ?= y
5354
endif
5455

@@ -167,7 +168,7 @@ core-ltc-vars += SIZE_OPTIMIZATION
167168
core-ltc-vars += SM2_PKE
168169
core-ltc-vars += SM2_DSA
169170
core-ltc-vars += SM2_KEP
170-
core-ltc-vars += X25519
171+
core-ltc-vars += ED25519 X25519
171172
# Assigned selected CFG_CRYPTO_xxx as _CFG_CORE_LTC_xxx
172173
$(foreach v, $(core-ltc-vars), $(eval _CFG_CORE_LTC_$(v) := $(CFG_CRYPTO_$(v))))
173174
_CFG_CORE_LTC_MPI := $(CFG_CORE_MBEDTLS_MPI)
@@ -192,6 +193,7 @@ _CFG_CORE_LTC_XTS := $(CFG_CRYPTO_XTS)
192193
_CFG_CORE_LTC_CCM := $(CFG_CRYPTO_CCM)
193194
_CFG_CORE_LTC_AES_DESC := $(call cfg-one-enabled, CFG_CRYPTO_XTS CFG_CRYPTO_CCM)
194195
$(call force,CFG_CRYPTO_X25519,n,not supported by mbedtls)
196+
$(call force,CFG_CRYPTO_ED25519,n,not supported by mbedtls)
195197
endif
196198

197199
###############################################################
@@ -232,7 +234,7 @@ _CFG_CORE_LTC_HASH := $(call ltc-one-enabled, MD5 SHA1 SHA224 SHA256 SHA384 \
232234
_CFG_CORE_LTC_MAC := $(call ltc-one-enabled, HMAC CMAC CBC_MAC)
233235
_CFG_CORE_LTC_CBC := $(call ltc-one-enabled, CBC CBC_MAC)
234236
_CFG_CORE_LTC_ASN1 := $(call ltc-one-enabled, RSA DSA ECC)
235-
_CFG_CORE_LTC_EC25519 := $(call ltc-one-enabled, X25519)
237+
_CFG_CORE_LTC_EC25519 := $(call ltc-one-enabled, ED25519 X25519)
236238

237239
###############################################################
238240
# Platform independent crypto-driver configuration

core/crypto/crypto.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,64 @@ TEE_Result crypto_acipher_x25519_shared_secret(struct x25519_keypair
825825
}
826826
#endif
827827

828+
#if !defined(CFG_CRYPTO_ED25519)
829+
TEE_Result crypto_acipher_alloc_ed25519_keypair(struct ed25519_keypair *key
830+
__unused,
831+
size_t key_size_bits __unused)
832+
{
833+
return TEE_ERROR_NOT_IMPLEMENTED;
834+
}
835+
836+
TEE_Result crypto_acipher_gen_ed25519_key(struct ed25519_keypair *key __unused,
837+
size_t key_size __unused)
838+
{
839+
return TEE_ERROR_NOT_IMPLEMENTED;
840+
}
841+
842+
TEE_Result crypto_acipher_ed25519_sign(struct ed25519_keypair *key __unused,
843+
const uint8_t *msg __unused,
844+
size_t msg_len __unused,
845+
uint8_t *sig __unused,
846+
size_t *sig_len __unused)
847+
{
848+
return TEE_ERROR_NOT_IMPLEMENTED;
849+
}
850+
851+
TEE_Result crypto_acipher_ed25519_verify(struct ed25519_keypair *key __unused,
852+
const uint8_t *msg __unused,
853+
size_t msg_len __unused,
854+
const uint8_t *sig __unused,
855+
size_t sig_len __unused)
856+
{
857+
return TEE_ERROR_NOT_IMPLEMENTED;
858+
}
859+
860+
TEE_Result crypto_acipher_ed25519ctx_sign(struct ed25519_keypair *key __unused,
861+
const uint8_t *msg __unused,
862+
size_t msg_len __unused,
863+
uint8_t *sig __unused,
864+
size_t *sig_len __unused,
865+
bool ph_flag __unused,
866+
const uint8_t *ctx __unused,
867+
size_t ctxlen __unused)
868+
{
869+
return TEE_ERROR_NOT_IMPLEMENTED;
870+
}
871+
872+
TEE_Result crypto_acipher_ed25519ctx_verify(struct ed25519_keypair *key
873+
__unused,
874+
const uint8_t *msg __unused,
875+
size_t msg_len __unused,
876+
const uint8_t *sig __unused,
877+
size_t sig_len __unused,
878+
bool ph_flag __unused,
879+
const uint8_t *ctx __unused,
880+
size_t ctxlen __unused)
881+
{
882+
return TEE_ERROR_NOT_IMPLEMENTED;
883+
}
884+
#endif
885+
828886
__weak void crypto_storage_obj_del(uint8_t *data __unused, size_t len __unused)
829887
{
830888
}

core/include/crypto/crypto.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ struct x25519_keypair {
172172
uint8_t *pub; /* Public value */
173173
};
174174

175+
struct ed25519_keypair {
176+
uint8_t *priv;
177+
uint8_t *pub;
178+
};
179+
175180
/*
176181
* Key allocation functions
177182
* Allocate the bignum's inside a key structure.
@@ -198,6 +203,8 @@ TEE_Result crypto_acipher_alloc_ecc_keypair(struct ecc_keypair *s,
198203
void crypto_acipher_free_ecc_public_key(struct ecc_public_key *s);
199204
TEE_Result crypto_acipher_alloc_x25519_keypair(struct x25519_keypair *s,
200205
size_t key_size_bits);
206+
TEE_Result crypto_acipher_alloc_ed25519_keypair(struct ed25519_keypair *s,
207+
size_t key_size_bits);
201208

202209
/*
203210
* Key generation functions
@@ -209,6 +216,24 @@ TEE_Result crypto_acipher_gen_dh_key(struct dh_keypair *key, struct bignum *q,
209216
TEE_Result crypto_acipher_gen_ecc_key(struct ecc_keypair *key, size_t key_size);
210217
TEE_Result crypto_acipher_gen_x25519_key(struct x25519_keypair *key,
211218
size_t key_size);
219+
TEE_Result crypto_acipher_gen_ed25519_key(struct ed25519_keypair *key,
220+
size_t key_size);
221+
TEE_Result crypto_acipher_ed25519_sign(struct ed25519_keypair *key,
222+
const uint8_t *msg, size_t msg_len,
223+
uint8_t *sig, size_t *sig_len);
224+
TEE_Result crypto_acipher_ed25519ctx_sign(struct ed25519_keypair *key,
225+
const uint8_t *msg, size_t msg_len,
226+
uint8_t *sig, size_t *sig_len,
227+
bool ph_flag,
228+
const uint8_t *ctx, size_t ctxlen);
229+
TEE_Result crypto_acipher_ed25519_verify(struct ed25519_keypair *key,
230+
const uint8_t *msg, size_t msg_len,
231+
const uint8_t *sig, size_t sig_len);
232+
TEE_Result crypto_acipher_ed25519ctx_verify(struct ed25519_keypair *key,
233+
const uint8_t *msg, size_t msg_len,
234+
const uint8_t *sig, size_t sig_len,
235+
bool ph_flag,
236+
const uint8_t *ctx, size_t ctxlen);
212237

213238
TEE_Result crypto_acipher_dh_shared_secret(struct dh_keypair *private_key,
214239
struct bignum *public_key,

core/lib/libtomcrypt/ed25519.c

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// SPDX-License-Identifier: BSD-2-Clause
2+
/*
3+
* Copyright (c) 2022, Technology Innovation Institute (TII)
4+
* Copyright (c) 2022, EPAM Systems
5+
*/
6+
7+
#include <crypto/crypto.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
#include <string_ext.h>
11+
#include <tee_api_types.h>
12+
#include <trace.h>
13+
#include <utee_defines.h>
14+
15+
#include "acipher_helpers.h"
16+
17+
#define ED25519_KEY_SIZE UL(256)
18+
19+
TEE_Result crypto_acipher_alloc_ed25519_keypair(struct ed25519_keypair *key,
20+
size_t key_size)
21+
{
22+
if (!key || key_size != ED25519_KEY_SIZE)
23+
return TEE_ERROR_BAD_PARAMETERS;
24+
25+
memset(key, 0, sizeof(*key));
26+
27+
key->priv = calloc(1, key_size >> 3);
28+
key->pub = calloc(1, key_size >> 3);
29+
30+
if (!key->priv || !key->pub) {
31+
free(key->priv);
32+
free(key->pub);
33+
return TEE_ERROR_OUT_OF_MEMORY;
34+
}
35+
36+
return TEE_SUCCESS;
37+
}
38+
39+
TEE_Result crypto_acipher_gen_ed25519_key(struct ed25519_keypair *key,
40+
size_t key_size)
41+
{
42+
curve25519_key ltc_tmp_key = { };
43+
44+
if (key_size != ED25519_KEY_SIZE)
45+
return TEE_ERROR_BAD_PARAMETERS;
46+
47+
if (ed25519_make_key(NULL, find_prng("prng_crypto"),
48+
&ltc_tmp_key) != CRYPT_OK)
49+
return TEE_ERROR_BAD_PARAMETERS;
50+
51+
assert(key_size >= sizeof(ltc_tmp_key.pub) &&
52+
key_size >= sizeof(ltc_tmp_key.priv));
53+
54+
memcpy(key->pub, ltc_tmp_key.pub, sizeof(ltc_tmp_key.pub));
55+
memcpy(key->priv, ltc_tmp_key.priv, sizeof(ltc_tmp_key.priv));
56+
memzero_explicit(&ltc_tmp_key, sizeof(ltc_tmp_key));
57+
58+
return TEE_SUCCESS;
59+
}
60+
61+
TEE_Result crypto_acipher_ed25519_sign(struct ed25519_keypair *key,
62+
const uint8_t *msg, size_t msg_len,
63+
uint8_t *sig, size_t *sig_len)
64+
{
65+
int err;
66+
unsigned long siglen;
67+
curve25519_key private_key = {
68+
.type = PK_PRIVATE,
69+
.algo = LTC_OID_ED25519,
70+
};
71+
72+
if (!key)
73+
return TEE_ERROR_BAD_PARAMETERS;
74+
75+
memcpy(private_key.priv, key->priv, sizeof(private_key.priv));
76+
memcpy(private_key.pub, key->pub, sizeof(private_key.pub));
77+
78+
err = ed25519_sign(msg, msg_len, sig, &siglen, &private_key);
79+
80+
memzero_explicit(&private_key, sizeof(private_key));
81+
82+
if (err != CRYPT_OK)
83+
return TEE_ERROR_BAD_PARAMETERS;
84+
*sig_len = siglen;
85+
return TEE_SUCCESS;
86+
}
87+
88+
TEE_Result crypto_acipher_ed25519ctx_sign(struct ed25519_keypair *key,
89+
const uint8_t *msg, size_t msg_len,
90+
uint8_t *sig, size_t *sig_len,
91+
bool ph_flag,
92+
const uint8_t *ctx, size_t ctxlen)
93+
{
94+
int err = CRYPT_ERROR;
95+
unsigned long siglen;
96+
curve25519_key private_key = {
97+
.type = PK_PRIVATE,
98+
.algo = LTC_OID_ED25519,
99+
};
100+
101+
if (!key)
102+
return TEE_ERROR_BAD_PARAMETERS;
103+
104+
memcpy(private_key.priv, key->priv, sizeof(private_key.priv));
105+
memcpy(private_key.pub, key->pub, sizeof(private_key.pub));
106+
107+
if (ph_flag) {
108+
err = ed25519ph_sign(msg, msg_len, sig, &siglen,
109+
ctx, ctxlen, &private_key);
110+
} else {
111+
err = ed25519ctx_sign(msg, msg_len, sig, &siglen,
112+
ctx, ctxlen, &private_key);
113+
}
114+
115+
memzero_explicit(&private_key, sizeof(private_key));
116+
117+
if (err != CRYPT_OK)
118+
return TEE_ERROR_BAD_PARAMETERS;
119+
*sig_len = siglen;
120+
return TEE_SUCCESS;
121+
}
122+
123+
TEE_Result crypto_acipher_ed25519_verify(struct ed25519_keypair *key,
124+
const uint8_t *msg, size_t msg_len,
125+
const uint8_t *sig, size_t sig_len)
126+
{
127+
int stat = 0;
128+
curve25519_key public_key = {
129+
.type = PK_PUBLIC,
130+
.algo = LTC_OID_ED25519,
131+
};
132+
133+
if (!key)
134+
return TEE_ERROR_BAD_PARAMETERS;
135+
136+
memcpy(public_key.pub, key->pub, sizeof(public_key.pub));
137+
138+
if (ed25519_verify(msg, msg_len, sig, sig_len, &stat,
139+
&public_key) != CRYPT_OK)
140+
return TEE_ERROR_BAD_PARAMETERS;
141+
142+
if (stat != 1)
143+
return TEE_ERROR_SIGNATURE_INVALID;
144+
145+
return TEE_SUCCESS;
146+
}
147+
148+
TEE_Result crypto_acipher_ed25519ctx_verify(struct ed25519_keypair *key,
149+
const uint8_t *msg, size_t msg_len,
150+
const uint8_t *sig, size_t sig_len,
151+
bool ph_flag,
152+
const uint8_t *ctx, size_t ctxlen)
153+
{
154+
int stat = 0;
155+
curve25519_key public_key = {
156+
.type = PK_PUBLIC,
157+
.algo = LTC_OID_ED25519,
158+
};
159+
160+
if (!key)
161+
return TEE_ERROR_BAD_PARAMETERS;
162+
163+
memcpy(public_key.pub, key->pub, sizeof(public_key.pub));
164+
165+
if (ph_flag) {
166+
if (ed25519ph_verify(msg, msg_len, sig, sig_len, ctx, ctxlen,
167+
&stat, &public_key) != CRYPT_OK)
168+
return TEE_ERROR_BAD_PARAMETERS;
169+
} else {
170+
if (ed25519ctx_verify(msg, msg_len, sig, sig_len, ctx, ctxlen,
171+
&stat, &public_key) != CRYPT_OK)
172+
return TEE_ERROR_BAD_PARAMETERS;
173+
}
174+
175+
if (stat != 1)
176+
return TEE_ERROR_SIGNATURE_INVALID;
177+
178+
return TEE_SUCCESS;
179+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
srcs-y += ec25519_export.c
2+
srcs-y += ec25519_crypto_ctx.c
23
srcs-y += tweetnacl.c

core/lib/libtomcrypt/src/pk/ed25519/ed25519_make_key.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ int ed25519_make_key(prng_state *prng, int wprng, curve25519_key *key)
2020
{
2121
int err;
2222

23-
LTC_ARGCHK(prng != NULL);
2423
LTC_ARGCHK(key != NULL);
2524

2625
if ((err = tweetnacl_crypto_sign_keypair(prng, wprng, key->pub, key->priv)) != CRYPT_OK) {

core/lib/libtomcrypt/src/pk/ed25519/ed25519_verify.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static int s_ed25519_verify(const unsigned char *msg, unsigned long msglen,
4545
public_key->pub);
4646

4747
#ifdef LTC_CLEAN_STACK
48-
zeromem(m, msglen + siglen);
48+
zeromem(m, msglen);
4949
#endif
5050
XFREE(m);
5151

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
srcs-y += ed25519_export.c
2+
srcs-y += ed25519_import.c
3+
srcs-y += ed25519_import_pkcs8.c
4+
srcs-y += ed25519_import_x509.c
5+
srcs-y += ed25519_make_key.c
6+
srcs-y += ed25519_sign.c
7+
srcs-y += ed25519_verify.c

core/lib/libtomcrypt/src/pk/sub.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ subdirs-$(_CFG_CORE_LTC_DH) += dh
77
subdirs-$(_CFG_CORE_LTC_ECC) += ecc
88
subdirs-$(_CFG_CORE_LTC_X25519) += ec25519
99
subdirs-$(_CFG_CORE_LTC_X25519) += x25519
10+
subdirs-$(_CFG_CORE_LTC_ED25519) += ec25519
11+
subdirs-$(_CFG_CORE_LTC_ED25519) += ed25519

core/lib/libtomcrypt/sub.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ ifeq ($(_CFG_CORE_LTC_ECC),y)
9595
cppflags-lib-y += -DLTC_ECC256
9696
cppflags-lib-y += -DLTC_ECC384
9797
cppflags-lib-y += -DLTC_ECC521
98+
cppflags-lib-y += -DLTC_CURVE25519
9899

99100
# ECC 521 bits is the max supported key size
100101
cppflags-lib-y += -DLTC_MAX_ECC=521
@@ -104,6 +105,7 @@ ifneq (,$(filter y,$(_CFG_CORE_LTC_SM2_DSA) $(_CFG_CORE_LTC_SM2_PKE)))
104105
endif
105106

106107
cppflags-lib-$(_CFG_CORE_LTC_X25519) += -DLTC_CURVE25519
108+
cppflags-lib-$(_CFG_CORE_LTC_ED25519) += -DLTC_CURVE25519
107109

108110
cppflags-lib-y += -DLTC_NO_PRNGS -DLTC_FORTUNA
109111

@@ -134,6 +136,7 @@ srcs-$(_CFG_CORE_LTC_SM2_DSA) += sm2-dsa.c
134136
srcs-$(_CFG_CORE_LTC_SM2_PKE) += sm2-pke.c
135137
srcs-$(_CFG_CORE_LTC_SM2_KEP) += sm2-kep.c
136138
srcs-$(_CFG_CORE_LTC_X25519) += x25519.c
139+
srcs-$(_CFG_CORE_LTC_ED25519) += ed25519.c
137140
ifeq ($(_CFG_CORE_LTC_ACIPHER),y)
138141
srcs-y += mpi_desc.c
139142
endif

0 commit comments

Comments
 (0)