algboss.c 6.1 KB

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