aegis 0.9.8

AEGIS authenticated ciphers (AEGIS-128, AEGIS-256, AEGIS-128X, AEGIS-256X)
Documentation
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
use super::AesBlock;
pub use crate::Error;

/// AEGIS-256 key
pub type Key = [u8; 32];

/// AEGIS-256 nonce
pub type Nonce = [u8; 32];

#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
struct State {
    blocks: [AesBlock; 6],
}

impl State {
    #[inline(always)]
    fn update(&mut self, d: AesBlock) {
        let blocks = &mut self.blocks;
        let tmp = blocks[5];
        blocks[5] = blocks[4].round(blocks[5]);
        blocks[4] = blocks[3].round(blocks[4]);
        blocks[3] = blocks[2].round(blocks[3]);
        blocks[2] = blocks[1].round(blocks[2]);
        blocks[1] = blocks[0].round(blocks[1]);
        blocks[0] = tmp.round(blocks[0]).xor(d);
    }

    #[inline(always)]
    pub fn new(key: &Key, nonce: &Nonce) -> Self {
        let c0 = AesBlock::from_bytes(&[
            0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d, 0x15, 0x22, 0x37, 0x59, 0x90, 0xe9,
            0x79, 0x62,
        ]);
        let c1 = AesBlock::from_bytes(&[
            0xdb, 0x3d, 0x18, 0x55, 0x6d, 0xc2, 0x2f, 0xf1, 0x20, 0x11, 0x31, 0x42, 0x73, 0xb5,
            0x28, 0xdd,
        ]);
        let key_blocks = (
            AesBlock::from_bytes(&key[0..16]),
            AesBlock::from_bytes(&key[16..32]),
        );
        let nonce_blocks = (
            AesBlock::from_bytes(&nonce[0..16]),
            AesBlock::from_bytes(&nonce[16..32]),
        );
        let kn_blocks = (
            key_blocks.0.xor(nonce_blocks.0),
            key_blocks.1.xor(nonce_blocks.1),
        );
        let blocks: [AesBlock; 6] = [
            kn_blocks.0,
            kn_blocks.1,
            c1,
            c0,
            key_blocks.0.xor(c0),
            key_blocks.1.xor(c1),
        ];
        let mut state = State { blocks };
        for _ in 0..4 {
            state.update(key_blocks.0);
            state.update(key_blocks.1);
            state.update(kn_blocks.0);
            state.update(kn_blocks.1);
        }
        state
    }

    #[inline(always)]
    fn absorb(&mut self, src: &[u8; 16]) {
        let msg = AesBlock::from_bytes(src);
        self.update(msg);
    }

    #[inline(always)]
    fn enc(&mut self, dst: &mut [u8; 16], src: &[u8; 16]) {
        let blocks = &self.blocks;
        let z = blocks[5]
            .xor(blocks[4])
            .xor(blocks[1])
            .xor(blocks[2].and(blocks[3]));
        let msg = AesBlock::from_bytes(src);
        let c = msg.xor(z);
        dst.copy_from_slice(&c.to_bytes());
        self.update(msg);
    }

    #[inline(always)]
    fn dec(&mut self, dst: &mut [u8; 16], src: &[u8; 16]) {
        let blocks = &self.blocks;
        let z = blocks[5]
            .xor(blocks[4])
            .xor(blocks[1])
            .xor(blocks[2].and(blocks[3]));
        let msg = AesBlock::from_bytes(src).xor(z);
        dst.copy_from_slice(&msg.to_bytes());
        self.update(msg);
    }

    #[inline(always)]
    fn dec_partial(&mut self, dst: &mut [u8; 16], src: &[u8]) {
        let len = src.len();
        let mut src_padded = [0u8; 16];
        src_padded[..len].copy_from_slice(src);

        let blocks = &self.blocks;
        let z = blocks[5]
            .xor(blocks[4])
            .xor(blocks[1])
            .xor(blocks[2].and(blocks[3]));
        let msg_padded = AesBlock::from_bytes(&src_padded).xor(z);

        dst.copy_from_slice(&msg_padded.to_bytes());
        dst[len..].fill(0);

        let msg = AesBlock::from_bytes(dst);
        self.update(msg);
    }

