ccp-crypto-aes-galois.c 5.9 KB

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