pq_crypto-seal 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ci.yml +66 -0
  3. data/CHANGELOG.md +44 -0
  4. data/FORMAT.md +139 -0
  5. data/GET_STARTED.md +30 -0
  6. data/Gemfile +7 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +215 -0
  9. data/RELEASING.md +34 -0
  10. data/Rakefile +19 -0
  11. data/SECURITY.md +85 -0
  12. data/VENDORING.md +14 -0
  13. data/ext/pq_crypto_seal/aegis_unused_stubs.c +20 -0
  14. data/ext/pq_crypto_seal/extconf.rb +296 -0
  15. data/ext/pq_crypto_seal/pq_crypto_seal.c +390 -0
  16. data/ext/pq_crypto_seal/vendor/libaegis/LICENSE +21 -0
  17. data/ext/pq_crypto_seal/vendor/libaegis/TREE_SHA256 +1 -0
  18. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l.c +246 -0
  19. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_aesni.c +73 -0
  20. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_aesni.h +9 -0
  21. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_altivec.c +70 -0
  22. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_altivec.h +9 -0
  23. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_common.h +691 -0
  24. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_aes.c +77 -0
  25. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_aes.h +9 -0
  26. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_sha3.c +86 -0
  27. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_sha3.h +9 -0
  28. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_soft.c +63 -0
  29. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_soft.h +9 -0
  30. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/implementations.h +40 -0
  31. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2.c +247 -0
  32. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_aesni.c +108 -0
  33. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_aesni.h +9 -0
  34. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_altivec.c +103 -0
  35. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_altivec.h +9 -0
  36. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_avx2.c +79 -0
  37. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_avx2.h +11 -0
  38. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_common.h +795 -0
  39. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_neon_aes.c +111 -0
  40. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_neon_aes.h +9 -0
  41. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_soft.c +96 -0
  42. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_soft.h +9 -0
  43. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/implementations.h +40 -0
  44. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4.c +252 -0
  45. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_aesni.c +117 -0
  46. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_aesni.h +9 -0
  47. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_altivec.c +111 -0
  48. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_altivec.h +9 -0
  49. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx2.c +112 -0
  50. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx2.h +11 -0
  51. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx512.c +85 -0
  52. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx512.h +11 -0
  53. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_common.h +808 -0
  54. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_neon_aes.c +119 -0
  55. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_neon_aes.h +9 -0
  56. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_soft.c +104 -0
  57. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_soft.h +9 -0
  58. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/implementations.h +41 -0
  59. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256.c +240 -0
  60. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_aesni.c +68 -0
  61. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_aesni.h +9 -0
  62. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_altivec.c +68 -0
  63. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_altivec.h +9 -0
  64. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_common.h +673 -0
  65. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_neon_aes.c +75 -0
  66. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_neon_aes.h +9 -0
  67. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_soft.c +58 -0
  68. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_soft.h +9 -0
  69. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/implementations.h +39 -0
  70. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2.c +247 -0
  71. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_aesni.c +103 -0
  72. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_aesni.h +9 -0
  73. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_altivec.c +101 -0
  74. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_altivec.h +9 -0
  75. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_avx2.c +74 -0
  76. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_avx2.h +11 -0
  77. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_common.h +790 -0
  78. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_neon_aes.c +109 -0
  79. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_neon_aes.h +9 -0
  80. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_soft.c +91 -0
  81. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_soft.h +9 -0
  82. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/implementations.h +41 -0
  83. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4.c +252 -0
  84. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_aesni.c +112 -0
  85. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_aesni.h +9 -0
  86. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_altivec.c +109 -0
  87. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_altivec.h +9 -0
  88. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx2.c +107 -0
  89. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx2.h +11 -0
  90. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx512.c +79 -0
  91. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx512.h +11 -0
  92. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_common.h +805 -0
  93. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_neon_aes.c +117 -0
  94. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_neon_aes.h +9 -0
  95. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_soft.c +99 -0
  96. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_soft.h +9 -0
  97. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/implementations.h +42 -0
  98. data/ext/pq_crypto_seal/vendor/libaegis/src/common/common.c +81 -0
  99. data/ext/pq_crypto_seal/vendor/libaegis/src/common/common.h +144 -0
  100. data/ext/pq_crypto_seal/vendor/libaegis/src/common/cpu.c +378 -0
  101. data/ext/pq_crypto_seal/vendor/libaegis/src/common/cpu.h +34 -0
  102. data/ext/pq_crypto_seal/vendor/libaegis/src/common/keccak.c +187 -0
  103. data/ext/pq_crypto_seal/vendor/libaegis/src/common/keccak.h +13 -0
  104. data/ext/pq_crypto_seal/vendor/libaegis/src/common/softaes.c +338 -0
  105. data/ext/pq_crypto_seal/vendor/libaegis/src/common/softaes.h +69 -0
  106. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis.h +77 -0
  107. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis128l.h +311 -0
  108. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis128x2.h +313 -0
  109. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis128x4.h +313 -0
  110. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis256.h +311 -0
  111. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis256x2.h +313 -0
  112. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis256x4.h +313 -0
  113. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis_raf.h +580 -0
  114. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf.c +87 -0
  115. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis128l.c +20 -0
  116. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis128x2.c +20 -0
  117. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis128x4.c +20 -0
  118. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis256.c +20 -0
  119. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis256x2.c +20 -0
  120. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis256x4.c +20 -0
  121. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_internal.h +152 -0
  122. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_merkle.c +268 -0
  123. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_merkle.h +165 -0
  124. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_variant.h +1119 -0
  125. data/fuzz/decrypt_fuzz.rb +119 -0
  126. data/lib/pq_crypto/seal/binary.rb +62 -0
  127. data/lib/pq_crypto/seal/core.rb +398 -0
  128. data/lib/pq_crypto/seal/errors.rb +15 -0
  129. data/lib/pq_crypto/seal/format.rb +275 -0
  130. data/lib/pq_crypto/seal/io.rb +336 -0
  131. data/lib/pq_crypto/seal/native.rb +13 -0
  132. data/lib/pq_crypto/seal/padding.rb +47 -0
  133. data/lib/pq_crypto/seal/version.rb +6 -0
  134. data/lib/pq_crypto/seal.rb +32 -0
  135. data/pq_crypto-seal.gemspec +37 -0
  136. data/script/vendor_libs.rb +79 -0
  137. data/test/aegis_vectors_test.rb +54 -0
  138. data/test/golden_vectors_test.rb +183 -0
  139. data/test/io_test.rb +125 -0
  140. data/test/native_test.rb +36 -0
  141. data/test/seal_test.rb +238 -0
  142. data/test/test_helper.rb +6 -0
  143. metadata +244 -0
