ccp-crypto-des3.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Cryptographic Coprocessor (CCP) DES3 crypto API support
  4. *
  5. * Copyright (C) 2016,2017 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Gary R Hook <ghook@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/algapi.h>
  15. #include <crypto/scatterwalk.h>
  16. #include <crypto/internal/des.h>
  17. #include "ccp-crypto.h"
  18. static int ccp_des3_complete(struct crypto_async_request *async_req, int ret)
  19. {
  20. struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
  21. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  22. struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req);
  23. if (ret)
  24. return ret;
  25. if (ctx->u.des3.mode != CCP_DES3_MODE_ECB)
  26. memcpy(req->info, rctx->iv, DES3_EDE_BLOCK_SIZE);
  27. return 0;
  28. }
  29. static int ccp_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  30. unsigned int key_len)
  31. {
  32. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
  33. struct ccp_crypto_ablkcipher_alg *alg =
  34. ccp_crypto_ablkcipher_alg(crypto_ablkcipher_tfm(tfm));
  35. int err;
  36. err = verify_ablkcipher_des3_key(tfm, key);
  37. if (err)
  38. return err;
  39. /* It's not clear that there is any support for a keysize of 112.
  40. * If needed, the caller should make K1 == K3
  41. */
  42. ctx->u.des3.type = CCP_DES3_TYPE_168;
  43. ctx->u.des3.mode = alg->mode;
  44. ctx->u.des3.key_len = key_len;
  45. memcpy(ctx->u.des3.key, key, key_len);
  46. sg_init_one(&ctx->u.des3.key_sg, ctx->u.des3.key, key_len);
  47. return 0;
  48. }
  49. static int ccp_des3_crypt(struct ablkcipher_request *req, bool encrypt)
  50. {
  51. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  52. struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req);
  53. struct scatterlist *iv_sg = NULL;
  54. unsigned int iv_len = 0;
  55. int ret;
  56. if (!ctx->u.des3.key_len)
  57. return -EINVAL;
  58. if (((ctx->u.des3.mode == CCP_DES3_MODE_ECB) ||
  59. (ctx->u.des3.mode == CCP_DES3_MODE_CBC)) &&
  60. (req->nbytes & (DES3_EDE_BLOCK_SIZE - 1)))
  61. return -EINVAL;
  62. if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) {
  63. if (!req->info)
  64. return -EINVAL;
  65. memcpy(rctx->iv, req->info, DES3_EDE_BLOCK_SIZE);
  66. iv_sg = &rctx->iv_sg;
  67. iv_len = DES3_EDE_BLOCK_SIZE;
  68. sg_init_one(iv_sg, rctx->iv, iv_len);
  69. }
  70. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  71. INIT_LIST_HEAD(&rctx->cmd.entry);
  72. rctx->cmd.engine = CCP_ENGINE_DES3;
  73. rctx->cmd.u.des3.type = ctx->u.des3.type;
  74. rctx->cmd.u.des3.mode = ctx->u.des3.mode;
  75. rctx->cmd.u.des3.action = (encrypt)
  76. ? CCP_DES3_ACTION_ENCRYPT
  77. : CCP_DES3_ACTION_DECRYPT;
  78. rctx->cmd.u.des3.key = &ctx->u.des3.key_sg;
  79. rctx->cmd.u.des3.key_len = ctx->u.des3.key_len;
  80. rctx->cmd.u.des3.iv = iv_sg;
  81. rctx->cmd.u.des3.iv_len = iv_len;
  82. rctx->cmd.u.des3.src = req->src;
  83. rctx->cmd.u.des3.src_len = req->nbytes;
  84. rctx->cmd.u.des3.dst = req->dst;
  85. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  86. return ret;
  87. }
  88. static int ccp_des3_encrypt(struct ablkcipher_request *req)
  89. {
  90. return ccp_des3_crypt(req, true);
  91. }
  92. static int ccp_des3_decrypt(struct ablkcipher_request *req)
  93. {
  94. return ccp_des3_crypt(req, false);
  95. }
  96. static int ccp_des3_cra_init(struct crypto_tfm *tfm)
  97. {
  98. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  99. ctx->complete = ccp_des3_complete;
  100. ctx->u.des3.key_len = 0;
  101. tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_des3_req_ctx);
  102. return 0;
  103. }
  104. static void ccp_des3_cra_exit(struct crypto_tfm *tfm)
  105. {
  106. }
  107. static struct crypto_alg ccp_des3_defaults = {
  108. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  109. CRYPTO_ALG_ASYNC |
  110. CRYPTO_ALG_KERN_DRIVER_ONLY |
  111. CRYPTO_ALG_NEED_FALLBACK,
  112. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  113. .cra_ctxsize = sizeof(struct ccp_ctx),
  114. .cra_priority = CCP_CRA_PRIORITY,
  115. .cra_type = &crypto_ablkcipher_type,
  116. .cra_init = ccp_des3_cra_init,
  117. .cra_exit = ccp_des3_cra_exit,
  118. .cra_module = THIS_MODULE,
  119. .cra_ablkcipher = {
  120. .setkey = ccp_des3_setkey,
  121. .encrypt = ccp_des3_encrypt,
  122. .decrypt = ccp_des3_decrypt,
  123. .min_keysize = DES3_EDE_KEY_SIZE,
  124. .max_keysize = DES3_EDE_KEY_SIZE,
  125. },
  126. };
  127. struct ccp_des3_def {
  128. enum ccp_des3_mode mode;
  129. unsigned int version;
  130. const char *name;
  131. const char *driver_name;
  132. unsigned int blocksize;
  133. unsigned int ivsize;
  134. struct crypto_alg *alg_defaults;
  135. };
  136. static struct ccp_des3_def des3_algs[] = {
  137. {
  138. .mode = CCP_DES3_MODE_ECB,
  139. .version = CCP_VERSION(5, 0),
  140. .name = "ecb(des3_ede)",
  141. .driver_name = "ecb-des3-ccp",
  142. .blocksize = DES3_EDE_BLOCK_SIZE,
  143. .ivsize = 0,
  144. .alg_defaults = &ccp_des3_defaults,
  145. },
  146. {
  147. .mode = CCP_DES3_MODE_CBC,
  148. .version = CCP_VERSION(5, 0),
  149. .name = "cbc(des3_ede)",
  150. .driver_name = "cbc-des3-ccp",
  151. .blocksize = DES3_EDE_BLOCK_SIZE,
  152. .ivsize = DES3_EDE_BLOCK_SIZE,
  153. .alg_defaults = &ccp_des3_defaults,
  154. },
  155. };
  156. static int ccp_register_des3_alg(struct list_head *head,
  157. const struct ccp_des3_def *def)
  158. {
  159. struct ccp_crypto_ablkcipher_alg *ccp_alg;
  160. struct crypto_alg *alg;
  161. int ret;
  162. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  163. if (!ccp_alg)
  164. return -ENOMEM;
  165. INIT_LIST_HEAD(&ccp_alg->entry);
  166. ccp_alg->mode = def->mode;
  167. /* Copy the defaults and override as necessary */
  168. alg = &ccp_alg->alg;
  169. *alg = *def->alg_defaults;
  170. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  171. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  172. def->driver_name);
  173. alg->cra_blocksize = def->blocksize;
  174. alg->cra_ablkcipher.ivsize = def->ivsize;
  175. ret = crypto_register_alg(alg);
  176. if (ret) {
  177. pr_err("%s ablkcipher algorithm registration error (%d)\n",
  178. alg->cra_name, ret);
  179. kfree(ccp_alg);
  180. return ret;
  181. }
  182. list_add(&ccp_alg->entry, head);
  183. return 0;
  184. }
  185. int ccp_register_des3_algs(struct list_head *head)
  186. {
  187. int i, ret;
  188. unsigned int ccpversion = ccp_version();
  189. for (i = 0; i < ARRAY_SIZE(des3_algs); i++) {
  190. if (des3_algs[i].version > ccpversion)
  191. continue;
  192. ret = ccp_register_des3_alg(head, &des3_algs[i]);
  193. if (ret)
  194. return ret;
  195. }
  196. return 0;
  197. }