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