Skip to content

Commit

Permalink
fixup! core: crypto: add Ed25519 support
Browse files Browse the repository at this point in the history
  • Loading branch information
sa-kib committed Aug 18, 2022
1 parent aa020d6 commit 43d0098
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions core/crypto/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ TEE_Result crypto_acipher_ed25519ctx_sign(struct ed25519_keypair *key __unused,
size_t msg_len __unused,
uint8_t *sig __unused,
size_t *sig_len __unused,
uint8_t flag __unused,
bool ph_flag __unused,
const uint8_t *ctx __unused)
{
return TEE_ERROR_NOT_IMPLEMENTED;
Expand All @@ -874,7 +874,7 @@ TEE_Result crypto_acipher_ed25519ctx_verify(struct ed25519_keypair *key
size_t msg_len __unused,
const uint8_t *sig __unused,
size_t sig_len __unused,
uint8_t flag __unused,
bool ph_flag __unused,
const uint8_t *ctx __unused)
{
return TEE_ERROR_NOT_IMPLEMENTED;
Expand Down
4 changes: 2 additions & 2 deletions core/include/crypto/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,14 @@ TEE_Result crypto_acipher_ed25519_sign(struct ed25519_keypair *key,
TEE_Result crypto_acipher_ed25519ctx_sign(struct ed25519_keypair *key,
const uint8_t *msg, size_t msg_len,
uint8_t *sig, size_t *sig_len,
uint8_t flag, const uint8_t *ctx);
bool ph_flag, const uint8_t *ctx);
TEE_Result crypto_acipher_ed25519_verify(struct ed25519_keypair *key,
const uint8_t *msg, size_t msg_len,
const uint8_t *sig, size_t sig_len);
TEE_Result crypto_acipher_ed25519ctx_verify(struct ed25519_keypair *key,
const uint8_t *msg, size_t msg_len,
const uint8_t *sig, size_t sig_len,
uint8_t flag, const uint8_t *ctx);
bool ph_flag, const uint8_t *ctx);

TEE_Result crypto_acipher_dh_shared_secret(struct dh_keypair *private_key,
struct bignum *public_key,
Expand Down
8 changes: 4 additions & 4 deletions core/lib/libtomcrypt/ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ TEE_Result crypto_acipher_ed25519_sign(struct ed25519_keypair *key,
TEE_Result crypto_acipher_ed25519ctx_sign(struct ed25519_keypair *key,
const uint8_t *msg, size_t msg_len,
uint8_t *sig, size_t *sig_len,
uint8_t flag, const uint8_t *ctx)
bool ph_flag, const uint8_t *ctx)
{
curve25519_key private_key = {
.type = PK_PRIVATE,
Expand All @@ -102,7 +102,7 @@ TEE_Result crypto_acipher_ed25519ctx_sign(struct ed25519_keypair *key,
memcpy(private_key.priv, key->priv, 32);
memcpy(private_key.pub, key->pub, 32);

if (flag) {
if (ph_flag) {
if (ed25519ph_sign(msg, msg_len, sig, (unsigned long *)sig_len,
ctx, &private_key) != CRYPT_OK)
return TEE_ERROR_BAD_PARAMETERS;
Expand Down Expand Up @@ -143,7 +143,7 @@ TEE_Result crypto_acipher_ed25519_verify(struct ed25519_keypair *key,
TEE_Result crypto_acipher_ed25519ctx_verify(struct ed25519_keypair *key,
const uint8_t *msg, size_t msg_len,
const uint8_t *sig, size_t sig_len,
unsigned char flag,
bool ph_flag,
const uint8_t *ctx)
{
int stat = 0;
Expand All @@ -157,7 +157,7 @@ TEE_Result crypto_acipher_ed25519ctx_verify(struct ed25519_keypair *key,

memcpy(public_key.pub, key->pub, 32);

if (flag) {
if (ph_flag) {
if (ed25519ph_verify(msg, msg_len, sig, sig_len, &stat,
ctx, &public_key) != CRYPT_OK)
return TEE_ERROR_BAD_PARAMETERS;
Expand Down
23 changes: 13 additions & 10 deletions core/tee/tee_svc_cryp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <crypto/crypto.h>
#include <kernel/tee_ta_manager.h>
#include <kernel/user_access.h>
#include <mempool.h>
#include <memtag.h>
#include <mm/vm.h>
#include <stdlib_ext.h>
Expand Down Expand Up @@ -2147,17 +2148,17 @@ tee_svc_obj_generate_key_ed25519(struct tee_obj *o,
uint32_t param_count)
{
TEE_Result res = TEE_ERROR_GENERIC;
struct ed25519_keypair *tee_ed25519_key = NULL;
struct ed25519_keypair *key = NULL;

/* Copy the present attributes into the obj before starting */
res = tee_svc_cryp_obj_populate_type(o, type_props, params,
param_count);
if (res != TEE_SUCCESS)
return res;

tee_ed25519_key = (struct ed25519_keypair *)o->attr;
key = o->attr;

res = crypto_acipher_gen_ed25519_key(tee_ed25519_key, key_size);
res = crypto_acipher_gen_ed25519_key(key, key_size);
if (res != TEE_SUCCESS)
return res;

Expand All @@ -2175,22 +2176,24 @@ tee_svc_obj_ed25519_sign(struct ed25519_keypair *key,
{
size_t n;
size_t ctx_len;
uint8_t ctx[256] = {0};
uint8_t ph_flag = 0;
uint8_t cx_flag = 0;
uint8_t *ctx = NULL;
bool ph_flag = false;
bool cx_flag = false;

for (n = 0u; n < num_params; n++) {
for (n = 0; n < num_params; n++) {
switch (params[n].attributeID) {
case TEE_ATTR_EDDSA_PREHASH:
ph_flag = 1;
ph_flag = true;
break;

case TEE_ATTR_EDDSA_CTX:
cx_flag = 1;
cx_flag = true;
ctx_len = params[n].content.ref.length;
if (ctx_len > 255)
return TEE_ERROR_BAD_PARAMETERS;

ctx = mempool_calloc(mempool_default, 1, 256);
if (!ctx)
return TEE_ERROR_OUT_OF_MEMORY;
memcpy(ctx, params[n].content.ref.buffer, ctx_len);
ctx[ctx_len] = 0;
break;
Expand Down

0 comments on commit 43d0098

Please sign in to comment.