cryptomgr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <linux/crypto.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. };
  43. static int cryptomgr_probe(void *data)
  44. {
  45. struct cryptomgr_param *param = data;
  46. struct crypto_template *tmpl;
  47. struct crypto_instance *inst;
  48. int err;
  49. tmpl = crypto_lookup_template(param->template);
  50. if (!tmpl)
  51. goto err;
  52. do {
  53. inst = tmpl->alloc(param->tb);
  54. if (IS_ERR(inst))
  55. err = PTR_ERR(inst);
  56. else if ((err = crypto_register_instance(tmpl, inst)))
  57. tmpl->free(inst);
  58. } while (err == -EAGAIN && !signal_pending(current));
  59. crypto_tmpl_put(tmpl);
  60. if (err)
  61. goto err;
  62. out:
  63. kfree(param);
  64. module_put_and_exit(0);
  65. err:
  66. crypto_larval_error(param->larval, param->type.data.type,
  67. param->type.data.mask);
  68. goto out;
  69. }
  70. static int cryptomgr_schedule_probe(struct crypto_larval *larval)
  71. {
  72. struct task_struct *thread;
  73. struct cryptomgr_param *param;
  74. const char *name = larval->alg.cra_name;
  75. const char *p;
  76. unsigned int len;
  77. int i;
  78. if (!try_module_get(THIS_MODULE))
  79. goto err;
  80. param = kzalloc(sizeof(*param), GFP_KERNEL);
  81. if (!param)
  82. goto err_put_module;
  83. for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
  84. ;
  85. len = p - name;
  86. if (!len || *p != '(')
  87. goto err_free_param;
  88. memcpy(param->template, name, len);
  89. i = 0;
  90. for (;;) {
  91. int notnum = 0;
  92. name = ++p;
  93. len = 0;
  94. for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
  95. notnum |= !isdigit(*p);
  96. if (*p == '(') {
  97. int recursion = 0;
  98. for (;;) {
  99. if (!*++p)
  100. goto err_free_param;
  101. if (*p == '(')
  102. recursion++;
  103. else if (*p == ')' && !recursion--)
  104. break;
  105. }
  106. notnum = 1;
  107. p++;
  108. }
  109. len = p - name;
  110. if (!len)
  111. goto err_free_param;
  112. if (notnum) {
  113. param->attrs[i].alg.attr.rta_len =
  114. sizeof(param->attrs[i].alg);
  115. param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
  116. memcpy(param->attrs[i].alg.data.name, name, len);
  117. } else {
  118. param->attrs[i].nu32.attr.rta_len =
  119. sizeof(param->attrs[i].nu32);
  120. param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
  121. param->attrs[i].nu32.data.num =
  122. simple_strtol(name, NULL, 0);
  123. }
  124. param->tb[i + 1] = &param->attrs[i].attr;
  125. i++;
  126. if (i >= CRYPTO_MAX_ATTRS)
  127. goto err_free_param;
  128. if (*p == ')')
  129. break;
  130. if (*p != ',')
  131. goto err_free_param;
  132. }
  133. if (!i)
  134. goto err_free_param;
  135. param->tb[i + 1] = NULL;
  136. param->type.attr.rta_len = sizeof(param->type);
  137. param->type.attr.rta_type = CRYPTOA_TYPE;
  138. param->type.data.type = larval->alg.cra_flags;
  139. param->type.data.mask = larval->mask;
  140. param->tb[0] = &param->type.attr;
  141. memcpy(param->larval, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
  142. thread = kthread_run(cryptomgr_probe, param, "cryptomgr");
  143. if (IS_ERR(thread))
  144. goto err_free_param;
  145. return NOTIFY_STOP;
  146. err_free_param:
  147. kfree(param);
  148. err_put_module:
  149. module_put(THIS_MODULE);
  150. err:
  151. return NOTIFY_OK;
  152. }
  153. static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
  154. void *data)
  155. {
  156. switch (msg) {
  157. case CRYPTO_MSG_ALG_REQUEST:
  158. return cryptomgr_schedule_probe(data);
  159. }
  160. return NOTIFY_DONE;
  161. }
  162. static struct notifier_block cryptomgr_notifier = {
  163. .notifier_call = cryptomgr_notify,
  164. };
  165. static int __init cryptomgr_init(void)
  166. {
  167. return crypto_register_notifier(&cryptomgr_notifier);
  168. }
  169. static void __exit cryptomgr_exit(void)
  170. {
  171. int err = crypto_unregister_notifier(&cryptomgr_notifier);
  172. BUG_ON(err);
  173. }
  174. module_init(cryptomgr_init);
  175. module_exit(cryptomgr_exit);
  176. MODULE_LICENSE("GPL");
  177. MODULE_DESCRIPTION("Crypto Algorithm Manager");