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

des.c 29 KiB

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