| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * The AEGIS-128 Authenticated-Encryption Algorithm |
|---|
| 3 | 4 | * Glue for AES-NI + SSE2 implementation |
|---|
| 4 | 5 | * |
|---|
| 5 | 6 | * Copyright (c) 2017-2018 Ondrej Mosnacek <omosnacek@gmail.com> |
|---|
| 6 | 7 | * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. |
|---|
| 7 | | - * |
|---|
| 8 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 9 | | - * under the terms of the GNU General Public License as published by the Free |
|---|
| 10 | | - * Software Foundation; either version 2 of the License, or (at your option) |
|---|
| 11 | | - * any later version. |
|---|
| 12 | 8 | */ |
|---|
| 13 | 9 | |
|---|
| 14 | | -#include <crypto/cryptd.h> |
|---|
| 15 | 10 | #include <crypto/internal/aead.h> |
|---|
| 11 | +#include <crypto/internal/simd.h> |
|---|
| 16 | 12 | #include <crypto/internal/skcipher.h> |
|---|
| 17 | 13 | #include <crypto/scatterwalk.h> |
|---|
| 18 | 14 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 148 | 144 | { |
|---|
| 149 | 145 | struct aegis_ctx *ctx = crypto_aegis128_aesni_ctx(aead); |
|---|
| 150 | 146 | |
|---|
| 151 | | - if (keylen != AEGIS128_KEY_SIZE) { |
|---|
| 152 | | - crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN); |
|---|
| 147 | + if (keylen != AEGIS128_KEY_SIZE) |
|---|
| 153 | 148 | return -EINVAL; |
|---|
| 154 | | - } |
|---|
| 155 | 149 | |
|---|
| 156 | 150 | memcpy(ctx->key.bytes, key, AEGIS128_KEY_SIZE); |
|---|
| 157 | 151 | |
|---|
| .. | .. |
|---|
| 242 | 236 | { |
|---|
| 243 | 237 | } |
|---|
| 244 | 238 | |
|---|
| 245 | | -static int cryptd_aegis128_aesni_setkey(struct crypto_aead *aead, |
|---|
| 246 | | - const u8 *key, unsigned int keylen) |
|---|
| 247 | | -{ |
|---|
| 248 | | - struct cryptd_aead **ctx = crypto_aead_ctx(aead); |
|---|
| 249 | | - struct cryptd_aead *cryptd_tfm = *ctx; |
|---|
| 239 | +static struct aead_alg crypto_aegis128_aesni_alg = { |
|---|
| 240 | + .setkey = crypto_aegis128_aesni_setkey, |
|---|
| 241 | + .setauthsize = crypto_aegis128_aesni_setauthsize, |
|---|
| 242 | + .encrypt = crypto_aegis128_aesni_encrypt, |
|---|
| 243 | + .decrypt = crypto_aegis128_aesni_decrypt, |
|---|
| 244 | + .init = crypto_aegis128_aesni_init_tfm, |
|---|
| 245 | + .exit = crypto_aegis128_aesni_exit_tfm, |
|---|
| 250 | 246 | |
|---|
| 251 | | - return crypto_aead_setkey(&cryptd_tfm->base, key, keylen); |
|---|
| 252 | | -} |
|---|
| 247 | + .ivsize = AEGIS128_NONCE_SIZE, |
|---|
| 248 | + .maxauthsize = AEGIS128_MAX_AUTH_SIZE, |
|---|
| 249 | + .chunksize = AEGIS128_BLOCK_SIZE, |
|---|
| 253 | 250 | |
|---|
| 254 | | -static int cryptd_aegis128_aesni_setauthsize(struct crypto_aead *aead, |
|---|
| 255 | | - unsigned int authsize) |
|---|
| 256 | | -{ |
|---|
| 257 | | - struct cryptd_aead **ctx = crypto_aead_ctx(aead); |
|---|
| 258 | | - struct cryptd_aead *cryptd_tfm = *ctx; |
|---|
| 251 | + .base = { |
|---|
| 252 | + .cra_flags = CRYPTO_ALG_INTERNAL, |
|---|
| 253 | + .cra_blocksize = 1, |
|---|
| 254 | + .cra_ctxsize = sizeof(struct aegis_ctx) + |
|---|
| 255 | + __alignof__(struct aegis_ctx), |
|---|
| 256 | + .cra_alignmask = 0, |
|---|
| 257 | + .cra_priority = 400, |
|---|
| 259 | 258 | |
|---|
| 260 | | - return crypto_aead_setauthsize(&cryptd_tfm->base, authsize); |
|---|
| 261 | | -} |
|---|
| 259 | + .cra_name = "__aegis128", |
|---|
| 260 | + .cra_driver_name = "__aegis128-aesni", |
|---|
| 262 | 261 | |
|---|
| 263 | | -static int cryptd_aegis128_aesni_encrypt(struct aead_request *req) |
|---|
| 264 | | -{ |
|---|
| 265 | | - struct crypto_aead *aead = crypto_aead_reqtfm(req); |
|---|
| 266 | | - struct cryptd_aead **ctx = crypto_aead_ctx(aead); |
|---|
| 267 | | - struct cryptd_aead *cryptd_tfm = *ctx; |
|---|
| 268 | | - |
|---|
| 269 | | - aead = &cryptd_tfm->base; |
|---|
| 270 | | - if (irq_fpu_usable() && (!in_atomic() || |
|---|
| 271 | | - !cryptd_aead_queued(cryptd_tfm))) |
|---|
| 272 | | - aead = cryptd_aead_child(cryptd_tfm); |
|---|
| 273 | | - |
|---|
| 274 | | - aead_request_set_tfm(req, aead); |
|---|
| 275 | | - |
|---|
| 276 | | - return crypto_aead_encrypt(req); |
|---|
| 277 | | -} |
|---|
| 278 | | - |
|---|
| 279 | | -static int cryptd_aegis128_aesni_decrypt(struct aead_request *req) |
|---|
| 280 | | -{ |
|---|
| 281 | | - struct crypto_aead *aead = crypto_aead_reqtfm(req); |
|---|
| 282 | | - struct cryptd_aead **ctx = crypto_aead_ctx(aead); |
|---|
| 283 | | - struct cryptd_aead *cryptd_tfm = *ctx; |
|---|
| 284 | | - |
|---|
| 285 | | - aead = &cryptd_tfm->base; |
|---|
| 286 | | - if (irq_fpu_usable() && (!in_atomic() || |
|---|
| 287 | | - !cryptd_aead_queued(cryptd_tfm))) |
|---|
| 288 | | - aead = cryptd_aead_child(cryptd_tfm); |
|---|
| 289 | | - |
|---|
| 290 | | - aead_request_set_tfm(req, aead); |
|---|
| 291 | | - |
|---|
| 292 | | - return crypto_aead_decrypt(req); |
|---|
| 293 | | -} |
|---|
| 294 | | - |
|---|
| 295 | | -static int cryptd_aegis128_aesni_init_tfm(struct crypto_aead *aead) |
|---|
| 296 | | -{ |
|---|
| 297 | | - struct cryptd_aead *cryptd_tfm; |
|---|
| 298 | | - struct cryptd_aead **ctx = crypto_aead_ctx(aead); |
|---|
| 299 | | - |
|---|
| 300 | | - cryptd_tfm = cryptd_alloc_aead("__aegis128-aesni", CRYPTO_ALG_INTERNAL, |
|---|
| 301 | | - CRYPTO_ALG_INTERNAL); |
|---|
| 302 | | - if (IS_ERR(cryptd_tfm)) |
|---|
| 303 | | - return PTR_ERR(cryptd_tfm); |
|---|
| 304 | | - |
|---|
| 305 | | - *ctx = cryptd_tfm; |
|---|
| 306 | | - crypto_aead_set_reqsize(aead, crypto_aead_reqsize(&cryptd_tfm->base)); |
|---|
| 307 | | - return 0; |
|---|
| 308 | | -} |
|---|
| 309 | | - |
|---|
| 310 | | -static void cryptd_aegis128_aesni_exit_tfm(struct crypto_aead *aead) |
|---|
| 311 | | -{ |
|---|
| 312 | | - struct cryptd_aead **ctx = crypto_aead_ctx(aead); |
|---|
| 313 | | - |
|---|
| 314 | | - cryptd_free_aead(*ctx); |
|---|
| 315 | | -} |
|---|
| 316 | | - |
|---|
| 317 | | -static struct aead_alg crypto_aegis128_aesni_alg[] = { |
|---|
| 318 | | - { |
|---|
| 319 | | - .setkey = crypto_aegis128_aesni_setkey, |
|---|
| 320 | | - .setauthsize = crypto_aegis128_aesni_setauthsize, |
|---|
| 321 | | - .encrypt = crypto_aegis128_aesni_encrypt, |
|---|
| 322 | | - .decrypt = crypto_aegis128_aesni_decrypt, |
|---|
| 323 | | - .init = crypto_aegis128_aesni_init_tfm, |
|---|
| 324 | | - .exit = crypto_aegis128_aesni_exit_tfm, |
|---|
| 325 | | - |
|---|
| 326 | | - .ivsize = AEGIS128_NONCE_SIZE, |
|---|
| 327 | | - .maxauthsize = AEGIS128_MAX_AUTH_SIZE, |
|---|
| 328 | | - .chunksize = AEGIS128_BLOCK_SIZE, |
|---|
| 329 | | - |
|---|
| 330 | | - .base = { |
|---|
| 331 | | - .cra_flags = CRYPTO_ALG_INTERNAL, |
|---|
| 332 | | - .cra_blocksize = 1, |
|---|
| 333 | | - .cra_ctxsize = sizeof(struct aegis_ctx) + |
|---|
| 334 | | - __alignof__(struct aegis_ctx), |
|---|
| 335 | | - .cra_alignmask = 0, |
|---|
| 336 | | - |
|---|
| 337 | | - .cra_name = "__aegis128", |
|---|
| 338 | | - .cra_driver_name = "__aegis128-aesni", |
|---|
| 339 | | - |
|---|
| 340 | | - .cra_module = THIS_MODULE, |
|---|
| 341 | | - } |
|---|
| 342 | | - }, { |
|---|
| 343 | | - .setkey = cryptd_aegis128_aesni_setkey, |
|---|
| 344 | | - .setauthsize = cryptd_aegis128_aesni_setauthsize, |
|---|
| 345 | | - .encrypt = cryptd_aegis128_aesni_encrypt, |
|---|
| 346 | | - .decrypt = cryptd_aegis128_aesni_decrypt, |
|---|
| 347 | | - .init = cryptd_aegis128_aesni_init_tfm, |
|---|
| 348 | | - .exit = cryptd_aegis128_aesni_exit_tfm, |
|---|
| 349 | | - |
|---|
| 350 | | - .ivsize = AEGIS128_NONCE_SIZE, |
|---|
| 351 | | - .maxauthsize = AEGIS128_MAX_AUTH_SIZE, |
|---|
| 352 | | - .chunksize = AEGIS128_BLOCK_SIZE, |
|---|
| 353 | | - |
|---|
| 354 | | - .base = { |
|---|
| 355 | | - .cra_flags = CRYPTO_ALG_ASYNC, |
|---|
| 356 | | - .cra_blocksize = 1, |
|---|
| 357 | | - .cra_ctxsize = sizeof(struct cryptd_aead *), |
|---|
| 358 | | - .cra_alignmask = 0, |
|---|
| 359 | | - |
|---|
| 360 | | - .cra_priority = 400, |
|---|
| 361 | | - |
|---|
| 362 | | - .cra_name = "aegis128", |
|---|
| 363 | | - .cra_driver_name = "aegis128-aesni", |
|---|
| 364 | | - |
|---|
| 365 | | - .cra_module = THIS_MODULE, |
|---|
| 366 | | - } |
|---|
| 262 | + .cra_module = THIS_MODULE, |
|---|
| 367 | 263 | } |
|---|
| 368 | 264 | }; |
|---|
| 265 | + |
|---|
| 266 | +static struct simd_aead_alg *simd_alg; |
|---|
| 369 | 267 | |
|---|
| 370 | 268 | static int __init crypto_aegis128_aesni_module_init(void) |
|---|
| 371 | 269 | { |
|---|
| .. | .. |
|---|
| 374 | 272 | !cpu_has_xfeatures(XFEATURE_MASK_SSE, NULL)) |
|---|
| 375 | 273 | return -ENODEV; |
|---|
| 376 | 274 | |
|---|
| 377 | | - return crypto_register_aeads(crypto_aegis128_aesni_alg, |
|---|
| 378 | | - ARRAY_SIZE(crypto_aegis128_aesni_alg)); |
|---|
| 275 | + return simd_register_aeads_compat(&crypto_aegis128_aesni_alg, 1, |
|---|
| 276 | + &simd_alg); |
|---|
| 379 | 277 | } |
|---|
| 380 | 278 | |
|---|
| 381 | 279 | static void __exit crypto_aegis128_aesni_module_exit(void) |
|---|
| 382 | 280 | { |
|---|
| 383 | | - crypto_unregister_aeads(crypto_aegis128_aesni_alg, |
|---|
| 384 | | - ARRAY_SIZE(crypto_aegis128_aesni_alg)); |
|---|
| 281 | + simd_unregister_aeads(&crypto_aegis128_aesni_alg, 1, &simd_alg); |
|---|
| 385 | 282 | } |
|---|
| 386 | 283 | |
|---|
| 387 | 284 | module_init(crypto_aegis128_aesni_module_init); |
|---|