akcipher.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. static int __maybe_unused crypto_akcipher_report(
  21. struct sk_buff *skb, struct crypto_alg *alg)
  22. {
  23. struct crypto_report_akcipher rakcipher;
  24. memset(&rakcipher, 0, sizeof(rakcipher));
  25. strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
  26. return nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
  27. sizeof(rakcipher), &rakcipher);
  28. }
  29. static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
  30. __maybe_unused;
  31. static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
  32. {
  33. seq_puts(m, "type : akcipher\n");
  34. }
  35. static void crypto_akcipher_exit_tfm(struct crypto_tfm *tfm)
  36. {
  37. struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
  38. struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
  39. alg->exit(akcipher);
  40. }
  41. static int crypto_akcipher_init_tfm(struct crypto_tfm *tfm)
  42. {
  43. struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
  44. struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
  45. if (alg->exit)
  46. akcipher->base.exit = crypto_akcipher_exit_tfm;
  47. if (alg->init)
  48. return alg->init(akcipher);
  49. return 0;
  50. }
  51. static void crypto_akcipher_free_instance(struct crypto_instance *inst)
  52. {
  53. struct akcipher_instance *akcipher = akcipher_instance(inst);
  54. akcipher->free(akcipher);
  55. }
  56. static const struct crypto_type crypto_akcipher_type = {
  57. .extsize = crypto_alg_extsize,
  58. .init_tfm = crypto_akcipher_init_tfm,
  59. .free = crypto_akcipher_free_instance,
  60. #ifdef CONFIG_PROC_FS
  61. .show = crypto_akcipher_show,
  62. #endif
  63. #if IS_ENABLED(CONFIG_CRYPTO_USER)
  64. .report = crypto_akcipher_report,
  65. #endif
  66. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  67. .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
  68. .type = CRYPTO_ALG_TYPE_AKCIPHER,
  69. .tfmsize = offsetof(struct crypto_akcipher, base),
  70. };
  71. int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
  72. struct crypto_instance *inst,
  73. const char *name, u32 type, u32 mask)
  74. {
  75. spawn->base.frontend = &crypto_akcipher_type;
  76. return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
  77. }
  78. EXPORT_SYMBOL_GPL(crypto_grab_akcipher);
  79. struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
  80. u32 mask)
  81. {
  82. return crypto_alloc_tfm(alg_name, &crypto_akcipher_type, type, mask);
  83. }
  84. EXPORT_SYMBOL_GPL(crypto_alloc_akcipher);
  85. static void akcipher_prepare_alg(struct akcipher_alg *alg)
  86. {
  87. struct crypto_alg *base = &alg->base;
  88. base->cra_type = &crypto_akcipher_type;
  89. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  90. base->cra_flags |= CRYPTO_ALG_TYPE_AKCIPHER;
  91. }
  92. static int akcipher_default_op(struct akcipher_request *req)
  93. {
  94. return -ENOSYS;
  95. }
  96. static int akcipher_default_set_key(struct crypto_akcipher *tfm,
  97. const void *key, unsigned int keylen)
  98. {
  99. return -ENOSYS;
  100. }
  101. int crypto_register_akcipher(struct akcipher_alg *alg)
  102. {
  103. struct crypto_alg *base = &alg->base;
  104. if (!alg->sign)
  105. alg->sign = akcipher_default_op;
  106. if (!alg->verify)
  107. alg->verify = akcipher_default_op;
  108. if (!alg->encrypt)
  109. alg->encrypt = akcipher_default_op;
  110. if (!alg->decrypt)
  111. alg->decrypt = akcipher_default_op;
  112. if (!alg->set_priv_key)
  113. alg->set_priv_key = akcipher_default_set_key;
  114. akcipher_prepare_alg(alg);
  115. return crypto_register_alg(base);
  116. }
  117. EXPORT_SYMBOL_GPL(crypto_register_akcipher);
  118. void crypto_unregister_akcipher(struct akcipher_alg *alg)
  119. {
  120. crypto_unregister_alg(&alg->base);
  121. }
  122. EXPORT_SYMBOL_GPL(crypto_unregister_akcipher);
  123. int akcipher_register_instance(struct crypto_template *tmpl,
  124. struct akcipher_instance *inst)
  125. {
  126. if (WARN_ON(!inst->free))
  127. return -EINVAL;
  128. akcipher_prepare_alg(&inst->alg);
  129. return crypto_register_instance(tmpl, akcipher_crypto_instance(inst));
  130. }
  131. EXPORT_SYMBOL_GPL(akcipher_register_instance);
  132. int crypto_akcipher_sync_prep(struct crypto_akcipher_sync_data *data)
  133. {
  134. unsigned int reqsize = crypto_akcipher_reqsize(data->tfm);
  135. struct akcipher_request *req;
  136. struct scatterlist *sg;
  137. unsigned int mlen;
  138. unsigned int len;
  139. u8 *buf;
  140. if (data->dst)
  141. mlen = max(data->slen, data->dlen);
  142. else
  143. mlen = data->slen + data->dlen;
  144. len = sizeof(*req) + reqsize + mlen;
  145. if (len < mlen)
  146. return -EOVERFLOW;
  147. req = kzalloc(len, GFP_KERNEL);
  148. if (!req)
  149. return -ENOMEM;
  150. data->req = req;
  151. akcipher_request_set_tfm(req, data->tfm);
  152. buf = (u8 *)(req + 1) + reqsize;
  153. data->buf = buf;
  154. memcpy(buf, data->src, data->slen);
  155. sg = &data->sg;
  156. sg_init_one(sg, buf, mlen);
  157. akcipher_request_set_crypt(req, sg, data->dst ? sg : NULL,
  158. data->slen, data->dlen);
  159. crypto_init_wait(&data->cwait);
  160. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
  161. crypto_req_done, &data->cwait);
  162. return 0;
  163. }
  164. EXPORT_SYMBOL_GPL(crypto_akcipher_sync_prep);
  165. int crypto_akcipher_sync_post(struct crypto_akcipher_sync_data *data, int err)
  166. {
  167. err = crypto_wait_req(err, &data->cwait);
  168. if (data->dst)
  169. memcpy(data->dst, data->buf, data->dlen);
  170. data->dlen = data->req->dst_len;
  171. kfree_sensitive(data->req);
  172. return err;
  173. }
  174. EXPORT_SYMBOL_GPL(crypto_akcipher_sync_post);
  175. int crypto_akcipher_sync_encrypt(struct crypto_akcipher *tfm,
  176. const void *src, unsigned int slen,
  177. void *dst, unsigned int dlen)
  178. {
  179. struct crypto_akcipher_sync_data data = {
  180. .tfm = tfm,
  181. .src = src,
  182. .dst = dst,
  183. .slen = slen,
  184. .dlen = dlen,
  185. };
  186. return crypto_akcipher_sync_prep(&data) ?:
  187. crypto_akcipher_sync_post(&data,
  188. crypto_akcipher_encrypt(data.req));
  189. }
  190. EXPORT_SYMBOL_GPL(crypto_akcipher_sync_encrypt);
  191. int crypto_akcipher_sync_decrypt(struct crypto_akcipher *tfm,
  192. const void *src, unsigned int slen,
  193. void *dst, unsigned int dlen)
  194. {
  195. struct crypto_akcipher_sync_data data = {
  196. .tfm = tfm,
  197. .src = src,
  198. .dst = dst,
  199. .slen = slen,
  200. .dlen = dlen,
  201. };
  202. return crypto_akcipher_sync_prep(&data) ?:
  203. crypto_akcipher_sync_post(&data,
  204. crypto_akcipher_decrypt(data.req)) ?:
  205. data.dlen;
  206. }
  207. EXPORT_SYMBOL_GPL(crypto_akcipher_sync_decrypt);
  208. static void crypto_exit_akcipher_ops_sig(struct crypto_tfm *tfm)
  209. {
  210. struct crypto_akcipher **ctx = crypto_tfm_ctx(tfm);
  211. crypto_free_akcipher(*ctx);
  212. }
  213. int crypto_init_akcipher_ops_sig(struct crypto_tfm *tfm)
  214. {
  215. struct crypto_akcipher **ctx = crypto_tfm_ctx(tfm);
  216. struct crypto_alg *calg = tfm->__crt_alg;
  217. struct crypto_akcipher *akcipher;
  218. if (!crypto_mod_get(calg))
  219. return -EAGAIN;
  220. akcipher = crypto_create_tfm(calg, &crypto_akcipher_type);
  221. if (IS_ERR(akcipher)) {
  222. crypto_mod_put(calg);
  223. return PTR_ERR(akcipher);
  224. }
  225. *ctx = akcipher;
  226. tfm->exit = crypto_exit_akcipher_ops_sig;
  227. return 0;
  228. }
  229. EXPORT_SYMBOL_GPL(crypto_init_akcipher_ops_sig);
  230. MODULE_LICENSE("GPL");
  231. MODULE_DESCRIPTION("Generic public key cipher type");