algboss.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Create default crypto algorithm instances.
  3. *
  4. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <crypto/internal/aead.h>
  13. #include <linux/completion.h>
  14. #include <linux/ctype.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/kthread.h>
  18. #include <linux/module.h>
  19. #include <linux/notifier.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/sched/signal.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include "internal.h"
  25. struct cryptomgr_param {
  26. struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
  27. struct {
  28. struct rtattr attr;
  29. struct crypto_attr_type data;
  30. } type;
  31. union {
  32. struct rtattr attr;
  33. struct {
  34. struct rtattr attr;
  35. struct crypto_attr_alg data;
  36. } alg;
  37. struct {
  38. struct rtattr attr;
  39. struct crypto_attr_u32 data;
  40. } nu32;
  41. } attrs[CRYPTO_MAX_ATTRS];
  42. char template[CRYPTO_MAX_ALG_NAME];
  43. struct crypto_larval *larval;
  44. u32 otype;
  45. u32 omask;
  46. };
  47. struct crypto_test_param {
  48. char driver[CRYPTO_MAX_ALG_NAME];
  49. char alg[CRYPTO_MAX_ALG_NAME];
  50. u32 type;
  51. };
  52. static int cryptomgr_probe(void *data)
  53. {
  54. struct cryptomgr_param *param = data;
  55. struct crypto_template *tmpl;
  56. struct crypto_instance *inst;
  57. int err;
  58. tmpl = crypto_lookup_template(param->template);
  59. if (!tmpl)
  60. goto out;
  61. do {
  62. if (tmpl->create) {
  63. err = tmpl->create(tmpl, param->tb);
  64. continue;
  65. }
  66. inst = tmpl->alloc(param->tb);
  67. if (IS_ERR(inst))
  68. err = PTR_ERR(inst);
  69. else if ((err = crypto_register_instance(tmpl, inst)))
  70. tmpl->free(inst);
  71. } while (err == -EAGAIN && !signal_pending(current));
  72. crypto_tmpl_put(tmpl);
  73. out:
  74. complete_all(&param->larval->completion);
  75. crypto_alg_put(&param->larval->alg);
  76. kfree(param);
  77. module_put_and_exit(0);
  78. }
  79. static int cryptomgr_schedule_probe(struct crypto_larval *larval)
  80. {
  81. struct task_struct *thread;
  82. struct cryptomgr_param *param;
  83. const char *name = larval->alg.cra_name;
  84. const char *p;
  85. unsigned int len;
  86. int i;
  87. if (!try_module_get(THIS_MODULE))
  88. goto err;
  89. param = kzalloc(sizeof(*param), GFP_KERNEL);
  90. if (!param)
  91. goto err_put_module;
  92. for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
  93. ;
  94. len = p - name;
  95. if (!len || *p != '(')
  96. goto err_free_param;
  97. memcpy(param->template, name, len);
  98. i = 0;
  99. for (;;) {
  100. int notnum = 0;
  101. name = ++p;
  102. for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
  103. notnum |= !isdigit(*p);
  104. if (*p == '(') {
  105. int recursion = 0;
  106. for (;;) {
  107. if (!*++p)
  108. goto err_free_param;
  109. if (*p == '(')
  110. recursion++;
  111. else if (*p == ')' && !recursion--)
  112. break;
  113. }
  114. notnum = 1;
  115. p++;
  116. }
  117. len = p - name;
  118. if (!len)
  119. goto err_free_param;
  120. if (notnum) {
  121. param->attrs[i].alg.attr.rta_len =
  122. sizeof(param->attrs[i].alg);
  123. param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
  124. memcpy(param->attrs[i].alg.data.name, name, len);
  125. } else {
  126. param->attrs[i].nu32.attr.rta_len =
  127. sizeof(param->attrs[i].nu32);
  128. param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
  129. param->attrs[i].nu32.data.num =
  130. simple_strtol(name, NULL, 0);
  131. }
  132. param->tb[i + 1] = &param->attrs[i].attr;
  133. i++;
  134. if (i >= CRYPTO_MAX_ATTRS)
  135. goto err_free_param;
  136. if (*p == ')')
  137. break;
  138. if (*p != ',')
  139. goto err_free_param;
  140. }
  141. if (!i)
  142. goto err_free_param;
  143. param->tb[i + 1] = NULL;
  144. param->type.attr.rta_len = sizeof(param->type);
  145. param->type.attr.rta_type = CRYPTOA_TYPE;
  146. param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
  147. param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
  148. param->tb[0] = &param->type.attr;
  149. param->otype = larval->alg.cra_flags;
  150. param->omask = larval->mask;
  151. crypto_alg_get(&larval->alg);
  152. param->larval = larval;
  153. thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
  154. if (IS_ERR(thread))
  155. goto err_put_larval;
  156. wait_for_completion_interruptible(&larval->completion);
  157. return NOTIFY_STOP;
  158. err_put_larval:
  159. crypto_alg_put(&larval->alg);
  160. err_free_param:
  161. kfree(param);
  162. err_put_module:
  163. module_put(THIS_MODULE);
  164. err:
  165. return NOTIFY_OK;
  166. }
  167. static int cryptomgr_test(void *data)
  168. {
  169. struct crypto_test_param *param = data;
  170. u32 type = param->type;
  171. int err = 0;
  172. #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
  173. goto skiptest;
  174. #endif
  175. if (type & CRYPTO_ALG_TESTED)
  176. goto skiptest;
  177. err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
  178. skiptest:
  179. crypto_alg_tested(param->driver, err);
  180. kfree(param);
  181. module_put_and_exit(0);
  182. }
  183. static int cryptomgr_schedule_test(struct crypto_alg *alg)
  184. {
  185. struct task_struct *thread;
  186. struct crypto_test_param *param;
  187. u32 type;
  188. if (!try_module_get(THIS_MODULE))
  189. goto err;
  190. param = kzalloc(sizeof(*param), GFP_KERNEL);
  191. if (!param)
  192. goto err_put_module;
  193. memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
  194. memcpy(param->alg, alg->cra_name, sizeof(param->alg));
  195. type = alg->cra_flags;
  196. /* Do not test internal algorithms. */
  197. if (type & CRYPTO_ALG_INTERNAL)
  198. type |= CRYPTO_ALG_TESTED;
  199. param->type = type;
  200. thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
  201. if (IS_ERR(thread))
  202. goto err_free_param;
  203. return NOTIFY_STOP;
  204. err_free_param:
  205. kfree(param);
  206. err_put_module:
  207. module_put(THIS_MODULE);
  208. err:
  209. return NOTIFY_OK;
  210. }
  211. static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
  212. void *data)
  213. {
  214. switch (msg) {
  215. case CRYPTO_MSG_ALG_REQUEST:
  216. return cryptomgr_schedule_probe(data);
  217. case CRYPTO_MSG_ALG_REGISTER:
  218. return cryptomgr_schedule_test(data);
  219. }
  220. return NOTIFY_DONE;
  221. }
  222. static struct notifier_block cryptomgr_notifier = {
  223. .notifier_call = cryptomgr_notify,
  224. };
  225. static int __init cryptomgr_init(void)
  226. {
  227. return crypto_register_notifier(&cryptomgr_notifier);
  228. }
  229. static void __exit cryptomgr_exit(void)
  230. {
  231. int err = crypto_unregister_notifier(&cryptomgr_notifier);
  232. BUG_ON(err);
  233. }
  234. subsys_initcall(cryptomgr_init);
  235. module_exit(cryptomgr_exit);
  236. MODULE_LICENSE("GPL");
  237. MODULE_DESCRIPTION("Crypto Algorithm Manager");