    #[inline(always)]
    fn mac<const TAG_BYTES: usize>(&mut self, adlen: usize, mlen: usize) -> Tag<TAG_BYTES> {
        let tmp = {
            let blocks = &self.blocks;
            let mut sizes = [0u8; 16];
            sizes[..8].copy_from_slice(&(adlen as u64 * 8).to_le_bytes());
            sizes[8..16].copy_from_slice(&(mlen as u64 * 8).to_le_bytes());
            AesBlock::from_bytes(&sizes).xor(blocks[3])
        };
        for _ in 0..7 {
            self.update(tmp);
        }
        let blocks = &self.blocks;
        let mut tag = [0u8; TAG_BYTES];
        match TAG_BYTES {
            16 => tag.copy_from_slice(
                &blocks[0]
                    .xor(blocks[1])
                    .xor(blocks[2])
                    .xor(blocks[3])
                    .xor(blocks[4])
                    .xor(blocks[5])
                    .to_bytes(),
            ),
            32 => {
                tag[..16].copy_from_slice(&blocks[0].xor(blocks[1]).xor(blocks[2]).to_bytes());
                tag[16..].copy_from_slice(&blocks[3].xor(blocks[4]).xor(blocks[5]).to_bytes());
            }
            _ => unreachable!(),
        }
        tag
    }

    #[inline(always)]
    fn mac_finalize<const TAG_BYTES: usize>(&mut self, data_len: usize) -> Tag<TAG_BYTES> {
        let tmp = {
            let blocks = &self.blocks;
            let mut sizes = [0u8; 16];
            sizes[..8].copy_from_slice(&(data_len as u64 * 8).to_le_bytes());
            sizes[8..16].copy_from_slice(&(TAG_BYTES as u64 * 8).to_le_bytes());
            AesBlock::from_bytes(&sizes).xor(blocks[3])
        };
        for _ in 0..7 {
            self.update(tmp);
        }
        let blocks = &self.blocks;
        let mut tag = [0u8; TAG_BYTES];
        match TAG_BYTES {
            16 => tag.copy_from_slice(
                &blocks[0]
                    .xor(blocks[1])
                    .xor(blocks[2])
                    .xor(blocks[3])
                    .xor(blocks[4])
                    .xor(blocks[5])
                    .to_bytes(),
            ),
            32 => {
                tag[..16].copy_from_slice(&blocks[0].xor(blocks[1]).xor(blocks[2]).to_bytes());
                tag[16..].copy_from_slice(&blocks[3].xor(blocks[4]).xor(blocks[5]).to_bytes());
            }
            _ => unreachable!(),
        }
        tag
    }
}

/// Tag length in bits must be 128 or 256
#[repr(transparent)]
pub struct Aegis256<const TAG_BYTES: usize>(State);

/// AEGIS-256 authentication tag
pub type Tag<const TAG_BYTES: usize> = [u8; TAG_BYTES];

impl<const TAG_BYTES: usize> Aegis256<TAG_BYTES> {
    /// Create a new AEAD instance.
    /// `key` and `nonce` must be 16 bytes long.
    pub fn new(key: &Key, nonce: &Nonce) -> Self {
        assert!(
            TAG_BYTES == 16 || TAG_BYTES == 32,
            "Invalid tag length, must be 16 or 32"
        );
        Aegis256(State::new(key, nonce))
    }

    /// Encrypts a message using AEGIS-256
    /// # Arguments
    /// * `m` - Message
    /// * `ad` - Associated data
    /// # Returns
    /// Encrypted message and authentication tag.
    #[cfg(feature = "std")]
    pub fn encrypt(mut self, m: &[u8], ad: &[u8]) -> (Vec<u8>, Tag<TAG_BYTES>) {
        let state = &mut self.0;
        let mlen = m.len();
        let adlen = ad.len();
        let mut c = Vec::with_capacity(mlen);
        let mut src = [0u8; 16];
        let mut dst = [0u8; 16];
        let mut i = 0;
        while i + 16 <= adlen {
            src.copy_from_slice(&ad[i..][..16]);
            state.absorb(&src);
            i += 16;
        }
        if adlen % 16 != 0 {
            src.fill(0);
            src[..adlen % 16].copy_from_slice(&ad[i..]);
            state.absorb(&src);
        }
        i = 0;
        while i + 16 <= mlen {
            src.copy_from_slice(&m[i..][..16]);
            state.enc(&mut dst, &src);
            c.extend_from_slice(&dst);
            i += 16;
        }
        if mlen % 16 != 0 {
            src.fill(0);
            src[..mlen % 16].copy_from_slice(&m[i..]);
            state.enc(&mut dst, &src);
            c.extend_from_slice(&dst[..mlen % 16]);
        }
        let tag = state.mac::<TAG_BYTES>(adlen, mlen);
        (c, tag)
    }

