algboss.c 6.2 KB

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