Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jussi Kivilinna | 937c30d | 2011-11-09 16:26:25 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Glue Code for SSE2 assembler versions of Serpent Cipher |
| 4 | * |
| 5 | * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi> |
| 6 | * |
| 7 | * Glue code based on aesni-intel_glue.c by: |
| 8 | * Copyright (C) 2008, Intel Corp. |
| 9 | * Author: Huang Ying <ying.huang@intel.com> |
| 10 | * |
| 11 | * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by: |
| 12 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
| 13 | * CTR part based on code (crypto/ctr.c) by: |
| 14 | * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com> |
Jussi Kivilinna | 937c30d | 2011-11-09 16:26:25 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
Jussi Kivilinna | 937c30d | 2011-11-09 16:26:25 +0200 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | #include <linux/crypto.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <crypto/algapi.h> |
Jussi Kivilinna | 937c30d | 2011-11-09 16:26:25 +0200 | [diff] [blame] | 22 | #include <crypto/b128ops.h> |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 23 | #include <crypto/internal/simd.h> |
| 24 | #include <crypto/serpent.h> |
Jussi Kivilinna | d4af0e9 | 2012-06-18 14:07:45 +0300 | [diff] [blame] | 25 | #include <asm/crypto/serpent-sse2.h> |
Jussi Kivilinna | 596d875 | 2012-06-18 14:07:19 +0300 | [diff] [blame] | 26 | #include <asm/crypto/glue_helper.h> |
Jussi Kivilinna | 937c30d | 2011-11-09 16:26:25 +0200 | [diff] [blame] | 27 | |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 28 | static int serpent_setkey_skcipher(struct crypto_skcipher *tfm, |
| 29 | const u8 *key, unsigned int keylen) |
| 30 | { |
| 31 | return __serpent_setkey(crypto_skcipher_ctx(tfm), key, keylen); |
| 32 | } |
| 33 | |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 34 | static void serpent_decrypt_cbc_xway(void *ctx, u128 *dst, const u128 *src) |
| 35 | { |
| 36 | u128 ivs[SERPENT_PARALLEL_BLOCKS - 1]; |
| 37 | unsigned int j; |
| 38 | |
| 39 | for (j = 0; j < SERPENT_PARALLEL_BLOCKS - 1; j++) |
| 40 | ivs[j] = src[j]; |
| 41 | |
| 42 | serpent_dec_blk_xway(ctx, (u8 *)dst, (u8 *)src); |
| 43 | |
| 44 | for (j = 0; j < SERPENT_PARALLEL_BLOCKS - 1; j++) |
| 45 | u128_xor(dst + (j + 1), dst + (j + 1), ivs + j); |
| 46 | } |
| 47 | |
Jussi Kivilinna | 5899098 | 2012-10-20 15:06:36 +0300 | [diff] [blame] | 48 | static void serpent_crypt_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv) |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 49 | { |
| 50 | be128 ctrblk; |
| 51 | |
Jussi Kivilinna | 5899098 | 2012-10-20 15:06:36 +0300 | [diff] [blame] | 52 | le128_to_be128(&ctrblk, iv); |
| 53 | le128_inc(iv); |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 54 | |
| 55 | __serpent_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk); |
| 56 | u128_xor(dst, src, (u128 *)&ctrblk); |
| 57 | } |
| 58 | |
| 59 | static void serpent_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src, |
Jussi Kivilinna | 5899098 | 2012-10-20 15:06:36 +0300 | [diff] [blame] | 60 | le128 *iv) |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 61 | { |
| 62 | be128 ctrblks[SERPENT_PARALLEL_BLOCKS]; |
| 63 | unsigned int i; |
| 64 | |
| 65 | for (i = 0; i < SERPENT_PARALLEL_BLOCKS; i++) { |
| 66 | if (dst != src) |
| 67 | dst[i] = src[i]; |
| 68 | |
Jussi Kivilinna | 5899098 | 2012-10-20 15:06:36 +0300 | [diff] [blame] | 69 | le128_to_be128(&ctrblks[i], iv); |
| 70 | le128_inc(iv); |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | serpent_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks); |
| 74 | } |
| 75 | |
| 76 | static const struct common_glue_ctx serpent_enc = { |
| 77 | .num_funcs = 2, |
| 78 | .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS, |
| 79 | |
| 80 | .funcs = { { |
| 81 | .num_blocks = SERPENT_PARALLEL_BLOCKS, |
| 82 | .fn_u = { .ecb = GLUE_FUNC_CAST(serpent_enc_blk_xway) } |
| 83 | }, { |
| 84 | .num_blocks = 1, |
| 85 | .fn_u = { .ecb = GLUE_FUNC_CAST(__serpent_encrypt) } |
| 86 | } } |
| 87 | }; |
| 88 | |
| 89 | static const struct common_glue_ctx serpent_ctr = { |
| 90 | .num_funcs = 2, |
| 91 | .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS, |
| 92 | |
| 93 | .funcs = { { |
| 94 | .num_blocks = SERPENT_PARALLEL_BLOCKS, |
| 95 | .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(serpent_crypt_ctr_xway) } |
| 96 | }, { |
| 97 | .num_blocks = 1, |
| 98 | .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(serpent_crypt_ctr) } |
| 99 | } } |
| 100 | }; |
| 101 | |
| 102 | static const struct common_glue_ctx serpent_dec = { |
| 103 | .num_funcs = 2, |
| 104 | .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS, |
| 105 | |
| 106 | .funcs = { { |
| 107 | .num_blocks = SERPENT_PARALLEL_BLOCKS, |
| 108 | .fn_u = { .ecb = GLUE_FUNC_CAST(serpent_dec_blk_xway) } |
| 109 | }, { |
| 110 | .num_blocks = 1, |
| 111 | .fn_u = { .ecb = GLUE_FUNC_CAST(__serpent_decrypt) } |
| 112 | } } |
| 113 | }; |
| 114 | |
| 115 | static const struct common_glue_ctx serpent_dec_cbc = { |
| 116 | .num_funcs = 2, |
| 117 | .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS, |
| 118 | |
| 119 | .funcs = { { |
| 120 | .num_blocks = SERPENT_PARALLEL_BLOCKS, |
| 121 | .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(serpent_decrypt_cbc_xway) } |
| 122 | }, { |
| 123 | .num_blocks = 1, |
| 124 | .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(__serpent_decrypt) } |
| 125 | } } |
| 126 | }; |
| 127 | |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 128 | static int ecb_encrypt(struct skcipher_request *req) |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 129 | { |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 130 | return glue_ecb_req_128bit(&serpent_enc, req); |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 131 | } |
| 132 | |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 133 | static int ecb_decrypt(struct skcipher_request *req) |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 134 | { |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 135 | return glue_ecb_req_128bit(&serpent_dec, req); |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 136 | } |
| 137 | |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 138 | static int cbc_encrypt(struct skcipher_request *req) |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 139 | { |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 140 | return glue_cbc_encrypt_req_128bit(GLUE_FUNC_CAST(__serpent_encrypt), |
| 141 | req); |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 142 | } |
| 143 | |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 144 | static int cbc_decrypt(struct skcipher_request *req) |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 145 | { |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 146 | return glue_cbc_decrypt_req_128bit(&serpent_dec_cbc, req); |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 147 | } |
| 148 | |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 149 | static int ctr_crypt(struct skcipher_request *req) |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 150 | { |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 151 | return glue_ctr_req_128bit(&serpent_ctr, req); |
Jussi Kivilinna | e81792f | 2012-06-18 14:07:14 +0300 | [diff] [blame] | 152 | } |
| 153 | |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 154 | static struct skcipher_alg serpent_algs[] = { |
| 155 | { |
| 156 | .base.cra_name = "__ecb(serpent)", |
| 157 | .base.cra_driver_name = "__ecb-serpent-sse2", |
| 158 | .base.cra_priority = 400, |
| 159 | .base.cra_flags = CRYPTO_ALG_INTERNAL, |
| 160 | .base.cra_blocksize = SERPENT_BLOCK_SIZE, |
| 161 | .base.cra_ctxsize = sizeof(struct serpent_ctx), |
| 162 | .base.cra_module = THIS_MODULE, |
| 163 | .min_keysize = SERPENT_MIN_KEY_SIZE, |
| 164 | .max_keysize = SERPENT_MAX_KEY_SIZE, |
| 165 | .setkey = serpent_setkey_skcipher, |
| 166 | .encrypt = ecb_encrypt, |
| 167 | .decrypt = ecb_decrypt, |
| 168 | }, { |
| 169 | .base.cra_name = "__cbc(serpent)", |
| 170 | .base.cra_driver_name = "__cbc-serpent-sse2", |
| 171 | .base.cra_priority = 400, |
| 172 | .base.cra_flags = CRYPTO_ALG_INTERNAL, |
| 173 | .base.cra_blocksize = SERPENT_BLOCK_SIZE, |
| 174 | .base.cra_ctxsize = sizeof(struct serpent_ctx), |
| 175 | .base.cra_module = THIS_MODULE, |
| 176 | .min_keysize = SERPENT_MIN_KEY_SIZE, |
| 177 | .max_keysize = SERPENT_MAX_KEY_SIZE, |
| 178 | .ivsize = SERPENT_BLOCK_SIZE, |
| 179 | .setkey = serpent_setkey_skcipher, |
| 180 | .encrypt = cbc_encrypt, |
| 181 | .decrypt = cbc_decrypt, |
| 182 | }, { |
| 183 | .base.cra_name = "__ctr(serpent)", |
| 184 | .base.cra_driver_name = "__ctr-serpent-sse2", |
| 185 | .base.cra_priority = 400, |
| 186 | .base.cra_flags = CRYPTO_ALG_INTERNAL, |
| 187 | .base.cra_blocksize = 1, |
| 188 | .base.cra_ctxsize = sizeof(struct serpent_ctx), |
| 189 | .base.cra_module = THIS_MODULE, |
| 190 | .min_keysize = SERPENT_MIN_KEY_SIZE, |
| 191 | .max_keysize = SERPENT_MAX_KEY_SIZE, |
| 192 | .ivsize = SERPENT_BLOCK_SIZE, |
| 193 | .chunksize = SERPENT_BLOCK_SIZE, |
| 194 | .setkey = serpent_setkey_skcipher, |
| 195 | .encrypt = ctr_crypt, |
| 196 | .decrypt = ctr_crypt, |
Jussi Kivilinna | 35474c3 | 2012-02-17 22:48:37 +0200 | [diff] [blame] | 197 | }, |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | static struct simd_skcipher_alg *serpent_simd_algs[ARRAY_SIZE(serpent_algs)]; |
Jussi Kivilinna | 5962f8b | 2011-11-09 16:26:41 +0200 | [diff] [blame] | 201 | |
Jussi Kivilinna | 937c30d | 2011-11-09 16:26:25 +0200 | [diff] [blame] | 202 | static int __init serpent_sse2_init(void) |
| 203 | { |
Borislav Petkov | 054efb6 | 2016-03-29 17:42:00 +0200 | [diff] [blame] | 204 | if (!boot_cpu_has(X86_FEATURE_XMM2)) { |
Jussi Kivilinna | 937c30d | 2011-11-09 16:26:25 +0200 | [diff] [blame] | 205 | printk(KERN_INFO "SSE2 instructions are not detected.\n"); |
| 206 | return -ENODEV; |
| 207 | } |
| 208 | |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 209 | return simd_register_skciphers_compat(serpent_algs, |
| 210 | ARRAY_SIZE(serpent_algs), |
| 211 | serpent_simd_algs); |
Jussi Kivilinna | 937c30d | 2011-11-09 16:26:25 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void __exit serpent_sse2_exit(void) |
| 215 | { |
Eric Biggers | e0f409d | 2018-02-19 23:48:03 -0800 | [diff] [blame] | 216 | simd_unregister_skciphers(serpent_algs, ARRAY_SIZE(serpent_algs), |
| 217 | serpent_simd_algs); |
Jussi Kivilinna | 937c30d | 2011-11-09 16:26:25 +0200 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | module_init(serpent_sse2_init); |
| 221 | module_exit(serpent_sse2_exit); |
| 222 | |
| 223 | MODULE_DESCRIPTION("Serpent Cipher Algorithm, SSE2 optimized"); |
| 224 | MODULE_LICENSE("GPL"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 225 | MODULE_ALIAS_CRYPTO("serpent"); |