Skip to content

Commit fde3e8c

Browse files
authored
Merge pull request #599 from libtom/rereview-curve25519-ctx-and-ph
Re-review curve25519 ctx and ph
2 parents 288088c + 1873838 commit fde3e8c

File tree

6 files changed

+22
-44
lines changed

6 files changed

+22
-44
lines changed

src/headers/tomcrypt_private.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,13 @@ int tweetnacl_crypto_sign_open(
345345
int *stat,
346346
unsigned char *m,unsigned long long *mlen,
347347
const unsigned char *sm,unsigned long long smlen,
348-
const unsigned char *ctx, unsigned long cs,
348+
const unsigned char *ctx, unsigned long long cs,
349349
const unsigned char *pk);
350350
int tweetnacl_crypto_sign_keypair(prng_state *prng, int wprng, unsigned char *pk,unsigned char *sk);
351351
int tweetnacl_crypto_sk_to_pk(unsigned char *pk, const unsigned char *sk);
352352
int tweetnacl_crypto_scalarmult(unsigned char *q, const unsigned char *n, const unsigned char *p);
353353
int tweetnacl_crypto_scalarmult_base(unsigned char *q,const unsigned char *n);
354-
int tweetnacl_crypto_ph(unsigned char *out, const unsigned char *msg, unsigned long msglen);
354+
int tweetnacl_crypto_ph(unsigned char *out, const unsigned char *msg, unsigned long long msglen);
355355

356356
typedef int (*sk_to_pk)(unsigned char *pk ,const unsigned char *sk);
357357
int ec25519_import_pkcs8(const unsigned char *in, unsigned long inlen,

src/pk/ec25519/ec25519_crypto_ctx.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int ec25519_crypto_ctx(unsigned char *out, unsigned long *outlen, unsigned char
2828
buf++;
2929

3030
if (ctxlen > 0u) {
31+
LTC_ARGCHK(ctx != NULL);
3132
XMEMCPY(buf, ctx, ctxlen);
3233
buf += ctxlen;
3334
}

src/pk/ec25519/tweetnacl.c

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -221,39 +221,22 @@ int tweetnacl_crypto_scalarmult_base(u8 *q,const u8 *n)
221221
return tweetnacl_crypto_scalarmult(q,n,nine);
222222
}
223223

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

229229
if (n > ULONG_MAX) return CRYPT_OVERFLOW;
230230

231-
hash_idx = find_hash("sha512");
232-
len = 64;
233-
if ((err = hash_memory(hash_idx, m, n, out, &len)) != CRYPT_OK) return err;
231+
if(cs == 0)
232+
return hash_memory(hash_idx, m, n, out, &len);
234233

235-
return 0;
234+
return hash_memory_multi(hash_idx, out, &len, ctx, cs, m, n, LTC_NULL);
236235
}
237236

238-
static int tweetnacl_crypto_hash_ctx(u8 *out,const u8 *m,u64 n,const u8 *ctx,u32 cs)
237+
static LTC_INLINE int tweetnacl_crypto_hash(u8 *out,const u8 *m,u64 n)
239238
{
240-
unsigned long len;
241-
int err;
242-
u8 buf[512];
243-
244-
if(cs == 0)
245-
return tweetnacl_crypto_hash(out,m,n);
246-
247-
len = n + cs;
248-
if (len > 512) return CRYPT_HASH_OVERFLOW;
249-
250-
XMEMCPY(buf,ctx,cs);
251-
XMEMCPY(buf+cs,m,n);
252-
253-
err = tweetnacl_crypto_hash(out,buf,len);
254-
zeromem(buf, len);
255-
256-
return err;
239+
return tweetnacl_crypto_hash_ctx(out, m, n, NULL, 0);
257240
}
258241

259242
sv add(gf p[4],gf q[4])
@@ -465,7 +448,7 @@ static int unpackneg(gf r[4],const u8 p[32])
465448
return 0;
466449
}
467450

