cryptomgr.c 3.5 KB

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