Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/encauth/eax/eax_decrypt_verify_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int eax_decrypt_verify_memory(int cipher,
{
int err;
eax_state *eax;
unsigned char *buf;
unsigned char buf[MAXBLOCKSIZE];
unsigned long buflen;

/* stat is cleared before anything else can return, it stays 0 unless the tag is good */
Expand All @@ -57,16 +57,8 @@ int eax_decrypt_verify_memory(int cipher,
return CRYPT_INVALID_ARG;
}

/* allocate ram */
buf = XMALLOC(taglen);
eax = XMALLOC(sizeof(*eax));
if (eax == NULL || buf == NULL) {
if (eax != NULL) {
XFREE(eax);
}
if (buf != NULL) {
XFREE(buf);
}
if (eax == NULL) {
return CRYPT_MEM;
}

Expand All @@ -91,12 +83,11 @@ int eax_decrypt_verify_memory(int cipher,
err = CRYPT_OK;
LBL_ERR:
#ifdef LTC_CLEAN_STACK
zeromem(buf, taglen);
zeromem(buf, sizeof(buf));
zeromem(eax, sizeof(*eax));
#endif

XFREE(eax);
XFREE(buf);

return err;
}
Expand Down
12 changes: 2 additions & 10 deletions src/encauth/ocb3/ocb3_decrypt_verify_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int ocb3_decrypt_verify_memory(int cipher,
{
int err;
ocb3_state *ocb;
unsigned char *buf;
unsigned char buf[MAXBLOCKSIZE];
unsigned long buflen;

/* stat is cleared before anything else can return, it stays 0 unless the tag is good */
Expand All @@ -48,15 +48,8 @@ int ocb3_decrypt_verify_memory(int cipher,
taglen = MIN(taglen, MAXBLOCKSIZE);

/* allocate memory */
buf = XMALLOC(taglen);
ocb = XMALLOC(sizeof(ocb3_state));
if (ocb == NULL || buf == NULL) {
if (ocb != NULL) {
XFREE(ocb);
}
if (buf != NULL) {
XFREE(buf);
}
if (ocb == NULL) {
return CRYPT_MEM;
}

Expand Down Expand Up @@ -92,7 +85,6 @@ int ocb3_decrypt_verify_memory(int cipher,
#endif

XFREE(ocb);
XFREE(buf);
return err;
}

Expand Down
4 changes: 2 additions & 2 deletions src/encauth/siv/siv.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ int siv_decrypt_memory( int cipher,
return err;
}
*ptlen = ctlen - 16;
pt_work = XMALLOC(*ptlen);
pt_work = XMALLOC(*ptlen ? *ptlen : 1); /* XMALLOC(0) may return NULL */
if (pt_work == NULL) {
return CRYPT_MEM;
}
Expand Down Expand Up @@ -450,7 +450,7 @@ int siv_memory( int cipher, int direction,
} else {
buf_len = inlen;
}
buf = XMALLOC(buf_len);
buf = XMALLOC(buf_len ? buf_len : 1); /* XMALLOC(0) may return NULL */
if (buf == NULL)
return CRYPT_MEM;

Expand Down
2 changes: 1 addition & 1 deletion src/misc/pem/pem_ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ int ssh_read_authorized_keys_filehandle(FILE *f, ssh_authorized_key_cb cb, void
tot_data = ftell(f);
if (fseek(f, 0, SEEK_SET) == -1)
return CRYPT_ERROR;
buf = XMALLOC(tot_data);
buf = XMALLOC(tot_data ? tot_data : 1); /* XMALLOC(0) may return NULL */
if (buf == NULL) {
return CRYPT_MEM;
}
Expand Down
4 changes: 2 additions & 2 deletions src/pk/ec25519/ec25519_import_pkcs8.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ int ec25519_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key,
}

key_len = sizeof(key->priv);
if ((err = der_decode_octet_string(priv_key->data, priv_key->size, key->priv, &key_len)) == CRYPT_OK) {
fp(key->pub, key->priv);
if ((err = der_decode_octet_string(priv_key->data, priv_key->size, key->priv, &key_len)) == CRYPT_OK
&& (err = fp(key->pub, key->priv)) == CRYPT_OK) {
key->type = PK_PRIVATE;
err = pk_get_pka_id(id, &key->pka);
}
Expand Down
22 changes: 15 additions & 7 deletions src/pk/ec25519/tweetnacl.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ static LTC_INLINE int tweetnacl_crypto_hash_ctx(u8 *out,const u8 *m,u64 n,const
if(cs == 0)
return hash_memory(hash_idx, m, n, out, &len);

return hash_memory_multi(hash_idx, out, &len, ctx, cs, m, n, LTC_NULL);
/* varargs lengths are read as unsigned long, a u64 breaks on 32-bit big-endian */
return hash_memory_multi(hash_idx, out, &len, ctx, cs, m, (unsigned long)n, LTC_NULL);
}

static LTC_INLINE int tweetnacl_crypto_hash(u8 *out,const u8 *m,u64 n)
Expand Down Expand Up @@ -315,8 +316,9 @@ int tweetnacl_crypto_sk_to_pk(u8 *pk, const u8 *sk)
{
u8 d[64];
gf p[4];
int err;
if (find_hash("sha512") == -1) return CRYPT_INVALID_HASH;
tweetnacl_crypto_hash(d, sk, 32);
if ((err = tweetnacl_crypto_hash(d, sk, 32)) != CRYPT_OK) return err;
d[0] &= 248;
d[31] &= 127;
d[31] |= 64;
Expand Down Expand Up @@ -400,10 +402,11 @@ int tweetnacl_crypto_sign(u8 *sm,u64 *smlen,const u8 *m,u64 mlen,const u8 *sk,co
u8 d[64],h[64],r[64];
i64 i,j,x[64];
gf p[4];
int err;

if (find_hash("sha512") == -1) return CRYPT_INVALID_HASH;

tweetnacl_crypto_hash(d, sk, 32);
if ((err = tweetnacl_crypto_hash(d, sk, 32)) != CRYPT_OK) return err;
d[0] &= 248;
d[31] &= 127;
d[31] |= 64;
Expand All @@ -412,13 +415,14 @@ int tweetnacl_crypto_sign(u8 *sm,u64 *smlen,const u8 *m,u64 mlen,const u8 *sk,co
FOR(i,(i64)mlen) sm[64 + i] = m[i];
FOR(i,32) sm[32 + i] = d[32 + i];

tweetnacl_crypto_hash_ctx(r, sm+32, mlen+32,ctx,cs);
err = tweetnacl_crypto_hash_ctx(r, sm+32, mlen+32,ctx,cs);
FOR(i,32) sm[i+32] = pk[i]; /* overwrite the secret prefix before any return */
if (err != CRYPT_OK) return err;
reduce(r);
scalarbase(p,r);
pack(sm,p);

FOR(i,32) sm[i+32] = pk[i];
tweetnacl_crypto_hash_ctx(h,sm,mlen + 64,ctx,cs);
if ((err = tweetnacl_crypto_hash_ctx(h,sm,mlen + 64,ctx,cs)) != CRYPT_OK) return err;
reduce(h);

FOR(i,64) x[i] = 0;
Expand Down Expand Up @@ -470,6 +474,7 @@ int tweetnacl_crypto_sign_open(int *stat, u8 *m,u64 *mlen,const u8 *sm,u64 smlen
u64 i;
u8 s[32],t[32],h[64];
gf p[4],q[4];
int err;

*stat = 0;
if (find_hash("sha512") == -1) return CRYPT_INVALID_HASH;
Expand All @@ -483,7 +488,10 @@ int tweetnacl_crypto_sign_open(int *stat, u8 *m,u64 *mlen,const u8 *sm,u64 smlen
XMEMMOVE(m,sm,smlen);
XMEMMOVE(s,m + 32,32);
XMEMMOVE(m + 32,pk,32);
tweetnacl_crypto_hash_ctx(h,m,smlen,ctx,cs);
if ((err = tweetnacl_crypto_hash_ctx(h,m,smlen,ctx,cs)) != CRYPT_OK) {
zeromem(m, smlen);
return err;
}
reduce(h);
scalarmult(p,q,h);

Expand Down
2 changes: 1 addition & 1 deletion src/pk/ecc/ecc_rfc6979_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ int ecc_rfc6979_key(const ecc_key *priv, const unsigned char *in, unsigned long
/* K = HMAC_K(V::0x00) */
buffer[0] = 0x0;
outlen = sizeof(k);
if((err = hmac_memory_multi(hash, k, klen, k, &klen, v, hashsize, buffer, 1, LTC_NULL)) != CRYPT_OK) { goto error; }
if((err = hmac_memory_multi(hash, k, klen, k, &klen, v, hashsize, buffer, 1uL, LTC_NULL)) != CRYPT_OK) { goto error; }

/* V = HMAC_K(V) */
outlen = sizeof(v);
Expand Down
6 changes: 5 additions & 1 deletion src/pk/ed25519/ed25519_import_raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
*/
int ed25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key)
{
int err;

LTC_ARGCHK(in != NULL);
LTC_ARGCHK(key != NULL);

if (which == PK_PRIVATE) {
LTC_ARGCHK(inlen == 32uL || inlen == 64uL);
XMEMCPY(key->priv, in, sizeof(key->priv));
if (inlen == 32) {
tweetnacl_crypto_sk_to_pk(key->pub, key->priv);
if ((err = tweetnacl_crypto_sk_to_pk(key->pub, key->priv)) != CRYPT_OK) {
return err;
}
} else {
XMEMCPY(key->pub, in + 32, sizeof(key->pub));
}
Expand Down
6 changes: 4 additions & 2 deletions src/pk/ed25519/ed25519_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ static int s_ed25519_sign(const unsigned char *msg, unsigned long msglen,
private_key->priv, private_key->pub,
ctx, ctxlen);

XMEMCPY(sig, s, 64uL);
*siglen = 64uL;
if (err == CRYPT_OK) {
XMEMCPY(sig, s, 64uL);
*siglen = 64uL;
}

#ifdef LTC_CLEAN_STACK
zeromem(s, smlen);
Expand Down
Loading