-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathascon.h
2131 lines (2046 loc) · 91.9 KB
/
ascon.h
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
/**
* @file
* Ascon cipher - Lightweight Authenticated Encryption & Hashing,
* also with Init-Update-Final paradigm.
*
* Ascon is a family of authenticated encryption and hashing algorithms
* designed to be lightweight and easy to implement, even with added
* countermeasures against side-channel attacks.
*
* For more information on the Ascon cipher itself, visit
* https://ascon.iaik.tugraz.at/
*
* This file is the interface to the Ascon library providing:
* - the Ascon symmetric AEAD ciphers Ascon128, Ascon128a, Ascon80pq
* - the Ascon fixed-size output hash and variable-size output hash (XOF)
*
* All functionalities are available in:
* - online form (init-update-final paradigm): the data is processed one
* chunk at the time; useful if it's still being received or does not
* fit into memory
* - offline form: the data is available as a whole in memory and processed
* in one go
*
* Library dependencies:
* - only the C99 or C11 standard library, as seen in the `#include` statements
* of this file
*
* @license Creative Commons Zero (CC0) 1.0
* @authors See AUTHORS.md file
*/
#ifndef ASCON_H
#define ASCON_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h> /* For uint8_t, uint_fast8_t, uint64_t */
#include <stddef.h> /* For size_t, NULL */
#include <stdbool.h> /* For bool, true, false */
#if defined(ASCON_INPUT_ASSERTS) && !defined(ASCON_ASSERT)
/**
* @def ASCON_INPUT_ASSERTS
* When defined, enables the runtime assertions on the parameters of all
* functions of the library API using #ASCON_ASSERT.
*
* The check is mostly against NULL pointers, for the correct order of calling
* of the many Init-Update-Final functions and against mixing functions from
* different AEAD algorithms (128 vs 128a vs 80pq). It's recommended to
* use it in debug mode, optionally also in release mode.
*
* If #ASCON_INPUT_ASSERTS is defined, the user can also pre-define
* #ASCON_ASSERT to any custom assertion macro or function.
*
* @see #ASCON_ASSERT
*/
// Redefining ASCON_INPUT_ASSERTS otherwise Doxygen does not find it
#undef ASCON_INPUT_ASSERTS
#define ASCON_INPUT_ASSERTS 1
/**
* @def ASCON_ASSERT
* Assertion macro, used to stop execution when a critical error is
* encountered.
*
* - Equal to `assert` from `assert.h` if #ASCON_INPUT_ASSERTS is defined.
* - Can also be pre-defined by the user to any custom assertion.
* - Otherwise does nothing.
*/
#include <assert.h> /* For assert() */
#define ASCON_ASSERT(expr) assert(expr)
#elif !defined(ASCON_ASSERT)
// Neither ASCON_INPUT_ASSERTS nor ASCON_ASSERT are defined,
// so make the assert macro do nothing.
#define ASCON_ASSERT(expr)
#endif
/**
* @def ASCON_API
* Marker of all the library's public API functions. Used to add exporting
* indicators for DLL on Windows, empty on other platforms.
*/
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(__NT__)
/**
* @def ASCON_WINDOWS
* Indicator simplifying the check for the Windows platform (undefined on other platforms).
* Used for internal decisions on how to inline functions.
*/
#define ASCON_WINDOWS 1
#define ASCON_API __declspec(dllexport)
#else
#define ASCON_API
#endif
/** Major version of this API conforming to semantic versioning. */
#define ASCON_API_VERSION_MAJOR 1
/** Minor version of this API conforming to semantic versioning. */
#define ASCON_API_VERSION_MINOR 2
/** Bugfix/patch version of this API conforming to semantic versioning. */
#define ASCON_API_VERSION_BUGFIX 1
/** Version of this API conforming to semantic versioning as a string. */
#define ASCON_API_VERSION "1.2.1"
/**
* Length in bytes of the secret symmetric key used for the Ascon128 cipher.
*/
#define ASCON_AEAD128_KEY_LEN 16U
/**
* Length in bytes of the secret symmetric key used for the Ascon128a cipher.
*/
#define ASCON_AEAD128a_KEY_LEN 16U
/**
* Length in bytes of the secret symmetric key used for the Ascon80pq cipher.
*/
#define ASCON_AEAD80pq_KEY_LEN 20U
/**
* Length in bytes of the public nonce used for authenticated
* encryption and decryption.
*/
#define ASCON_AEAD_NONCE_LEN 16U
/**
* Minimum recommended length in bytes of the authentication tag generated by
* the authenticated encryption and validated by the decryption.
*/
#define ASCON_AEAD_TAG_MIN_SECURE_LEN 16U
/**
* Length in bytes of the digest generated by the fixed-size (non-xof) hash
* function.
*/
#define ASCON_HASH_DIGEST_LEN 32U
/**
* Length in bytes of the digest generated by the fixed-size (non-xof) hasha
* function.
*/
#define ASCON_HASHA_DIGEST_LEN ASCON_HASH_DIGEST_LEN
/**
* Number of bytes the cipher can process at the time in AEAD128 mode and
* hashing.
*/
#define ASCON_RATE 8U
/**
* Number of bytes the cipher can process at the time in AEAD128a mode.
*
* The AEAD128a cipher absorbs blocks twice the standard rate size.
*/
#define ASCON_DOUBLE_RATE (2U * ASCON_RATE)
/**
* The tag is valid thus the associated data and ciphertext were intact.
*/
#define ASCON_TAG_OK true
/**
* The tag is invalid thus the associated data and decrypted data should be
* ignored.
*/
#define ASCON_TAG_INVALID false
/**
* Internal cipher sponge state (320 bits).
*/
typedef struct ascon_sponge
{
/** Sponge's first field */
uint64_t x0;
/** Sponge's second field */
uint64_t x1;
/** Sponge's third field */
uint64_t x2;
/** Sponge's fourth field */
uint64_t x3;
/** Sponge's fifth field */
uint64_t x4;
} ascon_sponge_t;
/**
* Internal cipher sponge state associated with a buffer holding for
* less-than-rate updates. Used for the Init-Update-Final implementation.
*/
typedef struct ascon_bufstate
{
/** Cipher sponge state. */
ascon_sponge_t sponge;
/** Buffer caching the less-than-rate long input between update calls. */
uint8_t buffer[ASCON_DOUBLE_RATE];
/** Currently used bytes of the buffer. */
uint8_t buffer_len;
/**
* State of the order of Init-Update-Final function calls, checked to
* know when to finalise the associated data processing and for the
* runtime assertions on the correct order of the functions.
*
* @see #ASCON_INPUT_ASSERTS
*
* Note: this variable is not semantically relevant in THIS struct,
* as it should belong in the struct ascon_aead_ctx_t, but by having it
* here we spare bytes of padding (7 on 64-bit systems, 3 on 32-bit,
* 1 on 16-bit) at the end of the struct ascon_aead_ctx_t, by using some
* of the padding space this struct anyway has.
*/
uint8_t flow_state;
/** Unused padding to the next uint64_t (sponge.x0 or ctx.k0)
* to avoid errors when compiling with `-Wpadding` on any platform. */
uint8_t pad[6];
} ascon_bufstate_t;
/** Cipher context for hashing. */
typedef ascon_bufstate_t ascon_hash_ctx_t;
/**
* Cipher context for authenticated encryption and validated decryption.
*
* Half of this context's size is the cipher's sponge state, the remaining
* part is holding the key and the buffering of online data (and some padding).
*/
typedef struct ascon_aead_ctx
{
/** Cipher buffered sponge state. */
ascon_bufstate_t bufstate;
/** Copy of the secret key, to be used in the final step, first part. */
uint64_t k0;
/** Copy of the secret key, to be used in the final step, second part. */
uint64_t k1;
/** Copy of the secret key, to be used in the final step, third part,
* used only in the Ascon80pq cipher. */
uint64_t k2;
} ascon_aead_ctx_t;
/**
* Offline symmetric encryption using Ascon128.
*
* Encrypts the data which is already available as a whole in a contiguous
* buffer, authenticating any optional associated data in the process.
* Provides the ciphertext and the authentication tag as output.
*
* In case of no associated data at all to be authenticated, set
* \p assoc_data_len to 0. Iff that is the case, \p assoc_data can
* be set to NULL.
*
* @image html encrypt.png
*
* @warning
* The nonce **must be unique**, as the strength of the AEAD is based on
* its uniqueness.
*
* @warning
* Using the AEAD to just authenticate any associated data with no
* plaintext to be encrypted is not recommended as the AEAD algorithm is not
* designed for that. Instead, use the Ascon hashing or xof functions in the form
* `Hash(key || msg)`.
*
* @param[out] ciphertext encrypted data with the same length as the
* plaintext, thus \p plaintext_len will be written in this buffer.
* This pointer may also point to the same location as \p plaintext
* to encrypt the plaintext in-place, sparing on memory instead
* of writing into a separate output buffer. Not NULL.
* @param[out] tag Message Authentication Code (MAC, a.k.a. cryptographic tag,
* fingerprint), used to validate the integrity and authenticity of the
* associated data and ciphertext. Has \p tag_len bytes. Not NULL.
* @param[in] key secret key of #ASCON_AEAD128_KEY_LEN. Not NULL.
* @param[in] nonce public **unique** nonce of #ASCON_AEAD_NONCE_LEN bytes.
* @param[in] assoc_data data to be authenticated with the same tag
* but not encrypted. Can be NULL iff \p assoc_data_len is 0.
* @param[in] plaintext data to be encrypted into \p ciphertext.
* @param[in] assoc_data_len length of the data pointed by \p assoc_data in
* bytes. Can be 0.
* @param[in] plaintext_len length of the data pointed by \p plaintext in
* bytes. Can be 0 (not recommended, see warning).
* @param[in] tag_len length of the tag to generate in bytes. At least
* #ASCON_AEAD_TAG_MIN_SECURE_LEN is recommended for security.
*/
ASCON_API void
ascon_aead128_encrypt(uint8_t* ciphertext,
uint8_t* tag,
const uint8_t key[ASCON_AEAD128_KEY_LEN],
const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
const uint8_t* assoc_data,
const uint8_t* plaintext,
size_t assoc_data_len,
size_t plaintext_len,
size_t tag_len);
/**
* Online symmetric encryption/decryption using Ascon128, initialisation.
*
* Prepares to start a new encryption or decryption session for plaintext or
* ciphertext and associated data being provided one chunk at the time.
*
* The key and nonce are copied/absorbed into the internal state, so they can
* be deleted from their original location after this function returns.
*
* The calling order for encryption/decryption is:
* 1. ascon_aead128_init() - once only
* 2. ascon_aead128_assoc_data_update() - 0 or more times
* 3. ascon_aead128_encrypt_update() / ascon_aead128_decrypt_update() - 0 or
* more times, see warning
* 4. ascon_aead128_encrypt_final() / ascon_aead128_encrypt_final() - once only
*
* @image html encrypt.png
* @image html decrypt.png
*
* @warning
* Using the AEAD to just authenticate any associated data with no
* plaintext to be encrypted is not recommended as the AEAD algorithm is not
* designed for that. Instead, use the Ascon hashing or xof functions in the form
* `Hash(key || msg)`.
*
* @warning
* A copy of the secret key is kept in the \p ctx struct and securely erased
* during the ascon_aead128_encrypt_final() or ascon_aead128_decrypt_final()
* call. In case the encryption or decryption session is interrupted and never
* finalised, clear the context with ascon_aead_cleanup() to erase the key copy.
*
* @warning
* Do not mix Init-Update-Final functions across ciphers.
*
* @param[in, out] ctx the encryption/decryption context, handling the cipher
* state and buffering of incoming data to be processed. Not NULL.
* @param[in] key secret key of #ASCON_AEAD128_KEY_LEN bytes. Not NULL
* @param[in] nonce public unique nonce of #ASCON_AEAD_NONCE_LEN bytes. Not
* NULL.
*/
ASCON_API void
ascon_aead128_init(ascon_aead_ctx_t* ctx,
const uint8_t key[ASCON_AEAD128_KEY_LEN],
const uint8_t nonce[ASCON_AEAD_NONCE_LEN]);
/**
* Online symmetric encryption/decryption using Ascon128, feeding associated
* data.
*
* Feeds a chunk of associated data to the already initialised encryption
* or decryption session. The data will be authenticated by the tag provided by
* the final function, but not encrypted or decrypted.
*
* In case of no associated data at all to be authenticated/validated, this
* function can either be skipped completely or called (also many times)
* with \p assoc_data_len set to 0. Iff that is the case, \p assoc_data can
* be set to NULL.
*
* After calling ascon_aead128_encrypt_update() or
* ascon_aead128_decrypt_update(), this function **must** not be used anymore
* on the same context.
*
* The calling order for encryption/decryption is:
* 1. ascon_aead128_init() - once only
* 2. ascon_aead128_assoc_data_update() - 0 or more times
* 3. ascon_aead128_encrypt_update() / ascon_aead128_decrypt_update() - 0 or
* more times, see warning
* 4. ascon_aead128_encrypt_final() / ascon_aead128_encrypt_final() - once only
*
* @warning
* Using the AEAD to just authenticate any associated data with no
* plaintext to be encrypted is not recommended as the AEAD algorithm is not
* designed for that. Instead, use the Ascon hashing or xof functions in the form
* `Hash(key || msg)`.
*
* @param[in, out] ctx the encryption/decryption context, handling the cipher
* state and buffering of incoming data to be processed. Not NULL.
* @param[in] assoc_data data to be authenticated/validated with the same tag
* but not encrypted/decrypted. May be NULL iff \p assoc_data_len is 0.
* @param[in] assoc_data_len length of the data pointed by \p assoc_data in
* bytes. May be 0.
*/
ASCON_API void
ascon_aead128_assoc_data_update(ascon_aead_ctx_t* ctx,
const uint8_t* assoc_data,
size_t assoc_data_len);
/**
* Online symmetric encryption using Ascon128, feeding plaintext and getting
* ciphertext.
*
* Feeds a chunk of plaintext data to the encryption session after any
* optional associated data has been processed. The plaintext will be encrypted
* and provided as ciphertext in buffered chunks of #ASCON_RATE bytes.
*
* It will automatically finalise the absorption of any associated data,
* so no new associated data could be processed after this function is called.
*
* The calling order for encryption is:
* 1. ascon_aead128_init() - once only
* 2. ascon_aead128_assoc_data_update() - 0 or more times
* 3. ascon_aead128_encrypt_update() - 0 or more times, see warning
* 4. ascon_aead128_encrypt_final() - once only
*
* @warning
* Using the AEAD to just authenticate any associated data with no
* plaintext to be encrypted is not recommended as the AEAD algorithm is not
* designed for that. Instead, use the Ascon hashing or xof functions in the form
* `Hash(key || msg)`.
*
* @param[in, out] ctx the encryption context, handling the cipher
* state and buffering of incoming data to be processed. Not NULL.
* @param[out] ciphertext encrypted data, buffered into chunks.
* This function will write a multiple of #ASCON_RATE bytes in the
* interval [0, \p plaintext_len + #ASCON_RATE[ into \p ciphertext.
* The exact number of written bytes is indicated by the return value.
* This pointer may also point to the same location as \p plaintext
* to encrypt the plaintext in-place, sparing on memory instead
* of writing into a separate output buffer. Not NULL.
* @param[in] plaintext data to be encrypted into \p ciphertext. All of the
* plaintext will be processed, even if the function provides less than
* \p plaintext_len output bytes. They are just buffered. Not NULL.
* @param[in] plaintext_len length of the data pointed by \p plaintext in
* bytes. May be 0.
* @returns number of bytes written into \p ciphertext. The value is a multiple
* of #ASCON_RATE in [0, \p plaintext_len + #ASCON_RATE[.
*/
ASCON_API size_t
ascon_aead128_encrypt_update(ascon_aead_ctx_t* ctx,
uint8_t* ciphertext,
const uint8_t* plaintext,
size_t plaintext_len);
/**
* Online symmetric encryption using Ascon128, finalisation and tag generation.
*
* Finalises the authenticated encryption by returning any remaining buffered
* ciphertext and the authentication tag. The total ciphertext length is
* equal to the total plaintext length.
*
* It will securely erase the content of the \p ctx struct before returning.
*
* The calling order for encryption is:
* 1. ascon_aead128_init() - once only
* 2. ascon_aead128_assoc_data_update() - 0 or more times
* 3. ascon_aead128_encrypt_update() - 0 or more times, see warning
* 4. ascon_aead128_encrypt_final() - once only
*
* @warning
* Using the AEAD to just authenticate any associated data with no
* plaintext to be encrypted is not recommended as the AEAD algorithm is not
* designed for that. Instead, use the Ascon hashing or xof functions in the form
* `Hash(key || msg)`.
*
* @warning
* A copy of the secret key is kept in the \p ctx struct and securely erased
* during this function call. In case the encryption session is interrupted
* and never finalised (this function is never called), clear the context with
* ascon_aead_cleanup() to erase the key copy.
*
* @param[in, out] ctx the encryption context, handling the cipher
* state and buffering of incoming data to be processed. It will be erased
* securely before this function returns. Not NULL.
* @param[out] ciphertext trailing encrypted data still available in the
* buffer of the buffered updating. This function will write
* [0, #ASCON_RATE[ bytes into \p ciphertext.
* The exact number of written bytes is indicated by the return value.
* Not NULL.
* @param[out] tag Message Authentication Code (MAC, a.k.a. cryptographic tag,
* fingerprint), used to validate the integrity and authenticity of the
* associated data and ciphertext. Has \p tag_len bytes. Not NULL.
* @param[in] tag_len length of the tag to generate in bytes. At least
* #ASCON_AEAD_TAG_MIN_SECURE_LEN is recommended for security.
* @returns number of bytes written into \p ciphertext. The value is in the
* interval [0, #ASCON_RATE[, i.e. whatever remained in the buffer
* after the last update call.
*/
ASCON_API size_t
ascon_aead128_encrypt_final(ascon_aead_ctx_t* ctx,
uint8_t* ciphertext,
uint8_t* tag,
size_t tag_len);
/**
* Offline symmetric decryption using Ascon128.
*
* Decrypts the data which is already available as a whole in a contiguous
* buffer, validating any optional associated data in the process.
* Provides the plaintext and the validity of the authentication tag as output.
* The total plaintext length is equal to the total ciphertext length.
*
* In case of no associated data at all to be authenticated, set
* \p assoc_data_len to 0. Iff that is the case, \p assoc_data can
* be set to NULL.
*
* @image html decrypt.png
*
* @param[out] plaintext decrypted data with the same length as the
* ciphertext, thus \p ciphertext_len will be written in this buffer.
* This pointer may also point to the same location as \p ciphertext
* to decrypt the ciphertext in-place, sparing on memory instead
* of writing into a separate output buffer. Not NULL.
* @param[in] key secret key of #ASCON_AEAD128_KEY_LEN bytes. Not NULL.
* @param[in] nonce public unique nonce of #ASCON_AEAD_NONCE_LEN bytes.
* @param[in] assoc_data data to be validated with the same tag
* but not decrypted. Can be NULL iff \p assoc_data_len is 0.
* @param[in] ciphertext data to be decrypted into \p plaintext.
* @param[in] expected_tag Message Authentication Code (MAC, a.k.a. cryptographic tag,
* fingerprint), used to validate the integrity and authenticity of the
* associated data and ciphertext. Has \p tag_len bytes. Not NULL.
* @param[in] assoc_data_len length of the data pointed by \p assoc_data in
* bytes. Can be 0.
* @param[in] ciphertext_len length of the data pointed by \p ciphertext in
* bytes. Can be 0 (not recommended, see warning of
* ascon_aead128_encrypt()).
* @param[in] expected_tag_len length of the \p tag to check in bytes. It should be
* the same length as generated during the encryption
* but it can be shorter (although it's not recommended).
* @returns the answer to the question "is tha tag valid?", thus
* `true` (== #ASCON_TAG_OK) if the validation of the tag is correct,
* thus the associated data and ciphertext are intact and authentic.
* `false` (== #ASCON_TAG_INVALID) otherwise.
*/
ASCON_API bool
ascon_aead128_decrypt(uint8_t* plaintext,
const uint8_t key[ASCON_AEAD128_KEY_LEN],
const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
const uint8_t* assoc_data,
const uint8_t* ciphertext,
const uint8_t* expected_tag,
size_t assoc_data_len,
size_t ciphertext_len,
size_t expected_tag_len);
/**
* Online symmetric decryption using Ascon128, feeding ciphertext and getting
* plaintext.
*
* Feeds a chunk of ciphertext data to the decryption session after any
* optional associated data has been processed. The ciphertext will be decrypted
* and provided back in buffered chunks of #ASCON_RATE bytes.
*
* It will automatically finalise the absorption of any associated data,
* so no new associated data could be processed after this function is called.
*
* The calling order for decryption is:
* 1. ascon_aead128_init() - once only
* 2. ascon_aead128_assoc_data_update() - 0 or more times
* 3. ascon_aead128_decrypt_update() - 0 or more times, see warning
* 4. ascon_aead128_decrypt_final() - once only
*
* @param[in, out] ctx the decryption context, handling the cipher state
* and buffering of incoming data to be processed. Not NULL.
* @param[out] plaintext decrypted data, buffered into chunks.
* This function will write a multiple of #ASCON_RATE bytes in the
* interval [0, \p ciphertext_len + #ASCON_RATE[ into \p plaintext.
* The exact number of written bytes is indicated by the return value.
* This pointer may also point to the same location as \p ciphertext
* to decrypt the ciphertext in-place, sparing on memory instead
* of writing into a separate output buffer. Not NULL.
* @param[in] ciphertext data to be decrypted into \p plaintext. All of the
* ciphertext will be processed, even if the function provides less than
* \p ciphertext_len output bytes. They are just buffered. Not NULL.
* @param[in] ciphertext_len length of the data pointed by \p ciphertext in
* bytes. May be 0.
* @returns number of bytes written into \p plaintext. The value is a multiple
* of #ASCON_RATE in [0, \p ciphertext_len + #ASCON_RATE[.
*/
ASCON_API size_t
ascon_aead128_decrypt_update(ascon_aead_ctx_t* ctx,
uint8_t* plaintext,
const uint8_t* ciphertext,
size_t ciphertext_len);
/**
* Online symmetric decryption using Ascon128, finalisation and tag validation.
*
* Finalises the authenticated decryption by returning any remaining buffered
* plaintext and the validity of the authentication tag.
* The total plaintext length is equal to the total ciphertext length.
*
* It will securely erase the content of the \p ctx struct before returning.
*
* The calling order for decryption is:
* 1. ascon_aead128_init() - once only
* 2. ascon_aead128_assoc_data_update() - 0 or more times
* 3. ascon_aead128_decrypt_update() - 0 or more times, see warning
* 4. ascon_aead128_decrypt_final() - once only
*
* @warning
* A copy of the secret key is kept in the \p ctx struct and securely erased
* during this function call. In case the decryption session is interrupted
* and never finalised (this function is never called), clear the context with
* ascon_aead_cleanup() to erase the key copy.
*
* @param[in, out] ctx the decryption context, handling the cipher
* state and buffering of incoming data to be processed. It will be erased
* securely before this function returns. Not NULL.
* @param[out] plaintext trailing decrypted data still available in the
* buffer of the buffered updating. This function will write
* [0, #ASCON_RATE[ bytes into \p plaintext.
* The exact number of written bytes is indicated by the return value.
* Not NULL.
* @param[out] is_tag_valid the answer to the question "is the tag valid?", thus
* `true` (== #ASCON_TAG_OK) if the validation of the tag is correct,
* thus the associated data and ciphertext are intact and authentic.
* `false` (== #ASCON_TAG_INVALID) otherwise.
* @param[in] expected_tag Message Authentication Code (MAC, a.k.a. cryptographic tag,
* fingerprint), used to validate the integrity and authenticity of the
* associated data and ciphertext. Has \p tag_len bytes. Not NULL.
* @param[in] expected_tag_len length of the \p tag to check in bytes. It should be
* the same length as generated during the encryption
* but it can be shorter (although it's not recommended).
* @returns number of bytes written into \p plaintext. The value is in the
* interval [0, #ASCON_RATE[, i.e. whatever remained in the buffer
* after the last update call.
*/
ASCON_API size_t
ascon_aead128_decrypt_final(ascon_aead_ctx_t* ctx,
uint8_t* plaintext,
bool* is_tag_valid,
const uint8_t* expected_tag,
size_t expected_tag_len);
/**
* Security cleanup of the AEAD context, in case the online processing
* is not completed to the end.
*
* Use this function only when something goes wrong between the calls of
* online encryption or decryption and you never call the
* `ascon_aead*_encrypt_final()` or `ascon_aead*_decrypt_final()` functions
* of the cipher you are currently using (because these 2 functions perform the
* cleanup automatically).
*
* This is to prevent any information (especially the key!) to leak through the
* context in case an encryption/decryption transaction is rolled back/abruptly
* terminated.
*
* @param[in, out] ctx to erase.
*/
ASCON_API void
ascon_aead_cleanup(ascon_aead_ctx_t* ctx);
/**
* Offline symmetric encryption using Ascon128a, which uses a double data rate
* compared to Ascon128.
*
* Encrypts the data which is already available as a whole in a contiguous
* buffer, authenticating any optional associated data in the process.
* Provides the ciphertext and the authentication tag as output.
*
* In case of no associated data at all to be authenticated, set
* \p assoc_data_len to 0. Iff that is the case, \p assoc_data can
* be set to NULL.
*
* @image html encrypt.png
*
* @warning
* The nonce **must be unique**, as the strength of the AEAD is based on
* its uniqueness.
*
* @warning
* Using the AEAD to just authenticate any associated data with no
* plaintext to be encrypted is not recommended as the AEAD algorithm is not
* designed for that. Instead, use the Ascon hashing or xof functions in the form
* `Hash(key || msg)`.
*
* @param[out] ciphertext encrypted data with the same length as the
* plaintext, thus \p plaintext_len will be written in this buffer.
* This pointer may also point to the same location as \p plaintext
* to encrypt the plaintext in-place, sparing on memory instead
* of writing into a separate output buffer. Not NULL.
* @param[out] tag Message Authentication Code (MAC, a.k.a. cryptographic tag,
* fingerprint), used to validate the integrity and authenticity of the
* associated data and ciphertext. Has \p tag_len bytes. Not NULL.
* @param[in] key secret key of #ASCON_AEAD128a_KEY_LEN bytes. Not NULL.
* @param[in] nonce public **unique** nonce of #ASCON_AEAD_NONCE_LEN bytes.
* @param[in] assoc_data data to be authenticated with the same tag
* but not encrypted. Can be NULL iff \p assoc_data_len is 0.
* @param[in] plaintext data to be encrypted into \p ciphertext.
* @param[in] assoc_data_len length of the data pointed by \p assoc_data in
* bytes. Can be 0.
* @param[in] plaintext_len length of the data pointed by \p plaintext in
* bytes. Can be 0 (not recommended, see warning).
* @param[in] tag_len length of the tag to generate in bytes. At least
* #ASCON_AEAD_TAG_MIN_SECURE_LEN is recommended for security.
*/
ASCON_API void
ascon_aead128a_encrypt(uint8_t* ciphertext,
uint8_t* tag,
const uint8_t key[ASCON_AEAD128_KEY_LEN],
const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
const uint8_t* assoc_data,
const uint8_t* plaintext,
size_t assoc_data_len,
size_t plaintext_len,
size_t tag_len);
/**
* Online symmetric encryption/decryption using Ascon128a, initialisation.
*
* Prepares to start a new encryption or decryption session for plaintext or
* ciphertext and associated data being provided one chunk at the time.
*
* The key and nonce are copied/absorbed into the internal state, so they can
* be deleted from their original location after this function returns.
*
* The calling order for encryption/decryption is:
* 1. ascon_aead128a_init() - once only
* 2. ascon_aead128a_assoc_data_update() - 0 or more times
* 3. ascon_aead128a_encrypt_update() / ascon_aead128a_decrypt_update() - 0 or
* more times, see warning
* 4. ascon_aead128a_encrypt_final() / ascon_aead128a_encrypt_final() - once
* only
*
* @image html encrypt.png
* @image html decrypt.png
*
* @warning
* Using the AEAD to just authenticate any associated data with no
* plaintext to be encrypted is not recommended as the AEAD algorithm is not
* designed for that. Instead, use the Ascon hashing or xof functions in the form
* `Hash(key || msg)`.
*
* @warning
* A copy of the secret key is kept in the \p ctx struct and securely erased
* during the ascon_aead128a_encrypt_final() or ascon_aead128a_decrypt_final()
* call. In case the encryption or decryption session is interrupted and never
* finalised, clear the context with ascon_aead_cleanup() to erase the key copy.
*
* @warning
* Do not mix Init-Update-Final functions across ciphers.
*
* @param[in, out] ctx the encryption/decryption context, handling the cipher
* state and buffering of incoming data to be processed. Not NULL.
* @param[in] key secret key of #ASCON_AEAD128a_KEY_LEN bytes. Not NULL
* @param[in] nonce public unique nonce of #ASCON_AEAD_NONCE_LEN bytes. Not
* NULL.
*/
ASCON_API void
ascon_aead128a_init(ascon_aead_ctx_t* ctx,
const uint8_t key[ASCON_AEAD128a_KEY_LEN],
const uint8_t nonce[ASCON_AEAD_NONCE_LEN]);
/**
* Online symmetric encryption/decryption using Ascon128a, feeding associated
* data.
*
* Feeds a chunk of associated data to the already initialised encryption
* or decryption session. The data will be authenticated by the tag provided by
* the final function, but not encrypted or decrypted.
*
* In case of no associated data at all to be authenticated/validated, this
* function can either be skipped completely or called (also many times)
* with \p assoc_data_len set to 0. Iff that is the case, \p assoc_data can
* be set to NULL.
*
* After calling ascon_aead128a_encrypt_update() or
* ascon_aead128a_decrypt_update(), this function **must** not be used anymore
* on the same context.
*
* The calling order for encryption/decryption is:
* 1. ascon_aead128a_init() - once only
* 2. ascon_aead128a_assoc_data_update() - 0 or more times
* 3. ascon_aead128a_encrypt_update() / ascon_aead128a_decrypt_update() - 0 or
* more times, see warning
* 4. ascon_aead128a_encrypt_final() / ascon_aead128a_encrypt_final() - once
* only
*
* @warning
* Using the AEAD to just authenticate any associated data with no
* plaintext to be encrypted is not recommended as the AEAD algorithm is not
* designed for that. Instead, use the Ascon hashing or xof functions in the form
* `Hash(key || msg)`.
*
* @param[in, out] ctx the encryption/decryption context, handling the cipher
* state and buffering of incoming data to be processed. Not NULL.
* @param[in] assoc_data data to be authenticated/validated with the same tag
* but not encrypted/decrypted. May be NULL iff \p assoc_data_len is 0.
* @param[in] assoc_data_len length of the data pointed by \p assoc_data in
* bytes. May be 0.
*/
ASCON_API void
ascon_aead128a_assoc_data_update(ascon_aead_ctx_t* ctx,
const uint8_t* assoc_data,
size_t assoc_data_len);
/**
* Online symmetric encryption using Ascon128a, feeding plaintext and getting
* ciphertext.
*
* Feeds a chunk of plaintext data to the encryption session after any
* optional associated data has been processed. The plaintext will be encrypted
* and provided as ciphertext in buffered chunks of #ASCON_DOUBLE_RATE bytes.
*
* It will automatically finalise the absorption of any associated data,
* so no new associated data could be processed after this function is called.
*
* The calling order for encryption is:
* 1. ascon_aead128a_init() - once only
* 2. ascon_aead128a_assoc_data_update() - 0 or more times
* 3. ascon_aead128a_encrypt_update() - 0 or more times, see warning
* 4. ascon_aead128a_encrypt_final() - once only
*
* @warning
* Using the AEAD to just authenticate any associated data with no
* plaintext to be encrypted is not recommended as the AEAD algorithm is not
* designed for that. Instead, use the Ascon hashing or xof functions in the form
* `Hash(key || msg)`.
*
* @param[in, out] ctx the encryption context, handling the cipher
* state and buffering of incoming data to be processed. Not NULL.
* @param[out] ciphertext encrypted data, buffered into chunks.
* This function will write a multiple of #ASCON_DOUBLE_RATE bytes in the
* interval [0, \p plaintext_len + #ASCON_DOUBLE_RATE[ into \p ciphertext.
* The exact number of written bytes is indicated by the return value.
* This pointer may also point to the same location as \p plaintext
* to encrypt the plaintext in-place, sparing on memory instead
* of writing into a separate output buffer. Not NULL.
* @param[in] plaintext data to be encrypted into \p ciphertext. All of the
* plaintext will be processed, even if the function provides less than
* \p plaintext_len output bytes. They are just buffered. Not NULL.
* @param[in] plaintext_len length of the data pointed by \p plaintext in
* bytes. May be 0.
* @returns number of bytes written into \p ciphertext. The value is a multiple
* of #ASCON_RATE in [0, \p plaintext_len + #ASCON_DOUBLE_RATE[.
*/
ASCON_API size_t
ascon_aead128a_encrypt_update(ascon_aead_ctx_t* ctx,
uint8_t* ciphertext,
const uint8_t* plaintext,
size_t plaintext_len);
/**
* Online symmetric encryption using Ascon128a, finalisation and tag generation.
*
* Finalises the authenticated encryption by returning any remaining buffered
* ciphertext and the authentication tag. The total ciphertext length is
* equal to the total plaintext length.
*
* It will securely erase the content of the \p ctx struct before returning.
*
* The calling order for encryption is:
* 1. ascon_aead128a_init() - once only
* 2. ascon_aead128a_assoc_data_update() - 0 or more times
* 3. ascon_aead128a_encrypt_update() - 0 or more times, see warning
* 4. ascon_aead128a_encrypt_final() - once only
*
* @warning
* Using the AEAD to just authenticate any associated data with no
* plaintext to be encrypted is not recommended as the AEAD algorithm is not
* designed for that. Instead, use the Ascon hashing or xof functions in the form
* `Hash(key || msg)`.
*
* @warning
* A copy of the secret key is kept in the \p ctx struct and securely erased
* during this function call. In case the encryption session is interrupted
* and never finalised (this function is never called), clear the context with
* ascon_aead_cleanup() to erase the key copy.
*
* @param[in, out] ctx the encryption context, handling the cipher
* state and buffering of incoming data to be processed. It will be erased
* securely before this function returns. Not NULL.
* @param[out] ciphertext trailing encrypted data still available in the
* buffer of the buffered updating. This function will write
* [0, #ASCON_DOUBLE_RATE[ bytes into \p ciphertext.
* The exact number of written bytes is indicated by the return value.
* Not NULL.
* @param[out] tag Message Authentication Code (MAC, a.k.a. cryptographic tag,
* fingerprint), used to validate the integrity and authenticity of the
* associated data and ciphertext. Has \p tag_len bytes. Not NULL.
* @param[in] tag_len length of the tag to generate in bytes. At least
* #ASCON_AEAD_TAG_MIN_SECURE_LEN is recommended for security.
* @returns number of bytes written into \p ciphertext. The value is in the
* interval [0, #ASCON_DOUBLE_RATE[, i.e. whatever remained in the buffer
* after the last update call.
*/
ASCON_API size_t
ascon_aead128a_encrypt_final(ascon_aead_ctx_t* ctx,
uint8_t* ciphertext,
uint8_t* tag,
size_t tag_len);
/**
* Offline symmetric decryption using Ascon128a, which uses a double data rate
* compared to Ascon128.
*
* Decrypts the data which is already available as a whole in a contiguous
* buffer, validating any optional associated data in the process.
* Provides the plaintext and the validity of the authentication tag as output.
* The total plaintext length is equal to the total ciphertext length.
*
* In case of no associated data at all to be authenticated, set
* \p assoc_data_len to 0. Iff that is the case, \p assoc_data can
* be set to NULL.
*
* @image html decrypt.png
*
* @param[out] plaintext decrypted data with the same length as the
* ciphertext, thus \p ciphertext_len will be written in this buffer.
* This pointer may also point to the same location as \p ciphertext
* to decrypt the ciphertext in-place, sparing on memory instead
* of writing into a separate output buffer. Not NULL.
* @param[in] key secret key of #ASCON_AEAD128a_KEY_LEN bytes. Not NULL.
* @param[in] nonce public unique nonce of #ASCON_AEAD_NONCE_LEN bytes.
* @param[in] assoc_data data to be validated with the same tag
* but not decrypted. Can be NULL iff \p assoc_data_len is 0.
* @param[in] ciphertext data to be decrypted into \p plaintext.
* @param[in] expected_tag Message Authentication Code (MAC, a.k.a. cryptographic tag,
* fingerprint), used to validate the integrity and authenticity of the
* associated data and ciphertext. Has \p tag_len bytes. Not NULL.
* @param[in] assoc_data_len length of the data pointed by \p assoc_data in
* bytes. Can be 0.
* @param[in] ciphertext_len length of the data pointed by \p ciphertext in
* bytes. Can be 0 (not recommended, see warning of
* ascon_aead128_encrypt()).
* @param[in] expected_tag_len length of the \p tag to check in bytes. It should be
* the same length as generated during the encryption
* but it can be shorter (although it's not recommended).
* @returns the answer to the question "is tha tag valid?", thus
* `true` (== #ASCON_TAG_OK) if the validation of the tag is correct,
* thus the associated data and ciphertext are intact and authentic.
* `false` (== #ASCON_TAG_INVALID) otherwise.
*/
ASCON_API bool
ascon_aead128a_decrypt(uint8_t* plaintext,
const uint8_t key[ASCON_AEAD128a_KEY_LEN],
const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
const uint8_t* assoc_data,
const uint8_t* ciphertext,
const uint8_t* expected_tag,
size_t assoc_data_len,
size_t ciphertext_len,
size_t expected_tag_len);
/**
* Online symmetric decryption using Ascon128a, feeding ciphertext and getting
* plaintext.
*
* Feeds a chunk of ciphertext data to the decryption session after any
* optional associated data has been processed. The ciphertext will be decrypted
* and provided back in buffered chunks of #ASCON_RATE bytes.
*
* It will automatically finalise the absorption of any associated data,
* so no new associated data could be processed after this function is called.
*
* The calling order for decryption is:
* 1. ascon_aead128a_init() - once only
* 2. ascon_aead128a_assoc_data_update() - 0 or more times
* 3. ascon_aead128a_decrypt_update() - 0 or more times, see warning
* 4. ascon_aead128a_decrypt_final() - once only
*
* @param[in, out] ctx the decryption context, handling the cipher state
* and buffering of incoming data to be processed. Not NULL.
* @param[out] plaintext decrypted data, buffered into chunks.
* This function will write a multiple of #ASCON_DOUBLE_RATE bytes in the
* interval [0, \p ciphertext_len + #ASCON_DOUBLE_RATE[ into \p plaintext.
* The exact number of written bytes is indicated by the return value.
* This pointer may also point to the same location as \p ciphertext
* to decrypt the ciphertext in-place, sparing on memory instead
* of writing into a separate output buffer. Not NULL.
* @param[in] ciphertext data to be decrypted into \p plaintext. All of the
* ciphertext will be processed, even if the function provides less than
* \p ciphertext_len output bytes. They are just buffered. Not NULL.
* @param[in] ciphertext_len length of the data pointed by \p ciphertext in
* bytes. May be 0.
* @returns number of bytes written into \p plaintext. The value is a multiple
* of #ASCON_DOUBLE_RATE in [0, \p ciphertext_len + #ASCON_DOUBLE_RATE[.
*/
ASCON_API size_t
ascon_aead128a_decrypt_update(ascon_aead_ctx_t* ctx,
uint8_t* plaintext,
const uint8_t* ciphertext,
size_t ciphertext_len);
/**
* Online symmetric decryption using Ascon128a, finalisation and tag validation.
*
* Finalises the authenticated decryption by returning any remaining buffered
* plaintext and the validity of the authentication tag.
* The total plaintext length is equal to the total ciphertext length.
*
* It will securely erase the content of the \p ctx struct before returning.
*
* The calling order for decryption is:
* 1. ascon_aead128a_init() - once only
* 2. ascon_aead128a_assoc_data_update() - 0 or more times
* 3. ascon_aead128a_decrypt_update() - 0 or more times, see warning
* 4. ascon_aead128a_decrypt_final() - once only
*
* @warning
* A copy of the secret key is kept in the \p ctx struct and securely erased
* during this function call. In case the decryption session is interrupted
* and never finalised (this function is never called), clear the context with
* ascon_aead_cleanup() to erase the key copy.
*
* @param[in, out] ctx the decryption context, handling the cipher
* state and buffering of incoming data to be processed. It will be erased
* securely before this function returns. Not NULL.