-
Notifications
You must be signed in to change notification settings - Fork 1
/
faest_aes.c
2656 lines (2271 loc) · 100 KB
/
faest_aes.c
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-License-Identifier: MIT
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "faest.h"
#include "faest_aes.h"
#include "fields.h"
#include "vole.h"
#include "universal_hashing.h"
#include "utils.h"
#include "parameters.h"
#include <string.h>
#include <stdlib.h>
static_assert(FAEST_128F_L == FAEST_128S_L, "Invalid parameters");
static_assert(FAEST_128F_LAMBDA == FAEST_128S_LAMBDA, "Invalid parameters");
static_assert(FAEST_128F_Lke == FAEST_128S_Lke, "Invalid parameters");
static_assert(FAEST_128F_Nwd == FAEST_128S_Nwd, "Invalid parameters");
static_assert(FAEST_128F_R == FAEST_128S_R, "Invalid parameters");
static_assert(FAEST_128F_Senc == FAEST_128S_Senc, "Invalid parameters");
static_assert(FAEST_128F_Ske == FAEST_128S_Ske, "Invalid parameters");
static_assert(FAEST_192F_L == FAEST_192S_L, "Invalid parameters");
static_assert(FAEST_192F_LAMBDA == FAEST_192S_LAMBDA, "Invalid parameters");
static_assert(FAEST_192F_Lke == FAEST_192S_Lke, "Invalid parameters");
static_assert(FAEST_192F_Nwd == FAEST_192S_Nwd, "Invalid parameters");
static_assert(FAEST_192F_R == FAEST_192S_R, "Invalid parameters");
static_assert(FAEST_192F_Senc == FAEST_192S_Senc, "Invalid parameters");
static_assert(FAEST_192F_Ske == FAEST_192S_Ske, "Invalid parameters");
static_assert(FAEST_256F_L == FAEST_256S_L, "Invalid parameters");
static_assert(FAEST_256F_LAMBDA == FAEST_256S_LAMBDA, "Invalid parameters");
static_assert(FAEST_256F_Lke == FAEST_256S_Lke, "Invalid parameters");
static_assert(FAEST_256F_Nwd == FAEST_256S_Nwd, "Invalid parameters");
static_assert(FAEST_256F_R == FAEST_256S_R, "Invalid parameters");
static_assert(FAEST_256F_Senc == FAEST_256S_Senc, "Invalid parameters");
static_assert(FAEST_256F_Ske == FAEST_256S_Ske, "Invalid parameters");
static_assert(FAEST_EM_128F_LAMBDA == FAEST_EM_128S_LAMBDA, "Invalid parameters");
static_assert(FAEST_EM_128F_Lenc == FAEST_EM_128S_Lenc, "Invalid parameters");
static_assert(FAEST_EM_128F_Nwd == FAEST_EM_128S_Nwd, "Invalid parameters");
static_assert(FAEST_EM_128F_R == FAEST_EM_128S_R, "Invalid parameters");
static_assert(FAEST_EM_128F_Senc == FAEST_EM_128S_Senc, "Invalid parameters");
// for scan-build
static_assert(FAEST_EM_128F_LAMBDA * (FAEST_EM_128F_R + 1) / 8 ==
sizeof(aes_word_t) * FAEST_EM_128F_Nwd * (FAEST_EM_128F_R + 1),
"Invalid parameters");
static_assert(FAEST_EM_192F_LAMBDA == FAEST_EM_192S_LAMBDA, "Invalid parameters");
static_assert(FAEST_EM_192F_Lenc == FAEST_EM_192S_Lenc, "Invalid parameters");
static_assert(FAEST_EM_192F_Nwd == FAEST_EM_192S_Nwd, "Invalid parameters");
static_assert(FAEST_EM_192F_R == FAEST_EM_192S_R, "Invalid parameters");
static_assert(FAEST_EM_192F_Senc == FAEST_EM_192S_Senc, "Invalid parameters");
// for scan-build
static_assert(FAEST_EM_192F_LAMBDA * (FAEST_EM_192F_R + 1) / 8 ==
sizeof(aes_word_t) * FAEST_EM_192F_Nwd * (FAEST_EM_192F_R + 1),
"Invalid parameters");
static_assert(FAEST_EM_256F_LAMBDA == FAEST_EM_256S_LAMBDA, "Invalid parameters");
static_assert(FAEST_EM_256F_Lenc == FAEST_EM_256S_Lenc, "Invalid parameters");
static_assert(FAEST_EM_256F_Nwd == FAEST_EM_256S_Nwd, "Invalid parameters");
static_assert(FAEST_EM_256F_R == FAEST_EM_256S_R, "Invalid parameters");
static_assert(FAEST_EM_256F_Senc == FAEST_EM_256S_Senc, "Invalid parameters");
// for scan-build
static_assert(FAEST_EM_256F_LAMBDA * (FAEST_EM_256F_R + 1) / 8 ==
sizeof(aes_word_t) * FAEST_EM_256F_Nwd * (FAEST_EM_256F_R + 1),
"Invalid parameters");
static const bf8_t Rcon[30] = {
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91,
};
static bf128_t* column_to_row_major_and_shrink_V_128(uint8_t** v, unsigned int ell) {
// V is \hat \ell times \lambda matrix over F_2
// v has \hat \ell rows, \lambda columns, storing in column-major order, new_v has \ell + \lambda
// rows and \lambda columns storing in row-major order
bf128_t* new_v = malloc((ell + sizeof(bf128_t) * 8) * sizeof(bf128_t));
for (unsigned int row = 0; row != ell + sizeof(bf128_t) * 8; ++row) {
uint8_t new_row[sizeof(bf128_t)] = {0};
for (unsigned int column = 0; column != sizeof(bf128_t) * 8; ++column) {
ptr_set_bit(new_row, ptr_get_bit(v[column], row), column);
}
new_v[row] = bf128_load(new_row);
}
return new_v;
}
static bf192_t* column_to_row_major_and_shrink_V_192(uint8_t** v, unsigned int ell) {
// V is \hat \ell times \lambda matrix over F_2
// v has \hat \ell rows, \lambda columns, storing in column-major order, new_v has \ell + \lambda
// rows and \lambda columns storing in row-major order
bf192_t* new_v = malloc((ell + sizeof(bf192_t) * 8) * sizeof(bf192_t));
for (unsigned int row = 0; row != ell + sizeof(bf192_t) * 8; ++row) {
uint8_t new_row[sizeof(bf192_t)] = {0};
for (unsigned int column = 0; column != sizeof(bf192_t) * 8; ++column) {
ptr_set_bit(new_row, ptr_get_bit(v[column], row), column);
}
new_v[row] = bf192_load(new_row);
}
return new_v;
}
static bf256_t* column_to_row_major_and_shrink_V_256(uint8_t** v, unsigned int ell) {
// V is \hat \ell times \lambda matrix over F_2
// v has \hat \ell rows, \lambda columns, storing in column-major order, new_v has \ell + \lambda
// rows and \lambda columns storing in row-major order
bf256_t* new_v = malloc((ell + sizeof(bf256_t) * 8) * sizeof(bf256_t));
for (unsigned int row = 0; row != ell + sizeof(bf256_t) * 8; ++row) {
uint8_t new_row[sizeof(bf256_t)] = {0};
for (unsigned int column = 0; column != sizeof(bf256_t) * 8; ++column) {
ptr_set_bit(new_row, ptr_get_bit(v[column], row), column);
}
new_v[row] = bf256_load(new_row);
}
return new_v;
}
// m == 1 implementations
static void aes_key_schedule_forward_1(const uint8_t* x, uint8_t* out,
const faest_paramset_t* params) {
// Step: 1 skipped (sanity check)
const unsigned int lambda = params->faest_param.lambda;
const unsigned int R = params->faest_param.R;
const unsigned int Nwd = params->faest_param.Nwd;
const unsigned int lambdaBytes = lambda / 8;
const unsigned int out_len = (R + 1) * 128 / 8;
// Step 3
memcpy(out, x, lambdaBytes);
memset(out + lambdaBytes, 0, out_len - lambdaBytes);
// Step: 4
unsigned int i_wd = lambda;
// Step: 5..10
for (unsigned int j = Nwd; j < 4 * (R + 1); j++) {
if ((j % Nwd) == 0 || (Nwd > 6 && (j % Nwd) == 4)) {
memcpy(out + 32 * j / 8, x + i_wd / 8, 4);
i_wd += 32;
} else {
for (unsigned int i = 0; i < 32; i += 8) {
// bit spliced
out[(32 * j + i) / 8] |= out[(32 * (j - Nwd) + i) / 8] ^ out[(32 * (j - 1) + i) / 8];
}
}
}
}
static void aes_key_schedule_backward_1(const uint8_t* x, const uint8_t* xk, uint8_t* out,
const faest_paramset_t* params) {
// Step: 1 skipped (sanity check)
const unsigned int lambda = params->faest_param.lambda;
const unsigned int Ske = params->faest_param.Ske;
// Step: 2
unsigned int iwd = 0;
unsigned int c = 0;
bool rmvRcon = true;
unsigned int ircon = 0;
for (unsigned int j = 0; j < Ske; j++) {
// Step 7 (bit sliced)
uint8_t x_tilde = x[j] ^ xk[iwd + c];
// Step 8
// this function is only called with Mtag == Mkey == 0
if (/* Mtag == 0 && */ rmvRcon == true && c == 0) {
// Steps 12 and 13, bitsliced; delta is always 0
x_tilde ^= Rcon[ircon];
++ircon;
}
// Step: 15..19 (bit spliced)
const uint8_t y_tilde = rotr8(x_tilde, 7) ^ rotr8(x_tilde, 5) ^ rotr8(x_tilde, 2);
// this function is only called with Mtag == Mkey == 0
// set_bit((1 ^ Mtag) & (1 ^ Mkey), 0) ^ set_bit((1 ^ Mtag) & (1 ^ Mkey), 2) == 0x5
out[j] = y_tilde ^ 0x5;
// Step: 20
++c;
if (c == 4) {
c = 0;
if (lambda == 192) {
iwd += 192 / 8;
} else {
iwd += 128 / 8;
if (lambda == 256) {
rmvRcon = !rmvRcon;
}
}
}
}
}
// lambda == 128 implementation
static void aes_key_schedule_forward_128(const bf128_t* v, bf128_t* bf_out) {
// Step: 1 sanity check (skipped)
memcpy(bf_out, v, FAEST_128F_LAMBDA * sizeof(bf128_t));
// Step: 4
unsigned int i_wd = FAEST_128F_LAMBDA;
// Step: 5..10
for (unsigned int j = FAEST_128F_Nwd; j < 4 * (FAEST_128F_R + 1); j++) {
if ((j % FAEST_128F_Nwd) == 0 || (FAEST_128F_Nwd > 6 && (j % FAEST_128F_Nwd) == 4)) {
// copy all at once
memcpy(bf_out + j * 32, v + i_wd, sizeof(bf128_t) * 32);
i_wd += 32;
} else {
for (unsigned int i = 0; i < 32; i++) {
bf_out[(32 * j) + i] =
bf128_add(bf_out[32 * (j - FAEST_128F_Nwd) + i], bf_out[32 * (j - 1) + i]);
}
}
}
}
static void aes_key_schedule_backward_128(const bf128_t* v, const bf128_t* Vk, uint8_t Mtag,
uint8_t Mkey, const uint8_t* delta, bf128_t* bf_out) {
// Step: 1
assert(!((Mtag == 1 && Mkey == 1) || (Mkey == 1 && delta == NULL)));
const bf128_t bf_delta = delta ? bf128_load(delta) : bf128_zero();
unsigned int iwd = 0;
unsigned int c = 0;
unsigned int ircon = 0;
bf128_t bf_minus_mkey = bf128_from_bit(1 ^ Mkey);
uint8_t minus_mtag = 1 ^ Mtag;
bf128_t bf_mkey_times_delta = bf128_mul_bit(bf_delta, Mkey);
bf_mkey_times_delta = bf128_add(bf_mkey_times_delta, bf_minus_mkey);
for (unsigned int j = 0; j < FAEST_128F_Ske; j++) {
// Step 7
bf128_t bf_x_tilde[8];
for (unsigned int i = 0; i < 8; i++) {
bf_x_tilde[i] = bf128_add(v[8 * j + i], Vk[iwd + 8 * c + i]);
}
if (Mtag == 0 && c == 0) {
// Step 9
uint8_t r = Rcon[ircon];
ircon = ircon + 1;
bf128_t bf_r[8];
for (unsigned int i = 0; i < 8; i++) {
// Step 12
bf_r[i] = bf128_mul_bit(bf_mkey_times_delta, get_bit(r, i));
// Step 13
bf_x_tilde[i] = bf128_add(bf_x_tilde[i], bf_r[i]);
}
}
for (unsigned int i = 0; i < 8; ++i) {
bf_out[i + 8 * j] = bf128_add(bf128_add(bf_x_tilde[(i + 7) % 8], bf_x_tilde[(i + 5) % 8]),
bf_x_tilde[(i + 2) % 8]);
}
bf_out[0 + 8 * j] =
bf128_add(bf_out[0 + 8 * j], bf128_mul_bit(bf_mkey_times_delta, minus_mtag));
bf_out[2 + 8 * j] =
bf128_add(bf_out[2 + 8 * j], bf128_mul_bit(bf_mkey_times_delta, minus_mtag));
c = c + 1;
if (c == 4) {
c = 0;
iwd += 128;
}
}
}
static void aes_key_schedule_constraints_Mkey_0_128(const uint8_t* w, const bf128_t* v, bf128_t* A0,
bf128_t* A1, uint8_t* k, bf128_t* vk,
const faest_paramset_t* params) {
// for scan-build
assert(FAEST_128F_Ske == params->faest_param.Ske);
// Step: 2
aes_key_schedule_forward_1(w, k, params);
// Step: 3
aes_key_schedule_forward_128(v, vk);
// Step: 4
uint8_t w_dash[FAEST_128F_Ske];
aes_key_schedule_backward_1(w + FAEST_128F_LAMBDA / 8, k, w_dash, params);
// Step: 5
bf128_t v_w_dash[FAEST_128F_Ske * 8];
aes_key_schedule_backward_128(v + FAEST_128F_LAMBDA, vk, 1, 0, NULL, v_w_dash);
// Step: 6..8
unsigned int iwd = 32 * (FAEST_128F_Nwd - 1);
for (unsigned int j = 0; j < FAEST_128F_Ske / 4; j++) {
bf128_t bf_k_hat[4];
bf128_t bf_v_k_hat[4];
bf128_t bf_w_dash_hat[4];
bf128_t bf_v_w_dash_hat[4];
for (unsigned int r = 0; r <= 3; r++) {
// Step: 10..11
bf_k_hat[(r + 3) % 4] = bf128_byte_combine_bits(k[(iwd + 8 * r) / 8]);
bf_v_k_hat[(r + 3) % 4] = bf128_byte_combine(vk + (iwd + 8 * r));
bf_w_dash_hat[r] = bf128_byte_combine_bits(w_dash[(32 * j + 8 * r) / 8]);
bf_v_w_dash_hat[r] = bf128_byte_combine(v_w_dash + (32 * j + 8 * r));
}
// Step: 13..17
for (unsigned int r = 0; r <= 3; r++) {
A0[4 * j + r] = bf128_mul(bf_v_k_hat[r], bf_v_w_dash_hat[r]);
A1[4 * j + r] =
bf128_add(bf128_add(bf128_mul(bf128_add(bf_k_hat[r], bf_v_k_hat[r]),
bf128_add(bf_w_dash_hat[r], bf_v_w_dash_hat[r])),
bf128_from_bf8(bf8_one())),
A0[4 * j + r]);
}
iwd = iwd + 128;
}
}
static void aes_key_schedule_constraints_Mkey_1_128(const bf128_t* q, const uint8_t* delta,
bf128_t* B, bf128_t* qk) {
// Step: 19..20
aes_key_schedule_forward_128(q, qk);
bf128_t q_w_dash[FAEST_128F_Ske * 8];
aes_key_schedule_backward_128(&q[FAEST_128F_LAMBDA], qk, 0, 1, delta, q_w_dash);
const bf128_t bf_delta = bf128_load(delta);
// Step 23..24
unsigned int iwd = 32 * (FAEST_128F_Nwd - 1);
for (unsigned int j = 0; j < FAEST_128F_Ske / 4; j++) {
bf128_t bf_q_hat_k[4];
bf128_t bf_q_hat_w_dash[4];
for (unsigned int r = 0; r <= 3; r++) {
// Step: 25..26
bf_q_hat_k[(r + 3) % 4] = bf128_byte_combine(qk + ((iwd + 8 * r)));
bf_q_hat_w_dash[r] = bf128_byte_combine(q_w_dash + ((32 * j + 8 * r)));
}
// Step: 27
for (unsigned int r = 0; r <= 3; r++) {
bf128_t bf_tmp = bf128_mul(bf_q_hat_k[r], bf_q_hat_w_dash[r]);
B[4 * j + r] = bf128_add(bf_tmp, bf128_mul(bf_delta, bf_delta));
}
iwd = iwd + 128;
}
}
static void aes_enc_forward_128_1(const uint8_t* x, const uint8_t* xk, const uint8_t* in,
bf128_t* bf_y) {
// called only with Mtag == Mkey == 0
// Step: 2
for (unsigned int i = 0; i < 16; i++) {
// Step: 3, 4 (bit spliced)
// -((1 ^ Mtag) & (1 ^ Mkey)) == 0xFF
const uint8_t xin = in[i];
// Step: 5
bf_y[i] = bf128_add(bf128_byte_combine_bits(xin), bf128_byte_combine_bits(xk[i]));
}
const bf128_t bf_two = bf128_byte_combine_bits(2);
const bf128_t bf_three = bf128_byte_combine_bits(3);
for (unsigned int j = 1; j < FAEST_128F_R; j++) {
for (unsigned int c = 0; c <= 3; c++) {
const unsigned int ix = 128 * (j - 1) + 32 * c;
const unsigned int ik = 128 * j + 32 * c;
const unsigned int iy = 16 * j + 4 * c;
bf128_t bf_x_hat[4];
bf128_t bf_xk_hat[4];
for (unsigned int r = 0; r <= 3; r++) {
// Step: 12..13
bf_x_hat[r] = bf128_byte_combine_bits(x[(ix + 8 * r) / 8]);
bf_xk_hat[r] = bf128_byte_combine_bits(xk[(ik + 8 * r) / 8]);
}
// Step : 14
bf_y[iy + 0] = bf128_add(bf_xk_hat[0], bf128_mul(bf_x_hat[0], bf_two));
bf_y[iy + 0] = bf128_add(bf_y[iy + 0], bf128_mul(bf_x_hat[1], bf_three));
bf_y[iy + 0] = bf128_add(bf_y[iy + 0], bf_x_hat[2]);
bf_y[iy + 0] = bf128_add(bf_y[iy + 0], bf_x_hat[3]);
// Step: 15
bf_y[iy + 1] = bf128_add(bf_xk_hat[1], bf_x_hat[0]);
bf_y[iy + 1] = bf128_add(bf_y[iy + 1], bf128_mul(bf_x_hat[1], bf_two));
bf_y[iy + 1] = bf128_add(bf_y[iy + 1], bf128_mul(bf_x_hat[2], bf_three));
bf_y[iy + 1] = bf128_add(bf_y[iy + 1], bf_x_hat[3]);
// Step: 16
bf_y[iy + 2] = bf128_add(bf_xk_hat[2], bf_x_hat[0]);
bf_y[iy + 2] = bf128_add(bf_y[iy + 2], bf_x_hat[1]);
bf_y[iy + 2] = bf128_add(bf_y[iy + 2], bf128_mul(bf_x_hat[2], bf_two));
bf_y[iy + 2] = bf128_add(bf_y[iy + 2], bf128_mul(bf_x_hat[3], bf_three));
// Step: 17
bf_y[iy + 3] = bf128_add(bf_xk_hat[3], bf128_mul(bf_x_hat[0], bf_three));
bf_y[iy + 3] = bf128_add(bf_y[iy + 3], bf_x_hat[1]);
bf_y[iy + 3] = bf128_add(bf_y[iy + 3], bf_x_hat[2]);
bf_y[iy + 3] = bf128_add(bf_y[iy + 3], bf128_mul(bf_x_hat[3], bf_two));
}
}
}
static void aes_enc_forward_128(const bf128_t* bf_x, const bf128_t* bf_xk, const uint8_t* in,
uint8_t Mtag, uint8_t Mkey, const uint8_t* delta, bf128_t* bf_y) {
const bf128_t bf_delta = delta ? bf128_load(delta) : bf128_zero();
const bf128_t bf_minus_mtag = bf128_from_bit(1 ^ Mtag);
const bf128_t bf_minus_mkey = bf128_from_bit(1 ^ Mkey);
const bf128_t bf_mkey = bf128_from_bit(Mkey);
// Step: 2..4
for (unsigned int i = 0; i < 16; i++) {
bf128_t bf_xin[8];
for (unsigned int j = 0; j < 8; j++) {
bf_xin[j] = bf128_mul(bf128_mul_bit(bf_minus_mtag, get_bit(in[i], j)),
bf128_add(bf128_mul(bf_mkey, bf_delta), bf_minus_mkey));
}
// Step: 5
bf_y[i] = bf128_add(bf128_byte_combine(bf_xin), bf128_byte_combine(bf_xk + (8 * i)));
}
const bf128_t bf_two = bf128_byte_combine_bits(2);
const bf128_t bf_three = bf128_byte_combine_bits(3);
for (unsigned int j = 1; j < FAEST_128F_R; j++) {
for (unsigned int c = 0; c <= 3; c++) {
const unsigned int ix = 128 * (j - 1) + 32 * c;
const unsigned int ik = 128 * j + 32 * c;
const unsigned int iy = 16 * j + 4 * c;
bf128_t bf_x_hat[4];
bf128_t bf_xk_hat[4];
for (unsigned int r = 0; r <= 3; r++) {
// Step: 12..13
bf_x_hat[r] = bf128_byte_combine(bf_x + (ix + 8 * r));
bf_xk_hat[r] = bf128_byte_combine(bf_xk + (ik + 8 * r));
}
// Step : 14
bf_y[iy + 0] = bf128_add(bf_xk_hat[0], bf128_mul(bf_x_hat[0], bf_two));
bf_y[iy + 0] = bf128_add(bf_y[iy + 0], bf128_mul(bf_x_hat[1], bf_three));
bf_y[iy + 0] = bf128_add(bf_y[iy + 0], bf_x_hat[2]);
bf_y[iy + 0] = bf128_add(bf_y[iy + 0], bf_x_hat[3]);
// Step: 15
bf_y[iy + 1] = bf128_add(bf_xk_hat[1], bf_x_hat[0]);
bf_y[iy + 1] = bf128_add(bf_y[iy + 1], bf128_mul(bf_x_hat[1], bf_two));
bf_y[iy + 1] = bf128_add(bf_y[iy + 1], bf128_mul(bf_x_hat[2], bf_three));
bf_y[iy + 1] = bf128_add(bf_y[iy + 1], bf_x_hat[3]);
// Step: 16
bf_y[iy + 2] = bf128_add(bf_xk_hat[2], bf_x_hat[0]);
bf_y[iy + 2] = bf128_add(bf_y[iy + 2], bf_x_hat[1]);
bf_y[iy + 2] = bf128_add(bf_y[iy + 2], bf128_mul(bf_x_hat[2], bf_two));
bf_y[iy + 2] = bf128_add(bf_y[iy + 2], bf128_mul(bf_x_hat[3], bf_three));
// Step: 17
bf_y[iy + 3] = bf128_add(bf_xk_hat[3], bf128_mul(bf_x_hat[0], bf_three));
bf_y[iy + 3] = bf128_add(bf_y[iy + 3], bf_x_hat[1]);
bf_y[iy + 3] = bf128_add(bf_y[iy + 3], bf_x_hat[2]);
bf_y[iy + 3] = bf128_add(bf_y[iy + 3], bf128_mul(bf_x_hat[3], bf_two));
}
}
}
static void aes_enc_backward_128_1(const uint8_t* x, const uint8_t* xk, const uint8_t* out,
bf128_t* y_out) {
// called only with Mtag == Mkey == 0
uint8_t xtilde;
// Step:2..4
for (unsigned int j = 0; j < FAEST_128F_R; j++) {
for (unsigned int c = 0; c <= 3; c++) {
for (unsigned int r = 0; r <= 3; r++) {
// Step: 5..6
unsigned int ird = (128 * j) + (32 * ((c - r + 4) % 4)) + (8 * r);
if (j < (FAEST_128F_R - 1)) {
// Step: 7
xtilde = x[ird / 8];
} else {
// Step: 9..11 (bit spliced)
// -((1 ^ Mtag) & (1 ^ Mkey)) == 0xff
const uint8_t xout = out[(ird - 128 * (FAEST_128F_R - 1)) / 8];
xtilde = xout ^ xk[(128 + ird) / 8];
}
// Step: 12..17 (bit spliced)
// set_bit((1 ^ Mtag) & (1 ^ Mkey), 0) ^ set_bit((1 ^ Mtag) & (1 ^ Mkey), 2) == 0x5
const uint8_t ytilde = rotr8(xtilde, 7) ^ rotr8(xtilde, 5) ^ rotr8(xtilde, 2) ^ 0x5;
// Step: 18
y_out[16 * j + 4 * c + r] = bf128_byte_combine_bits(ytilde);
}
}
}
}
static void aes_enc_backward_128(const bf128_t* bf_x, const bf128_t* bf_xk, uint8_t Mtag,
uint8_t Mkey, const uint8_t* delta, const uint8_t* out,
bf128_t* y_out) {
// Step: 1
const bf128_t bf_delta = delta ? bf128_load(delta) : bf128_zero();
const bf128_t factor =
bf128_mul_bit(bf128_add(bf128_mul_bit(bf_delta, Mkey), bf128_from_bit(1 ^ Mkey)), 1 ^ Mtag);
// Step: 2..4
for (unsigned int j = 0; j < FAEST_128F_R; j++) {
for (unsigned int c = 0; c <= 3; c++) {
for (unsigned int r = 0; r <= 3; r++) {
bf128_t bf_x_tilde[8];
// Step: 5
unsigned int ird = (128 * j) + (32 * ((c - r + 4) % 4)) + (8 * r);
// Step: 6
if (j < (FAEST_128F_R - 1)) {
// Step: 7
memcpy(bf_x_tilde, bf_x + ird, sizeof(bf_x_tilde));
} else {
// Step: 10
for (unsigned int i = 0; i < 8; ++i) {
// Step: 11
bf128_t bf_xout =
bf128_mul_bit(factor, get_bit(out[(ird - 128 * (FAEST_128F_R - 1)) / 8], i));
// Step: 12
bf_x_tilde[i] = bf128_add(bf_xout, bf_xk[128 + ird + i]);
}
}
// Step: 13..17
bf128_t bf_y_tilde[8];
for (unsigned int i = 0; i < 8; ++i) {
bf_y_tilde[i] = bf128_add(bf128_add(bf_x_tilde[(i + 7) % 8], bf_x_tilde[(i + 5) % 8]),
bf_x_tilde[(i + 2) % 8]);
}
bf_y_tilde[0] = bf128_add(bf_y_tilde[0], factor);
bf_y_tilde[2] = bf128_add(bf_y_tilde[2], factor);
// Step: 18
y_out[16 * j + 4 * c + r] = bf128_byte_combine(bf_y_tilde);
}
}
}
}
static void aes_enc_constraints_Mkey_0_128(const uint8_t* in, const uint8_t* out, const uint8_t* w,
const bf128_t* v, const uint8_t* k, const bf128_t* vk,
bf128_t* A0, bf128_t* A1) {
bf128_t s[FAEST_128F_Senc];
bf128_t vs[FAEST_128F_Senc];
bf128_t s_dash[FAEST_128F_Senc];
bf128_t vs_dash[FAEST_128F_Senc];
aes_enc_forward_128_1(w, k, in, s);
aes_enc_forward_128(v, vk, in, 1, 0, NULL, vs);
aes_enc_backward_128_1(w, k, out, s_dash);
aes_enc_backward_128(v, vk, 1, 0, NULL, out, vs_dash);
for (unsigned int j = 0; j < FAEST_128F_Senc; j++) {
A0[j] = bf128_mul(vs[j], vs_dash[j]);
A1[j] = bf128_add(
bf128_add(bf128_mul(bf128_add(s[j], vs[j]), bf128_add(s_dash[j], vs_dash[j])), A0[j]),
bf128_one());
}
}
static void aes_enc_constraints_Mkey_1_128(const uint8_t* in, const uint8_t* out, const bf128_t* q,
const bf128_t* qk, const uint8_t* delta, bf128_t* B) {
// Step: 11..12
bf128_t qs[FAEST_128F_Senc];
bf128_t qs_dash[FAEST_128F_Senc];
aes_enc_forward_128(q, qk, in, 0, 1, delta, qs);
aes_enc_backward_128(q, qk, 0, 1, delta, out, qs_dash);
// Step: 13..14
bf128_t minus_part = bf128_mul(bf128_load(delta), bf128_load(delta));
for (unsigned int j = 0; j < FAEST_128F_Senc; j++) {
B[j] = bf128_add(bf128_mul(qs[j], qs_dash[j]), minus_part);
}
}
static void aes_prove_128(const uint8_t* w, const uint8_t* u, uint8_t** V, const uint8_t* in,
const uint8_t* out, const uint8_t* chall, uint8_t* a_tilde,
uint8_t* b_tilde, const faest_paramset_t* params) {
// Step: 1..2
bf128_t* bf_v = column_to_row_major_and_shrink_V_128(V, FAEST_128F_L);
// Step: 3..4
// do nothing
// Step: 6
// Step: 7
bf128_t* A0 = malloc(sizeof(bf128_t) * (FAEST_128F_Ske + FAEST_128F_Senc + 1));
bf128_t* A1 = malloc(sizeof(bf128_t) * (FAEST_128F_Ske + FAEST_128F_Senc + 1));
uint8_t* k = malloc((FAEST_128F_R + 1) * 128 / 8);
bf128_t* vk = malloc(sizeof(bf128_t) * ((FAEST_128F_R + 1) * 128));
aes_key_schedule_constraints_Mkey_0_128(w, bf_v, A0, A1, k, vk, params);
// Step: Skipping 8 in implementation
// Step: 9
// Step: 10,11
aes_enc_constraints_Mkey_0_128(in, out, w + FAEST_128F_Lke / 8, bf_v + FAEST_128F_Lke, k, vk,
A0 + FAEST_128F_Ske, A1 + FAEST_128F_Ske);
// Step: 12 (beta == 1)
free(vk);
free(k);
// Step: 16..18
A1[FAEST_128F_Ske + FAEST_128F_Senc] = bf128_load(u + FAEST_128F_L / 8);
A0[FAEST_128F_Ske + FAEST_128F_Senc] = bf128_sum_poly(bf_v + FAEST_128F_L);
free(bf_v);
zk_hash_128(a_tilde, chall, A1, FAEST_128F_Ske + FAEST_128F_Senc);
zk_hash_128(b_tilde, chall, A0, FAEST_128F_Ske + FAEST_128F_Senc);
free(A0);
free(A1);
}
static uint8_t* aes_verify_128(const uint8_t* d, uint8_t** Q, const uint8_t* chall_2,
const uint8_t* chall_3, const uint8_t* a_tilde, const uint8_t* in,
const uint8_t* out, const faest_paramset_t* params) {
const unsigned int tau = params->faest_param.tau;
const unsigned int t0 = params->faest_param.t0;
const unsigned int k0 = params->faest_param.k0;
const unsigned int t1 = params->faest_param.t1;
const unsigned int k1 = params->faest_param.k1;
// Step: 1
const uint8_t* delta = chall_3;
// Step: 2,3
// do nothing
// Step: 4..10
for (unsigned int i = 0, col = 0; i < tau; i++) {
unsigned int depth = i < t0 ? k0 : k1;
uint8_t decoded_challenge[MAX_DEPTH];
ChalDec(chall_3, i, k0, t0, k1, t1, decoded_challenge);
for (unsigned int j = 0; j < depth; j++, ++col) {
if (decoded_challenge[j] == 1) {
xor_u8_array(d, Q[col], Q[col], (FAEST_128F_L + 7) / 8);
}
}
}
// Step: 11..12
bf128_t* bf_q = column_to_row_major_and_shrink_V_128(Q, FAEST_128F_L);
// Step: 13
bf128_t* qk = malloc(sizeof(bf128_t) * ((FAEST_128F_R + 1) * 128));
bf128_t* B_0 = malloc(sizeof(bf128_t) * (FAEST_128F_Ske + FAEST_128F_Senc + 1));
aes_key_schedule_constraints_Mkey_1_128(bf_q, delta, B_0, qk);
// Step: 14
bf128_t* B_1 = B_0 + FAEST_128F_Ske;
aes_enc_constraints_Mkey_1_128(in, out, bf_q + FAEST_128F_Lke, qk, delta, B_1);
// Step: 18 (beta == 1)
free(qk);
// Step: 20
B_0[FAEST_128F_Ske + FAEST_128F_Senc] = bf128_sum_poly(bf_q + FAEST_128F_L);
free(bf_q);
// Step 21
uint8_t* q_tilde = malloc(FAEST_128F_LAMBDA / 8);
zk_hash_128(q_tilde, chall_2, B_0, FAEST_128F_Ske + FAEST_128F_Senc);
free(B_0);
bf128_t bf_qtilde = bf128_load(q_tilde);
bf128_store(q_tilde, bf128_add(bf_qtilde, bf128_mul(bf128_load(a_tilde), bf128_load(delta))));
return q_tilde;
}
// lambda == 192 implementation
static void aes_key_schedule_forward_192(const bf192_t* v, bf192_t* bf_out) {
// Step: 1 sanity check (skipped)
memcpy(bf_out, v, FAEST_192F_LAMBDA * sizeof(bf192_t));
// Step: 4
unsigned int i_wd = FAEST_192F_LAMBDA;
// Step: 5..10
for (unsigned int j = FAEST_192F_Nwd; j < 4 * (FAEST_192F_R + 1); j++) {
if ((j % FAEST_192F_Nwd) == 0 || (FAEST_192F_Nwd > 6 && (j % FAEST_192F_Nwd) == 4)) {
memcpy(bf_out + j * 32, v + i_wd, sizeof(bf192_t) * 32);
i_wd += 32;
} else {
for (unsigned int i = 0; i < 32; i++) {
bf_out[(32 * j) + i] =
bf192_add(bf_out[32 * (j - FAEST_192F_Nwd) + i], bf_out[32 * (j - 1) + i]);
}
}
}
}
static void aes_key_schedule_backward_192(const bf192_t* v, const bf192_t* Vk, uint8_t Mtag,
uint8_t Mkey, const uint8_t* delta, bf192_t* bf_out) {
// Step: 1
assert(!((Mtag == 1 && Mkey == 1) || (Mkey == 1 && delta == NULL)));
const bf192_t bf_delta = delta ? bf192_load(delta) : bf192_zero();
unsigned int iwd = 0;
unsigned int c = 0;
unsigned int ircon = 0;
bf192_t bf_minus_mkey = bf192_from_bit(1 ^ Mkey);
uint8_t minus_mtag = 1 ^ Mtag;
bf192_t bf_mkey_times_delta = bf192_mul_bit(bf_delta, Mkey);
bf_mkey_times_delta = bf192_add(bf_mkey_times_delta, bf_minus_mkey);
for (unsigned int j = 0; j < FAEST_192F_Ske; j++) {
// Step 7
bf192_t bf_x_tilde[8];
for (unsigned int i = 0; i < 8; i++) {
bf_x_tilde[i] = bf192_add(v[8 * j + i], Vk[iwd + 8 * c + i]);
}
if (Mtag == 0 && c == 0) {
// Step 9
uint8_t r = Rcon[ircon];
ircon = ircon + 1;
bf192_t bf_r[8];
for (unsigned int i = 0; i < 8; i++) {
// Step 12
bf_r[i] = bf192_mul_bit(bf_mkey_times_delta, get_bit(r, i));
// Step 13
bf_x_tilde[i] = bf192_add(bf_x_tilde[i], bf_r[i]);
}
}
for (unsigned int i = 0; i < 8; ++i) {
bf_out[i + 8 * j] = bf192_add(bf192_add(bf_x_tilde[(i + 7) % 8], bf_x_tilde[(i + 5) % 8]),
bf_x_tilde[(i + 2) % 8]);
}
bf_out[0 + 8 * j] =
bf192_add(bf_out[0 + 8 * j], bf192_mul_bit(bf_mkey_times_delta, minus_mtag));
bf_out[2 + 8 * j] =
bf192_add(bf_out[2 + 8 * j], bf192_mul_bit(bf_mkey_times_delta, minus_mtag));
c = c + 1;
if (c == 4) {
c = 0;
iwd += 192;
}
}
}
static void aes_key_schedule_constraints_Mkey_0_192(const uint8_t* w, const bf192_t* v, bf192_t* A0,
bf192_t* A1, uint8_t* k, bf192_t* vk,
const faest_paramset_t* params) {
// for scan-build
assert(FAEST_192F_Ske == params->faest_param.Ske);
// Step: 2
aes_key_schedule_forward_1(w, k, params);
// Step: 3
aes_key_schedule_forward_192(v, vk);
// Step: 4
uint8_t w_dash[FAEST_192F_Ske];
aes_key_schedule_backward_1(w + FAEST_192F_LAMBDA / 8, k, w_dash, params);
// Step: 5
bf192_t v_w_dash[FAEST_192F_Ske * 8];
aes_key_schedule_backward_192(v + FAEST_192F_LAMBDA, vk, 1, 0, NULL, v_w_dash);
// Step: 6..8
unsigned int iwd = 32 * (FAEST_192F_Nwd - 1);
for (unsigned int j = 0; j < FAEST_192F_Ske / 4; j++) {
bf192_t bf_k_hat[4];
bf192_t bf_v_k_hat[4];
bf192_t bf_w_dash_hat[4];
bf192_t bf_v_w_dash_hat[4];
for (unsigned int r = 0; r <= 3; r++) {
// Step: 10..11
bf_k_hat[(r + 3) % 4] = bf192_byte_combine_bits(k[(iwd + 8 * r) / 8]);
bf_v_k_hat[(r + 3) % 4] = bf192_byte_combine(vk + (iwd + 8 * r));
bf_w_dash_hat[r] = bf192_byte_combine_bits(w_dash[(32 * j + 8 * r) / 8]);
bf_v_w_dash_hat[r] = bf192_byte_combine(v_w_dash + (32 * j + 8 * r));
}
// Step: 13..17
for (unsigned int r = 0; r <= 3; r++) {
A0[4 * j + r] = bf192_mul(bf_v_k_hat[r], bf_v_w_dash_hat[r]);
A1[4 * j + r] =
bf192_add(bf192_add(bf192_mul(bf192_add(bf_k_hat[r], bf_v_k_hat[r]),
bf192_add(bf_w_dash_hat[r], bf_v_w_dash_hat[r])),
bf192_from_bf8(bf8_one())),
A0[4 * j + r]);
}
iwd = iwd + 192;
}
}
static void aes_key_schedule_constraints_Mkey_1_192(const bf192_t* q, const uint8_t* delta,
bf192_t* B, bf192_t* qk) {
// Step: 19..20
aes_key_schedule_forward_192(q, qk);
bf192_t q_w_dash[FAEST_192F_Ske * 8];
aes_key_schedule_backward_192(&q[FAEST_192F_LAMBDA], qk, 0, 1, delta, q_w_dash);
const bf192_t bf_delta = bf192_load(delta);
// Step 23..24
unsigned int iwd = 32 * (FAEST_192F_Nwd - 1);
for (unsigned int j = 0; j < FAEST_192F_Ske / 4; j++) {
bf192_t bf_q_hat_k[4];
bf192_t bf_q_hat_w_dash[4];
for (unsigned int r = 0; r <= 3; r++) {
// Step: 25..26
bf_q_hat_k[(r + 3) % 4] = bf192_byte_combine(qk + ((iwd + 8 * r)));
bf_q_hat_w_dash[r] = bf192_byte_combine(q_w_dash + ((32 * j + 8 * r)));
}
// Step: 27
for (unsigned int r = 0; r <= 3; r++) {
bf192_t bf_tmp = bf192_mul(bf_q_hat_k[r], bf_q_hat_w_dash[r]);
B[4 * j + r] = bf192_add(bf_tmp, bf192_mul(bf_delta, bf_delta));
}
iwd = iwd + 192;
}
}
static void aes_enc_forward_192_1(const uint8_t* x, const uint8_t* xk, const uint8_t* in,
uint8_t Mtag, uint8_t Mkey, bf192_t* bf_y) {
// Step: 2
for (unsigned int i = 0; i < 16; i++) {
// Step: 3,4 (bit spliced)
const uint8_t xin = in[i] & -((1 ^ Mtag) & (1 ^ Mkey));
// Step: 5
bf_y[i] = bf192_add(bf192_byte_combine_bits(xin), bf192_byte_combine_bits(xk[i]));
}
const bf192_t bf_two = bf192_byte_combine_bits(2);
const bf192_t bf_three = bf192_byte_combine_bits(3);
for (unsigned int j = 1; j < FAEST_192F_R; j++) {
for (unsigned int c = 0; c <= 3; c++) {
const unsigned int ix = 128 * (j - 1) + 32 * c;
const unsigned int ik = 128 * j + 32 * c;
const unsigned int iy = 16 * j + 4 * c;
bf192_t bf_x_hat[4];
bf192_t bf_xk_hat[4];
for (unsigned int r = 0; r <= 3; r++) {
// Step: 12..13
bf_x_hat[r] = bf192_byte_combine_bits(x[(ix + 8 * r) / 8]);
bf_xk_hat[r] = bf192_byte_combine_bits(xk[(ik + 8 * r) / 8]);
}
// Step : 14
bf_y[iy + 0] = bf192_add(bf_xk_hat[0], bf192_mul(bf_x_hat[0], bf_two));
bf_y[iy + 0] = bf192_add(bf_y[iy + 0], bf192_mul(bf_x_hat[1], bf_three));
bf_y[iy + 0] = bf192_add(bf_y[iy + 0], bf_x_hat[2]);
bf_y[iy + 0] = bf192_add(bf_y[iy + 0], bf_x_hat[3]);
// Step: 15
bf_y[iy + 1] = bf192_add(bf_xk_hat[1], bf_x_hat[0]);
bf_y[iy + 1] = bf192_add(bf_y[iy + 1], bf192_mul(bf_x_hat[1], bf_two));
bf_y[iy + 1] = bf192_add(bf_y[iy + 1], bf192_mul(bf_x_hat[2], bf_three));
bf_y[iy + 1] = bf192_add(bf_y[iy + 1], bf_x_hat[3]);
// Step: 16
bf_y[iy + 2] = bf192_add(bf_xk_hat[2], bf_x_hat[0]);
bf_y[iy + 2] = bf192_add(bf_y[iy + 2], bf_x_hat[1]);
bf_y[iy + 2] = bf192_add(bf_y[iy + 2], bf192_mul(bf_x_hat[2], bf_two));
bf_y[iy + 2] = bf192_add(bf_y[iy + 2], bf192_mul(bf_x_hat[3], bf_three));
// Step: 17
bf_y[iy + 3] = bf192_add(bf_xk_hat[3], bf192_mul(bf_x_hat[0], bf_three));
bf_y[iy + 3] = bf192_add(bf_y[iy + 3], bf_x_hat[1]);
bf_y[iy + 3] = bf192_add(bf_y[iy + 3], bf_x_hat[2]);
bf_y[iy + 3] = bf192_add(bf_y[iy + 3], bf192_mul(bf_x_hat[3], bf_two));
}
}
return;
}
static void aes_enc_forward_192(const bf192_t* bf_x, const bf192_t* bf_xk, const uint8_t* in,
uint8_t Mtag, uint8_t Mkey, const uint8_t* delta, bf192_t* bf_y) {
const bf192_t bf_delta = delta ? bf192_load(delta) : bf192_zero();
const bf192_t bf_minus_mtag = bf192_from_bit(1 ^ Mtag);
const bf192_t bf_minus_mkey = bf192_from_bit(1 ^ Mkey);
// Step: 2..4
for (unsigned int i = 0; i < 16; i++) {
bf192_t bf_xin[8];
for (unsigned int j = 0; j < 8; j++) {
bf_xin[j] = bf192_mul(bf192_mul_bit(bf_minus_mtag, get_bit(in[i], j)),
bf192_add(bf192_mul_bit(bf_delta, Mkey), bf_minus_mkey));
}
// Step: 5
bf_y[i] = bf192_add(bf192_byte_combine(bf_xin), bf192_byte_combine(bf_xk + (8 * i)));
}
const bf192_t bf_two = bf192_byte_combine_bits(2);
const bf192_t bf_three = bf192_byte_combine_bits(3);
for (unsigned int j = 1; j < FAEST_192F_R; j++) {
for (unsigned int c = 0; c <= 3; c++) {
const unsigned int ix = 128 * (j - 1) + 32 * c;
const unsigned int ik = 128 * j + 32 * c;
const unsigned int iy = 16 * j + 4 * c;
bf192_t bf_x_hat[4];
bf192_t bf_xk_hat[4];
for (unsigned int r = 0; r <= 3; r++) {
// Step: 12..13
bf_x_hat[r] = bf192_byte_combine(bf_x + (ix + 8 * r));
bf_xk_hat[r] = bf192_byte_combine(bf_xk + (ik + 8 * r));
}
// Step : 14
bf_y[iy + 0] = bf192_add(bf_xk_hat[0], bf192_mul(bf_x_hat[0], bf_two));
bf_y[iy + 0] = bf192_add(bf_y[iy + 0], bf192_mul(bf_x_hat[1], bf_three));
bf_y[iy + 0] = bf192_add(bf_y[iy + 0], bf_x_hat[2]);
bf_y[iy + 0] = bf192_add(bf_y[iy + 0], bf_x_hat[3]);
// Step: 15
bf_y[iy + 1] = bf192_add(bf_xk_hat[1], bf_x_hat[0]);
bf_y[iy + 1] = bf192_add(bf_y[iy + 1], bf192_mul(bf_x_hat[1], bf_two));
bf_y[iy + 1] = bf192_add(bf_y[iy + 1], bf192_mul(bf_x_hat[2], bf_three));
bf_y[iy + 1] = bf192_add(bf_y[iy + 1], bf_x_hat[3]);
// Step: 16
bf_y[iy + 2] = bf192_add(bf_xk_hat[2], bf_x_hat[0]);
bf_y[iy + 2] = bf192_add(bf_y[iy + 2], bf_x_hat[1]);
bf_y[iy + 2] = bf192_add(bf_y[iy + 2], bf192_mul(bf_x_hat[2], bf_two));
bf_y[iy + 2] = bf192_add(bf_y[iy + 2], bf192_mul(bf_x_hat[3], bf_three));
// Step: 17
bf_y[iy + 3] = bf192_add(bf_xk_hat[3], bf192_mul(bf_x_hat[0], bf_three));
bf_y[iy + 3] = bf192_add(bf_y[iy + 3], bf_x_hat[1]);
bf_y[iy + 3] = bf192_add(bf_y[iy + 3], bf_x_hat[2]);
bf_y[iy + 3] = bf192_add(bf_y[iy + 3], bf192_mul(bf_x_hat[3], bf_two));
}
}
}
static void aes_enc_backward_192_1(const uint8_t* x, const uint8_t* xk, uint8_t Mtag, uint8_t Mkey,
const uint8_t* out, bf192_t* y_out) {
uint8_t xtilde;
// Step:2..4
for (unsigned int j = 0; j < FAEST_192F_R; j++) {
for (unsigned int c = 0; c <= 3; c++) {
for (unsigned int r = 0; r <= 3; r++) {
// Step: 5..6
unsigned int ird = (128 * j) + (32 * ((c - r + 4) % 4)) + (8 * r);
if (j < (FAEST_192F_R - 1)) {
// Step: 7
xtilde = x[ird / 8];
} else {
// Step: 9..11 (bit spliced)
uint8_t xout = out[(ird - 128 * (FAEST_192F_R - 1)) / 8] & -((1 ^ Mtag) & (1 ^ Mkey));
xtilde = xout ^ xk[(128 + ird) / 8];
}
// Step: 12..17 (bit spliced)
uint8_t ytilde = rotr8(xtilde, 7) ^ rotr8(xtilde, 5) ^ rotr8(xtilde, 2);
ytilde ^= set_bit((1 ^ Mtag) & (1 ^ Mkey), 0);
ytilde ^= set_bit((1 ^ Mtag) & (1 ^ Mkey), 2);
// Step: 18
y_out[16 * j + 4 * c + r] = bf192_byte_combine_bits(ytilde);
}
}
}
return;
}
static void aes_enc_backward_192(const bf192_t* bf_x, const bf192_t* bf_xk, uint8_t Mtag,
uint8_t Mkey, const uint8_t* delta, const uint8_t* out,
bf192_t* y_out) {
// Step: 1
const bf192_t bf_delta = delta ? bf192_load(delta) : bf192_zero();
const bf192_t factor =
bf192_mul_bit(bf192_add(bf192_mul_bit(bf_delta, Mkey), bf192_from_bit(1 ^ Mkey)), 1 ^ Mtag);
// Step: 2..4
for (unsigned int j = 0; j < FAEST_192F_R; j++) {
for (unsigned int c = 0; c <= 3; c++) {
for (unsigned int r = 0; r <= 3; r++) {
bf192_t bf_x_tilde[8];
// Step: 5
unsigned int ird = (128 * j) + (32 * ((c - r + 4) % 4)) + (8 * r);
// Step: 6
if (j < (FAEST_192F_R - 1)) {
// Step: 7