ccp-crypto-aes-galois.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Cryptographic Coprocessor (CCP) AES GCM crypto API support
  4. *
  5. * Copyright (C) 2016,2017 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Gary R Hook <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/delay.h>
  12. #include <linux/scatterlist.h>
  13. #include <linux/crypto.h>
  14. #include <crypto/internal/aead.h>
  15. #include <crypto/algapi.h>
  16. #include <crypto/aes.h>
  17. #include <crypto/ctr.h>
  18. #include <crypto/gcm.h>
  19. #include <crypto/scatterwalk.h>
  20. #include "ccp-crypto.h"
  21. static int ccp_aes_gcm_complete(struct crypto_async_request *async_req, int ret)
  22. {
  23. return ret;
  24. }
  25. static int ccp_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  26. unsigned int key_len)
  27. {
  28. struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
  29. switch (key_len) {
  30. case AES_KEYSIZE_128:
  31. ctx->u.aes.type = CCP_AES_TYPE_128;
  32. break;
  33. case AES_KEYSIZE_192:
  34. ctx->u.aes.type = CCP_AES_TYPE_192;
  35. break;
  36. case AES_KEYSIZE_256:
  37. ctx->u.aes.type = CCP_AES_TYPE_256;
  38. break;
  39. default:
  40. return -EINVAL;
  41. }
  42. ctx->u.aes.mode = CCP_AES_MODE_GCM;
  43. ctx->u.aes.key_len = key_len;
  44. memcpy(ctx->u.aes.key, key, key_len);
  45. sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
  46. return 0;
  47. }
  48. static int ccp_aes_gcm_setauthsize(struct crypto_aead *tfm,
  49. unsigned int authsize)
  50. {
  51. switch (authsize) {
  52. case 16:
  53. case 15:
  54. case 14:
  55. case 13:
  56. case 12:
  57. case 8:
  58. case 4:
  59. break;
  60. default:
  61. return -EINVAL;
  62. }
  63. return 0;
  64. }
  65. static int ccp_aes_gcm_crypt(struct aead_request *req, bool encrypt)
  66. {
  67. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  68. struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
  69. struct ccp_aes_req_ctx *rctx = aead_request_ctx(req);
  70. struct scatterlist *iv_sg = NULL;
  71. unsigned int iv_len = 0;
  72. int i;
  73. int ret = 0;
  74. if (!ctx->u.aes.key_len)
  75. return -EINVAL;
  76. if (ctx->u.aes.mode != CCP_AES_MODE_GCM)
  77. return -EINVAL;
  78. if (!req->iv)
  79. return -EINVAL;
  80. /*
  81. * 5 parts:
  82. * plaintext/ciphertext input
  83. * AAD
  84. * key
  85. * IV
  86. * Destination+tag buffer
  87. */
  88. /* Prepare the IV: 12 bytes + an integer (counter) */
  89. memcpy(rctx->iv, req->iv, GCM_AES_IV_SIZE);
  90. for (i = 0; i < 3; i++)
  91. rctx->iv[i + GCM_AES_IV_SIZE] = 0;
  92. rctx->iv[AES_BLOCK_SIZE - 1] = 1;
  93. /* Set up a scatterlist for the IV */
  94. iv_sg = &rctx->iv_sg;
  95. iv_len = AES_BLOCK_SIZE;
  96. sg_init_one(iv_sg, rctx->iv, iv_len);
  97. /* The AAD + plaintext are concatenated in the src buffer */
  98. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  99. INIT_LIST_HEAD(&rctx->cmd.entry);
  100. rctx->cmd.engine = CCP_ENGINE_AES;
  101. rctx->cmd.u.aes.authsize = crypto_aead_authsize(tfm);
  102. rctx->cmd.u.aes.type = ctx->u.aes.type;
  103. rctx->cmd.u.aes.mode = ctx->u.aes.mode;
  104. rctx->cmd.u.aes.action = encrypt;
  105. rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
  106. rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
  107. rctx->cmd.u.aes.iv = iv_sg;
  108. rctx->cmd.u.aes.iv_len = iv_len;
  109. rctx->cmd.u.aes.src = req->src;
  110. rctx->cmd.u.aes.src_len = req->cryptlen;
  111. rctx->cmd.u.aes.aad_len = req->assoclen;
  112. /* The cipher text + the tag are in the dst buffer */
  113. rctx->cmd.u.aes.dst = req->dst;
  114. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  115. return ret;
  116. }
  117. static int ccp_aes_gcm_encrypt(struct aead_request *req)
  118. {
  119. return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_ENCRYPT);
  120. }
  121. static int ccp_aes_gcm_decrypt(struct aead_request *req)
  122. {
  123. return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_DECRYPT);
  124. }
  125. static int ccp_aes_gcm_cra_init(struct crypto_aead *tfm)
  126. {
  127. struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
  128. ctx->complete = ccp_aes_gcm_complete;
  129. ctx->u.aes.key_len = 0;
  130. crypto_aead_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx));
  131. return 0;
  132. }
  133. static void ccp_aes_gcm_cra_exit(struct crypto_tfm *tfm)
  134. {
  135. }
  136. static struct aead_alg ccp_aes_gcm_defaults = {
  137. .setkey = ccp_aes_gcm_setkey,
  138. .setauthsize = ccp_aes_gcm_setauthsize,
  139. .encrypt = ccp_aes_gcm_encrypt,
  140. .decrypt = ccp_aes_gcm_decrypt,
  141. .init = ccp_aes_gcm_cra_init,
  142. .ivsize = GCM_AES_IV_SIZE,
  143. .maxauthsize = AES_BLOCK_SIZE,
  144. .base = {
  145. .cra_flags = CRYPTO_ALG_ASYNC |
  146. CRYPTO_ALG_ALLOCATES_MEMORY |
  147. CRYPTO_ALG_KERN_DRIVER_ONLY |
  148. CRYPTO_ALG_NEED_FALLBACK,
  149. .cra_blocksize = AES_BLOCK_SIZE,
  150. .cra_ctxsize = sizeof(struct ccp_ctx),
  151. .cra_priority = CCP_CRA_PRIORITY,
  152. .cra_exit = ccp_aes_gcm_cra_exit,
  153. .cra_module = THIS_MODULE,
  154. },
  155. };
  156. struct ccp_aes_aead_def {
  157. enum ccp_aes_mode mode;
  158. unsigned int version;
  159. const char *name;
  160. const char *driver_name;
  161. unsigned int blocksize;
  162. unsigned int ivsize;
  163. struct aead_alg *alg_defaults;
  164. };
  165. static struct ccp_aes_aead_def aes_aead_algs[] = {
  166. {
  167. .mode = CCP_AES_MODE_GHASH,
  168. .version = CCP_VERSION(5, 0),
  169. .name = "gcm(aes)",
  170. .driver_name = "gcm-aes-ccp",
  171. .blocksize = 1,
  172. .ivsize = AES_BLOCK_SIZE,
  173. .alg_defaults = &ccp_aes_gcm_defaults,
  174. },
  175. };
  176. static int ccp_register_aes_aead(struct list_head *head,
  177. const struct ccp_aes_aead_def *def)
  178. {
  179. struct ccp_crypto_aead *ccp_aead;
  180. struct aead_alg *alg;
  181. int ret;
  182. ccp_aead = kzalloc(sizeof(*ccp_aead), GFP_KERNEL);
  183. if (!ccp_aead)
  184. return -ENOMEM;
  185. INIT_LIST_HEAD(&ccp_aead->entry);
  186. ccp_aead->mode = def->mode;
  187. /* Copy the defaults and override as necessary */
  188. alg = &ccp_aead->alg;
  189. *alg = *def->alg_defaults;
  190. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  191. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  192. def->driver_name);
  193. alg->base.cra_blocksize = def->blocksize;
  194. ret = crypto_register_aead(alg);
  195. if (ret) {
  196. pr_err("%s aead algorithm registration error (%d)\n",
  197. alg->base.cra_name, ret);
  198. kfree(ccp_aead);
  199. return ret;
  200. }
  201. list_add(&ccp_aead->entry, head);
  202. return 0;
  203. }
  204. int ccp_register_aes_aeads(struct list_head *head)
  205. {
  206. int i, ret;
  207. unsigned int ccpversion = ccp_version();
  208. for (i = 0; i < ARRAY_SIZE(aes_aead_algs); i++) {
  209. if (aes_aead_algs[i].version > ccpversion)
  210. continue;
  211. ret = ccp_register_aes_aead(head, &aes_aead_algs[i]);
  212. if (ret)
  213. return ret;
  214. }
  215. return 0;
  216. }