crypto_null.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/internal/hash.h>
  20. #include <crypto/internal/skcipher.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/mm.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. #define NULL_IV_SIZE 0
  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 crypto_alg compress_null = {
  83. .cra_name = "compress_null",
  84. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  85. .cra_blocksize = NULL_BLOCK_SIZE,
  86. .cra_ctxsize = 0,
  87. .cra_module = THIS_MODULE,
  88. .cra_list = LIST_HEAD_INIT(compress_null.cra_list),
  89. .cra_u = { .compress = {
  90. .coa_compress = null_compress,
  91. .coa_decompress = null_compress } }
  92. };
  93. static struct shash_alg digest_null = {
  94. .digestsize = NULL_DIGEST_SIZE,
  95. .setkey = null_hash_setkey,
  96. .init = null_init,
  97. .update = null_update,
  98. .finup = null_digest,
  99. .digest = null_digest,
  100. .final = null_final,
  101. .base = {
  102. .cra_name = "digest_null",
  103. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  104. .cra_blocksize = NULL_BLOCK_SIZE,
  105. .cra_module = THIS_MODULE,
  106. }
  107. };
  108. static struct crypto_alg cipher_null = {
  109. .cra_name = "cipher_null",
  110. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  111. .cra_blocksize = NULL_BLOCK_SIZE,
  112. .cra_ctxsize = 0,
  113. .cra_module = THIS_MODULE,
  114. .cra_list = LIST_HEAD_INIT(cipher_null.cra_list),
  115. .cra_u = { .cipher = {
  116. .cia_min_keysize = NULL_KEY_SIZE,
  117. .cia_max_keysize = NULL_KEY_SIZE,
  118. .cia_setkey = null_setkey,
  119. .cia_encrypt = null_crypt,
  120. .cia_decrypt = null_crypt } }
  121. };
  122. static struct crypto_alg skcipher_null = {
  123. .cra_name = "ecb(cipher_null)",
  124. .cra_driver_name = "ecb-cipher_null",
  125. .cra_priority = 100,
  126. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  127. .cra_blocksize = NULL_BLOCK_SIZE,
  128. .cra_type = &crypto_blkcipher_type,
  129. .cra_ctxsize = 0,
  130. .cra_module = THIS_MODULE,
  131. .cra_list = LIST_HEAD_INIT(skcipher_null.cra_list),
  132. .cra_u = { .blkcipher = {
  133. .min_keysize = NULL_KEY_SIZE,
  134. .max_keysize = NULL_KEY_SIZE,
  135. .ivsize = NULL_IV_SIZE,
  136. .setkey = null_setkey,
  137. .encrypt = skcipher_null_crypt,
  138. .decrypt = skcipher_null_crypt } }
  139. };
  140. MODULE_ALIAS("compress_null");
  141. MODULE_ALIAS("digest_null");
  142. MODULE_ALIAS("cipher_null");
  143. static int __init crypto_null_mod_init(void)
  144. {
  145. int ret = 0;
  146. ret = crypto_register_alg(&cipher_null);
  147. if (ret < 0)
  148. goto out;
  149. ret = crypto_register_alg(&skcipher_null);
  150. if (ret < 0)
  151. goto out_unregister_cipher;
  152. ret = crypto_register_shash(&digest_null);
  153. if (ret < 0)
  154. goto out_unregister_skcipher;
  155. ret = crypto_register_alg(&compress_null);
  156. if (ret < 0)
  157. goto out_unregister_digest;
  158. out:
  159. return ret;
  160. out_unregister_digest:
  161. crypto_unregister_shash(&digest_null);
  162. out_unregister_skcipher:
  163. crypto_unregister_alg(&skcipher_null);
  164. out_unregister_cipher:
  165. crypto_unregister_alg(&cipher_null);
  166. goto out;
  167. }
  168. static void __exit crypto_null_mod_fini(void)
  169. {
  170. crypto_unregister_alg(&compress_null);
  171. crypto_unregister_shash(&digest_null);
  172. crypto_unregister_alg(&skcipher_null);
  173. crypto_unregister_alg(&cipher_null);
  174. }
  175. module_init(crypto_null_mod_init);
  176. module_exit(crypto_null_mod_fini);
  177. MODULE_LICENSE("GPL");
  178. MODULE_DESCRIPTION("Null Cryptographic Algorithms");