cryptomgr.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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[CRYPTOA_MAX];
  25. struct {
  26. struct rtattr attr;
  27. struct crypto_attr_type data;
  28. } type;
  29. struct {
  30. struct rtattr attr;
  31. struct crypto_attr_alg data;
  32. } alg;
  33. struct {
  34. char name[CRYPTO_MAX_ALG_NAME];
  35. } larval;
  36. char template[CRYPTO_MAX_ALG_NAME];
  37. };
  38. static int cryptomgr_probe(void *data)
  39. {
  40. struct cryptomgr_param *param = data;
  41. struct crypto_template *tmpl;
  42. struct crypto_instance *inst;
  43. int err;
  44. tmpl = crypto_lookup_template(param->template);
  45. if (!tmpl)
  46. goto err;
  47. do {
  48. inst = tmpl->alloc(param->tb);
  49. if (IS_ERR(inst))
  50. err = PTR_ERR(inst);
  51. else if ((err = crypto_register_instance(tmpl, inst)))
  52. tmpl->free(inst);
  53. } while (err == -EAGAIN && !signal_pending(current));
  54. crypto_tmpl_put(tmpl);
  55. if (err)
  56. goto err;
  57. out:
  58. kfree(param);
  59. module_put_and_exit(0);
  60. err:
  61. crypto_larval_error(param->larval.name, param->type.data.type,
  62. param->type.data.mask);
  63. goto out;
  64. }
  65. static int cryptomgr_schedule_probe(struct crypto_larval *larval)
  66. {
  67. struct task_struct *thread;
  68. struct cryptomgr_param *param;
  69. const char *name = larval->alg.cra_name;
  70. const char *p;
  71. unsigned int len;
  72. if (!try_module_get(THIS_MODULE))
  73. goto err;
  74. param = kzalloc(sizeof(*param), GFP_KERNEL);
  75. if (!param)
  76. goto err_put_module;
  77. for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
  78. ;
  79. len = p - name;
  80. if (!len || *p != '(')
  81. goto err_free_param;
  82. memcpy(param->template, name, len);
  83. name = p + 1;
  84. len = 0;
  85. for (p = name; *p; p++) {
  86. for (; isalnum(*p) || *p == '-' || *p == '_' || *p == '('; p++)
  87. ;
  88. if (*p != ')')
  89. goto err_free_param;
  90. len = p - name;
  91. }
  92. if (!len || name[len + 1])
  93. goto err_free_param;
  94. param->type.attr.rta_len = sizeof(param->type);
  95. param->type.attr.rta_type = CRYPTOA_TYPE;
  96. param->type.data.type = larval->alg.cra_flags;
  97. param->type.data.mask = larval->mask;
  98. param->tb[CRYPTOA_TYPE - 1] = &param->type.attr;
  99. param->alg.attr.rta_len = sizeof(param->alg);
  100. param->alg.attr.rta_type = CRYPTOA_ALG;
  101. memcpy(param->alg.data.name, name, len);
  102. param->tb[CRYPTOA_ALG - 1] = &param->alg.attr;
  103. memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
  104. thread = kthread_run(cryptomgr_probe, param, "cryptomgr");
  105. if (IS_ERR(thread))
  106. goto err_free_param;
  107. return NOTIFY_STOP;
  108. err_free_param:
  109. kfree(param);
  110. err_put_module:
  111. module_put(THIS_MODULE);
  112. err:
  113. return NOTIFY_OK;
  114. }
  115. static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
  116. void *data)
  117. {
  118. switch (msg) {
  119. case CRYPTO_MSG_ALG_REQUEST:
  120. return cryptomgr_schedule_probe(data);
  121. }
  122. return NOTIFY_DONE;
  123. }
  124. static struct notifier_block cryptomgr_notifier = {
  125. .notifier_call = cryptomgr_notify,
  126. };
  127. static int __init cryptomgr_init(void)
  128. {
  129. return crypto_register_notifier(&cryptomgr_notifier);
  130. }
  131. static void __exit cryptomgr_exit(void)
  132. {
  133. int err = crypto_unregister_notifier(&cryptomgr_notifier);
  134. BUG_ON(err);
  135. }
  136. module_init(cryptomgr_init);
  137. module_exit(cryptomgr_exit);
  138. MODULE_LICENSE("GPL");
  139. MODULE_DESCRIPTION("Crypto Algorithm Manager");