ENCRYPTO_utils icon indicating copy to clipboard operation
ENCRYPTO_utils copied to clipboard

Non-threadsafe function `hash_ctr`

Open Jamie-Cui opened this issue 6 years ago • 0 comments

Hi,

Recently I continuously get calculation error from ABY and I believe such error is caused by a non-threadsafe function at ENCRYPTO_utils/crypto/crypto.cpp.

I pasted the code block below for convenience.

void crypto::hash_ctr(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint64_t ctr) {
	uint8_t* tmpbuf = (uint8_t*) malloc(ninbytes + sizeof(uint64_t));
	memcpy(tmpbuf, &ctr, sizeof(uint64_t));
	memcpy(tmpbuf + sizeof(uint64_t), inbuf, ninbytes);
	hash_routine(resbuf, noutbytes, tmpbuf, ninbytes+sizeof(uint64_t), sha_hash_buf);
	free(tmpbuf);
}

Changing to the following sovles the issue,

void crypto::hash_ctr(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint64_t ctr) {
	uint8_t* hash_buf = (uint8_t*) malloc(get_hash_bytes()); 
	uint8_t* tmpbuf = (uint8_t*) malloc(ninbytes + sizeof(uint64_t));
	memcpy(tmpbuf, &ctr, sizeof(uint64_t));
	memcpy(tmpbuf + sizeof(uint64_t), inbuf, ninbytes);
	hash_routine(resbuf, noutbytes, tmpbuf, ninbytes+sizeof(uint64_t), hash_buf);
	free(tmpbuf);
        free(hash_buf);
}

I've also opened an issue in ABY: https://github.com/encryptogroup/ABY/issues/152 Thanks, Jamie

Jamie-Cui avatar Nov 20 '19 09:11 Jamie-Cui