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/ctype.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/kthread.h>
  17. #include <linux/module.h>
  18. #include <linux/notifier.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/string.h>
  23. #include "internal.h"
  24. struct cryptomgr_param {
  25. struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
  26. struct {
  27. struct rtattr attr;
  28. struct crypto_attr_type data;
  29. } type;
  30. union {
  31. struct rtattr attr;
  32. struct {
  33. struct rtattr attr;
  34. struct crypto_attr_alg data;
  35. } alg;
  36. struct {
  37. struct rtattr attr;
  38. struct crypto_attr_u32 data;
  39. } nu32;
  40. } attrs[CRYPTO_MAX_ATTRS];
  41. char larval[CRYPTO_MAX_ALG_NAME];
  42. char template[CRYPTO_MAX_ALG_NAME];
  43. u32 otype;
  44. u32 omask;
  45. };
  46. struct crypto_test_param {
  47. char driver[CRYPTO_MAX_ALG_NAME];
  48. char alg[CRYPTO_MAX_ALG_NAME];
  49. u32 type;
  50. };
  51. static int cryptomgr_probe(void *data)
  52. {
  53. struct cryptomgr_param *param = data;
  54. struct crypto_template *tmpl;
  55. struct crypto_instance *inst;
  56. int err;
  57. tmpl = crypto_lookup_template(param->template);
  58. if (!tmpl)
  59. goto err;
  60. do {
  61. if (tmpl->create) {
  62. err = tmpl->create(tmpl, param->tb);
  63. continue;
  64. }
  65. inst = tmpl->alloc(param->tb);
  66. if (IS_ERR(inst))
  67. err = PTR_ERR(inst);
  68. else if ((err = crypto_register_instance(tmpl, inst)))
  69. tmpl->free(inst);
  70. } while (err == -EAGAIN && !signal_pending(current));
  71. crypto_tmpl_put(tmpl);
  72. if (err)
  73. goto err;
  74. out:
  75. kfree(param);
  76. module_put_and_exit(0);
  77. err:
  78. crypto_larval_error(param->larval, param->otype, param->omask);
  79. goto out;
  80. }
  81. static int cryptomgr_schedule_probe(struct crypto_larval *larval)
  82. {
  83. struct task_struct *thread;
  84. struct cryptomgr_param *param;
  85. const char *name = larval->alg.cra_name;
  86. const char *p;
  87. unsigned int len;
  88. int i;
  89. if (!try_module_get(THIS_MODULE))
  90. goto err;
  91. param = kzalloc(sizeof(*param), GFP_KERNEL);
  92. if (!param)
  93. goto err_put_module;
  94. for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
  95. ;
  96. len = p - name;
  97. if (!len || *p != '(')
  98. goto err_free_param;
  99. memcpy(param->template, name, len);
  100. i = 0;
  101. for (;;) {
  102. int notnum = 0;
  103. name = ++p;
  104. len = 0;
  105. for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
  106. notnum |= !isdigit(*p);
  107. if (*p == '(') {
  108. int recursion = 0;
  109. for (;;) {
  110. if (!*++p)
  111. goto err_free_param;
  112. if (*p == '(')
  113. recursion++;
  114. else if (*p == ')' && !recursion--)
  115. break;
  116. }
  117. notnum = 1;
  118. p++;
  119. }
  120. len = p - name;
  121. if (!len)
  122. goto err_free_param;
  123. if (notnum) {
  124. param->attrs[i].alg.attr.rta_len =
  125. sizeof(param->attrs[i].alg);
  126. param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
  127. memcpy(param->attrs[i].alg.data.name, name, len);
  128. } else {
  129. param->attrs[i].nu32.attr.rta_len =
  130. sizeof(param->attrs[i].nu32);
  131. param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
  132. param->attrs[i].nu32.data.num =
  133. simple_strtol(name, NULL, 0);
  134. }
  135. param->tb[i + 1] = &param->attrs[i].attr;
  136. i++;
  137. if (i >= CRYPTO_MAX_ATTRS)
  138. goto err_free_param;
  139. if (*p == ')')
  140. break;
  141. if (*p != ',')
  142. goto err_free_param;
  143. }
  144. if (!i)
  145. goto err_free_param;
  146. param->tb[i + 1] = NULL;
  147. param->type.attr.rta_len = sizeof(param->type);
  148. param->type.attr.rta_type = CRYPTOA_TYPE;
  149. param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
  150. param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
  151. param->tb[0] = &param->type.attr;
  152. param->otype = larval->alg.cra_flags;
  153. param->omask = larval->mask;
  154. memcpy(param->larval, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
  155. thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
  156. if (IS_ERR(thread))
  157. goto err_free_param;
  158. return NOTIFY_STOP;
  159. err_free_param:
  160. kfree(param);
  161. err_put_module:
  162. module_put(THIS_MODULE);
  163. err:
  164. return NOTIFY_OK;
  165. }
  166. static int cryptomgr_test(void *data)
  167. {
  168. struct crypto_test_param *param = data;
  169. u32 type = param->type;
  170. int err = 0;
  171. #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
  172. goto skiptest;
  173. #endif
  174. if (type & CRYPTO_ALG_TESTED)
  175. goto skiptest;
  176. err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
  177. skiptest:
  178. crypto_alg_tested(param->driver, err);
  179. kfree(param);
  180. module_put_and_exit(0);
  181. }
  182. static int cryptomgr_schedule_test(struct crypto_alg *alg)
  183. {
  184. struct task_struct *thread;
  185. struct crypto_test_param *param;
  186. u32 type;
  187. if (!try_module_get(THIS_MODULE))
  188. goto err;
  189. param = kzalloc(sizeof(*param), GFP_KERNEL);
  190. if (!param)
  191. goto err_put_module;
  192. memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
  193. memcpy(param->alg, alg->cra_name, sizeof(param->alg));
  194. type = alg->cra_flags;
  195. /* This piece of crap needs to disappear into per-type test hooks. */
  196. if ((!((type ^ CRYPTO_ALG_TYPE_BLKCIPHER) &
  197. CRYPTO_ALG_TYPE_BLKCIPHER_MASK) && !(type & CRYPTO_ALG_GENIV) &&
  198. ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  199. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  200. alg->cra_ablkcipher.ivsize)) ||
  201. (!((type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK) &&
  202. alg->cra_type == &crypto_nivaead_type && alg->cra_aead.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");