-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromulusm.hpp
337 lines (239 loc) · 9.81 KB
/
romulusm.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
#pragma once
#include <algorithm>
#include "common.hpp"
#include "skinny.hpp"
// Romulus-M Authenticated Encryption with Associated Data
namespace romulusm {
// Extract N-th message block ( 128 -bit wide ) from concatenated padded
// associated data bytes and padded plain text bytes. Last block of both
// associated data and plain text can be padded, if required.
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 text, // M -bytes plain text
const size_t ctlen, // len(text) = M | >= 0
const size_t blk_idx, // Index of block to extract
uint8_t* const __restrict blk // 16 -bytes extracted (padded) block
) {
std::memset(blk, 0, 16);
const size_t tmp0 = dlen & 15ul;
const size_t tmp1 = ctlen & 15ul;
const bool flg0 = (dlen == 0) | (tmp0 > 0ul);
const bool flg1 = (ctlen == 0) | (tmp1 > 0ul);
const size_t off = blk_idx << 4;
const size_t padded_dlen = dlen + (16ul - tmp0) * flg0;
const size_t padded_ctlen = ctlen + (16ul - tmp1) * flg1;
const size_t padded_authlen = padded_dlen + padded_ctlen;
if (off < padded_dlen) {
const size_t read = std::min(16ul, dlen - off);
std::memcpy(blk, data + off, read);
const uint8_t br[]{blk[15], static_cast<uint8_t>(read)};
blk[15] = br[read < 16ul];
}
if ((off >= padded_dlen) && (off < padded_authlen)) {
const size_t ctoff = off - padded_dlen;
const size_t read = std::min(16ul, ctlen - ctoff);
std::memcpy(blk, text + ctoff, read);
const uint8_t br[]{blk[15], static_cast<uint8_t>(read)};
blk[15] = br[read < 16ul];
}
}
// Given 16 -bytes secret key, 16 -bytes 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-M authenticated encryption
// algorithm, which is nonce misuse-resistant.
//
// See encryption algorithm defined in figure 2.7 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 public message 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 encrypted text
const size_t ctlen, // len(text) = len(cipher) | >= 0
uint8_t* const __restrict tag // 128 -bit authentication tag
) {
skinny::state_t st;
uint8_t lfsr[7];
uint8_t enc[16];
std::memset(st.arr, 0, 16);
romulus_common::set_lfsr(lfsr);
{
const size_t ad_blk_cnt = dlen >> 4;
const size_t ct_blk_cnt = ctlen >> 4;
const size_t ad_rm_bytes = dlen & 15ul;
const size_t ct_rm_bytes = ctlen & 15ul;
const bool flg0 = (dlen == 0) | (ad_rm_bytes > 0);
const bool flg1 = (ctlen == 0) | (ct_rm_bytes > 0);
const size_t tot_ad_blk_cnt = ad_blk_cnt + 1ul * flg0;
const size_t tot_ct_blk_cnt = ct_blk_cnt + 1ul * flg1;
uint8_t w = 48;
w ^= 2 * flg0;
w ^= 1 * flg1;
w ^= 8 * (1 - (tot_ad_blk_cnt & 1));
w ^= 4 * (1 - (tot_ct_blk_cnt & 1));
const size_t tot_blk_cnt = tot_ad_blk_cnt + tot_ct_blk_cnt;
const size_t half_blk_cnt = tot_blk_cnt >> 1;
const size_t half_ad_blk_cnt = tot_ad_blk_cnt >> 1;
uint8_t blk[16]{};
uint8_t x = 40;
for (size_t i = 0; i < half_blk_cnt; i++) {
get_auth_block(data, dlen, text, ctlen, (i << 1) ^ 0ul, blk);
romulus_common::rho(st.arr, blk, enc);
romulus_common::update_lfsr(lfsr);
x ^= 4 * (i == half_ad_blk_cnt);
get_auth_block(data, dlen, text, ctlen, (i << 1) ^ 1ul, blk);
romulus_common::encode(key, blk, lfsr, x, st.arr + 16);
skinny::tbc(&st);
romulus_common::update_lfsr(lfsr);
}
const bool flg2 = static_cast<bool>(tot_ad_blk_cnt & 1ul);
const bool flg3 = static_cast<bool>(tot_ct_blk_cnt & 1ul);
if (flg2 == flg3) {
std::memset(blk, 0, 16);
} else {
get_auth_block(data, dlen, text, ctlen, tot_blk_cnt - 1, blk);
}
romulus_common::rho(st.arr, blk, enc);
if (tot_blk_cnt > (half_blk_cnt << 1)) {
romulus_common::update_lfsr(lfsr);
}
romulus_common::encode(key, nonce, lfsr, w, st.arr + 16);
skinny::tbc(&st);
}
uint8_t tmp[16]{};
std::memset(tmp, 0, 16);
romulus_common::rho(st.arr, tmp, tag);
if (ctlen > 0ul) {
romulus_common::set_lfsr(lfsr);
std::memcpy(st.arr, tag, 16);
const size_t blk_cnt = ctlen >> 4;
const size_t rm_bytes = ctlen & 15ul;
const bool flg = (ctlen == 0) | (rm_bytes > 0);
const size_t tot_blk_cnt = blk_cnt + 1ul * flg;
size_t off = 0ul;
for (size_t i = 0; i < tot_blk_cnt - 1; i++) {
romulus_common::encode(key, nonce, lfsr, 36, st.arr + 16);
skinny::tbc(&st);
romulus_common::rho(st.arr, text + off, cipher + off);
romulus_common::update_lfsr(lfsr);
off += 16;
}
const size_t read = ctlen - off;
uint8_t blk[16]{};
std::memset(blk, 0, 16);
std::memcpy(blk, text + off, read);
const uint8_t br[]{blk[15], static_cast<uint8_t>(read)};
blk[15] = br[read < 16ul];
romulus_common::encode(key, nonce, lfsr, 36, st.arr + 16);
skinny::tbc(&st);
romulus_common::rho(st.arr, blk, enc);
std::memcpy(cipher + off, enc, read);
}
}
// Given 16 -bytes secret key, 16 -bytes 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-M verified decryption algorithm, which is nonce misuse-resistant.
//
// See decryption algorithm defined in figure 2.7 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, // 128 -bit secret key
const uint8_t* const __restrict nonce, // 128 -bit public message nonce
const uint8_t* const __restrict tag, // 128 -bit authentication tag
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
uint8_t* const __restrict text, // M -bytes decrypted text
const size_t ctlen // len(text) = len(cipher) | >= 0
) {
skinny::state_t st;
uint8_t lfsr[7];
uint8_t enc[16];
if (ctlen > 0ul) {
romulus_common::set_lfsr(lfsr);
std::memcpy(st.arr, tag, 16);
const size_t blk_cnt = ctlen >> 4;
const size_t rm_bytes = ctlen & 15ul;
const bool flg = (ctlen == 0) | (rm_bytes > 0);
const size_t tot_blk_cnt = blk_cnt + 1ul * flg;
size_t off = 0ul;
for (size_t i = 0; i < tot_blk_cnt - 1; i++) {
romulus_common::encode(key, nonce, lfsr, 36, st.arr + 16);
skinny::tbc(&st);
romulus_common::rho_inv(st.arr, cipher + off, text + off);
romulus_common::update_lfsr(lfsr);
off += 16;
}
const size_t read = ctlen - off;
uint8_t blk[16]{};
std::memset(blk, 0, 16);
std::memcpy(blk, cipher + off, read);
const uint8_t br[]{blk[15], static_cast<uint8_t>(read)};
blk[15] = br[read < 16ul];
romulus_common::encode(key, nonce, lfsr, 36, st.arr + 16);
skinny::tbc(&st);
romulus_common::rho_inv(st.arr, blk, enc);
std::memcpy(text + off, enc, read);
}
{
std::memset(st.arr, 0, 16);
romulus_common::set_lfsr(lfsr);
const size_t ad_blk_cnt = dlen >> 4;
const size_t ct_blk_cnt = ctlen >> 4;
const size_t ad_rm_bytes = dlen & 15ul;
const size_t ct_rm_bytes = ctlen & 15ul;
const bool flg0 = (dlen == 0) | (ad_rm_bytes > 0);
const bool flg1 = (ctlen == 0) | (ct_rm_bytes > 0);
const size_t tot_ad_blk_cnt = ad_blk_cnt + 1ul * flg0;
const size_t tot_ct_blk_cnt = ct_blk_cnt + 1ul * flg1;
uint8_t w = 48;
w ^= 2 * flg0;
w ^= 1 * flg1;
w ^= 8 * (1 - (tot_ad_blk_cnt & 1));
w ^= 4 * (1 - (tot_ct_blk_cnt & 1));
const size_t tot_blk_cnt = tot_ad_blk_cnt + tot_ct_blk_cnt;
const size_t half_blk_cnt = tot_blk_cnt >> 1;
const size_t half_ad_blk_cnt = tot_ad_blk_cnt >> 1;
uint8_t blk[16]{};
uint8_t x = 40;
for (size_t i = 0; i < half_blk_cnt; i++) {
get_auth_block(data, dlen, text, ctlen, (i << 1) ^ 0ul, blk);
romulus_common::rho(st.arr, blk, enc);
romulus_common::update_lfsr(lfsr);
x ^= 4 * (i == half_ad_blk_cnt);
get_auth_block(data, dlen, text, ctlen, (i << 1) ^ 1ul, blk);
romulus_common::encode(key, blk, lfsr, x, st.arr + 16);
skinny::tbc(&st);
romulus_common::update_lfsr(lfsr);
}
const bool flg2 = static_cast<bool>(tot_ad_blk_cnt & 1ul);
const bool flg3 = static_cast<bool>(tot_ct_blk_cnt & 1ul);
if (flg2 == flg3) {
std::memset(blk, 0, 16);
} else {
get_auth_block(data, dlen, text, ctlen, tot_blk_cnt - 1, blk);
}
romulus_common::rho(st.arr, blk, enc);
if (tot_blk_cnt > (half_blk_cnt << 1)) {
romulus_common::update_lfsr(lfsr);
}
romulus_common::encode(key, nonce, lfsr, w, st.arr + 16);
skinny::tbc(&st);
}
uint8_t tmp[16]{};
uint8_t tag_[16]{};
std::memset(tmp, 0, 16);
std::memset(tag_, 0, 16);
romulus_common::rho(st.arr, tmp, tag_);
bool flg = false;
for (size_t i = 0; i < 16; i++) {
flg |= static_cast<bool>(tag[i] ^ tag_[i]);
}
std::memset(text, 0, flg * ctlen);
return !flg;
}
} // namespace romulusm