468-
int tweetnacl_crypto_sign_open(int *stat, u8 *m,u64 *mlen,const u8 *sm,u64 smlen,const u8 *ctx,size_t cs,const u8 *pk)
451+
int tweetnacl_crypto_sign_open(int *stat, u8 *m,u64 *mlen,const u8 *sm,u64 smlen,const u8 *ctx,u64 cs,const u8 *pk)
469452
{
470453
u64 i;
471454
u8 s[32],t[32],h[64];
@@ -502,7 +485,7 @@ int tweetnacl_crypto_sign_open(int *stat, u8 *m,u64 *mlen,const u8 *sm,u64 smlen
502485
return CRYPT_OK;
503486
}
504487

505-
int tweetnacl_crypto_ph(u8 *out,const u8 *msg,size_t msglen)
488+
int tweetnacl_crypto_ph(u8 *out,const u8 *msg,u64 msglen)
506489
{
507490
return tweetnacl_crypto_hash(out, msg, msglen);
508491
}

src/pk/ed25519/ed25519_sign.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int ed25519ctx_sign(const unsigned char *msg, unsigned long msglen,
6767
const curve25519_key *private_key)
6868
{
6969
int err;
70-
unsigned char ctx_prefix[512] = {0};
70+
unsigned char ctx_prefix[292];
7171
unsigned long ctx_prefix_size = sizeof(ctx_prefix);
7272

7373
LTC_ARGCHK(ctx != NULL);
@@ -94,8 +94,8 @@ int ed25519ph_sign(const unsigned char *msg, unsigned long msglen,
9494
const curve25519_key *private_key)
9595
{
9696
int err;
97-
unsigned char ctx_prefix[512] = {0};
98-
unsigned char msg_hash[64] = {0};
97+
unsigned char msg_hash[64];
98+
unsigned char ctx_prefix[292];
9999
unsigned long ctx_prefix_size = sizeof(ctx_prefix);
100100

101101
if ((err = ec25519_crypto_ctx(ctx_prefix, &ctx_prefix_size, 1, ctx, ctxlen)) != CRYPT_OK)
@@ -104,10 +104,7 @@ int ed25519ph_sign(const unsigned char *msg, unsigned long msglen,
104104
if ((err = tweetnacl_crypto_ph(msg_hash, msg, msglen)) != CRYPT_OK)
105105
return err;
106106

107-
msg = msg_hash;
108-
msglen = 64;
109-
110-
return s_ed25519_sign(msg, msglen, sig, siglen, ctx_prefix, ctx_prefix_size, private_key);
107+
return s_ed25519_sign(msg_hash, sizeof(msg_hash), sig, siglen, ctx_prefix, ctx_prefix_size, private_key);
111108
}
112109

113110
/**

src/pk/ed25519/ed25519_verify.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int ed25519ctx_verify(const unsigned char *msg, unsigned long msglen,
7070
int *stat,
7171
const curve25519_key *public_key)
7272
{
73-
unsigned char ctx_prefix[512] = {0};
73+
unsigned char ctx_prefix[292];
7474
unsigned long ctx_prefix_size = sizeof(ctx_prefix);
7575

7676
LTC_ARGCHK(ctx != NULL);
@@ -100,8 +100,8 @@ int ed25519ph_verify(const unsigned char *msg, unsigned long msglen,
100100
const curve25519_key *public_key)
101101
{
102102
int err;
103-
unsigned char ctx_prefix[512] = {0};
104-
unsigned char msg_hash[64] = {0};
103+
unsigned char msg_hash[64];
104+
unsigned char ctx_prefix[292];
105105
unsigned long ctx_prefix_size = sizeof(ctx_prefix);
106106

107107
if ((err = ec25519_crypto_ctx(ctx_prefix, &ctx_prefix_size, 1, ctx, ctxlen)) != CRYPT_OK)
@@ -110,10 +110,7 @@ int ed25519ph_verify(const unsigned char *msg, unsigned long msglen,
110110
if ((err = tweetnacl_crypto_ph(msg_hash, msg, msglen)) != CRYPT_OK)
111111
return err;
112112

113-
msg = msg_hash;
114-
msglen = 64;
115-
116-
return s_ed25519_verify(msg, msglen, sig, siglen, ctx_prefix, ctx_prefix_size, stat, public_key);
113+
return s_ed25519_verify(msg_hash, sizeof(msg_hash), sig, siglen, ctx_prefix, ctx_prefix_size, stat, public_key);
117114
}
118115

119116
/**

tests/multi_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ int multi_test(void)
1515

1616
/* HASH testing */
1717
len = sizeof(buf[0]);
18-
#if defined(ENDIAN_32BITWORD) || defined(_MSC_VER)
18+
#if defined(ENDIAN_32BITWORD) || defined(_WIN32)
1919
len2 = 0x80000000UL;
2020
#else
2121
/* Check against the max. input limit of SHA-1 as of RFC8017 */

0 commit comments

Comments
 (0)