crypto_null.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Null algorithms, aka Much Ado About Nothing.
  5. *
  6. * These are needed for IPsec, and may be useful in general for
  7. * testing & debugging.
  8. *
  9. * The null cipher is compliant with RFC2410.
  10. *
  11. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/mm.h>
  22. #include <asm/scatterlist.h>
  23. #include <linux/crypto.h>
  24. #include <linux/string.h>
  25. #define NULL_KEY_SIZE 0
  26. #define NULL_BLOCK_SIZE 1
  27. #define NULL_DIGEST_SIZE 0
  28. static int null_compress(struct crypto_tfm *tfm, const u8 *src,
  29. unsigned int slen, u8 *dst, unsigned int *dlen)
  30. {
  31. if (slen > *dlen)
  32. return -EINVAL;
  33. memcpy(dst, src, slen);
  34. *dlen = slen;
  35. return 0;
  36. }
  37. static void null_init(struct crypto_tfm *tfm)
  38. { }
  39. static void null_update(struct crypto_tfm *tfm, const u8 *data,
  40. unsigned int len)
  41. { }
  42. static void null_final(struct crypto_tfm *tfm, u8 *out)
  43. { }
  44. static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
  45. unsigned int keylen)
  46. { return 0; }
  47. static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  48. {
  49. memcpy(dst, src, NULL_BLOCK_SIZE);
  50. }
  51. static struct crypto_alg compress_null = {
  52. .cra_name = "compress_null",
  53. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  54. .cra_blocksize = NULL_BLOCK_SIZE,
  55. .cra_ctxsize = 0,
  56. .cra_module = THIS_MODULE,
  57. .cra_list = LIST_HEAD_INIT(compress_null.cra_list),
  58. .cra_u = { .compress = {
  59. .coa_compress = null_compress,
  60. .coa_decompress = null_compress } }
  61. };
  62. static struct crypto_alg digest_null = {
  63. .cra_name = "digest_null",
  64. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  65. .cra_blocksize = NULL_BLOCK_SIZE,
  66. .cra_ctxsize = 0,
  67. .cra_module = THIS_MODULE,
  68. .cra_list = LIST_HEAD_INIT(digest_null.cra_list),
  69. .cra_u = { .digest = {
  70. .dia_digestsize = NULL_DIGEST_SIZE,
  71. .dia_init = null_init,
  72. .dia_update = null_update,
  73. .dia_final = null_final } }
  74. };
  75. static struct crypto_alg cipher_null = {
  76. .cra_name = "cipher_null",
  77. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  78. .cra_blocksize = NULL_BLOCK_SIZE,
  79. .cra_ctxsize = 0,
  80. .cra_module = THIS_MODULE,
  81. .cra_list = LIST_HEAD_INIT(cipher_null.cra_list),
  82. .cra_u = { .cipher = {
  83. .cia_min_keysize = NULL_KEY_SIZE,
  84. .cia_max_keysize = NULL_KEY_SIZE,
  85. .cia_setkey = null_setkey,
  86. .cia_encrypt = null_crypt,
  87. .cia_decrypt = null_crypt } }
  88. };
  89. MODULE_ALIAS("compress_null");
  90. MODULE_ALIAS("digest_null");
  91. MODULE_ALIAS("cipher_null");
  92. static int __init init(void)
  93. {
  94. int ret = 0;
  95. ret = crypto_register_alg(&cipher_null);
  96. if (ret < 0)
  97. goto out;
  98. ret = crypto_register_alg(&digest_null);
  99. if (ret < 0) {
  100. crypto_unregister_alg(&cipher_null);
  101. goto out;
  102. }
  103. ret = crypto_register_alg(&compress_null);
  104. if (ret < 0) {
  105. crypto_unregister_alg(&digest_null);
  106. crypto_unregister_alg(&cipher_null);
  107. goto out;
  108. }
  109. out:
  110. return ret;
  111. }
  112. static void __exit fini(void)
  113. {
  114. crypto_unregister_alg(&compress_null);
  115. crypto_unregister_alg(&digest_null);
  116. crypto_unregister_alg(&cipher_null);
  117. }
  118. module_init(init);
  119. module_exit(fini);
  120. MODULE_LICENSE("GPL");
  121. MODULE_DESCRIPTION("Null Cryptographic Algorithms");