crypto_null.c 5.3 KB

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