Skip to content

Commit

Permalink
re-factor tweetnacl_crypto_hash[_ctx]()
Browse files Browse the repository at this point in the history
@etienne-lms remarked in [0] that the stack usage could be minimized
by using `hash_memory_multi()` instead of copying the data, so let's do
that.

[0] OP-TEE/optee_os#5486 (comment)

Signed-off-by: Steffen Jaeckel <[email protected]>
  • Loading branch information
sjaeckel committed Sep 2, 2022
1 parent 1ee85a8 commit 085415d
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions src/pk/ec25519/tweetnacl.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,39 +221,22 @@ int tweetnacl_crypto_scalarmult_base(u8 *q,const u8 *n)
return tweetnacl_crypto_scalarmult(q,n,nine);
}

static int tweetnacl_crypto_hash(u8 *out,const u8 *m,u64 n)
static LTC_INLINE int tweetnacl_crypto_hash_ctx(u8 *out,const u8 *m,u64 n,const u8 *ctx,u32 cs)
{
unsigned long len;
int err, hash_idx;
unsigned long len = 64;
int hash_idx = find_hash("sha512");

if (n > ULONG_MAX) return CRYPT_OVERFLOW;

hash_idx = find_hash("sha512");
len = 64;
if ((err = hash_memory(hash_idx, m, n, out, &len)) != CRYPT_OK) return err;
if(cs == 0)
return hash_memory(hash_idx, m, n, out, &len);

return 0;
return hash_memory_multi(hash_idx, out, &len, ctx, cs, m, n, LTC_NULL);
}

static int tweetnacl_crypto_hash_ctx(u8 *out,const u8 *m,u64 n,const u8 *ctx,u32 cs)
static LTC_INLINE int tweetnacl_crypto_hash(u8 *out,const u8 *m,u64 n)
{
unsigned long len;
int err;
u8 buf[512];

if(cs == 0)
return tweetnacl_crypto_hash(out,m,n);

len = n + cs;
if (len > 512) return CRYPT_HASH_OVERFLOW;

XMEMCPY(buf,ctx,cs);
XMEMCPY(buf+cs,m,n);

err = tweetnacl_crypto_hash(out,buf,len);
zeromem(buf, len);

return err;
return tweetnacl_crypto_hash_ctx(out, m, n, NULL, 0);
}

sv add(gf p[4],gf q[4])
Expand Down

0 comments on commit 085415d

Please sign in to comment.