ccp-crypto-aes-galois.c 6.1 KB

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