181658ad0SDavid S. Miller /* Glue code for CAMELLIA encryption optimized for sparc64 crypto opcodes. 281658ad0SDavid S. Miller * 381658ad0SDavid S. Miller * Copyright (C) 2012 David S. Miller <davem@davemloft.net> 481658ad0SDavid S. Miller */ 581658ad0SDavid S. Miller 671741680SDavid S. Miller #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 771741680SDavid S. Miller 881658ad0SDavid S. Miller #include <linux/crypto.h> 981658ad0SDavid S. Miller #include <linux/init.h> 1081658ad0SDavid S. Miller #include <linux/module.h> 1181658ad0SDavid S. Miller #include <linux/mm.h> 1281658ad0SDavid S. Miller #include <linux/types.h> 1381658ad0SDavid S. Miller #include <crypto/algapi.h> 1481658ad0SDavid S. Miller 1581658ad0SDavid S. Miller #include <asm/fpumacro.h> 1681658ad0SDavid S. Miller #include <asm/pstate.h> 1781658ad0SDavid S. Miller #include <asm/elf.h> 1881658ad0SDavid S. Miller 1910803624SDavid S. Miller #include "opcodes.h" 2010803624SDavid S. Miller 2181658ad0SDavid S. Miller #define CAMELLIA_MIN_KEY_SIZE 16 2281658ad0SDavid S. Miller #define CAMELLIA_MAX_KEY_SIZE 32 2381658ad0SDavid S. Miller #define CAMELLIA_BLOCK_SIZE 16 2481658ad0SDavid S. Miller #define CAMELLIA_TABLE_BYTE_LEN 272 2581658ad0SDavid S. Miller 2681658ad0SDavid S. Miller struct camellia_sparc64_ctx { 2781658ad0SDavid S. Miller u64 encrypt_key[CAMELLIA_TABLE_BYTE_LEN / sizeof(u64)]; 2881658ad0SDavid S. Miller u64 decrypt_key[CAMELLIA_TABLE_BYTE_LEN / sizeof(u64)]; 2981658ad0SDavid S. Miller int key_len; 3081658ad0SDavid S. Miller }; 3181658ad0SDavid S. Miller 3281658ad0SDavid S. Miller extern void camellia_sparc64_key_expand(const u32 *in_key, u64 *encrypt_key, 3381658ad0SDavid S. Miller unsigned int key_len, u64 *decrypt_key); 3481658ad0SDavid S. Miller 3581658ad0SDavid S. Miller static int camellia_set_key(struct crypto_tfm *tfm, const u8 *_in_key, 3681658ad0SDavid S. Miller unsigned int key_len) 3781658ad0SDavid S. Miller { 3881658ad0SDavid S. Miller struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm); 3981658ad0SDavid S. Miller const u32 *in_key = (const u32 *) _in_key; 4081658ad0SDavid S. Miller u32 *flags = &tfm->crt_flags; 4181658ad0SDavid S. Miller 4281658ad0SDavid S. Miller if (key_len != 16 && key_len != 24 && key_len != 32) { 4381658ad0SDavid S. Miller *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; 4481658ad0SDavid S. Miller return -EINVAL; 4581658ad0SDavid S. Miller } 4681658ad0SDavid S. Miller 4781658ad0SDavid S. Miller ctx->key_len = key_len; 4881658ad0SDavid S. Miller 4981658ad0SDavid S. Miller camellia_sparc64_key_expand(in_key, &ctx->encrypt_key[0], 5081658ad0SDavid S. Miller key_len, &ctx->decrypt_key[0]); 5181658ad0SDavid S. Miller return 0; 5281658ad0SDavid S. Miller } 5381658ad0SDavid S. Miller 5481658ad0SDavid S. Miller extern void camellia_sparc64_crypt(const u64 *key, const u32 *input, 5581658ad0SDavid S. Miller u32 *output, unsigned int key_len); 5681658ad0SDavid S. Miller 5781658ad0SDavid S. Miller static void camellia_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 5881658ad0SDavid S. Miller { 5981658ad0SDavid S. Miller struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm); 6081658ad0SDavid S. Miller 6181658ad0SDavid S. Miller camellia_sparc64_crypt(&ctx->encrypt_key[0], 6281658ad0SDavid S. Miller (const u32 *) src, 6381658ad0SDavid S. Miller (u32 *) dst, ctx->key_len); 6481658ad0SDavid S. Miller } 6581658ad0SDavid S. Miller 6681658ad0SDavid S. Miller static void camellia_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 6781658ad0SDavid S. Miller { 6881658ad0SDavid S. Miller struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm); 6981658ad0SDavid S. Miller 7081658ad0SDavid S. Miller camellia_sparc64_crypt(&ctx->decrypt_key[0], 7181658ad0SDavid S. Miller (const u32 *) src, 7281658ad0SDavid S. Miller (u32 *) dst, ctx->key_len); 7381658ad0SDavid S. Miller } 7481658ad0SDavid S. Miller 7581658ad0SDavid S. Miller extern void camellia_sparc64_load_keys(const u64 *key, unsigned int key_len); 7681658ad0SDavid S. Miller 7781658ad0SDavid S. Miller typedef void ecb_crypt_op(const u64 *input, u64 *output, unsigned int len, 7881658ad0SDavid S. Miller const u64 *key); 7981658ad0SDavid S. Miller 8081658ad0SDavid S. Miller extern ecb_crypt_op camellia_sparc64_ecb_crypt_3_grand_rounds; 8181658ad0SDavid S. Miller extern ecb_crypt_op camellia_sparc64_ecb_crypt_4_grand_rounds; 8281658ad0SDavid S. Miller 8381658ad0SDavid S. Miller #define CAMELLIA_BLOCK_MASK (~(CAMELLIA_BLOCK_SIZE - 1)) 8481658ad0SDavid S. Miller 8581658ad0SDavid S. Miller static int __ecb_crypt(struct blkcipher_desc *desc, 8681658ad0SDavid S. Miller struct scatterlist *dst, struct scatterlist *src, 8781658ad0SDavid S. Miller unsigned int nbytes, bool encrypt) 8881658ad0SDavid S. Miller { 8981658ad0SDavid S. Miller struct camellia_sparc64_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); 9081658ad0SDavid S. Miller struct blkcipher_walk walk; 9181658ad0SDavid S. Miller ecb_crypt_op *op; 9281658ad0SDavid S. Miller const u64 *key; 9381658ad0SDavid S. Miller int err; 9481658ad0SDavid S. Miller 9581658ad0SDavid S. Miller op = camellia_sparc64_ecb_crypt_3_grand_rounds; 9681658ad0SDavid S. Miller if (ctx->key_len != 16) 9781658ad0SDavid S. Miller op = camellia_sparc64_ecb_crypt_4_grand_rounds; 9881658ad0SDavid S. Miller 9981658ad0SDavid S. Miller blkcipher_walk_init(&walk, dst, src, nbytes); 10081658ad0SDavid S. Miller err = blkcipher_walk_virt(desc, &walk); 10162ba63dcSDavid S. Miller desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; 10281658ad0SDavid S. Miller 10381658ad0SDavid S. Miller if (encrypt) 10481658ad0SDavid S. Miller key = &ctx->encrypt_key[0]; 10581658ad0SDavid S. Miller else 10681658ad0SDavid S. Miller key = &ctx->decrypt_key[0]; 10781658ad0SDavid S. Miller camellia_sparc64_load_keys(key, ctx->key_len); 10881658ad0SDavid S. Miller while ((nbytes = walk.nbytes)) { 10981658ad0SDavid S. Miller unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK; 11081658ad0SDavid S. Miller 11181658ad0SDavid S. Miller if (likely(block_len)) { 11281658ad0SDavid S. Miller const u64 *src64; 11381658ad0SDavid S. Miller u64 *dst64; 11481658ad0SDavid S. Miller 11581658ad0SDavid S. Miller src64 = (const u64 *)walk.src.virt.addr; 11681658ad0SDavid S. Miller dst64 = (u64 *) walk.dst.virt.addr; 11781658ad0SDavid S. Miller op(src64, dst64, block_len, key); 11881658ad0SDavid S. Miller } 11981658ad0SDavid S. Miller nbytes &= CAMELLIA_BLOCK_SIZE - 1; 12081658ad0SDavid S. Miller err = blkcipher_walk_done(desc, &walk, nbytes); 12181658ad0SDavid S. Miller } 12281658ad0SDavid S. Miller fprs_write(0); 12381658ad0SDavid S. Miller return err; 12481658ad0SDavid S. Miller } 12581658ad0SDavid S. Miller 12681658ad0SDavid S. Miller static int ecb_encrypt(struct blkcipher_desc *desc, 12781658ad0SDavid S. Miller struct scatterlist *dst, struct scatterlist *src, 12881658ad0SDavid S. Miller unsigned int nbytes) 12981658ad0SDavid S. Miller { 13081658ad0SDavid S. Miller return __ecb_crypt(desc, dst, src, nbytes, true); 13181658ad0SDavid S. Miller } 13281658ad0SDavid S. Miller 13381658ad0SDavid S. Miller static int ecb_decrypt(struct blkcipher_desc *desc, 13481658ad0SDavid S. Miller struct scatterlist *dst, struct scatterlist *src, 13581658ad0SDavid S. Miller unsigned int nbytes) 13681658ad0SDavid S. Miller { 13781658ad0SDavid S. Miller return __ecb_crypt(desc, dst, src, nbytes, false); 13881658ad0SDavid S. Miller } 13981658ad0SDavid S. Miller 14081658ad0SDavid S. Miller typedef void cbc_crypt_op(const u64 *input, u64 *output, unsigned int len, 14181658ad0SDavid S. Miller const u64 *key, u64 *iv); 14281658ad0SDavid S. Miller 14381658ad0SDavid S. Miller extern cbc_crypt_op camellia_sparc64_cbc_encrypt_3_grand_rounds; 14481658ad0SDavid S. Miller extern cbc_crypt_op camellia_sparc64_cbc_encrypt_4_grand_rounds; 14581658ad0SDavid S. Miller extern cbc_crypt_op camellia_sparc64_cbc_decrypt_3_grand_rounds; 14681658ad0SDavid S. Miller extern cbc_crypt_op camellia_sparc64_cbc_decrypt_4_grand_rounds; 14781658ad0SDavid S. Miller 14881658ad0SDavid S. Miller static int cbc_encrypt(struct blkcipher_desc *desc, 14981658ad0SDavid S. Miller struct scatterlist *dst, struct scatterlist *src, 15081658ad0SDavid S. Miller unsigned int nbytes) 15181658ad0SDavid S. Miller { 15281658ad0SDavid S. Miller struct camellia_sparc64_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); 15381658ad0SDavid S. Miller struct blkcipher_walk walk; 15481658ad0SDavid S. Miller cbc_crypt_op *op; 15581658ad0SDavid S. Miller const u64 *key; 15681658ad0SDavid S. Miller int err; 15781658ad0SDavid S. Miller 15881658ad0SDavid S. Miller op = camellia_sparc64_cbc_encrypt_3_grand_rounds; 15981658ad0SDavid S. Miller if (ctx->key_len != 16) 16081658ad0SDavid S. Miller op = camellia_sparc64_cbc_encrypt_4_grand_rounds; 16181658ad0SDavid S. Miller 16281658ad0SDavid S. Miller blkcipher_walk_init(&walk, dst, src, nbytes); 16381658ad0SDavid S. Miller err = blkcipher_walk_virt(desc, &walk); 16462ba63dcSDavid S. Miller desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; 16581658ad0SDavid S. Miller 16681658ad0SDavid S. Miller key = &ctx->encrypt_key[0]; 16781658ad0SDavid S. Miller camellia_sparc64_load_keys(key, ctx->key_len); 16881658ad0SDavid S. Miller while ((nbytes = walk.nbytes)) { 16981658ad0SDavid S. Miller unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK; 17081658ad0SDavid S. Miller 17181658ad0SDavid S. Miller if (likely(block_len)) { 17281658ad0SDavid S. Miller const u64 *src64; 17381658ad0SDavid S. Miller u64 *dst64; 17481658ad0SDavid S. Miller 17581658ad0SDavid S. Miller src64 = (const u64 *)walk.src.virt.addr; 17681658ad0SDavid S. Miller dst64 = (u64 *) walk.dst.virt.addr; 17781658ad0SDavid S. Miller op(src64, dst64, block_len, key, 17881658ad0SDavid S. Miller (u64 *) walk.iv); 17981658ad0SDavid S. Miller } 18081658ad0SDavid S. Miller nbytes &= CAMELLIA_BLOCK_SIZE - 1; 18181658ad0SDavid S. Miller err = blkcipher_walk_done(desc, &walk, nbytes); 18281658ad0SDavid S. Miller } 18381658ad0SDavid S. Miller fprs_write(0); 18481658ad0SDavid S. Miller return err; 18581658ad0SDavid S. Miller } 18681658ad0SDavid S. Miller 18781658ad0SDavid S. Miller static int cbc_decrypt(struct blkcipher_desc *desc, 18881658ad0SDavid S. Miller struct scatterlist *dst, struct scatterlist *src, 18981658ad0SDavid S. Miller unsigned int nbytes) 19081658ad0SDavid S. Miller { 19181658ad0SDavid S. Miller struct camellia_sparc64_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); 19281658ad0SDavid S. Miller struct blkcipher_walk walk; 19381658ad0SDavid S. Miller cbc_crypt_op *op; 19481658ad0SDavid S. Miller const u64 *key; 19581658ad0SDavid S. Miller int err; 19681658ad0SDavid S. Miller 19781658ad0SDavid S. Miller op = camellia_sparc64_cbc_decrypt_3_grand_rounds; 19881658ad0SDavid S. Miller if (ctx->key_len != 16) 19981658ad0SDavid S. Miller op = camellia_sparc64_cbc_decrypt_4_grand_rounds; 20081658ad0SDavid S. Miller 20181658ad0SDavid S. Miller blkcipher_walk_init(&walk, dst, src, nbytes); 20281658ad0SDavid S. Miller err = blkcipher_walk_virt(desc, &walk); 20362ba63dcSDavid S. Miller desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; 20481658ad0SDavid S. Miller 20581658ad0SDavid S. Miller key = &ctx->decrypt_key[0]; 20681658ad0SDavid S. Miller camellia_sparc64_load_keys(key, ctx->key_len); 20781658ad0SDavid S. Miller while ((nbytes = walk.nbytes)) { 20881658ad0SDavid S. Miller unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK; 20981658ad0SDavid S. Miller 21081658ad0SDavid S. Miller if (likely(block_len)) { 21181658ad0SDavid S. Miller const u64 *src64; 21281658ad0SDavid S. Miller u64 *dst64; 21381658ad0SDavid S. Miller 21481658ad0SDavid S. Miller src64 = (const u64 *)walk.src.virt.addr; 21581658ad0SDavid S. Miller dst64 = (u64 *) walk.dst.virt.addr; 21681658ad0SDavid S. Miller op(src64, dst64, block_len, key, 21781658ad0SDavid S. Miller (u64 *) walk.iv); 21881658ad0SDavid S. Miller } 21981658ad0SDavid S. Miller nbytes &= CAMELLIA_BLOCK_SIZE - 1; 22081658ad0SDavid S. Miller err = blkcipher_walk_done(desc, &walk, nbytes); 22181658ad0SDavid S. Miller } 22281658ad0SDavid S. Miller fprs_write(0); 22381658ad0SDavid S. Miller return err; 22481658ad0SDavid S. Miller } 22581658ad0SDavid S. Miller 22681658ad0SDavid S. Miller static struct crypto_alg algs[] = { { 22781658ad0SDavid S. Miller .cra_name = "camellia", 22881658ad0SDavid S. Miller .cra_driver_name = "camellia-sparc64", 22910803624SDavid S. Miller .cra_priority = SPARC_CR_OPCODE_PRIORITY, 23081658ad0SDavid S. Miller .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 23181658ad0SDavid S. Miller .cra_blocksize = CAMELLIA_BLOCK_SIZE, 23281658ad0SDavid S. Miller .cra_ctxsize = sizeof(struct camellia_sparc64_ctx), 23381658ad0SDavid S. Miller .cra_alignmask = 3, 23481658ad0SDavid S. Miller .cra_module = THIS_MODULE, 23581658ad0SDavid S. Miller .cra_u = { 23681658ad0SDavid S. Miller .cipher = { 23781658ad0SDavid S. Miller .cia_min_keysize = CAMELLIA_MIN_KEY_SIZE, 23881658ad0SDavid S. Miller .cia_max_keysize = CAMELLIA_MAX_KEY_SIZE, 23981658ad0SDavid S. Miller .cia_setkey = camellia_set_key, 24081658ad0SDavid S. Miller .cia_encrypt = camellia_encrypt, 24181658ad0SDavid S. Miller .cia_decrypt = camellia_decrypt 24281658ad0SDavid S. Miller } 24381658ad0SDavid S. Miller } 24481658ad0SDavid S. Miller }, { 24581658ad0SDavid S. Miller .cra_name = "ecb(camellia)", 24681658ad0SDavid S. Miller .cra_driver_name = "ecb-camellia-sparc64", 24710803624SDavid S. Miller .cra_priority = SPARC_CR_OPCODE_PRIORITY, 24881658ad0SDavid S. Miller .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, 24981658ad0SDavid S. Miller .cra_blocksize = CAMELLIA_BLOCK_SIZE, 25081658ad0SDavid S. Miller .cra_ctxsize = sizeof(struct camellia_sparc64_ctx), 25181658ad0SDavid S. Miller .cra_alignmask = 7, 25281658ad0SDavid S. Miller .cra_type = &crypto_blkcipher_type, 25381658ad0SDavid S. Miller .cra_module = THIS_MODULE, 25481658ad0SDavid S. Miller .cra_u = { 25581658ad0SDavid S. Miller .blkcipher = { 25681658ad0SDavid S. Miller .min_keysize = CAMELLIA_MIN_KEY_SIZE, 25781658ad0SDavid S. Miller .max_keysize = CAMELLIA_MAX_KEY_SIZE, 25881658ad0SDavid S. Miller .setkey = camellia_set_key, 25981658ad0SDavid S. Miller .encrypt = ecb_encrypt, 26081658ad0SDavid S. Miller .decrypt = ecb_decrypt, 26181658ad0SDavid S. Miller }, 26281658ad0SDavid S. Miller }, 26381658ad0SDavid S. Miller }, { 26481658ad0SDavid S. Miller .cra_name = "cbc(camellia)", 26581658ad0SDavid S. Miller .cra_driver_name = "cbc-camellia-sparc64", 26610803624SDavid S. Miller .cra_priority = SPARC_CR_OPCODE_PRIORITY, 26781658ad0SDavid S. Miller .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, 26881658ad0SDavid S. Miller .cra_blocksize = CAMELLIA_BLOCK_SIZE, 26981658ad0SDavid S. Miller .cra_ctxsize = sizeof(struct camellia_sparc64_ctx), 27081658ad0SDavid S. Miller .cra_alignmask = 7, 27181658ad0SDavid S. Miller .cra_type = &crypto_blkcipher_type, 27281658ad0SDavid S. Miller .cra_module = THIS_MODULE, 27381658ad0SDavid S. Miller .cra_u = { 27481658ad0SDavid S. Miller .blkcipher = { 27581658ad0SDavid S. Miller .min_keysize = CAMELLIA_MIN_KEY_SIZE, 27681658ad0SDavid S. Miller .max_keysize = CAMELLIA_MAX_KEY_SIZE, 27781658ad0SDavid S. Miller .setkey = camellia_set_key, 27881658ad0SDavid S. Miller .encrypt = cbc_encrypt, 27981658ad0SDavid S. Miller .decrypt = cbc_decrypt, 28081658ad0SDavid S. Miller }, 28181658ad0SDavid S. Miller }, 28281658ad0SDavid S. Miller } 28381658ad0SDavid S. Miller }; 28481658ad0SDavid S. Miller 28581658ad0SDavid S. Miller static bool __init sparc64_has_camellia_opcode(void) 28681658ad0SDavid S. Miller { 28781658ad0SDavid S. Miller unsigned long cfr; 28881658ad0SDavid S. Miller 28981658ad0SDavid S. Miller if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO)) 29081658ad0SDavid S. Miller return false; 29181658ad0SDavid S. Miller 29281658ad0SDavid S. Miller __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr)); 29381658ad0SDavid S. Miller if (!(cfr & CFR_CAMELLIA)) 29481658ad0SDavid S. Miller return false; 29581658ad0SDavid S. Miller 29681658ad0SDavid S. Miller return true; 29781658ad0SDavid S. Miller } 29881658ad0SDavid S. Miller 29981658ad0SDavid S. Miller static int __init camellia_sparc64_mod_init(void) 30081658ad0SDavid S. Miller { 30181658ad0SDavid S. Miller int i; 30281658ad0SDavid S. Miller 30381658ad0SDavid S. Miller for (i = 0; i < ARRAY_SIZE(algs); i++) 30481658ad0SDavid S. Miller INIT_LIST_HEAD(&algs[i].cra_list); 30581658ad0SDavid S. Miller 30681658ad0SDavid S. Miller if (sparc64_has_camellia_opcode()) { 30781658ad0SDavid S. Miller pr_info("Using sparc64 camellia opcodes optimized CAMELLIA implementation\n"); 30881658ad0SDavid S. Miller return crypto_register_algs(algs, ARRAY_SIZE(algs)); 30981658ad0SDavid S. Miller } 31081658ad0SDavid S. Miller pr_info("sparc64 camellia opcodes not available.\n"); 31181658ad0SDavid S. Miller return -ENODEV; 31281658ad0SDavid S. Miller } 31381658ad0SDavid S. Miller 31481658ad0SDavid S. Miller static void __exit camellia_sparc64_mod_fini(void) 31581658ad0SDavid S. Miller { 31681658ad0SDavid S. Miller crypto_unregister_algs(algs, ARRAY_SIZE(algs)); 31781658ad0SDavid S. Miller } 31881658ad0SDavid S. Miller 31981658ad0SDavid S. Miller module_init(camellia_sparc64_mod_init); 32081658ad0SDavid S. Miller module_exit(camellia_sparc64_mod_fini); 32181658ad0SDavid S. Miller 32281658ad0SDavid S. Miller MODULE_LICENSE("GPL"); 32381658ad0SDavid S. Miller MODULE_DESCRIPTION("Camellia Cipher Algorithm, sparc64 camellia opcode accelerated"); 32481658ad0SDavid S. Miller 325*7d676fdbSMathias Krause MODULE_ALIAS_CRYPTO("camellia"); 326226f7ceaSDavid S. Miller 327226f7ceaSDavid S. Miller #include "crop_devid.c" 328