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 <omosnace@redhat.com>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
Eric Biggers 2024-10-16 17:00:47 -07:00 committed by Herbert Xu
parent af2aff7caf
commit 8da94b300f
2 changed files with 112 additions and 85 deletions

View File

@ -19,11 +19,6 @@
#define T0 %xmm6
#define T1 %xmm7
#define STATEP %rdi
#define LEN %esi
#define SRC %rdx
#define DST %rcx
.section .rodata.cst16.aegis128_const, "aM", @progbits, 32
.align 16
.Laegis128_const_0:
@ -72,6 +67,8 @@
* %r9
*/
SYM_FUNC_START_LOCAL(__load_partial)
.set LEN, %ecx
.set SRC, %rsi
xor %r9d, %r9d
pxor MSG, MSG
@ -138,6 +135,8 @@ SYM_FUNC_END(__load_partial)
* %r10
*/
SYM_FUNC_START_LOCAL(__store_partial)
.set LEN, %ecx
.set DST, %rdx
mov LEN, %r8d
mov DST, %r9
@ -184,16 +183,21 @@ SYM_FUNC_START_LOCAL(__store_partial)
SYM_FUNC_END(__store_partial)
/*
* void crypto_aegis128_aesni_init(void *state, const void *key, const void *iv);
* void aegis128_aesni_init(struct aegis_state *state,
* const struct aegis_block *key,
* const u8 iv[AEGIS128_NONCE_SIZE]);
*/
SYM_FUNC_START(crypto_aegis128_aesni_init)
SYM_FUNC_START(aegis128_aesni_init)
.set STATEP, %rdi
.set KEYP, %rsi
.set IVP, %rdx
FRAME_BEGIN
/* load IV: */
movdqu (%rdx), T1
movdqu (IVP), T1
/* load key: */
movdqa (%rsi), KEY
movdqa (KEYP), KEY
pxor KEY, T1
movdqa T1, STATE0
movdqa KEY, STATE3
@ -226,13 +230,16 @@ SYM_FUNC_START(crypto_aegis128_aesni_init)
FRAME_END
RET
SYM_FUNC_END(crypto_aegis128_aesni_init)
SYM_FUNC_END(aegis128_aesni_init)
/*
* void crypto_aegis128_aesni_ad(void *state, unsigned int length,
* const void *data);
* void aegis128_aesni_ad(struct aegis_state *state, const u8 *data,
* unsigned int len);
*/
SYM_FUNC_START(crypto_aegis128_aesni_ad)
SYM_FUNC_START(aegis128_aesni_ad)
.set STATEP, %rdi
.set SRC, %rsi
.set LEN, %edx
FRAME_BEGIN
cmp $0x10, LEN
@ -334,7 +341,7 @@ SYM_FUNC_START(crypto_aegis128_aesni_ad)
.Lad_out:
FRAME_END
RET
SYM_FUNC_END(crypto_aegis128_aesni_ad)
SYM_FUNC_END(aegis128_aesni_ad)
.macro encrypt_block s0 s1 s2 s3 s4 i
movdqu (\i * 0x10)(SRC), MSG
@ -355,10 +362,14 @@ SYM_FUNC_END(crypto_aegis128_aesni_ad)
.endm
/*
* void crypto_aegis128_aesni_enc(void *state, unsigned int length,
* const void *src, void *dst);
* void aegis128_aesni_enc(struct aegis_state *state, const u8 *src, u8 *dst,
* unsigned int len);
*/
SYM_FUNC_START(crypto_aegis128_aesni_enc)
SYM_FUNC_START(aegis128_aesni_enc)
.set STATEP, %rdi
.set SRC, %rsi
.set DST, %rdx
.set LEN, %ecx
FRAME_BEGIN
cmp $0x10, LEN
@ -432,13 +443,17 @@ SYM_FUNC_START(crypto_aegis128_aesni_enc)
.Lenc_out:
FRAME_END
RET
SYM_FUNC_END(crypto_aegis128_aesni_enc)
SYM_FUNC_END(aegis128_aesni_enc)
/*
* void crypto_aegis128_aesni_enc_tail(void *state, unsigned int length,
* const void *src, void *dst);
* void aegis128_aesni_enc_tail(struct aegis_state *state, const u8 *src,
* u8 *dst, unsigned int len);
*/
SYM_FUNC_START(crypto_aegis128_aesni_enc_tail)
SYM_FUNC_START(aegis128_aesni_enc_tail)
.set STATEP, %rdi
.set SRC, %rsi
.set DST, %rdx
.set LEN, %ecx
FRAME_BEGIN
/* load the state: */
@ -472,7 +487,7 @@ SYM_FUNC_START(crypto_aegis128_aesni_enc_tail)
FRAME_END
RET
SYM_FUNC_END(crypto_aegis128_aesni_enc_tail)
SYM_FUNC_END(aegis128_aesni_enc_tail)
.macro decrypt_block s0 s1 s2 s3 s4 i
movdqu (\i * 0x10)(SRC), MSG
@ -492,10 +507,14 @@ SYM_FUNC_END(crypto_aegis128_aesni_enc_tail)
.endm
/*
* void crypto_aegis128_aesni_dec(void *state, unsigned int length,
* const void *src, void *dst);
* void aegis128_aesni_dec(struct aegis_state *state, const u8 *src, u8 *dst,
* unsigned int len);
*/
SYM_FUNC_START(crypto_aegis128_aesni_dec)
SYM_FUNC_START(aegis128_aesni_dec)
.set STATEP, %rdi
.set SRC, %rsi
.set DST, %rdx
.set LEN, %ecx
FRAME_BEGIN
cmp $0x10, LEN
@ -569,13 +588,17 @@ SYM_FUNC_START(crypto_aegis128_aesni_dec)
.Ldec_out:
FRAME_END
RET
SYM_FUNC_END(crypto_aegis128_aesni_dec)
SYM_FUNC_END(aegis128_aesni_dec)
/*
* void crypto_aegis128_aesni_dec_tail(void *state, unsigned int length,
* const void *src, void *dst);
* void aegis128_aesni_dec_tail(struct aegis_state *state, const u8 *src,
* u8 *dst, unsigned int len);
*/
SYM_FUNC_START(crypto_aegis128_aesni_dec_tail)
SYM_FUNC_START(aegis128_aesni_dec_tail)
.set STATEP, %rdi
.set SRC, %rsi
.set DST, %rdx
.set LEN, %ecx
FRAME_BEGIN
/* load the state: */
@ -619,14 +642,18 @@ SYM_FUNC_START(crypto_aegis128_aesni_dec_tail)
FRAME_END
RET
SYM_FUNC_END(crypto_aegis128_aesni_dec_tail)
SYM_FUNC_END(aegis128_aesni_dec_tail)
/*
* void crypto_aegis128_aesni_final(void *state, void *tag_xor,
* unsigned int assoclen,
* unsigned int cryptlen);
* void aegis128_aesni_final(struct aegis_state *state,
* struct aegis_block *tag_xor,
* unsigned int assoclen, unsigned int cryptlen);
*/
SYM_FUNC_START(crypto_aegis128_aesni_final)
SYM_FUNC_START(aegis128_aesni_final)
.set STATEP, %rdi
.set TAG_XOR, %rsi
.set ASSOCLEN, %edx
.set CRYPTLEN, %ecx
FRAME_BEGIN
/* load the state: */
@ -637,8 +664,8 @@ SYM_FUNC_START(crypto_aegis128_aesni_final)
movdqu 0x40(STATEP), STATE4
/* prepare length block: */
movd %edx, MSG
pinsrd $2, %ecx, MSG
movd ASSOCLEN, MSG
pinsrd $2, CRYPTLEN, MSG
psllq $3, MSG /* multiply by 8 (to get bit count) */
pxor STATE3, MSG
@ -653,7 +680,7 @@ SYM_FUNC_START(crypto_aegis128_aesni_final)
aegis128_update; pxor MSG, STATE3
/* xor tag: */
movdqu (%rsi), MSG
movdqu (TAG_XOR), MSG
pxor STATE0, MSG
pxor STATE1, MSG
@ -661,8 +688,8 @@ SYM_FUNC_START(crypto_aegis128_aesni_final)
pxor STATE3, MSG
pxor STATE4, MSG
movdqu MSG, (%rsi)
movdqu MSG, (TAG_XOR)
FRAME_END
RET
SYM_FUNC_END(crypto_aegis128_aesni_final)
SYM_FUNC_END(aegis128_aesni_final)

