algapi.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Cryptographic API for algorithms (i.e., low-level API).
  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/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/string.h>
  17. #include "internal.h"
  18. static inline int crypto_set_driver_name(struct crypto_alg *alg)
  19. {
  20. static const char suffix[] = "-generic";
  21. char *driver_name = alg->cra_driver_name;
  22. int len;
  23. if (*driver_name)
  24. return 0;
  25. len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  26. if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
  27. return -ENAMETOOLONG;
  28. memcpy(driver_name + len, suffix, sizeof(suffix));
  29. return 0;
  30. }
  31. int crypto_register_alg(struct crypto_alg *alg)
  32. {
  33. int ret;
  34. struct crypto_alg *q;
  35. if (alg->cra_alignmask & (alg->cra_alignmask + 1))
  36. return -EINVAL;
  37. if (alg->cra_alignmask & alg->cra_blocksize)
  38. return -EINVAL;
  39. if (alg->cra_blocksize > PAGE_SIZE / 8)
  40. return -EINVAL;
  41. if (alg->cra_priority < 0)
  42. return -EINVAL;
  43. ret = crypto_set_driver_name(alg);
  44. if (unlikely(ret))
  45. return ret;
  46. down_write(&crypto_alg_sem);
  47. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  48. if (q == alg) {
  49. ret = -EEXIST;
  50. goto out;
  51. }
  52. }
  53. list_add(&alg->cra_list, &crypto_alg_list);
  54. atomic_set(&alg->cra_refcnt, 1);
  55. out:
  56. up_write(&crypto_alg_sem);
  57. return ret;
  58. }
  59. EXPORT_SYMBOL_GPL(crypto_register_alg);
  60. int crypto_unregister_alg(struct crypto_alg *alg)
  61. {
  62. int ret = -ENOENT;
  63. struct crypto_alg *q;
  64. down_write(&crypto_alg_sem);
  65. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  66. if (alg == q) {
  67. list_del(&alg->cra_list);
  68. ret = 0;
  69. goto out;
  70. }
  71. }
  72. out:
  73. up_write(&crypto_alg_sem);
  74. if (ret)
  75. return ret;
  76. BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
  77. if (alg->cra_destroy)
  78. alg->cra_destroy(alg);
  79. return 0;
  80. }
  81. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  82. static int __init crypto_algapi_init(void)
  83. {
  84. crypto_init_proc();
  85. return 0;
  86. }
  87. static void __exit crypto_algapi_exit(void)
  88. {
  89. crypto_exit_proc();
  90. }
  91. module_init(crypto_algapi_init);
  92. module_exit(crypto_algapi_exit);
  93. MODULE_LICENSE("GPL");
  94. MODULE_DESCRIPTION("Cryptographic algorithms API");