| 1 |
#include "params.h" |
| 2 |
|
| 3 |
#include "randombytes.h" |
| 4 |
#include "crypto_hash_sha512.h" |
| 5 |
#include "crypto_sort_uint32.h" |
| 6 |
#include "crypto_declassify.h" |
| 7 |
|
| 8 |
#include "crypto_int8.h" |
| 9 |
#include "crypto_int16.h" |
| 10 |
#include "crypto_int32.h" |
| 11 |
#include "crypto_uint16.h" |
| 12 |
#include "crypto_uint32.h" |
| 13 |
#define int8 crypto_int8 |
| 14 |
#define int16 crypto_int16 |
| 15 |
#define int32 crypto_int32 |
| 16 |
#define uint16 crypto_uint16 |
| 17 |
#define uint32 crypto_uint32 |
| 18 |
|
| 19 |
/* ----- arithmetic mod 3 */ |
| 20 |
|
| 21 |
typedef int8 small; |
| 22 |
/* F3 is always represented as -1,0,1 */ |
| 23 |
|
| 24 |
/* ----- arithmetic mod q */ |
| 25 |
|
| 26 |
typedef int16 Fq; |
| 27 |
/* always represented as -(q-1)/2...(q-1)/2 */ |
| 28 |
|
| 29 |
/* ----- small polynomials */ |
| 30 |
|
| 31 |
/* R3_fromR(R_fromRq(r)) */ |
| 32 |
static void R3_fromRq(small *out,const Fq *r) |
| 33 |
{ |
| 34 |
crypto_encode_pxfreeze3((unsigned char *) out,(unsigned char *) r); |
| 35 |
} |
| 36 |
|
| 37 |
/* h = f*g in the ring R3 */ |
| 38 |
static void R3_mult(small *h,const small *f,const small *g) |
| 39 |
{ |
| 40 |
crypto_core_mult3((unsigned char *) h,(const unsigned char *) f,(const unsigned char *) g,0); |
| 41 |
} |
| 42 |
|
| 43 |
/* ----- polynomials mod q */ |
| 44 |
|
| 45 |
/* h = h*g in the ring Rq */ |
| 46 |
static void Rq_mult_small(Fq *h,const small *g) |
| 47 |
{ |
| 48 |
crypto_encode_pxint16((unsigned char *) h,h); |
| 49 |
crypto_core_mult((unsigned char *) h,(const unsigned char *) h,(const unsigned char *) g,0); |
| 50 |
crypto_decode_pxint16(h,(const unsigned char *) h); |
| 51 |
} |
| 52 |
|
| 53 |
/* h = 3f in Rq */ |
| 54 |
static void Rq_mult3(Fq *h,const Fq *f) |
| 55 |
{ |
| 56 |
crypto_encode_pxint16((unsigned char *) h,f); |
| 57 |
crypto_core_scale3((unsigned char *) h,(const unsigned char *) h,0,0); |
| 58 |
crypto_decode_pxint16(h,(const unsigned char *) h); |
| 59 |
} |
| 60 |
|
| 61 |
/* out = 1/(3*in) in Rq */ |
| 62 |
/* caller must have 2p+1 bytes free in out, not just 2p */ |
| 63 |
static void Rq_recip3(Fq *out,const small *in) |
| 64 |
{ |
| 65 |
crypto_core_inv((unsigned char *) out,(const unsigned char *) in,0,0); |
| 66 |
/* could check byte 2*p for failure; but, in context, inv always works */ |
| 67 |
crypto_decode_pxint16(out,(unsigned char *) out); |
| 68 |
} |
| 69 |
|
| 70 |
/* ----- underlying hash function */ |
| 71 |
|
| 72 |
#define Hash_bytes 32 |
| 73 |
|
| 74 |
static void Hash(unsigned char *out,const unsigned char *in,int inlen) |
| 75 |
{ |
| 76 |
unsigned char h[64]; |
| 77 |
int i; |
| 78 |
crypto_hash_sha512(h,in,inlen); |
| 79 |
for (i = 0;i < 32;++i) out[i] = h[i]; |
| 80 |
} |
| 81 |
|
| 82 |
/* ----- higher-level randomness */ |
| 83 |
|
| 84 |
static void Short_random(small *out) |
| 85 |
{ |
| 86 |
uint32 L[ppadsort]; |
| 87 |
int i; |
| 88 |
|
| 89 |
randombytes((unsigned char *) L,4*p); |
| 90 |
crypto_decode_pxint32(L,(unsigned char *) L); |
| 91 |
for (i = 0;i < w;++i) L[i] = L[i]&(uint32)-2; |
| 92 |
for (i = w;i < p;++i) L[i] = (L[i]&(uint32)-3)|1; |
| 93 |
for (i = p;i < ppadsort;++i) L[i] = 0xffffffff; |
| 94 |
crypto_sort_uint32(L,ppadsort); |
| 95 |
for (i = 0;i < p;++i) out[i] = (L[i]&3)-1; |
| 96 |
} |
| 97 |
|
| 98 |
static void Small_random(small *out) |
| 99 |
{ |
| 100 |
uint32 L[p]; |
| 101 |
int i; |
| 102 |
|
| 103 |
randombytes((unsigned char *) L,sizeof L); |
| 104 |
crypto_decode_pxint32(L,(unsigned char *) L); |
| 105 |
for (i = 0;i < p;++i) out[i] = (((L[i]&0x3fffffff)*3)>>30)-1; |
| 106 |
} |
| 107 |
|
| 108 |
/* ----- Streamlined NTRU Prime */ |
| 109 |
|
| 110 |
typedef small Inputs[p]; /* passed by reference */ |
| 111 |
#define Ciphertexts_bytes Rounded_bytes |
| 112 |
#define SecretKeys_bytes (2*Small_bytes) |
| 113 |
#define PublicKeys_bytes Rq_bytes |
| 114 |
#define Confirm_bytes 32 |
| 115 |
|
| 116 |
/* c,r_enc[1:] = Hide(r,pk,cache); cache is Hash4(pk) */ |
| 117 |
/* also set r_enc[0]=3 */ |
| 118 |
/* also set x[0]=2, and x[1:1+Hash_bytes] = Hash3(r_enc) */ |
| 119 |
/* also overwrite x[1+Hash_bytes:1+2*Hash_bytes] */ |
| 120 |
static void Hide(unsigned char *x,unsigned char *c,unsigned char *r_enc,const Inputs r,const unsigned char *pk,const unsigned char *cache) |
| 121 |
{ |
| 122 |
Fq h[p]; |
| 123 |
int i; |
| 124 |
|
| 125 |
Small_encode(r_enc+1,r); |
| 126 |
Rq_decode(h,pk); |
| 127 |
Rq_mult_small(h,r); |
| 128 |
Round_and_encode(c,h); |
| 129 |
r_enc[0] = 3; |
| 130 |
Hash(x+1,r_enc,1+Small_bytes); |
| 131 |
for (i = 0;i < Hash_bytes;++i) x[1+Hash_bytes+i] = cache[i]; |
| 132 |
x[0] = 2; |
| 133 |
Hash(c+Ciphertexts_bytes,x,1+Hash_bytes*2); |
| 134 |
} |
| 135 |
|
| 136 |
#include "crypto_kem.h" |
| 137 |
|
| 138 |
void crypto_kem_keypair(unsigned char *pk,unsigned char *sk) |
| 139 |
{ |
| 140 |
small g[p]; |
| 141 |
for (;;) { |
| 142 |
Small_random(g); |
| 143 |
{ |
| 144 |
small v[p+1]; |
| 145 |
small vp; |
| 146 |
crypto_core_inv3((unsigned char *) v,(const unsigned char *) g,0,0); |
| 147 |
vp = v[p]; |
| 148 |
crypto_declassify(&vp,sizeof vp); |
| 149 |
if (vp == 0) { |
| 150 |
Small_encode(sk+Small_bytes,v); |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
{ |
| 156 |
small f[p]; |
| 157 |
Short_random(f); |
| 158 |
Small_encode(sk,f); |
| 159 |
{ |
| 160 |
Fq h[p+1]; |
| 161 |
Rq_recip3(h,f); /* always works */ |
| 162 |
Rq_mult_small(h,g); |
| 163 |
Rq_encode(pk,h); |
| 164 |
} |
| 165 |
} |
| 166 |
{ |
| 167 |
int i; |
| 168 |
unsigned char sksave = sk[SecretKeys_bytes-1]; |
| 169 |
for (i = 0;i < PublicKeys_bytes;++i) sk[SecretKeys_bytes+i] = pk[i]; |
| 170 |
sk[SecretKeys_bytes-1] = 4; |
| 171 |
Hash(sk+SecretKeys_bytes+PublicKeys_bytes+Small_bytes,sk+SecretKeys_bytes-1,1+PublicKeys_bytes); |
| 172 |
sk[SecretKeys_bytes-1] = sksave; |
| 173 |
randombytes(sk+SecretKeys_bytes+PublicKeys_bytes,Small_bytes); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void crypto_kem_enc(unsigned char *c,unsigned char *k,const unsigned char *pk) |
| 178 |
{ |
| 179 |
unsigned char cache[Hash_bytes]; |
| 180 |
int i; |
| 181 |
{ |
| 182 |
unsigned char y[1+PublicKeys_bytes]; /* XXX: can eliminate with incremental hashing */ |
| 183 |
for (i = 0;i < PublicKeys_bytes;++i) y[1+i] = pk[i]; |
| 184 |
y[0] = 4; |
| 185 |
Hash(cache,y,sizeof y); |
| 186 |
} |
| 187 |
{ |
| 188 |
Inputs r; |
| 189 |
Short_random(r); |
| 190 |
{ |
| 191 |
unsigned char r_enc[Small_bytes+1]; |
| 192 |
unsigned char x[1+Hash_bytes+Ciphertexts_bytes+Confirm_bytes]; |
| 193 |
Hide(x,c,r_enc,r,pk,cache); |
| 194 |
for (i = 0;i < Ciphertexts_bytes+Confirm_bytes;++i) x[1+Hash_bytes+i] = c[i]; |
| 195 |
x[0] = 1; |
| 196 |
Hash(k,x,sizeof x); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
void crypto_kem_dec(unsigned char *k,const unsigned char *c,const unsigned char *sk) |
| 202 |
{ |
| 203 |
const unsigned char *pk = sk+SecretKeys_bytes; |
| 204 |
const unsigned char *rho = pk+PublicKeys_bytes; |
| 205 |
const unsigned char *cache = rho+Small_bytes; |
| 206 |
int mask,i; |
| 207 |
Inputs r; |
| 208 |
{ |
| 209 |
Fq d[p]; |
| 210 |
Rounded_decode(d,c); |
| 211 |
{ |
| 212 |
small f[p]; |
| 213 |
Small_decode(f,sk); |
| 214 |
Rq_mult_small(d,f); |
| 215 |
Rq_mult3(d,d); |
| 216 |
} |
| 217 |
{ |
| 218 |
small e[p]; |
| 219 |
small v[p]; |
| 220 |
R3_fromRq(e,d); |
| 221 |
Small_decode(v,sk+Small_bytes); |
| 222 |
R3_mult(r,e,v); |
| 223 |
} |
| 224 |
crypto_core_wforce((unsigned char *) r,(unsigned char *) r,0,0); |
| 225 |
} |
| 226 |
{ |
| 227 |
unsigned char r_enc[1+Small_bytes]; |
| 228 |
unsigned char cnew[Ciphertexts_bytes+Confirm_bytes]; |
| 229 |
unsigned char x[1+Hash_bytes+Ciphertexts_bytes+Confirm_bytes]; |
| 230 |
/* XXX: can use incremental hashing to reduce x size */ |
| 231 |
|
| 232 |
Hide(x,cnew,r_enc,r,pk,cache); |
| 233 |
mask = crypto_verify_clen(c,cnew); |
| 234 |
for (i = 0;i < Small_bytes;++i) r_enc[i+1] ^= mask&(r_enc[i+1]^rho[i]); |
| 235 |
Hash(x+1,r_enc,1+Small_bytes); /* XXX: can instead do cmov on cached hash of rho */ |
| 236 |
for (i = 0;i < Ciphertexts_bytes+Confirm_bytes;++i) x[1+Hash_bytes+i] = c[i]; |
| 237 |
x[0] = 1+mask; |
| 238 |
Hash(k,x,sizeof x); |
| 239 |
} |
| 240 |
} |