Skip to content

Commit 8da94b3

Browse files
ebiggersherbertx
authored andcommitted
crypto: x86/aegis128 - improve assembly function prototypes
Adjust the prototypes of the AEGIS assembly functions: - Use proper types instead of 'void *', when applicable. - Move the length parameter to after the buffers it describes rather than before, to match the usual convention. Also shorten its name to just len (which is the name used in the assembly code). - Declare register aliases at the beginning of each function rather than once per file. This was necessary because len was moved, but also it allows adding some aliases where raw registers were used before. - Put assoclen and cryptlen in the correct order when declaring the finalization function in the .c file. - Remove the unnecessary "crypto_" prefix. Reviewed-by: Ondrej Mosnacek <[email protected]> Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent af2aff7 commit 8da94b3

File tree

2 files changed

+112
-85
lines changed

2 files changed

+112
-85
lines changed

arch/x86/crypto/aegis128-aesni-asm.S

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
#define T0 %xmm6
2020
#define T1 %xmm7
2121

22-
#define STATEP %rdi
23-
#define LEN %esi
24-
#define SRC %rdx
25-
#define DST %rcx
26-
2722
.section .rodata.cst16.aegis128_const, "aM", @progbits, 32
2823
.align 16
2924
.Laegis128_const_0:
@@ -72,6 +67,8 @@
7267
* %r9
7368
*/
7469
SYM_FUNC_START_LOCAL(__load_partial)
70+
.set LEN, %ecx
71+
.set SRC, %rsi
7572
xor %r9d, %r9d
7673
pxor MSG, MSG
7774

@@ -138,6 +135,8 @@ SYM_FUNC_END(__load_partial)
138135
* %r10
139136
*/
140137
SYM_FUNC_START_LOCAL(__store_partial)
138+
.set LEN, %ecx
139+
.set DST, %rdx
141140
mov LEN, %r8d
142141
mov DST, %r9
143142

@@ -184,16 +183,21 @@ SYM_FUNC_START_LOCAL(__store_partial)
184183
SYM_FUNC_END(__store_partial)
185184

186185
/*
187-
* void crypto_aegis128_aesni_init(void *state, const void *key, const void *iv);
186+
* void aegis128_aesni_init(struct aegis_state *state,
187+
* const struct aegis_block *key,
188+
* const u8 iv[AEGIS128_NONCE_SIZE]);
188189
*/
189-
SYM_FUNC_START(crypto_aegis128_aesni_init)
190+
SYM_FUNC_START(aegis128_aesni_init)
191+
.set STATEP, %rdi
192+
.set KEYP, %rsi
193+
.set IVP, %rdx
190194
FRAME_BEGIN
191195

192196
/* load IV: */
193-
movdqu (%rdx), T1
197+
movdqu (IVP), T1
194198

195199
/* load key: */
196-
movdqa (%rsi), KEY
200+
movdqa (KEYP), KEY
197201
pxor KEY, T1
198202
movdqa T1, STATE0
199203
movdqa KEY, STATE3
@@ -226,13 +230,16 @@ SYM_FUNC_START(crypto_aegis128_aesni_init)
226230

227231
FRAME_END
228232
RET
229-
SYM_FUNC_END(crypto_aegis128_aesni_init)
233+
SYM_FUNC_END(aegis128_aesni_init)
230234

231235
/*
232-
* void crypto_aegis128_aesni_ad(void *state, unsigned int length,
233-
* const void *data);
236+
* void aegis128_aesni_ad(struct aegis_state *state, const u8 *data,
237+
* unsigned int len);
234238
*/
235-
SYM_FUNC_START(crypto_aegis128_aesni_ad)
239+
SYM_FUNC_START(aegis128_aesni_ad)
240+
.set STATEP, %rdi
241+
.set SRC, %rsi
242+
.set LEN, %edx
236243
FRAME_BEGIN
237244

238245
cmp $0x10, LEN
@@ -334,7 +341,7 @@ SYM_FUNC_START(crypto_aegis128_aesni_ad)
334341
.Lad_out:
335342
FRAME_END
336343
RET
337-
SYM_FUNC_END(crypto_aegis128_aesni_ad)
344+
SYM_FUNC_END(aegis128_aesni_ad)
338345

339346
.macro encrypt_block s0 s1 s2 s3 s4 i
340347
movdqu (\i * 0x10)(SRC), MSG
@@ -355,10 +362,14 @@ SYM_FUNC_END(crypto_aegis128_aesni_ad)
355362
.endm
356363

