Răsfoiți Sursa

re-factor `tweetnacl_crypto_hash[_ctx]()`

@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] https://github.com/OP-TEE/optee_os/pull/5486#discussion_r955095821

Signed-off-by: Steffen Jaeckel <[email protected]>
Steffen Jaeckel 3 ani în urmă
părinte
comite
e836af56c2
1 a modificat fișierele cu 8 adăugiri și 25 ștergeri
  1. 8 25
      src/pk/ec25519/tweetnacl.c

+ 8 - 25
src/pk/ec25519/tweetnacl.c

@@ -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])