fcrypt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* FCrypt encryption algorithm
  2. *
  3. * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Based on code:
  12. *
  13. * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
  14. * (Royal Institute of Technology, Stockholm, Sweden).
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. *
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. *
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. *
  28. * 3. Neither the name of the Institute nor the names of its contributors
  29. * may be used to endorse or promote products derived from this software
  30. * without specific prior written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  33. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  36. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  37. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  38. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  41. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  42. * SUCH DAMAGE.
  43. */
  44. #include <asm/byteorder.h>
  45. #include <linux/bitops.h>
  46. #include <linux/init.h>
  47. #include <linux/module.h>
  48. #include <linux/crypto.h>
  49. #define ROUNDS 16
  50. struct fcrypt_ctx {
  51. __be32 sched[ROUNDS];
  52. };
  53. /* Rotate right two 32 bit numbers as a 56 bit number */
  54. #define ror56(hi, lo, n) \
  55. do { \
  56. u32 t = lo & ((1 << n) - 1); \
  57. lo = (lo >> n) | ((hi & ((1 << n) - 1)) << (32 - n)); \
  58. hi = (hi >> n) | (t << (24-n)); \
  59. } while (0)
  60. /* Rotate right one 64 bit number as a 56 bit number */
  61. #define ror56_64(k, n) \
  62. do { \
  63. k = (k >> n) | ((k & ((1 << n) - 1)) << (56 - n)); \
  64. } while (0)
  65. /*
  66. * Sboxes for Feistel network derived from
  67. * /afs/transarc.com/public/afsps/afs.rel31b.export-src/rxkad/sboxes.h
  68. */
  69. #undef Z
  70. #define Z(x) cpu_to_be32(x << 3)
  71. static const __be32 sbox0[256] = {
  72. Z(0xea), Z(0x7f), Z(0xb2), Z(0x64), Z(0x9d), Z(0xb0), Z(0xd9), Z(0x11),
  73. Z(0xcd), Z(0x86), Z(0x86), Z(0x91), Z(0x0a), Z(0xb2), Z(0x93), Z(0x06),
  74. Z(0x0e), Z(0x06), Z(0xd2), Z(0x65), Z(0x73), Z(0xc5), Z(0x28), Z(0x60),
  75. Z(0xf2), Z(0x20), Z(0xb5), Z(0x38), Z(0x7e), Z(0xda), Z(0x9f), Z(0xe3),
  76. Z(0xd2), Z(0xcf), Z(0xc4), Z(0x3c), Z(0x61), Z(0xff), Z(0x4a), Z(0x4a),
  77. Z(0x35), Z(0xac), Z(0xaa), Z(0x5f), Z(0x2b), Z(0xbb), Z(0xbc), Z(0x53),
  78. Z(0x4e), Z(0x9d), Z(0x78), Z(0xa3), Z(0xdc), Z(0x09), Z(0x32), Z(0x10),
  79. Z(0xc6), Z(0x6f), Z(0x66), Z(0xd6), Z(0xab), Z(0xa9), Z(0xaf), Z(0xfd),
  80. Z(0x3b), Z(0x95), Z(0xe8), Z(0x34), Z(0x9a), Z(0x81), Z(0x72), Z(0x80),
  81. Z(0x9c), Z(0xf3), Z(0xec), Z(0xda), Z(0x9f), Z(0x26), Z(0x76), Z(0x15),
  82. Z(0x3e), Z(0x55), Z(0x4d), Z(0xde), Z(0x84), Z(0xee), Z(0xad), Z(0xc7),
  83. Z(0xf1), Z(0x6b), Z(0x3d), Z(0xd3), Z(0x04), Z(0x49), Z(0xaa), Z(0x24),
  84. Z(0x0b), Z(0x8a), Z(0x83), Z(0xba), Z(0xfa), Z(0x85), Z(0xa0), Z(0xa8),
  85. Z(0xb1), Z(0xd4), Z(0x01), Z(0xd8), Z(0x70), Z(0x64), Z(0xf0), Z(0x51),
  86. Z(0xd2), Z(0xc3), Z(0xa7), Z(0x75), Z(0x8c), Z(0xa5), Z(0x64), Z(0xef),
  87. Z(0x10), Z(0x4e), Z(0xb7), Z(0xc6), Z(0x61), Z(0x03), Z(0xeb), Z(0x44),
  88. Z(0x3d), Z(0xe5), Z(0xb3), Z(0x5b), Z(0xae), Z(0xd5), Z(0xad), Z(0x1d),
  89. Z(0xfa), Z(0x5a), Z(0x1e), Z(0x33), Z(0xab), Z(0x93), Z(0xa2), Z(0xb7),
  90. Z(0xe7), Z(0xa8), Z(0x45), Z(0xa4), Z(0xcd), Z(0x29), Z(0x63), Z(0x44),
  91. Z(0xb6), Z(0x69), Z(0x7e), Z(0x2e), Z(0x62), Z(0x03), Z(0xc8), Z(0xe0),
  92. Z(0x17), Z(0xbb), Z(0xc7), Z(0xf3), Z(0x3f), Z(0x36), Z(0xba), Z(0x71),
  93. Z(0x8e), Z(0x97), Z(0x65), Z(0x60), Z(0x69), Z(0xb6), Z(0xf6), Z(0xe6),
  94. Z(0x6e), Z(0xe0), Z(0x81), Z(0x59), Z(0xe8), Z(0xaf), Z(0xdd), Z(0x95),
  95. Z(0x22), Z(0x99), Z(0xfd), Z(0x63), Z(0x19), Z(0x74), Z(0x61), Z(0xb1),
  96. Z(0xb6), Z(0x5b), Z(0xae), Z(0x54), Z(0xb3), Z(0x70), Z(0xff), Z(0xc6),
  97. Z(0x3b), Z(0x3e), Z(0xc1), Z(0xd7), Z(0xe1), Z(0x0e), Z(0x76), Z(0xe5),
  98. Z(0x36), Z(0x4f), Z(0x59), Z(0xc7), Z(0x08), Z(0x6e), Z(0x82), Z(0xa6),
  99. Z(0x93), Z(0xc4), Z(0xaa), Z(0x26), Z(0x49), Z(0xe0), Z(0x21), Z(0x64),
  100. Z(0x07), Z(0x9f), Z(0x64), Z(0x81), Z(0x9c), Z(0xbf), Z(0xf9), Z(0xd1),
  101. Z(0x43), Z(0xf8), Z(0xb6), Z(0xb9), Z(0xf1), Z(0x24), Z(0x75), Z(0x03),
  102. Z(0xe4), Z(0xb0), Z(0x99), Z(0x46), Z(0x3d), Z(0xf5), Z(0xd1), Z(0x39),
  103. Z(0x72), Z(0x12), Z(0xf6), Z(0xba), Z(0x0c), Z(0x0d), Z(0x42), Z(0x2e)
  104. };
  105. #undef Z
  106. #define Z(x) cpu_to_be32(((x & 0x1f) << 27) | (x >> 5))
  107. static const __be32 sbox1[256] = {
  108. Z(0x77), Z(0x14), Z(0xa6), Z(0xfe), Z(0xb2), Z(0x5e), Z(0x8c), Z(0x3e),
  109. Z(0x67), Z(0x6c), Z(0xa1), Z(0x0d), Z(0xc2), Z(0xa2), Z(0xc1), Z(0x85),
  110. Z(0x6c), Z(0x7b), Z(0x67), Z(0xc6), Z(0x23), Z(0xe3), Z(0xf2), Z(0x89),
  111. Z(0x50), Z(0x9c), Z(0x03), Z(0xb7), Z(0x73), Z(0xe6), Z(0xe1), Z(0x39),
  112. Z(0x31), Z(0x2c), Z(0x27), Z(0x9f), Z(0xa5), Z(0x69), Z(0x44), Z(0xd6),
  113. Z(0x23), Z(0x83), Z(0x98), Z(0x7d), Z(0x3c), Z(0xb4), Z(0x2d), Z(0x99),
  114. Z(0x1c), Z(0x1f), Z(0x8c), Z(0x20), Z(0x03), Z(0x7c), Z(0x5f), Z(0xad),
  115. Z(0xf4), Z(0xfa), Z(0x95), Z(0xca), Z(0x76), Z(0x44), Z(0xcd), Z(0xb6),
  116. Z(0xb8), Z(0xa1), Z(0xa1), Z(0xbe), Z(0x9e), Z(0x54), Z(0x8f), Z(0x0b),
  117. Z(0x16), Z(0x74), Z(0x31), Z(0x8a), Z(0x23), Z(0x17), Z(0x04), Z(0xfa),
  118. Z(0x79), Z(0x84), Z(0xb1), Z(0xf5), Z(0x13), Z(0xab), Z(0xb5), Z(0x2e),
  119. Z(0xaa), Z(0x0c), Z(0x60), Z(0x6b), Z(0x5b), Z(0xc4), Z(0x4b), Z(0xbc),
  120. Z(0xe2), Z(0xaf), Z(0x45), Z(0x73), Z(0xfa), Z(0xc9), Z(0x49), Z(0xcd),
  121. Z(0x00), Z(0x92), Z(0x7d), Z(0x97), Z(0x7a), Z(0x18), Z(0x60), Z(0x3d),
  122. Z(0xcf), Z(0x5b), Z(0xde), Z(0xc6), Z(0xe2), Z(0xe6), Z(0xbb), Z(0x8b),
  123. Z(0x06), Z(0xda), Z(0x08), Z(0x15), Z(0x1b), Z(0x88), Z(0x6a), Z(0x17),
  124. Z(0x89), Z(0xd0), Z(0xa9), Z(0xc1), Z(0xc9), Z(0x70), Z(0x6b), Z(0xe5),
  125. Z(0x43), Z(0xf4), Z(0x68), Z(0xc8), Z(0xd3), Z(0x84), Z(0x28), Z(0x0a),
  126. Z(0x52), Z(0x66), Z(0xa3), Z(0xca), Z(0xf2), Z(0xe3), Z(0x7f), Z(0x7a),
  127. Z(0x31), Z(0xf7), Z(0x88), Z(0x94), Z(0x5e), Z(0x9c), Z(0x63), Z(0xd5),
  128. Z(0x24), Z(0x66), Z(0xfc), Z(0xb3), Z(0x57), Z(0x25), Z(0xbe), Z(0x89),
  129. Z(0x44), Z(0xc4), Z(0xe0), Z(0x8f), Z(0x23), Z(0x3c), Z(0x12), Z(0x52),
  130. Z(0xf5), Z(0x1e), Z(0xf4), Z(0xcb), Z(0x18), Z(0x33), Z(0x1f), Z(0xf8),
  131. Z(0x69), Z(0x10), Z(0x9d), Z(0xd3), Z(0xf7), Z(0x28), Z(0xf8), Z(0x30),
  132. Z(0x05), Z(0x5e), Z(0x32), Z(0xc0), Z(0xd5), Z(0x19), Z(0xbd), Z(0x45),
  133. Z(0x8b), Z(0x5b), Z(0xfd), Z(0xbc), Z(0xe2), Z(0x5c), Z(0xa9), Z(0x96),
  134. Z(0xef), Z(0x70), Z(0xcf), Z(0xc2), Z(0x2a), Z(0xb3), Z(0x61), Z(0xad),
  135. Z(0x80), Z(0x48), Z(0x81), Z(0xb7), Z(0x1d), Z(0x43), Z(0xd9), Z(0xd7),
  136. Z(0x45), Z(0xf0), Z(0xd8), Z(0x8a), Z(0x59), Z(0x7c), Z(0x57), Z(0xc1),
  137. Z(0x79), Z(0xc7), Z(0x34), Z(0xd6), Z(0x43), Z(0xdf), Z(0xe4), Z(0x78),
  138. Z(0x16), Z(0x06), Z(0xda), Z(0x92), Z(0x76), Z(0x51), Z(0xe1), Z(0xd4),
  139. Z(0x70), Z(0x03), Z(0xe0), Z(0x2f), Z(0x96), Z(0x91), Z(0x82), Z(0x80)
  140. };
  141. #undef Z
  142. #define Z(x) cpu_to_be32(x << 11)
  143. static const __be32 sbox2[256] = {
  144. Z(0xf0), Z(0x37), Z(0x24), Z(0x53), Z(0x2a), Z(0x03), Z(0x83), Z(0x86),
  145. Z(0xd1), Z(0xec), Z(0x50), Z(0xf0), Z(0x42), Z(0x78), Z(0x2f), Z(0x6d),
  146. Z(0xbf), Z(0x80), Z(0x87), Z(0x27), Z(0x95), Z(0xe2), Z(0xc5), Z(0x5d),
  147. Z(0xf9), Z(0x6f), Z(0xdb), Z(0xb4), Z(0x65), Z(0x6e), Z(0xe7), Z(0x24),
  148. Z(0xc8), Z(0x1a), Z(0xbb), Z(0x49), Z(0xb5), Z(0x0a), Z(0x7d), Z(0xb9),
  149. Z(0xe8), Z(0xdc), Z(0xb7), Z(0xd9), Z(0x45), Z(0x20), Z(0x1b), Z(0xce),
  150. Z(0x59), Z(0x9d), Z(0x6b), Z(0xbd), Z(0x0e), Z(0x8f), Z(0xa3), Z(0xa9),
  151. Z(0xbc), Z(0x74), Z(0xa6), Z(0xf6), Z(0x7f), Z(0x5f), Z(0xb1), Z(0x68),
  152. Z(0x84), Z(0xbc), Z(0xa9), Z(0xfd), Z(0x55), Z(0x50), Z(0xe9), Z(0xb6),
  153. Z(0x13), Z(0x5e), Z(0x07), Z(0xb8), Z(0x95), Z(0x02), Z(0xc0), Z(0xd0),
  154. Z(0x6a), Z(0x1a), Z(0x85), Z(0xbd), Z(0xb6), Z(0xfd), Z(0xfe), Z(0x17),
  155. Z(0x3f), Z(0x09), Z(0xa3), Z(0x8d), Z(0xfb), Z(0xed), Z(0xda), Z(0x1d),
  156. Z(0x6d), Z(0x1c), Z(0x6c), Z(0x01), Z(0x5a), Z(0xe5), Z(0x71), Z(0x3e),
  157. Z(0x8b), Z(0x6b), Z(0xbe), Z(0x29), Z(0xeb), Z(0x12), Z(0x19), Z(0x34),
  158. Z(0xcd), Z(0xb3), Z(0xbd), Z(0x35), Z(0xea), Z(0x4b), Z(0xd5), Z(0xae),
  159. Z(0x2a), Z(0x79), Z(0x5a), Z(0xa5), Z(0x32), Z(0x12), Z(0x7b), Z(0xdc),
  160. Z(0x2c), Z(0xd0), Z(0x22), Z(0x4b), Z(0xb1), Z(0x85), Z(0x59), Z(0x80),
  161. Z(0xc0), Z(0x30), Z(0x9f), Z(0x73), Z(0xd3), Z(0x14), Z(0x48), Z(0x40),
  162. Z(0x07), Z(0x2d), Z(0x8f), Z(0x80), Z(0x0f), Z(0xce), Z(0x0b), Z(0x5e),
  163. Z(0xb7), Z(0x5e), Z(0xac), Z(0x24), Z(0x94), Z(0x4a), Z(0x18), Z(0x15),
  164. Z(0x05), Z(0xe8), Z(0x02), Z(0x77), Z(0xa9), Z(0xc7), Z(0x40), Z(0x45),
  165. Z(0x89), Z(0xd1), Z(0xea), Z(0xde), Z(0x0c), Z(0x79), Z(0x2a), Z(0x99),
  166. Z(0x6c), Z(0x3e), Z(0x95), Z(0xdd), Z(0x8c), Z(0x7d), Z(0xad), Z(0x6f),
  167. Z(0xdc), Z(0xff), Z(0xfd), Z(0x62), Z(0x47), Z(0xb3), Z(0x21), Z(0x8a),
  168. Z(0xec), Z(0x8e), Z(0x19), Z(0x18), Z(0xb4), Z(0x6e), Z(0x3d), Z(0xfd),
  169. Z(0x74), Z(0x54), Z(0x1e), Z(0x04), Z(0x85), Z(0xd8), Z(0xbc), Z(0x1f),
  170. Z(0x56), Z(0xe7), Z(0x3a), Z(0x56), Z(0x67), Z(0xd6), Z(0xc8), Z(0xa5),
  171. Z(0xf3), Z(0x8e), Z(0xde), Z(0xae), Z(0x37), Z(0x49), Z(0xb7), Z(0xfa),
  172. Z(0xc8), Z(0xf4), Z(0x1f), Z(0xe0), Z(0x2a), Z(0x9b), Z(0x15), Z(0xd1),
  173. Z(0x34), Z(0x0e), Z(0xb5), Z(0xe0), Z(0x44), Z(0x78), Z(0x84), Z(0x59),
  174. Z(0x56), Z(0x68), Z(0x77), Z(0xa5), Z(0x14), Z(0x06), Z(0xf5), Z(0x2f),
  175. Z(0x8c), Z(0x8a), Z(0x73), Z(0x80), Z(0x76), Z(0xb4), Z(0x10), Z(0x86)
  176. };
  177. #undef Z
  178. #define Z(x) cpu_to_be32(x << 19)
  179. static const __be32 sbox3[256] = {
  180. Z(0xa9), Z(0x2a), Z(0x48), Z(0x51), Z(0x84), Z(0x7e), Z(0x49), Z(0xe2),
  181. Z(0xb5), Z(0xb7), Z(0x42), Z(0x33), Z(0x7d), Z(0x5d), Z(0xa6), Z(0x12),
  182. Z(0x44), Z(0x48), Z(0x6d), Z(0x28), Z(0xaa), Z(0x20), Z(0x6d), Z(0x57),
  183. Z(0xd6), Z(0x6b), Z(0x5d), Z(0x72), Z(0xf0), Z(0x92), Z(0x5a), Z(0x1b),
  184. Z(0x53), Z(0x80), Z(0x24), Z(0x70), Z(0x9a), Z(0xcc), Z(0xa7), Z(0x66),
  185. Z(0xa1), Z(0x01), Z(0xa5), Z(0x41), Z(0x97), Z(0x41), Z(0x31), Z(0x82),
  186. Z(0xf1), Z(0x14), Z(0xcf), Z(0x53), Z(0x0d), Z(0xa0), Z(0x10), Z(0xcc),
  187. Z(0x2a), Z(0x7d), Z(0xd2), Z(0xbf), Z(0x4b), Z(0x1a), Z(0xdb), Z(0x16),
  188. Z(0x47), Z(0xf6), Z(0x51), Z(0x36), Z(0xed), Z(0xf3), Z(0xb9), Z(0x1a),
  189. Z(0xa7), Z(0xdf), Z(0x29), Z(0x43), Z(0x01), Z(0x54), Z(0x70), Z(0xa4),
  190. Z(0xbf), Z(0xd4), Z(0x0b), Z(0x53), Z(0x44), Z(0x60), Z(0x9e), Z(0x23),
  191. Z(0xa1), Z(0x18), Z(0x68), Z(0x4f), Z(0xf0), Z(0x2f), Z(0x82), Z(0xc2),
  192. Z(0x2a), Z(0x41), Z(0xb2), Z(0x42), Z(0x0c), Z(0xed), Z(0x0c), Z(0x1d),
  193. Z(0x13), Z(0x3a), Z(0x3c), Z(0x6e), Z(0x35), Z(0xdc), Z(0x60), Z(0x65),
  194. Z(0x85), Z(0xe9), Z(0x64), Z(0x02), Z(0x9a), Z(0x3f), Z(0x9f), Z(0x87),
  195. Z(0x96), Z(0xdf), Z(0xbe), Z(0xf2), Z(0xcb), Z(0xe5), Z(0x6c), Z(0xd4),
  196. Z(0x5a), Z(0x83), Z(0xbf), Z(0x92), Z(0x1b), Z(0x94), Z(0x00), Z(0x42),
  197. Z(0xcf), Z(0x4b), Z(0x00), Z(0x75), Z(0xba), Z(0x8f), Z(0x76), Z(0x5f),
  198. Z(0x5d), Z(0x3a), Z(0x4d), Z(0x09), Z(0x12), Z(0x08), Z(0x38), Z(0x95),
  199. Z(0x17), Z(0xe4), Z(0x01), Z(0x1d), Z(0x4c), Z(0xa9), Z(0xcc), Z(0x85),
  200. Z(0x82), Z(0x4c), Z(0x9d), Z(0x2f), Z(0x3b), Z(0x66), Z(0xa1), Z(0x34),
  201. Z(0x10), Z(0xcd), Z(0x59), Z(0x89), Z(0xa5), Z(0x31), Z(0xcf), Z(0x05),
  202. Z(0xc8), Z(0x84), Z(0xfa), Z(0xc7), Z(0xba), Z(0x4e), Z(0x8b), Z(0x1a),
  203. Z(0x19), Z(0xf1), Z(0xa1), Z(0x3b), Z(0x18), Z(0x12), Z(0x17), Z(0xb0),
  204. Z(0x98), Z(0x8d), Z(0x0b), Z(0x23), Z(0xc3), Z(0x3a), Z(0x2d), Z(0x20),
  205. Z(0xdf), Z(0x13), Z(0xa0), Z(0xa8), Z(0x4c), Z(0x0d), Z(0x6c), Z(0x2f),
  206. Z(0x47), Z(0x13), Z(0x13), Z(0x52), Z(0x1f), Z(0x2d), Z(0xf5), Z(0x79),
  207. Z(0x3d), Z(0xa2), Z(0x54), Z(0xbd), Z(0x69), Z(0xc8), Z(0x6b), Z(0xf3),
  208. Z(0x05), Z(0x28), Z(0xf1), Z(0x16), Z(0x46), Z(0x40), Z(0xb0), Z(0x11),
  209. Z(0xd3), Z(0xb7), Z(0x95), Z(0x49), Z(0xcf), Z(0xc3), Z(0x1d), Z(0x8f),
  210. Z(0xd8), Z(0xe1), Z(0x73), Z(0xdb), Z(0xad), Z(0xc8), Z(0xc9), Z(0xa9),
  211. Z(0xa1), Z(0xc2), Z(0xc5), Z(0xe3), Z(0xba), Z(0xfc), Z(0x0e), Z(0x25)
  212. };
  213. /*
  214. * This is a 16 round Feistel network with permutation F_ENCRYPT
  215. */
  216. #define F_ENCRYPT(R, L, sched) \
  217. do { \
  218. union lc4 { __be32 l; u8 c[4]; } u; \
  219. u.l = sched ^ R; \
  220. L ^= sbox0[u.c[0]] ^ sbox1[u.c[1]] ^ sbox2[u.c[2]] ^ sbox3[u.c[3]]; \
  221. } while (0)
  222. /*
  223. * encryptor
  224. */
  225. static void fcrypt_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  226. {
  227. const struct fcrypt_ctx *ctx = crypto_tfm_ctx(tfm);
  228. struct {
  229. __be32 l, r;
  230. } X;
  231. memcpy(&X, src, sizeof(X));
  232. F_ENCRYPT(X.r, X.l, ctx->sched[0x0]);
  233. F_ENCRYPT(X.l, X.r, ctx->sched[0x1]);
  234. F_ENCRYPT(X.r, X.l, ctx->sched[0x2]);
  235. F_ENCRYPT(X.l, X.r, ctx->sched[0x3]);
  236. F_ENCRYPT(X.r, X.l, ctx->sched[0x4]);
  237. F_ENCRYPT(X.l, X.r, ctx->sched[0x5]);
  238. F_ENCRYPT(X.r, X.l, ctx->sched[0x6]);
  239. F_ENCRYPT(X.l, X.r, ctx->sched[0x7]);
  240. F_ENCRYPT(X.r, X.l, ctx->sched[0x8]);
  241. F_ENCRYPT(X.l, X.r, ctx->sched[0x9]);
  242. F_ENCRYPT(X.r, X.l, ctx->sched[0xa]);
  243. F_ENCRYPT(X.l, X.r, ctx->sched[0xb]);
  244. F_ENCRYPT(X.r, X.l, ctx->sched[0xc]);
  245. F_ENCRYPT(X.l, X.r, ctx->sched[0xd]);
  246. F_ENCRYPT(X.r, X.l, ctx->sched[0xe]);
  247. F_ENCRYPT(X.l, X.r, ctx->sched[0xf]);
  248. memcpy(dst, &X, sizeof(X));
  249. }
  250. /*
  251. * decryptor
  252. */
  253. static void fcrypt_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  254. {
  255. const struct fcrypt_ctx *ctx = crypto_tfm_ctx(tfm);
  256. struct {
  257. __be32 l, r;
  258. } X;
  259. memcpy(&X, src, sizeof(X));
  260. F_ENCRYPT(X.l, X.r, ctx->sched[0xf]);
  261. F_ENCRYPT(X.r, X.l, ctx->sched[0xe]);
  262. F_ENCRYPT(X.l, X.r, ctx->sched[0xd]);
  263. F_ENCRYPT(X.r, X.l, ctx->sched[0xc]);
  264. F_ENCRYPT(X.l, X.r, ctx->sched[0xb]);
  265. F_ENCRYPT(X.r, X.l, ctx->sched[0xa]);
  266. F_ENCRYPT(X.l, X.r, ctx->sched[0x9]);
  267. F_ENCRYPT(X.r, X.l, ctx->sched[0x8]);
  268. F_ENCRYPT(X.l, X.r, ctx->sched[0x7]);
  269. F_ENCRYPT(X.r, X.l, ctx->sched[0x6]);
  270. F_ENCRYPT(X.l, X.r, ctx->sched[0x5]);
  271. F_ENCRYPT(X.r, X.l, ctx->sched[0x4]);
  272. F_ENCRYPT(X.l, X.r, ctx->sched[0x3]);
  273. F_ENCRYPT(X.r, X.l, ctx->sched[0x2]);
  274. F_ENCRYPT(X.l, X.r, ctx->sched[0x1]);
  275. F_ENCRYPT(X.r, X.l, ctx->sched[0x0]);
  276. memcpy(dst, &X, sizeof(X));
  277. }
  278. /*
  279. * Generate a key schedule from key, the least significant bit in each key byte
  280. * is parity and shall be ignored. This leaves 56 significant bits in the key
  281. * to scatter over the 16 key schedules. For each schedule extract the low
  282. * order 32 bits and use as schedule, then rotate right by 11 bits.
  283. */
  284. static int fcrypt_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  285. {
  286. struct fcrypt_ctx *ctx = crypto_tfm_ctx(tfm);
  287. #if BITS_PER_LONG == 64 /* the 64-bit version can also be used for 32-bit
  288. * kernels - it seems to be faster but the code is
  289. * larger */
  290. u64 k; /* k holds all 56 non-parity bits */
  291. /* discard the parity bits */
  292. k = (*key++) >> 1;
  293. k <<= 7;
  294. k |= (*key++) >> 1;
  295. k <<= 7;
  296. k |= (*key++) >> 1;
  297. k <<= 7;
  298. k |= (*key++) >> 1;
  299. k <<= 7;
  300. k |= (*key++) >> 1;
  301. k <<= 7;
  302. k |= (*key++) >> 1;
  303. k <<= 7;
  304. k |= (*key++) >> 1;
  305. k <<= 7;
  306. k |= (*key) >> 1;
  307. /* Use lower 32 bits for schedule, rotate by 11 each round (16 times) */
  308. ctx->sched[0x0] = cpu_to_be32(k); ror56_64(k, 11);
  309. ctx->sched[0x1] = cpu_to_be32(k); ror56_64(k, 11);
  310. ctx->sched[0x2] = cpu_to_be32(k); ror56_64(k, 11);
  311. ctx->sched[0x3] = cpu_to_be32(k); ror56_64(k, 11);
  312. ctx->sched[0x4] = cpu_to_be32(k); ror56_64(k, 11);
  313. ctx->sched[0x5] = cpu_to_be32(k); ror56_64(k, 11);
  314. ctx->sched[0x6] = cpu_to_be32(k); ror56_64(k, 11);
  315. ctx->sched[0x7] = cpu_to_be32(k); ror56_64(k, 11);
  316. ctx->sched[0x8] = cpu_to_be32(k); ror56_64(k, 11);
  317. ctx->sched[0x9] = cpu_to_be32(k); ror56_64(k, 11);
  318. ctx->sched[0xa] = cpu_to_be32(k); ror56_64(k, 11);
  319. ctx->sched[0xb] = cpu_to_be32(k); ror56_64(k, 11);
  320. ctx->sched[0xc] = cpu_to_be32(k); ror56_64(k, 11);
  321. ctx->sched[0xd] = cpu_to_be32(k); ror56_64(k, 11);
  322. ctx->sched[0xe] = cpu_to_be32(k); ror56_64(k, 11);
  323. ctx->sched[0xf] = cpu_to_be32(k);
  324. return 0;
  325. #else
  326. u32 hi, lo; /* hi is upper 24 bits and lo lower 32, total 56 */
  327. /* discard the parity bits */
  328. lo = (*key++) >> 1;
  329. lo <<= 7;
  330. lo |= (*key++) >> 1;
  331. lo <<= 7;
  332. lo |= (*key++) >> 1;
  333. lo <<= 7;
  334. lo |= (*key++) >> 1;
  335. hi = lo >> 4;
  336. lo &= 0xf;
  337. lo <<= 7;
  338. lo |= (*key++) >> 1;
  339. lo <<= 7;
  340. lo |= (*key++) >> 1;
  341. lo <<= 7;
  342. lo |= (*key++) >> 1;
  343. lo <<= 7;
  344. lo |= (*key) >> 1;
  345. /* Use lower 32 bits for schedule, rotate by 11 each round (16 times) */
  346. ctx->sched[0x0] = cpu_to_be32(lo); ror56(hi, lo, 11);
  347. ctx->sched[0x1] = cpu_to_be32(lo); ror56(hi, lo, 11);
  348. ctx->sched[0x2] = cpu_to_be32(lo); ror56(hi, lo, 11);
  349. ctx->sched[0x3] = cpu_to_be32(lo); ror56(hi, lo, 11);
  350. ctx->sched[0x4] = cpu_to_be32(lo); ror56(hi, lo, 11);
  351. ctx->sched[0x5] = cpu_to_be32(lo); ror56(hi, lo, 11);
  352. ctx->sched[0x6] = cpu_to_be32(lo); ror56(hi, lo, 11);
  353. ctx->sched[0x7] = cpu_to_be32(lo); ror56(hi, lo, 11);
  354. ctx->sched[0x8] = cpu_to_be32(lo); ror56(hi, lo, 11);
  355. ctx->sched[0x9] = cpu_to_be32(lo); ror56(hi, lo, 11);
  356. ctx->sched[0xa] = cpu_to_be32(lo); ror56(hi, lo, 11);
  357. ctx->sched[0xb] = cpu_to_be32(lo); ror56(hi, lo, 11);
  358. ctx->sched[0xc] = cpu_to_be32(lo); ror56(hi, lo, 11);
  359. ctx->sched[0xd] = cpu_to_be32(lo); ror56(hi, lo, 11);
  360. ctx->sched[0xe] = cpu_to_be32(lo); ror56(hi, lo, 11);
  361. ctx->sched[0xf] = cpu_to_be32(lo);
  362. return 0;
  363. #endif
  364. }
  365. static struct crypto_alg fcrypt_alg = {
  366. .cra_name = "fcrypt",
  367. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  368. .cra_blocksize = 8,
  369. .cra_ctxsize = sizeof(struct fcrypt_ctx),
  370. .cra_module = THIS_MODULE,
  371. .cra_alignmask = 3,
  372. .cra_u = { .cipher = {
  373. .cia_min_keysize = 8,
  374. .cia_max_keysize = 8,
  375. .cia_setkey = fcrypt_setkey,
  376. .cia_encrypt = fcrypt_encrypt,
  377. .cia_decrypt = fcrypt_decrypt } }
  378. };
  379. static int __init fcrypt_mod_init(void)
  380. {
  381. return crypto_register_alg(&fcrypt_alg);
  382. }
  383. static void __exit fcrypt_mod_fini(void)
  384. {
  385. crypto_unregister_alg(&fcrypt_alg);
  386. }
  387. module_init(fcrypt_mod_init);
  388. module_exit(fcrypt_mod_fini);
  389. MODULE_LICENSE("Dual BSD/GPL");
  390. MODULE_DESCRIPTION("FCrypt Cipher Algorithm");
  391. MODULE_AUTHOR("David Howells <dhowells@redhat.com>");
  392. MODULE_ALIAS_CRYPTO("fcrypt");