forked from sysprog21/rv32emu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.c
3617 lines (3260 loc) · 141 KB
/
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
/*
* Copyright © 2022 - polfosol
* µAES - A minimalist ANSI-C compatible code for the AES encryption and block
* cipher modes.
*/
/** If your desired alphabet contains non-ASCII characters, the CUSTOM_ALPHABET
* macro 'must be' set to a double-digit number, e.g 21. Please note that ANSI-C
* standard does not support such characters and the code loses its compliance
* in this case. In what follows, you will find some sample alphabets along with
* their corresponding macro definitions. It is straightforward to set another
* custom alphabet according to these samples.
*/
#define NON_ASCII_CHARACTER_SET (CUSTOM_ALPHABET >= 10)
#if NON_ASCII_CHARACTER_SET
#include <locale.h>
#include <wchar.h>
#define string_t wchar_t *
#else
#define string_t char * /* string pointer type */
#endif
#if CUSTOM_ALPHABET == 0
#define ALPHABET "0123456789"
#define RADIX 10 /* strlen (ALPHABET) */
#endif
/**----------------------------------------------------------------------------
binary strings
-----------------------------------------------------------------------------*/
#if CUSTOM_ALPHABET == 1
#define ALPHABET "01"
#define RADIX 2
#endif
/**----------------------------------------------------------------------------
lowercase english letters
-----------------------------------------------------------------------------*/
#if CUSTOM_ALPHABET == 2
#define ALPHABET "abcdefghijklmnopqrstuvwxyz"
#define RADIX 26
#endif
/**----------------------------------------------------------------------------
lowercase alphanumeric strings
-----------------------------------------------------------------------------*/
#if CUSTOM_ALPHABET == 3
#define ALPHABET "0123456789abcdefghijklmnopqrstuvwxyz"
#define RADIX 36
#endif
/**----------------------------------------------------------------------------
the English alphabet
-----------------------------------------------------------------------------*/
#if CUSTOM_ALPHABET == 4
#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define RADIX 52
#endif
/**----------------------------------------------------------------------------
base-64 encoded strings (RFC-4648), with no padding character
-----------------------------------------------------------------------------*/
#if CUSTOM_ALPHABET == 5
#define ALPHABET \
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
#define RADIX 64
#endif
/**----------------------------------------------------------------------------
base-85 encoded strings (RFC-1924)
-----------------------------------------------------------------------------*/
#if CUSTOM_ALPHABET == 6
#define ALPHABET \
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-" \
";<=>?@^_`{|}~"
#define RADIX 85
#endif
/**----------------------------------------------------------------------------
a character set with length 26, used by some test vectors
-----------------------------------------------------------------------------*/
#if CUSTOM_ALPHABET == 7
#define ALPHABET "0123456789abcdefghijklmnop"
#define RADIX 26
#endif
/**----------------------------------------------------------------------------
base-64 character set with different ordering, used by some test vectors
-----------------------------------------------------------------------------*/
#if CUSTOM_ALPHABET == 8
#define ALPHABET \
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
#define RADIX 64
#endif
/**----------------------------------------------------------------------------
Greek alphabet
-----------------------------------------------------------------------------*/
#if CUSTOM_ALPHABET == 10
#define ALPHABET L"ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρσςτυφχψω"
#define RADIX 49
#endif
/**----------------------------------------------------------------------------
Persian alphabet
-----------------------------------------------------------------------------*/
#if CUSTOM_ALPHABET == 11
#define ALPHABET L"ءئاآبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی"
#define RADIX 35
#endif
/*
It is mandatory to determine these constants for each alphabet. You can either
pre-calculate the logarithm value (with at least 10 significant digits) and
set it as a constant, or let it be calculated dynamically like this:
*/
#include <math.h>
#define LOGRDX (log(RADIX) / log(2)) /* log2( RADIX ) if std=C99 */
#if FF_X == 3
#define MAXLEN (2 * (int) (96.000001 / LOGRDX))
#endif
#define MINLEN ((int) (19.931568 / LOGRDX + 1))
/**----------------------------------------------------------------------------
You can use different AES algorithms by changing this macro. Default is AES-128
-----------------------------------------------------------------------------*/
#define AES___ 128 /* or 256 (or 192; not standardized in some modes) */
/**----------------------------------------------------------------------------
AES block-cipher modes of operation. The following modes can be enabled/disabled
by setting their corresponding macros to TRUE (1) or FALSE (0).
-----------------------------------------------------------------------------*/
#define BLOCKCIPHERS 1
#define AEAD_MODES 1 /* authenticated encryption with associated data. */
#if BLOCKCIPHERS
#define ECB 1 /* electronic code-book (NIST SP 800-38A) */
#define CBC 1 /* cipher block chaining (NIST SP 800-38A) */
#define CFB 1 /* cipher feedback (NIST SP 800-38A) */
#define OFB 1 /* output feedback (NIST SP 800-38A) */
#define CTR 1 /* counter-block (NIST SP 800-38A) */
#define XEX 1 /* xor-encrypt-xor (NIST SP 800-38E) */
#define KWA 1 /* key wrap with authentication (NIST SP 800-38F) */
#define FPE 1 /* format-preserving encryption (NIST SP 800-38G) */
#endif
#if AEAD_MODES
#define CMAC 1 /* message authentication code (NIST SP 800-38B) */
#if CTR
#define CCM 1 /* counter with CBC-MAC (RFC-3610/NIST SP 800-38C) */
#define GCM 1 /* Galois/counter mode with GMAC (NIST SP 800-38D) */
#define EAX 1 /* encrypt-authenticate-translate (ANSI C12.22) */
#define SIV 1 /* synthetic initialization vector (RFC-5297) */
#define GCM_SIV 1 /* nonce misuse-resistant AES-GCM (RFC-8452) */
#endif
#if XEX
#define OCB 1 /* offset codebook mode with PMAC (RFC-7253) */
#endif
#define POLY1305 1 /* poly1305-AES mac (https://cr.yp.to/mac.html) */
#endif
#if CBC
#define CTS 1 /* ciphertext stealing (CS3: unconditional swap) */
#endif
#if XEX
#define XTS 1 /* XEX tweaked-codebook with ciphertext stealing */
#endif
#if CTR
#define CTR_NA 1 /* pure counter mode, with no authentication */
#endif
#if EAX
#define EAXP 1 /* EAX-prime, as specified by IEEE Std 1703 */
#endif
#define WTF !(POLY1305 || CMAC || BLOCKCIPHERS)
#define M_RIJNDAEL WTF /* none of above; just rijndael API. dude.., why? */
/**----------------------------------------------------------------------------
Refer to the BOTTOM OF THIS DOCUMENT for some explanations about these macros:
-----------------------------------------------------------------------------*/
#if ECB || CBC || XEX || KWA || M_RIJNDAEL
#define DECRYPTION 1 /* only these modes need the decrypt part of AES */
#endif
#if ECB || (CBC && !CTS) || (XEX && !XTS)
#define AES_PADDING 0 /* standard values: (1) PKCS#7 (2) IEC7816-4 */
#endif
#if FPE
#define CUSTOM_ALPHABET \
0 /* if disabled, use default alphabet (digits 0..9) \
*/
#define FF_X 1 /* algorithm type: (1) for FF1, or (3) for FF3-1 */
#endif
#if CTR_NA
#define CTR_IV_LENGTH 12 /* for using the last 32 bits as counter */
#define CTR_STARTVALUE 1 /* recommended value according to the RFC-3686. */
#endif
#if CCM
#define CCM_NONCE_LEN 11 /* for 32-bit count (since one byte is reserved). */
#define CCM_TAG_LEN 16 /* must be an even number in the range of 4..16 */
#endif
#if GCM
#define GCM_NONCE_LEN 12 /* RECOMMENDED. but other values are supported. */
#endif
#if EAX && !EAXP
#define EAX_NONCE_LEN 16 /* no specified limit; can be arbitrarily large */
#endif
#if OCB
#define OCB_NONCE_LEN 12 /* RECOMMENDED. must be positive and less than 16. */
#define OCB_TAG_LEN 16 /* again, please see the bottom of this document! */
#endif
/**----------------------------------------------------------------------------
Since <stdint.h> is not a part of ANSI-C, we may need a 'trick' to use uint8_t
-----------------------------------------------------------------------------*/
#include <string.h>
#if __STDC_VERSION__ > 199900L || __cplusplus > 201100L || defined(_MSC_VER)
#include <stdint.h>
#else
#include <limits.h>
#if CHAR_BIT == 8
typedef unsigned char uint8_t;
#endif
#if INT_MAX > 100000L
typedef int int32_t;
#else
typedef long int32_t;
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**----------------------------------------------------------------------------
Encryption/decryption of a single block with Rijndael
-----------------------------------------------------------------------------*/
#if M_RIJNDAEL
void AES_Cipher(const uint8_t *key, /* encryption/decryption key */
const char mode, /* encrypt: 'E', decrypt: 'D' */
const uint8_t *x, /* input block byte array */
uint8_t *y); /* output block byte array */
#endif
/**----------------------------------------------------------------------------
Main functions for ECB-AES block ciphering
-----------------------------------------------------------------------------*/
#if ECB
void AES_ECB_encrypt(const uint8_t *key, /* encryption key */
const uint8_t *pntxt, /* plaintext buffer */
const size_t ptextLen, /* length of input plain text */
uint8_t *crtxt); /* cipher-text result */
char AES_ECB_decrypt(const uint8_t *key, /* decryption key */
const uint8_t *crtxt, /* cipher-text buffer */
const size_t crtxtLen, /* length of input cipher text */
uint8_t *pntxt); /* plaintext result */
#endif /* ECB */
/**----------------------------------------------------------------------------
Main functions for CBC-AES block ciphering
-----------------------------------------------------------------------------*/
#if CBC
char AES_CBC_encrypt(const uint8_t *key, /* encryption key */
const uint8_t *iVec, /* initialization vector */
const uint8_t *pntxt, /* plaintext buffer */
const size_t ptextLen, /* length of input plain text */
uint8_t *crtxt); /* cipher-text result */
char AES_CBC_decrypt(const uint8_t *key, /* decryption key */
const uint8_t *iVec, /* initialization vector */
const uint8_t *crtxt, /* cipher-text buffer */
const size_t crtxtLen, /* length of input cipher text */
uint8_t *pntxt); /* plaintext result */
#endif /* CBC */
/**----------------------------------------------------------------------------
Main functions for CFB-AES block ciphering
-----------------------------------------------------------------------------*/
#if CFB
void AES_CFB_encrypt(const uint8_t *key, /* encryption key */
const uint8_t *iVec, /* initialization vector */
const uint8_t *pntxt, /* plaintext buffer */
const size_t ptextLen, /* length of input plain text */
uint8_t *crtxt); /* cipher-text result */
void AES_CFB_decrypt(const uint8_t *key, /* decryption key */
const uint8_t *iVec, /* initialization vector */
const uint8_t *crtxt, /* cipher-text buffer */
const size_t crtxtLen, /* length of input cipher text */
uint8_t *pntxt); /* plaintext result */
#endif /* CFB */
/**----------------------------------------------------------------------------
Main functions for OFB-AES block ciphering
-----------------------------------------------------------------------------*/
#if OFB
void AES_OFB_encrypt(const uint8_t *key, /* encryption key */
const uint8_t *iVec, /* initialization vector */
const uint8_t *pntxt, /* plaintext buffer */
const size_t ptextLen, /* length of input plain text */
uint8_t *crtxt); /* cipher-text result */
void AES_OFB_decrypt(const uint8_t *key, /* decryption key */
const uint8_t *iVec, /* initialization vector */
const uint8_t *crtxt, /* cipher-text buffer */
const size_t crtxtLen, /* length of input cipher text */
uint8_t *pntxt); /* plaintext result */
#endif /* OFB */
/**----------------------------------------------------------------------------
Main functions for XTS-AES block ciphering
-----------------------------------------------------------------------------*/
#if XTS
char AES_XTS_encrypt(const uint8_t *keys, /* encryption key pair */
const uint8_t *unitId, /* tweak value (sector ID) */
const uint8_t *pntxt, /* plaintext buffer */
const size_t ptextLen, /* length of input plain text */
uint8_t *crtxt); /* cipher-text result */
char AES_XTS_decrypt(const uint8_t *keys, /* decryption key pair */
const uint8_t *unitId, /* tweak value (sector ID) */
const uint8_t *crtxt, /* cipher-text buffer */
const size_t crtxtLen, /* length of input cipher text */
uint8_t *pntxt); /* plaintext result */
#endif /* XTS */
/**----------------------------------------------------------------------------
Main functions for CTR-AES block ciphering
-----------------------------------------------------------------------------*/
#if CTR_NA
void AES_CTR_encrypt(const uint8_t *key, /* encryption key */
const uint8_t *iv, /* initialization vector/ nonce */
const uint8_t *pntxt, /* plaintext buffer */
const size_t ptextLen, /* length of input plain text */
uint8_t *crtxt); /* cipher-text result */
void AES_CTR_decrypt(const uint8_t *key, /* decryption key */
const uint8_t *iv, /* initialization vector/ nonce */
const uint8_t *crtxt, /* cipher-text buffer */
const size_t crtxtLen, /* length of input cipher text */
uint8_t *pntxt); /* plaintext result */
#endif /* CTR */
/**----------------------------------------------------------------------------
Main functions for SIV-AES block ciphering
-----------------------------------------------------------------------------*/
#if SIV
void AES_SIV_encrypt(const uint8_t *keys, /* encryption key pair */
const uint8_t *pntxt, /* plain text */
const size_t ptextLen, /* length of input plain text */
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
uint8_t *iv, /* synthesized initial-vector */
uint8_t *crtxt); /* cipher-text result */
char AES_SIV_decrypt(const uint8_t *keys, /* decryption key pair */
const uint8_t *iv, /* provided initial-vector */
const uint8_t *crtxt, /* cipher text */
const size_t crtxtLen, /* length of input cipher-text */
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
uint8_t *pntxt); /* plain-text result */
#endif /* SIV */
/**----------------------------------------------------------------------------
Main functions for GCM-AES block ciphering
-----------------------------------------------------------------------------*/
#if GCM
void AES_GCM_encrypt(const uint8_t *key, /* encryption key */
const uint8_t *nonce, /* a.k.a initialization vector */
const uint8_t *pntxt, /* plain text */
const size_t ptextLen, /* length of input plain text */
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
uint8_t *crtxt, /* cipher-text result */
uint8_t *auTag); /* message authentication tag */
char AES_GCM_decrypt(const uint8_t *key, /* decryption key */
const uint8_t *nonce, /* a.k.a initialization vector */
const uint8_t *crtxt, /* cipher text + appended tag */
const size_t crtxtLen, /* length of input cipher-text */
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
const uint8_t tagLen, /* size of tag (if any) */
uint8_t *pntxt); /* plain-text result */
#endif /* GCM */
/**----------------------------------------------------------------------------
Main functions for CCM-AES block ciphering
-----------------------------------------------------------------------------*/
#if CCM
void AES_CCM_encrypt(const uint8_t *key, /* encryption key */
const uint8_t *nonce, /* a.k.a initialization vector */
const uint8_t *pntxt, /* plain text */
const size_t ptextLen, /* length of input plain text */
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
uint8_t *crtxt, /* cipher-text result */
uint8_t *auTag); /* message authentication tag */
char AES_CCM_decrypt(const uint8_t *key, /* decryption key */
const uint8_t *nonce, /* a.k.a initialization vector */
const uint8_t *crtxt, /* cipher text + appended tag */
const size_t crtxtLen, /* length of input cipher-text */
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
const uint8_t tagLen, /* size of tag (if any) */
uint8_t *pntxt); /* plain-text result */
#endif /* CCM */
/**----------------------------------------------------------------------------
Main functions for OCB-AES block ciphering
-----------------------------------------------------------------------------*/
#if OCB
void AES_OCB_encrypt(const uint8_t *key, /* encryption key */
const uint8_t *nonce, /* a.k.a initialization vector */
const uint8_t *pntxt, /* plain text */
const size_t ptextLen, /* length of input plain text */
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
uint8_t *crtxt, /* cipher-text result */
uint8_t *auTag); /* message authentication tag */
char AES_OCB_decrypt(const uint8_t *key, /* decryption key */
const uint8_t *nonce, /* a.k.a initialization vector */
const uint8_t *crtxt, /* cipher text + appended tag */
const size_t crtxtLen, /* length of input cipher-text */
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
const uint8_t tagLen, /* size of tag (if any) */
uint8_t *pntxt); /* plain-text result */
#endif /* OCB */
/**----------------------------------------------------------------------------
Main functions for EAX-AES mode; more info at the bottom of this document.
-----------------------------------------------------------------------------*/
#if EAX
void AES_EAX_encrypt(const uint8_t *key, /* encryption key */
const uint8_t *nonce, /* arbitrary-size nonce array */
const uint8_t *pntxt, /* plain text */
const size_t ptextLen, /* length of input plain text */
#if EAXP
const size_t nonceLen, /* size of provided nonce */
uint8_t *crtxt); /* cipher-text result + mac (4) */
#else
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
uint8_t *crtxt, /* cipher-text result */
uint8_t *auTag); /* message authentication tag */
#endif
char AES_EAX_decrypt(const uint8_t *key, /* decryption key */
const uint8_t *nonce, /* arbitrary-size nonce array */
const uint8_t *crtxt, /* cipher text + appended tag */
const size_t crtxtLen, /* length of input cipher-text */
#if EAXP
const size_t nonceLen, /* size of provided nonce */
#else
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
const uint8_t tagLen, /* size of tag (if any) */
#endif
uint8_t *pntxt); /* plain-text result */
#endif /* EAX */
/**----------------------------------------------------------------------------
Main functions for GCM-SIV-AES block ciphering
-----------------------------------------------------------------------------*/
#if GCM_SIV
void GCM_SIV_encrypt(const uint8_t *key, /* encryption key */
const uint8_t *nonce, /* provided 96-bit nonce */
const uint8_t *pntxt, /* plain text */
const size_t ptextLen, /* length of input plain text */
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
uint8_t *crtxt, /* cipher-text result */
uint8_t *auTag); /* 16-bytes mandatory tag */
char GCM_SIV_decrypt(const uint8_t *key, /* decryption key */
const uint8_t *nonce, /* provided 96-bit nonce */
const uint8_t *crtxt, /* cipher text + appended tag */
const size_t crtxtLen, /* length of input cipher-text */
const uint8_t *aData, /* added authentication data */
const size_t aDataLen, /* size of authentication data */
const uint8_t tagLen, /* size of tag (must be 16) */
uint8_t *pntxt); /* plain-text result */
#endif /* GCM-SIV */
/**----------------------------------------------------------------------------
Main functions for AES key-wrapping
-----------------------------------------------------------------------------*/
#if KWA
char AES_KEY_wrap(const uint8_t *kek, /* key encryption key */
const uint8_t *secret, /* input secret to be wrapped */
const size_t secretLen, /* size of input */
uint8_t *wrapped); /* key-wrapped output */
char AES_KEY_unwrap(const uint8_t *kek, /* key encryption key */
const uint8_t *wrapped, /* key-wrapped secret */
const size_t wrapLen, /* size of input (secretLen +8) */
uint8_t *secret); /* buffer for unwrapped key */
#endif /* KWA */
/**----------------------------------------------------------------------------
Main functions for FPE-AES; more info at the bottom of this page.
-----------------------------------------------------------------------------*/
#if FPE
char AES_FPE_encrypt(const uint8_t *key, /* encryption key */
const uint8_t *tweak, /* tweak bytes */
#if FF_X != 3
const size_t tweakLen, /* size of tweak array */
#endif
const void *pntxt, /* input plaintext string */
const size_t ptextLen, /* length of plaintext string */
void *crtxt); /* cipher-text result */
char AES_FPE_decrypt(const uint8_t *key, /* decryption key */
const uint8_t *tweak, /* tweak bytes */
#if FF_X != 3
const size_t tweakLen, /* size of tweak array */
#endif
const void *crtxt, /* input ciphertext string */
const size_t crtxtLen, /* length of ciphertext string */
void *pntxt); /* plain-text result */
#endif /* FPE */
/**----------------------------------------------------------------------------
Main function for Poly1305-AES message authentication code
-----------------------------------------------------------------------------*/
#if POLY1305
void AES_Poly1305(const uint8_t *keys, /* encryption/mixing key pair */
const uint8_t *nonce, /* the 128-bit nonce */
const void *data, /* input data buffer */
const size_t dataSize, /* size of data in bytes */
uint8_t *mac); /* calculated poly1305-AES mac */
#endif
/**----------------------------------------------------------------------------
Main function for AES Cipher-based Message Authentication Code
-----------------------------------------------------------------------------*/
#if CMAC
void AES_CMAC(const uint8_t *key, /* encryption/cipher key */
const void *data, /* input data buffer */
const size_t dataSize, /* size of data in bytes */
uint8_t *mac); /* calculated CMAC hash */
#endif
#ifdef __cplusplus
}
#endif
/**----------------------------------------------------------------------------
The error codes and key length should be defined here for external references:
-----------------------------------------------------------------------------*/
#define ENCRYPTION_FAILURE 0x1E
#define DECRYPTION_FAILURE 0x1D
#define AUTHENTICATION_FAILURE 0x1A
#define ENDED_IN_SUCCESS 0x00
#if (AES___ == 256) || (AES___ == 192)
#define AES_KEY_LENGTH (AES___ / 8)
#else
#define AES_KEY_LENGTH 16
#endif
/**¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯**\
¦ Notes and remarks about the above-defined macros ¦
--------------------------------------------------------------------------------
* In EBC/CBC/XEX modes, the size of input must be a multiple of block-size.
Otherwise it needs to be padded. The simplest (default) padding mode is to
fill the rest of block by zeros. Supported standard padding methods are
PKCS#7 and ISO/IEC 7816-4, which can be enabled by the AES_PADDING macro.
* In many texts, you may see that the words 'nonce' and 'initialization vector'
are used interchangeably. But they have a subtle difference. Sometimes nonce
is a part of the I.V, which itself can either be a full block or a partial
one. In CBC/CFB/OFB modes, the provided I.V must be a full block. In pure
CTR mode (CTR_NA) you can either provide a 96-bit I.V and let the count
start at CTR_STARTVALUE, or use a full block IV.
* In AEAD modes, the size of nonce and tag might be a parameter of the algorithm
such that changing them affect the results. The GCM/EAX modes support
arbitrary sizes for nonce. In CCM, the nonce length may vary from 8 to 13
bytes. Also the tag size is an EVEN number between 4..16. In OCB, the nonce
size is 1..15 and the tag is 0..16 bytes. Note that the 'calculated' tag-
size is always 16 bytes which can later be truncated to desired values. So
in encryption functions, the provided authTag buffer must be 16 bytes long.
* For the EAX mode of operation, the IEEE-1703 standard defines EAX' which is a
modified version that combines AAD and nonce. Also the tag size is fixed to
4 bytes. So EAX-prime functions don't need to take additional authentication
data and tag-size as separate parameters.
* In SIV mode, multiple separate units of authentication headers can be provided
for the nonce synthesis. Here we assume that only one unit of AAD (aData) is
sufficient, which is practically true.
* The FPE mode has two distinct NIST-approved algorithms, namely FF1 and FF3-1.
Use the FF_X macro to change the encryption method, which is FF1 by default.
The input and output strings must be consisted of a fixed set of characters
called 'the alphabet'. The default alphabet is the set of digits {'0'..'9'}.
If you want to use a different alphabet, set the CUSTOM_ALPHABET macro and
refer to the "micro_fpe.h" header. This file is needed only when a custom
alphabet has to be defined, and contains some illustrative examples and
clear guidelines on how to do so.
* The key wrapping mode is also denoted by KW. In this mode, the input secret is
divided into 64bit blocks. Number of blocks is at least 2, and it is assumed
that no padding is required. For padding, the KWP mode must be used which is
easily implementable, but left as an exercise! In the NIST document you may
find some mentions of TKW which is for 3DES and irrelevant here. Anyway, the
wrapped output has an additional block, i.e. wrappedSize = secretSize + 8.
* Let me explain three extra options that are defined in the source file. If the
length of the input cipher/plain text is 'always' less than 4KB, you can
enable the SMALL_CIPHER macro to save a few bytes in the compiled code. Note
that for key-wrapping, this limit is 42 blocks (336 bytes) of secret key.
These assumptions are likely to be valid for some embedded systems and small
applications. Furthermore, enabling that other macro, REDUCE_CODE_SIZE had a
considerable effect on the size of the compiled code in my own tests.
Nonetheless, others might get a different result from them.
The INCREASE_SECURITY macro, as its name suggests, is dealing with security
considerations. For example, since the RoundKey is declared as static array
it might get exposed to some attacks. By enabling this macro, round-keys are
wiped out at the end of ciphering operations. However, please keep in mind
that this is NOT A GUARANTEE against side-channel attacks.
*/
/*----------------------------------------------------------------------------*\
Global constants, data types, and important / useful MACROs
\*----------------------------------------------------------------------------*/
#define KEYSIZE AES_KEY_LENGTH
#define BLOCKSIZE (128 / 8) /* Block length in AES is 'always' 128-bits. */
#define Nb (BLOCKSIZE / 4) /* The number of columns comprising a AES state. */
#define Nk (KEYSIZE / 4) /* The number of 32 bit words in a key. */
#define LAST \
(BLOCKSIZE - 1) /* The index at the end of block, or last index \
*/
#define ROUNDS (Nk + 6) /* The number of rounds in AES Cipher. */
#define IMPLEMENT(x) (x) > 0
#define INCREASE_SECURITY 0 /* refer to the bottom of the header file for */
#define SMALL_CIPHER 0 /* ... some explanations and the rationale of */
#define REDUCE_CODE_SIZE 1 /* ... these three macros */
/** state_t represents rijndael state matrix. sincefixed-size memory blocks have
* an essential role in all algorithms, they are indicated by a specific type */
typedef uint8_t state_t[Nb][4];
typedef uint8_t block_t[BLOCKSIZE];
/** these types are function pointers, whose arguments are fixed-size blocks: */
typedef void (*fdouble_t)(block_t);
typedef void (*fmix_t)(const block_t, block_t);
#if SMALL_CIPHER
typedef unsigned char count_t;
#else
typedef size_t count_t;
#endif
/*----------------------------------------------------------------------------*\
Private variables:
\*----------------------------------------------------------------------------*/
/** The array that stores the round keys during AES key-expansion process ... */
static uint8_t RoundKey[BLOCKSIZE * ROUNDS + KEYSIZE];
/** Lookup-tables are static constant, so that they can be placed in read-only
* storage instead of RAM. They can be computed dynamically trading ROM for RAM.
* This may be useful in (embedded) bootloader applications, where ROM is often
* limited. Please refer to: https://en.wikipedia.org/wiki/Rijndael_S-box */
static const uint8_t sbox[256] = {
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26,
0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed,
0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec,
0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11,
0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
0xb0, 0x54, 0xbb, 0x16};
#if DECRYPTION
static const uint8_t rsbox[256] = {
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e,
0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32,
0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49,
0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50,
0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05,
0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41,
0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8,
0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b,
0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59,
0x27, 0x80, 0xec, 0x5f, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d,
0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63,
0x55, 0x21, 0x0c, 0x7d};
#endif
/*----------------------------------------------------------------------------*\
Auxiliary functions for the Rijndael algorithm
\*----------------------------------------------------------------------------*/
#define getSBoxValue(num) (sbox[(num)])
#define getSBoxInvert(num) (rsbox[num])
#if REDUCE_CODE_SIZE
/** this function carries out XOR operation on two 128-bit blocks ........... */
static void xorBlock(const block_t src, block_t dest)
{
uint8_t i;
for (i = 0; i < BLOCKSIZE; ++i)
dest[i] ^= src[i];
}
/** doubling in GF(2^8): left-shift and if carry bit is set, xor it with 0x1b */
static uint8_t xtime(uint8_t x)
{
return (x > 0x7F) * 0x1b ^ (x << 1);
}
#if DECRYPTION
/** This function multiplies two numbers in the Galois field GF(2^8) ........ */
static uint8_t mulGF8(uint8_t x, uint8_t y)
{
uint8_t m;
for (m = 0; x > 1; x >>= 1) /* optimized algorithm for nonzero x */
{
m ^= (x & 1) * y;
y = xtime(y);
}
return m ^ y; /* or use (9 11 13 14) lookup tables */
}
#endif
#else
#define xtime(y) (y & 0x80 ? (y) << 1 ^ 0x1b : (y) << 1)
#define mulGF8(x, y) \
(((x & 1) * y) ^ ((x >> 1 & 1) * xtime(y)) ^ \
((x >> 2 & 1) * xtime(xtime(y))) ^ \
((x >> 3 & 1) * xtime(xtime(xtime(y)))))
static void xorBlock(const block_t src, block_t dest)
{
long long *d = (void *) dest; /* not supported in ANSI-C / ISO-C90 */
long long const *s = (const void *) src;
d[0] ^= s[0];
d[1] ^= s[1];
}
#endif
/*----------------------------------------------------------------------------*\
Main functions for the Rijndael encryption algorithm
\*----------------------------------------------------------------------------*/
/** This function produces (ROUNDS+1) round keys, which are used in each round
* to encrypt/decrypt the intermediate states. First round key is the main key
* itself, and other rounds are constructed from the previous ones as follows */
static void KeyExpansion(const uint8_t *key)
{
uint8_t rcon = 1, i;
memcpy(RoundKey, key, KEYSIZE);
for (i = KEYSIZE; i < (ROUNDS + 1) * BLOCKSIZE; ++i) {
switch (i % KEYSIZE) {
case 0:
memcpy(&RoundKey[i], &RoundKey[i - KEYSIZE], KEYSIZE);
#if Nk == 4
if (rcon == 0)
rcon = 0x1b; /* RCON may reach 0 only in AES-128. */
#endif
RoundKey[i] ^= getSBoxValue(RoundKey[i - 3]) ^ rcon;
rcon <<= 1;
break;
case 1:
case 2:
RoundKey[i] ^= getSBoxValue(RoundKey[i - 3]);
break;
case 3:
RoundKey[i] ^= getSBoxValue(RoundKey[i - 7]);
break;
#if Nk == 8 /* additional round only for AES-256 */
case 16: /* 0 <= i % KEYSIZE - BLOCKSIZE < 4 */
case 17:
case 18:
case 19:
RoundKey[i] ^= getSBoxValue(RoundKey[i - 4]);
break;
#endif
default:
RoundKey[i] ^= RoundKey[i - 4];
break;
}
}
}
/** This function adds the round key to the state matrix via an XOR function. */
static void AddRoundKey(const uint8_t round, block_t state)
{
xorBlock(RoundKey + BLOCKSIZE * round, state);
}
/** Substitute values in the state matrix with associated values in the S-box */
static void SubBytes(block_t state)
{
uint8_t i;
for (i = 0; i < BLOCKSIZE; ++i) {
state[i] = getSBoxValue(state[i]);
}
}
/** Shift/rotate the rows of the state matrix to the left. Each row is shifted
* with a different offset (= Row number). So the first row is not shifted .. */
static void ShiftRows(state_t *state)
{
uint8_t temp = (*state)[0][1];
(*state)[0][1] = (*state)[1][1];
(*state)[1][1] = (*state)[2][1];
(*state)[2][1] = (*state)[3][1];
(*state)[3][1] = temp; /* Rotated the 1st row 1 columns to left */
temp = (*state)[0][2];
(*state)[0][2] = (*state)[2][2];
(*state)[2][2] = temp;
temp = (*state)[1][2];
(*state)[1][2] = (*state)[3][2];
(*state)[3][2] = temp; /* Rotated the 2nd row 2 columns to left */
temp = (*state)[0][3];
(*state)[0][3] = (*state)[3][3];
(*state)[3][3] = (*state)[2][3];
(*state)[2][3] = (*state)[1][3];
(*state)[1][3] = temp; /* Rotated the 3rd row 3 columns to left */
}
/** This function mixes the columns of the state matrix in a rotational way.. */
static void MixColumns(state_t *state)
{
uint8_t a, b, c, d, i;
for (i = 0; i < Nb; ++i) {
a = (*state)[i][0] ^ (*state)[i][1];
b = (*state)[i][1] ^ (*state)[i][2];
c = (*state)[i][2] ^ (*state)[i][3];
d = a ^ c; /* d is XOR of all the elements in a row */
(*state)[i][0] ^= d ^ xtime(a);
(*state)[i][1] ^= d ^ xtime(b);
b ^= d; /* -> b = (*state)[i][3] ^ (*state)[i][0] */
(*state)[i][2] ^= d ^ xtime(c);
(*state)[i][3] ^= d ^ xtime(b);
}
}
/** Encrypts a plain-text input block, into a cipher text block as output ... */
static void rijndaelEncrypt(const block_t input, block_t output)
{
uint8_t round = ROUNDS;
/* copy the input to the state matrix, and beware of undefined behavior.. */
if (input != output)
memcpy(output, input, BLOCKSIZE);
AddRoundKey(0, output); /* Add the first round key to the state */
/* The encryption is carried out in #ROUNDS iterations, of which the first
* #ROUNDS-1 are identical. The last round doesn't involve mixing columns */
while (round) {
SubBytes(output);
ShiftRows((state_t *) output);
if (--round)
MixColumns((state_t *) output);
AddRoundKey(ROUNDS - round, output);
}
}
/*----------------------------------------------------------------------------*\
Block-decryption part of the Rijndael algorithm
\*----------------------------------------------------------------------------*/
#if IMPLEMENT(DECRYPTION)
/** Substitutes the values in the state matrix with values of inverted S-box. */
static void InvSubBytes(block_t state)
{
uint8_t i;
for (i = 0; i < BLOCKSIZE; ++i) {
state[i] = getSBoxInvert(state[i]);
}
}
/** This function shifts/rotates the rows of the state matrix to right ...... */
static void InvShiftRows(state_t *state)
{
uint8_t temp = (*state)[3][1];
(*state)[3][1] = (*state)[2][1];
(*state)[2][1] = (*state)[1][1];
(*state)[1][1] = (*state)[0][1];
(*state)[0][1] = temp; /* Rotated first row 1 columns to right */
temp = (*state)[0][2];
(*state)[0][2] = (*state)[2][2];
(*state)[2][2] = temp;
temp = (*state)[1][2];
(*state)[1][2] = (*state)[3][2];
(*state)[3][2] = temp; /* Rotated second row 2 columns to right */
temp = (*state)[0][3];
(*state)[0][3] = (*state)[1][3];
(*state)[1][3] = (*state)[2][3];
(*state)[2][3] = (*state)[3][3];
(*state)[3][3] = temp; /* Rotated third row 3 columns to right */
}
/** Mixes the columns of (already-mixed) state matrix to reverse the process. */
static void InvMixColumns(state_t *state)
{
uint8_t a, b, c, d, i;
for (i = 0; i < Nb; ++i) { /* see: crypto.stackexchange.com/q/48872 */
a = (*state)[i][0];
b = (*state)[i][1];
c = (*state)[i][2];
d = (*state)[i][3];
(*state)[i][0] =
mulGF8(14, a) ^ mulGF8(11, b) ^ mulGF8(13, c) ^ mulGF8(9, d);
(*state)[i][1] =
mulGF8(14, b) ^ mulGF8(11, c) ^ mulGF8(13, d) ^ mulGF8(9, a);
(*state)[i][2] =
mulGF8(14, c) ^ mulGF8(11, d) ^ mulGF8(13, a) ^ mulGF8(9, b);
(*state)[i][3] =
mulGF8(14, d) ^ mulGF8(11, a) ^ mulGF8(13, b) ^ mulGF8(9, c);
}
}
/** Decrypts a cipher-text input block, into a 128-bit plain text as output.. */
static void rijndaelDecrypt(const block_t input, block_t output)
{
uint8_t round = ROUNDS;
/* copy the input into state matrix, i.e. state is initialized by input.. */
if (input != output)
memcpy(output, input, BLOCKSIZE);
AddRoundKey(ROUNDS, output); /* First, add the last round key to state */
/* The decryption completes after #ROUNDS iterations, of which the first
* #ROUNDS-1 are identical. The last round doesn't involve mixing columns */
while (round) {
InvShiftRows((state_t *) output);
InvSubBytes(output);
AddRoundKey(--round, output);
if (round)
InvMixColumns((state_t *) output);
}