Skip to content

Commit 1a9fb21

Browse files
committed
Move remaining variants to the new MAC API and construction
1 parent 471873e commit 1a9fb21

28 files changed

+463
-92
lines changed

src/aegis128x2/aegis128x2_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ typedef struct _aegis128x2_state {
407407
} _aegis128x2_state;
408408

409409
typedef struct _aegis128x2_mac_state {
410-
aegis_blocks blocks0;
411410
aegis_blocks blocks;
411+
aegis_blocks blocks0;
412412
uint8_t buf[RATE];
413413
uint64_t adlen;
414414
size_t pos;

src/aegis128x4/aegis128x4_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ typedef struct _aegis128x4_state {
422422
} _aegis128x4_state;
423423

424424
typedef struct _aegis128x4_mac_state {
425-
aegis_blocks blocks0;
426425
aegis_blocks blocks;
426+
aegis_blocks blocks0;
427427
uint8_t buf[RATE];
428428
uint64_t adlen;
429429
size_t pos;

src/aegis256/aegis256_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ typedef struct _aegis256_state {
289289
} _aegis256_state;
290290

291291
typedef struct _aegis256_mac_state {
292-
aegis_blocks blocks0;
293292
aegis_blocks blocks;
293+
aegis_blocks blocks0;
294294
uint8_t buf[RATE];
295295
uint64_t adlen;
296296
size_t pos;

src/aegis256x2/aegis256x2.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,19 @@ aegis256x2_decrypt_unauthenticated(uint8_t *m, const uint8_t *c, size_t clen, co
176176
}
177177

178178
void
179-
aegis256x2_mac_init(aegis256x2_state *st_, const uint8_t *k, const uint8_t *npub)
179+
aegis256x2_mac_init(aegis256x2_mac_state *st_, const uint8_t *k, const uint8_t *npub)
180180
{
181-
implementation->state_init(st_, NULL, 0, npub, k);
181+
implementation->state_mac_init(st_, npub, k);
182182
}
183183

184184
int
185-
aegis256x2_mac_update(aegis256x2_state *st_, const uint8_t *m, size_t mlen)
185+
aegis256x2_mac_update(aegis256x2_mac_state *st_, const uint8_t *m, size_t mlen)
186186
{
187187
return implementation->state_mac_update(st_, m, mlen);
188188
}
189189

190190
int
191-
aegis256x2_mac_final(aegis256x2_state *st_, uint8_t *mac, size_t maclen)
191+
aegis256x2_mac_final(aegis256x2_mac_state *st_, uint8_t *mac, size_t maclen)
192192
{
193193
if (maclen != 16 && maclen != 32) {
194194
errno = EINVAL;
@@ -198,7 +198,7 @@ aegis256x2_mac_final(aegis256x2_state *st_, uint8_t *mac, size_t maclen)
198198
}
199199

200200
int
201-
aegis256x2_mac_verify(aegis256x2_state *st_, const uint8_t *mac, size_t maclen)
201+
aegis256x2_mac_verify(aegis256x2_mac_state *st_, const uint8_t *mac, size_t maclen)
202202
{
203203
uint8_t expected_mac[32];
204204

@@ -216,9 +216,15 @@ aegis256x2_mac_verify(aegis256x2_state *st_, const uint8_t *mac, size_t maclen)
216216
}
217217

218218
void
219-
aegis256x2_mac_state_clone(aegis256x2_state *dst, const aegis256x2_state *src)
219+
aegis256x2_mac_reset(aegis256x2_mac_state *st_)
220220
{
221-
implementation->state_clone(dst, src);
221+
implementation->state_mac_reset(st_);
222+
}
223+
224+
void
225+
aegis256x2_mac_state_clone(aegis256x2_mac_state *dst, const aegis256x2_mac_state *src)
226+
{
227+
implementation->state_mac_clone(dst, src);
222228
}
223229

224230
int

src/aegis256x2/aegis256x2_aesni.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ struct aegis256x2_implementation aegis256x2_aesni_implementation = {
9393
.state_encrypt_final = state_encrypt_final,
9494
.state_decrypt_detached_update = state_decrypt_detached_update,
9595
.state_decrypt_detached_final = state_decrypt_detached_final,
96+
.state_mac_init = state_mac_init,
9697
.state_mac_update = state_mac_update,
9798
.state_mac_final = state_mac_final,
98-
.state_clone = state_clone,
99+
.state_mac_reset = state_mac_reset,
100+
.state_mac_clone = state_mac_clone,
99101
};
100102

101103
# ifdef __clang__

src/aegis256x2/aegis256x2_altivec.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ struct aegis256x2_implementation aegis256x2_altivec_implementation = {
9191
.state_encrypt_final = state_encrypt_final,
9292
.state_decrypt_detached_update = state_decrypt_detached_update,
9393
.state_decrypt_detached_final = state_decrypt_detached_final,
94+
.state_mac_init = state_mac_init,
9495
.state_mac_update = state_mac_update,
9596
.state_mac_final = state_mac_final,
96-
.state_clone = state_clone,
97+
.state_mac_reset = state_mac_reset,
98+
.state_mac_clone = state_mac_clone,
9799
};
98100

99101
# ifdef __clang__

src/aegis256x2/aegis256x2_armcrypto.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ struct aegis256x2_implementation aegis256x2_armcrypto_implementation = {
9999
.state_encrypt_final = state_encrypt_final,
100100
.state_decrypt_detached_update = state_decrypt_detached_update,
101101
.state_decrypt_detached_final = state_decrypt_detached_final,
102+
.state_mac_init = state_mac_init,
102103
.state_mac_update = state_mac_update,
103104
.state_mac_final = state_mac_final,
104-
.state_clone = state_clone,
105+
.state_mac_reset = state_mac_reset,
106+
.state_mac_clone = state_mac_clone,
105107
};
106108

107109
# ifdef __clang__

src/aegis256x2/aegis256x2_avx2.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ struct aegis256x2_implementation aegis256x2_avx2_implementation = {
6161
.state_encrypt_final = state_encrypt_final,
6262
.state_decrypt_detached_update = state_decrypt_detached_update,
6363
.state_decrypt_detached_final = state_decrypt_detached_final,
64+
.state_mac_init = state_mac_init,
6465
.state_mac_update = state_mac_update,
6566
.state_mac_final = state_mac_final,
66-
.state_clone = state_clone,
67+
.state_mac_reset = state_mac_reset,
68+
.state_mac_clone = state_mac_clone,
6769
};
6870

6971
# ifdef __clang__

src/aegis256x2/aegis256x2_common.h

Lines changed: 125 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,74 @@ aegis256x2_declast(uint8_t *const dst, const uint8_t *const src, size_t len,
178178
aegis256x2_update(state, msg);
179179
}
180180

181+
static void
182+
aegis256x2_mac_nr(uint8_t *mac, size_t maclen, uint64_t adlen, aes_block_t *state)
183+
{
184+
uint8_t t[2 * AES_BLOCK_LENGTH];
185+
uint8_t r[RATE];
186+
aes_block_t tmp;
187+
int i;
188+
const int d = AES_BLOCK_LENGTH / 16;
189+
190+
tmp = AES_BLOCK_LOAD_64x2(0, adlen << 3);
191+
tmp = AES_BLOCK_XOR(tmp, state[3]);
192+
193+
for (i = 0; i < 7; i++) {
194+
aegis256x2_update(state, tmp);
195+
}
196+
197+
memset(r, 0, sizeof r);
198+
if (maclen == 16) {
199+
#if AES_BLOCK_LENGTH > 16
200+
tmp = AES_BLOCK_XOR(state[5], state[4]);
201+
tmp = AES_BLOCK_XOR(tmp, AES_BLOCK_XOR(state[3], state[2]));
202+
tmp = AES_BLOCK_XOR(tmp, AES_BLOCK_XOR(state[1], state[0]));
203+
AES_BLOCK_STORE(t, tmp);
204+
205+
for (i = 1; i < d; i++) {
206+
memcpy(r, t + i * 16, 16);
207+
aegis256x2_absorb(r, state);
208+
}
209+
tmp = AES_BLOCK_LOAD_64x2(d, maclen);
210+
tmp = AES_BLOCK_XOR(tmp, state[3]);
211+
for (i = 0; i < 7; i++) {
212+
aegis256x2_update(state, tmp);
213+
}
214+
#endif
215+
tmp = AES_BLOCK_XOR(state[5], state[4]);
216+
tmp = AES_BLOCK_XOR(tmp, AES_BLOCK_XOR(state[3], state[2]));
217+
tmp = AES_BLOCK_XOR(tmp, AES_BLOCK_XOR(state[1], state[0]));
218+
AES_BLOCK_STORE(t, tmp);
219+
memcpy(mac, t, 16);
220+
} else if (maclen == 32) {
221+
#if AES_BLOCK_LENGTH > 16
222+
tmp = AES_BLOCK_XOR(state[2], AES_BLOCK_XOR(state[1], state[0]));
223+
AES_BLOCK_STORE(t, tmp);
224+
tmp = AES_BLOCK_XOR(state[5], AES_BLOCK_XOR(state[4], state[3]));
225+
AES_BLOCK_STORE(t + AES_BLOCK_LENGTH, tmp);
226+
for (i = 1; i < d; i++) {
227+
memcpy(r, t + i * 16, 16);
228+
aegis256x2_absorb(r, state);
229+
memcpy(r, t + AES_BLOCK_LENGTH + i * 16, 16);
230+
aegis256x2_absorb(r, state);
231+
}
232+
tmp = AES_BLOCK_LOAD_64x2(d, maclen);
233+
tmp = AES_BLOCK_XOR(tmp, state[3]);
234+
for (i = 0; i < 7; i++) {
235+
aegis256x2_update(state, tmp);
236+
}
237+
#endif
238+
tmp = AES_BLOCK_XOR(state[2], AES_BLOCK_XOR(state[1], state[0]));
239+
AES_BLOCK_STORE(t, tmp);
240+
memcpy(mac, t, 16);
241+
tmp = AES_BLOCK_XOR(state[5], AES_BLOCK_XOR(state[4], state[3]));
242+
AES_BLOCK_STORE(t, tmp);
243+
memcpy(mac + 16, t, 16);
244+
} else {
245+
memset(mac, 0, maclen);
246+
}
247+
}
248+
181249
static int
182250
encrypt_detached(uint8_t *c, uint8_t *mac, size_t maclen, const uint8_t *m, size_t mlen,
183251
const uint8_t *ad, size_t adlen, const uint8_t *npub, const uint8_t *k)
@@ -337,6 +405,14 @@ typedef struct _aegis256x2_state {
337405
size_t pos;
338406
} _aegis256x2_state;
339407

408+
typedef struct _aegis256x2_mac_state {
409+
aegis_blocks blocks;
410+
aegis_blocks blocks0;
411+
uint8_t buf[RATE];
412+
uint64_t adlen;
413+
size_t pos;
414+
} _aegis256x2_mac_state;
415+
340416
static void
341417
state_init(aegis256x2_state *st_, const uint8_t *ad, size_t adlen, const uint8_t *npub,
342418
const uint8_t *k)
@@ -605,13 +681,33 @@ state_decrypt_detached_final(aegis256x2_state *st_, uint8_t *m, size_t mlen_max,
605681
return ret;
606682
}
607683

684+
static void
685+
state_mac_init(aegis256x2_mac_state *st_, const uint8_t *npub, const uint8_t *k)
686+
{
687+
aegis_blocks blocks;
688+
_aegis256x2_mac_state *const st =
689+
(_aegis256x2_mac_state *) ((((uintptr_t) &st_->opaque) + (ALIGNMENT - 1)) &
690+
~(uintptr_t) (ALIGNMENT - 1));
691+
692+
COMPILER_ASSERT((sizeof *st) + ALIGNMENT <= sizeof *st_);
693+
st->pos = 0;
694+
695+
memcpy(blocks, st->blocks, sizeof blocks);
696+
697+
aegis256x2_init(k, npub, blocks);
698+
699+
memcpy(st->blocks0, blocks, sizeof blocks);
700+
memcpy(st->blocks, blocks, sizeof blocks);
701+
st->adlen = 0;
702+
}
703+
608704
static int
609-
state_mac_update(aegis256x2_state *st_, const uint8_t *ad, size_t adlen)
705+
state_mac_update(aegis256x2_mac_state *st_, const uint8_t *ad, size_t adlen)
610706
{
611-
aegis_blocks blocks;
612-
_aegis256x2_state *const st =
613-
(_aegis256x2_state *) ((((uintptr_t) &st_->opaque) + (ALIGNMENT - 1)) &
614-
~(uintptr_t) (ALIGNMENT - 1));
707+
aegis_blocks blocks;
708+
_aegis256x2_mac_state *const st =
709+
(_aegis256x2_mac_state *) ((((uintptr_t) &st_->opaque) + (ALIGNMENT - 1)) &
710+
~(uintptr_t) (ALIGNMENT - 1));
615711
size_t i;
616712
size_t left;
617713

@@ -653,12 +749,12 @@ state_mac_update(aegis256x2_state *st_, const uint8_t *ad, size_t adlen)
653749
}
654750

655751
static int
656-
state_mac_final(aegis256x2_state *st_, uint8_t *mac, size_t maclen)
752+
state_mac_final(aegis256x2_mac_state *st_, uint8_t *mac, size_t maclen)
657753
{
658-
aegis_blocks blocks;
659-
_aegis256x2_state *const st =
660-
(_aegis256x2_state *) ((((uintptr_t) &st_->opaque) + (ALIGNMENT - 1)) &
661-
~(uintptr_t) (ALIGNMENT - 1));
754+
aegis_blocks blocks;
755+
_aegis256x2_mac_state *const st =
756+
(_aegis256x2_mac_state *) ((((uintptr_t) &st_->opaque) + (ALIGNMENT - 1)) &
757+
~(uintptr_t) (ALIGNMENT - 1));
662758
size_t left;
663759

664760
memcpy(blocks, st->blocks, sizeof blocks);
@@ -668,21 +764,32 @@ state_mac_final(aegis256x2_state *st_, uint8_t *mac, size_t maclen)
668764
memset(st->buf + left, 0, RATE - left);
669765
aegis256x2_absorb(st->buf, blocks);
670766
}
671-
aegis256x2_mac(mac, maclen, st->adlen, 0, blocks);
767+
aegis256x2_mac_nr(mac, maclen, st->adlen, blocks);
672768

673769
memcpy(st->blocks, blocks, sizeof blocks);
674770

675771
return 0;
676772
}
677773

678774
static void
679-
state_clone(aegis256x2_state *dst, const aegis256x2_state *src)
775+
state_mac_reset(aegis256x2_mac_state *st_)
680776
{
681-
_aegis256x2_state *const dst_ =
682-
(_aegis256x2_state *) ((((uintptr_t) &dst->opaque) + (ALIGNMENT - 1)) &
683-
~(uintptr_t) (ALIGNMENT - 1));
684-
const _aegis256x2_state *const src_ =
685-
(const _aegis256x2_state *) ((((uintptr_t) &src->opaque) + (ALIGNMENT - 1)) &
686-
~(uintptr_t) (ALIGNMENT - 1));
777+
_aegis256x2_mac_state *const st =
778+
(_aegis256x2_mac_state *) ((((uintptr_t) &st_->opaque) + (ALIGNMENT - 1)) &
779+
~(uintptr_t) (ALIGNMENT - 1));
780+
st->adlen = 0;
781+
st->pos = 0;
782+
memcpy(st->blocks, st->blocks0, sizeof(aegis_blocks));
783+
}
784+
785+
static void
786+
state_mac_clone(aegis256x2_mac_state *dst, const aegis256x2_mac_state *src)
787+
{
788+
_aegis256x2_mac_state *const dst_ =
789+
(_aegis256x2_mac_state *) ((((uintptr_t) &dst->opaque) + (ALIGNMENT - 1)) &
790+
~(uintptr_t) (ALIGNMENT - 1));
791+
const _aegis256x2_mac_state *const src_ =
792+
(const _aegis256x2_mac_state *) ((((uintptr_t) &src->opaque) + (ALIGNMENT - 1)) &
793+
~(uintptr_t) (ALIGNMENT - 1));
687794
*dst_ = *src_;
688795
}

src/aegis256x2/aegis256x2_soft.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ struct aegis256x2_implementation aegis256x2_soft_implementation = {
8585
.state_encrypt_final = state_encrypt_final,
8686
.state_decrypt_detached_update = state_decrypt_detached_update,
8787
.state_decrypt_detached_final = state_decrypt_detached_final,
88+
.state_mac_init = state_mac_init,
8889
.state_mac_update = state_mac_update,
8990
.state_mac_final = state_mac_final,
90-
.state_clone = state_clone,
91+
.state_mac_reset = state_mac_reset,
92+
.state_mac_clone = state_mac_clone,
9193
};
9294

9395
#endif

src/aegis256x2/implementations.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ typedef struct aegis256x2_implementation {
2929
size_t *written, const uint8_t *c, size_t clen);
3030
int (*state_decrypt_detached_final)(aegis256x2_state *st_, uint8_t *m, size_t mlen_max,
3131
size_t *written, const uint8_t *mac, size_t maclen);
32-
int (*state_mac_update)(aegis256x2_state *st_, const uint8_t *ad, size_t adlen);
33-
int (*state_mac_final)(aegis256x2_state *st_, uint8_t *mac, size_t maclen);
34-
void (*state_clone)(aegis256x2_state *dst, const aegis256x2_state *src);
32+
void (*state_mac_init)(aegis256x2_mac_state *st_, const uint8_t *npub, const uint8_t *k);
33+
int (*state_mac_update)(aegis256x2_mac_state *st_, const uint8_t *ad, size_t adlen);
34+
int (*state_mac_final)(aegis256x2_mac_state *st_, uint8_t *mac, size_t maclen);
35+
void (*state_mac_reset)(aegis256x2_mac_state *st);
36+
void (*state_mac_clone)(aegis256x2_mac_state *dst, const aegis256x2_mac_state *src);
37+
3538
} aegis256x2_implementation;
3639

3740
#endif

0 commit comments

Comments
 (0)