From cc7eae7ce7b29105f3f404ff02172fd353f34115 Mon Sep 17 00:00:00 2001 From: "John M. Schanck" Date: Thu, 10 Sep 2020 17:00:45 -0400 Subject: [PATCH] typo and more endianness fixes --- crypto_kem/hqc-128/avx2/vector.c | 2 +- crypto_kem/hqc-128/clean/parsing.c | 58 ++++++++++++++++++++ crypto_kem/hqc-128/clean/parsing.h | 9 +++ crypto_kem/hqc-128/clean/vector.c | 6 +- crypto_kem/hqc-192/avx2/vector.c | 1 + crypto_kem/hqc-192/clean/parsing.c | 58 ++++++++++++++++++++ crypto_kem/hqc-192/clean/parsing.h | 9 +++ crypto_kem/hqc-192/clean/vector.c | 3 +- crypto_kem/hqc-256/avx2/vector.c | 1 + crypto_kem/hqc-256/clean/parsing.c | 58 ++++++++++++++++++++ crypto_kem/hqc-256/clean/parsing.h | 9 +++ crypto_kem/hqc-256/clean/vector.c | 3 +- crypto_kem/hqc-rmrs-128/avx2/vector.c | 2 +- crypto_kem/hqc-rmrs-128/clean/parsing.c | 58 ++++++++++++++++++++ crypto_kem/hqc-rmrs-128/clean/parsing.h | 9 +++ crypto_kem/hqc-rmrs-128/clean/vector.c | 3 +- crypto_kem/hqc-rmrs-192/avx2/vector.c | 2 +- crypto_kem/hqc-rmrs-192/clean/parsing.c | 58 ++++++++++++++++++++ crypto_kem/hqc-rmrs-192/clean/parsing.h | 9 +++ crypto_kem/hqc-rmrs-192/clean/vector.c | 3 +- crypto_kem/hqc-rmrs-256/avx2/vector.c | 2 +- crypto_kem/hqc-rmrs-256/clean/parsing.c | 58 ++++++++++++++++++++ crypto_kem/hqc-rmrs-256/clean/parsing.h | 9 +++ crypto_kem/hqc-rmrs-256/clean/vector.c | 3 +- test/duplicate_consistency/hqc-128_clean.yml | 5 -- 25 files changed, 422 insertions(+), 16 deletions(-) diff --git a/crypto_kem/hqc-128/avx2/vector.c b/crypto_kem/hqc-128/avx2/vector.c index 25be7853..8f5da9df 100644 --- a/crypto_kem/hqc-128/avx2/vector.c +++ b/crypto_kem/hqc-128/avx2/vector.c @@ -116,7 +116,7 @@ void PQCLEAN_HQC128_AVX2_vect_set_random(AES_XOF_struct *ctx, uint64_t *v) { seedexpander(ctx, rand_bytes, VEC_N_SIZE_BYTES); - PQCLEAN_HQC128_AVX2_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N1N2_SIZE_BYTES); + PQCLEAN_HQC128_AVX2_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N_SIZE_BYTES); v[VEC_N_SIZE_64 - 1] &= RED_MASK; } diff --git a/crypto_kem/hqc-128/clean/parsing.c b/crypto_kem/hqc-128/clean/parsing.c index 75b74bb5..747cfd7b 100644 --- a/crypto_kem/hqc-128/clean/parsing.c +++ b/crypto_kem/hqc-128/clean/parsing.c @@ -11,6 +11,64 @@ */ +void PQCLEAN_HQC128_CLEAN_store8(unsigned char *out, uint64_t in) { + out[0] = (in >> 0x00) & 0xFF; + out[1] = (in >> 0x08) & 0xFF; + out[2] = (in >> 0x10) & 0xFF; + out[3] = (in >> 0x18) & 0xFF; + out[4] = (in >> 0x20) & 0xFF; + out[5] = (in >> 0x28) & 0xFF; + out[6] = (in >> 0x30) & 0xFF; + out[7] = (in >> 0x38) & 0xFF; +} + + +uint64_t PQCLEAN_HQC128_CLEAN_load8(const unsigned char *in) { + uint64_t ret = in[7]; + + for (int8_t i = 6; i >= 0; i--) { + ret <<= 8; + ret |= in[i]; + } + + return ret; +} + +void PQCLEAN_HQC128_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen) { + size_t index_in = 0; + size_t index_out = 0; + + // first copy by 8 bytes + if (inlen >= 8 && outlen >= 1) { + while (index_out < outlen && index_in + 8 <= inlen) { + out64[index_out] = PQCLEAN_HQC128_CLEAN_load8(in8 + index_in); + + index_in += 8; + index_out += 1; + } + } + + // we now need to do the last 7 bytes if necessary + if (index_in >= inlen || index_out >= outlen) { + return; + } + out64[index_out] = in8[inlen - 1]; + for (int8_t i = (int8_t)(inlen - index_in) - 2; i >= 0; i--) { + out64[index_out] <<= 8; + out64[index_out] |= in8[index_in + i]; + } +} + +void PQCLEAN_HQC128_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen) { + for (size_t index_out = 0, index_in = 0; index_out < outlen && index_in < inlen;) { + out8[index_out] = (in64[index_in] >> ((index_out % 8) * 8)) & 0xFF; + index_out++; + if (index_out % 8 == 0) { + index_in++; + } + } +} + /** * @brief Parse a secret key into a string diff --git a/crypto_kem/hqc-128/clean/parsing.h b/crypto_kem/hqc-128/clean/parsing.h index e11cfaf8..9549df60 100644 --- a/crypto_kem/hqc-128/clean/parsing.h +++ b/crypto_kem/hqc-128/clean/parsing.h @@ -11,6 +11,15 @@ #include +void PQCLEAN_HQC128_CLEAN_store8(unsigned char *out, uint64_t in); + +uint64_t PQCLEAN_HQC128_CLEAN_load8(const unsigned char *in); + +void PQCLEAN_HQC128_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen); + +void PQCLEAN_HQC128_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen); + + void PQCLEAN_HQC128_CLEAN_hqc_secret_key_to_string(uint8_t *sk, const uint8_t *sk_seed, const uint8_t *pk); void PQCLEAN_HQC128_CLEAN_hqc_secret_key_from_string(uint64_t *x, uint32_t *y, uint8_t *pk, const uint8_t *sk); diff --git a/crypto_kem/hqc-128/clean/vector.c b/crypto_kem/hqc-128/clean/vector.c index 6fa07a15..43f1c605 100644 --- a/crypto_kem/hqc-128/clean/vector.c +++ b/crypto_kem/hqc-128/clean/vector.c @@ -1,5 +1,6 @@ #include "nistseedexpander.h" #include "parameters.h" +#include "parsing.h" #include "randombytes.h" #include "vector.h" #include @@ -147,7 +148,7 @@ void PQCLEAN_HQC128_CLEAN_vect_set_random(AES_XOF_struct *ctx, uint64_t *v) { seedexpander(ctx, rand_bytes, VEC_N_SIZE_BYTES); - memcpy(v, rand_bytes, VEC_N_SIZE_BYTES); + PQCLEAN_HQC128_CLEAN_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N_SIZE_BYTES); v[VEC_N_SIZE_64 - 1] &= RED_MASK; } @@ -164,7 +165,7 @@ void PQCLEAN_HQC128_CLEAN_vect_set_random_from_randombytes(uint64_t *v) { uint8_t rand_bytes [VEC_K_SIZE_BYTES] = {0}; randombytes(rand_bytes, VEC_K_SIZE_BYTES); - memcpy(v, rand_bytes, VEC_K_SIZE_BYTES); + PQCLEAN_HQC128_CLEAN_load8_arr(v, VEC_K_SIZE_64, rand_bytes, VEC_K_SIZE_BYTES); } @@ -184,6 +185,7 @@ void PQCLEAN_HQC128_CLEAN_vect_add(uint64_t *o, const uint64_t *v1, const uint64 } + /** * @brief Compares two vectors * diff --git a/crypto_kem/hqc-192/avx2/vector.c b/crypto_kem/hqc-192/avx2/vector.c index 56334ffd..1dceddfc 100644 --- a/crypto_kem/hqc-192/avx2/vector.c +++ b/crypto_kem/hqc-192/avx2/vector.c @@ -1,5 +1,6 @@ #include "nistseedexpander.h" #include "parameters.h" +#include "parsing.h" #include "randombytes.h" #include "vector.h" #include diff --git a/crypto_kem/hqc-192/clean/parsing.c b/crypto_kem/hqc-192/clean/parsing.c index 9a89a1ee..3cfcaf93 100644 --- a/crypto_kem/hqc-192/clean/parsing.c +++ b/crypto_kem/hqc-192/clean/parsing.c @@ -11,6 +11,64 @@ */ +void PQCLEAN_HQC192_CLEAN_store8(unsigned char *out, uint64_t in) { + out[0] = (in >> 0x00) & 0xFF; + out[1] = (in >> 0x08) & 0xFF; + out[2] = (in >> 0x10) & 0xFF; + out[3] = (in >> 0x18) & 0xFF; + out[4] = (in >> 0x20) & 0xFF; + out[5] = (in >> 0x28) & 0xFF; + out[6] = (in >> 0x30) & 0xFF; + out[7] = (in >> 0x38) & 0xFF; +} + + +uint64_t PQCLEAN_HQC192_CLEAN_load8(const unsigned char *in) { + uint64_t ret = in[7]; + + for (int8_t i = 6; i >= 0; i--) { + ret <<= 8; + ret |= in[i]; + } + + return ret; +} + +void PQCLEAN_HQC192_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen) { + size_t index_in = 0; + size_t index_out = 0; + + // first copy by 8 bytes + if (inlen >= 8 && outlen >= 1) { + while (index_out < outlen && index_in + 8 <= inlen) { + out64[index_out] = PQCLEAN_HQC192_CLEAN_load8(in8 + index_in); + + index_in += 8; + index_out += 1; + } + } + + // we now need to do the last 7 bytes if necessary + if (index_in >= inlen || index_out >= outlen) { + return; + } + out64[index_out] = in8[inlen - 1]; + for (int8_t i = (int8_t)(inlen - index_in) - 2; i >= 0; i--) { + out64[index_out] <<= 8; + out64[index_out] |= in8[index_in + i]; + } +} + +void PQCLEAN_HQC192_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen) { + for (size_t index_out = 0, index_in = 0; index_out < outlen && index_in < inlen;) { + out8[index_out] = (in64[index_in] >> ((index_out % 8) * 8)) & 0xFF; + index_out++; + if (index_out % 8 == 0) { + index_in++; + } + } +} + /** * @brief Parse a secret key into a string diff --git a/crypto_kem/hqc-192/clean/parsing.h b/crypto_kem/hqc-192/clean/parsing.h index a4b663ba..85ce1b7f 100644 --- a/crypto_kem/hqc-192/clean/parsing.h +++ b/crypto_kem/hqc-192/clean/parsing.h @@ -11,6 +11,15 @@ #include +void PQCLEAN_HQC192_CLEAN_store8(unsigned char *out, uint64_t in); + +uint64_t PQCLEAN_HQC192_CLEAN_load8(const unsigned char *in); + +void PQCLEAN_HQC192_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen); + +void PQCLEAN_HQC192_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen); + + void PQCLEAN_HQC192_CLEAN_hqc_secret_key_to_string(uint8_t *sk, const uint8_t *sk_seed, const uint8_t *pk); void PQCLEAN_HQC192_CLEAN_hqc_secret_key_from_string(uint64_t *x, uint32_t *y, uint8_t *pk, const uint8_t *sk); diff --git a/crypto_kem/hqc-192/clean/vector.c b/crypto_kem/hqc-192/clean/vector.c index 031d2af0..d2301f82 100644 --- a/crypto_kem/hqc-192/clean/vector.c +++ b/crypto_kem/hqc-192/clean/vector.c @@ -1,5 +1,6 @@ #include "nistseedexpander.h" #include "parameters.h" +#include "parsing.h" #include "randombytes.h" #include "vector.h" #include @@ -147,7 +148,7 @@ void PQCLEAN_HQC192_CLEAN_vect_set_random(AES_XOF_struct *ctx, uint64_t *v) { seedexpander(ctx, rand_bytes, VEC_N_SIZE_BYTES); - memcpy(v, rand_bytes, VEC_N_SIZE_BYTES); + PQCLEAN_HQC192_CLEAN_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N_SIZE_BYTES); v[VEC_N_SIZE_64 - 1] &= RED_MASK; } diff --git a/crypto_kem/hqc-256/avx2/vector.c b/crypto_kem/hqc-256/avx2/vector.c index 06e20a89..9c3c9ea3 100644 --- a/crypto_kem/hqc-256/avx2/vector.c +++ b/crypto_kem/hqc-256/avx2/vector.c @@ -1,5 +1,6 @@ #include "nistseedexpander.h" #include "parameters.h" +#include "parsing.h" #include "randombytes.h" #include "vector.h" #include diff --git a/crypto_kem/hqc-256/clean/parsing.c b/crypto_kem/hqc-256/clean/parsing.c index 2606b928..7fd3dcc9 100644 --- a/crypto_kem/hqc-256/clean/parsing.c +++ b/crypto_kem/hqc-256/clean/parsing.c @@ -11,6 +11,64 @@ */ +void PQCLEAN_HQC256_CLEAN_store8(unsigned char *out, uint64_t in) { + out[0] = (in >> 0x00) & 0xFF; + out[1] = (in >> 0x08) & 0xFF; + out[2] = (in >> 0x10) & 0xFF; + out[3] = (in >> 0x18) & 0xFF; + out[4] = (in >> 0x20) & 0xFF; + out[5] = (in >> 0x28) & 0xFF; + out[6] = (in >> 0x30) & 0xFF; + out[7] = (in >> 0x38) & 0xFF; +} + + +uint64_t PQCLEAN_HQC256_CLEAN_load8(const unsigned char *in) { + uint64_t ret = in[7]; + + for (int8_t i = 6; i >= 0; i--) { + ret <<= 8; + ret |= in[i]; + } + + return ret; +} + +void PQCLEAN_HQC256_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen) { + size_t index_in = 0; + size_t index_out = 0; + + // first copy by 8 bytes + if (inlen >= 8 && outlen >= 1) { + while (index_out < outlen && index_in + 8 <= inlen) { + out64[index_out] = PQCLEAN_HQC256_CLEAN_load8(in8 + index_in); + + index_in += 8; + index_out += 1; + } + } + + // we now need to do the last 7 bytes if necessary + if (index_in >= inlen || index_out >= outlen) { + return; + } + out64[index_out] = in8[inlen - 1]; + for (int8_t i = (int8_t)(inlen - index_in) - 2; i >= 0; i--) { + out64[index_out] <<= 8; + out64[index_out] |= in8[index_in + i]; + } +} + +void PQCLEAN_HQC256_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen) { + for (size_t index_out = 0, index_in = 0; index_out < outlen && index_in < inlen;) { + out8[index_out] = (in64[index_in] >> ((index_out % 8) * 8)) & 0xFF; + index_out++; + if (index_out % 8 == 0) { + index_in++; + } + } +} + /** * @brief Parse a secret key into a string diff --git a/crypto_kem/hqc-256/clean/parsing.h b/crypto_kem/hqc-256/clean/parsing.h index 626964d1..2ed5af17 100644 --- a/crypto_kem/hqc-256/clean/parsing.h +++ b/crypto_kem/hqc-256/clean/parsing.h @@ -11,6 +11,15 @@ #include +void PQCLEAN_HQC256_CLEAN_store8(unsigned char *out, uint64_t in); + +uint64_t PQCLEAN_HQC256_CLEAN_load8(const unsigned char *in); + +void PQCLEAN_HQC256_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen); + +void PQCLEAN_HQC256_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen); + + void PQCLEAN_HQC256_CLEAN_hqc_secret_key_to_string(uint8_t *sk, const uint8_t *sk_seed, const uint8_t *pk); void PQCLEAN_HQC256_CLEAN_hqc_secret_key_from_string(uint64_t *x, uint32_t *y, uint8_t *pk, const uint8_t *sk); diff --git a/crypto_kem/hqc-256/clean/vector.c b/crypto_kem/hqc-256/clean/vector.c index 9645f40a..5887f7a6 100644 --- a/crypto_kem/hqc-256/clean/vector.c +++ b/crypto_kem/hqc-256/clean/vector.c @@ -1,5 +1,6 @@ #include "nistseedexpander.h" #include "parameters.h" +#include "parsing.h" #include "randombytes.h" #include "vector.h" #include @@ -147,7 +148,7 @@ void PQCLEAN_HQC256_CLEAN_vect_set_random(AES_XOF_struct *ctx, uint64_t *v) { seedexpander(ctx, rand_bytes, VEC_N_SIZE_BYTES); - memcpy(v, rand_bytes, VEC_N_SIZE_BYTES); + PQCLEAN_HQC256_CLEAN_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N_SIZE_BYTES); v[VEC_N_SIZE_64 - 1] &= RED_MASK; } diff --git a/crypto_kem/hqc-rmrs-128/avx2/vector.c b/crypto_kem/hqc-rmrs-128/avx2/vector.c index d7e27463..ffc7bc77 100644 --- a/crypto_kem/hqc-rmrs-128/avx2/vector.c +++ b/crypto_kem/hqc-rmrs-128/avx2/vector.c @@ -116,7 +116,7 @@ void PQCLEAN_HQCRMRS128_AVX2_vect_set_random(AES_XOF_struct *ctx, uint64_t *v) { seedexpander(ctx, rand_bytes, VEC_N_SIZE_BYTES); - PQCLEAN_HQCRMRS128_AVX2_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N1N2_SIZE_BYTES); + PQCLEAN_HQCRMRS128_AVX2_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N_SIZE_BYTES); v[VEC_N_SIZE_64 - 1] &= RED_MASK; } diff --git a/crypto_kem/hqc-rmrs-128/clean/parsing.c b/crypto_kem/hqc-rmrs-128/clean/parsing.c index 1159c610..989b5390 100644 --- a/crypto_kem/hqc-rmrs-128/clean/parsing.c +++ b/crypto_kem/hqc-rmrs-128/clean/parsing.c @@ -11,6 +11,64 @@ */ +void PQCLEAN_HQCRMRS128_CLEAN_store8(unsigned char *out, uint64_t in) { + out[0] = (in >> 0x00) & 0xFF; + out[1] = (in >> 0x08) & 0xFF; + out[2] = (in >> 0x10) & 0xFF; + out[3] = (in >> 0x18) & 0xFF; + out[4] = (in >> 0x20) & 0xFF; + out[5] = (in >> 0x28) & 0xFF; + out[6] = (in >> 0x30) & 0xFF; + out[7] = (in >> 0x38) & 0xFF; +} + + +uint64_t PQCLEAN_HQCRMRS128_CLEAN_load8(const unsigned char *in) { + uint64_t ret = in[7]; + + for (int8_t i = 6; i >= 0; i--) { + ret <<= 8; + ret |= in[i]; + } + + return ret; +} + +void PQCLEAN_HQCRMRS128_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen) { + size_t index_in = 0; + size_t index_out = 0; + + // first copy by 8 bytes + if (inlen >= 8 && outlen >= 1) { + while (index_out < outlen && index_in + 8 <= inlen) { + out64[index_out] = PQCLEAN_HQCRMRS128_CLEAN_load8(in8 + index_in); + + index_in += 8; + index_out += 1; + } + } + + // we now need to do the last 7 bytes if necessary + if (index_in >= inlen || index_out >= outlen) { + return; + } + out64[index_out] = in8[inlen - 1]; + for (int8_t i = (int8_t)(inlen - index_in) - 2; i >= 0; i--) { + out64[index_out] <<= 8; + out64[index_out] |= in8[index_in + i]; + } +} + +void PQCLEAN_HQCRMRS128_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen) { + for (size_t index_out = 0, index_in = 0; index_out < outlen && index_in < inlen;) { + out8[index_out] = (in64[index_in] >> ((index_out % 8) * 8)) & 0xFF; + index_out++; + if (index_out % 8 == 0) { + index_in++; + } + } +} + /** * @brief Parse a secret key into a string diff --git a/crypto_kem/hqc-rmrs-128/clean/parsing.h b/crypto_kem/hqc-rmrs-128/clean/parsing.h index 568b3f2e..7b0a5bbf 100644 --- a/crypto_kem/hqc-rmrs-128/clean/parsing.h +++ b/crypto_kem/hqc-rmrs-128/clean/parsing.h @@ -11,6 +11,15 @@ #include +void PQCLEAN_HQCRMRS128_CLEAN_store8(unsigned char *out, uint64_t in); + +uint64_t PQCLEAN_HQCRMRS128_CLEAN_load8(const unsigned char *in); + +void PQCLEAN_HQCRMRS128_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen); + +void PQCLEAN_HQCRMRS128_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen); + + void PQCLEAN_HQCRMRS128_CLEAN_hqc_secret_key_to_string(uint8_t *sk, const uint8_t *sk_seed, const uint8_t *pk); void PQCLEAN_HQCRMRS128_CLEAN_hqc_secret_key_from_string(uint64_t *x, uint32_t *y, uint8_t *pk, const uint8_t *sk); diff --git a/crypto_kem/hqc-rmrs-128/clean/vector.c b/crypto_kem/hqc-rmrs-128/clean/vector.c index 2cebf3eb..f1f0d4b0 100644 --- a/crypto_kem/hqc-rmrs-128/clean/vector.c +++ b/crypto_kem/hqc-rmrs-128/clean/vector.c @@ -1,5 +1,6 @@ #include "nistseedexpander.h" #include "parameters.h" +#include "parsing.h" #include "randombytes.h" #include "vector.h" #include @@ -147,7 +148,7 @@ void PQCLEAN_HQCRMRS128_CLEAN_vect_set_random(AES_XOF_struct *ctx, uint64_t *v) seedexpander(ctx, rand_bytes, VEC_N_SIZE_BYTES); - memcpy(v, rand_bytes, VEC_N_SIZE_BYTES); + PQCLEAN_HQCRMRS128_CLEAN_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N_SIZE_BYTES); v[VEC_N_SIZE_64 - 1] &= RED_MASK; } diff --git a/crypto_kem/hqc-rmrs-192/avx2/vector.c b/crypto_kem/hqc-rmrs-192/avx2/vector.c index c8559625..935d443f 100644 --- a/crypto_kem/hqc-rmrs-192/avx2/vector.c +++ b/crypto_kem/hqc-rmrs-192/avx2/vector.c @@ -116,7 +116,7 @@ void PQCLEAN_HQCRMRS192_AVX2_vect_set_random(AES_XOF_struct *ctx, uint64_t *v) { seedexpander(ctx, rand_bytes, VEC_N_SIZE_BYTES); - PQCLEAN_HQCRMRS192_AVX2_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N1N2_SIZE_BYTES); + PQCLEAN_HQCRMRS192_AVX2_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N_SIZE_BYTES); v[VEC_N_SIZE_64 - 1] &= RED_MASK; } diff --git a/crypto_kem/hqc-rmrs-192/clean/parsing.c b/crypto_kem/hqc-rmrs-192/clean/parsing.c index a574517f..3323804c 100644 --- a/crypto_kem/hqc-rmrs-192/clean/parsing.c +++ b/crypto_kem/hqc-rmrs-192/clean/parsing.c @@ -11,6 +11,64 @@ */ +void PQCLEAN_HQCRMRS192_CLEAN_store8(unsigned char *out, uint64_t in) { + out[0] = (in >> 0x00) & 0xFF; + out[1] = (in >> 0x08) & 0xFF; + out[2] = (in >> 0x10) & 0xFF; + out[3] = (in >> 0x18) & 0xFF; + out[4] = (in >> 0x20) & 0xFF; + out[5] = (in >> 0x28) & 0xFF; + out[6] = (in >> 0x30) & 0xFF; + out[7] = (in >> 0x38) & 0xFF; +} + + +uint64_t PQCLEAN_HQCRMRS192_CLEAN_load8(const unsigned char *in) { + uint64_t ret = in[7]; + + for (int8_t i = 6; i >= 0; i--) { + ret <<= 8; + ret |= in[i]; + } + + return ret; +} + +void PQCLEAN_HQCRMRS192_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen) { + size_t index_in = 0; + size_t index_out = 0; + + // first copy by 8 bytes + if (inlen >= 8 && outlen >= 1) { + while (index_out < outlen && index_in + 8 <= inlen) { + out64[index_out] = PQCLEAN_HQCRMRS192_CLEAN_load8(in8 + index_in); + + index_in += 8; + index_out += 1; + } + } + + // we now need to do the last 7 bytes if necessary + if (index_in >= inlen || index_out >= outlen) { + return; + } + out64[index_out] = in8[inlen - 1]; + for (int8_t i = (int8_t)(inlen - index_in) - 2; i >= 0; i--) { + out64[index_out] <<= 8; + out64[index_out] |= in8[index_in + i]; + } +} + +void PQCLEAN_HQCRMRS192_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen) { + for (size_t index_out = 0, index_in = 0; index_out < outlen && index_in < inlen;) { + out8[index_out] = (in64[index_in] >> ((index_out % 8) * 8)) & 0xFF; + index_out++; + if (index_out % 8 == 0) { + index_in++; + } + } +} + /** * @brief Parse a secret key into a string diff --git a/crypto_kem/hqc-rmrs-192/clean/parsing.h b/crypto_kem/hqc-rmrs-192/clean/parsing.h index bfeb29cb..aad8a8cc 100644 --- a/crypto_kem/hqc-rmrs-192/clean/parsing.h +++ b/crypto_kem/hqc-rmrs-192/clean/parsing.h @@ -11,6 +11,15 @@ #include +void PQCLEAN_HQCRMRS192_CLEAN_store8(unsigned char *out, uint64_t in); + +uint64_t PQCLEAN_HQCRMRS192_CLEAN_load8(const unsigned char *in); + +void PQCLEAN_HQCRMRS192_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen); + +void PQCLEAN_HQCRMRS192_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen); + + void PQCLEAN_HQCRMRS192_CLEAN_hqc_secret_key_to_string(uint8_t *sk, const uint8_t *sk_seed, const uint8_t *pk); void PQCLEAN_HQCRMRS192_CLEAN_hqc_secret_key_from_string(uint64_t *x, uint32_t *y, uint8_t *pk, const uint8_t *sk); diff --git a/crypto_kem/hqc-rmrs-192/clean/vector.c b/crypto_kem/hqc-rmrs-192/clean/vector.c index 4b51ee3c..8a9fda05 100644 --- a/crypto_kem/hqc-rmrs-192/clean/vector.c +++ b/crypto_kem/hqc-rmrs-192/clean/vector.c @@ -1,5 +1,6 @@ #include "nistseedexpander.h" #include "parameters.h" +#include "parsing.h" #include "randombytes.h" #include "vector.h" #include @@ -147,7 +148,7 @@ void PQCLEAN_HQCRMRS192_CLEAN_vect_set_random(AES_XOF_struct *ctx, uint64_t *v) seedexpander(ctx, rand_bytes, VEC_N_SIZE_BYTES); - memcpy(v, rand_bytes, VEC_N_SIZE_BYTES); + PQCLEAN_HQCRMRS192_CLEAN_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N_SIZE_BYTES); v[VEC_N_SIZE_64 - 1] &= RED_MASK; } diff --git a/crypto_kem/hqc-rmrs-256/avx2/vector.c b/crypto_kem/hqc-rmrs-256/avx2/vector.c index 1904528b..575abe76 100644 --- a/crypto_kem/hqc-rmrs-256/avx2/vector.c +++ b/crypto_kem/hqc-rmrs-256/avx2/vector.c @@ -116,7 +116,7 @@ void PQCLEAN_HQCRMRS256_AVX2_vect_set_random(AES_XOF_struct *ctx, uint64_t *v) { seedexpander(ctx, rand_bytes, VEC_N_SIZE_BYTES); - PQCLEAN_HQCRMRS256_AVX2_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N1N2_SIZE_BYTES); + PQCLEAN_HQCRMRS256_AVX2_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N_SIZE_BYTES); v[VEC_N_SIZE_64 - 1] &= RED_MASK; } diff --git a/crypto_kem/hqc-rmrs-256/clean/parsing.c b/crypto_kem/hqc-rmrs-256/clean/parsing.c index a003f484..8cae77e2 100644 --- a/crypto_kem/hqc-rmrs-256/clean/parsing.c +++ b/crypto_kem/hqc-rmrs-256/clean/parsing.c @@ -11,6 +11,64 @@ */ +void PQCLEAN_HQCRMRS256_CLEAN_store8(unsigned char *out, uint64_t in) { + out[0] = (in >> 0x00) & 0xFF; + out[1] = (in >> 0x08) & 0xFF; + out[2] = (in >> 0x10) & 0xFF; + out[3] = (in >> 0x18) & 0xFF; + out[4] = (in >> 0x20) & 0xFF; + out[5] = (in >> 0x28) & 0xFF; + out[6] = (in >> 0x30) & 0xFF; + out[7] = (in >> 0x38) & 0xFF; +} + + +uint64_t PQCLEAN_HQCRMRS256_CLEAN_load8(const unsigned char *in) { + uint64_t ret = in[7]; + + for (int8_t i = 6; i >= 0; i--) { + ret <<= 8; + ret |= in[i]; + } + + return ret; +} + +void PQCLEAN_HQCRMRS256_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen) { + size_t index_in = 0; + size_t index_out = 0; + + // first copy by 8 bytes + if (inlen >= 8 && outlen >= 1) { + while (index_out < outlen && index_in + 8 <= inlen) { + out64[index_out] = PQCLEAN_HQCRMRS256_CLEAN_load8(in8 + index_in); + + index_in += 8; + index_out += 1; + } + } + + // we now need to do the last 7 bytes if necessary + if (index_in >= inlen || index_out >= outlen) { + return; + } + out64[index_out] = in8[inlen - 1]; + for (int8_t i = (int8_t)(inlen - index_in) - 2; i >= 0; i--) { + out64[index_out] <<= 8; + out64[index_out] |= in8[index_in + i]; + } +} + +void PQCLEAN_HQCRMRS256_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen) { + for (size_t index_out = 0, index_in = 0; index_out < outlen && index_in < inlen;) { + out8[index_out] = (in64[index_in] >> ((index_out % 8) * 8)) & 0xFF; + index_out++; + if (index_out % 8 == 0) { + index_in++; + } + } +} + /** * @brief Parse a secret key into a string diff --git a/crypto_kem/hqc-rmrs-256/clean/parsing.h b/crypto_kem/hqc-rmrs-256/clean/parsing.h index 3da34dd2..eea2ab86 100644 --- a/crypto_kem/hqc-rmrs-256/clean/parsing.h +++ b/crypto_kem/hqc-rmrs-256/clean/parsing.h @@ -11,6 +11,15 @@ #include +void PQCLEAN_HQCRMRS256_CLEAN_store8(unsigned char *out, uint64_t in); + +uint64_t PQCLEAN_HQCRMRS256_CLEAN_load8(const unsigned char *in); + +void PQCLEAN_HQCRMRS256_CLEAN_load8_arr(uint64_t *out64, size_t outlen, const uint8_t *in8, size_t inlen); + +void PQCLEAN_HQCRMRS256_CLEAN_store8_arr(uint8_t *out8, size_t outlen, const uint64_t *in64, size_t inlen); + + void PQCLEAN_HQCRMRS256_CLEAN_hqc_secret_key_to_string(uint8_t *sk, const uint8_t *sk_seed, const uint8_t *pk); void PQCLEAN_HQCRMRS256_CLEAN_hqc_secret_key_from_string(uint64_t *x, uint32_t *y, uint8_t *pk, const uint8_t *sk); diff --git a/crypto_kem/hqc-rmrs-256/clean/vector.c b/crypto_kem/hqc-rmrs-256/clean/vector.c index 0a7cbd94..228e460b 100644 --- a/crypto_kem/hqc-rmrs-256/clean/vector.c +++ b/crypto_kem/hqc-rmrs-256/clean/vector.c @@ -1,5 +1,6 @@ #include "nistseedexpander.h" #include "parameters.h" +#include "parsing.h" #include "randombytes.h" #include "vector.h" #include @@ -147,7 +148,7 @@ void PQCLEAN_HQCRMRS256_CLEAN_vect_set_random(AES_XOF_struct *ctx, uint64_t *v) seedexpander(ctx, rand_bytes, VEC_N_SIZE_BYTES); - memcpy(v, rand_bytes, VEC_N_SIZE_BYTES); + PQCLEAN_HQCRMRS256_CLEAN_load8_arr(v, VEC_N_SIZE_64, rand_bytes, VEC_N_SIZE_BYTES); v[VEC_N_SIZE_64 - 1] &= RED_MASK; } diff --git a/test/duplicate_consistency/hqc-128_clean.yml b/test/duplicate_consistency/hqc-128_clean.yml index b5aada62..a527e144 100644 --- a/test/duplicate_consistency/hqc-128_clean.yml +++ b/test/duplicate_consistency/hqc-128_clean.yml @@ -27,7 +27,6 @@ consistency_checks: - hqc.c - kem.c - parsing.c - - vector.c - source: scheme: hqc-192 implementation: avx2 @@ -55,7 +54,6 @@ consistency_checks: - hqc.c - kem.c - parsing.c - - vector.c - source: scheme: hqc-256 implementation: avx2 @@ -75,7 +73,6 @@ consistency_checks: - hqc.c - kem.c - parsing.c - - vector.c - source: scheme: hqc-rmrs-128 implementation: avx2 @@ -95,7 +92,6 @@ consistency_checks: - hqc.c - kem.c - parsing.c - - vector.c - source: scheme: hqc-rmrs-192 implementation: avx2 @@ -115,7 +111,6 @@ consistency_checks: - hqc.c - kem.c - parsing.c - - vector.c - source: scheme: hqc-rmrs-256 implementation: avx2