    /// Encrypts a message in-place using AEGIS-256
    /// # Arguments
    /// * `mc` - Input and output buffer
    /// * `ad` - Associated data
    /// # Returns
    /// Encrypted message and authentication tag.
    pub fn encrypt_in_place(mut self, mc: &mut [u8], ad: &[u8]) -> Tag<TAG_BYTES> {
        let state = &mut self.0;
        let mclen = mc.len();
        let adlen = ad.len();
        let mut src = [0u8; 16];
        let mut dst = [0u8; 16];
        let mut i = 0;
        while i + 16 <= adlen {
            src.copy_from_slice(&ad[i..][..16]);
            state.absorb(&src);
            i += 16;
        }
        if adlen % 16 != 0 {
            src.fill(0);
            src[..adlen % 16].copy_from_slice(&ad[i..]);
            state.absorb(&src);
        }
        i = 0;
        while i + 16 <= mclen {
            src.copy_from_slice(&mc[i..][..16]);
            state.enc(&mut dst, &src);
            mc[i..][..16].copy_from_slice(&dst);
            i += 16;
        }
        if mclen % 16 != 0 {
            src.fill(0);
            src[..mclen % 16].copy_from_slice(&mc[i..]);
            state.enc(&mut dst, &src);
            mc[i..].copy_from_slice(&dst[..mclen % 16]);
        }

        state.mac::<TAG_BYTES>(adlen, mclen)
    }

    /// Decrypts a message using AEGIS-256
    /// # Arguments
    /// * `c` - Ciphertext
    /// * `tag` - Authentication tag
    /// * `ad` - Associated data
    /// # Returns
    /// Decrypted message.
    #[cfg(feature = "std")]
    pub fn decrypt(mut self, c: &[u8], tag: &Tag<TAG_BYTES>, ad: &[u8]) -> Result<Vec<u8>, Error> {
        let state = &mut self.0;
        let clen = c.len();
        let adlen = ad.len();
        let mut m = Vec::with_capacity(clen);
        let mut src = [0u8; 16];
        let mut dst = [0u8; 16];
        let mut i = 0;
        while i + 16 <= adlen {
            src.copy_from_slice(&ad[i..][..16]);
            state.enc(&mut dst, &src);
            i += 16;
        }
        if adlen % 16 != 0 {
            src.fill(0);
            src[..adlen % 16].copy_from_slice(&ad[i..]);
            state.enc(&mut dst, &src);
        }
        i = 0;
        while i + 16 <= clen {
            src.copy_from_slice(&c[i..][..16]);
            state.dec(&mut dst, &src);
            m.extend_from_slice(&dst);
            i += 16;
        }
        if clen % 16 != 0 {
            state.dec_partial(&mut dst, &c[i..]);
            m.extend_from_slice(&dst[0..clen % 16]);
        }
        let tag2 = state.mac::<TAG_BYTES>(adlen, clen);
        let mut acc = 0;
        for (a, b) in tag.iter().zip(tag2.iter()) {
            acc |= a ^ b;
        }
        if acc != 0 {
            m.fill(0xaa);
            return Err(Error::InvalidTag);
        }
        Ok(m)
    }

