Debian Codegraph @

crypto_kem/6960119pcf/vec/encrypt.c

raw
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_uint64.h"
29 #include "crypto_xof_bitwrite16.h"
30
31 #include "encrypt.h"
32
33 static inline crypto_uint16 uint16_is_smaller_declassify(uint16_t t,uint16_t u)
34 {
35 crypto_uint16 mask = crypto_uint16_smaller_mask(t,u);
36 crypto_declassify(&mask,sizeof mask);
37 return mask;
38 }
39
40 /* output: e, an error vector of weight t */
41 static void gen_e(unsigned char *e)
42 {
43 int i, j, count;
44
45 union
46 {
47 uint16_t nums[ SYS_T*2 ];
48 unsigned char bytes[ SYS_T*2 * sizeof(uint16_t) ];
49 } buf;
50
51 uint16_t ind[ SYS_T ];
52 crypto_int16 negifcollision;
53 unsigned char indbytes[ SYS_T*2 ];
54
55 while (1)
56 {
57 randombytes(buf.bytes, sizeof(buf));
58
59 for (i = 0; i < SYS_T*2; i++)
60 buf.nums[i] = load_gf(buf.bytes + i*2);
61
62 // moving and counting indices in the correct range
63
64 count = 0;
65 for (i = 0; i < SYS_T*2 && count < SYS_T; i++)
66 if (uint16_is_smaller_declassify(buf.nums[i],SYS_N))
67 ind[ count++ ] = buf.nums[i];
68
69 if (count < SYS_T) continue;
70
71 // check for repetition
72
73 crypto_sort_int16(ind, SYS_T);
74
75 negifcollision = 0;
76 for (i = 1; i < SYS_T; i++)
77 negifcollision |= (ind[i-1]^ind[i])-1;
78
79 negifcollision = crypto_int16_negative_mask(negifcollision);
80 crypto_declassify(&negifcollision,sizeof negifcollision);
81
82 if (!negifcollision)
83 break;
84 }
85
86 for (j = 0; j < SYS_T; j++)
87 crypto_uint16_store(indbytes+2*j,ind[j]);
88
89 crypto_xof_bitwrite16(e,SYS_N/8,indbytes,2*SYS_T);
90 }
91
92 /* input: public key pk, error vector e */
93 /* output: syndrome s */
94 static void syndrome(unsigned char *s, const unsigned char *pk, unsigned char *e)
95 {
96 unsigned char e_tmp[ SYS_N/8 ];
97
98 crypto_uint64 b;
99
100 const unsigned char *pk_ptr;
101 const unsigned char *e_ptr = e_tmp + SYND_BYTES - 1;
102 crypto_uint64 eword[PK_NCOLS/64];
103
104 int i, j, k, tail = (PK_NROWS % 8);
105
106 //
107
108 for (i = 0; i < SYND_BYTES; i++)
109 s[i] = e[i];
110
111 s[i-1] &= (1 << tail) - 1;
112
113 for (i = SYND_BYTES-1; i < SYS_N/8-1; i++)
114 e_tmp[i] = (e[i] >> tail) | (e[i+1] << (8-tail));
115
116 e_tmp[i] = e[i] >> tail;
117
118 for (j = 0;j < PK_NCOLS/64;++j)
119 eword[j] = crypto_uint64_load(e_ptr+8*j);
120
121 for (i = 0; i < PK_NROWS; i++)
122 {
123 pk_ptr = pk + PK_ROW_BYTES * i;
124
125 b = 0;
126 for (j = 0; j < PK_NCOLS/64; j++)
127 b ^= crypto_uint64_load(pk_ptr+8*j) & eword[j];
128
129 for (k = 0; k < (PK_NCOLS%64 + 7)/8; k++)
130 b ^= *(pk_ptr+8*j+k) & *(e_ptr+8*j+k);
131
132 b ^= b >> 32;
133 b ^= b >> 16;
134 b ^= b >> 8;
135 b ^= b >> 4;
136 b ^= b >> 2;
137 b ^= b >> 1;
138 b = crypto_uint64_bottombit_01(b);
139
140 s[ i/8 ] ^= (b << (i%8));
141 }
142 }
143
144 /* input: public key pk */
145 /* output: error vector e, syndrome s */
146 void pke_encrypt(unsigned char *s, const unsigned char *pk, unsigned char *e)
147 {
148 gen_e(e);
149 syndrome(s, pk, e);
150 }
151