crypto_null.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/skcipher.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/mm.h>
  23. #include <linux/string.h>
  24. #define NULL_KEY_SIZE 0
  25. #define NULL_BLOCK_SIZE 1
  26. #define NULL_DIGEST_SIZE 0
  27. #define NULL_IV_SIZE 0
  28. static int null_compress(struct crypto_tfm *tfm, const u8 *src,
  29. unsigned int slen, u8 *dst, unsigned int *dlen)
  30. {
  31. if (slen > *dlen)
  32. return -EINVAL;
  33. memcpy(dst, src, slen);
  34. *dlen = slen;
  35. return 0;
  36. }
  37. static void null_init(struct crypto_tfm *tfm)
  38. { }
  39. static void null_update(struct crypto_tfm *tfm, const u8 *data,
  40. unsigned int len)
  41. { }
  42. static void null_final(struct crypto_tfm *tfm, u8 *out)
  43. { }
  44. static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
  45. unsigned int keylen)
  46. { return 0; }
  47. static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  48. {
  49. memcpy(dst, src, NULL_BLOCK_SIZE);
  50. }
  51. static int skcipher_null_crypt(struct blkcipher_desc *desc,
  52. struct scatterlist *dst,
  53. struct scatterlist *src, unsigned int nbytes)
  54. {
  55. struct blkcipher_walk walk;
  56. int err;
  57. blkcipher_walk_init(&walk, dst, src, nbytes);
  58. err = blkcipher_walk_virt(desc, &walk);
  59. while (walk.nbytes) {
  60. if (walk.src.virt.addr != walk.dst.virt.addr)
  61. memcpy(walk.dst.virt.addr, walk.src.virt.addr,
  62. walk.nbytes);
  63. err = blkcipher_walk_done(desc, &walk, 0);
  64. }
  65. return err;
  66. }
  67. static struct crypto_alg compress_null = {
  68. .cra_name = "compress_null",
  69. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  70. .cra_blocksize = NULL_BLOCK_SIZE,
  71. .cra_ctxsize = 0,
  72. .cra_module = THIS_MODULE,
  73. .cra_list = LIST_HEAD_INIT(compress_null.cra_list),
  74. .cra_u = { .compress = {
  75. .coa_compress = null_compress,
  76. .coa_decompress = null_compress } }
  77. };
  78. static struct crypto_alg digest_null = {
  79. .cra_name = "digest_null",
  80. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  81. .cra_blocksize = NULL_BLOCK_SIZE,
  82. .cra_ctxsize = 0,
  83. .cra_module = THIS_MODULE,
  84. .cra_list = LIST_HEAD_INIT(digest_null.cra_list),
  85. .cra_u = { .digest = {
  86. .dia_digestsize = NULL_DIGEST_SIZE,
  87. .dia_setkey = null_setkey,
  88. .dia_init = null_init,
  89. .dia_update = null_update,
  90. .dia_final = null_final } }
  91. };
  92. static struct crypto_alg cipher_null = {
  93. .cra_name = "cipher_null",
  94. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  95. .cra_blocksize = NULL_BLOCK_SIZE,
  96. .cra_ctxsize = 0,
  97. .cra_module = THIS_MODULE,
  98. .cra_list = LIST_HEAD_INIT(cipher_null.cra_list),
  99. .cra_u = { .cipher = {
  100. .cia_min_keysize = NULL_KEY_SIZE,
  101. .cia_max_keysize = NULL_KEY_SIZE,
  102. .cia_setkey = null_setkey,
  103. .cia_encrypt = null_crypt,
  104. .cia_decrypt = null_crypt } }
  105. };
  106. static struct crypto_alg skcipher_null = {
  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_list = LIST_HEAD_INIT(skcipher_null.cra_list),
  116. .cra_u = { .blkcipher = {
  117. .min_keysize = NULL_KEY_SIZE,
  118. .max_keysize = NULL_KEY_SIZE,
  119. .ivsize = NULL_IV_SIZE,
  120. .setkey = null_setkey,
  121. .encrypt = skcipher_null_crypt,
  122. .decrypt = skcipher_null_crypt } }
  123. };
  124. MODULE_ALIAS("compress_null");
  125. MODULE_ALIAS("digest_null");
  126. MODULE_ALIAS("cipher_null");
  127. static int __init crypto_null_mod_init(void)
  128. {
  129. int ret = 0;
  130. ret = crypto_register_alg(&cipher_null);
  131. if (ret < 0)
  132. goto out;
  133. ret = crypto_register_alg(&skcipher_null);
  134. if (ret < 0)
  135. goto out_unregister_cipher;
  136. ret = crypto_register_alg(&digest_null);
  137. if (ret < 0)
  138. goto out_unregister_skcipher;
  139. ret = crypto_register_alg(&compress_null);
  140. if (ret < 0)
  141. goto out_unregister_digest;
  142. out:
  143. return ret;
  144. out_unregister_digest:
  145. crypto_unregister_alg(&digest_null);
  146. out_unregister_skcipher:
  147. crypto_unregister_alg(&skcipher_null);
  148. out_unregister_cipher:
  149. crypto_unregister_alg(&cipher_null);
  150. goto out;
  151. }
  152. static void __exit crypto_null_mod_fini(void)
  153. {
  154. crypto_unregister_alg(&compress_null);
  155. crypto_unregister_alg(&digest_null);
  156. crypto_unregister_alg(&skcipher_null);
  157. crypto_unregister_alg(&cipher_null);
  158. }
  159. module_init(crypto_null_mod_init);
  160. module_exit(crypto_null_mod_fini);
  161. MODULE_LICENSE("GPL");
  162. MODULE_DESCRIPTION("Null Cryptographic Algorithms");