From 0deb7637c6d8caab010515a0efea4e7875ea3f1c Mon Sep 17 00:00:00 2001 From: Michael Zohner Date: Wed, 10 Feb 2016 18:58:03 +0100 Subject: [PATCH] Various changes --- src/util/crypto/crypto.cpp | 18 +++++++++++------- src/util/crypto/crypto.h | 6 +++++- src/util/crypto/gmp-pk-crypto.cpp | 4 ++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/util/crypto/crypto.cpp b/src/util/crypto/crypto.cpp index 45adc3f..15eac9f 100644 --- a/src/util/crypto/crypto.cpp +++ b/src/util/crypto/crypto.cpp @@ -45,20 +45,24 @@ void crypto::init(uint32_t symsecbits, uint8_t* seed) { aes_hash_buf_y1 = (uint8_t*) malloc(AES_BYTES); aes_hash_buf_y2 = (uint8_t*) malloc(AES_BYTES); - sha_hash_buf = (uint8_t*) malloc((secparam.symbits >> 3 ) * 2); - - if(secparam.symbits == ST.symbits) { + if (secparam.symbits == ST.symbits) { hash_routine = &sha1_hash; - } else if(secparam.symbits == MT.symbits) { + sha_hash_buf = (uint8_t*) malloc(SHA1_OUT_BYTES); + } else if (secparam.symbits == MT.symbits) { hash_routine = &sha256_hash; - } else if(secparam.symbits == LT.symbits) { + sha_hash_buf = (uint8_t*) malloc(SHA256_OUT_BYTES); + } else if (secparam.symbits == LT.symbits) { hash_routine = &sha256_hash; - } else if(secparam.symbits == XLT.symbits) { + sha_hash_buf = (uint8_t*) malloc(SHA256_OUT_BYTES); + } else if (secparam.symbits == XLT.symbits) { hash_routine = &sha512_hash; - } else if(secparam.symbits == XXLT.symbits) { + sha_hash_buf = (uint8_t*) malloc(SHA512_OUT_BYTES); + } else if (secparam.symbits == XXLT.symbits) { hash_routine = &sha512_hash; + sha_hash_buf = (uint8_t*) malloc(SHA512_OUT_BYTES); } else { hash_routine = &sha256_hash; + sha_hash_buf = (uint8_t*) malloc(SHA256_OUT_BYTES); } } diff --git a/src/util/crypto/crypto.h b/src/util/crypto/crypto.h index 25ace6a..90d7652 100644 --- a/src/util/crypto/crypto.h +++ b/src/util/crypto/crypto.h @@ -27,6 +27,10 @@ #define AES_BYTES 16 #define AES_BITS AES_BYTES*8 +#define SHA1_OUT_BYTES 20 +#define SHA256_OUT_BYTES 32 +#define SHA512_OUT_BYTES 64 + const uint8_t ZERO_IV[AES_BYTES]={0}; const uint8_t const_seed[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; @@ -133,7 +137,7 @@ static void InitAndReadCodeWord(REGISTER_SIZE*** codewords) { uint32_t ncodewords = m_nCodeWordBits; uint32_t ncwintlen = 8; *codewords = (REGISTER_SIZE**) malloc(sizeof(REGISTER_SIZE*) * ncodewords); - for(uint32_t i = 0; i < m_nCodewords; i++) { + for(uint32_t i = 0; i < ncodewords; i++) { (*codewords)[i] = (REGISTER_SIZE*) malloc(sizeof(REGISTER_SIZE) * ((ncwintlen * sizeof(uint32_t)) / sizeof(REGISTER_SIZE))); } readCodeWords(*codewords); diff --git a/src/util/crypto/gmp-pk-crypto.cpp b/src/util/crypto/gmp-pk-crypto.cpp index 5329b93..900193d 100644 --- a/src/util/crypto/gmp-pk-crypto.cpp +++ b/src/util/crypto/gmp-pk-crypto.cpp @@ -186,7 +186,7 @@ void prime_field::init(seclvl sp, uint8_t* seed) { mpz_init(rnd_seed); secparam = sp; - mpz_import(rnd_seed, secparam.symbits, 1, sizeof((seed)[0]), 0, 0, seed); + mpz_import(rnd_seed, ceil_divide(secparam.symbits, 8), 1, sizeof((seed)[0]), 0, 0, seed); if (secparam.ifcbits == ST.ifcbits) { mpz_set_str(p, ifcp1024, 16); @@ -204,7 +204,7 @@ void prime_field::init(seclvl sp, uint8_t* seed) { { mpz_set_str(p, ifcp3072, 10); mpz_set_str(g, ifcg3072, 10); - mpz_set_str(q, ifcq1024, 10); + mpz_set_str(q, ifcq3072, 10); } //TODO it would be better not to GMP's internal random generator, since it is not secure