View File

@ -23,27 +23,6 @@
#define AEGIS128_MIN_AUTH_SIZE 8
#define AEGIS128_MAX_AUTH_SIZE 16
asmlinkage void crypto_aegis128_aesni_init(void *state, void *key, void *iv);
asmlinkage void crypto_aegis128_aesni_ad(
void *state, unsigned int length, const void *data);
asmlinkage void crypto_aegis128_aesni_enc(
void *state, unsigned int length, const void *src, void *dst);
asmlinkage void crypto_aegis128_aesni_dec(
void *state, unsigned int length, const void *src, void *dst);
asmlinkage void crypto_aegis128_aesni_enc_tail(
void *state, unsigned int length, const void *src, void *dst);
asmlinkage void crypto_aegis128_aesni_dec_tail(
void *state, unsigned int length, const void *src, void *dst);
asmlinkage void crypto_aegis128_aesni_final(
void *state, void *tag_xor, unsigned int cryptlen,
unsigned int assoclen);
struct aegis_block {
u8 bytes[AEGIS128_BLOCK_SIZE] __aligned(AEGIS128_BLOCK_ALIGN);
};
@ -56,6 +35,32 @@ struct aegis_ctx {
struct aegis_block key;
};
asmlinkage void aegis128_aesni_init(struct aegis_state *state,
const struct aegis_block *key,
const u8 iv[AEGIS128_NONCE_SIZE]);
asmlinkage void aegis128_aesni_ad(struct aegis_state *state, const u8 *data,
unsigned int len);
asmlinkage void aegis128_aesni_enc(struct aegis_state *state, const u8 *src,
u8 *dst, unsigned int len);
asmlinkage void aegis128_aesni_dec(struct aegis_state *state, const u8 *src,
u8 *dst, unsigned int len);
asmlinkage void aegis128_aesni_enc_tail(struct aegis_state *state,
const u8 *src, u8 *dst,
unsigned int len);
asmlinkage void aegis128_aesni_dec_tail(struct aegis_state *state,
const u8 *src, u8 *dst,
unsigned int len);
asmlinkage void aegis128_aesni_final(struct aegis_state *state,
struct aegis_block *tag_xor,
unsigned int assoclen,
unsigned int cryptlen);
static void crypto_aegis128_aesni_process_ad(
struct aegis_state *state, struct scatterlist *sg_src,
unsigned int assoclen)
@ -75,15 +80,14 @@ static void crypto_aegis128_aesni_process_ad(
if (pos > 0) {
unsigned int fill = AEGIS128_BLOCK_SIZE - pos;
memcpy(buf.bytes + pos, src, fill);
crypto_aegis128_aesni_ad(state,
AEGIS128_BLOCK_SIZE,
buf.bytes);
aegis128_aesni_ad(state, buf.bytes,
AEGIS128_BLOCK_SIZE);
pos = 0;
left -= fill;
src += fill;
}
crypto_aegis128_aesni_ad(state, left, src);
aegis128_aesni_ad(state, src, left);
src += left & ~(AEGIS128_BLOCK_SIZE - 1);
left &= AEGIS128_BLOCK_SIZE - 1;
@ -100,7 +104,7 @@ static void crypto_aegis128_aesni_process_ad(
if (pos > 0) {
memset(buf.bytes + pos, 0, AEGIS128_BLOCK_SIZE - pos);
crypto_aegis128_aesni_ad(state, AEGIS128_BLOCK_SIZE, buf.bytes);
aegis128_aesni_ad(state, buf.bytes, AEGIS128_BLOCK_SIZE);
}
}
@ -110,31 +114,27 @@ crypto_aegis128_aesni_process_crypt(struct aegis_state *state,
{
while (walk->nbytes >= AEGIS128_BLOCK_SIZE) {
if (enc)
crypto_aegis128_aesni_enc(
state,
round_down(walk->nbytes,
AEGIS128_BLOCK_SIZE),
walk->src.virt.addr,
walk->dst.virt.addr);
aegis128_aesni_enc(state, walk->src.virt.addr,
walk->dst.virt.addr,
round_down(walk->nbytes,
AEGIS128_BLOCK_SIZE));
else
crypto_aegis128_aesni_dec(
state,
round_down(walk->nbytes,
AEGIS128_BLOCK_SIZE),
walk->src.virt.addr,
walk->dst.virt.addr);
aegis128_aesni_dec(state, walk->src.virt.addr,
walk->dst.virt.addr,
round_down(walk->nbytes,
AEGIS128_BLOCK_SIZE));
skcipher_walk_done(walk, walk->nbytes % AEGIS128_BLOCK_SIZE);
}
if (walk->nbytes) {
if (enc)
crypto_aegis128_aesni_enc_tail(state, walk->nbytes,
walk->src.virt.addr,
walk->dst.virt.addr);
aegis128_aesni_enc_tail(state, walk->src.virt.addr,
walk->dst.virt.addr,
walk->nbytes);
else
crypto_aegis128_aesni_dec_tail(state, walk->nbytes,
walk->src.virt.addr,
walk->dst.virt.addr);
aegis128_aesni_dec_tail(state, walk->src.virt.addr,
walk->dst.virt.addr,
walk->nbytes);
skcipher_walk_done(walk, 0);
}
}
@ -186,10 +186,10 @@ crypto_aegis128_aesni_crypt(struct aead_request *req,
kernel_fpu_begin();
crypto_aegis128_aesni_init(&state, ctx->key.bytes, req->iv);
aegis128_aesni_init(&state, &ctx->key, req->iv);
crypto_aegis128_aesni_process_ad(&state, req->src, req->assoclen);
crypto_aegis128_aesni_process_crypt(&state, &walk, enc);
crypto_aegis128_aesni_final(&state, tag_xor, req->assoclen, cryptlen);
aegis128_aesni_final(&state, tag_xor, req->assoclen, cryptlen);
kernel_fpu_end();
}