    /// Decrypts a message in-place using AEGIS-256
    /// # Arguments
    /// * `mc` - Input and output buffer
    /// * `tag` - Authentication tag
    /// * `ad` - Associated data
    pub fn decrypt_in_place(
        mut self,
        mc: &mut [u8],
        tag: &Tag<TAG_BYTES>,
        ad: &[u8],
    ) -> Result<(), Error> {
        let state = &mut self.0;
        let mclen = mc.len();
        let adlen = ad.len();
        let mut src = [0u8; 16];
        let mut dst = [0u8; 16];
        let mut i = 0;
        while i + 16 <= adlen {
            src.copy_from_slice(&ad[i..][..16]);
            state.enc(&mut dst, &src);
            i += 16;
        }
        if adlen % 16 != 0 {
            src.fill(0);
            src[..adlen % 16].copy_from_slice(&ad[i..]);
            state.enc(&mut dst, &src);
        }
        i = 0;
        while i + 16 <= mclen {
            src.copy_from_slice(&mc[i..][..16]);
            state.dec(&mut dst, &src);
            mc[i..][..16].copy_from_slice(&dst);
            i += 16;
        }
        if mclen % 16 != 0 {
            state.dec_partial(&mut dst, &mc[i..]);
            mc[i..].copy_from_slice(&dst[0..mclen % 16]);
        }
        let tag2 = state.mac::<TAG_BYTES>(adlen, mclen);
        let mut acc = 0;
        for (a, b) in tag.iter().zip(tag2.iter()) {
            acc |= a ^ b;
        }
        if acc != 0 {
            mc.fill(0xaa);
            return Err(Error::InvalidTag);
        }
        Ok(())
    }
}

/// AEGIS-256 MAC with incremental update support.
///
/// The state can be cloned to authenticate multiple messages with the same key.
///
/// 256-bit output tags are recommended for security.
///
/// Note that AEGIS is not a hash function. It is a MAC that requires a secret key.
/// Inputs leading to a state collision can be efficiently computed if the key is known.
#[derive(Clone)]
pub struct Aegis256Mac<const TAG_BYTES: usize> {
    state: State,
    buf: [u8; 16],
    buf_len: usize,
    msg_len: usize,
}

impl<const TAG_BYTES: usize> Aegis256Mac<TAG_BYTES> {
    /// Initializes the MAC state with a key.
    ///
    /// The state can be cloned to authenticate multiple messages with the same key.
    pub fn new(key: &Key) -> Self {
        let nonce = [0u8; 32];
        Self::new_with_nonce(key, &nonce)
    }

    /// Initializes the MAC state with a key and a nonce.
    ///
    /// The state can be cloned to authenticate multiple messages with the same key.
    pub fn new_with_nonce(key: &Key, nonce: &Nonce) -> Self {
        assert!(
            TAG_BYTES == 16 || TAG_BYTES == 32,
            "Invalid tag length, must be 16 or 32"
        );
        Aegis256Mac {
            state: State::new(key, nonce),
            buf: [0u8; 16],
            buf_len: 0,
            msg_len: 0,
        }
    }

    /// Updates the MAC state with a message.
    ///
    /// This function can be called multiple times to update the MAC state with additional data.
    pub fn update(&mut self, data: &[u8]) {
        self.msg_len += data.len();
        let mut offset = 0;

        // Process buffered data first
        if self.buf_len > 0 {
            let needed = 16 - self.buf_len;
            if data.len() < needed {
                self.buf[self.buf_len..self.buf_len + data.len()].copy_from_slice(data);
                self.buf_len += data.len();
                return;
            }
            self.buf[self.buf_len..].copy_from_slice(&data[..needed]);
            self.state.absorb(&self.buf);
            self.buf_len = 0;
            offset = needed;
        }

        // Process full blocks
        while offset + 16 <= data.len() {
            let mut block = [0u8; 16];
            block.copy_from_slice(&data[offset..offset + 16]);
            self.state.absorb(&block);
            offset += 16;
        }

        // Buffer remaining
        if offset < data.len() {
            self.buf_len = data.len() - offset;
            self.buf[..self.buf_len].copy_from_slice(&data[offset..]);
        }
    }

    /// Finalizes the MAC and returns the authentication tag.
    pub fn finalize(mut self) -> Tag<TAG_BYTES> {
        if self.buf_len > 0 || self.msg_len == 0 {
            self.buf[self.buf_len..].fill(0);
            self.state.absorb(&self.buf);
        }
        self.state.mac_finalize::<TAG_BYTES>(self.msg_len)
    }

    /// Verifies the authentication tag.
    pub fn verify(self, expected: &Tag<TAG_BYTES>) -> Result<(), Error> {
        let tag = self.finalize();
        let mut acc = 0u8;
        for (a, b) in tag.iter().zip(expected.iter()) {
            acc |= a ^ b;
        }
        if acc != 0 {
            return Err(Error::InvalidTag);
        }
        Ok(())
    }
}