cryptomgr.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/module.h>
  17. #include <linux/notifier.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/sched.h>
  20. #include <linux/string.h>
  21. #include <linux/workqueue.h>
  22. #include "internal.h"
  23. struct cryptomgr_param {
  24. struct work_struct work;
  25. struct {
  26. struct rtattr attr;
  27. struct crypto_attr_alg data;
  28. } alg;
  29. struct {
  30. u32 type;
  31. u32 mask;
  32. char name[CRYPTO_MAX_ALG_NAME];
  33. } larval;
  34. char template[CRYPTO_MAX_ALG_NAME];
  35. };
  36. static void cryptomgr_probe(struct work_struct *work)
  37. {
  38. struct cryptomgr_param *param =
  39. container_of(work, struct cryptomgr_param, work);
  40. struct crypto_template *tmpl;
  41. struct crypto_instance *inst;
  42. int err;
  43. tmpl = crypto_lookup_template(param->template);
  44. if (!tmpl)
  45. goto err;
  46. do {
  47. inst = tmpl->alloc(&param->alg, sizeof(param->alg));
  48. if (IS_ERR(inst))
  49. err = PTR_ERR(inst);
  50. else if ((err = crypto_register_instance(tmpl, inst)))
  51. tmpl->free(inst);
  52. } while (err == -EAGAIN && !signal_pending(current));
  53. crypto_tmpl_put(tmpl);
  54. if (err)
  55. goto err;
  56. out:
  57. kfree(param);
  58. return;
  59. err:
  60. crypto_larval_error(param->larval.name, param->larval.type,
  61. param->larval.mask);
  62. goto out;
  63. }
  64. static int cryptomgr_schedule_probe(struct crypto_larval *larval)
  65. {
  66. struct cryptomgr_param *param;
  67. const char *name = larval->alg.cra_name;
  68. const char *p;
  69. unsigned int len;
  70. param = kmalloc(sizeof(*param), GFP_KERNEL);
  71. if (!param)
  72. goto err;
  73. for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
  74. ;
  75. len = p - name;
  76. if (!len || *p != '(')
  77. goto err_free_param;
  78. memcpy(param->template, name, len);
  79. param->template[len] = 0;
  80. name = p + 1;
  81. for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
  82. ;
  83. len = p - name;
  84. if (!len || *p != ')' || p[1])
  85. goto err_free_param;
  86. param->alg.attr.rta_len = sizeof(param->alg);
  87. param->alg.attr.rta_type = CRYPTOA_ALG;
  88. memcpy(param->alg.data.name, name, len);
  89. param->alg.data.name[len] = 0;
  90. memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
  91. param->larval.type = larval->alg.cra_flags;
  92. param->larval.mask = larval->mask;
  93. INIT_WORK(&param->work, cryptomgr_probe);
  94. schedule_work(&param->work);
  95. return NOTIFY_STOP;
  96. err_free_param:
  97. kfree(param);
  98. err:
  99. return NOTIFY_OK;
  100. }
  101. static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
  102. void *data)
  103. {
  104. switch (msg) {
  105. case CRYPTO_MSG_ALG_REQUEST:
  106. return cryptomgr_schedule_probe(data);
  107. }
  108. return NOTIFY_DONE;
  109. }
  110. static struct notifier_block cryptomgr_notifier = {
  111. .notifier_call = cryptomgr_notify,
  112. };
  113. static int __init cryptomgr_init(void)
  114. {
  115. return crypto_register_notifier(&cryptomgr_notifier);
  116. }
  117. static void __exit cryptomgr_exit(void)
  118. {
  119. int err = crypto_unregister_notifier(&cryptomgr_notifier);
  120. BUG_ON(err);
  121. }
  122. module_init(cryptomgr_init);
  123. module_exit(cryptomgr_exit);
  124. MODULE_LICENSE("GPL");
  125. MODULE_DESCRIPTION("Crypto Algorithm Manager");