-
Notifications
You must be signed in to change notification settings - Fork 0
/
romulust.hpp
393 lines (279 loc) · 11 KB
/
romulust.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#pragma once
#include <algorithm>
#include "common.hpp"
#include "romulush.hpp"
#include "skinny.hpp"
// Romulus-T Authenticated Encryption with Associated Data
namespace romulust {
// Extract N -th message block ( 256 -bit wide ) from padded message bytes
// which is to be authenticated i.e.
//
// msg = ipad_256(padded associated data bytes ||
// padded cipher text bytes ||
// 16 -bytes nonce ||
// 7 -bytes LFSR counter)
//
// Note, block index = N, begins at zero.
static void get_auth_block(
const uint8_t* const __restrict data, // N -bytes associated data
const size_t dlen, // len(data) = N | >= 0
const uint8_t* const __restrict cipher, // M -bytes encrypted text
const size_t ctlen, // len(cipher) = M | >= 0
const uint8_t* const __restrict nonce, // 16 -bytes nonce
const uint8_t* const __restrict lfsr, // 7 -bytes LFSR counter
const size_t blk_idx, // Index of block to be extracted
uint8_t* const __restrict blk // 32 -bytes extracted block
) {
std::memset(blk, 0, 32);
const size_t tmp0 = dlen & 15ul;
const size_t tmp1 = ctlen & 15ul;
const size_t tmp2 = 16ul - tmp0;
const size_t tmp3 = 16ul - tmp1;
const bool flg0 = dlen > 0ul;
const bool flg1 = ctlen > 0ul;
const size_t padded_dlen = dlen + tmp2 * flg0;
const size_t padded_ctlen = ctlen + tmp3 * flg1;
const size_t tmp4 = padded_dlen + padded_ctlen;
const size_t tmp5 = tmp4 + 16ul;
const size_t padded_authlen = tmp5 + 7ul;
size_t off = blk_idx << 5;
size_t boff = 0ul;
if (off < padded_dlen) {
const size_t read = std::min(32ul, dlen - off);
std::memcpy(blk + boff, data + off, read);
const uint8_t br0[]{blk[15], static_cast<uint8_t>(read)};
const uint8_t br1[]{blk[31], static_cast<uint8_t>(read & 15ul)};
blk[15] = br0[read < 16ul];
blk[31] = br1[(read > 16ul) & (read < 32ul)];
const size_t read_ = std::min(32ul, padded_dlen - off);
off += read_;
boff += read_;
}
const bool flg2 = off >= padded_dlen;
const bool flg3 = off < tmp4;
if ((boff < 32ul) && (flg2 & flg3)) {
constexpr size_t br0[]{32ul, 16ul};
const size_t ctoff = off - padded_dlen;
const size_t read = std::min(br0[boff >= 16ul], ctlen - ctoff);
std::memcpy(blk + boff, cipher + ctoff, read);
const uint8_t br1[]{blk[15], static_cast<uint8_t>(read)};
const uint8_t br2[]{blk[31], static_cast<uint8_t>(read & 15ul)};
blk[15] = br1[(boff < 16ul) & (read < 16ul)];
blk[31] = br2[(boff < 16ul) & (read > 16ul) & (read < 32ul)];
const uint8_t br3[]{blk[31], static_cast<uint8_t>(read)};
blk[31] = br3[(boff >= 16ul) & (read < 16ul)];
const size_t read_ = std::min(32ul, padded_ctlen - ctoff);
off += read_;
boff += read_;
}
const bool flg4 = off >= tmp4;
const bool flg5 = off < tmp5;
if ((boff < 32ul) && (flg4 & flg5)) {
std::memcpy(blk + boff, nonce, 16);
off += 16ul;
boff += 16ul;
}
const bool flg6 = off >= tmp5;
const bool flg7 = off < padded_authlen;
if ((boff < 32ul) && (flg6 & flg7)) {
std::memcpy(blk + boff, lfsr, 7);
off += 7ul;
boff += 7ul;
}
const uint8_t br[]{blk[31], static_cast<uint8_t>(boff)};
blk[31] = br[boff < 32ul];
}
// Given 16 -bytes secret key, 16 -bytes public message nonce, N -bytes
// associated data and M -bytes plain text | N, M >= 0, this routine computes M
// -bytes encrypted text and 16 -bytes authentication tag, using Romulus-T
// authenticated encryption algorithm, which is leakage-resistant.
//
// See encryption algorithm defined in figure 2.9 of Romulus specification
// https://csrc.nist.gov/CSRC/media/Projects/lightweight-cryptography/documents/finalist-round/updated-spec-doc/romulus-spec-final.pdf
static void encrypt(
const uint8_t* const __restrict key, // 128 -bit secret key
const uint8_t* const __restrict nonce, // 128 -bit nonce
const uint8_t* const __restrict data, // N -bytes associated data
const size_t dlen, // len(data) = N | >= 0
const uint8_t* const __restrict text, // M -bytes plain text
uint8_t* const __restrict cipher, // M -bytes cipher text
const size_t ctlen, // len(text) = len(cipher) | >= 0
uint8_t* const __restrict tag // 128 -bit authentication tag
) {
uint8_t state[16];
uint8_t lfsr[7];
uint8_t tweakey[48];
skinny::state_t st;
const size_t blk_cnt = ctlen >> 4;
const size_t rm_bytes = ctlen & 15ul;
const bool flg = rm_bytes > 0ul;
const size_t tot_blk_cnt = blk_cnt + 1ul * flg;
if (ctlen > 0ul) {
uint8_t blk[16];
std::memset(blk, 0, 16);
std::memset(lfsr, 0, 7);
romulus_common::encode(key, blk, lfsr, 66, tweakey);
skinny::initialize(&st, nonce, tweakey);
skinny::tbc(&st);
std::memcpy(state, st.arr, 16);
romulus_common::set_lfsr(lfsr);
size_t off = 0ul;
for (size_t i = 0; i < tot_blk_cnt - 1ul; i++) {
romulus_common::encode(state, blk, lfsr, 64, tweakey);
skinny::initialize(&st, nonce, tweakey);
skinny::tbc(&st);
for (size_t j = 0; j < 16; j++) {
cipher[off + j] = text[off + j] ^ st.arr[j];
}
romulus_common::encode(state, blk, lfsr, 65, tweakey);
skinny::initialize(&st, nonce, tweakey);
skinny::tbc(&st);
std::memcpy(state, st.arr, 16);
off += 16ul;
romulus_common::update_lfsr(lfsr);
}
romulus_common::encode(state, blk, lfsr, 64, tweakey);
skinny::initialize(&st, nonce, tweakey);
skinny::tbc(&st);
const size_t read = ctlen - off;
for (size_t i = 0; i < read; i++) {
cipher[off + i] = text[off + i] ^ st.arr[i];
}
}
romulus_common::set_lfsr(lfsr);
for (size_t i = 0; i < tot_blk_cnt; i++) {
romulus_common::update_lfsr(lfsr);
}
{
uint8_t left[16]{};
uint8_t right[16]{};
uint8_t blk[32]{};
std::memset(left, 0, sizeof(left));
std::memset(right, 0, sizeof(right));
const size_t tmp0 = dlen & 15ul;
const size_t tmp1 = ctlen & 15ul;
const size_t tmp2 = 16ul - tmp0;
const size_t tmp3 = 16ul - tmp1;
const bool flg0 = dlen > 0ul;
const bool flg1 = ctlen > 0ul;
const size_t padded_dlen = dlen + tmp2 * flg0;
const size_t padded_ctlen = ctlen + tmp3 * flg1;
const size_t padded_authlen = padded_dlen + padded_ctlen + 16ul + 7ul;
const size_t padded_blk_cnt = padded_authlen >> 5;
const size_t padded_rm_bytes = padded_authlen & 31ul;
const bool flg2 = padded_rm_bytes > 0ul;
const size_t tot_padded_blk_cnt = padded_blk_cnt + 1ul * flg2;
for (size_t i = 0; i < tot_padded_blk_cnt - 1ul; i++) {
get_auth_block(data, dlen, cipher, ctlen, nonce, lfsr, i, blk);
romulush::compress(left, right, blk);
}
left[0] ^= 0b00000010;
const size_t id = tot_padded_blk_cnt - 1ul;
get_auth_block(data, dlen, cipher, ctlen, nonce, lfsr, id, blk);
romulush::compress(left, right, blk);
std::memset(lfsr, 0, 7);
romulus_common::encode(key, right, lfsr, 68, tweakey);
skinny::initialize(&st, left, tweakey);
skinny::tbc(&st);
std::memcpy(tag, st.arr, 16);
}
}
// Given 16 -bytes secret key, 16 -bytes public message nonce, 16 -bytes
// authentication tag, N -bytes associated data and M -bytes encrypted text | N,
// M >= 0, this routine computes M -bytes decrypted text and boolean
// verification flag, using Romulus-T verified decryption algorithm, which is
// leakage-resistant.
//
// See decryption algorithm defined in figure 2.9 of Romulus specification
// https://csrc.nist.gov/CSRC/media/Projects/lightweight-cryptography/documents/finalist-round/updated-spec-doc/romulus-spec-final.pdf
static bool decrypt(const uint8_t* const __restrict key,
const uint8_t* const __restrict nonce,
const uint8_t* const __restrict tag,
const uint8_t* const __restrict data, const size_t dlen,
const uint8_t* const __restrict cipher,
uint8_t* const __restrict text, const size_t ctlen) {
uint8_t state[16];
uint8_t tag_[16];
uint8_t lfsr[7];
uint8_t tweakey[48];
skinny::state_t st;
const size_t blk_cnt = ctlen >> 4;
const size_t rm_bytes = ctlen & 15ul;
const bool flg0 = rm_bytes > 0ul;
const size_t tot_blk_cnt = blk_cnt + 1ul * flg0;
romulus_common::set_lfsr(lfsr);
for (size_t i = 0; i < tot_blk_cnt; i++) {
romulus_common::update_lfsr(lfsr);
}
{
uint8_t left[16]{};
uint8_t right[16]{};
uint8_t blk[32]{};
std::memset(left, 0, sizeof(left));
std::memset(right, 0, sizeof(right));
const size_t tmp0 = dlen & 15ul;
const size_t tmp1 = ctlen & 15ul;
const size_t tmp2 = 16ul - tmp0;
const size_t tmp3 = 16ul - tmp1;
const bool flg0 = dlen > 0ul;
const bool flg1 = ctlen > 0ul;
const size_t padded_dlen = dlen + tmp2 * flg0;
const size_t padded_ctlen = ctlen + tmp3 * flg1;
const size_t padded_authlen = padded_dlen + padded_ctlen + 16ul + 7ul;
const size_t padded_blk_cnt = padded_authlen >> 5;
const size_t padded_rm_bytes = padded_authlen & 31ul;
const bool flg2 = padded_rm_bytes > 0ul;
const size_t tot_padded_blk_cnt = padded_blk_cnt + 1ul * flg2;
for (size_t i = 0; i < tot_padded_blk_cnt - 1ul; i++) {
get_auth_block(data, dlen, cipher, ctlen, nonce, lfsr, i, blk);
romulush::compress(left, right, blk);
}
left[0] ^= 0b00000010;
const size_t id = tot_padded_blk_cnt - 1ul;
get_auth_block(data, dlen, cipher, ctlen, nonce, lfsr, id, blk);
romulush::compress(left, right, blk);
std::memset(lfsr, 0, 7);
romulus_common::encode(key, right, lfsr, 68, tweakey);
skinny::initialize(&st, left, tweakey);
skinny::tbc(&st);
std::memcpy(tag_, st.arr, 16);
}
bool flg1 = false;
for (size_t i = 0; i < 16; i++) {
flg1 |= static_cast<bool>(tag[i] ^ tag_[i]);
}
if (!flg1 & (ctlen > 0ul)) {
uint8_t blk[16];
std::memset(blk, 0, 16);
std::memset(lfsr, 0, 7);
romulus_common::encode(key, blk, lfsr, 66, tweakey);
skinny::initialize(&st, nonce, tweakey);
skinny::tbc(&st);
std::memcpy(state, st.arr, 16);
romulus_common::set_lfsr(lfsr);
size_t off = 0ul;
for (size_t i = 0; i < tot_blk_cnt - 1ul; i++) {
romulus_common::encode(state, blk, lfsr, 64, tweakey);
skinny::initialize(&st, nonce, tweakey);
skinny::tbc(&st);
for (size_t j = 0; j < 16; j++) {
text[off + j] = cipher[off + j] ^ st.arr[j];
}
romulus_common::encode(state, blk, lfsr, 65, tweakey);
skinny::initialize(&st, nonce, tweakey);
skinny::tbc(&st);
std::memcpy(state, st.arr, 16);
off += 16ul;
romulus_common::update_lfsr(lfsr);
}
romulus_common::encode(state, blk, lfsr, 64, tweakey);
skinny::initialize(&st, nonce, tweakey);
skinny::tbc(&st);
const size_t read = ctlen - off;
for (size_t i = 0; i < read; i++) {
text[off + i] = cipher[off + i] ^ st.arr[i];
}
}
return !flg1;
}
} // namespace romulust