| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Glue Code for AVX assembler versions of Serpent Cipher |
| 4 | * |
| 5 | * Copyright (C) 2012 Johannes Goetzfried |
| 6 | * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de> |
| 7 | * |
| 8 | * Copyright © 2011-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 <linux/export.h> |
| 16 | #include <crypto/algapi.h> |
| 17 | #include <crypto/serpent.h> |
| 18 | |
| 19 | #include "serpent-avx.h" |
| 20 | #include "ecb_cbc_helpers.h" |
| 21 | |
| 22 | /* 8-way parallel cipher functions */ |
| 23 | asmlinkage void serpent_ecb_enc_8way_avx(const void *ctx, u8 *dst, |
| 24 | const u8 *src); |
| 25 | EXPORT_SYMBOL_GPL(serpent_ecb_enc_8way_avx); |
| 26 | |
| 27 | asmlinkage void serpent_ecb_dec_8way_avx(const void *ctx, u8 *dst, |
| 28 | const u8 *src); |
| 29 | EXPORT_SYMBOL_GPL(serpent_ecb_dec_8way_avx); |
| 30 | |
| 31 | asmlinkage void serpent_cbc_dec_8way_avx(const void *ctx, u8 *dst, |
| 32 | const u8 *src); |
| 33 | EXPORT_SYMBOL_GPL(serpent_cbc_dec_8way_avx); |
| 34 | |
| 35 | static int serpent_setkey_skcipher(struct crypto_skcipher *tfm, |
| 36 | const u8 *key, unsigned int keylen) |
| 37 | { |
| 38 | return __serpent_setkey(ctx: crypto_skcipher_ctx(tfm), key, keylen); |
| 39 | } |
| 40 | |
| 41 | static int ecb_encrypt(struct skcipher_request *req) |
| 42 | { |
| 43 | ECB_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS); |
| 44 | ECB_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_ecb_enc_8way_avx); |
| 45 | ECB_BLOCK(1, __serpent_encrypt); |
| 46 | ECB_WALK_END(); |
| 47 | } |
| 48 | |
| 49 | static int ecb_decrypt(struct skcipher_request *req) |
| 50 | { |
| 51 | ECB_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS); |
| 52 | ECB_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_ecb_dec_8way_avx); |
| 53 | ECB_BLOCK(1, __serpent_decrypt); |
| 54 | ECB_WALK_END(); |
| 55 | } |
| 56 | |
| 57 | static int cbc_encrypt(struct skcipher_request *req) |
| 58 | { |
| 59 | CBC_WALK_START(req, SERPENT_BLOCK_SIZE, -1); |
| 60 | CBC_ENC_BLOCK(__serpent_encrypt); |
| 61 | CBC_WALK_END(); |
| 62 | } |
| 63 | |
| 64 | static int cbc_decrypt(struct skcipher_request *req) |
| 65 | { |
| 66 | CBC_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS); |
| 67 | CBC_DEC_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_cbc_dec_8way_avx); |
| 68 | CBC_DEC_BLOCK(1, __serpent_decrypt); |
| 69 | CBC_WALK_END(); |
| 70 | } |
| 71 | |
| 72 | static struct skcipher_alg serpent_algs[] = { |
| 73 | { |
| 74 | .base.cra_name = "ecb(serpent)" , |
| 75 | .base.cra_driver_name = "ecb-serpent-avx" , |
| 76 | .base.cra_priority = 500, |
| 77 | .base.cra_blocksize = SERPENT_BLOCK_SIZE, |
| 78 | .base.cra_ctxsize = sizeof(struct serpent_ctx), |
| 79 | .base.cra_module = THIS_MODULE, |
| 80 | .min_keysize = SERPENT_MIN_KEY_SIZE, |
| 81 | .max_keysize = SERPENT_MAX_KEY_SIZE, |
| 82 | .setkey = serpent_setkey_skcipher, |
| 83 | .encrypt = ecb_encrypt, |
| 84 | .decrypt = ecb_decrypt, |
| 85 | }, { |
| 86 | .base.cra_name = "cbc(serpent)" , |
| 87 | .base.cra_driver_name = "cbc-serpent-avx" , |
| 88 | .base.cra_priority = 500, |
| 89 | .base.cra_blocksize = SERPENT_BLOCK_SIZE, |
| 90 | .base.cra_ctxsize = sizeof(struct serpent_ctx), |
| 91 | .base.cra_module = THIS_MODULE, |
| 92 | .min_keysize = SERPENT_MIN_KEY_SIZE, |
| 93 | .max_keysize = SERPENT_MAX_KEY_SIZE, |
| 94 | .ivsize = SERPENT_BLOCK_SIZE, |
| 95 | .setkey = serpent_setkey_skcipher, |
| 96 | .encrypt = cbc_encrypt, |
| 97 | .decrypt = cbc_decrypt, |
| 98 | }, |
| 99 | }; |
| 100 | |
| 101 | static int __init serpent_init(void) |
| 102 | { |
| 103 | const char *feature_name; |
| 104 | |
| 105 | if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, |
| 106 | feature_name: &feature_name)) { |
| 107 | pr_info("CPU feature '%s' is not supported.\n" , feature_name); |
| 108 | return -ENODEV; |
| 109 | } |
| 110 | |
| 111 | return crypto_register_skciphers(algs: serpent_algs, |
| 112 | ARRAY_SIZE(serpent_algs)); |
| 113 | } |
| 114 | |
| 115 | static void __exit serpent_exit(void) |
| 116 | { |
| 117 | crypto_unregister_skciphers(algs: serpent_algs, ARRAY_SIZE(serpent_algs)); |
| 118 | } |
| 119 | |
| 120 | module_init(serpent_init); |
| 121 | module_exit(serpent_exit); |
| 122 | |
| 123 | MODULE_DESCRIPTION("Serpent Cipher Algorithm, AVX optimized" ); |
| 124 | MODULE_LICENSE("GPL" ); |
| 125 | MODULE_ALIAS_CRYPTO("serpent" ); |
| 126 | |