^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * The AEGIS-128 Authenticated-Encryption Algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Glue for AES-NI + SSE2 implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2017-2018 Ondrej Mosnacek <omosnacek@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/internal/aead.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/internal/simd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/fpu/api.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/cpu_device_id.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define AEGIS128_BLOCK_ALIGN 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define AEGIS128_BLOCK_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define AEGIS128_NONCE_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define AEGIS128_STATE_BLOCKS 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define AEGIS128_KEY_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define AEGIS128_MIN_AUTH_SIZE 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define AEGIS128_MAX_AUTH_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) asmlinkage void crypto_aegis128_aesni_init(void *state, void *key, void *iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) asmlinkage void crypto_aegis128_aesni_ad(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) void *state, unsigned int length, const void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) asmlinkage void crypto_aegis128_aesni_enc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) void *state, unsigned int length, const void *src, void *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) asmlinkage void crypto_aegis128_aesni_dec(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) void *state, unsigned int length, const void *src, void *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) asmlinkage void crypto_aegis128_aesni_enc_tail(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) void *state, unsigned int length, const void *src, void *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) asmlinkage void crypto_aegis128_aesni_dec_tail(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) void *state, unsigned int length, const void *src, void *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) asmlinkage void crypto_aegis128_aesni_final(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) void *state, void *tag_xor, unsigned int cryptlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned int assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct aegis_block {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u8 bytes[AEGIS128_BLOCK_SIZE] __aligned(AEGIS128_BLOCK_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct aegis_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct aegis_block blocks[AEGIS128_STATE_BLOCKS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct aegis_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct aegis_block key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct aegis_crypt_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int (*skcipher_walk_init)(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct aead_request *req, bool atomic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) void (*crypt_blocks)(void *state, unsigned int length, const void *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) void *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) void (*crypt_tail)(void *state, unsigned int length, const void *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) void *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static void crypto_aegis128_aesni_process_ad(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct aegis_state *state, struct scatterlist *sg_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) unsigned int assoclen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct scatter_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct aegis_block buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned int pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) scatterwalk_start(&walk, sg_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) while (assoclen != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned int size = scatterwalk_clamp(&walk, assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned int left = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) void *mapped = scatterwalk_map(&walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) const u8 *src = (const u8 *)mapped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (pos + size >= AEGIS128_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (pos > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned int fill = AEGIS128_BLOCK_SIZE - pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) memcpy(buf.bytes + pos, src, fill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) crypto_aegis128_aesni_ad(state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) AEGIS128_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) buf.bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) left -= fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) src += fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) crypto_aegis128_aesni_ad(state, left, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) src += left & ~(AEGIS128_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) left &= AEGIS128_BLOCK_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) memcpy(buf.bytes + pos, src, left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) pos += left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) assoclen -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) scatterwalk_unmap(mapped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) scatterwalk_advance(&walk, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) scatterwalk_done(&walk, 0, assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (pos > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) memset(buf.bytes + pos, 0, AEGIS128_BLOCK_SIZE - pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) crypto_aegis128_aesni_ad(state, AEGIS128_BLOCK_SIZE, buf.bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static void crypto_aegis128_aesni_process_crypt(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct aegis_state *state, struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) const struct aegis_crypt_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) while (walk->nbytes >= AEGIS128_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ops->crypt_blocks(state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) round_down(walk->nbytes, AEGIS128_BLOCK_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) walk->src.virt.addr, walk->dst.virt.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) skcipher_walk_done(walk, walk->nbytes % AEGIS128_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (walk->nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ops->crypt_tail(state, walk->nbytes, walk->src.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) walk->dst.virt.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) skcipher_walk_done(walk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static struct aegis_ctx *crypto_aegis128_aesni_ctx(struct crypto_aead *aead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) u8 *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ctx = PTR_ALIGN(ctx, __alignof__(struct aegis_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return (void *)ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int crypto_aegis128_aesni_setkey(struct crypto_aead *aead, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct aegis_ctx *ctx = crypto_aegis128_aesni_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (keylen != AEGIS128_KEY_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) memcpy(ctx->key.bytes, key, AEGIS128_KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int crypto_aegis128_aesni_setauthsize(struct crypto_aead *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) unsigned int authsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (authsize > AEGIS128_MAX_AUTH_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (authsize < AEGIS128_MIN_AUTH_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static void crypto_aegis128_aesni_crypt(struct aead_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct aegis_block *tag_xor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) unsigned int cryptlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) const struct aegis_crypt_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct crypto_aead *tfm = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct aegis_ctx *ctx = crypto_aegis128_aesni_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct aegis_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) ops->skcipher_walk_init(&walk, req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) kernel_fpu_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) crypto_aegis128_aesni_init(&state, ctx->key.bytes, req->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) crypto_aegis128_aesni_process_ad(&state, req->src, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) crypto_aegis128_aesni_process_crypt(&state, &walk, ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) crypto_aegis128_aesni_final(&state, tag_xor, req->assoclen, cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) kernel_fpu_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int crypto_aegis128_aesni_encrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static const struct aegis_crypt_ops OPS = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .skcipher_walk_init = skcipher_walk_aead_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) .crypt_blocks = crypto_aegis128_aesni_enc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) .crypt_tail = crypto_aegis128_aesni_enc_tail,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct crypto_aead *tfm = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct aegis_block tag = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) unsigned int authsize = crypto_aead_authsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) unsigned int cryptlen = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) crypto_aegis128_aesni_crypt(req, &tag, cryptlen, &OPS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) scatterwalk_map_and_copy(tag.bytes, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) req->assoclen + cryptlen, authsize, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static int crypto_aegis128_aesni_decrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static const struct aegis_block zeros = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static const struct aegis_crypt_ops OPS = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) .skcipher_walk_init = skcipher_walk_aead_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) .crypt_blocks = crypto_aegis128_aesni_dec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) .crypt_tail = crypto_aegis128_aesni_dec_tail,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct crypto_aead *tfm = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct aegis_block tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) unsigned int authsize = crypto_aead_authsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) unsigned int cryptlen = req->cryptlen - authsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) scatterwalk_map_and_copy(tag.bytes, req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) req->assoclen + cryptlen, authsize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) crypto_aegis128_aesni_crypt(req, &tag, cryptlen, &OPS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return crypto_memneq(tag.bytes, zeros.bytes, authsize) ? -EBADMSG : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int crypto_aegis128_aesni_init_tfm(struct crypto_aead *aead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static void crypto_aegis128_aesni_exit_tfm(struct crypto_aead *aead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static struct aead_alg crypto_aegis128_aesni_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) .setkey = crypto_aegis128_aesni_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) .setauthsize = crypto_aegis128_aesni_setauthsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) .encrypt = crypto_aegis128_aesni_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) .decrypt = crypto_aegis128_aesni_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) .init = crypto_aegis128_aesni_init_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) .exit = crypto_aegis128_aesni_exit_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) .ivsize = AEGIS128_NONCE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) .maxauthsize = AEGIS128_MAX_AUTH_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) .chunksize = AEGIS128_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) .cra_flags = CRYPTO_ALG_INTERNAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) .cra_blocksize = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) .cra_ctxsize = sizeof(struct aegis_ctx) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) __alignof__(struct aegis_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) .cra_alignmask = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) .cra_priority = 400,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) .cra_name = "__aegis128",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) .cra_driver_name = "__aegis128-aesni",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static struct simd_aead_alg *simd_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int __init crypto_aegis128_aesni_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (!boot_cpu_has(X86_FEATURE_XMM2) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) !boot_cpu_has(X86_FEATURE_AES) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) !cpu_has_xfeatures(XFEATURE_MASK_SSE, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return simd_register_aeads_compat(&crypto_aegis128_aesni_alg, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) &simd_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static void __exit crypto_aegis128_aesni_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) simd_unregister_aeads(&crypto_aegis128_aesni_alg, 1, &simd_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) module_init(crypto_aegis128_aesni_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) module_exit(crypto_aegis128_aesni_module_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) MODULE_DESCRIPTION("AEGIS-128 AEAD algorithm -- AESNI+SSE2 implementation");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) MODULE_ALIAS_CRYPTO("aegis128");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) MODULE_ALIAS_CRYPTO("aegis128-aesni");