blob: d19d01f852a9a485ed2c54f44dfac8f89749640c [file] [log] [blame]
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +00001// 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-Yossef747c8ce2018-03-06 09:44:42 +000019/**
Eric Biggers674f3682019-12-30 21:19:36 -060020 * crypto_sm4_set_key - Set the SM4 key.
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000021 * @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 Zhang2b312772021-07-20 11:46:39 +080025 * This function uses sm4_expandkey() to expand the key.
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000026 * &crypto_sm4_ctx _must_ be the private data embedded in @tfm which is
27 * retrieved with crypto_tfm_ctx().
Eric Biggers674f3682019-12-30 21:19:36 -060028 *
29 * Return: 0 on success; -EINVAL on failure (only happens for bad key lengths)
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000030 */
31int crypto_sm4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
32 unsigned int key_len)
33{
34 struct crypto_sm4_ctx *ctx = crypto_tfm_ctx(tfm);
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000035
Tianjia Zhang2b312772021-07-20 11:46:39 +080036 return sm4_expandkey(ctx, in_key, key_len);
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000037}
38EXPORT_SYMBOL_GPL(crypto_sm4_set_key);
39
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000040/* encrypt a block of text */
41
Ard Biesheuvel8da02bf2018-04-25 14:20:45 +020042void crypto_sm4_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000043{
44 const struct crypto_sm4_ctx *ctx = crypto_tfm_ctx(tfm);
45
Tianjia Zhang2b312772021-07-20 11:46:39 +080046 sm4_crypt_block(ctx->rkey_enc, out, in);
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000047}
Ard Biesheuvel8da02bf2018-04-25 14:20:45 +020048EXPORT_SYMBOL_GPL(crypto_sm4_encrypt);
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000049
50/* decrypt a block of text */
51
Ard Biesheuvel8da02bf2018-04-25 14:20:45 +020052void crypto_sm4_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000053{
54 const struct crypto_sm4_ctx *ctx = crypto_tfm_ctx(tfm);
55
Tianjia Zhang2b312772021-07-20 11:46:39 +080056 sm4_crypt_block(ctx->rkey_dec, out, in);
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000057}
Ard Biesheuvel8da02bf2018-04-25 14:20:45 +020058EXPORT_SYMBOL_GPL(crypto_sm4_decrypt);
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000059
60static struct crypto_alg sm4_alg = {
61 .cra_name = "sm4",
62 .cra_driver_name = "sm4-generic",
63 .cra_priority = 100,
64 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
65 .cra_blocksize = SM4_BLOCK_SIZE,
66 .cra_ctxsize = sizeof(struct crypto_sm4_ctx),
67 .cra_module = THIS_MODULE,
68 .cra_u = {
69 .cipher = {
70 .cia_min_keysize = SM4_KEY_SIZE,
71 .cia_max_keysize = SM4_KEY_SIZE,
72 .cia_setkey = crypto_sm4_set_key,
Ard Biesheuvel8da02bf2018-04-25 14:20:45 +020073 .cia_encrypt = crypto_sm4_encrypt,
74 .cia_decrypt = crypto_sm4_decrypt
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000075 }
76 }
77};
78
79static int __init sm4_init(void)
80{
81 return crypto_register_alg(&sm4_alg);
82}
83
84static void __exit sm4_fini(void)
85{
86 crypto_unregister_alg(&sm4_alg);
87}
88
Eric Biggersc4741b22019-04-11 21:57:42 -070089subsys_initcall(sm4_init);
Gilad Ben-Yossef747c8ce2018-03-06 09:44:42 +000090module_exit(sm4_fini);
91
92MODULE_DESCRIPTION("SM4 Cipher Algorithm");
93MODULE_LICENSE("GPL v2");
94MODULE_ALIAS_CRYPTO("sm4");
95MODULE_ALIAS_CRYPTO("sm4-generic");