1
1
package aead
2
2
3
+ import " core:crypto/aegis"
3
4
import " core:crypto/aes"
4
5
import " core:crypto/chacha20"
5
6
import " core:crypto/chacha20poly1305"
@@ -15,7 +16,7 @@ Implementation :: union {
15
16
16
17
// MAX_TAG_SIZE is the maximum size tag that can be returned by any of the
17
18
// Algorithms supported via this package.
18
- MAX_TAG_SIZE :: 16
19
+ MAX_TAG_SIZE :: 32
19
20
20
21
// Algorithm is the algorithm identifier associated with a given Context.
21
22
Algorithm :: enum {
@@ -25,16 +26,24 @@ Algorithm :: enum {
25
26
AES_GCM_256,
26
27
CHACHA20POLY1305,
27
28
XCHACHA20POLY1305,
29
+ AEGIS_128L,
30
+ AEGIS_128L_256, // AEGIS-128L (256-bit tag)
31
+ AEGIS_256,
32
+ AEGIS_256_256, // AEGIS-256 (256-bit tag)
28
33
}
29
34
30
- // ALGORITM_NAMES is the Agorithm to algorithm name string.
35
+ // ALGORITM_NAMES is the Algorithm to algorithm name string.
31
36
ALGORITHM_NAMES := [Algorithm]string {
32
37
.Invalid = " Invalid" ,
33
38
.AES_GCM_128 = " AES-GCM-128" ,
34
39
.AES_GCM_192 = " AES-GCM-192" ,
35
40
.AES_GCM_256 = " AES-GCM-256" ,
36
41
.CHACHA20POLY1305 = " chacha20poly1305" ,
37
42
.XCHACHA20POLY1305 = " xchacha20poly1305" ,
43
+ .AEGIS_128L = " AEGIS-128L" ,
44
+ .AEGIS_128L_256 = " AEGIS-128L-256" ,
45
+ .AEGIS_256 = " AEGIS-256" ,
46
+ .AEGIS_256_256 = " AEGIS-256-256" ,
38
47
}
39
48
40
49
// TAG_SIZES is the Algorithm to tag size in bytes.
@@ -45,6 +54,10 @@ TAG_SIZES := [Algorithm]int {
45
54
.AES_GCM_256 = aes.GCM_TAG_SIZE,
46
55
.CHACHA20POLY1305 = chacha20poly1305.TAG_SIZE,
47
56
.XCHACHA20POLY1305 = chacha20poly1305.TAG_SIZE,
57
+ .AEGIS_128L = aegis.TAG_SIZE_128,
58
+ .AEGIS_128L_256 = aegis.TAG_SIZE_256,
59
+ .AEGIS_256 = aegis.TAG_SIZE_128,
60
+ .AEGIS_256_256 = aegis.TAG_SIZE_256,
48
61
}
49
62
50
63
// KEY_SIZES is the Algorithm to key size in bytes.
@@ -55,6 +68,10 @@ KEY_SIZES := [Algorithm]int {
55
68
.AES_GCM_256 = aes.KEY_SIZE_256,
56
69
.CHACHA20POLY1305 = chacha20poly1305.KEY_SIZE,
57
70
.XCHACHA20POLY1305 = chacha20poly1305.KEY_SIZE,
71
+ .AEGIS_128L = aegis.KEY_SIZE_128L,
72
+ .AEGIS_128L_256 = aegis.KEY_SIZE_128L,
73
+ .AEGIS_256 = aegis.KEY_SIZE_256,
74
+ .AEGIS_256_256 = aegis.KEY_SIZE_256,
58
75
}
59
76
60
77
// IV_SIZES is the Algorithm to initialization vector size in bytes.
@@ -67,6 +84,10 @@ IV_SIZES := [Algorithm]int {
67
84
.AES_GCM_256 = aes.GCM_IV_SIZE,
68
85
.CHACHA20POLY1305 = chacha20poly1305.IV_SIZE,
69
86
.XCHACHA20POLY1305 = chacha20poly1305.XIV_SIZE,
87
+ .AEGIS_128L = aegis.IV_SIZE_128L,
88
+ .AEGIS_128L_256 = aegis.IV_SIZE_128L,
89
+ .AEGIS_256 = aegis.IV_SIZE_256,
90
+ .AEGIS_256_256 = aegis.IV_SIZE_256,
70
91
}
71
92
72
93
// Context is a concrete instantiation of a specific AEAD algorithm.
@@ -75,6 +96,7 @@ Context :: struct {
75
96
_impl: union {
76
97
aes.Context_GCM,
77
98
chacha20poly1305.Context,
99
+ aegis.Context,
78
100
},
79
101
}
80
102
@@ -86,6 +108,10 @@ _IMPL_IDS := [Algorithm]typeid {
86
108
.AES_GCM_256 = typeid_of (aes.Context_GCM),
87
109
.CHACHA20POLY1305 = typeid_of (chacha20poly1305.Context),
88
110
.XCHACHA20POLY1305 = typeid_of (chacha20poly1305.Context),
111
+ .AEGIS_128L = typeid_of (aegis.Context),
112
+ .AEGIS_128L_256 = typeid_of (aegis.Context),
113
+ .AEGIS_256 = typeid_of (aegis.Context),
114
+ .AEGIS_256_256 = typeid_of (aegis.Context),
89
115
}
90
116
91
117
// init initializes a Context with a specific AEAD Algorithm.
@@ -113,6 +139,9 @@ init :: proc(ctx: ^Context, algorithm: Algorithm, key: []byte, impl: Implementat
113
139
case .XCHACHA20POLY1305:
114
140
impl_ := impl != nil ? impl.(chacha20.Implementation) : chacha20.DEFAULT_IMPLEMENTATION
115
141
chacha20poly1305.init_xchacha (&ctx._impl.(chacha20poly1305.Context), key, impl_)
142
+ case .AEGIS_128L, .AEGIS_128L_256, .AEGIS_256, .AEGIS_256_256:
143
+ impl_ := impl != nil ? impl.(aes.Implementation) : aes.DEFAULT_IMPLEMENTATION
144
+ aegis.init (&ctx._impl.(aegis.Context), key, impl_)
116
145
case .Invalid:
117
146
panic (" crypto/aead: uninitialized algorithm" )
118
147
case :
@@ -127,11 +156,17 @@ init :: proc(ctx: ^Context, algorithm: Algorithm, key: []byte, impl: Implementat
127
156
//
128
157
// dst and plaintext MUST alias exactly or not at all.
129
158
seal_ctx :: proc (ctx: ^Context, dst, tag, iv, aad, plaintext: []byte ) {
159
+ if len (tag) != TAG_SIZES[ctx._algo] {
160
+ panic (" crypto/aead: invalid tag size" )
161
+ }
162
+
130
163
switch &impl in ctx._impl {
131
164
case aes.Context_GCM:
132
165
aes.seal_gcm (&impl, dst, tag, iv, aad, plaintext)
133
166
case chacha20poly1305.Context:
134
167
chacha20poly1305.seal (&impl, dst, tag, iv, aad, plaintext)
168
+ case aegis.Context:
169
+ aegis.seal (&impl, dst, tag, iv, aad, plaintext)
135
170
case :
136
171
panic (" crypto/aead: uninitialized algorithm" )
137
172
}
@@ -145,11 +180,17 @@ seal_ctx :: proc(ctx: ^Context, dst, tag, iv, aad, plaintext: []byte) {
145
180
// dst and plaintext MUST alias exactly or not at all.
146
181
@(require_results)
147
182
open_ctx :: proc (ctx: ^Context, dst, iv, aad, ciphertext, tag: []byte ) -> bool {
183
+ if len (tag) != TAG_SIZES[ctx._algo] {
184
+ panic (" crypto/aead: invalid tag size" )
185
+ }
186
+
148
187
switch &impl in ctx._impl {
149
188
case aes.Context_GCM:
150
189
return aes.open_gcm (&impl, dst, iv, aad, ciphertext, tag)
151
190
case chacha20poly1305.Context:
152
191
return chacha20poly1305.open (&impl, dst, iv, aad, ciphertext, tag)
192
+ case aegis.Context:
193
+ return aegis.open (&impl, dst, iv, aad, ciphertext, tag)
153
194
case :
154
195
panic (" crypto/aead: uninitialized algorithm" )
155
196
}
@@ -163,6 +204,8 @@ reset :: proc(ctx: ^Context) {
163
204
aes.reset_gcm (&impl)
164
205
case chacha20poly1305.Context:
165
206
chacha20poly1305.reset (&impl)
207
+ case aegis.Context:
208
+ aegis.reset (&impl)
166
209
case :
167
210
// Calling reset repeatedly is fine.
168
211
}
0 commit comments