ccp-crypto-aes-galois.c 5.9 KB

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