sm4-ce-gcm-glue.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #include <linux/module.h>
  10. #include <linux/crypto.h>
  11. #include <linux/kernel.h>
  12. #include <linux/cpufeature.h>
  13. #include <asm/neon.h>
  14. #include <crypto/b128ops.h>
  15. #include <crypto/scatterwalk.h>
  16. #include <crypto/internal/aead.h>
  17. #include <crypto/internal/skcipher.h>
  18. #include <crypto/sm4.h>
  19. #include "sm4-ce.h"
  20. asmlinkage void sm4_ce_pmull_ghash_setup(const u32 *rkey_enc, u8 *ghash_table);
  21. asmlinkage void pmull_ghash_update(const u8 *ghash_table, u8 *ghash,
  22. const u8 *src, unsigned int nblocks);
  23. asmlinkage void sm4_ce_pmull_gcm_enc(const u32 *rkey_enc, u8 *dst,
  24. const u8 *src, u8 *iv,
  25. unsigned int nbytes, u8 *ghash,
  26. const u8 *ghash_table, const u8 *lengths);
  27. asmlinkage void sm4_ce_pmull_gcm_dec(const u32 *rkey_enc, u8 *dst,
  28. const u8 *src, u8 *iv,
  29. unsigned int nbytes, u8 *ghash,
  30. const u8 *ghash_table, const u8 *lengths);
  31. #define GHASH_BLOCK_SIZE 16
  32. #define GCM_IV_SIZE 12
  33. struct sm4_gcm_ctx {
  34. struct sm4_ctx key;
  35. u8 ghash_table[16 * 4];
  36. };
  37. static int gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  38. unsigned int key_len)
  39. {
  40. struct sm4_gcm_ctx *ctx = crypto_aead_ctx(tfm);
  41. if (key_len != SM4_KEY_SIZE)
  42. return -EINVAL;
  43. kernel_neon_begin();
  44. sm4_ce_expand_key(key, ctx->key.rkey_enc, ctx->key.rkey_dec,
  45. crypto_sm4_fk, crypto_sm4_ck);
  46. sm4_ce_pmull_ghash_setup(ctx->key.rkey_enc, ctx->ghash_table);
  47. kernel_neon_end();
  48. return 0;
  49. }
  50. static int gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  51. {
  52. switch (authsize) {
  53. case 4:
  54. case 8:
  55. case 12 ... 16:
  56. return 0;
  57. default:
  58. return -EINVAL;
  59. }
  60. }
  61. static void gcm_calculate_auth_mac(struct aead_request *req, u8 ghash[])
  62. {
  63. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  64. struct sm4_gcm_ctx *ctx = crypto_aead_ctx(aead);
  65. u8 __aligned(8) buffer[GHASH_BLOCK_SIZE];
  66. u32 assoclen = req->assoclen;
  67. struct scatter_walk walk;
  68. unsigned int buflen = 0;
  69. scatterwalk_start(&walk, req->src);
  70. do {
  71. u32 n = scatterwalk_clamp(&walk, assoclen);
  72. u8 *p, *ptr;
  73. if (!n) {
  74. scatterwalk_start(&walk, sg_next(walk.sg));
  75. n = scatterwalk_clamp(&walk, assoclen);
  76. }
  77. p = ptr = scatterwalk_map(&walk);
  78. assoclen -= n;
  79. scatterwalk_advance(&walk, n);
  80. if (n + buflen < GHASH_BLOCK_SIZE) {
  81. memcpy(&buffer[buflen], ptr, n);
  82. buflen += n;
  83. } else {
  84. unsigned int nblocks;
  85. if (buflen) {
  86. unsigned int l = GHASH_BLOCK_SIZE - buflen;
  87. memcpy(&buffer[buflen], ptr, l);
  88. ptr += l;
  89. n -= l;
  90. pmull_ghash_update(ctx->ghash_table, ghash,
  91. buffer, 1);
  92. }
  93. nblocks = n / GHASH_BLOCK_SIZE;
  94. if (nblocks) {
  95. pmull_ghash_update(ctx->ghash_table, ghash,
  96. ptr, nblocks);
  97. ptr += nblocks * GHASH_BLOCK_SIZE;
  98. }
  99. buflen = n % GHASH_BLOCK_SIZE;
  100. if (buflen)
  101. memcpy(&buffer[0], ptr, buflen);
  102. }
  103. scatterwalk_unmap(p);
  104. scatterwalk_done(&walk, 0, assoclen);
  105. } while (assoclen);
  106. /* padding with '0' */
  107. if (buflen) {
  108. memset(&buffer[buflen], 0, GHASH_BLOCK_SIZE - buflen);
  109. pmull_ghash_update(ctx->ghash_table, ghash, buffer, 1);
  110. }
  111. }
  112. static int gcm_crypt(struct aead_request *req, struct skcipher_walk *walk,
  113. u8 ghash[], int err,
  114. void (*sm4_ce_pmull_gcm_crypt)(const u32 *rkey_enc,
  115. u8 *dst, const u8 *src, u8 *iv,
  116. unsigned int nbytes, u8 *ghash,
  117. const u8 *ghash_table, const u8 *lengths))
  118. {
  119. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  120. struct sm4_gcm_ctx *ctx = crypto_aead_ctx(aead);
  121. u8 __aligned(8) iv[SM4_BLOCK_SIZE];
  122. be128 __aligned(8) lengths;
  123. memset(ghash, 0, SM4_BLOCK_SIZE);
  124. lengths.a = cpu_to_be64(req->assoclen * 8);
  125. lengths.b = cpu_to_be64(walk->total * 8);
  126. memcpy(iv, req->iv, GCM_IV_SIZE);
  127. put_unaligned_be32(2, iv + GCM_IV_SIZE);
  128. kernel_neon_begin();
  129. if (req->assoclen)
  130. gcm_calculate_auth_mac(req, ghash);
  131. while (walk->nbytes) {
  132. unsigned int tail = walk->nbytes % SM4_BLOCK_SIZE;
  133. const u8 *src = walk->src.virt.addr;
  134. u8 *dst = walk->dst.virt.addr;
  135. if (walk->nbytes == walk->total) {
  136. sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, dst, src, iv,
  137. walk->nbytes, ghash,
  138. ctx->ghash_table,
  139. (const u8 *)&lengths);
  140. kernel_neon_end();
  141. return skcipher_walk_done(walk, 0);
  142. }
  143. sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, dst, src, iv,
  144. walk->nbytes - tail, ghash,
  145. ctx->ghash_table, NULL);
  146. kernel_neon_end();
  147. err = skcipher_walk_done(walk, tail);
  148. kernel_neon_begin();
  149. }
  150. sm4_ce_pmull_gcm_crypt(ctx->key.rkey_enc, NULL, NULL, iv,
  151. walk->nbytes, ghash, ctx->ghash_table,
  152. (const u8 *)&lengths);
  153. kernel_neon_end();
  154. return err;
  155. }
  156. static int gcm_encrypt(struct aead_request *req)
  157. {
  158. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  159. u8 __aligned(8) ghash[SM4_BLOCK_SIZE];
  160. struct skcipher_walk walk;
  161. int err;
  162. err = skcipher_walk_aead_encrypt(&walk, req, false);
  163. err = gcm_crypt(req, &walk, ghash, err, sm4_ce_pmull_gcm_enc);
  164. if (err)
  165. return err;
  166. /* copy authtag to end of dst */
  167. scatterwalk_map_and_copy(ghash, req->dst, req->assoclen + req->cryptlen,
  168. crypto_aead_authsize(aead), 1);
  169. return 0;
  170. }
  171. static int gcm_decrypt(struct aead_request *req)
  172. {
  173. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  174. unsigned int authsize = crypto_aead_authsize(aead);
  175. u8 __aligned(8) ghash[SM4_BLOCK_SIZE];
  176. u8 authtag[SM4_BLOCK_SIZE];
  177. struct skcipher_walk walk;
  178. int err;
  179. err = skcipher_walk_aead_decrypt(&walk, req, false);
  180. err = gcm_crypt(req, &walk, ghash, err, sm4_ce_pmull_gcm_dec);
  181. if (err)
  182. return err;
  183. /* compare calculated auth tag with the stored one */
  184. scatterwalk_map_and_copy(authtag, req->src,
  185. req->assoclen + req->cryptlen - authsize,
  186. authsize, 0);
  187. if (crypto_memneq(authtag, ghash, authsize))
  188. return -EBADMSG;
  189. return 0;
  190. }
  191. static struct aead_alg sm4_gcm_alg = {
  192. .base = {
  193. .cra_name = "gcm(sm4)",
  194. .cra_driver_name = "gcm-sm4-ce",
  195. .cra_priority = 400,
  196. .cra_blocksize = 1,
  197. .cra_ctxsize = sizeof(struct sm4_gcm_ctx),
  198. .cra_module = THIS_MODULE,
  199. },
  200. .ivsize = GCM_IV_SIZE,
  201. .chunksize = SM4_BLOCK_SIZE,
  202. .maxauthsize = SM4_BLOCK_SIZE,
  203. .setkey = gcm_setkey,
  204. .setauthsize = gcm_setauthsize,
  205. .encrypt = gcm_encrypt,
  206. .decrypt = gcm_decrypt,
  207. };
  208. static int __init sm4_ce_gcm_init(void)
  209. {
  210. if (!cpu_have_named_feature(PMULL))
  211. return -ENODEV;
  212. return crypto_register_aead(&sm4_gcm_alg);
  213. }
  214. static void __exit sm4_ce_gcm_exit(void)
  215. {
  216. crypto_unregister_aead(&sm4_gcm_alg);
  217. }
  218. static const struct cpu_feature __maybe_unused sm4_ce_gcm_cpu_feature[] = {
  219. { cpu_feature(PMULL) },
  220. {}
  221. };
  222. MODULE_DEVICE_TABLE(cpu, sm4_ce_gcm_cpu_feature);
  223. module_cpu_feature_match(SM4, sm4_ce_gcm_init);
  224. module_exit(sm4_ce_gcm_exit);
  225. MODULE_DESCRIPTION("Synchronous SM4 in GCM mode using ARMv8 Crypto Extensions");
  226. MODULE_ALIAS_CRYPTO("gcm(sm4)");
  227. MODULE_AUTHOR("Tianjia Zhang <tianjia.zhang@linux.alibaba.com>");
  228. MODULE_LICENSE("GPL v2");