ocfnull.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * An OCF module for determining the cost of crypto versus the cost of
  3. * IPSec processing outside of OCF. This modules gives us the effect of
  4. * zero cost encryption, of course you will need to run it at both ends
  5. * since it does no crypto at all.
  6. *
  7. * Written by David McCullough <david_mccullough@mcafee.com>
  8. * Copyright (C) 2006-2010 David McCullough
  9. *
  10. * LICENSE TERMS
  11. *
  12. * The free distribution and use of this software in both source and binary
  13. * form is allowed (with or without changes) provided that:
  14. *
  15. * 1. distributions of this source code include the above copyright
  16. * notice, this list of conditions and the following disclaimer;
  17. *
  18. * 2. distributions in binary form include the above copyright
  19. * notice, this list of conditions and the following disclaimer
  20. * in the documentation and/or other associated materials;
  21. *
  22. * 3. the copyright holder's name is not used to endorse products
  23. * built using this software without specific written permission.
  24. *
  25. * ALTERNATIVELY, provided that this notice is retained in full, this product
  26. * may be distributed under the terms of the GNU General Public License (GPL),
  27. * in which case the provisions of the GPL apply INSTEAD OF those given above.
  28. *
  29. * DISCLAIMER
  30. *
  31. * This software is provided 'as is' with no explicit or implied warranties
  32. * in respect of its properties, including, but not limited to, correctness
  33. * and/or fitness for purpose.
  34. */
  35. #include <linux/version.h>
  36. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
  37. #include <linux/config.h>
  38. #endif
  39. #include <linux/module.h>
  40. #include <linux/init.h>
  41. #include <linux/list.h>
  42. #include <linux/slab.h>
  43. #include <linux/sched.h>
  44. #include <linux/wait.h>
  45. #include <linux/crypto.h>
  46. #include <linux/interrupt.h>
  47. #include <cryptodev.h>
  48. #include <uio.h>
  49. static int32_t null_id = -1;
  50. static u_int32_t null_sesnum = 0;
  51. static int null_process(device_t, struct cryptop *, int);
  52. static int null_newsession(device_t, u_int32_t *, struct cryptoini *);
  53. static int null_freesession(device_t, u_int64_t);
  54. #define debug ocfnull_debug
  55. int ocfnull_debug = 0;
  56. module_param(ocfnull_debug, int, 0644);
  57. MODULE_PARM_DESC(ocfnull_debug, "Enable debug");
  58. /*
  59. * dummy device structure
  60. */
  61. static struct {
  62. softc_device_decl sc_dev;
  63. } nulldev;
  64. static device_method_t null_methods = {
  65. /* crypto device methods */
  66. DEVMETHOD(cryptodev_newsession, null_newsession),
  67. DEVMETHOD(cryptodev_freesession,null_freesession),
  68. DEVMETHOD(cryptodev_process, null_process),
  69. };
  70. /*
  71. * Generate a new software session.
  72. */
  73. static int
  74. null_newsession(device_t arg, u_int32_t *sid, struct cryptoini *cri)
  75. {
  76. dprintk("%s()\n", __FUNCTION__);
  77. if (sid == NULL || cri == NULL) {
  78. dprintk("%s,%d - EINVAL\n", __FILE__, __LINE__);
  79. return EINVAL;
  80. }
  81. if (null_sesnum == 0)
  82. null_sesnum++;
  83. *sid = null_sesnum++;
  84. return 0;
  85. }
  86. /*
  87. * Free a session.
  88. */
  89. static int
  90. null_freesession(device_t arg, u_int64_t tid)
  91. {
  92. u_int32_t sid = CRYPTO_SESID2LID(tid);
  93. dprintk("%s()\n", __FUNCTION__);
  94. if (sid > null_sesnum) {
  95. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  96. return EINVAL;
  97. }
  98. /* Silently accept and return */
  99. if (sid == 0)
  100. return 0;
  101. return 0;
  102. }
  103. /*
  104. * Process a request.
  105. */
  106. static int
  107. null_process(device_t arg, struct cryptop *crp, int hint)
  108. {
  109. unsigned int lid;
  110. dprintk("%s()\n", __FUNCTION__);
  111. /* Sanity check */
  112. if (crp == NULL) {
  113. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  114. return EINVAL;
  115. }
  116. crp->crp_etype = 0;
  117. if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
  118. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  119. crp->crp_etype = EINVAL;
  120. goto done;
  121. }
  122. /*
  123. * find the session we are using
  124. */
  125. lid = crp->crp_sid & 0xffffffff;
  126. if (lid >= null_sesnum || lid == 0) {
  127. crp->crp_etype = ENOENT;
  128. dprintk("%s,%d: ENOENT\n", __FILE__, __LINE__);
  129. goto done;
  130. }
  131. done:
  132. crypto_done(crp);
  133. return 0;
  134. }
  135. /*
  136. * our driver startup and shutdown routines
  137. */
  138. static int
  139. null_init(void)
  140. {
  141. dprintk("%s(%p)\n", __FUNCTION__, null_init);
  142. memset(&nulldev, 0, sizeof(nulldev));
  143. softc_device_init(&nulldev, "ocfnull", 0, null_methods);
  144. null_id = crypto_get_driverid(softc_get_device(&nulldev),
  145. CRYPTOCAP_F_HARDWARE);
  146. if (null_id < 0)
  147. panic("ocfnull: crypto device cannot initialize!");
  148. #define REGISTER(alg) \
  149. crypto_register(null_id,alg,0,0)
  150. REGISTER(CRYPTO_DES_CBC);
  151. REGISTER(CRYPTO_3DES_CBC);
  152. REGISTER(CRYPTO_RIJNDAEL128_CBC);
  153. REGISTER(CRYPTO_MD5);
  154. REGISTER(CRYPTO_SHA1);
  155. REGISTER(CRYPTO_MD5_HMAC);
  156. REGISTER(CRYPTO_SHA1_HMAC);
  157. #undef REGISTER
  158. return 0;
  159. }
  160. static void
  161. null_exit(void)
  162. {
  163. dprintk("%s()\n", __FUNCTION__);
  164. crypto_unregister_all(null_id);
  165. null_id = -1;
  166. }
  167. module_init(null_init);
  168. module_exit(null_exit);
  169. MODULE_LICENSE("Dual BSD/GPL");
  170. MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
  171. MODULE_DESCRIPTION("ocfnull - claims a lot but does nothing");