ccp-crypto-des3.c 6.3 KB

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