-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblock_cipher
More file actions
713 lines (664 loc) · 31.8 KB
/
Copy pathblock_cipher
File metadata and controls
713 lines (664 loc) · 31.8 KB
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_CRYPTO_BLOCK_CIPHER_HPP
#define GTL_CRYPTO_BLOCK_CIPHER_HPP
// Summary: An implementation of many block cipher modes to be used with block ciphers like aes<128>.
#ifndef NDEBUG
#if defined(_MSC_VER)
#define __builtin_trap() __debugbreak()
#endif
/// @brief A simple assert macro to break the program if the block_cipher is misused.
#define GTL_BLOCK_CIPHER_ASSERT(ASSERTION, MESSAGE) static_cast<void>((ASSERTION) || (__builtin_trap(), 0))
#else
/// @brief At release time the assert macro is implemented as a nop.
#define GTL_BLOCK_CIPHER_ASSERT(ASSERTION, MESSAGE) static_cast<void>(0)
#endif
namespace gtl {
/// @brief Different block cipher modes available for block cipher operations.
enum class block_cipher_mode {
ecb, // Electronic codebook.
cbc, // Cipher block chaining.
cfb, // Cipher feedback.
ofb, // Output feedback.
ctr, // Counter.
gcm, // Galois/counter.
ccm, // Counter with cipher block chaining message authentication code.
siv // Synthetic initialization vector.
};
namespace {
template <typename cipher_type>
static typename cipher_type::key_type load_key(const unsigned char* b) {
typename cipher_type::key_type k = {};
for (unsigned int i = 0; i < cipher_type::key_size; ++i)
k.data[i] = b[i];
return k;
}
template <typename cipher_type>
static typename cipher_type::block_type load_block(const unsigned char* b) {
typename cipher_type::block_type bl = {};
for (unsigned int i = 0; i < cipher_type::block_size * cipher_type::block_size; ++i)
bl.data[i % cipher_type::block_size].data[i / cipher_type::block_size] = b[i];
return bl;
}
template <typename cipher_type>
static void store_block(const typename cipher_type::block_type& bl, unsigned char* b) {
for (unsigned int i = 0; i < cipher_type::block_size * cipher_type::block_size; ++i)
b[i] = bl.data[i % cipher_type::block_size].data[i / cipher_type::block_size];
}
template <typename cipher_type>
static void encrypt_bytes(const typename cipher_type::key_type& k, const unsigned char* in, unsigned char* out) {
store_block<cipher_type>(cipher_type::encrypt_block(k, load_block<cipher_type>(in)), out);
}
template <typename cipher_type>
static void decrypt_bytes(const typename cipher_type::key_type& k, const unsigned char* in, unsigned char* out) {
store_block<cipher_type>(cipher_type::decrypt_block(k, load_block<cipher_type>(in)), out);
}
static void xor_bytes(const unsigned char* a, const unsigned char* b, unsigned char* o, unsigned int n) {
for (unsigned int i = 0; i < n; ++i)
o[i] = a[i] ^ b[i];
}
static void copy_bytes(const unsigned char* src, unsigned char* dst, unsigned int n) {
for (unsigned int i = 0; i < n; ++i)
dst[i] = src[i];
}
static void zero_bytes(unsigned char* dst, unsigned int n) {
for (unsigned int i = 0; i < n; ++i)
dst[i] = 0;
}
static void inc_be(unsigned char* c, unsigned int n) {
for (int i = static_cast<int>(n) - 1; i >= 0; --i) {
if (++c[i] != 0)
return;
}
}
static void inc32(unsigned char* c) {
for (int i = 15; i >= 12; --i) {
if (++c[i] != 0)
return;
}
}
static void gcm_gf_mul_128(const unsigned char* X, const unsigned char* Y, unsigned char* Z) {
unsigned char z[16] = {};
unsigned char v[16];
copy_bytes(X, v, 16);
for (unsigned int i = 0; i < 128; ++i) {
if ((Y[i / 8] >> (7 - (i % 8))) & 1)
xor_bytes(z, v, z, 16);
unsigned char lsb = v[15] & 1;
for (int j = 15; j > 0; --j)
v[j] = static_cast<unsigned char>((v[j] >> 1) | ((v[j - 1] & 1) << 7));
v[0] >>= 1;
if (lsb)
v[0] ^= 0xE1;
}
copy_bytes(z, Z, 16);
}
static void gcm_ghash_block(const unsigned char* H, unsigned char* Y, const unsigned char* block) {
xor_bytes(Y, block, Y, 16);
unsigned char tmp[16];
gcm_gf_mul_128(Y, H, tmp);
copy_bytes(tmp, Y, 16);
}
static void gcm_ghash(const unsigned char* H, const unsigned char* aad, unsigned int aad_len, const unsigned char* data, unsigned int data_len, unsigned char* out) {
zero_bytes(out, 16);
unsigned char block[16];
for (unsigned int i = 0; i < aad_len; i += 16) {
zero_bytes(block, 16);
for (unsigned int j = 0; j < 16 && (i + j) < aad_len; ++j)
block[j] = aad[i + j];
gcm_ghash_block(H, out, block);
}
for (unsigned int i = 0; i < data_len; i += 16) {
zero_bytes(block, 16);
for (unsigned int j = 0; j < 16 && (i + j) < data_len; ++j)
block[j] = data[i + j];
gcm_ghash_block(H, out, block);
}
zero_bytes(block, 16);
unsigned long long ab = static_cast<unsigned long long>(aad_len) * 8;
unsigned long long db = static_cast<unsigned long long>(data_len) * 8;
for (unsigned int i = 0; i < 8; ++i) {
block[i] = static_cast<unsigned char>((ab >> (56 - 8 * i)) & 0xFF);
block[8 + i] = static_cast<unsigned char>((db >> (56 - 8 * i)) & 0xFF);
}
gcm_ghash_block(H, out, block);
}
static void siv_dbl(const unsigned char* in, unsigned char* out) {
unsigned char carry = 0;
for (int i = 15; i >= 0; --i) {
out[i] = static_cast<unsigned char>((in[i] << 1) | carry);
carry = (in[i] >> 7) & 1;
}
if (carry)
out[15] ^= 0x87;
}
template <typename cipher_type>
static void siv_cmac(const typename cipher_type::key_type& key, const unsigned char* data, unsigned int length, unsigned char* mac, const unsigned char* xor_end = nullptr) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
unsigned char zero[N] = {}, L[N], K1[N], K2[N];
encrypt_bytes<cipher_type>(key, zero, L);
siv_dbl(L, K1);
siv_dbl(K1, K2);
zero_bytes(mac, N);
unsigned int num = (length + N - 1) / N;
if (length == 0)
num = 1;
for (unsigned int i = 0; i < num; ++i) {
unsigned char block[N] = {};
bool last = (i == num - 1);
if (last) {
if (length == 0 || length % N != 0) {
unsigned int rem = length % N;
copy_bytes(data + i * N, block, rem);
block[rem] = 0x80;
xor_bytes(block, K2, block, N);
}
else {
copy_bytes(data + i * N, block, N);
xor_bytes(block, K1, block, N);
}
if (xor_end)
xor_bytes(block, xor_end, block, N);
}
else {
copy_bytes(data + i * N, block, N);
}
xor_bytes(mac, block, mac, N);
encrypt_bytes<cipher_type>(key, mac, mac);
}
}
template <typename cipher_type>
static void siv_s2v(const typename cipher_type::key_type& key, const unsigned char* aad, unsigned int aad_len, const unsigned char* nonce, unsigned int nonce_len, const unsigned char* payload, unsigned int payload_len, unsigned char* out) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
unsigned char zero[N] = {}, d[N], tmp[N];
siv_cmac<cipher_type>(key, zero, N, d);
if (aad && aad_len > 0) {
siv_cmac<cipher_type>(key, aad, aad_len, tmp);
unsigned char dd[N];
siv_dbl(d, dd);
xor_bytes(dd, tmp, d, N);
}
else {
siv_dbl(d, d);
}
if (nonce && nonce_len > 0) {
siv_cmac<cipher_type>(key, nonce, nonce_len, tmp);
unsigned char dd[N];
siv_dbl(d, dd);
xor_bytes(dd, tmp, d, N);
}
else {
unsigned char dd[N];
siv_dbl(d, dd);
copy_bytes(dd, d, N);
}
if (payload_len >= N) {
siv_cmac<cipher_type>(key, payload, payload_len, out, d);
}
else {
unsigned char padded[N] = {};
if (payload)
copy_bytes(payload, padded, payload_len);
padded[payload_len] = 0x80;
unsigned char dd[N];
siv_dbl(d, dd);
xor_bytes(dd, padded, tmp, N);
siv_cmac<cipher_type>(key, tmp, N, out);
}
}
}
template <typename cipher_type, block_cipher_mode mode>
class block_cipher;
// ECB
template <typename cipher_type>
class block_cipher<cipher_type, block_cipher_mode::ecb> final {
public:
constexpr static const unsigned int block_size = cipher_type::block_size;
constexpr static const unsigned int key_size = cipher_type::key_size;
static void encrypt(const unsigned char* data, unsigned int length, const unsigned char* key, unsigned char* output) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
GTL_BLOCK_CIPHER_ASSERT((length % N) == 0, "Length must be multiple of block size.");
const typename cipher_type::key_type k = load_key<cipher_type>(key);
for (unsigned int i = 0; i < length; i += N) {
encrypt_bytes<cipher_type>(k, data + i, output + i);
}
}
static void decrypt(const unsigned char* data, unsigned int length, const unsigned char* key, unsigned char* output) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
GTL_BLOCK_CIPHER_ASSERT((length % N) == 0, "Length must be multiple of block size.");
const typename cipher_type::key_type k = load_key<cipher_type>(key);
for (unsigned int i = 0; i < length; i += N) {
decrypt_bytes<cipher_type>(k, data + i, output + i);
}
}
};
// CBC
template <typename cipher_type>
class block_cipher<cipher_type, block_cipher_mode::cbc> final {
public:
constexpr static const unsigned int block_size = cipher_type::block_size;
constexpr static const unsigned int key_size = cipher_type::key_size;
static void encrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
GTL_BLOCK_CIPHER_ASSERT((length % N) == 0, "Length must be multiple of block size.");
const typename cipher_type::key_type k = load_key<cipher_type>(key);
unsigned char prev[N];
copy_bytes(iv, prev, N);
for (unsigned int i = 0; i < length; i += N) {
unsigned char tmp[N];
xor_bytes(data + i, prev, tmp, N);
encrypt_bytes<cipher_type>(k, tmp, output + i);
copy_bytes(output + i, prev, N);
}
}
static void decrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
GTL_BLOCK_CIPHER_ASSERT((length % N) == 0, "Length must be multiple of block size.");
const typename cipher_type::key_type k = load_key<cipher_type>(key);
unsigned char prev[N];
copy_bytes(iv, prev, N);
for (unsigned int i = 0; i < length; i += N) {
unsigned char dec[N];
decrypt_bytes<cipher_type>(k, data + i, dec);
xor_bytes(dec, prev, output + i, N);
copy_bytes(data + i, prev, N);
}
}
};
// CFB
template <typename cipher_type>
class block_cipher<cipher_type, block_cipher_mode::cfb> final {
public:
constexpr static const unsigned int block_size = cipher_type::block_size;
constexpr static const unsigned int key_size = cipher_type::key_size;
static void encrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
GTL_BLOCK_CIPHER_ASSERT((length % N) == 0, "Length must be multiple of block size.");
const typename cipher_type::key_type k = load_key<cipher_type>(key);
unsigned char fb[N];
copy_bytes(iv, fb, N);
for (unsigned int i = 0; i < length; i += N) {
unsigned char enc[N];
encrypt_bytes<cipher_type>(k, fb, enc);
xor_bytes(data + i, enc, output + i, N);
copy_bytes(output + i, fb, N);
}
}
static void decrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
GTL_BLOCK_CIPHER_ASSERT((length % N) == 0, "Length must be multiple of block size.");
const typename cipher_type::key_type k = load_key<cipher_type>(key);
unsigned char fb[N];
copy_bytes(iv, fb, N);
for (unsigned int i = 0; i < length; i += N) {
unsigned char enc[N];
encrypt_bytes<cipher_type>(k, fb, enc);
xor_bytes(data + i, enc, output + i, N);
copy_bytes(data + i, fb, N);
}
}
};
// OFB
template <typename cipher_type>
class block_cipher<cipher_type, block_cipher_mode::ofb> final {
public:
constexpr static const unsigned int block_size = cipher_type::block_size;
constexpr static const unsigned int key_size = cipher_type::key_size;
static void encrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
GTL_BLOCK_CIPHER_ASSERT((length % N) == 0, "Length must be multiple of block size.");
const typename cipher_type::key_type k = load_key<cipher_type>(key);
unsigned char fb[N];
copy_bytes(iv, fb, N);
for (unsigned int i = 0; i < length; i += N) {
unsigned char enc[N];
encrypt_bytes<cipher_type>(k, fb, enc);
xor_bytes(data + i, enc, output + i, N);
copy_bytes(enc, fb, N);
}
}
static void decrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output) {
encrypt(data, length, key, iv, output);
}
};
// CTR
template <typename cipher_type>
class block_cipher<cipher_type, block_cipher_mode::ctr> final {
public:
constexpr static const unsigned int block_size = cipher_type::block_size;
constexpr static const unsigned int key_size = cipher_type::key_size;
static void encrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
const typename cipher_type::key_type k = load_key<cipher_type>(key);
unsigned char ctr[N];
copy_bytes(iv, ctr, N);
for (unsigned int i = 0; i < length; i += N) {
unsigned char enc[N];
encrypt_bytes<cipher_type>(k, ctr, enc);
unsigned int blen = (length - i < N) ? (length - i) : N;
for (unsigned int j = 0; j < blen; ++j) {
output[i + j] = data[i + j] ^ enc[j];
}
inc_be(ctr, N);
}
}
static void decrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output) {
encrypt(data, length, key, iv, output);
}
};
// GCM
template <typename cipher_type>
class block_cipher<cipher_type, block_cipher_mode::gcm> final {
public:
constexpr static const unsigned int block_size = cipher_type::block_size;
constexpr static const unsigned int key_size = cipher_type::key_size;
static void encrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output, unsigned char* tag, const unsigned char* aad = nullptr, unsigned int aad_length = 0) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
const typename cipher_type::key_type k = load_key<cipher_type>(key);
unsigned char h[N] = {}, zero[N] = {};
encrypt_bytes<cipher_type>(k, zero, h);
unsigned char j0[N] = {};
copy_bytes(iv, j0, 12);
j0[15] = 0x01;
unsigned char ctr[N];
copy_bytes(j0, ctr, N);
inc32(ctr);
for (unsigned int i = 0; i < length; i += N) {
unsigned char enc[N];
encrypt_bytes<cipher_type>(k, ctr, enc);
unsigned int blen = (length - i < N) ? (length - i) : N;
for (unsigned int j = 0; j < blen; ++j) {
output[i + j] = data[i + j] ^ enc[j];
}
inc32(ctr);
}
if (tag) {
unsigned char ghash[N], j0e[N];
gcm_ghash(h, aad, aad_length, output, length, ghash);
encrypt_bytes<cipher_type>(k, j0, j0e);
xor_bytes(j0e, ghash, tag, N);
}
}
static bool decrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output, const unsigned char* tag, const unsigned char* aad = nullptr, unsigned int aad_length = 0) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
const typename cipher_type::key_type k = load_key<cipher_type>(key);
unsigned char h[N] = {}, zero[N] = {};
encrypt_bytes<cipher_type>(k, zero, h);
unsigned char j0[N] = {};
copy_bytes(iv, j0, 12);
j0[15] = 0x01;
// Verify tag if provided with AAD
if (tag && aad && aad_length > 0) {
unsigned char ghash[N], j0e[N], computed[N];
gcm_ghash(h, aad, aad_length, data, length, ghash);
encrypt_bytes<cipher_type>(k, j0, j0e);
xor_bytes(j0e, ghash, computed, N);
bool valid = true;
for (unsigned int i = 0; i < N; ++i) {
if (tag[i] != computed[i]) {
valid = false;
}
}
if (!valid) {
zero_bytes(output, length);
return false;
}
}
unsigned char ctr[N];
copy_bytes(j0, ctr, N);
inc32(ctr);
for (unsigned int i = 0; i < length; i += N) {
unsigned char enc[N];
encrypt_bytes<cipher_type>(k, ctr, enc);
unsigned int blen = (length - i < N) ? (length - i) : N;
for (unsigned int j = 0; j < blen; ++j) {
output[i + j] = data[i + j] ^ enc[j];
}
inc32(ctr);
}
return true;
}
};
// CCM
template <typename cipher_type>
class block_cipher<cipher_type, block_cipher_mode::ccm> final {
public:
constexpr static const unsigned int block_size = cipher_type::block_size;
constexpr static const unsigned int key_size = cipher_type::key_size;
static void encrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output, unsigned char* tag, const unsigned char* aad = nullptr, unsigned int aad_length = 0) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
const typename cipher_type::key_type k = load_key<cipher_type>(key);
const unsigned int nonce_len = 12;
const unsigned int L = 15 - nonce_len;
// B0
unsigned char b0[N] = {};
b0[0] = static_cast<unsigned char>((aad_length > 0 ? 0x40 : 0x00) | (((N - 2) / 2) << 3) | (L - 1));
copy_bytes(iv, b0 + 1, nonce_len);
for (unsigned int i = 0; i < L; ++i)
b0[15 - i] = static_cast<unsigned char>((length >> (8 * i)) & 0xFF);
unsigned char Y[N];
encrypt_bytes<cipher_type>(k, b0, Y);
// AAD
if (aad_length > 0) {
unsigned char ba[N] = {};
unsigned int off = 0;
if (aad_length < 0xFF00) {
ba[0] = static_cast<unsigned char>((aad_length >> 8) & 0xFF);
ba[1] = static_cast<unsigned char>(aad_length & 0xFF);
off = 2;
}
else {
ba[0] = 0xFF;
ba[1] = 0xFE;
ba[2] = static_cast<unsigned char>((aad_length >> 24) & 0xFF);
ba[3] = static_cast<unsigned char>((aad_length >> 16) & 0xFF);
ba[4] = static_cast<unsigned char>((aad_length >> 8) & 0xFF);
ba[5] = static_cast<unsigned char>(aad_length & 0xFF);
off = 6;
}
for (unsigned int i = 0; i < aad_length; ++i) {
if (off == N) {
unsigned char tmp[N];
xor_bytes(Y, ba, tmp, N);
encrypt_bytes<cipher_type>(k, tmp, Y);
zero_bytes(ba, N);
off = 0;
}
ba[off++] ^= aad[i];
}
unsigned char tmp[N];
xor_bytes(Y, ba, tmp, N);
encrypt_bytes<cipher_type>(k, tmp, Y);
}
// Payload auth
for (unsigned int i = 0; i < length; i += N) {
unsigned char bp[N] = {};
unsigned int blen = (length - i < N) ? (length - i) : N;
copy_bytes(data + i, bp, blen);
unsigned char tmp[N];
xor_bytes(Y, bp, tmp, N);
encrypt_bytes<cipher_type>(k, tmp, Y);
}
// Counter blocks
unsigned char a0[N] = {};
a0[0] = static_cast<unsigned char>(L - 1);
copy_bytes(iv, a0 + 1, nonce_len);
unsigned char s0[N];
encrypt_bytes<cipher_type>(k, a0, s0);
for (unsigned int i = 0; i < length; i += N) {
unsigned char ai[N];
copy_bytes(a0, ai, N);
unsigned int counter = (i / N) + 1;
for (unsigned int j = 0; j < L; ++j) {
ai[15 - j] = static_cast<unsigned char>((counter >> (8 * j)) & 0xFF);
}
unsigned char si[N];
encrypt_bytes<cipher_type>(k, ai, si);
unsigned int blen = (length - i < N) ? (length - i) : N;
for (unsigned int j = 0; j < blen; ++j) {
output[i + j] = data[i + j] ^ si[j];
}
}
if (tag)
xor_bytes(Y, s0, tag, N);
}
static bool decrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output, const unsigned char* tag, const unsigned char* aad = nullptr, unsigned int aad_length = 0) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
const typename cipher_type::key_type k = load_key<cipher_type>(key);
const unsigned int nonce_len = 12;
const unsigned int L = 15 - nonce_len;
// Decrypt
unsigned char a0[N] = {};
a0[0] = static_cast<unsigned char>(L - 1);
copy_bytes(iv, a0 + 1, nonce_len);
unsigned char s0[N];
encrypt_bytes<cipher_type>(k, a0, s0);
for (unsigned int i = 0; i < length; i += N) {
unsigned char ai[N];
copy_bytes(a0, ai, N);
unsigned int counter = (i / N) + 1;
for (unsigned int j = 0; j < L; ++j) {
ai[15 - j] = static_cast<unsigned char>((counter >> (8 * j)) & 0xFF);
}
unsigned char si[N];
encrypt_bytes<cipher_type>(k, ai, si);
unsigned int blen = (length - i < N) ? (length - i) : N;
for (unsigned int j = 0; j < blen; ++j) {
output[i + j] = data[i + j] ^ si[j];
}
}
// Verify
if (tag && aad && aad_length > 0) {
unsigned char b0[N] = {};
b0[0] = static_cast<unsigned char>(0x40 | (((N - 2) / 2) << 3) | (L - 1));
copy_bytes(iv, b0 + 1, nonce_len);
for (unsigned int i = 0; i < L; ++i) {
b0[15 - i] = static_cast<unsigned char>((length >> (8 * i)) & 0xFF);
}
unsigned char Y[N];
encrypt_bytes<cipher_type>(k, b0, Y);
unsigned char ba[N] = {};
unsigned int off = 0;
if (aad_length < 0xFF00) {
ba[0] = static_cast<unsigned char>((aad_length >> 8) & 0xFF);
ba[1] = static_cast<unsigned char>(aad_length & 0xFF);
off = 2;
}
else {
ba[0] = 0xFF;
ba[1] = 0xFE;
ba[2] = static_cast<unsigned char>((aad_length >> 24) & 0xFF);
ba[3] = static_cast<unsigned char>((aad_length >> 16) & 0xFF);
ba[4] = static_cast<unsigned char>((aad_length >> 8) & 0xFF);
ba[5] = static_cast<unsigned char>(aad_length & 0xFF);
off = 6;
}
for (unsigned int i = 0; i < aad_length; ++i) {
if (off == N) {
unsigned char tmp[N];
xor_bytes(Y, ba, tmp, N);
encrypt_bytes<cipher_type>(k, tmp, Y);
zero_bytes(ba, N);
off = 0;
}
ba[off++] ^= aad[i];
}
unsigned char tmp[N];
xor_bytes(Y, ba, tmp, N);
encrypt_bytes<cipher_type>(k, tmp, Y);
for (unsigned int i = 0; i < length; i += N) {
unsigned char bp[N] = {};
unsigned int blen = (length - i < N) ? (length - i) : N;
copy_bytes(output + i, bp, blen);
xor_bytes(Y, bp, tmp, N);
encrypt_bytes<cipher_type>(k, tmp, Y);
}
unsigned char computed[N];
xor_bytes(Y, s0, computed, N);
for (unsigned int i = 0; i < N; ++i) {
if (tag[i] != computed[i]) {
zero_bytes(output, length);
return false;
}
}
}
return true;
}
};
// SIV
template <typename cipher_type>
class block_cipher<cipher_type, block_cipher_mode::siv> final {
public:
constexpr static const unsigned int block_size = cipher_type::block_size;
constexpr static const unsigned int key_size = cipher_type::key_size;
static void encrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output, unsigned char* tag, const unsigned char* aad = nullptr, unsigned int aad_length = 0) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
const typename cipher_type::key_type k = load_key<cipher_type>(key);
unsigned char v[N];
siv_s2v<cipher_type>(k, aad, aad_length, iv, 12, data, length, v);
if (tag) {
copy_bytes(v, tag, N);
}
unsigned char ctr[N];
copy_bytes(v, ctr, N);
ctr[8] &= 0x7F;
ctr[12] &= 0x7F;
for (unsigned int i = 0; i < length; i += N) {
unsigned char enc[N];
encrypt_bytes<cipher_type>(k, ctr, enc);
unsigned int blen = (length - i < N) ? (length - i) : N;
for (unsigned int j = 0; j < blen; ++j) {
output[i + j] = data[i + j] ^ enc[j];
}
inc_be(ctr, N);
}
}
static bool decrypt(const unsigned char* data, unsigned int length, const unsigned char* key, const unsigned char* iv, unsigned char* output, const unsigned char* tag, const unsigned char* aad = nullptr, unsigned int aad_length = 0) {
constexpr unsigned int N = cipher_type::block_size * cipher_type::block_size;
const typename cipher_type::key_type k = load_key<cipher_type>(key);
if (!tag) {
zero_bytes(output, length);
return false;
}
unsigned char v[N];
copy_bytes(tag, v, N);
unsigned char ctr[N];
copy_bytes(v, ctr, N);
ctr[8] &= 0x7F;
ctr[12] &= 0x7F;
for (unsigned int i = 0; i < length; i += N) {
unsigned char enc[N];
encrypt_bytes<cipher_type>(k, ctr, enc);
unsigned int blen = (length - i < N) ? (length - i) : N;
for (unsigned int j = 0; j < blen; ++j) {
output[i + j] = data[i + j] ^ enc[j];
}
inc_be(ctr, N);
}
if (aad && aad_length > 0) {
unsigned char cv[N];
siv_s2v<cipher_type>(k, aad, aad_length, iv, 12, output, length, cv);
for (unsigned int i = 0; i < N; ++i) {
if (cv[i] != v[i]) {
zero_bytes(output, length);
return false;
}
}
}
return true;
}
};
}
#undef GTL_BLOCK_CIPHER_ASSERT
#endif // GTL_CRYPTO_BLOCK_CIPHER_HPP