algboss.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Create default crypto algorithm instances.
  4. *
  5. * Copyright (c) 2006 Herbert Xu <[email protected]>
  6. */
  7. #include <crypto/internal/aead.h>
  8. #include <linux/completion.h>
  9. #include <linux/ctype.h>
  10. #include <linux/err.h>
  11. #include <linux/init.h>
  12. #include <linux/kthread.h>
  13. #include <linux/module.h>
  14. #include <linux/notifier.h>
  15. #include <linux/rtnetlink.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include "internal.h"
  20. struct cryptomgr_param {
  21. struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
  22. struct {
  23. struct rtattr attr;
  24. struct crypto_attr_type data;
  25. } type;
  26. struct {
  27. struct rtattr attr;
  28. struct crypto_attr_alg data;
  29. } attrs[CRYPTO_MAX_ATTRS];
  30. char template[CRYPTO_MAX_ALG_NAME];
  31. struct crypto_larval *larval;
  32. u32 otype;
  33. u32 omask;
  34. };
  35. struct crypto_test_param {
  36. char driver[CRYPTO_MAX_ALG_NAME];
  37. char alg[CRYPTO_MAX_ALG_NAME];
  38. u32 type;
  39. };
  40. static int cryptomgr_probe(void *data)
  41. {
  42. struct cryptomgr_param *param = data;
  43. struct crypto_template *tmpl;
  44. int err;
  45. tmpl = crypto_lookup_template(param->template);
  46. if (!tmpl)
  47. goto out;
  48. do {
  49. err = tmpl->create(tmpl, param->tb);
  50. } while (err == -EAGAIN && !signal_pending(current));
  51. crypto_tmpl_put(tmpl);
  52. out:
  53. complete_all(&param->larval->completion);
  54. crypto_alg_put(&param->larval->alg);
  55. kfree(param);
  56. module_put_and_kthread_exit(0);
  57. }
  58. static int cryptomgr_schedule_probe(struct crypto_larval *larval)
  59. {
  60. struct task_struct *thread;
  61. struct cryptomgr_param *param;
  62. const char *name = larval->alg.cra_name;
  63. const char *p;
  64. unsigned int len;
  65. int i;
  66. if (!try_module_get(THIS_MODULE))
  67. goto err;
  68. param = kzalloc(sizeof(*param), GFP_KERNEL);
  69. if (!param)
  70. goto err_put_module;
  71. for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
  72. ;
  73. len = p - name;
  74. if (!len || *p != '(')
  75. goto err_free_param;
  76. memcpy(param->template, name, len);
  77. i = 0;
  78. for (;;) {
  79. name = ++p;
  80. for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
  81. ;
  82. if (*p == '(') {
  83. int recursion = 0;
  84. for (;;) {
  85. if (!*++p)
  86. goto err_free_param;
  87. if (*p == '(')
  88. recursion++;
  89. else if (*p == ')' && !recursion--)
  90. break;
  91. }
  92. p++;
  93. }
  94. len = p - name;
  95. if (!len)
  96. goto err_free_param;
  97. param->attrs[i].attr.rta_len = sizeof(param->attrs[i]);
  98. param->attrs[i].attr.rta_type = CRYPTOA_ALG;
  99. memcpy(param->attrs[i].data.name, name, len);
  100. param->tb[i + 1] = &param->attrs[i].attr;
  101. i++;
  102. if (i >= CRYPTO_MAX_ATTRS)
  103. goto err_free_param;
  104. if (*p == ')')
  105. break;
  106. if (*p != ',')
  107. goto err_free_param;
  108. }
  109. if (!i)
  110. goto err_free_param;
  111. param->tb[i + 1] = NULL;
  112. param->type.attr.rta_len = sizeof(param->type);
  113. param->type.attr.rta_type = CRYPTOA_TYPE;
  114. param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
  115. param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
  116. param->tb[0] = &param->type.attr;
  117. param->otype = larval->alg.cra_flags;
  118. param->omask = larval->mask;
  119. crypto_alg_get(&larval->alg);
  120. param->larval = larval;
  121. thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
  122. if (IS_ERR(thread))
  123. goto err_put_larval;
  124. return NOTIFY_STOP;
  125. err_put_larval:
  126. crypto_alg_put(&larval->alg);
  127. err_free_param:
  128. kfree(param);
  129. err_put_module:
  130. module_put(THIS_MODULE);
  131. err:
  132. return NOTIFY_OK;
  133. }
  134. static int cryptomgr_test(void *data)
  135. {
  136. struct crypto_test_param *param = data;
  137. u32 type = param->type;
  138. int err;
  139. err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
  140. crypto_alg_tested(param->driver, err);
  141. kfree(param);
  142. module_put_and_kthread_exit(0);
  143. }
  144. static int cryptomgr_schedule_test(struct crypto_alg *alg)
  145. {
  146. struct task_struct *thread;
  147. struct crypto_test_param *param;
  148. if (IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS))
  149. return NOTIFY_DONE;
  150. if (!try_module_get(THIS_MODULE))
  151. goto err;
  152. param = kzalloc(sizeof(*param), GFP_KERNEL);
  153. if (!param)
  154. goto err_put_module;
  155. memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
  156. memcpy(param->alg, alg->cra_name, sizeof(param->alg));
  157. param->type = alg->cra_flags;
  158. thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
  159. if (IS_ERR(thread))
  160. goto err_free_param;
  161. return NOTIFY_STOP;
  162. err_free_param:
  163. kfree(param);
  164. err_put_module:
  165. module_put(THIS_MODULE);
  166. err:
  167. return NOTIFY_OK;
  168. }
  169. static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
  170. void *data)
  171. {
  172. switch (msg) {
  173. case CRYPTO_MSG_ALG_REQUEST:
  174. return cryptomgr_schedule_probe(data);
  175. case CRYPTO_MSG_ALG_REGISTER:
  176. return cryptomgr_schedule_test(data);
  177. case CRYPTO_MSG_ALG_LOADED:
  178. break;
  179. }
  180. return NOTIFY_DONE;
  181. }
  182. static struct notifier_block cryptomgr_notifier = {
  183. .notifier_call = cryptomgr_notify,
  184. };
  185. static int __init cryptomgr_init(void)
  186. {
  187. return crypto_register_notifier(&cryptomgr_notifier);
  188. }
  189. static void __exit cryptomgr_exit(void)
  190. {
  191. int err = crypto_unregister_notifier(&cryptomgr_notifier);
  192. BUG_ON(err);
  193. }
  194. /*
  195. * This is arch_initcall() so that the crypto self-tests are run on algorithms
  196. * registered early by subsys_initcall(). subsys_initcall() is needed for
  197. * generic implementations so that they're available for comparison tests when
  198. * other implementations are registered later by module_init().
  199. */
  200. arch_initcall(cryptomgr_init);
  201. module_exit(cryptomgr_exit);
  202. MODULE_LICENSE("GPL");
  203. MODULE_DESCRIPTION("Crypto Algorithm Manager");