algboss.c 6.6 KB

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