crypto_null.c 4.3 KB

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