1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * SM4-GCM AEAD Algorithm using ARMv8 Crypto Extensions
4 * as specified in rfc8998
5 * https://datatracker.ietf.org/doc/html/rfc8998
7 * Copyright (C) 2022 Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
10 #include <linux/module.h>
11 #include <linux/crypto.h>
12 #include <linux/kernel.h>
13 #include <linux/cpufeature.h>
15 #include <crypto/b128ops.h>
16 #include <crypto/scatterwalk.h>
17 #include <crypto/internal/aead.h>
18 #include <crypto/internal/skcipher.h>
19 #include <crypto/sm4.h>
22 asmlinkage void sm4_ce_pmull_ghash_setup(const u32 *rkey_enc, u8 *ghash_table);
23 asmlinkage void pmull_ghash_update(const u8 *ghash_table, u8 *ghash,
24 const u8 *src, unsigned int nblocks);
25 asmlinkage void sm4_ce_pmull_gcm_enc(const u32 *rkey_enc, u8 *dst,
26 const u8 *src, u8 *iv,
27 unsigned int nbytes, u8 *ghash,
28 const u8 *ghash_table, const u8 *lengths);
29 asmlinkage void sm4_ce_pmull_gcm_dec(const u32 *rkey_enc, u8 *dst,
30 const u8 *src, u8 *iv,
31 unsigned int nbytes, u8 *ghash,
32 const u8 *ghash_table, const u8 *lengths);
34 #define GHASH_BLOCK_SIZE 16
35 #define GCM_IV_SIZE 12
39 u8 ghash_table[16 * 4];
43 static int gcm_setkey(struct crypto_aead *tfm, const u8 *key,
46 struct sm4_gcm_ctx *ctx = crypto_aead_ctx(tfm);
48 if (key_len != SM4_KEY_SIZE)
53 sm4_ce_expand_key(key, ctx->key.rkey_enc, ctx->key.rkey_dec,
54 crypto_sm4_fk, crypto_sm4_ck);
55 sm4_ce_pmull_ghash_setup(ctx->key.rkey_enc, ctx->ghash_table);
61 static int gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
73 static void gcm_calculate_auth_mac(struct aead_request *req, u8 ghash[])
75 struct crypto_aead *aead = crypto_aead_reqtfm(req);
76 struct sm4_gcm_ctx *ctx = crypto_aead_ctx(aead);
77 u8 __aligned(8) buffer[GHASH_BLOCK_SIZE];
78 u32 assoclen = req->assoclen;
79 struct scatter_walk walk;
80 unsigned int buflen = 0;
82 scatterwalk_start(&walk, req->src);
85 u32 n = scatterwalk_clamp(&walk, assoclen);
89 scatterwalk_start(&walk, sg_next(walk.sg));
90 n = scatterwalk_clamp(&walk, assoclen);
93 p = ptr = scatterwalk_map(&walk);
95 scatterwalk_advance(&walk, n);
97 if (n + buflen < GHASH_BLOCK_SIZE) {
98 memcpy(&buffer[buflen], ptr, n);
101 unsigned int nblocks;
104 unsigned int l = GHASH_BLOCK_SIZE - buflen;
106 memcpy(&buffer[buflen], ptr, l);
110 pmull_ghash_update(ctx->ghash_table, ghash,
114 nblocks = n / GHASH_BLOCK_SIZE;
116 pmull_ghash_update(ctx->ghash_table, ghash,
118 ptr += nblocks * GHASH_BLOCK_SIZE;
121 buflen = n % GHASH_BLOCK_SIZE;
123 memcpy(&buffer[0], ptr, buflen);
126 scatterwalk_unmap(p);
127 scatterwalk_done(&walk, 0, assoclen);
130 /* padding with '0' */
132 memset(&buffer[buflen], 0, GHASH_BLOCK_SIZE - buflen);
133 pmull_ghash_update(ctx->ghash_table, ghash, buffer, 1);
137 static int gcm_crypt(struct aead_request *req, struct skcipher_walk *walk,
139 void (*sm4_ce_pmull_gcm_crypt)(const u32 *rkey_enc,
140 u8 *dst, const u8 *src, u8 *iv,
141 unsigned int nbytes, u8 *ghash,
142 const u8 *ghash_table, const u8 *lengths))
144 struct crypto_aead *aead = crypto_aead_reqtfm(req);
145 struct sm4_gcm_ctx *ctx = crypto_aead_ctx(aead);
146 u8 __aligned(8) iv[SM4_BLOCK_SIZE];
147 be128 __aligned(8) lengths;
149 memset(ghash, 0, SM4_BLOCK_SIZE);
151 lengths.a = cpu_to_be64(req->assoclen * 8);
152 lengths.b = cpu_to_be64(walk->total * 8);
154 memcpy(iv, req->iv, GCM_IV_SIZE);
155 put_unaligned_be32(2, iv + GCM_IV_SIZE);
160 gcm_calculate_auth_mac(req, ghash);
162 while (walk->nbytes) {
163 unsigned int tail = walk->nbytes % SM4_BLOCK_SIZE;
164 const u8 *src = walk->src.virt.addr;
165 u8 *dst = walk->dst.virt.addr;
167 if (walk->nbytes == walk->total) {
168 sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, dst, src, iv,
171 (const u8 *)&lengths);
175 return skcipher_walk_done(walk, 0);
178 sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, dst, src, iv,
179 walk->nbytes - tail, ghash,
180 ctx->ghash_table, NULL);
184 err = skcipher_walk_done(walk, tail);
189 sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, NULL, NULL, iv,
190 walk->nbytes, ghash, ctx->ghash_table,
191 (const u8 *)&lengths);
198 static int gcm_encrypt(struct aead_request *req)
200 struct crypto_aead *aead = crypto_aead_reqtfm(req);
201 u8 __aligned(8) ghash[SM4_BLOCK_SIZE];
202 struct skcipher_walk walk;
205 err = skcipher_walk_aead_encrypt(&walk, req, false);
206 err = gcm_crypt(req, &walk, ghash, err, sm4_ce_pmull_gcm_enc);
210 /* copy authtag to end of dst */
211 scatterwalk_map_and_copy(ghash, req->dst, req->assoclen + req->cryptlen,
212 crypto_aead_authsize(aead), 1);
217 static int gcm_decrypt(struct aead_request *req)
219 struct crypto_aead *aead = crypto_aead_reqtfm(req);
220 unsigned int authsize = crypto_aead_authsize(aead);
221 u8 __aligned(8) ghash[SM4_BLOCK_SIZE];
222 u8 authtag[SM4_BLOCK_SIZE];
223 struct skcipher_walk walk;
226 err = skcipher_walk_aead_decrypt(&walk, req, false);
227 err = gcm_crypt(req, &walk, ghash, err, sm4_ce_pmull_gcm_dec);
231 /* compare calculated auth tag with the stored one */
232 scatterwalk_map_and_copy(authtag, req->src,
233 req->assoclen + req->cryptlen - authsize,
236 if (crypto_memneq(authtag, ghash, authsize))
242 static struct aead_alg sm4_gcm_alg = {
244 .cra_name = "gcm(sm4)",
245 .cra_driver_name = "gcm-sm4-ce",
248 .cra_ctxsize = sizeof(struct sm4_gcm_ctx),
249 .cra_module = THIS_MODULE,
251 .ivsize = GCM_IV_SIZE,
252 .chunksize = SM4_BLOCK_SIZE,
253 .maxauthsize = SM4_BLOCK_SIZE,
254 .setkey = gcm_setkey,
255 .setauthsize = gcm_setauthsize,
256 .encrypt = gcm_encrypt,
257 .decrypt = gcm_decrypt,
260 static int __init sm4_ce_gcm_init(void)
262 if (!cpu_have_named_feature(PMULL))
265 return crypto_register_aead(&sm4_gcm_alg);
268 static void __exit sm4_ce_gcm_exit(void)
270 crypto_unregister_aead(&sm4_gcm_alg);
273 static const struct cpu_feature __maybe_unused sm4_ce_gcm_cpu_feature[] = {
274 { cpu_feature(PMULL) },
277 MODULE_DEVICE_TABLE(cpu, sm4_ce_gcm_cpu_feature);
279 module_cpu_feature_match(SM4, sm4_ce_gcm_init);
280 module_exit(sm4_ce_gcm_exit);
282 MODULE_DESCRIPTION("Synchronous SM4 in GCM mode using ARMv8 Crypto Extensions");
283 MODULE_ALIAS_CRYPTO("gcm(sm4)");
284 MODULE_AUTHOR("Tianjia Zhang <tianjia.zhang@linux.alibaba.com>");
285 MODULE_LICENSE("GPL v2");