diff --git a/src/encauth/eax/eax_decrypt_verify_memory.c b/src/encauth/eax/eax_decrypt_verify_memory.c index 9c4203a00..d65c48f12 100644 --- a/src/encauth/eax/eax_decrypt_verify_memory.c +++ b/src/encauth/eax/eax_decrypt_verify_memory.c @@ -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 */ @@ -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; } @@ -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; } diff --git a/src/encauth/ocb3/ocb3_decrypt_verify_memory.c b/src/encauth/ocb3/ocb3_decrypt_verify_memory.c index 93c63a28a..93d2496ae 100644 --- a/src/encauth/ocb3/ocb3_decrypt_verify_memory.c +++ b/src/encauth/ocb3/ocb3_decrypt_verify_memory.c @@ -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 */ @@ -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; } @@ -92,7 +85,6 @@ int ocb3_decrypt_verify_memory(int cipher, #endif XFREE(ocb); - XFREE(buf); return err; } diff --git a/src/encauth/siv/siv.c b/src/encauth/siv/siv.c index 66e7967c3..01c22a161 100644 --- a/src/encauth/siv/siv.c +++ b/src/encauth/siv/siv.c @@ -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; } @@ -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; diff --git a/src/misc/pem/pem_ssh.c b/src/misc/pem/pem_ssh.c index 9830aeabd..22604a4c9 100644 --- a/src/misc/pem/pem_ssh.c +++ b/src/misc/pem/pem_ssh.c @@ -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; } diff --git a/src/pk/ec25519/ec25519_import_pkcs8.c b/src/pk/ec25519/ec25519_import_pkcs8.c index 9f9a6bcb8..df3e4013e 100644 --- a/src/pk/ec25519/ec25519_import_pkcs8.c +++ b/src/pk/ec25519/ec25519_import_pkcs8.c @@ -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); } diff --git a/src/pk/ec25519/tweetnacl.c b/src/pk/ec25519/tweetnacl.c index 02df12cd2..e0a4173c2 100644 --- a/src/pk/ec25519/tweetnacl.c +++ b/src/pk/ec25519/tweetnacl.c @@ -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) @@ -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; @@ -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; @@ -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; @@ -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; @@ -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); diff --git a/src/pk/ecc/ecc_rfc6979_key.c b/src/pk/ecc/ecc_rfc6979_key.c index 1f313bd34..664665a58 100644 --- a/src/pk/ecc/ecc_rfc6979_key.c +++ b/src/pk/ecc/ecc_rfc6979_key.c @@ -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); diff --git a/src/pk/ed25519/ed25519_import_raw.c b/src/pk/ed25519/ed25519_import_raw.c index 6d470739d..88d17fe16 100644 --- a/src/pk/ed25519/ed25519_import_raw.c +++ b/src/pk/ed25519/ed25519_import_raw.c @@ -20,6 +20,8 @@ */ 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); @@ -27,7 +29,9 @@ int ed25519_import_raw(const unsigned char *in, unsigned long inlen, int which, 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)); } diff --git a/src/pk/ed25519/ed25519_sign.c b/src/pk/ed25519/ed25519_sign.c index 193bfcdb7..82be9ac5d 100644 --- a/src/pk/ed25519/ed25519_sign.c +++ b/src/pk/ed25519/ed25519_sign.c @@ -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);