Ard Biesheuvel | 317f2f7 | 2014-02-05 18:13:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions |
| 3 | * |
| 4 | * Copyright (C) 2013 - 2014 Linaro Ltd <ard.biesheuvel@linaro.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
Ard Biesheuvel | 317f2f7 | 2014-02-05 18:13:38 +0100 | [diff] [blame] | 11 | #include <crypto/aes.h> |
| 12 | #include <linux/cpufeature.h> |
| 13 | #include <linux/crypto.h> |
| 14 | #include <linux/module.h> |
| 15 | |
Ard Biesheuvel | 12ac3ef | 2014-11-03 16:50:01 +0000 | [diff] [blame] | 16 | #include "aes-ce-setkey.h" |
| 17 | |
Ard Biesheuvel | 317f2f7 | 2014-02-05 18:13:38 +0100 | [diff] [blame] | 18 | MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions"); |
| 19 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); |
| 20 | MODULE_LICENSE("GPL v2"); |
| 21 | |
Sami Tolvanen | 7c20af0 | 2017-08-10 09:42:10 -0700 | [diff] [blame] | 22 | extern void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]); |
| 23 | extern void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]); |
Ard Biesheuvel | 317f2f7 | 2014-02-05 18:13:38 +0100 | [diff] [blame] | 24 | |
Sami Tolvanen | 7c20af0 | 2017-08-10 09:42:10 -0700 | [diff] [blame] | 25 | #ifdef CONFIG_CFI_CLANG |
| 26 | static inline void __cfi_aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]) |
Ard Biesheuvel | 317f2f7 | 2014-02-05 18:13:38 +0100 | [diff] [blame] | 27 | { |
Sami Tolvanen | 7c20af0 | 2017-08-10 09:42:10 -0700 | [diff] [blame] | 28 | aes_cipher_encrypt(tfm, dst, src); |
Ard Biesheuvel | 317f2f7 | 2014-02-05 18:13:38 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Sami Tolvanen | 7c20af0 | 2017-08-10 09:42:10 -0700 | [diff] [blame] | 31 | static inline void __cfi_aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]) |
Ard Biesheuvel | 317f2f7 | 2014-02-05 18:13:38 +0100 | [diff] [blame] | 32 | { |
Sami Tolvanen | 7c20af0 | 2017-08-10 09:42:10 -0700 | [diff] [blame] | 33 | aes_cipher_decrypt(tfm, dst, src); |
Ard Biesheuvel | 317f2f7 | 2014-02-05 18:13:38 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Sami Tolvanen | 7c20af0 | 2017-08-10 09:42:10 -0700 | [diff] [blame] | 36 | #define aes_cipher_encrypt __cfi_aes_cipher_encrypt |
| 37 | #define aes_cipher_decrypt __cfi_aes_cipher_decrypt |
Ard Biesheuvel | d33a490 | 2016-10-11 19:15:13 +0100 | [diff] [blame] | 38 | #endif |
Ard Biesheuvel | 12ac3ef | 2014-11-03 16:50:01 +0000 | [diff] [blame] | 39 | |
| 40 | int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key, |
| 41 | unsigned int key_len) |
| 42 | { |
| 43 | struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 44 | int ret; |
| 45 | |
| 46 | ret = ce_aes_expandkey(ctx, in_key, key_len); |
| 47 | if (!ret) |
| 48 | return 0; |
| 49 | |
| 50 | tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
| 51 | return -EINVAL; |
| 52 | } |
| 53 | EXPORT_SYMBOL(ce_aes_setkey); |
| 54 | |
Ard Biesheuvel | 317f2f7 | 2014-02-05 18:13:38 +0100 | [diff] [blame] | 55 | static struct crypto_alg aes_alg = { |
| 56 | .cra_name = "aes", |
| 57 | .cra_driver_name = "aes-ce", |
Ard Biesheuvel | 08c6781 | 2015-11-16 13:12:48 +0100 | [diff] [blame] | 58 | .cra_priority = 250, |
Ard Biesheuvel | 317f2f7 | 2014-02-05 18:13:38 +0100 | [diff] [blame] | 59 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 60 | .cra_blocksize = AES_BLOCK_SIZE, |
| 61 | .cra_ctxsize = sizeof(struct crypto_aes_ctx), |
| 62 | .cra_module = THIS_MODULE, |
| 63 | .cra_cipher = { |
| 64 | .cia_min_keysize = AES_MIN_KEY_SIZE, |
| 65 | .cia_max_keysize = AES_MAX_KEY_SIZE, |
Ard Biesheuvel | 12ac3ef | 2014-11-03 16:50:01 +0000 | [diff] [blame] | 66 | .cia_setkey = ce_aes_setkey, |
Ard Biesheuvel | 317f2f7 | 2014-02-05 18:13:38 +0100 | [diff] [blame] | 67 | .cia_encrypt = aes_cipher_encrypt, |
| 68 | .cia_decrypt = aes_cipher_decrypt |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | static int __init aes_mod_init(void) |
| 73 | { |
| 74 | return crypto_register_alg(&aes_alg); |
| 75 | } |
| 76 | |
| 77 | static void __exit aes_mod_exit(void) |
| 78 | { |
| 79 | crypto_unregister_alg(&aes_alg); |
| 80 | } |
| 81 | |
| 82 | module_cpu_feature_match(AES, aes_mod_init); |
| 83 | module_exit(aes_mod_exit); |