cryptomgr.c 3.2 KB

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