crypto_null.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(void *ctx, const u8 *src, unsigned int slen,
  29. 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(void *ctx)
  38. { }
  39. static void null_update(void *ctx, const u8 *data, unsigned int len)
  40. { }
  41. static void null_final(void *ctx, u8 *out)
  42. { }
  43. static int null_setkey(void *ctx, const u8 *key,
  44. unsigned int keylen, u32 *flags)
  45. { return 0; }
  46. static void null_crypt(void *ctx, u8 *dst, const u8 *src)
  47. {
  48. memcpy(dst, src, NULL_BLOCK_SIZE);
  49. }
  50. static struct crypto_alg compress_null = {
  51. .cra_name = "compress_null",
  52. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  53. .cra_blocksize = NULL_BLOCK_SIZE,
  54. .cra_ctxsize = 0,
  55. .cra_module = THIS_MODULE,
  56. .cra_list = LIST_HEAD_INIT(compress_null.cra_list),
  57. .cra_u = { .compress = {
  58. .coa_compress = null_compress,
  59. .coa_decompress = null_compress } }
  60. };
  61. static struct crypto_alg digest_null = {
  62. .cra_name = "digest_null",
  63. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  64. .cra_blocksize = NULL_BLOCK_SIZE,
  65. .cra_ctxsize = 0,
  66. .cra_module = THIS_MODULE,
  67. .cra_list = LIST_HEAD_INIT(digest_null.cra_list),
  68. .cra_u = { .digest = {
  69. .dia_digestsize = NULL_DIGEST_SIZE,
  70. .dia_init = null_init,
  71. .dia_update = null_update,
  72. .dia_final = null_final } }
  73. };
  74. static struct crypto_alg cipher_null = {
  75. .cra_name = "cipher_null",
  76. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  77. .cra_blocksize = NULL_BLOCK_SIZE,
  78. .cra_ctxsize = 0,
  79. .cra_module = THIS_MODULE,
  80. .cra_list = LIST_HEAD_INIT(cipher_null.cra_list),
  81. .cra_u = { .cipher = {
  82. .cia_min_keysize = NULL_KEY_SIZE,
  83. .cia_max_keysize = NULL_KEY_SIZE,
  84. .cia_setkey = null_setkey,
  85. .cia_encrypt = null_crypt,
  86. .cia_decrypt = null_crypt } }
  87. };
  88. MODULE_ALIAS("compress_null");
  89. MODULE_ALIAS("digest_null");
  90. MODULE_ALIAS("cipher_null");
  91. static int __init init(void)
  92. {
  93. int ret = 0;
  94. ret = crypto_register_alg(&cipher_null);
  95. if (ret < 0)
  96. goto out;
  97. ret = crypto_register_alg(&digest_null);
  98. if (ret < 0) {
  99. crypto_unregister_alg(&cipher_null);
  100. goto out;
  101. }
  102. ret = crypto_register_alg(&compress_null);
  103. if (ret < 0) {
  104. crypto_unregister_alg(&digest_null);
  105. crypto_unregister_alg(&cipher_null);
  106. goto out;
  107. }
  108. out:
  109. return ret;
  110. }
  111. static void __exit fini(void)
  112. {
  113. crypto_unregister_alg(&compress_null);
  114. crypto_unregister_alg(&digest_null);
  115. crypto_unregister_alg(&cipher_null);
  116. }
  117. module_init(init);
  118. module_exit(fini);
  119. MODULE_LICENSE("GPL");
  120. MODULE_DESCRIPTION("Null Cryptographic Algorithms");