357364
/*
358-
* void crypto_aegis128_aesni_enc(void *state, unsigned int length,
359-
* const void *src, void *dst);
365+
* void aegis128_aesni_enc(struct aegis_state *state, const u8 *src, u8 *dst,
366+
* unsigned int len);
360367
*/
361-
SYM_FUNC_START(crypto_aegis128_aesni_enc)
368+
SYM_FUNC_START(aegis128_aesni_enc)
369+
.set STATEP, %rdi
370+
.set SRC, %rsi
371+
.set DST, %rdx
372+
.set LEN, %ecx
362373
FRAME_BEGIN
363374

364375
cmp $0x10, LEN
@@ -432,13 +443,17 @@ SYM_FUNC_START(crypto_aegis128_aesni_enc)
432443
.Lenc_out:
433444
FRAME_END
434445
RET
435-
SYM_FUNC_END(crypto_aegis128_aesni_enc)
446+
SYM_FUNC_END(aegis128_aesni_enc)
436447

437448
/*
438-
* void crypto_aegis128_aesni_enc_tail(void *state, unsigned int length,
439-
* const void *src, void *dst);
449+
* void aegis128_aesni_enc_tail(struct aegis_state *state, const u8 *src,
450+
* u8 *dst, unsigned int len);
440451
*/
441-
SYM_FUNC_START(crypto_aegis128_aesni_enc_tail)
452+
SYM_FUNC_START(aegis128_aesni_enc_tail)
453+
.set STATEP, %rdi
454+
.set SRC, %rsi
455+
.set DST, %rdx
456+
.set LEN, %ecx
442457
FRAME_BEGIN
443458

444459
/* load the state: */
@@ -472,7 +487,7 @@ SYM_FUNC_START(crypto_aegis128_aesni_enc_tail)
472487

473488
FRAME_END
474489
RET
475-
SYM_FUNC_END(crypto_aegis128_aesni_enc_tail)
490+
SYM_FUNC_END(aegis128_aesni_enc_tail)
476491

477492
.macro decrypt_block s0 s1 s2 s3 s4 i
478493
movdqu (\i * 0x10)(SRC), MSG
@@ -492,10 +507,14 @@ SYM_FUNC_END(crypto_aegis128_aesni_enc_tail)
492507
.endm
493508

494509
/*
495-
* void crypto_aegis128_aesni_dec(void *state, unsigned int length,
496-
* const void *src, void *dst);
510+
* void aegis128_aesni_dec(struct aegis_state *state, const u8 *src, u8 *dst,
511+
* unsigned int len);
497512
*/
498-
SYM_FUNC_START(crypto_aegis128_aesni_dec)
513+
SYM_FUNC_START(aegis128_aesni_dec)
514+
.set STATEP, %rdi
515+
.set SRC, %rsi
516+
.set DST, %rdx
517+
.set LEN, %ecx
499518
FRAME_BEGIN
500519

501520
cmp $0x10, LEN
@@ -569,13 +588,17 @@ SYM_FUNC_START(crypto_aegis128_aesni_dec)
569588
.Ldec_out:
570589
FRAME_END
571590
RET
572-
SYM_FUNC_END(crypto_aegis128_aesni_dec)
591+
SYM_FUNC_END(aegis128_aesni_dec)
573592

574593
/*
575-
* void crypto_aegis128_aesni_dec_tail(void *state, unsigned int length,
576-
* const void *src, void *dst);
594+
* void aegis128_aesni_dec_tail(struct aegis_state *state, const u8 *src,
595+
* u8 *dst, unsigned int len);
577596
*/
578-
SYM_FUNC_START(crypto_aegis128_aesni_dec_tail)
597+
SYM_FUNC_START(aegis128_aesni_dec_tail)
598+
.set STATEP, %rdi
599+
.set SRC, %rsi
600+
.set DST, %rdx
601+
.set LEN, %ecx
579602
FRAME_BEGIN
580603

581604
/* load the state: */
@@ -619,14 +642,18 @@ SYM_FUNC_START(crypto_aegis128_aesni_dec_tail)
619642

620643
FRAME_END
621644
RET
622-
SYM_FUNC_END(crypto_aegis128_aesni_dec_tail)
645+
SYM_FUNC_END(aegis128_aesni_dec_tail)
623646

