crypto: x86/twofish - drop CTR mode implementation
[linux-2.6-microblaze.git] / arch / x86 / crypto / twofish_avx_glue.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Glue Code for AVX assembler version of Twofish Cipher
4  *
5  * Copyright (C) 2012 Johannes Goetzfried
6  *     <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
7  *
8  * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
9  */
10
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/crypto.h>
14 #include <linux/err.h>
15 #include <crypto/algapi.h>
16 #include <crypto/internal/simd.h>
17 #include <crypto/twofish.h>
18 #include <asm/crypto/glue_helper.h>
19 #include <asm/crypto/twofish.h>
20
21 #define TWOFISH_PARALLEL_BLOCKS 8
22
23 /* 8-way parallel cipher functions */
24 asmlinkage void twofish_ecb_enc_8way(const void *ctx, u8 *dst, const u8 *src);
25 asmlinkage void twofish_ecb_dec_8way(const void *ctx, u8 *dst, const u8 *src);
26
27 asmlinkage void twofish_cbc_dec_8way(const void *ctx, u8 *dst, const u8 *src);
28
29 static int twofish_setkey_skcipher(struct crypto_skcipher *tfm,
30                                    const u8 *key, unsigned int keylen)
31 {
32         return twofish_setkey(&tfm->base, key, keylen);
33 }
34
35 static inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src)
36 {
37         __twofish_enc_blk_3way(ctx, dst, src, false);
38 }
39
40 static const struct common_glue_ctx twofish_enc = {
41         .num_funcs = 3,
42         .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
43
44         .funcs = { {
45                 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
46                 .fn_u = { .ecb = twofish_ecb_enc_8way }
47         }, {
48                 .num_blocks = 3,
49                 .fn_u = { .ecb = twofish_enc_blk_3way }
50         }, {
51                 .num_blocks = 1,
52                 .fn_u = { .ecb = twofish_enc_blk }
53         } }
54 };
55
56 static const struct common_glue_ctx twofish_dec = {
57         .num_funcs = 3,
58         .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
59
60         .funcs = { {
61                 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
62                 .fn_u = { .ecb = twofish_ecb_dec_8way }
63         }, {
64                 .num_blocks = 3,
65                 .fn_u = { .ecb = twofish_dec_blk_3way }
66         }, {
67                 .num_blocks = 1,
68                 .fn_u = { .ecb = twofish_dec_blk }
69         } }
70 };
71
72 static const struct common_glue_ctx twofish_dec_cbc = {
73         .num_funcs = 3,
74         .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
75
76         .funcs = { {
77                 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
78                 .fn_u = { .cbc = twofish_cbc_dec_8way }
79         }, {
80                 .num_blocks = 3,
81                 .fn_u = { .cbc = twofish_dec_blk_cbc_3way }
82         }, {
83                 .num_blocks = 1,
84                 .fn_u = { .cbc = twofish_dec_blk }
85         } }
86 };
87
88 static int ecb_encrypt(struct skcipher_request *req)
89 {
90         return glue_ecb_req_128bit(&twofish_enc, req);
91 }
92
93 static int ecb_decrypt(struct skcipher_request *req)
94 {
95         return glue_ecb_req_128bit(&twofish_dec, req);
96 }
97
98 static int cbc_encrypt(struct skcipher_request *req)
99 {
100         return glue_cbc_encrypt_req_128bit(twofish_enc_blk, req);
101 }
102
103 static int cbc_decrypt(struct skcipher_request *req)
104 {
105         return glue_cbc_decrypt_req_128bit(&twofish_dec_cbc, req);
106 }
107
108 static struct skcipher_alg twofish_algs[] = {
109         {
110                 .base.cra_name          = "__ecb(twofish)",
111                 .base.cra_driver_name   = "__ecb-twofish-avx",
112                 .base.cra_priority      = 400,
113                 .base.cra_flags         = CRYPTO_ALG_INTERNAL,
114                 .base.cra_blocksize     = TF_BLOCK_SIZE,
115                 .base.cra_ctxsize       = sizeof(struct twofish_ctx),
116                 .base.cra_module        = THIS_MODULE,
117                 .min_keysize            = TF_MIN_KEY_SIZE,
118                 .max_keysize            = TF_MAX_KEY_SIZE,
119                 .setkey                 = twofish_setkey_skcipher,
120                 .encrypt                = ecb_encrypt,
121                 .decrypt                = ecb_decrypt,
122         }, {
123                 .base.cra_name          = "__cbc(twofish)",
124                 .base.cra_driver_name   = "__cbc-twofish-avx",
125                 .base.cra_priority      = 400,
126                 .base.cra_flags         = CRYPTO_ALG_INTERNAL,
127                 .base.cra_blocksize     = TF_BLOCK_SIZE,
128                 .base.cra_ctxsize       = sizeof(struct twofish_ctx),
129                 .base.cra_module        = THIS_MODULE,
130                 .min_keysize            = TF_MIN_KEY_SIZE,
131                 .max_keysize            = TF_MAX_KEY_SIZE,
132                 .ivsize                 = TF_BLOCK_SIZE,
133                 .setkey                 = twofish_setkey_skcipher,
134                 .encrypt                = cbc_encrypt,
135                 .decrypt                = cbc_decrypt,
136         },
137 };
138
139 static struct simd_skcipher_alg *twofish_simd_algs[ARRAY_SIZE(twofish_algs)];
140
141 static int __init twofish_init(void)
142 {
143         const char *feature_name;
144
145         if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, &feature_name)) {
146                 pr_info("CPU feature '%s' is not supported.\n", feature_name);
147                 return -ENODEV;
148         }
149
150         return simd_register_skciphers_compat(twofish_algs,
151                                               ARRAY_SIZE(twofish_algs),
152                                               twofish_simd_algs);
153 }
154
155 static void __exit twofish_exit(void)
156 {
157         simd_unregister_skciphers(twofish_algs, ARRAY_SIZE(twofish_algs),
158                                   twofish_simd_algs);
159 }
160
161 module_init(twofish_init);
162 module_exit(twofish_exit);
163
164 MODULE_DESCRIPTION("Twofish Cipher Algorithm, AVX optimized");
165 MODULE_LICENSE("GPL");
166 MODULE_ALIAS_CRYPTO("twofish");