ccp-crypto-aes-galois.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <gary.hook@amd.com>
  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. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  41. return -EINVAL;
  42. }
  43. ctx->u.aes.mode = CCP_AES_MODE_GCM;
  44. ctx->u.aes.key_len = key_len;
  45. memcpy(ctx->u.aes.key, key, key_len);
  46. sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
  47. return 0;
  48. }
  49. static int ccp_aes_gcm_setauthsize(struct crypto_aead *tfm,
  50. unsigned int authsize)
  51. {
  52. switch (authsize) {
  53. case 16:
  54. case 15:
  55. case 14:
  56. case 13:
  57. case 12:
  58. case 8:
  59. case 4:
  60. break;
  61. default:
  62. return -EINVAL;
  63. }
  64. return 0;
  65. }
  66. static int ccp_aes_gcm_crypt(struct aead_request *req, bool encrypt)
  67. {
  68. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  69. struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
  70. struct ccp_aes_req_ctx *rctx = aead_request_ctx(req);
  71. struct scatterlist *iv_sg = NULL;
  72. unsigned int iv_len = 0;
  73. int i;
  74. int ret = 0;
  75. if (!ctx->u.aes.key_len)
  76. return -EINVAL;
  77. if (ctx->u.aes.mode != CCP_AES_MODE_GCM)
  78. return -EINVAL;
  79. if (!req->iv)
  80. return -EINVAL;
  81. /*
  82. * 5 parts:
  83. * plaintext/ciphertext input
  84. * AAD
  85. * key
  86. * IV
  87. * Destination+tag buffer
  88. */
  89. /* Prepare the IV: 12 bytes + an integer (counter) */
  90. memcpy(rctx->iv, req->iv, GCM_AES_IV_SIZE);
  91. for (i = 0; i < 3; i++)
  92. rctx->iv[i + GCM_AES_IV_SIZE] = 0;
  93. rctx->iv[AES_BLOCK_SIZE - 1] = 1;
  94. /* Set up a scatterlist for the IV */
  95. iv_sg = &rctx->iv_sg;
  96. iv_len = AES_BLOCK_SIZE;
  97. sg_init_one(iv_sg, rctx->iv, iv_len);
  98. /* The AAD + plaintext are concatenated in the src buffer */
  99. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  100. INIT_LIST_HEAD(&rctx->cmd.entry);
  101. rctx->cmd.engine = CCP_ENGINE_AES;
  102. rctx->cmd.u.aes.authsize = crypto_aead_authsize(tfm);
  103. rctx->cmd.u.aes.type = ctx->u.aes.type;
  104. rctx->cmd.u.aes.mode = ctx->u.aes.mode;
  105. rctx->cmd.u.aes.action = encrypt;
  106. rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
  107. rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
  108. rctx->cmd.u.aes.iv = iv_sg;
  109. rctx->cmd.u.aes.iv_len = iv_len;
  110. rctx->cmd.u.aes.src = req->src;
  111. rctx->cmd.u.aes.src_len = req->cryptlen;
  112. rctx->cmd.u.aes.aad_len = req->assoclen;
  113. /* The cipher text + the tag are in the dst buffer */
  114. rctx->cmd.u.aes.dst = req->dst;
  115. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  116. return ret;
  117. }
  118. static int ccp_aes_gcm_encrypt(struct aead_request *req)
  119. {
  120. return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_ENCRYPT);
  121. }
  122. static int ccp_aes_gcm_decrypt(struct aead_request *req)
  123. {
  124. return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_DECRYPT);
  125. }
  126. static int ccp_aes_gcm_cra_init(struct crypto_aead *tfm)
  127. {
  128. struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
  129. ctx->complete = ccp_aes_gcm_complete;
  130. ctx->u.aes.key_len = 0;
  131. crypto_aead_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx));
  132. return 0;
  133. }
  134. static void ccp_aes_gcm_cra_exit(struct crypto_tfm *tfm)
  135. {
  136. }
  137. static struct aead_alg ccp_aes_gcm_defaults = {
  138. .setkey = ccp_aes_gcm_setkey,
  139. .setauthsize = ccp_aes_gcm_setauthsize,
  140. .encrypt = ccp_aes_gcm_encrypt,
  141. .decrypt = ccp_aes_gcm_decrypt,
  142. .init = ccp_aes_gcm_cra_init,
  143. .ivsize = GCM_AES_IV_SIZE,
  144. .maxauthsize = AES_BLOCK_SIZE,
  145. .base = {
  146. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  147. CRYPTO_ALG_ASYNC |
  148. CRYPTO_ALG_KERN_DRIVER_ONLY |
  149. CRYPTO_ALG_NEED_FALLBACK,
  150. .cra_blocksize = AES_BLOCK_SIZE,
  151. .cra_ctxsize = sizeof(struct ccp_ctx),
  152. .cra_priority = CCP_CRA_PRIORITY,
  153. .cra_type = &crypto_ablkcipher_type,
  154. .cra_exit = ccp_aes_gcm_cra_exit,
  155. .cra_module = THIS_MODULE,
  156. },
  157. };
  158. struct ccp_aes_aead_def {
  159. enum ccp_aes_mode mode;
  160. unsigned int version;
  161. const char *name;
  162. const char *driver_name;
  163. unsigned int blocksize;
  164. unsigned int ivsize;
  165. struct aead_alg *alg_defaults;
  166. };
  167. static struct ccp_aes_aead_def aes_aead_algs[] = {
  168. {
  169. .mode = CCP_AES_MODE_GHASH,
  170. .version = CCP_VERSION(5, 0),
  171. .name = "gcm(aes)",
  172. .driver_name = "gcm-aes-ccp",
  173. .blocksize = 1,
  174. .ivsize = AES_BLOCK_SIZE,
  175. .alg_defaults = &ccp_aes_gcm_defaults,
  176. },
  177. };
  178. static int ccp_register_aes_aead(struct list_head *head,
  179. const struct ccp_aes_aead_def *def)
  180. {
  181. struct ccp_crypto_aead *ccp_aead;
  182. struct aead_alg *alg;
  183. int ret;
  184. ccp_aead = kzalloc(sizeof(*ccp_aead), GFP_KERNEL);
  185. if (!ccp_aead)
  186. return -ENOMEM;
  187. INIT_LIST_HEAD(&ccp_aead->entry);
  188. ccp_aead->mode = def->mode;
  189. /* Copy the defaults and override as necessary */
  190. alg = &ccp_aead->alg;
  191. *alg = *def->alg_defaults;
  192. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  193. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  194. def->driver_name);
  195. alg->base.cra_blocksize = def->blocksize;
  196. alg->base.cra_ablkcipher.ivsize = def->ivsize;
  197. ret = crypto_register_aead(alg);
  198. if (ret) {
  199. pr_err("%s ablkcipher algorithm registration error (%d)\n",
  200. alg->base.cra_name, ret);
  201. kfree(ccp_aead);
  202. return ret;
  203. }
  204. list_add(&ccp_aead->entry, head);
  205. return 0;
  206. }
  207. int ccp_register_aes_aeads(struct list_head *head)
  208. {
  209. int i, ret;
  210. unsigned int ccpversion = ccp_version();
  211. for (i = 0; i < ARRAY_SIZE(aes_aead_algs); i++) {
  212. if (aes_aead_algs[i].version > ccpversion)
  213. continue;
  214. ret = ccp_register_aes_aead(head, &aes_aead_algs[i]);
  215. if (ret)
  216. return ret;
  217. }
  218. return 0;
  219. }