akcipher.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Public Key Encryption
  4. *
  5. * Copyright (c) 2015, Intel Corporation
  6. * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
  7. */
  8. #include <crypto/internal/akcipher.h>
  9. #include <linux/cryptouser.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <net/netlink.h>
  18. #include "internal.h"
  19. #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
  20. struct crypto_akcipher_sync_data {
  21. struct crypto_akcipher *tfm;
  22. const void *src;
  23. void *dst;
  24. unsigned int slen;
  25. unsigned int dlen;
  26. struct akcipher_request *req;
  27. struct crypto_wait cwait;
  28. struct scatterlist sg;
  29. u8 *buf;
  30. };
  31. static int __maybe_unused crypto_akcipher_report(
  32. struct sk_buff *skb, struct crypto_alg *alg)
  33. {
  34. struct crypto_report_akcipher rakcipher;
  35. memset(&rakcipher, 0, sizeof(rakcipher));
  36. strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
  37. return nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
  38. sizeof(rakcipher), &rakcipher);
  39. }
  40. static void __maybe_unused crypto_akcipher_show(struct seq_file *m,
  41. struct crypto_alg *alg)
  42. {
  43. seq_puts(m, "type : akcipher\n");
  44. }
  45. static void crypto_akcipher_exit_tfm(struct crypto_tfm *tfm)
  46. {
  47. struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
  48. struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
  49. alg->exit(akcipher);
  50. }
  51. static int crypto_akcipher_init_tfm(struct crypto_tfm *tfm)
  52. {
  53. struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
  54. struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
  55. if (alg->exit)
  56. akcipher->base.exit = crypto_akcipher_exit_tfm;
  57. if (alg->init)
  58. return alg->init(akcipher);
  59. return 0;
  60. }
  61. static void crypto_akcipher_free_instance(struct crypto_instance *inst)
  62. {
  63. struct akcipher_instance *akcipher = akcipher_instance(inst);
  64. akcipher->free(akcipher);
  65. }
  66. static const struct crypto_type crypto_akcipher_type = {
  67. .extsize = crypto_alg_extsize,
  68. .init_tfm = crypto_akcipher_init_tfm,
  69. .free = crypto_akcipher_free_instance,
  70. #ifdef CONFIG_PROC_FS
  71. .show = crypto_akcipher_show,
  72. #endif
  73. #if IS_ENABLED(CONFIG_CRYPTO_USER)
  74. .report = crypto_akcipher_report,
  75. #endif
  76. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  77. .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
  78. .type = CRYPTO_ALG_TYPE_AKCIPHER,
  79. .tfmsize = offsetof(struct crypto_akcipher, base),
  80. .algsize = offsetof(struct akcipher_alg, base),
  81. };
  82. int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
  83. struct crypto_instance *inst,
  84. const char *name, u32 type, u32 mask)
  85. {
  86. spawn->base.frontend = &crypto_akcipher_type;
  87. return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
  88. }
  89. EXPORT_SYMBOL_GPL(crypto_grab_akcipher);
  90. struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
  91. u32 mask)
  92. {
  93. return crypto_alloc_tfm(alg_name, &crypto_akcipher_type, type, mask);
  94. }
  95. EXPORT_SYMBOL_GPL(crypto_alloc_akcipher);
  96. static void akcipher_prepare_alg(struct akcipher_alg *alg)
  97. {
  98. struct crypto_alg *base = &alg->base;
  99. base->cra_type = &crypto_akcipher_type;
  100. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  101. base->cra_flags |= CRYPTO_ALG_TYPE_AKCIPHER;
  102. }
  103. static int akcipher_default_op(struct akcipher_request *req)
  104. {
  105. return -ENOSYS;
  106. }
  107. static int akcipher_default_set_key(struct crypto_akcipher *tfm,
  108. const void *key, unsigned int keylen)
  109. {
  110. return -ENOSYS;
  111. }
  112. int crypto_register_akcipher(struct akcipher_alg *alg)
  113. {
  114. struct crypto_alg *base = &alg->base;
  115. if (!alg->encrypt)
  116. alg->encrypt = akcipher_default_op;
  117. if (!alg->decrypt)
  118. alg->decrypt = akcipher_default_op;
  119. if (!alg->set_priv_key)
  120. alg->set_priv_key = akcipher_default_set_key;
  121. akcipher_prepare_alg(alg);
  122. return crypto_register_alg(base);
  123. }
  124. EXPORT_SYMBOL_GPL(crypto_register_akcipher);
  125. void crypto_unregister_akcipher(struct akcipher_alg *alg)
  126. {
  127. crypto_unregister_alg(&alg->base);
  128. }
  129. EXPORT_SYMBOL_GPL(crypto_unregister_akcipher);
  130. int akcipher_register_instance(struct crypto_template *tmpl,
  131. struct akcipher_instance *inst)
  132. {
  133. if (WARN_ON(!inst->free))
  134. return -EINVAL;
  135. akcipher_prepare_alg(&inst->alg);
  136. return crypto_register_instance(tmpl, akcipher_crypto_instance(inst));
  137. }
  138. EXPORT_SYMBOL_GPL(akcipher_register_instance);
  139. static int crypto_akcipher_sync_prep(struct crypto_akcipher_sync_data *data)
  140. {
  141. unsigned int reqsize = crypto_akcipher_reqsize(data->tfm);
  142. struct akcipher_request *req;
  143. struct scatterlist *sg;
  144. unsigned int mlen;
  145. unsigned int len;
  146. u8 *buf;
  147. mlen = max(data->slen, data->dlen);
  148. len = sizeof(*req) + reqsize + mlen;
  149. if (len < mlen)
  150. return -EOVERFLOW;
  151. req = kzalloc(len, GFP_KERNEL);
  152. if (!req)
  153. return -ENOMEM;
  154. data->req = req;
  155. akcipher_request_set_tfm(req, data->tfm);
  156. buf = (u8 *)(req + 1) + reqsize;
  157. data->buf = buf;
  158. memcpy(buf, data->src, data->slen);
  159. sg = &data->sg;
  160. sg_init_one(sg, buf, mlen);
  161. akcipher_request_set_crypt(req, sg, sg, data->slen, data->dlen);
  162. crypto_init_wait(&data->cwait);
  163. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
  164. crypto_req_done, &data->cwait);
  165. return 0;
  166. }
  167. static int crypto_akcipher_sync_post(struct crypto_akcipher_sync_data *data,
  168. int err)
  169. {
  170. err = crypto_wait_req(err, &data->cwait);
  171. memcpy(data->dst, data->buf, data->dlen);
  172. data->dlen = data->req->dst_len;
  173. kfree_sensitive(data->req);
  174. return err;
  175. }
  176. int crypto_akcipher_sync_encrypt(struct crypto_akcipher *tfm,
  177. const void *src, unsigned int slen,
  178. void *dst, unsigned int dlen)
  179. {
  180. struct crypto_akcipher_sync_data data = {
  181. .tfm = tfm,
  182. .src = src,
  183. .dst = dst,
  184. .slen = slen,
  185. .dlen = dlen,
  186. };
  187. return crypto_akcipher_sync_prep(&data) ?:
  188. crypto_akcipher_sync_post(&data,
  189. crypto_akcipher_encrypt(data.req));
  190. }
  191. EXPORT_SYMBOL_GPL(crypto_akcipher_sync_encrypt);
  192. int crypto_akcipher_sync_decrypt(struct crypto_akcipher *tfm,
  193. const void *src, unsigned int slen,
  194. void *dst, unsigned int dlen)
  195. {
  196. struct crypto_akcipher_sync_data data = {
  197. .tfm = tfm,
  198. .src = src,
  199. .dst = dst,
  200. .slen = slen,
  201. .dlen = dlen,
  202. };
  203. return crypto_akcipher_sync_prep(&data) ?:
  204. crypto_akcipher_sync_post(&data,
  205. crypto_akcipher_decrypt(data.req)) ?:
  206. data.dlen;
  207. }
  208. EXPORT_SYMBOL_GPL(crypto_akcipher_sync_decrypt);
  209. MODULE_LICENSE("GPL");
  210. MODULE_DESCRIPTION("Generic public key cipher type");