ocfnull.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <[email protected]>
  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. #ifndef AUTOCONF_INCLUDED
  36. #include <linux/config.h>
  37. #endif
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/list.h>
  41. #include <linux/slab.h>
  42. #include <linux/sched.h>
  43. #include <linux/wait.h>
  44. #include <linux/crypto.h>
  45. #include <linux/interrupt.h>
  46. #include <cryptodev.h>
  47. #include <uio.h>
  48. static int32_t null_id = -1;
  49. static u_int32_t null_sesnum = 0;
  50. static int null_process(device_t, struct cryptop *, int);
  51. static int null_newsession(device_t, u_int32_t *, struct cryptoini *);
  52. static int null_freesession(device_t, u_int64_t);
  53. #define debug ocfnull_debug
  54. int ocfnull_debug = 0;
  55. module_param(ocfnull_debug, int, 0644);
  56. MODULE_PARM_DESC(ocfnull_debug, "Enable debug");
  57. /*
  58. * dummy device structure
  59. */
  60. static struct {
  61. softc_device_decl sc_dev;
  62. } nulldev;
  63. static device_method_t null_methods = {
  64. /* crypto device methods */
  65. DEVMETHOD(cryptodev_newsession, null_newsession),
  66. DEVMETHOD(cryptodev_freesession,null_freesession),
  67. DEVMETHOD(cryptodev_process, null_process),
  68. };
  69. /*
  70. * Generate a new software session.
  71. */
  72. static int
  73. null_newsession(device_t arg, u_int32_t *sid, struct cryptoini *cri)
  74. {
  75. dprintk("%s()\n", __FUNCTION__);
  76. if (sid == NULL || cri == NULL) {
  77. dprintk("%s,%d - EINVAL\n", __FILE__, __LINE__);
  78. return EINVAL;
  79. }
  80. if (null_sesnum == 0)
  81. null_sesnum++;
  82. *sid = null_sesnum++;
  83. return 0;
  84. }
  85. /*
  86. * Free a session.
  87. */
  88. static int
  89. null_freesession(device_t arg, u_int64_t tid)
  90. {
  91. u_int32_t sid = CRYPTO_SESID2LID(tid);
  92. dprintk("%s()\n", __FUNCTION__);
  93. if (sid > null_sesnum) {
  94. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  95. return EINVAL;
  96. }
  97. /* Silently accept and return */
  98. if (sid == 0)
  99. return 0;
  100. return 0;
  101. }
  102. /*
  103. * Process a request.
  104. */
  105. static int
  106. null_process(device_t arg, struct cryptop *crp, int hint)
  107. {
  108. unsigned int lid;
  109. dprintk("%s()\n", __FUNCTION__);
  110. /* Sanity check */
  111. if (crp == NULL) {
  112. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  113. return EINVAL;
  114. }
  115. crp->crp_etype = 0;
  116. if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
  117. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  118. crp->crp_etype = EINVAL;
  119. goto done;
  120. }
  121. /*
  122. * find the session we are using
  123. */
  124. lid = crp->crp_sid & 0xffffffff;
  125. if (lid >= null_sesnum || lid == 0) {
  126. crp->crp_etype = ENOENT;
  127. dprintk("%s,%d: ENOENT\n", __FILE__, __LINE__);
  128. goto done;
  129. }
  130. done:
  131. crypto_done(crp);
  132. return 0;
  133. }
  134. /*
  135. * our driver startup and shutdown routines
  136. */
  137. static int
  138. null_init(void)
  139. {
  140. dprintk("%s(%p)\n", __FUNCTION__, null_init);
  141. memset(&nulldev, 0, sizeof(nulldev));
  142. softc_device_init(&nulldev, "ocfnull", 0, null_methods);
  143. null_id = crypto_get_driverid(softc_get_device(&nulldev),
  144. CRYPTOCAP_F_HARDWARE);
  145. if (null_id < 0)
  146. panic("ocfnull: crypto device cannot initialize!");
  147. #define REGISTER(alg) \
  148. crypto_register(null_id,alg,0,0)
  149. REGISTER(CRYPTO_DES_CBC);
  150. REGISTER(CRYPTO_3DES_CBC);
  151. REGISTER(CRYPTO_RIJNDAEL128_CBC);
  152. REGISTER(CRYPTO_MD5);
  153. REGISTER(CRYPTO_SHA1);
  154. REGISTER(CRYPTO_MD5_HMAC);
  155. REGISTER(CRYPTO_SHA1_HMAC);
  156. #undef REGISTER
  157. return 0;
  158. }
  159. static void
  160. null_exit(void)
  161. {
  162. dprintk("%s()\n", __FUNCTION__);
  163. crypto_unregister_all(null_id);
  164. null_id = -1;
  165. }
  166. module_init(null_init);
  167. module_exit(null_exit);
  168. MODULE_LICENSE("Dual BSD/GPL");
  169. MODULE_AUTHOR("David McCullough <[email protected]>");
  170. MODULE_DESCRIPTION("ocfnull - claims a lot but does nothing");