| 1 |
/* |
| 2 |
This file is for Niederreiter encryption |
| 3 |
*/ |
| 4 |
// 20240805 djb: more cryptoint usage |
| 5 |
// 20240508 djb: using crypto_sort_int16() |
| 6 |
// 20240507 djb: using crypto_uint64_load() |
| 7 |
// 20240504 djb: negifcollision |
| 8 |
// 20240504 djb: use crypto_xof_bitwrite16 |
| 9 |
// 20240503 djb: remove #ifdef KAT ... #endif |
| 10 |
// 20230102 djb: rename encrypt() as pke_encrypt() |
| 11 |
// 20221231 djb: move encrypt.h last for macos portability; tnx thom wiggers |
| 12 |
// 20221230 djb: add linker line |
| 13 |
|
| 14 |
// linker define pke_encrypt |
| 15 |
|
| 16 |
#include "util.h" |
| 17 |
#include "params.h" |
| 18 |
#include "crypto_sort_int16.h" |
| 19 |
#include "randombytes.h" |
| 20 |
|
| 21 |
#include <stdint.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "gf.h" |
| 25 |
#include "crypto_declassify.h" |
| 26 |
#include "crypto_int16.h" |
| 27 |
#include "crypto_uint16.h" |
| 28 |
#include "crypto_uint32.h" |
| 29 |
#include "crypto_uint64.h" |
| 30 |
#include "crypto_xof_bitwrite16.h" |
| 31 |
|
| 32 |
#include "encrypt.h" |
| 33 |
|
| 34 |
static inline crypto_uint16 uint16_is_smaller_declassify(uint16_t t,uint16_t u) |
| 35 |
{ |
| 36 |
crypto_uint16 mask = crypto_uint16_smaller_mask(t,u); |
| 37 |
crypto_declassify(&mask,sizeof mask); |
| 38 |
return mask; |
| 39 |
} |
| 40 |
|
| 41 |
/* output: e, an error vector of weight t */ |
| 42 |
static void gen_e(unsigned char *e) |
| 43 |
{ |
| 44 |
int i, j, count; |
| 45 |
|
| 46 |
union |
| 47 |
{ |
| 48 |
uint16_t nums[ SYS_T*2 ]; |
| 49 |
unsigned char bytes[ SYS_T*2 * sizeof(uint16_t) ]; |
| 50 |
} buf; |
| 51 |
|
| 52 |
uint16_t ind[ SYS_T ]; |
| 53 |
crypto_int16 negifcollision; |
| 54 |
unsigned char indbytes[ SYS_T*2 ]; |
| 55 |
|
| 56 |
while (1) |
| 57 |
{ |
| 58 |
randombytes(buf.bytes, sizeof(buf)); |
| 59 |
|
| 60 |
for (i = 0; i < SYS_T*2; i++) |
| 61 |
buf.nums[i] = load_gf(buf.bytes + i*2); |
| 62 |
|
| 63 |
// moving and counting indices in the correct range |
| 64 |
|
| 65 |
count = 0; |
| 66 |
for (i = 0; i < SYS_T*2 && count < SYS_T; i++) |
| 67 |
if (uint16_is_smaller_declassify(buf.nums[i],SYS_N)) |
| 68 |
ind[ count++ ] = buf.nums[i]; |
| 69 |
|
| 70 |
if (count < SYS_T) continue; |
| 71 |
|
| 72 |
// check for repetition |
| 73 |
|
| 74 |
crypto_sort_int16(ind, SYS_T); |
| 75 |
|
| 76 |
negifcollision = 0; |
| 77 |
for (i = 1; i < SYS_T; i++) |
| 78 |
negifcollision |= (ind[i-1]^ind[i])-1; |
| 79 |
|
| 80 |
negifcollision = crypto_int16_negative_mask(negifcollision); |
| 81 |
crypto_declassify(&negifcollision,sizeof negifcollision); |
| 82 |
|
| 83 |
if (!negifcollision) |
| 84 |
break; |
| 85 |
} |
| 86 |
|
| 87 |
for (j = 0; j < SYS_T; j++) |
| 88 |
crypto_uint16_store(indbytes+2*j,ind[j]); |
| 89 |
|
| 90 |
crypto_xof_bitwrite16(e,SYS_N/8,indbytes,2*SYS_T); |
| 91 |
} |
| 92 |
|
| 93 |
/* input: public key pk, error vector e */ |
| 94 |
/* output: syndrome s */ |
| 95 |
static void syndrome(unsigned char *s, const unsigned char *pk, unsigned char *e) |
| 96 |
{ |
| 97 |
crypto_uint64 b; |
| 98 |
|
| 99 |
const unsigned char *pk_ptr; |
| 100 |
const unsigned char *e_ptr = e + SYND_BYTES; |
| 101 |
crypto_uint64 eword[PK_NCOLS/64]; |
| 102 |
|
| 103 |
int i, j; |
| 104 |
|
| 105 |
// |
| 106 |
|
| 107 |
for (i = 0; i < SYND_BYTES; i++) |
| 108 |
s[i] = e[i]; |
| 109 |
|
| 110 |
for (j = 0;j < PK_NCOLS/64;++j) |
| 111 |
eword[j] = crypto_uint64_load(e_ptr+8*j); |
| 112 |
|
| 113 |
for (i = 0; i < PK_NROWS; i++) |
| 114 |
{ |
| 115 |
pk_ptr = pk + PK_ROW_BYTES * i; |
| 116 |
|
| 117 |
b = 0; |
| 118 |
for (j = 0; j < PK_NCOLS/64; j++) |
| 119 |
b ^= crypto_uint64_load(pk_ptr+8*j) & eword[j]; |
| 120 |
|
| 121 |
b ^= crypto_uint32_load(pk_ptr+8*j) & crypto_uint32_load(e_ptr+8*j); |
| 122 |
|
| 123 |
b ^= b >> 32; |
| 124 |
b ^= b >> 16; |
| 125 |
b ^= b >> 8; |
| 126 |
b ^= b >> 4; |
| 127 |
b ^= b >> 2; |
| 128 |
b ^= b >> 1; |
| 129 |
b = crypto_uint64_bottombit_01(b); |
| 130 |
|
| 131 |
s[ i/8 ] ^= (b << (i%8)); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/* input: public key pk */ |
| 136 |
/* output: error vector e, syndrome s */ |
| 137 |
void pke_encrypt(unsigned char *s, const unsigned char *pk, unsigned char *e) |
| 138 |
{ |
| 139 |
gen_e(e); |
| 140 |
syndrome(s, pk, e); |
| 141 |
} |
| 142 |
|