@@ -0,0 +1,313 @@
1
+ #ifndef aegis128x4_H
2
+ #define aegis128x4_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif
10
+
11
+ /* The length of an AEGIS key, in bytes */
12
+ #define aegis128x4_KEYBYTES 16
13
+
14
+ /* The length of an AEGIS nonce, in bytes */
15
+ #define aegis128x4_NPUBBYTES 16
16
+
17
+ /* The minimum length of an AEGIS authentication tag, in bytes */
18
+ #define aegis128x4_ABYTES_MIN 16
19
+
20
+ /* The maximum length of an AEGIS authentication tag, in bytes */
21
+ #define aegis128x4_ABYTES_MAX 32
22
+
23
+ /*
24
+ * Maximum number of leftover bytes at finalization (always 0).
25
+ * This constant is kept for backwards compatibility but is no longer needed
26
+ * since update functions now output data immediately without buffering.
27
+ */
28
+ #define aegis128x4_TAILBYTES_MAX 127
29
+
30
+ /* An AEGIS state, for incremental updates */
31
+ typedef struct aegis128x4_state {
32
+ CRYPTO_ALIGN(64) uint8_t opaque[832];
33
+ } aegis128x4_state;
34
+
35
+ /* An AEGIS state, only for MAC updates */
36
+ typedef struct aegis128x4_mac_state {
37
+ CRYPTO_ALIGN(64) uint8_t opaque[1344];
38
+ } aegis128x4_mac_state;
39
+
40
+ /* The length of an AEGIS key, in bytes */
41
+ size_t aegis128x4_keybytes(void);
42
+
43
+ /* The length of an AEGIS nonce, in bytes */
44
+ size_t aegis128x4_npubbytes(void);
45
+
46
+ /* The minimum length of an AEGIS authentication tag, in bytes */
47
+ size_t aegis128x4_abytes_min(void);
48
+
49
+ /* The maximum length of an AEGIS authentication tag, in bytes */
50
+ size_t aegis128x4_abytes_max(void);
51
+
52
+ /*
53
+ * Maximum number of leftover bytes at finalization (always 0).
54
+ * This function is kept for backwards compatibility but always returns 127.
55
+ * Update functions now output data immediately without buffering.
56
+ */
57
+ size_t aegis128x4_tailbytes_max(void);
58
+
59
+ /*
60
+ * Encrypt a message with AEGIS in one shot mode, returning the tag and the ciphertext separately.
61
+ *
62
+ * c: ciphertext output buffer
63
+ * mac: authentication tag output buffer
64
+ * maclen: length of the authentication tag to generate (16 or 32)
65
+ * m: plaintext input buffer
66
+ * mlen: length of the plaintext
67
+ * ad: additional data input buffer
68
+ * adlen: length of the additional data
69
+ * npub: nonce input buffer (16 bytes)
70
+ * k: key input buffer (16 bytes)
71
+ */
72
+ int aegis128x4_encrypt_detached(uint8_t *c, uint8_t *mac, size_t maclen, const uint8_t *m,
73
+ size_t mlen, const uint8_t *ad, size_t adlen, const uint8_t *npub,
74
+ const uint8_t *k);
75
+
76
+ /*
77
+ * Decrypt a message with AEGIS in one shot mode with a detached tag.
78
+ *
79
+ * m: plaintext output buffer
80
+ * c: ciphertext input buffer
81
+ * clen: length of the ciphertext
82
+ * mac: authentication tag input buffer
83
+ * maclen: length of the authentication tag (16 or 32)
84
+ * ad: additional data input buffer
85
+ * adlen: length of the additional data
86
+ * npub: nonce input buffer (16 bytes)
87
+ * k: key input buffer (16 bytes)
88
+ *
89
+ * Returns 0 if the ciphertext is authentic, -1 otherwise.
90
+ */
91
+ AEGIS_WARN_UNUSED_RESULT
92
+ int aegis128x4_decrypt_detached(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *mac,
93
+ size_t maclen, const uint8_t *ad, size_t adlen, const uint8_t *npub,
94
+ const uint8_t *k);
95
+
96
+ /*
97
+ * Encrypt a message with AEGIS in one shot mode, returning the tag and the ciphertext together.
98
+ *
99
+ * c: ciphertext output buffer
100
+ * maclen: length of the authentication tag to generate (16 or 32)
101
+ * m: plaintext input buffer
102
+ * mlen: length of the plaintext
103
+ * ad: additional data input buffer
104
+ * adlen: length of the additional data
105
+ * npub: nonce input buffer (16 bytes)
106
+ * k: key input buffer (16 bytes)
107
+ */
108
+ int aegis128x4_encrypt(uint8_t *c, size_t maclen, const uint8_t *m, size_t mlen, const uint8_t *ad,
109
+ size_t adlen, const uint8_t *npub, const uint8_t *k);
110
+
111
+ /*
112
+ * Decrypt a message with AEGIS in one shot mode, returning the tag and the ciphertext together.
113
+ *
114
+ * m: plaintext output buffer
115
+ * c: ciphertext input buffer
116
+ * clen: length of the ciphertext
117
+ * maclen: length of the authentication tag (16 or 32)
118
+ * ad: additional data input buffer
119
+ * adlen: length of the additional data
120
+ * npub: nonce input buffer (16 bytes)
121
+ * k: key input buffer (16 bytes)
122
+ *
123
+ * Returns 0 if the ciphertext is authentic, -1 otherwise.
124
+ */
125
+ AEGIS_WARN_UNUSED_RESULT
126
+ int aegis128x4_decrypt(uint8_t *m, const uint8_t *c, size_t clen, size_t maclen, const uint8_t *ad,
127
+ size_t adlen, const uint8_t *npub, const uint8_t *k);
128
+
129
+ /*
130
+ * Initialize a state for incremental encryption or decryption.
131
+ *
132
+ * st_: state to initialize
133
+ * ad: additional data input buffer
134
+ * adlen: length of the additional data
135
+ * npub: nonce input buffer (16 bytes)
136
+ * k: key input buffer (16 bytes)
137
+ */
138
+ void aegis128x4_state_init(aegis128x4_state *st_, const uint8_t *ad, size_t adlen,
139
+ const uint8_t *npub, const uint8_t *k);
140
+
141
+ /*
142
+ * Encrypt a message chunk.
143
+ * The same function can be used regardless of whether the tag will be attached or not.
144
+ *
145
+ * st_: state to update
146
+ * c: ciphertext output buffer (must be at least mlen bytes)
147
+ * m: plaintext input buffer
148
+ * mlen: length of the plaintext
149
+ *
150
+ * Return 0 on success, -1 on failure.
151
+ */
152
+ int aegis128x4_state_encrypt_update(aegis128x4_state *st_, uint8_t *c, const uint8_t *m,
153
+ size_t mlen);
154
+
155
+ /*
156
+ * Finalize the incremental encryption and generate the authentication tag.
157
+ *
158
+ * st_: state to finalize
159
+ * mac: output buffer for the authentication tag
160
+ * maclen: length of the authentication tag to generate (16 or 32)
161
+ *
162
+ * Return 0 on success, -1 on failure.
163
+ */
164
+ int aegis128x4_state_encrypt_final(aegis128x4_state *st_, uint8_t *mac, size_t maclen);
165
+
166
+ /*
167
+ * Decrypt a message chunk.
168
+ *
169
+ * The decrypted message must not be used until state_decrypt_final() has been
170
+ * called and returns successfully.
171
+ *
172
+ * st_: state to update
173
+ * m: plaintext output buffer (must be at least clen bytes)
174
+ * c: ciphertext chunk input buffer
175
+ * clen: length of the ciphertext chunk
176
+ *
177
+ * Return 0 on success, -1 on failure.
178
+ */
179
+ AEGIS_WARN_UNUSED_RESULT
180
+ int aegis128x4_state_decrypt_update(aegis128x4_state *st_, uint8_t *m, const uint8_t *c,
181
+ size_t clen);
182
+
183
+ /*
184
+ * Finalize the incremental decryption and verify the authentication tag.
185
+ *
186
+ * This function verifies the authentication tag. It must be called after all
187
+ * decryption updates, and the decrypted message must not be used until this
188
+ * function returns successfully.
189
+ *
190
+ * st_: state to finalize
191
+ * mac: authentication tag input buffer
192
+ * maclen: length of the authentication tag (16 or 32)
193
+ *
194
+ * Return 0 if the tag is valid, -1 otherwise.
195
+ */
196
+ AEGIS_WARN_UNUSED_RESULT
197
+ int aegis128x4_state_decrypt_final(aegis128x4_state *st_, const uint8_t *mac, size_t maclen);
198
+
199
+ /*
200
+ * Return a deterministic pseudo-random byte sequence.
201
+ *
202
+ * out: output buffer
203
+ * len: number of bytes to generate
204
+ * npub: nonce input buffer (16 bytes) - Can be set to `NULL` if only one sequence has to be
205
+ * generated from a given key.
206
+ * k: key input buffer (16 bytes)
207
+ */
208
+ void aegis128x4_stream(uint8_t *out, size_t len, const uint8_t *npub, const uint8_t *k);
209
+
210
+ /*
211
+ * Encrypt a message WITHOUT AUTHENTICATION, similar to AES-CTR.
212
+ *
213
+ * WARNING: this is an insecure mode of operation, provided for compatibility with specific
214
+ * protocols that bring their own authentication scheme.
215
+ *
216
+ * c: ciphertext output buffer
217
+ * m: plaintext input buffer
218
+ * mlen: length of the plaintext
219
+ * npub: nonce input buffer (16 bytes)
220
+ * k: key input buffer (16 bytes)
221
+ */
222
+ void aegis128x4_encrypt_unauthenticated(uint8_t *c, const uint8_t *m, size_t mlen,
223
+ const uint8_t *npub, const uint8_t *k);
224
+
225
+ /*
226
+ * Decrypt a message WITHOUT AUTHENTICATION, similar to AES-CTR.
227
+ *
228
+ * WARNING: this is an insecure mode of operation, provided for compatibility with specific
229
+ * protocols that bring their own authentication scheme.
230
+ *
231
+ * m: plaintext output buffer
232
+ * c: ciphertext input buffer
233
+ * clen: length of the ciphertext
234
+ * npub: nonce input buffer (16 bytes)
235
+ * k: key input buffer (16 bytes)
236
+ */
237
+ void aegis128x4_decrypt_unauthenticated(uint8_t *m, const uint8_t *c, size_t clen,
238
+ const uint8_t *npub, const uint8_t *k);
239
+
240
+ /*
241
+ * Initialize a state for generating a MAC.
242
+ *
243
+ * st_: state to initialize
244
+ * k: key input buffer (16 bytes)
245
+ * npub: nonce input buffer (16 bytes) - Can be set to `NULL` to use an all-zero nonce
246
+ *
247
+ * - The same key MUST NOT be used both for MAC and encryption.
248
+ * - If the key is secret, the MAC is secure against forgery.
249
+ * - However, if the key is known, arbitrary inputs matching a tag can be efficiently computed.
250
+ *
251
+ * The recommended way to use the MAC mode is to generate a random key and keep it secret.
252
+ *
253
+ * After initialization, the state can be reused to generate multiple MACs by cloning it
254
+ * with `aegis128x4_mac_state_clone()`. It is only safe to copy a state directly without using
255
+ * the clone function if the state is guaranteed to be properly aligned.
256
+ *
257
+ * A state can also be reset for reuse without cloning with `aegis128x4_mac_reset()`.
258
+ */
259
+ void aegis128x4_mac_init(aegis128x4_mac_state *st_, const uint8_t *k, const uint8_t *npub);
260
+
261
+ /*
262
+ * Update the MAC state with input data.
263
+ *
264
+ * st_: state to update
265
+ * m: input data
266
+ * mlen: length of the input data
267
+ *
268
+ * This function can be called multiple times.
269
+ *
270
+ * Once the full input has been absorbed, call either `_mac_final` or `_mac_verify`.
271
+ */
272
+ int aegis128x4_mac_update(aegis128x4_mac_state *st_, const uint8_t *m, size_t mlen);
273
+
274
+ /*
275
+ * Finalize the MAC and generate the authentication tag.
276
+ *
277
+ * st_: state to finalize
278
+ * mac: authentication tag output buffer
279
+ * maclen: length of the authentication tag to generate (16 or 32. 32 is recommended).
280
+ */
281
+ int aegis128x4_mac_final(aegis128x4_mac_state *st_, uint8_t *mac, size_t maclen);
282
+
283
+ /*
284
+ * Verify a MAC in constant time.
285
+ *
286
+ * st_: state to verify
287
+ * mac: authentication tag to verify
288
+ * maclen: length of the authentication tag (16 or 32)
289
+ *
290
+ * Returns 0 if the tag is authentic, -1 otherwise.
291
+ */
292
+ int aegis128x4_mac_verify(aegis128x4_mac_state *st_, const uint8_t *mac, size_t maclen);
293
+
294
+ /*
295
+ * Reset an AEGIS-MAC state.
296
+ */
297
+ void aegis128x4_mac_reset(aegis128x4_mac_state *st_);
298
+
299
+ /*
300
+ * Clone an AEGIS-MAC state.
301
+ *
302
+ * dst: destination state
303
+ * src: source state
304
+ *
305
+ * This function MUST be used in order to clone states.
306
+ */
307
+ void aegis128x4_mac_state_clone(aegis128x4_mac_state *dst, const aegis128x4_mac_state *src);
308
+
309
+ #ifdef __cplusplus
310
+ }
311
+ #endif
312
+
313
+ #endif
@@ -0,0 +1,311 @@
1
+ #ifndef aegis256_H
2
+ #define aegis256_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif
10
+
11
+ /* The length of an AEGIS key, in bytes */
12
+ #define aegis256_KEYBYTES 32
13
+
14
+ /* The length of an AEGIS nonce, in bytes */
15
+ #define aegis256_NPUBBYTES 32
16
+
17
+ /* The minimum length of an AEGIS authentication tag, in bytes */
18
+ #define aegis256_ABYTES_MIN 16
19
+
20
+ /* The maximum length of an AEGIS authentication tag, in bytes */
21
+ #define aegis256_ABYTES_MAX 32
22
+
23
+ /*
24
+ * Maximum number of leftover bytes at finalization (always 0).
25
+ * This constant is kept for backwards compatibility but is no longer needed
26
+ * since update functions now output data immediately without buffering.
27
+ */
28
+ #define aegis256_TAILBYTES_MAX 15
29
+
30
+ /* An AEGIS state, for incremental updates */
31
+ typedef struct aegis256_state {
32
+ CRYPTO_ALIGN(16) uint8_t opaque[192];
33
+ } aegis256_state;
34
+
35
+ /* An AEGIS state, only for MAC updates */
36
+ typedef struct aegis256_mac_state {
37
+ CRYPTO_ALIGN(16) uint8_t opaque[288];
38
+ } aegis256_mac_state;
39
+
40
+ /* The length of an AEGIS key, in bytes */
41
+ size_t aegis256_keybytes(void);
42
+
43
+ /* The length of an AEGIS nonce, in bytes */
44
+ size_t aegis256_npubbytes(void);
45
+
46
+ /* The minimum length of an AEGIS authentication tag, in bytes */
47
+ size_t aegis256_abytes_min(void);
48
+
49
+ /* The maximum length of an AEGIS authentication tag, in bytes */
50
+ size_t aegis256_abytes_max(void);
51
+
52
+ /*
53
+ * Maximum number of leftover bytes at finalization (always 0).
54
+ * This function is kept for backwards compatibility but always returns 15.
55
+ * Update functions now output data immediately without buffering.
56
+ */
57
+ size_t aegis256_tailbytes_max(void);
58
+
59
+ /*
60
+ * Encrypt a message with AEGIS in one shot mode, returning the tag and the ciphertext separately.
61
+ *
62
+ * c: ciphertext output buffer
63
+ * mac: authentication tag output buffer
64
+ * maclen: length of the authentication tag to generate (16 or 32)
65
+ * m: plaintext input buffer
66
+ * mlen: length of the plaintext
67
+ * ad: additional data input buffer
68
+ * adlen: length of the additional data
69
+ * npub: nonce input buffer (32 bytes)
70
+ * k: key input buffer (32 bytes)
71
+ */
72
+ int aegis256_encrypt_detached(uint8_t *c, uint8_t *mac, size_t maclen, const uint8_t *m,
73
+ size_t mlen, const uint8_t *ad, size_t adlen, const uint8_t *npub,
74
+ const uint8_t *k);
75
+
76
+ /*
77
+ * Decrypt a message with AEGIS in one shot mode with a detached tag.
78
+ *
79
+ * m: plaintext output buffer
80
+ * c: ciphertext input buffer
81
+ * clen: length of the ciphertext
82
+ * mac: authentication tag input buffer
83
+ * maclen: length of the authentication tag (16 or 32)
84
+ * ad: additional data input buffer
85
+ * adlen: length of the additional data
86
+ * npub: nonce input buffer (32 bytes)
87
+ * k: key input buffer (32 bytes)
88
+ *
89
+ * Returns 0 if the ciphertext is authentic, -1 otherwise.
90
+ */
91
+ AEGIS_WARN_UNUSED_RESULT
92
+ int aegis256_decrypt_detached(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *mac,
93
+ size_t maclen, const uint8_t *ad, size_t adlen, const uint8_t *npub,
94
+ const uint8_t *k);
95
+
96
+ /*
97
+ * Encrypt a message with AEGIS in one shot mode, returning the tag and the ciphertext together.
98
+ *
99
+ * c: ciphertext output buffer
100
+ * maclen: length of the authentication tag to generate (16 or 32)
101
+ * m: plaintext input buffer
102
+ * mlen: length of the plaintext
103
+ * ad: additional data input buffer
104
+ * adlen: length of the additional data
105
+ * npub: nonce input buffer (32 bytes)
106
+ * k: key input buffer (32 bytes)
107
+ */
108
+ int aegis256_encrypt(uint8_t *c, size_t maclen, const uint8_t *m, size_t mlen, const uint8_t *ad,
109
+ size_t adlen, const uint8_t *npub, const uint8_t *k);
110
+
111
+ /*
112
+ * Decrypt a message with AEGIS in one shot mode, returning the tag and the ciphertext together.
113
+ *
114
+ * m: plaintext output buffer
115
+ * c: ciphertext input buffer
116
+ * clen: length of the ciphertext
117
+ * maclen: length of the authentication tag (16 or 32)
118
+ * ad: additional data input buffer
119
+ * adlen: length of the additional data
120
+ * npub: nonce input buffer (32 bytes)
121
+ * k: key input buffer (32 bytes)
122
+ *
123
+ * Returns 0 if the ciphertext is authentic, -1 otherwise.
124
+ */
125
+ AEGIS_WARN_UNUSED_RESULT
126
+ int aegis256_decrypt(uint8_t *m, const uint8_t *c, size_t clen, size_t maclen, const uint8_t *ad,
127
+ size_t adlen, const uint8_t *npub, const uint8_t *k);
128
+
129
+ /*
130
+ * Initialize a state for incremental encryption or decryption.
131
+ *
132
+ * st_: state to initialize
133
+ * ad: additional data input buffer
134
+ * adlen: length of the additional data
135
+ * npub: nonce input buffer (32 bytes)
136
+ * k: key input buffer (32 bytes)
137
+ */
138
+ void aegis256_state_init(aegis256_state *st_, const uint8_t *ad, size_t adlen, const uint8_t *npub,
139
+ const uint8_t *k);
140
+
141
+ /*
142
+ * Encrypt a message chunk.
143
+ * The same function can be used regardless of whether the tag will be attached or not.
144
+ *
145
+ * st_: state to update
146
+ * c: ciphertext output buffer (must be at least mlen bytes)
147
+ * m: plaintext input buffer
148
+ * mlen: length of the plaintext
149
+ *
150
+ * Return 0 on success, -1 on failure.
151
+ */
152
+ int aegis256_state_encrypt_update(aegis256_state *st_, uint8_t *c, const uint8_t *m, size_t mlen);
153
+
154
+ /*
155
+ * Finalize the incremental encryption and generate the authentication tag.
156
+ *
157
+ * st_: state to finalize
158
+ * mac: output buffer for the authentication tag
159
+ * maclen: length of the authentication tag to generate (16 or 32)
160
+ *
161
+ * Return 0 on success, -1 on failure.
162
+ */
163
+ int aegis256_state_encrypt_final(aegis256_state *st_, uint8_t *mac, size_t maclen);
164
+
165
+ /*
166
+ * Decrypt a message chunk.
167
+ *
168
+ * The decrypted message must not be used until state_decrypt_final() has been
169
+ * called and returns successfully.
170
+ *
171
+ * st_: state to update
172
+ * m: plaintext output buffer (must be at least clen bytes)
173
+ * c: ciphertext chunk input buffer
174
+ * clen: length of the ciphertext chunk
175
+ *
176
+ * Return 0 on success, -1 on failure.
177
+ */
178
+ AEGIS_WARN_UNUSED_RESULT
179
+ int aegis256_state_decrypt_update(aegis256_state *st_, uint8_t *m, const uint8_t *c, size_t clen);
180
+
181
+ /*
182
+ * Finalize the incremental decryption and verify the authentication tag.
183
+ *
184
+ * This function verifies the authentication tag. It must be called after all
185
+ * decryption updates, and the decrypted message must not be used until this
186
+ * function returns successfully.
187
+ *
188
+ * st_: state to finalize
189
+ * mac: authentication tag input buffer
190
+ * maclen: length of the authentication tag (16 or 32)
191
+ *
192
+ * Return 0 if the tag is valid, -1 otherwise.
193
+ */
194
+ AEGIS_WARN_UNUSED_RESULT
195
+ int aegis256_state_decrypt_final(aegis256_state *st_, const uint8_t *mac, size_t maclen);
196
+
197
+ /*
198
+ * Return a deterministic pseudo-random byte sequence.
199
+ *
200
+ * out: output buffer
201
+ * len: number of bytes to generate
202
+ * npub: nonce input buffer (32 bytes) - Can be set to `NULL` if only one sequence has to be
203
+ * generated from a given key.
204
+ * k: key input buffer (32 bytes)
205
+ */
206
+ void aegis256_stream(uint8_t *out, size_t len, const uint8_t *npub, const uint8_t *k);
207
+
208
+ /*
209
+ * Encrypt a message WITHOUT AUTHENTICATION, similar to AES-CTR.
210
+ *
211
+ * WARNING: this is an insecure mode of operation, provided for compatibility with specific
212
+ * protocols that bring their own authentication scheme.
213
+ *
214
+ * c: ciphertext output buffer
215
+ * m: plaintext input buffer
216
+ * mlen: length of the plaintext
217
+ * npub: nonce input buffer (32 bytes)
218
+ * k: key input buffer (32 bytes)
219
+ */
220
+ void aegis256_encrypt_unauthenticated(uint8_t *c, const uint8_t *m, size_t mlen,
221
+ const uint8_t *npub, const uint8_t *k);
222
+
223
+ /*
224
+ * Decrypt a message WITHOUT AUTHENTICATION, similar to AES-CTR.
225
+ *
226
+ * WARNING: this is an insecure mode of operation, provided for compatibility with specific
227
+ * protocols that bring their own authentication scheme.
228
+ *
229
+ * m: plaintext output buffer
230
+ * c: ciphertext input buffer
231
+ * clen: length of the ciphertext
232
+ * npub: nonce input buffer (32 bytes)
233
+ * k: key input buffer (32 bytes)
234
+ */
235
+ void aegis256_decrypt_unauthenticated(uint8_t *m, const uint8_t *c, size_t clen,
236
+ const uint8_t *npub, const uint8_t *k);
237
+
238
+ /*
239
+ * Initialize a state for generating a MAC.
240
+ *
241
+ * st_: state to initialize
242
+ * k: key input buffer (32 bytes)
243
+ * npub: nonce input buffer (32 bytes) - Can be set to `NULL` to use an all-zero nonce
244
+ *
245
+ * - The same key MUST NOT be used both for MAC and encryption.
246
+ * - If the key is secret, the MAC is secure against forgery.
247
+ * - However, if the key is known, arbitrary inputs matching a tag can be efficiently computed.
248
+ *
249
+ * The recommended way to use the MAC mode is to generate a random key and keep it secret.
250
+ *
251
+ * After initialization, the state can be reused to generate multiple MACs by cloning it
252
+ * with `aegis256_mac_state_clone()`. It is only safe to copy a state directly without using
253
+ * the clone function if the state is guaranteed to be properly aligned.
254
+ *
255
+ * A state can also be reset for reuse without cloning with `aegis256_mac_reset()`.
256
+ */
257
+ void aegis256_mac_init(aegis256_mac_state *st_, const uint8_t *k, const uint8_t *npub);
258
+
259
+ /*
260
+ * Update the MAC state with input data.
261
+ *
262
+ * st_: state to update
263
+ * m: input data
264
+ * mlen: length of the input data
265
+ *
266
+ * This function can be called multiple times.
267
+ *
268
+ * Once the full input has been absorbed, call either `_mac_final` or `_mac_verify`.
269
+ */
270
+ int aegis256_mac_update(aegis256_mac_state *st_, const uint8_t *m, size_t mlen);
271
+
272
+ /*
273
+ * Finalize the MAC and generate the authentication tag.
274
+ *
275
+ * st_: state to finalize
276
+ * mac: authentication tag output buffer
277
+ * maclen: length of the authentication tag to generate (16 or 32. 32 is recommended).
278
+ */
279
+ int aegis256_mac_final(aegis256_mac_state *st_, uint8_t *mac, size_t maclen);
280
+
281
+ /*
282
+ * Verify a MAC in constant time.
283
+ *
284
+ * st_: state to verify
285
+ * mac: authentication tag to verify
286
+ * maclen: length of the authentication tag (16 or 32)
287
+ *
288
+ * Returns 0 if the tag is authentic, -1 otherwise.
289
+ */
290
+ int aegis256_mac_verify(aegis256_mac_state *st_, const uint8_t *mac, size_t maclen);
291
+
292
+ /*
293
+ * Reset an AEGIS-MAC state.
294
+ */
295
+ void aegis256_mac_reset(aegis256_mac_state *st_);
296
+
297
+ /*
298
+ * Clone an AEGIS-MAC state.
299
+ *
300
+ * dst: destination state
301
+ * src: source state
302
+ *
303
+ * This function MUST be used in order to clone states.
304
+ */
305
+ void aegis256_mac_state_clone(aegis256_mac_state *dst, const aegis256_mac_state *src);
306
+
307
+ #ifdef __cplusplus
308
+ }
309
+ #endif
310
+
311
+ #endif