cryptomgr.c 2.9 KB

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