algboss.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.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. len = 0;
  103. for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
  104. notnum |= !isdigit(*p);
  105. if (*p == '(') {
  106. int recursion = 0;
  107. for (;;) {
  108. if (!*++p)
  109. goto err_free_param;
  110. if (*p == '(')
  111. recursion++;
  112. else if (*p == ')' && !recursion--)
  113. break;
  114. }
  115. notnum = 1;
  116. p++;
  117. }
  118. len = p - name;
  119. if (!len)
  120. goto err_free_param;
  121. if (notnum) {
  122. param->attrs[i].alg.attr.rta_len =
  123. sizeof(param->attrs[i].alg);
  124. param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
  125. memcpy(param->attrs[i].alg.data.name, name, len);
  126. } else {
  127. param->attrs[i].nu32.attr.rta_len =
  128. sizeof(param->attrs[i].nu32);
  129. param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
  130. param->attrs[i].nu32.data.num =
  131. simple_strtol(name, NULL, 0);
  132. }
  133. param->tb[i + 1] = &param->attrs[i].attr;
  134. i++;
  135. if (i >= CRYPTO_MAX_ATTRS)
  136. goto err_free_param;
  137. if (*p == ')')
  138. break;
  139. if (*p != ',')
  140. goto err_free_param;
  141. }
  142. if (!i)
  143. goto err_free_param;
  144. param->tb[i + 1] = NULL;
  145. param->type.attr.rta_len = sizeof(param->type);
  146. param->type.attr.rta_type = CRYPTOA_TYPE;
  147. param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
  148. param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
  149. param->tb[0] = &param->type.attr;
  150. param->otype = larval->alg.cra_flags;
  151. param->omask = larval->mask;
  152. crypto_alg_get(&larval->alg);
  153. param->larval = larval;
  154. thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
  155. if (IS_ERR(thread))
  156. goto err_put_larval;
  157. wait_for_completion_interruptible(&larval->completion);
  158. return NOTIFY_STOP;
  159. err_put_larval:
  160. crypto_alg_put(&larval->alg);
  161. err_free_param:
  162. kfree(param);
  163. err_put_module:
  164. module_put(THIS_MODULE);
  165. err:
  166. return NOTIFY_OK;
  167. }
  168. static int cryptomgr_test(void *data)
  169. {
  170. struct crypto_test_param *param = data;
  171. u32 type = param->type;
  172. int err = 0;
  173. #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
  174. goto skiptest;
  175. #endif
  176. if (type & CRYPTO_ALG_TESTED)
  177. goto skiptest;
  178. err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
  179. skiptest:
  180. crypto_alg_tested(param->driver, err);
  181. kfree(param);
  182. module_put_and_exit(0);
  183. }
  184. static int cryptomgr_schedule_test(struct crypto_alg *alg)
  185. {
  186. struct task_struct *thread;
  187. struct crypto_test_param *param;
  188. u32 type;
  189. if (!try_module_get(THIS_MODULE))
  190. goto err;
  191. param = kzalloc(sizeof(*param), GFP_KERNEL);
  192. if (!param)
  193. goto err_put_module;
  194. memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
  195. memcpy(param->alg, alg->cra_name, sizeof(param->alg));
  196. type = alg->cra_flags;
  197. /* This piece of crap needs to disappear into per-type test hooks. */
  198. if (!((type ^ CRYPTO_ALG_TYPE_BLKCIPHER) &
  199. CRYPTO_ALG_TYPE_BLKCIPHER_MASK) && !(type & CRYPTO_ALG_GENIV) &&
  200. ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  201. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  202. alg->cra_ablkcipher.ivsize))
  203. type |= CRYPTO_ALG_TESTED;
  204. param->type = type;
  205. thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
  206. if (IS_ERR(thread))
  207. goto err_free_param;
  208. return NOTIFY_STOP;
  209. err_free_param:
  210. kfree(param);
  211. err_put_module:
  212. module_put(THIS_MODULE);
  213. err:
  214. return NOTIFY_OK;
  215. }
  216. static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
  217. void *data)
  218. {
  219. switch (msg) {
  220. case CRYPTO_MSG_ALG_REQUEST:
  221. return cryptomgr_schedule_probe(data);
  222. case CRYPTO_MSG_ALG_REGISTER:
  223. return cryptomgr_schedule_test(data);
  224. }
  225. return NOTIFY_DONE;
  226. }
  227. static struct notifier_block cryptomgr_notifier = {
  228. .notifier_call = cryptomgr_notify,
  229. };
  230. static int __init cryptomgr_init(void)
  231. {
  232. return crypto_register_notifier(&cryptomgr_notifier);
  233. }
  234. static void __exit cryptomgr_exit(void)
  235. {
  236. int err = crypto_unregister_notifier(&cryptomgr_notifier);
  237. BUG_ON(err);
  238. }
  239. subsys_initcall(cryptomgr_init);
  240. module_exit(cryptomgr_exit);
  241. MODULE_LICENSE("GPL");
  242. MODULE_DESCRIPTION("Crypto Algorithm Manager");