1 /* Glue code for CAMELLIA encryption optimized for sparc64 crypto opcodes.
3 * Copyright (C) 2012 David S. Miller <davem@davemloft.net>
6 #include <linux/crypto.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <crypto/algapi.h>
13 #include <asm/fpumacro.h>
14 #include <asm/pstate.h>
17 #define CAMELLIA_MIN_KEY_SIZE 16
18 #define CAMELLIA_MAX_KEY_SIZE 32
19 #define CAMELLIA_BLOCK_SIZE 16
20 #define CAMELLIA_TABLE_BYTE_LEN 272
22 struct camellia_sparc64_ctx {
23 u64 encrypt_key[CAMELLIA_TABLE_BYTE_LEN / sizeof(u64)];
24 u64 decrypt_key[CAMELLIA_TABLE_BYTE_LEN / sizeof(u64)];
28 extern void camellia_sparc64_key_expand(const u32 *in_key, u64 *encrypt_key,
29 unsigned int key_len, u64 *decrypt_key);
31 static int camellia_set_key(struct crypto_tfm *tfm, const u8 *_in_key,
34 struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
35 const u32 *in_key = (const u32 *) _in_key;
36 u32 *flags = &tfm->crt_flags;
38 if (key_len != 16 && key_len != 24 && key_len != 32) {
39 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
43 ctx->key_len = key_len;
45 camellia_sparc64_key_expand(in_key, &ctx->encrypt_key[0],
46 key_len, &ctx->decrypt_key[0]);
50 extern void camellia_sparc64_crypt(const u64 *key, const u32 *input,
51 u32 *output, unsigned int key_len);
53 static void camellia_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
55 struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
57 camellia_sparc64_crypt(&ctx->encrypt_key[0],
59 (u32 *) dst, ctx->key_len);
62 static void camellia_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
64 struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
66 camellia_sparc64_crypt(&ctx->decrypt_key[0],
68 (u32 *) dst, ctx->key_len);
71 extern void camellia_sparc64_load_keys(const u64 *key, unsigned int key_len);
73 typedef void ecb_crypt_op(const u64 *input, u64 *output, unsigned int len,
76 extern ecb_crypt_op camellia_sparc64_ecb_crypt_3_grand_rounds;
77 extern ecb_crypt_op camellia_sparc64_ecb_crypt_4_grand_rounds;
79 #define CAMELLIA_BLOCK_MASK (~(CAMELLIA_BLOCK_SIZE - 1))
81 static int __ecb_crypt(struct blkcipher_desc *desc,
82 struct scatterlist *dst, struct scatterlist *src,
83 unsigned int nbytes, bool encrypt)
85 struct camellia_sparc64_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
86 struct blkcipher_walk walk;
91 op = camellia_sparc64_ecb_crypt_3_grand_rounds;
92 if (ctx->key_len != 16)
93 op = camellia_sparc64_ecb_crypt_4_grand_rounds;
95 blkcipher_walk_init(&walk, dst, src, nbytes);
96 err = blkcipher_walk_virt(desc, &walk);
99 key = &ctx->encrypt_key[0];
101 key = &ctx->decrypt_key[0];
102 camellia_sparc64_load_keys(key, ctx->key_len);
103 while ((nbytes = walk.nbytes)) {
104 unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK;
106 if (likely(block_len)) {
110 src64 = (const u64 *)walk.src.virt.addr;
111 dst64 = (u64 *) walk.dst.virt.addr;
112 op(src64, dst64, block_len, key);
114 nbytes &= CAMELLIA_BLOCK_SIZE - 1;
115 err = blkcipher_walk_done(desc, &walk, nbytes);
121 static int ecb_encrypt(struct blkcipher_desc *desc,
122 struct scatterlist *dst, struct scatterlist *src,
125 return __ecb_crypt(desc, dst, src, nbytes, true);
128 static int ecb_decrypt(struct blkcipher_desc *desc,
129 struct scatterlist *dst, struct scatterlist *src,
132 return __ecb_crypt(desc, dst, src, nbytes, false);
135 typedef void cbc_crypt_op(const u64 *input, u64 *output, unsigned int len,
136 const u64 *key, u64 *iv);
138 extern cbc_crypt_op camellia_sparc64_cbc_encrypt_3_grand_rounds;
139 extern cbc_crypt_op camellia_sparc64_cbc_encrypt_4_grand_rounds;
140 extern cbc_crypt_op camellia_sparc64_cbc_decrypt_3_grand_rounds;
141 extern cbc_crypt_op camellia_sparc64_cbc_decrypt_4_grand_rounds;
143 static int cbc_encrypt(struct blkcipher_desc *desc,
144 struct scatterlist *dst, struct scatterlist *src,
147 struct camellia_sparc64_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
148 struct blkcipher_walk walk;
153 op = camellia_sparc64_cbc_encrypt_3_grand_rounds;
154 if (ctx->key_len != 16)
155 op = camellia_sparc64_cbc_encrypt_4_grand_rounds;
157 blkcipher_walk_init(&walk, dst, src, nbytes);
158 err = blkcipher_walk_virt(desc, &walk);
160 key = &ctx->encrypt_key[0];
161 camellia_sparc64_load_keys(key, ctx->key_len);
162 while ((nbytes = walk.nbytes)) {
163 unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK;
165 if (likely(block_len)) {
169 src64 = (const u64 *)walk.src.virt.addr;
170 dst64 = (u64 *) walk.dst.virt.addr;
171 op(src64, dst64, block_len, key,
174 nbytes &= CAMELLIA_BLOCK_SIZE - 1;
175 err = blkcipher_walk_done(desc, &walk, nbytes);
181 static int cbc_decrypt(struct blkcipher_desc *desc,
182 struct scatterlist *dst, struct scatterlist *src,
185 struct camellia_sparc64_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
186 struct blkcipher_walk walk;
191 op = camellia_sparc64_cbc_decrypt_3_grand_rounds;
192 if (ctx->key_len != 16)
193 op = camellia_sparc64_cbc_decrypt_4_grand_rounds;
195 blkcipher_walk_init(&walk, dst, src, nbytes);
196 err = blkcipher_walk_virt(desc, &walk);
198 key = &ctx->decrypt_key[0];
199 camellia_sparc64_load_keys(key, ctx->key_len);
200 while ((nbytes = walk.nbytes)) {
201 unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK;
203 if (likely(block_len)) {
207 src64 = (const u64 *)walk.src.virt.addr;
208 dst64 = (u64 *) walk.dst.virt.addr;
209 op(src64, dst64, block_len, key,
212 nbytes &= CAMELLIA_BLOCK_SIZE - 1;
213 err = blkcipher_walk_done(desc, &walk, nbytes);
219 static struct crypto_alg algs[] = { {
220 .cra_name = "camellia",
221 .cra_driver_name = "camellia-sparc64",
223 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
224 .cra_blocksize = CAMELLIA_BLOCK_SIZE,
225 .cra_ctxsize = sizeof(struct camellia_sparc64_ctx),
227 .cra_module = THIS_MODULE,
230 .cia_min_keysize = CAMELLIA_MIN_KEY_SIZE,
231 .cia_max_keysize = CAMELLIA_MAX_KEY_SIZE,
232 .cia_setkey = camellia_set_key,
233 .cia_encrypt = camellia_encrypt,
234 .cia_decrypt = camellia_decrypt
238 .cra_name = "ecb(camellia)",
239 .cra_driver_name = "ecb-camellia-sparc64",
241 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
242 .cra_blocksize = CAMELLIA_BLOCK_SIZE,
243 .cra_ctxsize = sizeof(struct camellia_sparc64_ctx),
245 .cra_type = &crypto_blkcipher_type,
246 .cra_module = THIS_MODULE,
249 .min_keysize = CAMELLIA_MIN_KEY_SIZE,
250 .max_keysize = CAMELLIA_MAX_KEY_SIZE,
251 .setkey = camellia_set_key,
252 .encrypt = ecb_encrypt,
253 .decrypt = ecb_decrypt,
257 .cra_name = "cbc(camellia)",
258 .cra_driver_name = "cbc-camellia-sparc64",
260 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
261 .cra_blocksize = CAMELLIA_BLOCK_SIZE,
262 .cra_ctxsize = sizeof(struct camellia_sparc64_ctx),
264 .cra_type = &crypto_blkcipher_type,
265 .cra_module = THIS_MODULE,
268 .min_keysize = CAMELLIA_MIN_KEY_SIZE,
269 .max_keysize = CAMELLIA_MAX_KEY_SIZE,
270 .setkey = camellia_set_key,
271 .encrypt = cbc_encrypt,
272 .decrypt = cbc_decrypt,
278 static bool __init sparc64_has_camellia_opcode(void)
282 if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
285 __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
286 if (!(cfr & CFR_CAMELLIA))
292 static int __init camellia_sparc64_mod_init(void)
296 for (i = 0; i < ARRAY_SIZE(algs); i++)
297 INIT_LIST_HEAD(&algs[i].cra_list);
299 if (sparc64_has_camellia_opcode()) {
300 pr_info("Using sparc64 camellia opcodes optimized CAMELLIA implementation\n");
301 return crypto_register_algs(algs, ARRAY_SIZE(algs));
303 pr_info("sparc64 camellia opcodes not available.\n");
307 static void __exit camellia_sparc64_mod_fini(void)
309 crypto_unregister_algs(algs, ARRAY_SIZE(algs));
312 module_init(camellia_sparc64_mod_init);
313 module_exit(camellia_sparc64_mod_fini);
315 MODULE_LICENSE("GPL");
316 MODULE_DESCRIPTION("Camellia Cipher Algorithm, sparc64 camellia opcode accelerated");