1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * SM4-GCM AEAD Algorithm using ARMv8 Crypto Extensions 4 * as specified in rfc8998 5 * https://datatracker.ietf.org/doc/html/rfc8998 6 * 7 * Copyright (C) 2022 Tianjia Zhang <tianjia.zhang@linux.alibaba.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/crypto.h> 12 #include <linux/kernel.h> 13 #include <linux/cpufeature.h> 14 #include <asm/neon.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> 20 #include "sm4-ce.h" 21 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); 33 34 #define GHASH_BLOCK_SIZE 16 35 #define GCM_IV_SIZE 12 36 37 struct sm4_gcm_ctx { 38 struct sm4_ctx key; 39 u8 ghash_table[16 * 4]; 40 }; 41 42 43 static int gcm_setkey(struct crypto_aead *tfm, const u8 *key, 44 unsigned int key_len) 45 { 46 struct sm4_gcm_ctx *ctx = crypto_aead_ctx(tfm); 47 48 if (key_len != SM4_KEY_SIZE) 49 return -EINVAL; 50 51 kernel_neon_begin(); 52 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); 56 57 kernel_neon_end(); 58 return 0; 59 } 60 61 static int gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize) 62 { 63 switch (authsize) { 64 case 4: 65 case 8: 66 case 12 ... 16: 67 return 0; 68 default: 69 return -EINVAL; 70 } 71 } 72 73 static void gcm_calculate_auth_mac(struct aead_request *req, u8 ghash[]) 74 { 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; 81 82 scatterwalk_start(&walk, req->src); 83 84 do { 85 u32 n = scatterwalk_clamp(&walk, assoclen); 86 u8 *p, *ptr; 87 88 if (!n) { 89 scatterwalk_start(&walk, sg_next(walk.sg)); 90 n = scatterwalk_clamp(&walk, assoclen); 91 } 92 93 p = ptr = scatterwalk_map(&walk); 94 assoclen -= n; 95 scatterwalk_advance(&walk, n); 96 97 if (n + buflen < GHASH_BLOCK_SIZE) { 98 memcpy(&buffer[buflen], ptr, n); 99 buflen += n; 100 } else { 101 unsigned int nblocks; 102 103 if (buflen) { 104 unsigned int l = GHASH_BLOCK_SIZE - buflen; 105 106 memcpy(&buffer[buflen], ptr, l); 107 ptr += l; 108 n -= l; 109 110 pmull_ghash_update(ctx->ghash_table, ghash, 111 buffer, 1); 112 } 113 114 nblocks = n / GHASH_BLOCK_SIZE; 115 if (nblocks) { 116 pmull_ghash_update(ctx->ghash_table, ghash, 117 ptr, nblocks); 118 ptr += nblocks * GHASH_BLOCK_SIZE; 119 } 120 121 buflen = n % GHASH_BLOCK_SIZE; 122 if (buflen) 123 memcpy(&buffer[0], ptr, buflen); 124 } 125 126 scatterwalk_unmap(p); 127 scatterwalk_done(&walk, 0, assoclen); 128 } while (assoclen); 129 130 /* padding with '0' */ 131 if (buflen) { 132 memset(&buffer[buflen], 0, GHASH_BLOCK_SIZE - buflen); 133 pmull_ghash_update(ctx->ghash_table, ghash, buffer, 1); 134 } 135 } 136 137 static int gcm_crypt(struct aead_request *req, struct skcipher_walk *walk, 138 struct sm4_gcm_ctx *ctx, u8 ghash[], 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)) 143 { 144 u8 __aligned(8) iv[SM4_BLOCK_SIZE]; 145 be128 __aligned(8) lengths; 146 int err; 147 148 memset(ghash, 0, SM4_BLOCK_SIZE); 149 150 lengths.a = cpu_to_be64(req->assoclen * 8); 151 lengths.b = cpu_to_be64(walk->total * 8); 152 153 memcpy(iv, walk->iv, GCM_IV_SIZE); 154 put_unaligned_be32(2, iv + GCM_IV_SIZE); 155 156 kernel_neon_begin(); 157 158 if (req->assoclen) 159 gcm_calculate_auth_mac(req, ghash); 160 161 do { 162 unsigned int tail = walk->nbytes % SM4_BLOCK_SIZE; 163 const u8 *src = walk->src.virt.addr; 164 u8 *dst = walk->dst.virt.addr; 165 166 if (walk->nbytes == walk->total) { 167 tail = 0; 168 169 sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, dst, src, iv, 170 walk->nbytes, ghash, 171 ctx->ghash_table, 172 (const u8 *)&lengths); 173 } else if (walk->nbytes - tail) { 174 sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, dst, src, iv, 175 walk->nbytes - tail, ghash, 176 ctx->ghash_table, NULL); 177 } 178 179 kernel_neon_end(); 180 181 err = skcipher_walk_done(walk, tail); 182 if (err) 183 return err; 184 if (walk->nbytes) 185 kernel_neon_begin(); 186 } while (walk->nbytes > 0); 187 188 return 0; 189 } 190 191 static int gcm_encrypt(struct aead_request *req) 192 { 193 struct crypto_aead *aead = crypto_aead_reqtfm(req); 194 struct sm4_gcm_ctx *ctx = crypto_aead_ctx(aead); 195 u8 __aligned(8) ghash[SM4_BLOCK_SIZE]; 196 struct skcipher_walk walk; 197 int err; 198 199 err = skcipher_walk_aead_encrypt(&walk, req, false); 200 if (err) 201 return err; 202 203 err = gcm_crypt(req, &walk, ctx, ghash, sm4_ce_pmull_gcm_enc); 204 if (err) 205 return err; 206 207 /* copy authtag to end of dst */ 208 scatterwalk_map_and_copy(ghash, req->dst, req->assoclen + req->cryptlen, 209 crypto_aead_authsize(aead), 1); 210 211 return 0; 212 } 213 214 static int gcm_decrypt(struct aead_request *req) 215 { 216 struct crypto_aead *aead = crypto_aead_reqtfm(req); 217 unsigned int authsize = crypto_aead_authsize(aead); 218 struct sm4_gcm_ctx *ctx = crypto_aead_ctx(aead); 219 u8 __aligned(8) ghash[SM4_BLOCK_SIZE]; 220 u8 authtag[SM4_BLOCK_SIZE]; 221 struct skcipher_walk walk; 222 int err; 223 224 err = skcipher_walk_aead_decrypt(&walk, req, false); 225 if (err) 226 return err; 227 228 err = gcm_crypt(req, &walk, ctx, ghash, sm4_ce_pmull_gcm_dec); 229 if (err) 230 return err; 231 232 /* compare calculated auth tag with the stored one */ 233 scatterwalk_map_and_copy(authtag, req->src, 234 req->assoclen + req->cryptlen - authsize, 235 authsize, 0); 236 237 if (crypto_memneq(authtag, ghash, authsize)) 238 return -EBADMSG; 239 240 return 0; 241 } 242 243 static struct aead_alg sm4_gcm_alg = { 244 .base = { 245 .cra_name = "gcm(sm4)", 246 .cra_driver_name = "gcm-sm4-ce", 247 .cra_priority = 400, 248 .cra_blocksize = 1, 249 .cra_ctxsize = sizeof(struct sm4_gcm_ctx), 250 .cra_module = THIS_MODULE, 251 }, 252 .ivsize = GCM_IV_SIZE, 253 .chunksize = SM4_BLOCK_SIZE, 254 .maxauthsize = SM4_BLOCK_SIZE, 255 .setkey = gcm_setkey, 256 .setauthsize = gcm_setauthsize, 257 .encrypt = gcm_encrypt, 258 .decrypt = gcm_decrypt, 259 }; 260 261 static int __init sm4_ce_gcm_init(void) 262 { 263 if (!cpu_have_named_feature(PMULL)) 264 return -ENODEV; 265 266 return crypto_register_aead(&sm4_gcm_alg); 267 } 268 269 static void __exit sm4_ce_gcm_exit(void) 270 { 271 crypto_unregister_aead(&sm4_gcm_alg); 272 } 273 274 static const struct cpu_feature __maybe_unused sm4_ce_gcm_cpu_feature[] = { 275 { cpu_feature(PMULL) }, 276 {} 277 }; 278 MODULE_DEVICE_TABLE(cpu, sm4_ce_gcm_cpu_feature); 279 280 module_cpu_feature_match(SM4, sm4_ce_gcm_init); 281 module_exit(sm4_ce_gcm_exit); 282 283 MODULE_DESCRIPTION("Synchronous SM4 in GCM mode using ARMv8 Crypto Extensions"); 284 MODULE_ALIAS_CRYPTO("gcm(sm4)"); 285 MODULE_AUTHOR("Tianjia Zhang <tianjia.zhang@linux.alibaba.com>"); 286 MODULE_LICENSE("GPL v2"); 287