624647
/*
625-
* void crypto_aegis128_aesni_final(void *state, void *tag_xor,
626-
* unsigned int assoclen,
627-
* unsigned int cryptlen);
648+
* void aegis128_aesni_final(struct aegis_state *state,
649+
* struct aegis_block *tag_xor,
650+
* unsigned int assoclen, unsigned int cryptlen);
628651
*/
629-
SYM_FUNC_START(crypto_aegis128_aesni_final)
652+
SYM_FUNC_START(aegis128_aesni_final)
653+
.set STATEP, %rdi
654+
.set TAG_XOR, %rsi
655+
.set ASSOCLEN, %edx
656+
.set CRYPTLEN, %ecx
630657
FRAME_BEGIN
631658

632659
/* load the state: */
@@ -637,8 +664,8 @@ SYM_FUNC_START(crypto_aegis128_aesni_final)
637664
movdqu 0x40(STATEP), STATE4
638665

639666
/* prepare length block: */
640-
movd %edx, MSG
641-
pinsrd $2, %ecx, MSG
667+
movd ASSOCLEN, MSG
668+
pinsrd $2, CRYPTLEN, MSG
642669
psllq $3, MSG /* multiply by 8 (to get bit count) */
643670

644671
pxor STATE3, MSG
@@ -653,16 +680,16 @@ SYM_FUNC_START(crypto_aegis128_aesni_final)
653680
aegis128_update; pxor MSG, STATE3
654681

655682
/* xor tag: */
656-
movdqu (%rsi), MSG
683+
movdqu (TAG_XOR), MSG
657684

658685
pxor STATE0, MSG
659686
pxor STATE1, MSG
660687
pxor STATE2, MSG
661688
pxor STATE3, MSG
662689
pxor STATE4, MSG
663690

664-
movdqu MSG, (%rsi)
691+
movdqu MSG, (TAG_XOR)
665692

666693
FRAME_END
667694
RET
668-
SYM_FUNC_END(crypto_aegis128_aesni_final)
695+
SYM_FUNC_END(aegis128_aesni_final)

arch/x86/crypto/aegis128-aesni-glue.c

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,6 @@
2323
#define AEGIS128_MIN_AUTH_SIZE 8
2424
#define AEGIS128_MAX_AUTH_SIZE 16
2525

26-
asmlinkage void crypto_aegis128_aesni_init(void *state, void *key, void *iv);
27-
28-
asmlinkage void crypto_aegis128_aesni_ad(
29-
void *state, unsigned int length, const void *data);
30-
31-
asmlinkage void crypto_aegis128_aesni_enc(
32-
void *state, unsigned int length, const void *src, void *dst);
33-
34-
asmlinkage void crypto_aegis128_aesni_dec(
35-
void *state, unsigned int length, const void *src, void *dst);
36-
37-
asmlinkage void crypto_aegis128_aesni_enc_tail(
38-
void *state, unsigned int length, const void *src, void *dst);
39-
40-
asmlinkage void crypto_aegis128_aesni_dec_tail(
41-
void *state, unsigned int length, const void *src, void *dst);
42-
43-
asmlinkage void crypto_aegis128_aesni_final(
44-
void *state, void *tag_xor, unsigned int cryptlen,
45-
unsigned int assoclen);
46-
4726
struct aegis_block {
4827
u8 bytes[AEGIS128_BLOCK_SIZE] __aligned(AEGIS128_BLOCK_ALIGN);
4928
};
@@ -56,6 +35,32 @@ struct aegis_ctx {
5635
struct aegis_block key;
5736
};
5837

