crypto_null.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <linux/string.h>
  21. static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
  22. static struct crypto_sync_skcipher *crypto_default_null_skcipher;
  23. static int crypto_default_null_skcipher_refcnt;
  24. static int null_compress(struct crypto_tfm *tfm, const u8 *src,
  25. unsigned int slen, u8 *dst, unsigned int *dlen)
  26. {
  27. if (slen > *dlen)
  28. return -EINVAL;
  29. memcpy(dst, src, slen);
  30. *dlen = slen;
  31. return 0;
  32. }
  33. static int null_init(struct shash_desc *desc)
  34. {
  35. return 0;
  36. }
  37. static int null_update(struct shash_desc *desc, const u8 *data,
  38. unsigned int len)
  39. {
  40. return 0;
  41. }
  42. static int null_final(struct shash_desc *desc, u8 *out)
  43. {
  44. return 0;
  45. }
  46. static int null_digest(struct shash_desc *desc, const u8 *data,
  47. unsigned int len, u8 *out)
  48. {
  49. return 0;
  50. }
  51. static int null_hash_setkey(struct crypto_shash *tfm, const u8 *key,
  52. unsigned int keylen)
  53. { return 0; }
  54. static int null_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
  55. unsigned int keylen)
  56. { return 0; }
  57. static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
  58. unsigned int keylen)
  59. { return 0; }
  60. static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  61. {
  62. memcpy(dst, src, NULL_BLOCK_SIZE);
  63. }
  64. static int null_skcipher_crypt(struct skcipher_request *req)
  65. {
  66. struct skcipher_walk walk;
  67. int err;
  68. err = skcipher_walk_virt(&walk, req, false);
  69. while (walk.nbytes) {
  70. if (walk.src.virt.addr != walk.dst.virt.addr)
  71. memcpy(walk.dst.virt.addr, walk.src.virt.addr,
  72. walk.nbytes);
  73. err = skcipher_walk_done(&walk, 0);
  74. }
  75. return err;
  76. }
  77. static struct shash_alg digest_null = {
  78. .digestsize = NULL_DIGEST_SIZE,
  79. .setkey = null_hash_setkey,
  80. .init = null_init,
  81. .update = null_update,
  82. .finup = null_digest,
  83. .digest = null_digest,
  84. .final = null_final,
  85. .base = {
  86. .cra_name = "digest_null",
  87. .cra_driver_name = "digest_null-generic",
  88. .cra_blocksize = NULL_BLOCK_SIZE,
  89. .cra_module = THIS_MODULE,
  90. }
  91. };
  92. static struct skcipher_alg skcipher_null = {
  93. .base.cra_name = "ecb(cipher_null)",
  94. .base.cra_driver_name = "ecb-cipher_null",
  95. .base.cra_priority = 100,
  96. .base.cra_blocksize = NULL_BLOCK_SIZE,
  97. .base.cra_ctxsize = 0,
  98. .base.cra_module = THIS_MODULE,
  99. .min_keysize = NULL_KEY_SIZE,
  100. .max_keysize = NULL_KEY_SIZE,
  101. .ivsize = NULL_IV_SIZE,
  102. .setkey = null_skcipher_setkey,
  103. .encrypt = null_skcipher_crypt,
  104. .decrypt = null_skcipher_crypt,
  105. };
  106. static struct crypto_alg null_algs[] = { {
  107. .cra_name = "cipher_null",
  108. .cra_driver_name = "cipher_null-generic",
  109. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  110. .cra_blocksize = NULL_BLOCK_SIZE,
  111. .cra_ctxsize = 0,
  112. .cra_module = THIS_MODULE,
  113. .cra_u = { .cipher = {
  114. .cia_min_keysize = NULL_KEY_SIZE,
  115. .cia_max_keysize = NULL_KEY_SIZE,
  116. .cia_setkey = null_setkey,
  117. .cia_encrypt = null_crypt,
  118. .cia_decrypt = null_crypt } }
  119. }, {
  120. .cra_name = "compress_null",
  121. .cra_driver_name = "compress_null-generic",
  122. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  123. .cra_blocksize = NULL_BLOCK_SIZE,
  124. .cra_ctxsize = 0,
  125. .cra_module = THIS_MODULE,
  126. .cra_u = { .compress = {
  127. .coa_compress = null_compress,
  128. .coa_decompress = null_compress } }
  129. } };
  130. MODULE_ALIAS_CRYPTO("compress_null");
  131. MODULE_ALIAS_CRYPTO("digest_null");
  132. MODULE_ALIAS_CRYPTO("cipher_null");
  133. struct crypto_sync_skcipher *crypto_get_default_null_skcipher(void)
  134. {
  135. struct crypto_sync_skcipher *tfm;
  136. mutex_lock(&crypto_default_null_skcipher_lock);
  137. tfm = crypto_default_null_skcipher;
  138. if (!tfm) {
  139. tfm = crypto_alloc_sync_skcipher("ecb(cipher_null)", 0, 0);
  140. if (IS_ERR(tfm))
  141. goto unlock;
  142. crypto_default_null_skcipher = tfm;
  143. }
  144. crypto_default_null_skcipher_refcnt++;
  145. unlock:
  146. mutex_unlock(&crypto_default_null_skcipher_lock);
  147. return tfm;
  148. }
  149. EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);
  150. void crypto_put_default_null_skcipher(void)
  151. {
  152. mutex_lock(&crypto_default_null_skcipher_lock);
  153. if (!--crypto_default_null_skcipher_refcnt) {
  154. crypto_free_sync_skcipher(crypto_default_null_skcipher);
  155. crypto_default_null_skcipher = NULL;
  156. }
  157. mutex_unlock(&crypto_default_null_skcipher_lock);
  158. }
  159. EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);
  160. static int __init crypto_null_mod_init(void)
  161. {
  162. int ret = 0;
  163. ret = crypto_register_algs(null_algs, ARRAY_SIZE(null_algs));
  164. if (ret < 0)
  165. goto out;
  166. ret = crypto_register_shash(&digest_null);
  167. if (ret < 0)
  168. goto out_unregister_algs;
  169. ret = crypto_register_skcipher(&skcipher_null);
  170. if (ret < 0)
  171. goto out_unregister_shash;
  172. return 0;
  173. out_unregister_shash:
  174. crypto_unregister_shash(&digest_null);
  175. out_unregister_algs:
  176. crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
  177. out:
  178. return ret;
  179. }
  180. static void __exit crypto_null_mod_fini(void)
  181. {
  182. crypto_unregister_algs(null_algs, ARRAY_SIZE(null_algs));
  183. crypto_unregister_shash(&digest_null);
  184. crypto_unregister_skcipher(&skcipher_null);
  185. }
  186. subsys_initcall(crypto_null_mod_init);
  187. module_exit(crypto_null_mod_fini);
  188. MODULE_LICENSE("GPL");
  189. MODULE_DESCRIPTION("Null Cryptographic Algorithms");