PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/crypto/algboss.c

https://gitlab.com/kush/linux
C | 309 lines | 227 code | 64 blank | 18 comment | 50 complexity | 97f066758cab85937ead3955278de09d MD5 | raw file
  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/signal.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. for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
  103. notnum |= !isdigit(*p);
  104. if (*p == '(') {
  105. int recursion = 0;
  106. for (;;) {
  107. if (!*++p)
  108. goto err_free_param;
  109. if (*p == '(')
  110. recursion++;
  111. else if (*p == ')' && !recursion--)
  112. break;
  113. }
  114. notnum = 1;
  115. p++;
  116. }
  117. len = p - name;
  118. if (!len)
  119. goto err_free_param;
  120. if (notnum) {
  121. param->attrs[i].alg.attr.rta_len =
  122. sizeof(param->attrs[i].alg);
  123. param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
  124. memcpy(param->attrs[i].alg.data.name, name, len);
  125. } else {
  126. param->attrs[i].nu32.attr.rta_len =
  127. sizeof(param->attrs[i].nu32);
  128. param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
  129. param->attrs[i].nu32.data.num =
  130. simple_strtol(name, NULL, 0);
  131. }
  132. param->tb[i + 1] = &param->attrs[i].attr;
  133. i++;
  134. if (i >= CRYPTO_MAX_ATTRS)
  135. goto err_free_param;
  136. if (*p == ')')
  137. break;
  138. if (*p != ',')
  139. goto err_free_param;
  140. }
  141. if (!i)
  142. goto err_free_param;
  143. param->tb[i + 1] = NULL;
  144. param->type.attr.rta_len = sizeof(param->type);
  145. param->type.attr.rta_type = CRYPTOA_TYPE;
  146. param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
  147. param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
  148. param->tb[0] = &param->type.attr;
  149. param->otype = larval->alg.cra_flags;
  150. param->omask = larval->mask;
  151. crypto_alg_get(&larval->alg);
  152. param->larval = larval;
  153. thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
  154. if (IS_ERR(thread))
  155. goto err_put_larval;
  156. wait_for_completion_interruptible(&larval->completion);
  157. return NOTIFY_STOP;
  158. err_put_larval:
  159. crypto_alg_put(&larval->alg);
  160. err_free_param:
  161. kfree(param);
  162. err_put_module:
  163. module_put(THIS_MODULE);
  164. err:
  165. return NOTIFY_OK;
  166. }
  167. static int cryptomgr_test(void *data)
  168. {
  169. struct crypto_test_param *param = data;
  170. u32 type = param->type;
  171. int err = 0;
  172. #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
  173. goto skiptest;
  174. #endif
  175. if (type & CRYPTO_ALG_TESTED)
  176. goto skiptest;
  177. err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
  178. skiptest:
  179. crypto_alg_tested(param->driver, err);
  180. kfree(param);
  181. module_put_and_exit(0);
  182. }
  183. static int cryptomgr_schedule_test(struct crypto_alg *alg)
  184. {
  185. struct task_struct *thread;
  186. struct crypto_test_param *param;
  187. u32 type;
  188. if (!try_module_get(THIS_MODULE))
  189. goto err;
  190. param = kzalloc(sizeof(*param), GFP_KERNEL);
  191. if (!param)
  192. goto err_put_module;
  193. memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
  194. memcpy(param->alg, alg->cra_name, sizeof(param->alg));
  195. type = alg->cra_flags;
  196. /* Do not test internal algorithms. */
  197. if (type & CRYPTO_ALG_INTERNAL)
  198. type |= CRYPTO_ALG_TESTED;
  199. param->type = type;
  200. thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
  201. if (IS_ERR(thread))
  202. goto err_free_param;
  203. return NOTIFY_STOP;
  204. err_free_param:
  205. kfree(param);
  206. err_put_module:
  207. module_put(THIS_MODULE);
  208. err:
  209. return NOTIFY_OK;
  210. }
  211. static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
  212. void *data)
  213. {
  214. switch (msg) {
  215. case CRYPTO_MSG_ALG_REQUEST:
  216. return cryptomgr_schedule_probe(data);
  217. case CRYPTO_MSG_ALG_REGISTER:
  218. return cryptomgr_schedule_test(data);
  219. case CRYPTO_MSG_ALG_LOADED:
  220. break;
  221. }
  222. return NOTIFY_DONE;
  223. }
  224. static struct notifier_block cryptomgr_notifier = {
  225. .notifier_call = cryptomgr_notify,
  226. };
  227. static int __init cryptomgr_init(void)
  228. {
  229. return crypto_register_notifier(&cryptomgr_notifier);
  230. }
  231. static void __exit cryptomgr_exit(void)
  232. {
  233. int err = crypto_unregister_notifier(&cryptomgr_notifier);
  234. BUG_ON(err);
  235. }
  236. /*
  237. * This is arch_initcall() so that the crypto self-tests are run on algorithms
  238. * registered early by subsys_initcall(). subsys_initcall() is needed for
  239. * generic implementations so that they're available for comparison tests when
  240. * other implementations are registered later by module_init().
  241. */
  242. arch_initcall(cryptomgr_init);
  243. module_exit(cryptomgr_exit);
  244. MODULE_LICENSE("GPL");
  245. MODULE_DESCRIPTION("Crypto Algorithm Manager");