Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | /* |
| 4 | * SM4 Cipher Algorithm. |
| 5 | * |
| 6 | * Copyright (C) 2018 ARM Limited or its affiliates. |
| 7 | * All rights reserved. |
| 8 | */ |
| 9 | |
| 10 | #include <crypto/sm4.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/crypto.h> |
| 16 | #include <asm/byteorder.h> |
| 17 | #include <asm/unaligned.h> |
| 18 | |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 19 | /** |
Tianjia Zhang | c59de48 | 2021-07-20 11:46:40 +0800 | [diff] [blame] | 20 | * sm4_setkey - Set the SM4 key. |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 21 | * @tfm: The %crypto_tfm that is used in the context. |
| 22 | * @in_key: The input key. |
| 23 | * @key_len: The size of the key. |
| 24 | * |
Tianjia Zhang | 2b31277 | 2021-07-20 11:46:39 +0800 | [diff] [blame] | 25 | * This function uses sm4_expandkey() to expand the key. |
Tianjia Zhang | c59de48 | 2021-07-20 11:46:40 +0800 | [diff] [blame] | 26 | * &sm4_ctx _must_ be the private data embedded in @tfm which is |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 27 | * retrieved with crypto_tfm_ctx(). |
Eric Biggers | 674f368 | 2019-12-30 21:19:36 -0600 | [diff] [blame] | 28 | * |
| 29 | * Return: 0 on success; -EINVAL on failure (only happens for bad key lengths) |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 30 | */ |
Tianjia Zhang | c59de48 | 2021-07-20 11:46:40 +0800 | [diff] [blame] | 31 | static int sm4_setkey(struct crypto_tfm *tfm, const u8 *in_key, |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 32 | unsigned int key_len) |
| 33 | { |
Tianjia Zhang | c59de48 | 2021-07-20 11:46:40 +0800 | [diff] [blame] | 34 | struct sm4_ctx *ctx = crypto_tfm_ctx(tfm); |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 35 | |
Tianjia Zhang | 2b31277 | 2021-07-20 11:46:39 +0800 | [diff] [blame] | 36 | return sm4_expandkey(ctx, in_key, key_len); |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 37 | } |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 38 | |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 39 | /* encrypt a block of text */ |
| 40 | |
Tianjia Zhang | c59de48 | 2021-07-20 11:46:40 +0800 | [diff] [blame] | 41 | static void sm4_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 42 | { |
Tianjia Zhang | c59de48 | 2021-07-20 11:46:40 +0800 | [diff] [blame] | 43 | const struct sm4_ctx *ctx = crypto_tfm_ctx(tfm); |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 44 | |
Tianjia Zhang | 2b31277 | 2021-07-20 11:46:39 +0800 | [diff] [blame] | 45 | sm4_crypt_block(ctx->rkey_enc, out, in); |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /* decrypt a block of text */ |
| 49 | |
Tianjia Zhang | c59de48 | 2021-07-20 11:46:40 +0800 | [diff] [blame] | 50 | static void sm4_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 51 | { |
Tianjia Zhang | c59de48 | 2021-07-20 11:46:40 +0800 | [diff] [blame] | 52 | const struct sm4_ctx *ctx = crypto_tfm_ctx(tfm); |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 53 | |
Tianjia Zhang | 2b31277 | 2021-07-20 11:46:39 +0800 | [diff] [blame] | 54 | sm4_crypt_block(ctx->rkey_dec, out, in); |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static struct crypto_alg sm4_alg = { |
| 58 | .cra_name = "sm4", |
| 59 | .cra_driver_name = "sm4-generic", |
| 60 | .cra_priority = 100, |
| 61 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 62 | .cra_blocksize = SM4_BLOCK_SIZE, |
Tianjia Zhang | c59de48 | 2021-07-20 11:46:40 +0800 | [diff] [blame] | 63 | .cra_ctxsize = sizeof(struct sm4_ctx), |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 64 | .cra_module = THIS_MODULE, |
| 65 | .cra_u = { |
| 66 | .cipher = { |
| 67 | .cia_min_keysize = SM4_KEY_SIZE, |
| 68 | .cia_max_keysize = SM4_KEY_SIZE, |
Tianjia Zhang | c59de48 | 2021-07-20 11:46:40 +0800 | [diff] [blame] | 69 | .cia_setkey = sm4_setkey, |
| 70 | .cia_encrypt = sm4_encrypt, |
| 71 | .cia_decrypt = sm4_decrypt |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | static int __init sm4_init(void) |
| 77 | { |
| 78 | return crypto_register_alg(&sm4_alg); |
| 79 | } |
| 80 | |
| 81 | static void __exit sm4_fini(void) |
| 82 | { |
| 83 | crypto_unregister_alg(&sm4_alg); |
| 84 | } |
| 85 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 86 | subsys_initcall(sm4_init); |
Gilad Ben-Yossef | 747c8ce | 2018-03-06 09:44:42 +0000 | [diff] [blame] | 87 | module_exit(sm4_fini); |
| 88 | |
| 89 | MODULE_DESCRIPTION("SM4 Cipher Algorithm"); |
| 90 | MODULE_LICENSE("GPL v2"); |
| 91 | MODULE_ALIAS_CRYPTO("sm4"); |
| 92 | MODULE_ALIAS_CRYPTO("sm4-generic"); |