選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

773 行
31 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/des.h>
  57. #include <stdlib.h>
  58. #include "internal.h"
  59. static const uint32_t des_skb[8][64] = {
  60. {/* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
  61. 0x00000000L, 0x00000010L, 0x20000000L, 0x20000010L, 0x00010000L,
  62. 0x00010010L, 0x20010000L, 0x20010010L, 0x00000800L, 0x00000810L,
  63. 0x20000800L, 0x20000810L, 0x00010800L, 0x00010810L, 0x20010800L,
  64. 0x20010810L, 0x00000020L, 0x00000030L, 0x20000020L, 0x20000030L,
  65. 0x00010020L, 0x00010030L, 0x20010020L, 0x20010030L, 0x00000820L,
  66. 0x00000830L, 0x20000820L, 0x20000830L, 0x00010820L, 0x00010830L,
  67. 0x20010820L, 0x20010830L, 0x00080000L, 0x00080010L, 0x20080000L,
  68. 0x20080010L, 0x00090000L, 0x00090010L, 0x20090000L, 0x20090010L,
  69. 0x00080800L, 0x00080810L, 0x20080800L, 0x20080810L, 0x00090800L,
  70. 0x00090810L, 0x20090800L, 0x20090810L, 0x00080020L, 0x00080030L,
  71. 0x20080020L, 0x20080030L, 0x00090020L, 0x00090030L, 0x20090020L,
  72. 0x20090030L, 0x00080820L, 0x00080830L, 0x20080820L, 0x20080830L,
  73. 0x00090820L, 0x00090830L, 0x20090820L, 0x20090830L, },
  74. {/* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */
  75. 0x00000000L, 0x02000000L, 0x00002000L, 0x02002000L, 0x00200000L,
  76. 0x02200000L, 0x00202000L, 0x02202000L, 0x00000004L, 0x02000004L,
  77. 0x00002004L, 0x02002004L, 0x00200004L, 0x02200004L, 0x00202004L,
  78. 0x02202004L, 0x00000400L, 0x02000400L, 0x00002400L, 0x02002400L,
  79. 0x00200400L, 0x02200400L, 0x00202400L, 0x02202400L, 0x00000404L,
  80. 0x02000404L, 0x00002404L, 0x02002404L, 0x00200404L, 0x02200404L,
  81. 0x00202404L, 0x02202404L, 0x10000000L, 0x12000000L, 0x10002000L,
  82. 0x12002000L, 0x10200000L, 0x12200000L, 0x10202000L, 0x12202000L,
  83. 0x10000004L, 0x12000004L, 0x10002004L, 0x12002004L, 0x10200004L,
  84. 0x12200004L, 0x10202004L, 0x12202004L, 0x10000400L, 0x12000400L,
  85. 0x10002400L, 0x12002400L, 0x10200400L, 0x12200400L, 0x10202400L,
  86. 0x12202400L, 0x10000404L, 0x12000404L, 0x10002404L, 0x12002404L,
  87. 0x10200404L, 0x12200404L, 0x10202404L, 0x12202404L, },
  88. {/* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */
  89. 0x00000000L, 0x00000001L, 0x00040000L, 0x00040001L, 0x01000000L,
  90. 0x01000001L, 0x01040000L, 0x01040001L, 0x00000002L, 0x00000003L,
  91. 0x00040002L, 0x00040003L, 0x01000002L, 0x01000003L, 0x01040002L,
  92. 0x01040003L, 0x00000200L, 0x00000201L, 0x00040200L, 0x00040201L,
  93. 0x01000200L, 0x01000201L, 0x01040200L, 0x01040201L, 0x00000202L,
  94. 0x00000203L, 0x00040202L, 0x00040203L, 0x01000202L, 0x01000203L,
  95. 0x01040202L, 0x01040203L, 0x08000000L, 0x08000001L, 0x08040000L,
  96. 0x08040001L, 0x09000000L, 0x09000001L, 0x09040000L, 0x09040001L,
  97. 0x08000002L, 0x08000003L, 0x08040002L, 0x08040003L, 0x09000002L,
  98. 0x09000003L, 0x09040002L, 0x09040003L, 0x08000200L, 0x08000201L,
  99. 0x08040200L, 0x08040201L, 0x09000200L, 0x09000201L, 0x09040200L,
  100. 0x09040201L, 0x08000202L, 0x08000203L, 0x08040202L, 0x08040203L,
  101. 0x09000202L, 0x09000203L, 0x09040202L, 0x09040203L, },
  102. {/* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */
  103. 0x00000000L, 0x00100000L, 0x00000100L, 0x00100100L, 0x00000008L,
  104. 0x00100008L, 0x00000108L, 0x00100108L, 0x00001000L, 0x00101000L,
  105. 0x00001100L, 0x00101100L, 0x00001008L, 0x00101008L, 0x00001108L,
  106. 0x00101108L, 0x04000000L, 0x04100000L, 0x04000100L, 0x04100100L,
  107. 0x04000008L, 0x04100008L, 0x04000108L, 0x04100108L, 0x04001000L,
  108. 0x04101000L, 0x04001100L, 0x04101100L, 0x04001008L, 0x04101008L,
  109. 0x04001108L, 0x04101108L, 0x00020000L, 0x00120000L, 0x00020100L,
  110. 0x00120100L, 0x00020008L, 0x00120008L, 0x00020108L, 0x00120108L,
  111. 0x00021000L, 0x00121000L, 0x00021100L, 0x00121100L, 0x00021008L,
  112. 0x00121008L, 0x00021108L, 0x00121108L, 0x04020000L, 0x04120000L,
  113. 0x04020100L, 0x04120100L, 0x04020008L, 0x04120008L, 0x04020108L,
  114. 0x04120108L, 0x04021000L, 0x04121000L, 0x04021100L, 0x04121100L,
  115. 0x04021008L, 0x04121008L, 0x04021108L, 0x04121108L, },
  116. {/* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
  117. 0x00000000L, 0x10000000L, 0x00010000L, 0x10010000L, 0x00000004L,
  118. 0x10000004L, 0x00010004L, 0x10010004L, 0x20000000L, 0x30000000L,
  119. 0x20010000L, 0x30010000L, 0x20000004L, 0x30000004L, 0x20010004L,
  120. 0x30010004L, 0x00100000L, 0x10100000L, 0x00110000L, 0x10110000L,
  121. 0x00100004L, 0x10100004L, 0x00110004L, 0x10110004L, 0x20100000L,
  122. 0x30100000L, 0x20110000L, 0x30110000L, 0x20100004L, 0x30100004L,
  123. 0x20110004L, 0x30110004L, 0x00001000L, 0x10001000L, 0x00011000L,
  124. 0x10011000L, 0x00001004L, 0x10001004L, 0x00011004L, 0x10011004L,
  125. 0x20001000L, 0x30001000L, 0x20011000L, 0x30011000L, 0x20001004L,
  126. 0x30001004L, 0x20011004L, 0x30011004L, 0x00101000L, 0x10101000L,
  127. 0x00111000L, 0x10111000L, 0x00101004L, 0x10101004L, 0x00111004L,
  128. 0x10111004L, 0x20101000L, 0x30101000L, 0x20111000L, 0x30111000L,
  129. 0x20101004L, 0x30101004L, 0x20111004L, 0x30111004L, },
  130. {/* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */
  131. 0x00000000L, 0x08000000L, 0x00000008L, 0x08000008L, 0x00000400L,
  132. 0x08000400L, 0x00000408L, 0x08000408L, 0x00020000L, 0x08020000L,
  133. 0x00020008L, 0x08020008L, 0x00020400L, 0x08020400L, 0x00020408L,
  134. 0x08020408L, 0x00000001L, 0x08000001L, 0x00000009L, 0x08000009L,
  135. 0x00000401L, 0x08000401L, 0x00000409L, 0x08000409L, 0x00020001L,
  136. 0x08020001L, 0x00020009L, 0x08020009L, 0x00020401L, 0x08020401L,
  137. 0x00020409L, 0x08020409L, 0x02000000L, 0x0A000000L, 0x02000008L,
  138. 0x0A000008L, 0x02000400L, 0x0A000400L, 0x02000408L, 0x0A000408L,
  139. 0x02020000L, 0x0A020000L, 0x02020008L, 0x0A020008L, 0x02020400L,
  140. 0x0A020400L, 0x02020408L, 0x0A020408L, 0x02000001L, 0x0A000001L,
  141. 0x02000009L, 0x0A000009L, 0x02000401L, 0x0A000401L, 0x02000409L,
  142. 0x0A000409L, 0x02020001L, 0x0A020001L, 0x02020009L, 0x0A020009L,
  143. 0x02020401L, 0x0A020401L, 0x02020409L, 0x0A020409L, },
  144. {/* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */
  145. 0x00000000L, 0x00000100L, 0x00080000L, 0x00080100L, 0x01000000L,
  146. 0x01000100L, 0x01080000L, 0x01080100L, 0x00000010L, 0x00000110L,
  147. 0x00080010L, 0x00080110L, 0x01000010L, 0x01000110L, 0x01080010L,
  148. 0x01080110L, 0x00200000L, 0x00200100L, 0x00280000L, 0x00280100L,
  149. 0x01200000L, 0x01200100L, 0x01280000L, 0x01280100L, 0x00200010L,
  150. 0x00200110L, 0x00280010L, 0x00280110L, 0x01200010L, 0x01200110L,
  151. 0x01280010L, 0x01280110L, 0x00000200L, 0x00000300L, 0x00080200L,
  152. 0x00080300L, 0x01000200L, 0x01000300L, 0x01080200L, 0x01080300L,
  153. 0x00000210L, 0x00000310L, 0x00080210L, 0x00080310L, 0x01000210L,
  154. 0x01000310L, 0x01080210L, 0x01080310L, 0x00200200L, 0x00200300L,
  155. 0x00280200L, 0x00280300L, 0x01200200L, 0x01200300L, 0x01280200L,
  156. 0x01280300L, 0x00200210L, 0x00200310L, 0x00280210L, 0x00280310L,
  157. 0x01200210L, 0x01200310L, 0x01280210L, 0x01280310L, },
  158. {/* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */
  159. 0x00000000L, 0x04000000L, 0x00040000L, 0x04040000L, 0x00000002L,
  160. 0x04000002L, 0x00040002L, 0x04040002L, 0x00002000L, 0x04002000L,
  161. 0x00042000L, 0x04042000L, 0x00002002L, 0x04002002L, 0x00042002L,
  162. 0x04042002L, 0x00000020L, 0x04000020L, 0x00040020L, 0x04040020L,
  163. 0x00000022L, 0x04000022L, 0x00040022L, 0x04040022L, 0x00002020L,
  164. 0x04002020L, 0x00042020L, 0x04042020L, 0x00002022L, 0x04002022L,
  165. 0x00042022L, 0x04042022L, 0x00000800L, 0x04000800L, 0x00040800L,
  166. 0x04040800L, 0x00000802L, 0x04000802L, 0x00040802L, 0x04040802L,
  167. 0x00002800L, 0x04002800L, 0x00042800L, 0x04042800L, 0x00002802L,
  168. 0x04002802L, 0x00042802L, 0x04042802L, 0x00000820L, 0x04000820L,
  169. 0x00040820L, 0x04040820L, 0x00000822L, 0x04000822L, 0x00040822L,
  170. 0x04040822L, 0x00002820L, 0x04002820L, 0x00042820L, 0x04042820L,
  171. 0x00002822L, 0x04002822L, 0x00042822L, 0x04042822L, }};
  172. static const uint32_t DES_SPtrans[8][64] = {
  173. {/* nibble 0 */
  174. 0x02080800L, 0x00080000L, 0x02000002L, 0x02080802L, 0x02000000L,
  175. 0x00080802L, 0x00080002L, 0x02000002L, 0x00080802L, 0x02080800L,
  176. 0x02080000L, 0x00000802L, 0x02000802L, 0x02000000L, 0x00000000L,
  177. 0x00080002L, 0x00080000L, 0x00000002L, 0x02000800L, 0x00080800L,
  178. 0x02080802L, 0x02080000L, 0x00000802L, 0x02000800L, 0x00000002L,
  179. 0x00000800L, 0x00080800L, 0x02080002L, 0x00000800L, 0x02000802L,
  180. 0x02080002L, 0x00000000L, 0x00000000L, 0x02080802L, 0x02000800L,
  181. 0x00080002L, 0x02080800L, 0x00080000L, 0x00000802L, 0x02000800L,
  182. 0x02080002L, 0x00000800L, 0x00080800L, 0x02000002L, 0x00080802L,
  183. 0x00000002L, 0x02000002L, 0x02080000L, 0x02080802L, 0x00080800L,
  184. 0x02080000L, 0x02000802L, 0x02000000L, 0x00000802L, 0x00080002L,
  185. 0x00000000L, 0x00080000L, 0x02000000L, 0x02000802L, 0x02080800L,
  186. 0x00000002L, 0x02080002L, 0x00000800L, 0x00080802L, },
  187. {/* nibble 1 */
  188. 0x40108010L, 0x00000000L, 0x00108000L, 0x40100000L, 0x40000010L,
  189. 0x00008010L, 0x40008000L, 0x00108000L, 0x00008000L, 0x40100010L,
  190. 0x00000010L, 0x40008000L, 0x00100010L, 0x40108000L, 0x40100000L,
  191. 0x00000010L, 0x00100000L, 0x40008010L, 0x40100010L, 0x00008000L,
  192. 0x00108010L, 0x40000000L, 0x00000000L, 0x00100010L, 0x40008010L,
  193. 0x00108010L, 0x40108000L, 0x40000010L, 0x40000000L, 0x00100000L,
  194. 0x00008010L, 0x40108010L, 0x00100010L, 0x40108000L, 0x40008000L,
  195. 0x00108010L, 0x40108010L, 0x00100010L, 0x40000010L, 0x00000000L,
  196. 0x40000000L, 0x00008010L, 0x00100000L, 0x40100010L, 0x00008000L,
  197. 0x40000000L, 0x00108010L, 0x40008010L, 0x40108000L, 0x00008000L,
  198. 0x00000000L, 0x40000010L, 0x00000010L, 0x40108010L, 0x00108000L,
  199. 0x40100000L, 0x40100010L, 0x00100000L, 0x00008010L, 0x40008000L,
  200. 0x40008010L, 0x00000010L, 0x40100000L, 0x00108000L, },
  201. {/* nibble 2 */
  202. 0x04000001L, 0x04040100L, 0x00000100L, 0x04000101L, 0x00040001L,
  203. 0x04000000L, 0x04000101L, 0x00040100L, 0x04000100L, 0x00040000L,
  204. 0x04040000L, 0x00000001L, 0x04040101L, 0x00000101L, 0x00000001L,
  205. 0x04040001L, 0x00000000L, 0x00040001L, 0x04040100L, 0x00000100L,
  206. 0x00000101L, 0x04040101L, 0x00040000L, 0x04000001L, 0x04040001L,
  207. 0x04000100L, 0x00040101L, 0x04040000L, 0x00040100L, 0x00000000L,
  208. 0x04000000L, 0x00040101L, 0x04040100L, 0x00000100L, 0x00000001L,
  209. 0x00040000L, 0x00000101L, 0x00040001L, 0x04040000L, 0x04000101L,
  210. 0x00000000L, 0x04040100L, 0x00040100L, 0x04040001L, 0x00040001L,
  211. 0x04000000L, 0x04040101L, 0x00000001L, 0x00040101L, 0x04000001L,
  212. 0x04000000L, 0x04040101L, 0x00040000L, 0x04000100L, 0x04000101L,
  213. 0x00040100L, 0x04000100L, 0x00000000L, 0x04040001L, 0x00000101L,
  214. 0x04000001L, 0x00040101L, 0x00000100L, 0x04040000L, },
  215. {/* nibble 3 */
  216. 0x00401008L, 0x10001000L, 0x00000008L, 0x10401008L, 0x00000000L,
  217. 0x10400000L, 0x10001008L, 0x00400008L, 0x10401000L, 0x10000008L,
  218. 0x10000000L, 0x00001008L, 0x10000008L, 0x00401008L, 0x00400000L,
  219. 0x10000000L, 0x10400008L, 0x00401000L, 0x00001000L, 0x00000008L,
  220. 0x00401000L, 0x10001008L, 0x10400000L, 0x00001000L, 0x00001008L,
  221. 0x00000000L, 0x00400008L, 0x10401000L, 0x10001000L, 0x10400008L,
  222. 0x10401008L, 0x00400000L, 0x10400008L, 0x00001008L, 0x00400000L,
  223. 0x10000008L, 0x00401000L, 0x10001000L, 0x00000008L, 0x10400000L,
  224. 0x10001008L, 0x00000000L, 0x00001000L, 0x00400008L, 0x00000000L,
  225. 0x10400008L, 0x10401000L, 0x00001000L, 0x10000000L, 0x10401008L,
  226. 0x00401008L, 0x00400000L, 0x10401008L, 0x00000008L, 0x10001000L,
  227. 0x00401008L, 0x00400008L, 0x00401000L, 0x10400000L, 0x10001008L,
  228. 0x00001008L, 0x10000000L, 0x10000008L, 0x10401000L, },
  229. {/* nibble 4 */
  230. 0x08000000L, 0x00010000L, 0x00000400L, 0x08010420L, 0x08010020L,
  231. 0x08000400L, 0x00010420L, 0x08010000L, 0x00010000L, 0x00000020L,
  232. 0x08000020L, 0x00010400L, 0x08000420L, 0x08010020L, 0x08010400L,
  233. 0x00000000L, 0x00010400L, 0x08000000L, 0x00010020L, 0x00000420L,
  234. 0x08000400L, 0x00010420L, 0x00000000L, 0x08000020L, 0x00000020L,
  235. 0x08000420L, 0x08010420L, 0x00010020L, 0x08010000L, 0x00000400L,
  236. 0x00000420L, 0x08010400L, 0x08010400L, 0x08000420L, 0x00010020L,
  237. 0x08010000L, 0x00010000L, 0x00000020L, 0x08000020L, 0x08000400L,
  238. 0x08000000L, 0x00010400L, 0x08010420L, 0x00000000L, 0x00010420L,
  239. 0x08000000L, 0x00000400L, 0x00010020L, 0x08000420L, 0x00000400L,
  240. 0x00000000L, 0x08010420L, 0x08010020L, 0x08010400L, 0x00000420L,
  241. 0x00010000L, 0x00010400L, 0x08010020L, 0x08000400L, 0x00000420L,
  242. 0x00000020L, 0x00010420L, 0x08010000L, 0x08000020L, },
  243. {/* nibble 5 */
  244. 0x80000040L, 0x00200040L, 0x00000000L, 0x80202000L, 0x00200040L,
  245. 0x00002000L, 0x80002040L, 0x00200000L, 0x00002040L, 0x80202040L,
  246. 0x00202000L, 0x80000000L, 0x80002000L, 0x80000040L, 0x80200000L,
  247. 0x00202040L, 0x00200000L, 0x80002040L, 0x80200040L, 0x00000000L,
  248. 0x00002000L, 0x00000040L, 0x80202000L, 0x80200040L, 0x80202040L,
  249. 0x80200000L, 0x80000000L, 0x00002040L, 0x00000040L, 0x00202000L,
  250. 0x00202040L, 0x80002000L, 0x00002040L, 0x80000000L, 0x80002000L,
  251. 0x00202040L, 0x80202000L, 0x00200040L, 0x00000000L, 0x80002000L,
  252. 0x80000000L, 0x00002000L, 0x80200040L, 0x00200000L, 0x00200040L,
  253. 0x80202040L, 0x00202000L, 0x00000040L, 0x80202040L, 0x00202000L,
  254. 0x00200000L, 0x80002040L, 0x80000040L, 0x80200000L, 0x00202040L,
  255. 0x00000000L, 0x00002000L, 0x80000040L, 0x80002040L, 0x80202000L,
  256. 0x80200000L, 0x00002040L, 0x00000040L, 0x80200040L, },
  257. {/* nibble 6 */
  258. 0x00004000L, 0x00000200L, 0x01000200L, 0x01000004L, 0x01004204L,
  259. 0x00004004L, 0x00004200L, 0x00000000L, 0x01000000L, 0x01000204L,
  260. 0x00000204L, 0x01004000L, 0x00000004L, 0x01004200L, 0x01004000L,
  261. 0x00000204L, 0x01000204L, 0x00004000L, 0x00004004L, 0x01004204L,
  262. 0x00000000L, 0x01000200L, 0x01000004L, 0x00004200L, 0x01004004L,
  263. 0x00004204L, 0x01004200L, 0x00000004L, 0x00004204L, 0x01004004L,
  264. 0x00000200L, 0x01000000L, 0x00004204L, 0x01004000L, 0x01004004L,
  265. 0x00000204L, 0x00004000L, 0x00000200L, 0x01000000L, 0x01004004L,
  266. 0x01000204L, 0x00004204L, 0x00004200L, 0x00000000L, 0x00000200L,
  267. 0x01000004L, 0x00000004L, 0x01000200L, 0x00000000L, 0x01000204L,
  268. 0x01000200L, 0x00004200L, 0x00000204L, 0x00004000L, 0x01004204L,
  269. 0x01000000L, 0x01004200L, 0x00000004L, 0x00004004L, 0x01004204L,
  270. 0x01000004L, 0x01004200L, 0x01004000L, 0x00004004L, },
  271. {/* nibble 7 */
  272. 0x20800080L, 0x20820000L, 0x00020080L, 0x00000000L, 0x20020000L,
  273. 0x00800080L, 0x20800000L, 0x20820080L, 0x00000080L, 0x20000000L,
  274. 0x00820000L, 0x00020080L, 0x00820080L, 0x20020080L, 0x20000080L,
  275. 0x20800000L, 0x00020000L, 0x00820080L, 0x00800080L, 0x20020000L,
  276. 0x20820080L, 0x20000080L, 0x00000000L, 0x00820000L, 0x20000000L,
  277. 0x00800000L, 0x20020080L, 0x20800080L, 0x00800000L, 0x00020000L,
  278. 0x20820000L, 0x00000080L, 0x00800000L, 0x00020000L, 0x20000080L,
  279. 0x20820080L, 0x00020080L, 0x20000000L, 0x00000000L, 0x00820000L,
  280. 0x20800080L, 0x20020080L, 0x20020000L, 0x00800080L, 0x20820000L,
  281. 0x00000080L, 0x00800080L, 0x20020000L, 0x20820080L, 0x00800000L,
  282. 0x20800000L, 0x20000080L, 0x00820000L, 0x00020080L, 0x20020080L,
  283. 0x20800000L, 0x00000080L, 0x20820000L, 0x00820080L, 0x00000000L,
  284. 0x20000000L, 0x20800080L, 0x00020000L, 0x00820080L, }};
  285. #define HPERM_OP(a, t, n, m) \
  286. ((t) = ((((a) << (16 - (n))) ^ (a)) & (m)), \
  287. (a) = (a) ^ (t) ^ (t >> (16 - (n))))
  288. void DES_set_key(const DES_cblock *key, DES_key_schedule *schedule) {
  289. static const int shifts2[16] = {0, 0, 1, 1, 1, 1, 1, 1,
  290. 0, 1, 1, 1, 1, 1, 1, 0};
  291. uint32_t c, d, t, s, t2;
  292. const uint8_t *in;
  293. uint32_t *k;
  294. int i;
  295. k = &schedule->ks->deslong[0];
  296. in = key->bytes;
  297. c2l(in, c);
  298. c2l(in, d);
  299. /* do PC1 in 47 simple operations :-)
  300. * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
  301. * for the inspiration. :-) */
  302. PERM_OP(d, c, t, 4, 0x0f0f0f0fL);
  303. HPERM_OP(c, t, -2, 0xcccc0000L);
  304. HPERM_OP(d, t, -2, 0xcccc0000L);
  305. PERM_OP(d, c, t, 1, 0x55555555L);
  306. PERM_OP(c, d, t, 8, 0x00ff00ffL);
  307. PERM_OP(d, c, t, 1, 0x55555555L);
  308. d = (((d & 0x000000ffL) << 16L) | (d & 0x0000ff00L) |
  309. ((d & 0x00ff0000L) >> 16L) | ((c & 0xf0000000L) >> 4L));
  310. c &= 0x0fffffffL;
  311. for (i = 0; i < ITERATIONS; i++) {
  312. if (shifts2[i]) {
  313. c = ((c >> 2L) | (c << 26L));
  314. d = ((d >> 2L) | (d << 26L));
  315. } else {
  316. c = ((c >> 1L) | (c << 27L));
  317. d = ((d >> 1L) | (d << 27L));
  318. }
  319. c &= 0x0fffffffL;
  320. d &= 0x0fffffffL;
  321. /* could be a few less shifts but I am to lazy at this
  322. * point in time to investigate */
  323. s = des_skb[0][(c) & 0x3f] |
  324. des_skb[1][((c >> 6L) & 0x03) | ((c >> 7L) & 0x3c)] |
  325. des_skb[2][((c >> 13L) & 0x0f) | ((c >> 14L) & 0x30)] |
  326. des_skb[3][((c >> 20L) & 0x01) | ((c >> 21L) & 0x06) |
  327. ((c >> 22L) & 0x38)];
  328. t = des_skb[4][(d) & 0x3f] |
  329. des_skb[5][((d >> 7L) & 0x03) | ((d >> 8L) & 0x3c)] |
  330. des_skb[6][(d >> 15L) & 0x3f] |
  331. des_skb[7][((d >> 21L) & 0x0f) | ((d >> 22L) & 0x30)];
  332. /* table contained 0213 4657 */
  333. t2 = ((t << 16L) | (s & 0x0000ffffL)) & 0xffffffffL;
  334. *(k++) = ROTATE(t2, 30) & 0xffffffffL;
  335. t2 = ((s >> 16L) | (t & 0xffff0000L));
  336. *(k++) = ROTATE(t2, 26) & 0xffffffffL;
  337. }
  338. }
  339. static const uint8_t kOddParity[256] = {
  340. 1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14,
  341. 14, 16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28,
  342. 31, 31, 32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44,
  343. 44, 47, 47, 49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59,
  344. 61, 61, 62, 62, 64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74,
  345. 74, 76, 76, 79, 79, 81, 81, 82, 82, 84, 84, 87, 87, 88, 88,
  346. 91, 91, 93, 93, 94, 94, 97, 97, 98, 98, 100, 100, 103, 103, 104,
  347. 104, 107, 107, 109, 109, 110, 110, 112, 112, 115, 115, 117, 117, 118, 118,
  348. 121, 121, 122, 122, 124, 124, 127, 127, 128, 128, 131, 131, 133, 133, 134,
  349. 134, 137, 137, 138, 138, 140, 140, 143, 143, 145, 145, 146, 146, 148, 148,
  350. 151, 151, 152, 152, 155, 155, 157, 157, 158, 158, 161, 161, 162, 162, 164,
  351. 164, 167, 167, 168, 168, 171, 171, 173, 173, 174, 174, 176, 176, 179, 179,
  352. 181, 181, 182, 182, 185, 185, 186, 186, 188, 188, 191, 191, 193, 193, 194,
  353. 194, 196, 196, 199, 199, 200, 200, 203, 203, 205, 205, 206, 206, 208, 208,
  354. 211, 211, 213, 213, 214, 214, 217, 217, 218, 218, 220, 220, 223, 223, 224,
  355. 224, 227, 227, 229, 229, 230, 230, 233, 233, 234, 234, 236, 236, 239, 239,
  356. 241, 241, 242, 242, 244, 244, 247, 247, 248, 248, 251, 251, 253, 253, 254,
  357. 254
  358. };
  359. void DES_set_odd_parity(DES_cblock *key) {
  360. unsigned i;
  361. for (i = 0; i < DES_KEY_SZ; i++) {
  362. key->bytes[i] = kOddParity[key->bytes[i]];
  363. }
  364. }
  365. static void DES_encrypt1(uint32_t *data, const DES_key_schedule *ks, int enc) {
  366. uint32_t l, r, t, u;
  367. const uint32_t *s;
  368. r = data[0];
  369. l = data[1];
  370. IP(r, l);
  371. /* Things have been modified so that the initial rotate is done outside
  372. * the loop. This required the DES_SPtrans values in sp.h to be
  373. * rotated 1 bit to the right. One perl script later and things have a
  374. * 5% speed up on a sparc2. Thanks to Richard Outerbridge
  375. * <71755.204@CompuServe.COM> for pointing this out. */
  376. /* clear the top bits on machines with 8byte longs */
  377. /* shift left by 2 */
  378. r = ROTATE(r, 29) & 0xffffffffL;
  379. l = ROTATE(l, 29) & 0xffffffffL;
  380. s = ks->ks->deslong;
  381. /* I don't know if it is worth the effort of loop unrolling the
  382. * inner loop */
  383. if (enc) {
  384. D_ENCRYPT(l, r, 0); /* 1 */
  385. D_ENCRYPT(r, l, 2); /* 2 */
  386. D_ENCRYPT(l, r, 4); /* 3 */
  387. D_ENCRYPT(r, l, 6); /* 4 */
  388. D_ENCRYPT(l, r, 8); /* 5 */
  389. D_ENCRYPT(r, l, 10); /* 6 */
  390. D_ENCRYPT(l, r, 12); /* 7 */
  391. D_ENCRYPT(r, l, 14); /* 8 */
  392. D_ENCRYPT(l, r, 16); /* 9 */
  393. D_ENCRYPT(r, l, 18); /* 10 */
  394. D_ENCRYPT(l, r, 20); /* 11 */
  395. D_ENCRYPT(r, l, 22); /* 12 */
  396. D_ENCRYPT(l, r, 24); /* 13 */
  397. D_ENCRYPT(r, l, 26); /* 14 */
  398. D_ENCRYPT(l, r, 28); /* 15 */
  399. D_ENCRYPT(r, l, 30); /* 16 */
  400. } else {
  401. D_ENCRYPT(l, r, 30); /* 16 */
  402. D_ENCRYPT(r, l, 28); /* 15 */
  403. D_ENCRYPT(l, r, 26); /* 14 */
  404. D_ENCRYPT(r, l, 24); /* 13 */
  405. D_ENCRYPT(l, r, 22); /* 12 */
  406. D_ENCRYPT(r, l, 20); /* 11 */
  407. D_ENCRYPT(l, r, 18); /* 10 */
  408. D_ENCRYPT(r, l, 16); /* 9 */
  409. D_ENCRYPT(l, r, 14); /* 8 */
  410. D_ENCRYPT(r, l, 12); /* 7 */
  411. D_ENCRYPT(l, r, 10); /* 6 */
  412. D_ENCRYPT(r, l, 8); /* 5 */
  413. D_ENCRYPT(l, r, 6); /* 4 */
  414. D_ENCRYPT(r, l, 4); /* 3 */
  415. D_ENCRYPT(l, r, 2); /* 2 */
  416. D_ENCRYPT(r, l, 0); /* 1 */
  417. }
  418. /* rotate and clear the top bits on machines with 8byte longs */
  419. l = ROTATE(l, 3) & 0xffffffffL;
  420. r = ROTATE(r, 3) & 0xffffffffL;
  421. FP(r, l);
  422. data[0] = l;
  423. data[1] = r;
  424. }
  425. static void DES_encrypt2(uint32_t *data, const DES_key_schedule *ks, int enc) {
  426. uint32_t l, r, t, u;
  427. const uint32_t *s;
  428. r = data[0];
  429. l = data[1];
  430. /* Things have been modified so that the initial rotate is done outside the
  431. * loop. This required the DES_SPtrans values in sp.h to be rotated 1 bit to
  432. * the right. One perl script later and things have a 5% speed up on a
  433. * sparc2. Thanks to Richard Outerbridge <71755.204@CompuServe.COM> for
  434. * pointing this out. */
  435. /* clear the top bits on machines with 8byte longs */
  436. r = ROTATE(r, 29) & 0xffffffffL;
  437. l = ROTATE(l, 29) & 0xffffffffL;
  438. s = ks->ks->deslong;
  439. /* I don't know if it is worth the effort of loop unrolling the
  440. * inner loop */
  441. if (enc) {
  442. D_ENCRYPT(l, r, 0); /* 1 */
  443. D_ENCRYPT(r, l, 2); /* 2 */
  444. D_ENCRYPT(l, r, 4); /* 3 */
  445. D_ENCRYPT(r, l, 6); /* 4 */
  446. D_ENCRYPT(l, r, 8); /* 5 */
  447. D_ENCRYPT(r, l, 10); /* 6 */
  448. D_ENCRYPT(l, r, 12); /* 7 */
  449. D_ENCRYPT(r, l, 14); /* 8 */
  450. D_ENCRYPT(l, r, 16); /* 9 */
  451. D_ENCRYPT(r, l, 18); /* 10 */
  452. D_ENCRYPT(l, r, 20); /* 11 */
  453. D_ENCRYPT(r, l, 22); /* 12 */
  454. D_ENCRYPT(l, r, 24); /* 13 */
  455. D_ENCRYPT(r, l, 26); /* 14 */
  456. D_ENCRYPT(l, r, 28); /* 15 */
  457. D_ENCRYPT(r, l, 30); /* 16 */
  458. } else {
  459. D_ENCRYPT(l, r, 30); /* 16 */
  460. D_ENCRYPT(r, l, 28); /* 15 */
  461. D_ENCRYPT(l, r, 26); /* 14 */
  462. D_ENCRYPT(r, l, 24); /* 13 */
  463. D_ENCRYPT(l, r, 22); /* 12 */
  464. D_ENCRYPT(r, l, 20); /* 11 */
  465. D_ENCRYPT(l, r, 18); /* 10 */
  466. D_ENCRYPT(r, l, 16); /* 9 */
  467. D_ENCRYPT(l, r, 14); /* 8 */
  468. D_ENCRYPT(r, l, 12); /* 7 */
  469. D_ENCRYPT(l, r, 10); /* 6 */
  470. D_ENCRYPT(r, l, 8); /* 5 */
  471. D_ENCRYPT(l, r, 6); /* 4 */
  472. D_ENCRYPT(r, l, 4); /* 3 */
  473. D_ENCRYPT(l, r, 2); /* 2 */
  474. D_ENCRYPT(r, l, 0); /* 1 */
  475. }
  476. /* rotate and clear the top bits on machines with 8byte longs */
  477. data[0] = ROTATE(l, 3) & 0xffffffffL;
  478. data[1] = ROTATE(r, 3) & 0xffffffffL;
  479. }
  480. static void DES_encrypt3(uint32_t *data, const DES_key_schedule *ks1,
  481. const DES_key_schedule *ks2,
  482. const DES_key_schedule *ks3) {
  483. uint32_t l, r;
  484. l = data[0];
  485. r = data[1];
  486. IP(l, r);
  487. data[0] = l;
  488. data[1] = r;
  489. DES_encrypt2((uint32_t *)data, ks1, DES_ENCRYPT);
  490. DES_encrypt2((uint32_t *)data, ks2, DES_DECRYPT);
  491. DES_encrypt2((uint32_t *)data, ks3, DES_ENCRYPT);
  492. l = data[0];
  493. r = data[1];
  494. FP(r, l);
  495. data[0] = l;
  496. data[1] = r;
  497. }
  498. static void DES_decrypt3(uint32_t *data, const DES_key_schedule *ks1,
  499. const DES_key_schedule *ks2,
  500. const DES_key_schedule *ks3) {
  501. uint32_t l, r;
  502. l = data[0];
  503. r = data[1];
  504. IP(l, r);
  505. data[0] = l;
  506. data[1] = r;
  507. DES_encrypt2((uint32_t *)data, ks3, DES_DECRYPT);
  508. DES_encrypt2((uint32_t *)data, ks2, DES_ENCRYPT);
  509. DES_encrypt2((uint32_t *)data, ks1, DES_DECRYPT);
  510. l = data[0];
  511. r = data[1];
  512. FP(r, l);
  513. data[0] = l;
  514. data[1] = r;
  515. }
  516. void DES_ecb_encrypt(const DES_cblock *in_block, DES_cblock *out_block,
  517. const DES_key_schedule *schedule, int is_encrypt) {
  518. uint32_t l;
  519. uint32_t ll[2];
  520. const uint8_t *in = in_block->bytes;
  521. uint8_t *out = out_block->bytes;
  522. c2l(in, l);
  523. ll[0] = l;
  524. c2l(in, l);
  525. ll[1] = l;
  526. DES_encrypt1(ll, schedule, is_encrypt);
  527. l = ll[0];
  528. l2c(l, out);
  529. l = ll[1];
  530. l2c(l, out);
  531. ll[0] = ll[1] = 0;
  532. }
  533. void DES_ncbc_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  534. const DES_key_schedule *schedule, DES_cblock *ivec,
  535. int enc) {
  536. uint32_t tin0, tin1;
  537. uint32_t tout0, tout1, xor0, xor1;
  538. uint32_t tin[2];
  539. unsigned char *iv;
  540. iv = ivec->bytes;
  541. if (enc) {
  542. c2l(iv, tout0);
  543. c2l(iv, tout1);
  544. for (; len >= 8; len -= 8) {
  545. c2l(in, tin0);
  546. c2l(in, tin1);
  547. tin0 ^= tout0;
  548. tin[0] = tin0;
  549. tin1 ^= tout1;
  550. tin[1] = tin1;
  551. DES_encrypt1((uint32_t *)tin, schedule, DES_ENCRYPT);
  552. tout0 = tin[0];
  553. l2c(tout0, out);
  554. tout1 = tin[1];
  555. l2c(tout1, out);
  556. }
  557. if (len != 0) {
  558. c2ln(in, tin0, tin1, len);
  559. tin0 ^= tout0;
  560. tin[0] = tin0;
  561. tin1 ^= tout1;
  562. tin[1] = tin1;
  563. DES_encrypt1((uint32_t *)tin, schedule, DES_ENCRYPT);
  564. tout0 = tin[0];
  565. l2c(tout0, out);
  566. tout1 = tin[1];
  567. l2c(tout1, out);
  568. }
  569. iv = ivec->bytes;
  570. l2c(tout0, iv);
  571. l2c(tout1, iv);
  572. } else {
  573. c2l(iv, xor0);
  574. c2l(iv, xor1);
  575. for (; len >= 8; len -= 8) {
  576. c2l(in, tin0);
  577. tin[0] = tin0;
  578. c2l(in, tin1);
  579. tin[1] = tin1;
  580. DES_encrypt1((uint32_t *)tin, schedule, DES_DECRYPT);
  581. tout0 = tin[0] ^ xor0;
  582. tout1 = tin[1] ^ xor1;
  583. l2c(tout0, out);
  584. l2c(tout1, out);
  585. xor0 = tin0;
  586. xor1 = tin1;
  587. }
  588. if (len != 0) {
  589. c2l(in, tin0);
  590. tin[0] = tin0;
  591. c2l(in, tin1);
  592. tin[1] = tin1;
  593. DES_encrypt1((uint32_t *)tin, schedule, DES_DECRYPT);
  594. tout0 = tin[0] ^ xor0;
  595. tout1 = tin[1] ^ xor1;
  596. l2cn(tout0, tout1, out, len);
  597. xor0 = tin0;
  598. xor1 = tin1;
  599. }
  600. iv = ivec->bytes;
  601. l2c(xor0, iv);
  602. l2c(xor1, iv);
  603. }
  604. tin[0] = tin[1] = 0;
  605. }
  606. void DES_ecb3_encrypt(const DES_cblock *input, DES_cblock *output,
  607. const DES_key_schedule *ks1, const DES_key_schedule *ks2,
  608. const DES_key_schedule *ks3, int enc) {
  609. uint32_t l0, l1;
  610. uint32_t ll[2];
  611. const uint8_t *in = input->bytes;
  612. uint8_t *out = output->bytes;
  613. c2l(in, l0);
  614. c2l(in, l1);
  615. ll[0] = l0;
  616. ll[1] = l1;
  617. if (enc) {
  618. DES_encrypt3(ll, ks1, ks2, ks3);
  619. } else {
  620. DES_decrypt3(ll, ks1, ks2, ks3);
  621. }
  622. l0 = ll[0];
  623. l1 = ll[1];
  624. l2c(l0, out);
  625. l2c(l1, out);
  626. }
  627. void DES_ede3_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  628. const DES_key_schedule *ks1,
  629. const DES_key_schedule *ks2,
  630. const DES_key_schedule *ks3, DES_cblock *ivec,
  631. int enc) {
  632. uint32_t tin0, tin1;
  633. uint32_t tout0, tout1, xor0, xor1;
  634. uint32_t tin[2];
  635. uint8_t *iv;
  636. iv = ivec->bytes;
  637. if (enc) {
  638. c2l(iv, tout0);
  639. c2l(iv, tout1);
  640. for (; len >= 8; len -= 8) {
  641. c2l(in, tin0);
  642. c2l(in, tin1);
  643. tin0 ^= tout0;
  644. tin1 ^= tout1;
  645. tin[0] = tin0;
  646. tin[1] = tin1;
  647. DES_encrypt3((uint32_t *)tin, ks1, ks2, ks3);
  648. tout0 = tin[0];
  649. tout1 = tin[1];
  650. l2c(tout0, out);
  651. l2c(tout1, out);
  652. }
  653. if (len != 0) {
  654. c2ln(in, tin0, tin1, len);
  655. tin0 ^= tout0;
  656. tin1 ^= tout1;
  657. tin[0] = tin0;
  658. tin[1] = tin1;
  659. DES_encrypt3((uint32_t *)tin, ks1, ks2, ks3);
  660. tout0 = tin[0];
  661. tout1 = tin[1];
  662. l2c(tout0, out);
  663. l2c(tout1, out);
  664. }
  665. iv = ivec->bytes;
  666. l2c(tout0, iv);
  667. l2c(tout1, iv);
  668. } else {
  669. uint32_t t0, t1;
  670. c2l(iv, xor0);
  671. c2l(iv, xor1);
  672. for (; len >= 8; len -= 8) {
  673. c2l(in, tin0);
  674. c2l(in, tin1);
  675. t0 = tin0;
  676. t1 = tin1;
  677. tin[0] = tin0;
  678. tin[1] = tin1;
  679. DES_decrypt3((uint32_t *)tin, ks1, ks2, ks3);
  680. tout0 = tin[0];
  681. tout1 = tin[1];
  682. tout0 ^= xor0;
  683. tout1 ^= xor1;
  684. l2c(tout0, out);
  685. l2c(tout1, out);
  686. xor0 = t0;
  687. xor1 = t1;
  688. }
  689. if (len != 0) {
  690. c2l(in, tin0);
  691. c2l(in, tin1);
  692. t0 = tin0;
  693. t1 = tin1;
  694. tin[0] = tin0;
  695. tin[1] = tin1;
  696. DES_decrypt3((uint32_t *)tin, ks1, ks2, ks3);
  697. tout0 = tin[0];
  698. tout1 = tin[1];
  699. tout0 ^= xor0;
  700. tout1 ^= xor1;
  701. l2cn(tout0, tout1, out, len);
  702. xor0 = t0;
  703. xor1 = t1;
  704. }
  705. iv = ivec->bytes;
  706. l2c(xor0, iv);
  707. l2c(xor1, iv);
  708. }
  709. tin[0] = tin[1] = 0;
  710. }
  711. void DES_ede2_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  712. const DES_key_schedule *ks1,
  713. const DES_key_schedule *ks2,
  714. DES_cblock *ivec,
  715. int enc) {
  716. DES_ede3_cbc_encrypt(in, out, len, ks1, ks2, ks1, ivec, enc);
  717. }