crypto_null.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API.
  4. *
  5. * Null algorithms, aka Much Ado About Nothing.
  6. *
  7. * These are needed for IPsec, and may be useful in general for
  8. * testing & debugging.
  9. *
  10. * The null cipher is compliant with RFC2410.
  11. *
  12. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  13. */
  14. #include <crypto/null.h>
  15. #include <crypto/internal/hash.h>
  16. #include <crypto/internal/skcipher.h>
  17. #include <crypto/scatterwalk.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/string.h>
  21. static int null_init(struct shash_desc *desc)
  22. {
  23. return 0;
  24. }
  25. static int null_update(struct shash_desc *desc, const u8 *data,
  26. unsigned int len)
  27. {
  28. return 0;
  29. }
  30. static int null_final(struct shash_desc *desc, u8 *out)
  31. {
  32. return 0;
  33. }
  34. static int null_digest(struct shash_desc *desc, const u8 *data,
  35. unsigned int len, u8 *out)
  36. {
  37. return 0;
  38. }
  39. static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key,
  40. unsigned int keylen)
  41. { return 0; }
  42. static int null_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
  43. unsigned int keylen)
  44. { return 0; }
  45. static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
  46. unsigned int keylen)
  47. { return 0; }
  48. static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  49. {
  50. memcpy(dst, src, NULL_BLOCK_SIZE);
  51. }
  52. static int null_skcipher_crypt(struct skcipher_request *req)
  53. {
  54. if (req->src != req->dst)
  55. memcpy_sglist(req->dst, req->src, req->cryptlen);
  56. return 0;
  57. }
  58. static struct shash_alg digest_null = {
  59. .digestsize = NULL_DIGEST_SIZE,
  60. .setkey = null_hash_setkey,
  61. .init = null_init,
  62. .update = null_update,
  63. .finup = null_digest,
  64. .digest = null_digest,
  65. .final = null_final,
  66. .base = {
  67. .cra_name = "digest_null",
  68. .cra_driver_name = "digest_null-generic",
  69. .cra_blocksize = NULL_BLOCK_SIZE,
  70. .cra_module = THIS_MODULE,
  71. }
  72. };
  73. static struct skcipher_alg skcipher_null = {
  74. .base.cra_name = "ecb(cipher_null)",
  75. .base.cra_driver_name = "ecb-cipher_null",
  76. .base.cra_priority = 100,
  77. .base.cra_blocksize = NULL_BLOCK_SIZE,
  78. .base.cra_ctxsize = 0,
  79. .base.cra_module = THIS_MODULE,
  80. .min_keysize = NULL_KEY_SIZE,
  81. .max_keysize = NULL_KEY_SIZE,
  82. .ivsize = NULL_IV_SIZE,
  83. .setkey = null_skcipher_setkey,
  84. .encrypt = null_skcipher_crypt,
  85. .decrypt = null_skcipher_crypt,
  86. };
  87. static struct crypto_alg cipher_null = {
  88. .cra_name = "cipher_null",
  89. .cra_driver_name = "cipher_null-generic",
  90. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  91. .cra_blocksize = NULL_BLOCK_SIZE,
  92. .cra_ctxsize = 0,
  93. .cra_module = THIS_MODULE,
  94. .cra_u = { .cipher = {
  95. .cia_min_keysize = NULL_KEY_SIZE,
  96. .cia_max_keysize = NULL_KEY_SIZE,
  97. .cia_setkey = null_setkey,
  98. .cia_encrypt = null_crypt,
  99. .cia_decrypt = null_crypt } }
  100. };
  101. MODULE_ALIAS_CRYPTO("digest_null");
  102. MODULE_ALIAS_CRYPTO("cipher_null");
  103. static int __init crypto_null_mod_init(void)
  104. {
  105. int ret = 0;
  106. ret = crypto_register_alg(&cipher_null);
  107. if (ret < 0)
  108. goto out;
  109. ret = crypto_register_shash(&digest_null);
  110. if (ret < 0)
  111. goto out_unregister_algs;
  112. ret = crypto_register_skcipher(&skcipher_null);
  113. if (ret < 0)
  114. goto out_unregister_shash;
  115. return 0;
  116. out_unregister_shash:
  117. crypto_unregister_shash(&digest_null);
  118. out_unregister_algs:
  119. crypto_unregister_alg(&cipher_null);
  120. out:
  121. return ret;
  122. }
  123. static void __exit crypto_null_mod_fini(void)
  124. {
  125. crypto_unregister_alg(&cipher_null);
  126. crypto_unregister_shash(&digest_null);
  127. crypto_unregister_skcipher(&skcipher_null);
  128. }
  129. module_init(crypto_null_mod_init);
  130. module_exit(crypto_null_mod_fini);
  131. MODULE_LICENSE("GPL");
  132. MODULE_DESCRIPTION("Null Cryptographic Algorithms");