Skip to content

Commit

Permalink
include openssl/crypto.h and resolve conflict varible for ntru
Browse files Browse the repository at this point in the history
Signed-off-by: Songling Han <[email protected]>
  • Loading branch information
songlingatpan committed Sep 21, 2024
1 parent d73b469 commit 9e15107
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 137 deletions.
24 changes: 1 addition & 23 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,8 @@ extern "C" {
* standard C library functions otherwise.
*/
#if defined(OQS_USE_OPENSSL)
#ifndef OPENSSL_malloc
# define OPENSSL_malloc(num) \
CRYPTO_malloc(num, __FILE__, __LINE__)
#endif

#ifndef OPENSSL_zalloc
# define OPENSSL_zalloc(num) \
CRYPTO_zalloc(num, __FILE__, __LINE__)
#endif

#ifndef OPENSSL_free
# define OPENSSL_free(addr) \
CRYPTO_free(addr, __FILE__, __LINE__)
#endif

#ifndef OPENSSL_strdup
# define OPENSSL_strdup(str) \
CRYPTO_strdup(str, __FILE__, __LINE__)
#endif
#include <openssl/crypto.h>

extern void *CRYPTO_malloc(size_t num, const char *file, int line);
extern void *CRYPTO_zalloc(size_t num, const char *file, int line);
extern void CRYPTO_free(void *str, const char *file, int line);
extern char *CRYPTO_strdup(const char *str, const char *file, int line);
/**
* Allocates memory of a given size.
* @param size The size of the memory to be allocated in bytes.
Expand Down
26 changes: 13 additions & 13 deletions src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_invsntrup761.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static inline void vectormodq_xswapeliminate(Fq *f, Fq *g, int len, const Fq f0,
int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, const unsigned char *inbytes) {
small *in = (void *) inbytes;
int loop;
Fq out[p], f[ppad], g[ppad], v[ppad], r[ppad];
Fq out[p_param], f[ppad], g[ppad], v[ppad], r[ppad];
Fq f0, g0;
Fq scale;
int i;
Expand All @@ -142,14 +142,14 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con
f[i] = 0;
}
f[0] = 1;
f[p - 1] = -1;
f[p] = -1;
f[p_param - 1] = -1;
f[p_param] = -1;
/* generalization: initialize f to reversal of any deg-p polynomial m */

for (i = 0; i < p; ++i) {
g[i] = in[p - 1 - i];
for (i = 0; i < p_param; ++i) {
g[i] = in[p_param - 1 - i];
}
for (i = p; i < ppad; ++i) {
for (i = p_param; i < ppad; ++i) {
g[i] = 0;
}

Expand All @@ -162,7 +162,7 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con
v[i] = 0;
}

for (loop = 0; loop < p; ++loop) {
for (loop = 0; loop < p_param; ++loop) {
g0 = Fq_freeze(g[0]);
f0 = f[0];
if (q > 5167) {
Expand All @@ -180,11 +180,11 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con

f[0] = f0;

vectormodq_swapeliminate(f + 1, g + 1, p, f0, g0, swap);
vectormodq_swapeliminate(f + 1, g + 1, p_param, f0, g0, swap);
vectormodq_xswapeliminate(v, r, loop + 1, f0, g0, swap);
}

for (loop = p - 1; loop > 0; --loop) {
for (loop = p_param - 1; loop > 0; --loop) {
g0 = Fq_freeze(g[0]);
f0 = f[0];
if (q > 5167) {
Expand All @@ -203,15 +203,15 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con
f[0] = f0;

vectormodq_swapeliminate(f + 1, g + 1, loop, f0, g0, swap);
vectormodq_xswapeliminate(v, r, p, f0, g0, swap);
vectormodq_xswapeliminate(v, r, p_param, f0, g0, swap);
}

scale = Fq_recip(Fq_freeze(f[0]));
for (i = 0; i < p; ++i) {
out[i] = Fq_bigfreeze(scale * (int32)Fq_freeze(v[p - i]));
for (i = 0; i < p_param; ++i) {
out[i] = Fq_bigfreeze(scale * (int32)Fq_freeze(v[p_param - i]));
}

crypto_encode_pxint16(outbytes, out);
outbytes[2 * p] = (unsigned char) int16_nonzero_mask((int16) delta);
outbytes[2 * p_param] = (unsigned char) int16_nonzero_mask((int16) delta);
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_weightsntrup761(unsigned char *outbytes,
__m256i sum, sumhi;
int16 weight;

sum = _mm256_loadu_si256((__m256i *) (in + p - 32));
sum = _mm256_loadu_si256((__m256i *) (in + p_param - 32));
sum &= endingmask;

for (i = p - 32; i >= 0; i -= 32) {
for (i = p_param - 32; i >= 0; i -= 32) {
__m256i bits = _mm256_loadu_si256((__m256i *) in);
bits &= _mm256_set1_epi8(1);
sum = _mm256_add_epi8(sum, bits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_wforcesntrup761(unsigned char *out, const
out += i;
}

i = p - w - 32;
i = p_param - w - 32;
for (;;) {
do {
__m256i x = _mm256_loadu_si256((__m256i *) in);
Expand Down
34 changes: 17 additions & 17 deletions src/kem/ntruprime/pqclean_sntrup761_avx2/kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,37 +78,37 @@ static void Short_random(small *out) {
uint32 L[ppadsort];
int i;

randombytes((unsigned char *) L, 4 * p);
randombytes((unsigned char *) L, 4 * p_param);
crypto_decode_pxint32(L, (unsigned char *) L);
for (i = 0; i < w; ++i) {
L[i] = L[i] & (uint32) - 2;
}
for (i = w; i < p; ++i) {
for (i = w; i < p_param; ++i) {
L[i] = (L[i] & (uint32) - 3) | 1;
}
for (i = p; i < ppadsort; ++i) {
for (i = p_param; i < ppadsort; ++i) {
L[i] = 0xffffffff;
}
PQCLEAN_SNTRUP761_AVX2_crypto_sort_uint32(L, ppadsort);
for (i = 0; i < p; ++i) {
for (i = 0; i < p_param; ++i) {
out[i] = (small) ((L[i] & 3) - 1);
}
}

static void Small_random(small *out) {
uint32 L[p];
uint32 L[p_param];
int i;

randombytes((unsigned char *) L, sizeof L);
crypto_decode_pxint32(L, (unsigned char *) L);
for (i = 0; i < p; ++i) {
for (i = 0; i < p_param; ++i) {
out[i] = (small) ((((L[i] & 0x3fffffff) * 3) >> 30) - 1);
}
}

/* ----- Streamlined NTRU Prime */

typedef small Inputs[p]; /* passed by reference */
typedef small Inputs[p_param]; /* passed by reference */
#define Ciphertexts_bytes Rounded_bytes
#define SecretKeys_bytes (2*Small_bytes)
#define PublicKeys_bytes Rq_bytes
Expand All @@ -119,7 +119,7 @@ typedef small Inputs[p]; /* passed by reference */
/* also set x[0]=2, and x[1:1+Hash_bytes] = Hash3(r_enc) */
/* also overwrite x[1+Hash_bytes:1+2*Hash_bytes] */
static void Hide(unsigned char *x, unsigned char *c, unsigned char *r_enc, const Inputs r, const unsigned char *pk, const unsigned char *cache) {
Fq h[p];
Fq h[p_param];
int i;

Small_encode(r_enc + 1, r);
Expand All @@ -137,14 +137,14 @@ static void Hide(unsigned char *x, unsigned char *c, unsigned char *r_enc, const


int PQCLEAN_SNTRUP761_AVX2_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) {
small g[p];
small g[p_param];
for (;;) {
Small_random(g);
{
small v[p + 1];
small v[p_param + 1];
small vp;
crypto_core_inv3((unsigned char *) v, (const unsigned char *) g);
vp = v[p];
vp = v[p_param];
crypto_declassify(&vp, sizeof vp);
if (vp == 0) {
Small_encode(sk + Small_bytes, v);
Expand All @@ -153,11 +153,11 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) {
}
}
{
small f[p];
small f[p_param];
Short_random(f);
Small_encode(sk, f);
{
Fq h[p + 1];
Fq h[p_param + 1];
Rq_recip3(h, f); /* always works */
Rq_mult_small(h, g);
Rq_encode(pk, h);
Expand Down Expand Up @@ -212,17 +212,17 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_kem_dec(uint8_t *k, const uint8_t *c, const ui
int mask, i;
Inputs r;
{
Fq d[p];
Fq d[p_param];
Rounded_decode(d, c);
{
small f[p];
small f[p_param];
Small_decode(f, sk);
Rq_mult_small(d, f);
Rq_mult3(d, d);
}
{
small e[p];
small v[p];
small e[p_param];
small v[p_param];
R3_fromRq(e, d);
Small_decode(v, sk + Small_bytes);
R3_mult(r, e, v);
Expand Down
2 changes: 1 addition & 1 deletion src/kem/ntruprime/pqclean_sntrup761_avx2/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define ppad 769
#define endingmask _mm256_set_epi8(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0)
#define crypto_core_weight PQCLEAN_SNTRUP761_AVX2_crypto_core_weightsntrup761
#define p 761
#define p_param 761
#define q 4591
#define w 286

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,32 @@ static small F3_freeze(int16 x) {
int PQCLEAN_SNTRUP761_CLEAN_crypto_core_inv3sntrup761(unsigned char *outbytes, const unsigned char *inbytes) {
small *out = (void *) outbytes;
small *in = (void *) inbytes;
small f[p + 1], g[p + 1], v[p + 1], r[p + 1];
small f[p_param + 1], g[p_param + 1], v[p_param + 1], r[p_param + 1];
int i, loop, delta;
int sign, swap, t;

for (i = 0; i < p + 1; ++i) {
for (i = 0; i < p_param + 1; ++i) {
v[i] = 0;
}
for (i = 0; i < p + 1; ++i) {
for (i = 0; i < p_param + 1; ++i) {
r[i] = 0;
}
r[0] = 1;
for (i = 0; i < p; ++i) {
for (i = 0; i < p_param; ++i) {
f[i] = 0;
}
f[0] = 1;
f[p - 1] = f[p] = -1;
for (i = 0; i < p; ++i) {
f[p_param - 1] = f[p_param] = -1;
for (i = 0; i < p_param; ++i) {
small i1 = in[i] & 1;
g[p - 1 - i] = (small) (i1 - (in[i] & (i1 << 1)));
g[p_param - 1 - i] = (small) (i1 - (in[i] & (i1 << 1)));
}
g[p] = 0;
g[p_param] = 0;

delta = 1;

for (loop = 0; loop < 2 * p - 1; ++loop) {
for (i = p; i > 0; --i) {
for (loop = 0; loop < 2 * p_param - 1; ++loop) {
for (i = p_param; i > 0; --i) {
v[i] = v[i - 1];
}
v[0] = 0;
Expand All @@ -78,7 +78,7 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_inv3sntrup761(unsigned char *outbytes, c
delta ^= swap & (delta ^ -delta);
delta += 1;

for (i = 0; i < p + 1; ++i) {
for (i = 0; i < p_param + 1; ++i) {
t = swap & (f[i] ^ g[i]);
f[i] ^= (small) t;
g[i] ^= (small) t;
Expand All @@ -87,24 +87,24 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_inv3sntrup761(unsigned char *outbytes, c
r[i] ^= (small) t;
}

for (i = 0; i < p + 1; ++i) {
for (i = 0; i < p_param + 1; ++i) {
g[i] = F3_freeze((int16) (g[i] + sign * f[i]));
}
for (i = 0; i < p + 1; ++i) {
for (i = 0; i < p_param + 1; ++i) {
r[i] = F3_freeze((int16) (r[i] + sign * v[i]));
}

for (i = 0; i < p; ++i) {
for (i = 0; i < p_param; ++i) {
g[i] = g[i + 1];
}
g[p] = (int16) 0;
g[p_param] = (int16) 0;
}

sign = (int) f[0];
for (i = 0; i < p; ++i) {
out[i] = (small) (sign * v[p - 1 - i]);
for (i = 0; i < p_param; ++i) {
out[i] = (small) (sign * v[p_param - 1 - i]);
}

out[p] = (small) int16_nonzero_mask((int16) delta);
out[p_param] = (small) int16_nonzero_mask((int16) delta);
return 0;
}
Loading

0 comments on commit 9e15107

Please sign in to comment.