des.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Cryptographic API.
  3. *
  4. * DES & Triple DES EDE Cipher Algorithms.
  5. *
  6. * Copyright (c) 2005 Dag Arne Osvik <da@osvik.no>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. */
  14. #include <asm/byteorder.h>
  15. #include <linux/bitops.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/crypto.h>
  20. #include <linux/types.h>
  21. #define DES_KEY_SIZE 8
  22. #define DES_EXPKEY_WORDS 32
  23. #define DES_BLOCK_SIZE 8
  24. #define DES3_EDE_KEY_SIZE (3 * DES_KEY_SIZE)
  25. #define DES3_EDE_EXPKEY_WORDS (3 * DES_EXPKEY_WORDS)
  26. #define DES3_EDE_BLOCK_SIZE DES_BLOCK_SIZE
  27. #define ROL(x, r) ((x) = rol32((x), (r)))
  28. #define ROR(x, r) ((x) = ror32((x), (r)))
  29. struct des_ctx {
  30. u32 expkey[DES_EXPKEY_WORDS];
  31. };
  32. struct des3_ede_ctx {
  33. u32 expkey[DES3_EDE_EXPKEY_WORDS];
  34. };
  35. /* Lookup tables for key expansion */
  36. static const u8 pc1[256] = {
  37. 0x00, 0x00, 0x40, 0x04, 0x10, 0x10, 0x50, 0x14,
  38. 0x04, 0x40, 0x44, 0x44, 0x14, 0x50, 0x54, 0x54,
  39. 0x02, 0x02, 0x42, 0x06, 0x12, 0x12, 0x52, 0x16,
  40. 0x06, 0x42, 0x46, 0x46, 0x16, 0x52, 0x56, 0x56,
  41. 0x80, 0x08, 0xc0, 0x0c, 0x90, 0x18, 0xd0, 0x1c,
  42. 0x84, 0x48, 0xc4, 0x4c, 0x94, 0x58, 0xd4, 0x5c,
  43. 0x82, 0x0a, 0xc2, 0x0e, 0x92, 0x1a, 0xd2, 0x1e,
  44. 0x86, 0x4a, 0xc6, 0x4e, 0x96, 0x5a, 0xd6, 0x5e,
  45. 0x20, 0x20, 0x60, 0x24, 0x30, 0x30, 0x70, 0x34,
  46. 0x24, 0x60, 0x64, 0x64, 0x34, 0x70, 0x74, 0x74,
  47. 0x22, 0x22, 0x62, 0x26, 0x32, 0x32, 0x72, 0x36,
  48. 0x26, 0x62, 0x66, 0x66, 0x36, 0x72, 0x76, 0x76,
  49. 0xa0, 0x28, 0xe0, 0x2c, 0xb0, 0x38, 0xf0, 0x3c,
  50. 0xa4, 0x68, 0xe4, 0x6c, 0xb4, 0x78, 0xf4, 0x7c,
  51. 0xa2, 0x2a, 0xe2, 0x2e, 0xb2, 0x3a, 0xf2, 0x3e,
  52. 0xa6, 0x6a, 0xe6, 0x6e, 0xb6, 0x7a, 0xf6, 0x7e,
  53. 0x08, 0x80, 0x48, 0x84, 0x18, 0x90, 0x58, 0x94,
  54. 0x0c, 0xc0, 0x4c, 0xc4, 0x1c, 0xd0, 0x5c, 0xd4,
  55. 0x0a, 0x82, 0x4a, 0x86, 0x1a, 0x92, 0x5a, 0x96,
  56. 0x0e, 0xc2, 0x4e, 0xc6, 0x1e, 0xd2, 0x5e, 0xd6,
  57. 0x88, 0x88, 0xc8, 0x8c, 0x98, 0x98, 0xd8, 0x9c,
  58. 0x8c, 0xc8, 0xcc, 0xcc, 0x9c, 0xd8, 0xdc, 0xdc,
  59. 0x8a, 0x8a, 0xca, 0x8e, 0x9a, 0x9a, 0xda, 0x9e,
  60. 0x8e, 0xca, 0xce, 0xce, 0x9e, 0xda, 0xde, 0xde,
  61. 0x28, 0xa0, 0x68, 0xa4, 0x38, 0xb0, 0x78, 0xb4,
  62. 0x2c, 0xe0, 0x6c, 0xe4, 0x3c, 0xf0, 0x7c, 0xf4,
  63. 0x2a, 0xa2, 0x6a, 0xa6, 0x3a, 0xb2, 0x7a, 0xb6,
  64. 0x2e, 0xe2, 0x6e, 0xe6, 0x3e, 0xf2, 0x7e, 0xf6,
  65. 0xa8, 0xa8, 0xe8, 0xac, 0xb8, 0xb8, 0xf8, 0xbc,
  66. 0xac, 0xe8, 0xec, 0xec, 0xbc, 0xf8, 0xfc, 0xfc,
  67. 0xaa, 0xaa, 0xea, 0xae, 0xba, 0xba, 0xfa, 0xbe,
  68. 0xae, 0xea, 0xee, 0xee, 0xbe, 0xfa, 0xfe, 0xfe
  69. };
  70. static const u8 rs[256] = {
  71. 0x00, 0x00, 0x80, 0x80, 0x02, 0x02, 0x82, 0x82,
  72. 0x04, 0x04, 0x84, 0x84, 0x06, 0x06, 0x86, 0x86,
  73. 0x08, 0x08, 0x88, 0x88, 0x0a, 0x0a, 0x8a, 0x8a,
  74. 0x0c, 0x0c, 0x8c, 0x8c, 0x0e, 0x0e, 0x8e, 0x8e,
  75. 0x10, 0x10, 0x90, 0x90, 0x12, 0x12, 0x92, 0x92,
  76. 0x14, 0x14, 0x94, 0x94, 0x16, 0x16, 0x96, 0x96,
  77. 0x18, 0x18, 0x98, 0x98, 0x1a, 0x1a, 0x9a, 0x9a,
  78. 0x1c, 0x1c, 0x9c, 0x9c, 0x1e, 0x1e, 0x9e, 0x9e,
  79. 0x20, 0x20, 0xa0, 0xa0, 0x22, 0x22, 0xa2, 0xa2,
  80. 0x24, 0x24, 0xa4, 0xa4, 0x26, 0x26, 0xa6, 0xa6,
  81. 0x28, 0x28, 0xa8, 0xa8, 0x2a, 0x2a, 0xaa, 0xaa,
  82. 0x2c, 0x2c, 0xac, 0xac, 0x2e, 0x2e, 0xae, 0xae,
  83. 0x30, 0x30, 0xb0, 0xb0, 0x32, 0x32, 0xb2, 0xb2,
  84. 0x34, 0x34, 0xb4, 0xb4, 0x36, 0x36, 0xb6, 0xb6,
  85. 0x38, 0x38, 0xb8, 0xb8, 0x3a, 0x3a, 0xba, 0xba,
  86. 0x3c, 0x3c, 0xbc, 0xbc, 0x3e, 0x3e, 0xbe, 0xbe,
  87. 0x40, 0x40, 0xc0, 0xc0, 0x42, 0x42, 0xc2, 0xc2,
  88. 0x44, 0x44, 0xc4, 0xc4, 0x46, 0x46, 0xc6, 0xc6,
  89. 0x48, 0x48, 0xc8, 0xc8, 0x4a, 0x4a, 0xca, 0xca,
  90. 0x4c, 0x4c, 0xcc, 0xcc, 0x4e, 0x4e, 0xce, 0xce,
  91. 0x50, 0x50, 0xd0, 0xd0, 0x52, 0x52, 0xd2, 0xd2,
  92. 0x54, 0x54, 0xd4, 0xd4, 0x56, 0x56, 0xd6, 0xd6,
  93. 0x58, 0x58, 0xd8, 0xd8, 0x5a, 0x5a, 0xda, 0xda,
  94. 0x5c, 0x5c, 0xdc, 0xdc, 0x5e, 0x5e, 0xde, 0xde,
  95. 0x60, 0x60, 0xe0, 0xe0, 0x62, 0x62, 0xe2, 0xe2,
  96. 0x64, 0x64, 0xe4, 0xe4, 0x66, 0x66, 0xe6, 0xe6,
  97. 0x68, 0x68, 0xe8, 0xe8, 0x6a, 0x6a, 0xea, 0xea,
  98. 0x6c, 0x6c, 0xec, 0xec, 0x6e, 0x6e, 0xee, 0xee,
  99. 0x70, 0x70, 0xf0, 0xf0, 0x72, 0x72, 0xf2, 0xf2,
  100. 0x74, 0x74, 0xf4, 0xf4, 0x76, 0x76, 0xf6, 0xf6,
  101. 0x78, 0x78, 0xf8, 0xf8, 0x7a, 0x7a, 0xfa, 0xfa,
  102. 0x7c, 0x7c, 0xfc, 0xfc, 0x7e, 0x7e, 0xfe, 0xfe
  103. };
  104. static const u32 pc2[1024] = {
  105. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  106. 0x00040000, 0x00000000, 0x04000000, 0x00100000,
  107. 0x00400000, 0x00000008, 0x00000800, 0x40000000,
  108. 0x00440000, 0x00000008, 0x04000800, 0x40100000,
  109. 0x00000400, 0x00000020, 0x08000000, 0x00000100,
  110. 0x00040400, 0x00000020, 0x0c000000, 0x00100100,
  111. 0x00400400, 0x00000028, 0x08000800, 0x40000100,
  112. 0x00440400, 0x00000028, 0x0c000800, 0x40100100,
  113. 0x80000000, 0x00000010, 0x00000000, 0x00800000,
  114. 0x80040000, 0x00000010, 0x04000000, 0x00900000,
  115. 0x80400000, 0x00000018, 0x00000800, 0x40800000,
  116. 0x80440000, 0x00000018, 0x04000800, 0x40900000,
  117. 0x80000400, 0x00000030, 0x08000000, 0x00800100,
  118. 0x80040400, 0x00000030, 0x0c000000, 0x00900100,
  119. 0x80400400, 0x00000038, 0x08000800, 0x40800100,
  120. 0x80440400, 0x00000038, 0x0c000800, 0x40900100,
  121. 0x10000000, 0x00000000, 0x00200000, 0x00001000,
  122. 0x10040000, 0x00000000, 0x04200000, 0x00101000,
  123. 0x10400000, 0x00000008, 0x00200800, 0x40001000,
  124. 0x10440000, 0x00000008, 0x04200800, 0x40101000,
  125. 0x10000400, 0x00000020, 0x08200000, 0x00001100,
  126. 0x10040400, 0x00000020, 0x0c200000, 0x00101100,
  127. 0x10400400, 0x00000028, 0x08200800, 0x40001100,
  128. 0x10440400, 0x00000028, 0x0c200800, 0x40101100,
  129. 0x90000000, 0x00000010, 0x00200000, 0x00801000,
  130. 0x90040000, 0x00000010, 0x04200000, 0x00901000,
  131. 0x90400000, 0x00000018, 0x00200800, 0x40801000,
  132. 0x90440000, 0x00000018, 0x04200800, 0x40901000,
  133. 0x90000400, 0x00000030, 0x08200000, 0x00801100,
  134. 0x90040400, 0x00000030, 0x0c200000, 0x00901100,
  135. 0x90400400, 0x00000038, 0x08200800, 0x40801100,
  136. 0x90440400, 0x00000038, 0x0c200800, 0x40901100,
  137. 0x00000200, 0x00080000, 0x00000000, 0x00000004,
  138. 0x00040200, 0x00080000, 0x04000000, 0x00100004,
  139. 0x00400200, 0x00080008, 0x00000800, 0x40000004,
  140. 0x00440200, 0x00080008, 0x04000800, 0x40100004,
  141. 0x00000600, 0x00080020, 0x08000000, 0x00000104,
  142. 0x00040600, 0x00080020, 0x0c000000, 0x00100104,
  143. 0x00400600, 0x00080028, 0x08000800, 0x40000104,
  144. 0x00440600, 0x00080028, 0x0c000800, 0x40100104,
  145. 0x80000200, 0x00080010, 0x00000000, 0x00800004,
  146. 0x80040200, 0x00080010, 0x04000000, 0x00900004,
  147. 0x80400200, 0x00080018, 0x00000800, 0x40800004,
  148. 0x80440200, 0x00080018, 0x04000800, 0x40900004,
  149. 0x80000600, 0x00080030, 0x08000000, 0x00800104,
  150. 0x80040600, 0x00080030, 0x0c000000, 0x00900104,
  151. 0x80400600, 0x00080038, 0x08000800, 0x40800104,
  152. 0x80440600, 0x00080038, 0x0c000800, 0x40900104,
  153. 0x10000200, 0x00080000, 0x00200000, 0x00001004,
  154. 0x10040200, 0x00080000, 0x04200000, 0x00101004,
  155. 0x10400200, 0x00080008, 0x00200800, 0x40001004,
  156. 0x10440200, 0x00080008, 0x04200800, 0x40101004,
  157. 0x10000600, 0x00080020, 0x08200000, 0x00001104,
  158. 0x10040600, 0x00080020, 0x0c200000, 0x00101104,
  159. 0x10400600, 0x00080028, 0x08200800, 0x40001104,
  160. 0x10440600, 0x00080028, 0x0c200800, 0x40101104,
  161. 0x90000200, 0x00080010, 0x00200000, 0x00801004,
  162. 0x90040200, 0x00080010, 0x04200000, 0x00901004,
  163. 0x90400200, 0x00080018, 0x00200800, 0x40801004,
  164. 0x90440200, 0x00080018, 0x04200800, 0x40901004,
  165. 0x90000600, 0x00080030, 0x08200000, 0x00801104,
  166. 0x90040600, 0x00080030, 0x0c200000, 0x00901104,
  167. 0x90400600, 0x00080038, 0x08200800, 0x40801104,
  168. 0x90440600, 0x00080038, 0x0c200800, 0x40901104,
  169. 0x00000002, 0x00002000, 0x20000000, 0x00000001,
  170. 0x00040002, 0x00002000, 0x24000000, 0x00100001,
  171. 0x00400002, 0x00002008, 0x20000800, 0x40000001,
  172. 0x00440002, 0x00002008, 0x24000800, 0x40100001,
  173. 0x00000402, 0x00002020, 0x28000000, 0x00000101,
  174. 0x00040402, 0x00002020, 0x2c000000, 0x00100101,
  175. 0x00400402, 0x00002028, 0x28000800, 0x40000101,
  176. 0x00440402, 0x00002028, 0x2c000800, 0x40100101,
  177. 0x80000002, 0x00002010, 0x20000000, 0x00800001,
  178. 0x80040002, 0x00002010, 0x24000000, 0x00900001,
  179. 0x80400002, 0x00002018, 0x20000800, 0x40800001,
  180. 0x80440002, 0x00002018, 0x24000800, 0x40900001,
  181. 0x80000402, 0x00002030, 0x28000000, 0x00800101,
  182. 0x80040402, 0x00002030, 0x2c000000, 0x00900101,
  183. 0x80400402, 0x00002038, 0x28000800, 0x40800101,
  184. 0x80440402, 0x00002038, 0x2c000800, 0x40900101,
  185. 0x10000002, 0x00002000, 0x20200000, 0x00001001,
  186. 0x10040002, 0x00002000, 0x24200000, 0x00101001,
  187. 0x10400002, 0x00002008, 0x20200800, 0x40001001,
  188. 0x10440002, 0x00002008, 0x24200800, 0x40101001,
  189. 0x10000402, 0x00002020, 0x28200000, 0x00001101,
  190. 0x10040402, 0x00002020, 0x2c200000, 0x00101101,
  191. 0x10400402, 0x00002028, 0x28200800, 0x40001101,
  192. 0x10440402, 0x00002028, 0x2c200800, 0x40101101,
  193. 0x90000002, 0x00002010, 0x20200000, 0x00801001,
  194. 0x90040002, 0x00002010, 0x24200000, 0x00901001,
  195. 0x90400002, 0x00002018, 0x20200800, 0x40801001,
  196. 0x90440002, 0x00002018, 0x24200800, 0x40901001,
  197. 0x90000402, 0x00002030, 0x28200000, 0x00801101,
  198. 0x90040402, 0x00002030, 0x2c200000, 0x00901101,
  199. 0x90400402, 0x00002038, 0x28200800, 0x40801101,
  200. 0x90440402, 0x00002038, 0x2c200800, 0x40901101,
  201. 0x00000202, 0x00082000, 0x20000000, 0x00000005,
  202. 0x00040202, 0x00082000, 0x24000000, 0x00100005,
  203. 0x00400202, 0x00082008, 0x20000800, 0x40000005,
  204. 0x00440202, 0x00082008, 0x24000800, 0x40100005,
  205. 0x00000602, 0x00082020, 0x28000000, 0x00000105,
  206. 0x00040602, 0x00082020, 0x2c000000, 0x00100105,
  207. 0x00400602, 0x00082028, 0x28000800, 0x40000105,
  208. 0x00440602, 0x00082028, 0x2c000800, 0x40100105,
  209. 0x80000202, 0x00082010, 0x20000000, 0x00800005,
  210. 0x80040202, 0x00082010, 0x24000000, 0x00900005,
  211. 0x80400202, 0x00082018, 0x20000800, 0x40800005,
  212. 0x80440202, 0x00082018, 0x24000800, 0x40900005,
  213. 0x80000602, 0x00082030, 0x28000000, 0x00800105,
  214. 0x80040602, 0x00082030, 0x2c000000, 0x00900105,
  215. 0x80400602, 0x00082038, 0x28000800, 0x40800105,
  216. 0x80440602, 0x00082038, 0x2c000800, 0x40900105,
  217. 0x10000202, 0x00082000, 0x20200000, 0x00001005,
  218. 0x10040202, 0x00082000, 0x24200000, 0x00101005,
  219. 0x10400202, 0x00082008, 0x20200800, 0x40001005,
  220. 0x10440202, 0x00082008, 0x24200800, 0x40101005,
  221. 0x10000602, 0x00082020, 0x28200000, 0x00001105,
  222. 0x10040602, 0x00082020, 0x2c200000, 0x00101105,
  223. 0x10400602, 0x00082028, 0x28200800, 0x40001105,
  224. 0x10440602, 0x00082028, 0x2c200800, 0x40101105,
  225. 0x90000202, 0x00082010, 0x20200000, 0x00801005,
  226. 0x90040202, 0x00082010, 0x24200000, 0x00901005,
  227. 0x90400202, 0x00082018, 0x20200800, 0x40801005,
  228. 0x90440202, 0x00082018, 0x24200800, 0x40901005,
  229. 0x90000602, 0x00082030, 0x28200000, 0x00801105,
  230. 0x90040602, 0x00082030, 0x2c200000, 0x00901105,
  231. 0x90400602, 0x00082038, 0x28200800, 0x40801105,
  232. 0x90440602, 0x00082038, 0x2c200800, 0x40901105,
  233. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  234. 0x00000000, 0x00000008, 0x00080000, 0x10000000,
  235. 0x02000000, 0x00000000, 0x00000080, 0x00001000,
  236. 0x02000000, 0x00000008, 0x00080080, 0x10001000,
  237. 0x00004000, 0x00000000, 0x00000040, 0x00040000,
  238. 0x00004000, 0x00000008, 0x00080040, 0x10040000,
  239. 0x02004000, 0x00000000, 0x000000c0, 0x00041000,
  240. 0x02004000, 0x00000008, 0x000800c0, 0x10041000,
  241. 0x00020000, 0x00008000, 0x08000000, 0x00200000,
  242. 0x00020000, 0x00008008, 0x08080000, 0x10200000,
  243. 0x02020000, 0x00008000, 0x08000080, 0x00201000,
  244. 0x02020000, 0x00008008, 0x08080080, 0x10201000,
  245. 0x00024000, 0x00008000, 0x08000040, 0x00240000,
  246. 0x00024000, 0x00008008, 0x08080040, 0x10240000,
  247. 0x02024000, 0x00008000, 0x080000c0, 0x00241000,
  248. 0x02024000, 0x00008008, 0x080800c0, 0x10241000,
  249. 0x00000000, 0x01000000, 0x00002000, 0x00000020,
  250. 0x00000000, 0x01000008, 0x00082000, 0x10000020,
  251. 0x02000000, 0x01000000, 0x00002080, 0x00001020,
  252. 0x02000000, 0x01000008, 0x00082080, 0x10001020,
  253. 0x00004000, 0x01000000, 0x00002040, 0x00040020,
  254. 0x00004000, 0x01000008, 0x00082040, 0x10040020,
  255. 0x02004000, 0x01000000, 0x000020c0, 0x00041020,
  256. 0x02004000, 0x01000008, 0x000820c0, 0x10041020,
  257. 0x00020000, 0x01008000, 0x08002000, 0x00200020,
  258. 0x00020000, 0x01008008, 0x08082000, 0x10200020,
  259. 0x02020000, 0x01008000, 0x08002080, 0x00201020,
  260. 0x02020000, 0x01008008, 0x08082080, 0x10201020,
  261. 0x00024000, 0x01008000, 0x08002040, 0x00240020,
  262. 0x00024000, 0x01008008, 0x08082040, 0x10240020,
  263. 0x02024000, 0x01008000, 0x080020c0, 0x00241020,
  264. 0x02024000, 0x01008008, 0x080820c0, 0x10241020,
  265. 0x00000400, 0x04000000, 0x00100000, 0x00000004,
  266. 0x00000400, 0x04000008, 0x00180000, 0x10000004,
  267. 0x02000400, 0x04000000, 0x00100080, 0x00001004,
  268. 0x02000400, 0x04000008, 0x00180080, 0x10001004,
  269. 0x00004400, 0x04000000, 0x00100040, 0x00040004,
  270. 0x00004400, 0x04000008, 0x00180040, 0x10040004,
  271. 0x02004400, 0x04000000, 0x001000c0, 0x00041004,
  272. 0x02004400, 0x04000008, 0x001800c0, 0x10041004,
  273. 0x00020400, 0x04008000, 0x08100000, 0x00200004,
  274. 0x00020400, 0x04008008, 0x08180000, 0x10200004,
  275. 0x02020400, 0x04008000, 0x08100080, 0x00201004,
  276. 0x02020400, 0x04008008, 0x08180080, 0x10201004,
  277. 0x00024400, 0x04008000, 0x08100040, 0x00240004,
  278. 0x00024400, 0x04008008, 0x08180040, 0x10240004,
  279. 0x02024400, 0x04008000, 0x081000c0, 0x00241004,
  280. 0x02024400, 0x04008008, 0x081800c0, 0x10241004,
  281. 0x00000400, 0x05000000, 0x00102000, 0x00000024,
  282. 0x00000400, 0x05000008, 0x00182000, 0x10000024,
  283. 0x02000400, 0x05000000, 0x00102080, 0x00001024,
  284. 0x02000400, 0x05000008, 0x00182080, 0x10001024,
  285. 0x00004400, 0x05000000, 0x00102040, 0x00040024,
  286. 0x00004400, 0x05000008, 0x00182040, 0x10040024,
  287. 0x02004400, 0x05000000, 0x001020c0, 0x00041024,
  288. 0x02004400, 0x05000008, 0x001820c0, 0x10041024,
  289. 0x00020400, 0x05008000, 0x08102000, 0x00200024,
  290. 0x00020400, 0x05008008, 0x08182000, 0x10200024,
  291. 0x02020400, 0x05008000, 0x08102080, 0x00201024,
  292. 0x02020400, 0x05008008, 0x08182080, 0x10201024,
  293. 0x00024400, 0x05008000, 0x08102040, 0x00240024,
  294. 0x00024400, 0x05008008, 0x08182040, 0x10240024,
  295. 0x02024400, 0x05008000, 0x081020c0, 0x00241024,
  296. 0x02024400, 0x05008008, 0x081820c0, 0x10241024,
  297. 0x00000800, 0x00010000, 0x20000000, 0x00000010,
  298. 0x00000800, 0x00010008, 0x20080000, 0x10000010,
  299. 0x02000800, 0x00010000, 0x20000080, 0x00001010,
  300. 0x02000800, 0x00010008, 0x20080080, 0x10001010,
  301. 0x00004800, 0x00010000, 0x20000040, 0x00040010,
  302. 0x00004800, 0x00010008, 0x20080040, 0x10040010,
  303. 0x02004800, 0x00010000, 0x200000c0, 0x00041010,
  304. 0x02004800, 0x00010008, 0x200800c0, 0x10041010,
  305. 0x00020800, 0x00018000, 0x28000000, 0x00200010,
  306. 0x00020800, 0x00018008, 0x28080000, 0x10200010,
  307. 0x02020800, 0x00018000, 0x28000080, 0x00201010,
  308. 0x02020800, 0x00018008, 0x28080080, 0x10201010,
  309. 0x00024800, 0x00018000, 0x28000040, 0x00240010,
  310. 0x00024800, 0x00018008, 0x28080040, 0x10240010,
  311. 0x02024800, 0x00018000, 0x280000c0, 0x00241010,
  312. 0x02024800, 0x00018008, 0x280800c0, 0x10241010,
  313. 0x00000800, 0x01010000, 0x20002000, 0x00000030,
  314. 0x00000800, 0x01010008, 0x20082000, 0x10000030,
  315. 0x02000800, 0x01010000, 0x20002080, 0x00001030,
  316. 0x02000800, 0x01010008, 0x20082080, 0x10001030,
  317. 0x00004800, 0x01010000, 0x20002040, 0x00040030,
  318. 0x00004800, 0x01010008, 0x20082040, 0x10040030,
  319. 0x02004800, 0x01010000, 0x200020c0, 0x00041030,
  320. 0x02004800, 0x01010008, 0x200820c0, 0x10041030,
  321. 0x00020800, 0x01018000, 0x28002000, 0x00200030,
  322. 0x00020800, 0x01018008, 0x28082000, 0x10200030,
  323. 0x02020800, 0x01018000, 0x28002080, 0x00201030,
  324. 0x02020800, 0x01018008, 0x28082080, 0x10201030,
  325. 0x00024800, 0x01018000, 0x28002040, 0x00240030,
  326. 0x00024800, 0x01018008, 0x28082040, 0x10240030,
  327. 0x02024800, 0x01018000, 0x280020c0, 0x00241030,
  328. 0x02024800, 0x01018008, 0x280820c0, 0x10241030,
  329. 0x00000c00, 0x04010000, 0x20100000, 0x00000014,
  330. 0x00000c00, 0x04010008, 0x20180000, 0x10000014,
  331. 0x02000c00, 0x04010000, 0x20100080, 0x00001014,
  332. 0x02000c00, 0x04010008, 0x20180080, 0x10001014,
  333. 0x00004c00, 0x04010000, 0x20100040, 0x00040014,
  334. 0x00004c00, 0x04010008, 0x20180040, 0x10040014,
  335. 0x02004c00, 0x04010000, 0x201000c0, 0x00041014,
  336. 0x02004c00, 0x04010008, 0x201800c0, 0x10041014,
  337. 0x00020c00, 0x04018000, 0x28100000, 0x00200014,
  338. 0x00020c00, 0x04018008, 0x28180000, 0x10200014,
  339. 0x02020c00, 0x04018000, 0x28100080, 0x00201014,
  340. 0x02020c00, 0x04018008, 0x28180080, 0x10201014,
  341. 0x00024c00, 0x04018000, 0x28100040, 0x00240014,
  342. 0x00024c00, 0x04018008, 0x28180040, 0x10240014,
  343. 0x02024c00, 0x04018000, 0x281000c0, 0x00241014,
  344. 0x02024c00, 0x04018008, 0x281800c0, 0x10241014,
  345. 0x00000c00, 0x05010000, 0x20102000, 0x00000034,
  346. 0x00000c00, 0x05010008, 0x20182000, 0x10000034,
  347. 0x02000c00, 0x05010000, 0x20102080, 0x00001034,
  348. 0x02000c00, 0x05010008, 0x20182080, 0x10001034,
  349. 0x00004c00, 0x05010000, 0x20102040, 0x00040034,
  350. 0x00004c00, 0x05010008, 0x20182040, 0x10040034,
  351. 0x02004c00, 0x05010000, 0x201020c0, 0x00041034,
  352. 0x02004c00, 0x05010008, 0x201820c0, 0x10041034,
  353. 0x00020c00, 0x05018000, 0x28102000, 0x00200034,
  354. 0x00020c00, 0x05018008, 0x28182000, 0x10200034,
  355. 0x02020c00, 0x05018000, 0x28102080, 0x00201034,
  356. 0x02020c00, 0x05018008, 0x28182080, 0x10201034,
  357. 0x00024c00, 0x05018000, 0x28102040, 0x00240034,
  358. 0x00024c00, 0x05018008, 0x28182040, 0x10240034,
  359. 0x02024c00, 0x05018000, 0x281020c0, 0x00241034,
  360. 0x02024c00, 0x05018008, 0x281820c0, 0x10241034
  361. };
  362. /* S-box lookup tables */
  363. static const u32 S1[64] = {
  364. 0x01010400, 0x00000000, 0x00010000, 0x01010404,
  365. 0x01010004, 0x00010404, 0x00000004, 0x00010000,
  366. 0x00000400, 0x01010400, 0x01010404, 0x00000400,
  367. 0x01000404, 0x01010004, 0x01000000, 0x00000004,
  368. 0x00000404, 0x01000400, 0x01000400, 0x00010400,
  369. 0x00010400, 0x01010000, 0x01010000, 0x01000404,
  370. 0x00010004, 0x01000004, 0x01000004, 0x00010004,
  371. 0x00000000, 0x00000404, 0x00010404, 0x01000000,
  372. 0x00010000, 0x01010404, 0x00000004, 0x01010000,
  373. 0x01010400, 0x01000000, 0x01000000, 0x00000400,
  374. 0x01010004, 0x00010000, 0x00010400, 0x01000004,
  375. 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  376. 0x01010404, 0x00010004, 0x01010000, 0x01000404,
  377. 0x01000004, 0x00000404, 0x00010404, 0x01010400,
  378. 0x00000404, 0x01000400, 0x01000400, 0x00000000,
  379. 0x00010004, 0x00010400, 0x00000000, 0x01010004
  380. };
  381. static const u32 S2[64] = {
  382. 0x80108020, 0x80008000, 0x00008000, 0x00108020,
  383. 0x00100000, 0x00000020, 0x80100020, 0x80008020,
  384. 0x80000020, 0x80108020, 0x80108000, 0x80000000,
  385. 0x80008000, 0x00100000, 0x00000020, 0x80100020,
  386. 0x00108000, 0x00100020, 0x80008020, 0x00000000,
  387. 0x80000000, 0x00008000, 0x00108020, 0x80100000,
  388. 0x00100020, 0x80000020, 0x00000000, 0x00108000,
  389. 0x00008020, 0x80108000, 0x80100000, 0x00008020,
  390. 0x00000000, 0x00108020, 0x80100020, 0x00100000,
  391. 0x80008020, 0x80100000, 0x80108000, 0x00008000,
  392. 0x80100000, 0x80008000, 0x00000020, 0x80108020,
  393. 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  394. 0x00008020, 0x80108000, 0x00100000, 0x80000020,
  395. 0x00100020, 0x80008020, 0x80000020, 0x00100020,
  396. 0x00108000, 0x00000000, 0x80008000, 0x00008020,
  397. 0x80000000, 0x80100020, 0x80108020, 0x00108000
  398. };
  399. static const u32 S3[64] = {
  400. 0x00000208, 0x08020200, 0x00000000, 0x08020008,
  401. 0x08000200, 0x00000000, 0x00020208, 0x08000200,
  402. 0x00020008, 0x08000008, 0x08000008, 0x00020000,
  403. 0x08020208, 0x00020008, 0x08020000, 0x00000208,
  404. 0x08000000, 0x00000008, 0x08020200, 0x00000200,
  405. 0x00020200, 0x08020000, 0x08020008, 0x00020208,
  406. 0x08000208, 0x00020200, 0x00020000, 0x08000208,
  407. 0x00000008, 0x08020208, 0x00000200, 0x08000000,
  408. 0x08020200, 0x08000000, 0x00020008, 0x00000208,
  409. 0x00020000, 0x08020200, 0x08000200, 0x00000000,
  410. 0x00000200, 0x00020008, 0x08020208, 0x08000200,
  411. 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  412. 0x08000208, 0x00020000, 0x08000000, 0x08020208,
  413. 0x00000008, 0x00020208, 0x00020200, 0x08000008,
  414. 0x08020000, 0x08000208, 0x00000208, 0x08020000,
  415. 0x00020208, 0x00000008, 0x08020008, 0x00020200
  416. };
  417. static const u32 S4[64] = {
  418. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  419. 0x00802080, 0x00800081, 0x00800001, 0x00002001,
  420. 0x00000000, 0x00802000, 0x00802000, 0x00802081,
  421. 0x00000081, 0x00000000, 0x00800080, 0x00800001,
  422. 0x00000001, 0x00002000, 0x00800000, 0x00802001,
  423. 0x00000080, 0x00800000, 0x00002001, 0x00002080,
  424. 0x00800081, 0x00000001, 0x00002080, 0x00800080,
  425. 0x00002000, 0x00802080, 0x00802081, 0x00000081,
  426. 0x00800080, 0x00800001, 0x00802000, 0x00802081,
  427. 0x00000081, 0x00000000, 0x00000000, 0x00802000,
  428. 0x00002080, 0x00800080, 0x00800081, 0x00000001,
  429. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  430. 0x00802081, 0x00000081, 0x00000001, 0x00002000,
  431. 0x00800001, 0x00002001, 0x00802080, 0x00800081,
  432. 0x00002001, 0x00002080, 0x00800000, 0x00802001,
  433. 0x00000080, 0x00800000, 0x00002000, 0x00802080
  434. };
  435. static const u32 S5[64] = {
  436. 0x00000100, 0x02080100, 0x02080000, 0x42000100,
  437. 0x00080000, 0x00000100, 0x40000000, 0x02080000,
  438. 0x40080100, 0x00080000, 0x02000100, 0x40080100,
  439. 0x42000100, 0x42080000, 0x00080100, 0x40000000,
  440. 0x02000000, 0x40080000, 0x40080000, 0x00000000,
  441. 0x40000100, 0x42080100, 0x42080100, 0x02000100,
  442. 0x42080000, 0x40000100, 0x00000000, 0x42000000,
  443. 0x02080100, 0x02000000, 0x42000000, 0x00080100,
  444. 0x00080000, 0x42000100, 0x00000100, 0x02000000,
  445. 0x40000000, 0x02080000, 0x42000100, 0x40080100,
  446. 0x02000100, 0x40000000, 0x42080000, 0x02080100,
  447. 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  448. 0x42080100, 0x00080100, 0x42000000, 0x42080100,
  449. 0x02080000, 0x00000000, 0x40080000, 0x42000000,
  450. 0x00080100, 0x02000100, 0x40000100, 0x00080000,
  451. 0x00000000, 0x40080000, 0x02080100, 0x40000100
  452. };
  453. static const u32 S6[64] = {
  454. 0x20000010, 0x20400000, 0x00004000, 0x20404010,
  455. 0x20400000, 0x00000010, 0x20404010, 0x00400000,
  456. 0x20004000, 0x00404010, 0x00400000, 0x20000010,
  457. 0x00400010, 0x20004000, 0x20000000, 0x00004010,
  458. 0x00000000, 0x00400010, 0x20004010, 0x00004000,
  459. 0x00404000, 0x20004010, 0x00000010, 0x20400010,
  460. 0x20400010, 0x00000000, 0x00404010, 0x20404000,
  461. 0x00004010, 0x00404000, 0x20404000, 0x20000000,
  462. 0x20004000, 0x00000010, 0x20400010, 0x00404000,
  463. 0x20404010, 0x00400000, 0x00004010, 0x20000010,
  464. 0x00400000, 0x20004000, 0x20000000, 0x00004010,
  465. 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  466. 0x00404010, 0x20404000, 0x00000000, 0x20400010,
  467. 0x00000010, 0x00004000, 0x20400000, 0x00404010,
  468. 0x00004000, 0x00400010, 0x20004010, 0x00000000,
  469. 0x20404000, 0x20000000, 0x00400010, 0x20004010
  470. };
  471. static const u32 S7[64] = {
  472. 0x00200000, 0x04200002, 0x04000802, 0x00000000,
  473. 0x00000800, 0x04000802, 0x00200802, 0x04200800,
  474. 0x04200802, 0x00200000, 0x00000000, 0x04000002,
  475. 0x00000002, 0x04000000, 0x04200002, 0x00000802,
  476. 0x04000800, 0x00200802, 0x00200002, 0x04000800,
  477. 0x04000002, 0x04200000, 0x04200800, 0x00200002,
  478. 0x04200000, 0x00000800, 0x00000802, 0x04200802,
  479. 0x00200800, 0x00000002, 0x04000000, 0x00200800,
  480. 0x04000000, 0x00200800, 0x00200000, 0x04000802,
  481. 0x04000802, 0x04200002, 0x04200002, 0x00000002,
  482. 0x00200002, 0x04000000, 0x04000800, 0x00200000,
  483. 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  484. 0x00000802, 0x04000002, 0x04200802, 0x04200000,
  485. 0x00200800, 0x00000000, 0x00000002, 0x04200802,
  486. 0x00000000, 0x00200802, 0x04200000, 0x00000800,
  487. 0x04000002, 0x04000800, 0x00000800, 0x00200002
  488. };
  489. static const u32 S8[64] = {
  490. 0x10001040, 0x00001000, 0x00040000, 0x10041040,
  491. 0x10000000, 0x10001040, 0x00000040, 0x10000000,
  492. 0x00040040, 0x10040000, 0x10041040, 0x00041000,
  493. 0x10041000, 0x00041040, 0x00001000, 0x00000040,
  494. 0x10040000, 0x10000040, 0x10001000, 0x00001040,
  495. 0x00041000, 0x00040040, 0x10040040, 0x10041000,
  496. 0x00001040, 0x00000000, 0x00000000, 0x10040040,
  497. 0x10000040, 0x10001000, 0x00041040, 0x00040000,
  498. 0x00041040, 0x00040000, 0x10041000, 0x00001000,
  499. 0x00000040, 0x10040040, 0x00001000, 0x00041040,
  500. 0x10001000, 0x00000040, 0x10000040, 0x10040000,
  501. 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  502. 0x00000000, 0x10041040, 0x00040040, 0x10000040,
  503. 0x10040000, 0x10001000, 0x10001040, 0x00000000,
  504. 0x10041040, 0x00041000, 0x00041000, 0x00001040,
  505. 0x00001040, 0x00040040, 0x10000000, 0x10041000
  506. };
  507. /* Encryption components: IP, FP, and round function */
  508. #define IP(L, R, T) \
  509. ROL(R, 4); \
  510. T = L; \
  511. L ^= R; \
  512. L &= 0xf0f0f0f0; \
  513. R ^= L; \
  514. L ^= T; \
  515. ROL(R, 12); \
  516. T = L; \
  517. L ^= R; \
  518. L &= 0xffff0000; \
  519. R ^= L; \
  520. L ^= T; \
  521. ROR(R, 14); \
  522. T = L; \
  523. L ^= R; \
  524. L &= 0xcccccccc; \
  525. R ^= L; \
  526. L ^= T; \
  527. ROL(R, 6); \
  528. T = L; \
  529. L ^= R; \
  530. L &= 0xff00ff00; \
  531. R ^= L; \
  532. L ^= T; \
  533. ROR(R, 7); \
  534. T = L; \
  535. L ^= R; \
  536. L &= 0xaaaaaaaa; \
  537. R ^= L; \
  538. L ^= T; \
  539. ROL(L, 1);
  540. #define FP(L, R, T) \
  541. ROR(L, 1); \
  542. T = L; \
  543. L ^= R; \
  544. L &= 0xaaaaaaaa; \
  545. R ^= L; \
  546. L ^= T; \
  547. ROL(R, 7); \
  548. T = L; \
  549. L ^= R; \
  550. L &= 0xff00ff00; \
  551. R ^= L; \
  552. L ^= T; \
  553. ROR(R, 6); \
  554. T = L; \
  555. L ^= R; \
  556. L &= 0xcccccccc; \
  557. R ^= L; \
  558. L ^= T; \
  559. ROL(R, 14); \
  560. T = L; \
  561. L ^= R; \
  562. L &= 0xffff0000; \
  563. R ^= L; \
  564. L ^= T; \
  565. ROR(R, 12); \
  566. T = L; \
  567. L ^= R; \
  568. L &= 0xf0f0f0f0; \
  569. R ^= L; \
  570. L ^= T; \
  571. ROR(R, 4);
  572. #define ROUND(L, R, A, B, K, d) \
  573. B = K[0]; A = K[1]; K += d; \
  574. B ^= R; A ^= R; \
  575. B &= 0x3f3f3f3f; ROR(A, 4); \
  576. L ^= S8[0xff & B]; A &= 0x3f3f3f3f; \
  577. L ^= S6[0xff & (B >> 8)]; B >>= 16; \
  578. L ^= S7[0xff & A]; \
  579. L ^= S5[0xff & (A >> 8)]; A >>= 16; \
  580. L ^= S4[0xff & B]; \
  581. L ^= S2[0xff & (B >> 8)]; \
  582. L ^= S3[0xff & A]; \
  583. L ^= S1[0xff & (A >> 8)];
  584. /*
  585. * PC2 lookup tables are organized as 2 consecutive sets of 4 interleaved
  586. * tables of 128 elements. One set is for C_i and the other for D_i, while
  587. * the 4 interleaved tables correspond to four 7-bit subsets of C_i or D_i.
  588. *
  589. * After PC1 each of the variables a,b,c,d contains a 7 bit subset of C_i
  590. * or D_i in bits 7-1 (bit 0 being the least significant).
  591. */
  592. #define T1(x) pt[2 * (x) + 0]
  593. #define T2(x) pt[2 * (x) + 1]
  594. #define T3(x) pt[2 * (x) + 2]
  595. #define T4(x) pt[2 * (x) + 3]
  596. #define PC2(a, b, c, d) (T4(d) | T3(c) | T2(b) | T1(a))
  597. /*
  598. * Encryption key expansion
  599. *
  600. * RFC2451: Weak key checks SHOULD be performed.
  601. *
  602. * FIPS 74:
  603. *
  604. * Keys having duals are keys which produce all zeros, all ones, or
  605. * alternating zero-one patterns in the C and D registers after Permuted
  606. * Choice 1 has operated on the key.
  607. *
  608. */
  609. static unsigned long ekey(u32 *pe, const u8 *k)
  610. {
  611. /* K&R: long is at least 32 bits */
  612. unsigned long a, b, c, d, w;
  613. const u32 *pt = pc2;
  614. d = k[4]; d &= 0x0e; d <<= 4; d |= k[0] & 0x1e; d = pc1[d];
  615. c = k[5]; c &= 0x0e; c <<= 4; c |= k[1] & 0x1e; c = pc1[c];
  616. b = k[6]; b &= 0x0e; b <<= 4; b |= k[2] & 0x1e; b = pc1[b];
  617. a = k[7]; a &= 0x0e; a <<= 4; a |= k[3] & 0x1e; a = pc1[a];
  618. pe[15 * 2 + 0] = PC2(a, b, c, d); d = rs[d];
  619. pe[14 * 2 + 0] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  620. pe[13 * 2 + 0] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  621. pe[12 * 2 + 0] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  622. pe[11 * 2 + 0] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  623. pe[10 * 2 + 0] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  624. pe[ 9 * 2 + 0] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  625. pe[ 8 * 2 + 0] = PC2(d, a, b, c); c = rs[c];
  626. pe[ 7 * 2 + 0] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  627. pe[ 6 * 2 + 0] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  628. pe[ 5 * 2 + 0] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  629. pe[ 4 * 2 + 0] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  630. pe[ 3 * 2 + 0] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  631. pe[ 2 * 2 + 0] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  632. pe[ 1 * 2 + 0] = PC2(c, d, a, b); b = rs[b];
  633. pe[ 0 * 2 + 0] = PC2(b, c, d, a);
  634. /* Check if first half is weak */
  635. w = (a ^ c) | (b ^ d) | (rs[a] ^ c) | (b ^ rs[d]);
  636. /* Skip to next table set */
  637. pt += 512;
  638. d = k[0]; d &= 0xe0; d >>= 4; d |= k[4] & 0xf0; d = pc1[d + 1];
  639. c = k[1]; c &= 0xe0; c >>= 4; c |= k[5] & 0xf0; c = pc1[c + 1];
  640. b = k[2]; b &= 0xe0; b >>= 4; b |= k[6] & 0xf0; b = pc1[b + 1];
  641. a = k[3]; a &= 0xe0; a >>= 4; a |= k[7] & 0xf0; a = pc1[a + 1];
  642. /* Check if second half is weak */
  643. w |= (a ^ c) | (b ^ d) | (rs[a] ^ c) | (b ^ rs[d]);
  644. pe[15 * 2 + 1] = PC2(a, b, c, d); d = rs[d];
  645. pe[14 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  646. pe[13 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  647. pe[12 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  648. pe[11 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  649. pe[10 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  650. pe[ 9 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  651. pe[ 8 * 2 + 1] = PC2(d, a, b, c); c = rs[c];
  652. pe[ 7 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  653. pe[ 6 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  654. pe[ 5 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  655. pe[ 4 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  656. pe[ 3 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  657. pe[ 2 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  658. pe[ 1 * 2 + 1] = PC2(c, d, a, b); b = rs[b];
  659. pe[ 0 * 2 + 1] = PC2(b, c, d, a);
  660. /* Fixup: 2413 5768 -> 1357 2468 */
  661. for (d = 0; d < 16; ++d) {
  662. a = pe[2 * d];
  663. b = pe[2 * d + 1];
  664. c = a ^ b;
  665. c &= 0xffff0000;
  666. a ^= c;
  667. b ^= c;
  668. ROL(b, 18);
  669. pe[2 * d] = a;
  670. pe[2 * d + 1] = b;
  671. }
  672. /* Zero if weak key */
  673. return w;
  674. }
  675. /*
  676. * Decryption key expansion
  677. *
  678. * No weak key checking is performed, as this is only used by triple DES
  679. *
  680. */
  681. static void dkey(u32 *pe, const u8 *k)
  682. {
  683. /* K&R: long is at least 32 bits */
  684. unsigned long a, b, c, d;
  685. const u32 *pt = pc2;
  686. d = k[4]; d &= 0x0e; d <<= 4; d |= k[0] & 0x1e; d = pc1[d];
  687. c = k[5]; c &= 0x0e; c <<= 4; c |= k[1] & 0x1e; c = pc1[c];
  688. b = k[6]; b &= 0x0e; b <<= 4; b |= k[2] & 0x1e; b = pc1[b];
  689. a = k[7]; a &= 0x0e; a <<= 4; a |= k[3] & 0x1e; a = pc1[a];
  690. pe[ 0 * 2] = PC2(a, b, c, d); d = rs[d];
  691. pe[ 1 * 2] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  692. pe[ 2 * 2] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  693. pe[ 3 * 2] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  694. pe[ 4 * 2] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  695. pe[ 5 * 2] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  696. pe[ 6 * 2] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  697. pe[ 7 * 2] = PC2(d, a, b, c); c = rs[c];
  698. pe[ 8 * 2] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  699. pe[ 9 * 2] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  700. pe[10 * 2] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  701. pe[11 * 2] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  702. pe[12 * 2] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  703. pe[13 * 2] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  704. pe[14 * 2] = PC2(c, d, a, b); b = rs[b];
  705. pe[15 * 2] = PC2(b, c, d, a);
  706. /* Skip to next table set */
  707. pt += 512;
  708. d = k[0]; d &= 0xe0; d >>= 4; d |= k[4] & 0xf0; d = pc1[d + 1];
  709. c = k[1]; c &= 0xe0; c >>= 4; c |= k[5] & 0xf0; c = pc1[c + 1];
  710. b = k[2]; b &= 0xe0; b >>= 4; b |= k[6] & 0xf0; b = pc1[b + 1];
  711. a = k[3]; a &= 0xe0; a >>= 4; a |= k[7] & 0xf0; a = pc1[a + 1];
  712. pe[ 0 * 2 + 1] = PC2(a, b, c, d); d = rs[d];
  713. pe[ 1 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  714. pe[ 2 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  715. pe[ 3 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  716. pe[ 4 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  717. pe[ 5 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
  718. pe[ 6 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
  719. pe[ 7 * 2 + 1] = PC2(d, a, b, c); c = rs[c];
  720. pe[ 8 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  721. pe[ 9 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  722. pe[10 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  723. pe[11 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  724. pe[12 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
  725. pe[13 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
  726. pe[14 * 2 + 1] = PC2(c, d, a, b); b = rs[b];
  727. pe[15 * 2 + 1] = PC2(b, c, d, a);
  728. /* Fixup: 2413 5768 -> 1357 2468 */
  729. for (d = 0; d < 16; ++d) {
  730. a = pe[2 * d];
  731. b = pe[2 * d + 1];
  732. c = a ^ b;
  733. c &= 0xffff0000;
  734. a ^= c;
  735. b ^= c;
  736. ROL(b, 18);
  737. pe[2 * d] = a;
  738. pe[2 * d + 1] = b;
  739. }
  740. }
  741. static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
  742. unsigned int keylen)
  743. {
  744. struct des_ctx *dctx = crypto_tfm_ctx(tfm);
  745. u32 *flags = &tfm->crt_flags;
  746. u32 tmp[DES_EXPKEY_WORDS];
  747. int ret;
  748. /* Expand to tmp */
  749. ret = ekey(tmp, key);
  750. if (unlikely(ret == 0) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  751. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  752. return -EINVAL;
  753. }
  754. /* Copy to output */
  755. memcpy(dctx->expkey, tmp, sizeof(dctx->expkey));
  756. return 0;
  757. }
  758. static void des_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  759. {
  760. struct des_ctx *ctx = crypto_tfm_ctx(tfm);
  761. const u32 *K = ctx->expkey;
  762. const __le32 *s = (const __le32 *)src;
  763. __le32 *d = (__le32 *)dst;
  764. u32 L, R, A, B;
  765. int i;
  766. L = le32_to_cpu(s[0]);
  767. R = le32_to_cpu(s[1]);
  768. IP(L, R, A);
  769. for (i = 0; i < 8; i++) {
  770. ROUND(L, R, A, B, K, 2);
  771. ROUND(R, L, A, B, K, 2);
  772. }
  773. FP(R, L, A);
  774. d[0] = cpu_to_le32(R);
  775. d[1] = cpu_to_le32(L);
  776. }
  777. static void des_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  778. {
  779. struct des_ctx *ctx = crypto_tfm_ctx(tfm);
  780. const u32 *K = ctx->expkey + DES_EXPKEY_WORDS - 2;
  781. const __le32 *s = (const __le32 *)src;
  782. __le32 *d = (__le32 *)dst;
  783. u32 L, R, A, B;
  784. int i;
  785. L = le32_to_cpu(s[0]);
  786. R = le32_to_cpu(s[1]);
  787. IP(L, R, A);
  788. for (i = 0; i < 8; i++) {
  789. ROUND(L, R, A, B, K, -2);
  790. ROUND(R, L, A, B, K, -2);
  791. }
  792. FP(R, L, A);
  793. d[0] = cpu_to_le32(R);
  794. d[1] = cpu_to_le32(L);
  795. }
  796. /*
  797. * RFC2451:
  798. *
  799. * For DES-EDE3, there is no known need to reject weak or
  800. * complementation keys. Any weakness is obviated by the use of
  801. * multiple keys.
  802. *
  803. * However, if the first two or last two independent 64-bit keys are
  804. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  805. * same as DES. Implementers MUST reject keys that exhibit this
  806. * property.
  807. *
  808. */
  809. static int des3_ede_setkey(struct crypto_tfm *tfm, const u8 *key,
  810. unsigned int keylen)
  811. {
  812. const u32 *K = (const u32 *)key;
  813. struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
  814. u32 *expkey = dctx->expkey;
  815. u32 *flags = &tfm->crt_flags;
  816. if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
  817. !((K[2] ^ K[4]) | (K[3] ^ K[5]))))
  818. {
  819. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  820. return -EINVAL;
  821. }
  822. ekey(expkey, key); expkey += DES_EXPKEY_WORDS; key += DES_KEY_SIZE;
  823. dkey(expkey, key); expkey += DES_EXPKEY_WORDS; key += DES_KEY_SIZE;
  824. ekey(expkey, key);
  825. return 0;
  826. }
  827. static void des3_ede_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  828. {
  829. struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
  830. const u32 *K = dctx->expkey;
  831. const __le32 *s = (const __le32 *)src;
  832. __le32 *d = (__le32 *)dst;
  833. u32 L, R, A, B;
  834. int i;
  835. L = le32_to_cpu(s[0]);
  836. R = le32_to_cpu(s[1]);
  837. IP(L, R, A);
  838. for (i = 0; i < 8; i++) {
  839. ROUND(L, R, A, B, K, 2);
  840. ROUND(R, L, A, B, K, 2);
  841. }
  842. for (i = 0; i < 8; i++) {
  843. ROUND(R, L, A, B, K, 2);
  844. ROUND(L, R, A, B, K, 2);
  845. }
  846. for (i = 0; i < 8; i++) {
  847. ROUND(L, R, A, B, K, 2);
  848. ROUND(R, L, A, B, K, 2);
  849. }
  850. FP(R, L, A);
  851. d[0] = cpu_to_le32(R);
  852. d[1] = cpu_to_le32(L);
  853. }
  854. static void des3_ede_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  855. {
  856. struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
  857. const u32 *K = dctx->expkey + DES3_EDE_EXPKEY_WORDS - 2;
  858. const __le32 *s = (const __le32 *)src;
  859. __le32 *d = (__le32 *)dst;
  860. u32 L, R, A, B;
  861. int i;
  862. L = le32_to_cpu(s[0]);
  863. R = le32_to_cpu(s[1]);
  864. IP(L, R, A);
  865. for (i = 0; i < 8; i++) {
  866. ROUND(L, R, A, B, K, -2);
  867. ROUND(R, L, A, B, K, -2);
  868. }
  869. for (i = 0; i < 8; i++) {
  870. ROUND(R, L, A, B, K, -2);
  871. ROUND(L, R, A, B, K, -2);
  872. }
  873. for (i = 0; i < 8; i++) {
  874. ROUND(L, R, A, B, K, -2);
  875. ROUND(R, L, A, B, K, -2);
  876. }
  877. FP(R, L, A);
  878. d[0] = cpu_to_le32(R);
  879. d[1] = cpu_to_le32(L);
  880. }
  881. static struct crypto_alg des_alg = {
  882. .cra_name = "des",
  883. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  884. .cra_blocksize = DES_BLOCK_SIZE,
  885. .cra_ctxsize = sizeof(struct des_ctx),
  886. .cra_module = THIS_MODULE,
  887. .cra_alignmask = 3,
  888. .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
  889. .cra_u = { .cipher = {
  890. .cia_min_keysize = DES_KEY_SIZE,
  891. .cia_max_keysize = DES_KEY_SIZE,
  892. .cia_setkey = des_setkey,
  893. .cia_encrypt = des_encrypt,
  894. .cia_decrypt = des_decrypt } }
  895. };
  896. static struct crypto_alg des3_ede_alg = {
  897. .cra_name = "des3_ede",
  898. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  899. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  900. .cra_ctxsize = sizeof(struct des3_ede_ctx),
  901. .cra_module = THIS_MODULE,
  902. .cra_alignmask = 3,
  903. .cra_list = LIST_HEAD_INIT(des3_ede_alg.cra_list),
  904. .cra_u = { .cipher = {
  905. .cia_min_keysize = DES3_EDE_KEY_SIZE,
  906. .cia_max_keysize = DES3_EDE_KEY_SIZE,
  907. .cia_setkey = des3_ede_setkey,
  908. .cia_encrypt = des3_ede_encrypt,
  909. .cia_decrypt = des3_ede_decrypt } }
  910. };
  911. MODULE_ALIAS("des3_ede");
  912. static int __init init(void)
  913. {
  914. int ret = 0;
  915. ret = crypto_register_alg(&des_alg);
  916. if (ret < 0)
  917. goto out;
  918. ret = crypto_register_alg(&des3_ede_alg);
  919. if (ret < 0)
  920. crypto_unregister_alg(&des_alg);
  921. out:
  922. return ret;
  923. }
  924. static void __exit fini(void)
  925. {
  926. crypto_unregister_alg(&des3_ede_alg);
  927. crypto_unregister_alg(&des_alg);
  928. }
  929. module_init(init);
  930. module_exit(fini);
  931. MODULE_LICENSE("GPL");
  932. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");
  933. MODULE_AUTHOR("Dag Arne Osvik <da@osvik.no>");