1019cd469SArd Biesheuvel /* 2019cd469SArd Biesheuvel * aes-ce-cipher.c - core AES cipher using ARMv8 Crypto Extensions 3019cd469SArd Biesheuvel * 4019cd469SArd Biesheuvel * Copyright (C) 2013 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org> 5019cd469SArd Biesheuvel * 6019cd469SArd Biesheuvel * This program is free software; you can redistribute it and/or modify 7019cd469SArd Biesheuvel * it under the terms of the GNU General Public License version 2 as 8019cd469SArd Biesheuvel * published by the Free Software Foundation. 9019cd469SArd Biesheuvel */ 10019cd469SArd Biesheuvel 11019cd469SArd Biesheuvel #include <asm/neon.h> 12019cd469SArd Biesheuvel #include <asm/simd.h> 13019cd469SArd Biesheuvel #include <asm/unaligned.h> 14019cd469SArd Biesheuvel #include <crypto/aes.h> 15019cd469SArd Biesheuvel #include <linux/cpufeature.h> 16019cd469SArd Biesheuvel #include <linux/crypto.h> 17019cd469SArd Biesheuvel #include <linux/module.h> 18019cd469SArd Biesheuvel 19019cd469SArd Biesheuvel #include "aes-ce-setkey.h" 20019cd469SArd Biesheuvel 21019cd469SArd Biesheuvel MODULE_DESCRIPTION("Synchronous AES cipher using ARMv8 Crypto Extensions"); 22019cd469SArd Biesheuvel MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); 23019cd469SArd Biesheuvel MODULE_LICENSE("GPL v2"); 24019cd469SArd Biesheuvel 25019cd469SArd Biesheuvel asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds); 26019cd469SArd Biesheuvel asmlinkage void __aes_arm64_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds); 27019cd469SArd Biesheuvel 28019cd469SArd Biesheuvel struct aes_block { 29019cd469SArd Biesheuvel u8 b[AES_BLOCK_SIZE]; 30019cd469SArd Biesheuvel }; 31019cd469SArd Biesheuvel 32019cd469SArd Biesheuvel asmlinkage void __aes_ce_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds); 33019cd469SArd Biesheuvel asmlinkage void __aes_ce_decrypt(u32 *rk, u8 *out, const u8 *in, int rounds); 34019cd469SArd Biesheuvel 35019cd469SArd Biesheuvel asmlinkage u32 __aes_ce_sub(u32 l); 36019cd469SArd Biesheuvel asmlinkage void __aes_ce_invert(struct aes_block *out, 37019cd469SArd Biesheuvel const struct aes_block *in); 38019cd469SArd Biesheuvel 39019cd469SArd Biesheuvel static int num_rounds(struct crypto_aes_ctx *ctx) 40019cd469SArd Biesheuvel { 41019cd469SArd Biesheuvel /* 42019cd469SArd Biesheuvel * # of rounds specified by AES: 43019cd469SArd Biesheuvel * 128 bit key 10 rounds 44019cd469SArd Biesheuvel * 192 bit key 12 rounds 45019cd469SArd Biesheuvel * 256 bit key 14 rounds 46019cd469SArd Biesheuvel * => n byte key => 6 + (n/4) rounds 47019cd469SArd Biesheuvel */ 48019cd469SArd Biesheuvel return 6 + ctx->key_length / 4; 49019cd469SArd Biesheuvel } 50019cd469SArd Biesheuvel 51019cd469SArd Biesheuvel static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]) 52019cd469SArd Biesheuvel { 53019cd469SArd Biesheuvel struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); 54019cd469SArd Biesheuvel 55019cd469SArd Biesheuvel if (!may_use_simd()) { 56019cd469SArd Biesheuvel __aes_arm64_encrypt(ctx->key_enc, dst, src, num_rounds(ctx)); 57019cd469SArd Biesheuvel return; 58019cd469SArd Biesheuvel } 59019cd469SArd Biesheuvel 60019cd469SArd Biesheuvel kernel_neon_begin(); 61019cd469SArd Biesheuvel __aes_ce_encrypt(ctx->key_enc, dst, src, num_rounds(ctx)); 62019cd469SArd Biesheuvel kernel_neon_end(); 63019cd469SArd Biesheuvel } 64019cd469SArd Biesheuvel 65019cd469SArd Biesheuvel static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[]) 66019cd469SArd Biesheuvel { 67019cd469SArd Biesheuvel struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); 68019cd469SArd Biesheuvel 69019cd469SArd Biesheuvel if (!may_use_simd()) { 70019cd469SArd Biesheuvel __aes_arm64_decrypt(ctx->key_dec, dst, src, num_rounds(ctx)); 71019cd469SArd Biesheuvel return; 72019cd469SArd Biesheuvel } 73019cd469SArd Biesheuvel 74019cd469SArd Biesheuvel kernel_neon_begin(); 75019cd469SArd Biesheuvel __aes_ce_decrypt(ctx->key_dec, dst, src, num_rounds(ctx)); 76019cd469SArd Biesheuvel kernel_neon_end(); 77019cd469SArd Biesheuvel } 78019cd469SArd Biesheuvel 79019cd469SArd Biesheuvel int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key, 80019cd469SArd Biesheuvel unsigned int key_len) 81019cd469SArd Biesheuvel { 82019cd469SArd Biesheuvel /* 83019cd469SArd Biesheuvel * The AES key schedule round constants 84019cd469SArd Biesheuvel */ 85019cd469SArd Biesheuvel static u8 const rcon[] = { 86019cd469SArd Biesheuvel 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 87019cd469SArd Biesheuvel }; 88019cd469SArd Biesheuvel 89019cd469SArd Biesheuvel u32 kwords = key_len / sizeof(u32); 90019cd469SArd Biesheuvel struct aes_block *key_enc, *key_dec; 91019cd469SArd Biesheuvel int i, j; 92019cd469SArd Biesheuvel 93019cd469SArd Biesheuvel if (key_len != AES_KEYSIZE_128 && 94019cd469SArd Biesheuvel key_len != AES_KEYSIZE_192 && 95019cd469SArd Biesheuvel key_len != AES_KEYSIZE_256) 96019cd469SArd Biesheuvel return -EINVAL; 97019cd469SArd Biesheuvel 98019cd469SArd Biesheuvel ctx->key_length = key_len; 99019cd469SArd Biesheuvel for (i = 0; i < kwords; i++) 100019cd469SArd Biesheuvel ctx->key_enc[i] = get_unaligned_le32(in_key + i * sizeof(u32)); 101019cd469SArd Biesheuvel 102019cd469SArd Biesheuvel kernel_neon_begin(); 103019cd469SArd Biesheuvel for (i = 0; i < sizeof(rcon); i++) { 104019cd469SArd Biesheuvel u32 *rki = ctx->key_enc + (i * kwords); 105019cd469SArd Biesheuvel u32 *rko = rki + kwords; 106019cd469SArd Biesheuvel 107019cd469SArd Biesheuvel rko[0] = ror32(__aes_ce_sub(rki[kwords - 1]), 8) ^ rcon[i] ^ rki[0]; 108019cd469SArd Biesheuvel rko[1] = rko[0] ^ rki[1]; 109019cd469SArd Biesheuvel rko[2] = rko[1] ^ rki[2]; 110019cd469SArd Biesheuvel rko[3] = rko[2] ^ rki[3]; 111019cd469SArd Biesheuvel 112019cd469SArd Biesheuvel if (key_len == AES_KEYSIZE_192) { 113019cd469SArd Biesheuvel if (i >= 7) 114019cd469SArd Biesheuvel break; 115019cd469SArd Biesheuvel rko[4] = rko[3] ^ rki[4]; 116019cd469SArd Biesheuvel rko[5] = rko[4] ^ rki[5]; 117019cd469SArd Biesheuvel } else if (key_len == AES_KEYSIZE_256) { 118019cd469SArd Biesheuvel if (i >= 6) 119019cd469SArd Biesheuvel break; 120019cd469SArd Biesheuvel rko[4] = __aes_ce_sub(rko[3]) ^ rki[4]; 121019cd469SArd Biesheuvel rko[5] = rko[4] ^ rki[5]; 122019cd469SArd Biesheuvel rko[6] = rko[5] ^ rki[6]; 123019cd469SArd Biesheuvel rko[7] = rko[6] ^ rki[7]; 124019cd469SArd Biesheuvel } 125019cd469SArd Biesheuvel } 126019cd469SArd Biesheuvel 127019cd469SArd Biesheuvel /* 128019cd469SArd Biesheuvel * Generate the decryption keys for the Equivalent Inverse Cipher. 129019cd469SArd Biesheuvel * This involves reversing the order of the round keys, and applying 130019cd469SArd Biesheuvel * the Inverse Mix Columns transformation on all but the first and 131019cd469SArd Biesheuvel * the last one. 132019cd469SArd Biesheuvel */ 133019cd469SArd Biesheuvel key_enc = (struct aes_block *)ctx->key_enc; 134019cd469SArd Biesheuvel key_dec = (struct aes_block *)ctx->key_dec; 135019cd469SArd Biesheuvel j = num_rounds(ctx); 136019cd469SArd Biesheuvel 137019cd469SArd Biesheuvel key_dec[0] = key_enc[j]; 138019cd469SArd Biesheuvel for (i = 1, j--; j > 0; i++, j--) 139019cd469SArd Biesheuvel __aes_ce_invert(key_dec + i, key_enc + j); 140019cd469SArd Biesheuvel key_dec[i] = key_enc[0]; 141019cd469SArd Biesheuvel 142019cd469SArd Biesheuvel kernel_neon_end(); 143019cd469SArd Biesheuvel return 0; 144019cd469SArd Biesheuvel } 145019cd469SArd Biesheuvel EXPORT_SYMBOL(ce_aes_expandkey); 146019cd469SArd Biesheuvel 147019cd469SArd Biesheuvel int ce_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key, 148019cd469SArd Biesheuvel unsigned int key_len) 149019cd469SArd Biesheuvel { 150019cd469SArd Biesheuvel struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); 151019cd469SArd Biesheuvel int ret; 152019cd469SArd Biesheuvel 153019cd469SArd Biesheuvel ret = ce_aes_expandkey(ctx, in_key, key_len); 154019cd469SArd Biesheuvel if (!ret) 155019cd469SArd Biesheuvel return 0; 156019cd469SArd Biesheuvel 157019cd469SArd Biesheuvel tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; 158019cd469SArd Biesheuvel return -EINVAL; 159019cd469SArd Biesheuvel } 160019cd469SArd Biesheuvel EXPORT_SYMBOL(ce_aes_setkey); 161019cd469SArd Biesheuvel 162019cd469SArd Biesheuvel static struct crypto_alg aes_alg = { 163019cd469SArd Biesheuvel .cra_name = "aes", 164019cd469SArd Biesheuvel .cra_driver_name = "aes-ce", 165019cd469SArd Biesheuvel .cra_priority = 250, 166019cd469SArd Biesheuvel .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 167019cd469SArd Biesheuvel .cra_blocksize = AES_BLOCK_SIZE, 168019cd469SArd Biesheuvel .cra_ctxsize = sizeof(struct crypto_aes_ctx), 169019cd469SArd Biesheuvel .cra_module = THIS_MODULE, 170019cd469SArd Biesheuvel .cra_cipher = { 171019cd469SArd Biesheuvel .cia_min_keysize = AES_MIN_KEY_SIZE, 172019cd469SArd Biesheuvel .cia_max_keysize = AES_MAX_KEY_SIZE, 173019cd469SArd Biesheuvel .cia_setkey = ce_aes_setkey, 174019cd469SArd Biesheuvel .cia_encrypt = aes_cipher_encrypt, 175019cd469SArd Biesheuvel .cia_decrypt = aes_cipher_decrypt 176019cd469SArd Biesheuvel } 177019cd469SArd Biesheuvel }; 178019cd469SArd Biesheuvel 179019cd469SArd Biesheuvel static int __init aes_mod_init(void) 180019cd469SArd Biesheuvel { 181019cd469SArd Biesheuvel return crypto_register_alg(&aes_alg); 182019cd469SArd Biesheuvel } 183019cd469SArd Biesheuvel 184019cd469SArd Biesheuvel static void __exit aes_mod_exit(void) 185019cd469SArd Biesheuvel { 186019cd469SArd Biesheuvel crypto_unregister_alg(&aes_alg); 187019cd469SArd Biesheuvel } 188019cd469SArd Biesheuvel 189019cd469SArd Biesheuvel module_cpu_feature_match(AES, aes_mod_init); 190019cd469SArd Biesheuvel module_exit(aes_mod_exit); 191