38+
asmlinkage void aegis128_aesni_init(struct aegis_state *state,
39+
const struct aegis_block *key,
40+
const u8 iv[AEGIS128_NONCE_SIZE]);
41+
42+
asmlinkage void aegis128_aesni_ad(struct aegis_state *state, const u8 *data,
43+
unsigned int len);
44+
45+
asmlinkage void aegis128_aesni_enc(struct aegis_state *state, const u8 *src,
46+
u8 *dst, unsigned int len);
47+
48+
asmlinkage void aegis128_aesni_dec(struct aegis_state *state, const u8 *src,
49+
u8 *dst, unsigned int len);
50+
51+
asmlinkage void aegis128_aesni_enc_tail(struct aegis_state *state,
52+
const u8 *src, u8 *dst,
53+
unsigned int len);
54+
55+
asmlinkage void aegis128_aesni_dec_tail(struct aegis_state *state,
56+
const u8 *src, u8 *dst,
57+
unsigned int len);
58+
59+
asmlinkage void aegis128_aesni_final(struct aegis_state *state,
60+
struct aegis_block *tag_xor,
61+
unsigned int assoclen,
62+
unsigned int cryptlen);
63+
5964
static void crypto_aegis128_aesni_process_ad(
6065
struct aegis_state *state, struct scatterlist *sg_src,
6166
unsigned int assoclen)
@@ -75,15 +80,14 @@ static void crypto_aegis128_aesni_process_ad(
7580
if (pos > 0) {
7681
unsigned int fill = AEGIS128_BLOCK_SIZE - pos;
7782
memcpy(buf.bytes + pos, src, fill);
78-
crypto_aegis128_aesni_ad(state,
79-
AEGIS128_BLOCK_SIZE,
80-
buf.bytes);
83+
aegis128_aesni_ad(state, buf.bytes,
84+
AEGIS128_BLOCK_SIZE);
8185
pos = 0;
8286
left -= fill;
8387
src += fill;
8488
}
8589

86-
crypto_aegis128_aesni_ad(state, left, src);
90+
aegis128_aesni_ad(state, src, left);
8791

8892
src += left & ~(AEGIS128_BLOCK_SIZE - 1);
8993
left &= AEGIS128_BLOCK_SIZE - 1;
@@ -100,7 +104,7 @@ static void crypto_aegis128_aesni_process_ad(
100104

101105
if (pos > 0) {
102106
memset(buf.bytes + pos, 0, AEGIS128_BLOCK_SIZE - pos);
103-
crypto_aegis128_aesni_ad(state, AEGIS128_BLOCK_SIZE, buf.bytes);
107+
aegis128_aesni_ad(state, buf.bytes, AEGIS128_BLOCK_SIZE);
104108
}
105109
}
106110

@@ -110,31 +114,27 @@ crypto_aegis128_aesni_process_crypt(struct aegis_state *state,
110114
{
111115
while (walk->nbytes >= AEGIS128_BLOCK_SIZE) {
112116
if (enc)
113-
crypto_aegis128_aesni_enc(
114-
state,
115-
round_down(walk->nbytes,
116-
AEGIS128_BLOCK_SIZE),
117-
walk->src.virt.addr,
118-
walk->dst.virt.addr);
117+
aegis128_aesni_enc(state, walk->src.virt.addr,
118+
walk->dst.virt.addr,
119+
round_down(walk->nbytes,
120+
AEGIS128_BLOCK_SIZE));
119121
else
120-
crypto_aegis128_aesni_dec(
121-
state,
122-
round_down(walk->nbytes,
123-
AEGIS128_BLOCK_SIZE),
124-
walk->src.virt.addr,
125-
walk->dst.virt.addr);
122+
aegis128_aesni_dec(state, walk->src.virt.addr,
123+
walk->dst.virt.addr,
124+
round_down(walk->nbytes,
125+
AEGIS128_BLOCK_SIZE));
126126
skcipher_walk_done(walk, walk->nbytes % AEGIS128_BLOCK_SIZE);
127127
}
128128

129129
if (walk->nbytes) {
130130
if (enc)
131-
crypto_aegis128_aesni_enc_tail(state, walk->nbytes,
132-
walk->src.virt.addr,
133-
walk->dst.virt.addr);
131+
aegis128_aesni_enc_tail(state, walk->src.virt.addr,
132+
walk->dst.virt.addr,
133+
walk->nbytes);
134134
else
135-
crypto_aegis128_aesni_dec_tail(state, walk->nbytes,
136-
walk->src.virt.addr,
137-
walk->dst.virt.addr);
135+
aegis128_aesni_dec_tail(state, walk->src.virt.addr,
136+
walk->dst.virt.addr,
137+
walk->nbytes);
138138
skcipher_walk_done(walk, 0);
139139
}
140140
}
@@ -186,10 +186,10 @@ crypto_aegis128_aesni_crypt(struct aead_request *req,
186186

187187
kernel_fpu_begin();
188188

189-
crypto_aegis128_aesni_init(&state, ctx->key.bytes, req->iv);
189+
aegis128_aesni_init(&state, &ctx->key, req->iv);
190190
crypto_aegis128_aesni_process_ad(&state, req->src, req->assoclen);
191191
crypto_aegis128_aesni_process_crypt(&state, &walk, enc);
192-
crypto_aegis128_aesni_final(&state, tag_xor, req->assoclen, cryptlen);
192+
aegis128_aesni_final(&state, tag_xor, req->assoclen, cryptlen);
193193

194194
kernel_fpu_end();
195195
}

0 commit comments

Comments
 (0)