des.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /* des.c - DES and Triple-DES encryption/decryption Algorithm
  2. * Copyright (C) 1998, 1999, 2001, 2002, 2003,
  3. * 2008 Free Software Foundation, Inc.
  4. *
  5. * This file is part of Libgcrypt.
  6. *
  7. * Libgcrypt is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser general Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * Libgcrypt is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. *
  21. * For a description of triple encryption, see:
  22. * Bruce Schneier: Applied Cryptography. Second Edition.
  23. * John Wiley & Sons, 1996. ISBN 0-471-12845-7. Pages 358 ff.
  24. * This implementation is according to the definition of DES in FIPS
  25. * PUB 46-2 from December 1993.
  26. */
  27. /*
  28. * Written by Michael Roth <mroth@nessie.de>, September 1998
  29. */
  30. /*
  31. * U S A G E
  32. * ===========
  33. *
  34. * For DES or Triple-DES encryption/decryption you must initialize a proper
  35. * encryption context with a key.
  36. *
  37. * A DES key is 64bit wide but only 56bits of the key are used. The remaining
  38. * bits are parity bits and they will _not_ checked in this implementation, but
  39. * simply ignored.
  40. *
  41. * For Triple-DES you could use either two 64bit keys or three 64bit keys.
  42. * The parity bits will _not_ checked, too.
  43. *
  44. * After initializing a context with a key you could use this context to
  45. * encrypt or decrypt data in 64bit blocks in Electronic Codebook Mode.
  46. *
  47. * (In the examples below the slashes at the beginning and ending of comments
  48. * are omited.)
  49. *
  50. * DES Example
  51. * -----------
  52. * unsigned char key[8];
  53. * unsigned char plaintext[8];
  54. * unsigned char ciphertext[8];
  55. * unsigned char recoverd[8];
  56. * des_ctx context;
  57. *
  58. * * Fill 'key' and 'plaintext' with some data *
  59. * ....
  60. *
  61. * * Set up the DES encryption context *
  62. * des_setkey(context, key);
  63. *
  64. * * Encrypt the plaintext *
  65. * des_ecb_encrypt(context, plaintext, ciphertext);
  66. *
  67. * * To recover the orginal plaintext from ciphertext use: *
  68. * des_ecb_decrypt(context, ciphertext, recoverd);
  69. *
  70. *
  71. * Triple-DES Example
  72. * ------------------
  73. * unsigned char key1[8];
  74. * unsigned char key2[8];
  75. * unsigned char key3[8];
  76. * unsigned char plaintext[8];
  77. * unsigned char ciphertext[8];
  78. * unsigned char recoverd[8];
  79. * tripledes_ctx context;
  80. *
  81. * * If you would like to use two 64bit keys, fill 'key1' and'key2'
  82. * then setup the encryption context: *
  83. * tripledes_set2keys(context, key1, key2);
  84. *
  85. * * To use three 64bit keys with Triple-DES use: *
  86. * tripledes_set3keys(context, key1, key2, key3);
  87. *
  88. * * Encrypting plaintext with Triple-DES *
  89. * tripledes_ecb_encrypt(context, plaintext, ciphertext);
  90. *
  91. * * Decrypting ciphertext to recover the plaintext with Triple-DES *
  92. * tripledes_ecb_decrypt(context, ciphertext, recoverd);
  93. *
  94. *
  95. * Selftest
  96. * --------
  97. * char *error_msg;
  98. *
  99. * * To perform a selftest of this DES/Triple-DES implementation use the
  100. * function selftest(). It will return an error string if there are
  101. * some problems with this library. *
  102. *
  103. * if ( (error_msg = selftest()) )
  104. * {
  105. * fprintf(stderr, "An error in the DES/Tripple-DES implementation occured: %s\n", error_msg);
  106. * abort();
  107. * }
  108. */
  109. #include <config.h>
  110. #include <stdio.h>
  111. #include <string.h> /* memcpy, memcmp */
  112. #include "types.h" /* for byte and u32 typedefs */
  113. #include "g10lib.h"
  114. #include "cipher.h"
  115. #if defined(__GNUC__) && defined(__GNU_LIBRARY__)
  116. #define working_memcmp memcmp
  117. #else
  118. /*
  119. * According to the SunOS man page, memcmp returns indeterminate sign
  120. * depending on whether characters are signed or not.
  121. */
  122. static int
  123. working_memcmp( const char *a, const char *b, size_t n )
  124. {
  125. for( ; n; n--, a++, b++ )
  126. if( *a != *b )
  127. return (int)(*(byte*)a) - (int)(*(byte*)b);
  128. return 0;
  129. }
  130. #endif
  131. /*
  132. * Encryption/Decryption context of DES
  133. */
  134. typedef struct _des_ctx
  135. {
  136. u32 encrypt_subkeys[32];
  137. u32 decrypt_subkeys[32];
  138. }
  139. des_ctx[1];
  140. /*
  141. * Encryption/Decryption context of Triple-DES
  142. */
  143. typedef struct _tripledes_ctx
  144. {
  145. u32 encrypt_subkeys[96];
  146. u32 decrypt_subkeys[96];
  147. struct {
  148. int no_weak_key;
  149. } flags;
  150. }
  151. tripledes_ctx[1];
  152. static void des_key_schedule (const byte *, u32 *);
  153. static int des_setkey (struct _des_ctx *, const byte *);
  154. static int des_ecb_crypt (struct _des_ctx *, const byte *, byte *, int);
  155. static int tripledes_set2keys (struct _tripledes_ctx *,
  156. const byte *, const byte *);
  157. static int tripledes_set3keys (struct _tripledes_ctx *,
  158. const byte *, const byte *, const byte *);
  159. static int tripledes_ecb_crypt (struct _tripledes_ctx *,
  160. const byte *, byte *, int);
  161. static int is_weak_key ( const byte *key );
  162. static const char *selftest (void);
  163. static int initialized;
  164. /*
  165. * The s-box values are permuted according to the 'primitive function P'
  166. * and are rotated one bit to the left.
  167. */
  168. static u32 sbox1[64] =
  169. {
  170. 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000,
  171. 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004,
  172. 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404,
  173. 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000,
  174. 0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400,
  175. 0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  176. 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400,
  177. 0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004
  178. };
  179. static u32 sbox2[64] =
  180. {
  181. 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020,
  182. 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020,
  183. 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000,
  184. 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020,
  185. 0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000,
  186. 0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  187. 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020,
  188. 0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000
  189. };
  190. static u32 sbox3[64] =
  191. {
  192. 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200,
  193. 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208,
  194. 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208,
  195. 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000,
  196. 0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000,
  197. 0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  198. 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008,
  199. 0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200
  200. };
  201. static u32 sbox4[64] =
  202. {
  203. 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001,
  204. 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001,
  205. 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080,
  206. 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081,
  207. 0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000,
  208. 0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  209. 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081,
  210. 0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080
  211. };
  212. static u32 sbox5[64] =
  213. {
  214. 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000,
  215. 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000,
  216. 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100,
  217. 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100,
  218. 0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100,
  219. 0x02000100, 0x40000000, 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  220. 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, 0x40080000, 0x42000000,
  221. 0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100, 0x40000100
  222. };
  223. static u32 sbox6[64] =
  224. {
  225. 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000,
  226. 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010,
  227. 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010,
  228. 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000,
  229. 0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010,
  230. 0x00400000, 0x20004000, 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  231. 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, 0x20400000, 0x00404010,
  232. 0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010, 0x20004010
  233. };
  234. static u32 sbox7[64] =
  235. {
  236. 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800,
  237. 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802,
  238. 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002,
  239. 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800,
  240. 0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002,
  241. 0x00200002, 0x04000000, 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  242. 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, 0x00000002, 0x04200802,
  243. 0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800, 0x00200002
  244. };
  245. static u32 sbox8[64] =
  246. {
  247. 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000,
  248. 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040,
  249. 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000,
  250. 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000,
  251. 0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040,
  252. 0x10001000, 0x00000040, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  253. 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0x00000000,
  254. 0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000, 0x10041000
  255. };
  256. /*
  257. * These two tables are part of the 'permuted choice 1' function.
  258. * In this implementation several speed improvements are done.
  259. */
  260. static u32 leftkey_swap[16] =
  261. {
  262. 0x00000000, 0x00000001, 0x00000100, 0x00000101,
  263. 0x00010000, 0x00010001, 0x00010100, 0x00010101,
  264. 0x01000000, 0x01000001, 0x01000100, 0x01000101,
  265. 0x01010000, 0x01010001, 0x01010100, 0x01010101
  266. };
  267. static u32 rightkey_swap[16] =
  268. {
  269. 0x00000000, 0x01000000, 0x00010000, 0x01010000,
  270. 0x00000100, 0x01000100, 0x00010100, 0x01010100,
  271. 0x00000001, 0x01000001, 0x00010001, 0x01010001,
  272. 0x00000101, 0x01000101, 0x00010101, 0x01010101,
  273. };
  274. /*
  275. * Numbers of left shifts per round for encryption subkeys.
  276. * To calculate the decryption subkeys we just reverse the
  277. * ordering of the calculated encryption subkeys. So their
  278. * is no need for a decryption rotate tab.
  279. */
  280. static byte encrypt_rotate_tab[16] =
  281. {
  282. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  283. };
  284. /*
  285. * Table with weak DES keys sorted in ascending order.
  286. * In DES their are 64 known keys which are weak. They are weak
  287. * because they produce only one, two or four different
  288. * subkeys in the subkey scheduling process.
  289. * The keys in this table have all their parity bits cleared.
  290. */
  291. static byte weak_keys[64][8] =
  292. {
  293. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /*w*/
  294. { 0x00, 0x00, 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e },
  295. { 0x00, 0x00, 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0 },
  296. { 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe },
  297. { 0x00, 0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e }, /*sw*/
  298. { 0x00, 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e, 0x00 },
  299. { 0x00, 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0, 0xfe },
  300. { 0x00, 0x1e, 0xfe, 0xe0, 0x00, 0x0e, 0xfe, 0xf0 },
  301. { 0x00, 0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0 }, /*sw*/
  302. { 0x00, 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e, 0xfe },
  303. { 0x00, 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0, 0x00 },
  304. { 0x00, 0xe0, 0xfe, 0x1e, 0x00, 0xf0, 0xfe, 0x0e },
  305. { 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe }, /*sw*/
  306. { 0x00, 0xfe, 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0 },
  307. { 0x00, 0xfe, 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e },
  308. { 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00 },
  309. { 0x1e, 0x00, 0x00, 0x1e, 0x0e, 0x00, 0x00, 0x0e },
  310. { 0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e, 0x00 }, /*sw*/
  311. { 0x1e, 0x00, 0xe0, 0xfe, 0x0e, 0x00, 0xf0, 0xfe },
  312. { 0x1e, 0x00, 0xfe, 0xe0, 0x0e, 0x00, 0xfe, 0xf0 },
  313. { 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e, 0x00, 0x00 },
  314. { 0x1e, 0x1e, 0x1e, 0x1e, 0x0e, 0x0e, 0x0e, 0x0e }, /*w*/
  315. { 0x1e, 0x1e, 0xe0, 0xe0, 0x0e, 0x0e, 0xf0, 0xf0 },
  316. { 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e, 0xfe, 0xfe },
  317. { 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0, 0x00, 0xfe },
  318. { 0x1e, 0xe0, 0x1e, 0xe0, 0x0e, 0xf0, 0x0e, 0xf0 }, /*sw*/
  319. { 0x1e, 0xe0, 0xe0, 0x1e, 0x0e, 0xf0, 0xf0, 0x0e },
  320. { 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0, 0xfe, 0x00 },
  321. { 0x1e, 0xfe, 0x00, 0xe0, 0x0e, 0xfe, 0x00, 0xf0 },
  322. { 0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e, 0xfe }, /*sw*/
  323. { 0x1e, 0xfe, 0xe0, 0x00, 0x0e, 0xfe, 0xf0, 0x00 },
  324. { 0x1e, 0xfe, 0xfe, 0x1e, 0x0e, 0xfe, 0xfe, 0x0e },
  325. { 0xe0, 0x00, 0x00, 0xe0, 0xf0, 0x00, 0x00, 0xf0 },
  326. { 0xe0, 0x00, 0x1e, 0xfe, 0xf0, 0x00, 0x0e, 0xfe },
  327. { 0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0, 0x00 }, /*sw*/
  328. { 0xe0, 0x00, 0xfe, 0x1e, 0xf0, 0x00, 0xfe, 0x0e },
  329. { 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e, 0x00, 0xfe },
  330. { 0xe0, 0x1e, 0x1e, 0xe0, 0xf0, 0x0e, 0x0e, 0xf0 },
  331. { 0xe0, 0x1e, 0xe0, 0x1e, 0xf0, 0x0e, 0xf0, 0x0e }, /*sw*/
  332. { 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e, 0xfe, 0x00 },
  333. { 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0, 0x00, 0x00 },
  334. { 0xe0, 0xe0, 0x1e, 0x1e, 0xf0, 0xf0, 0x0e, 0x0e },
  335. { 0xe0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf0, 0xf0 }, /*w*/
  336. { 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0, 0xfe, 0xfe },
  337. { 0xe0, 0xfe, 0x00, 0x1e, 0xf0, 0xfe, 0x00, 0x0e },
  338. { 0xe0, 0xfe, 0x1e, 0x00, 0xf0, 0xfe, 0x0e, 0x00 },
  339. { 0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0, 0xfe }, /*sw*/
  340. { 0xe0, 0xfe, 0xfe, 0xe0, 0xf0, 0xfe, 0xfe, 0xf0 },
  341. { 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe },
  342. { 0xfe, 0x00, 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0 },
  343. { 0xfe, 0x00, 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e },
  344. { 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00 }, /*sw*/
  345. { 0xfe, 0x1e, 0x00, 0xe0, 0xfe, 0x0e, 0x00, 0xf0 },
  346. { 0xfe, 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e, 0xfe },
  347. { 0xfe, 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0, 0x00 },
  348. { 0xfe, 0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e }, /*sw*/
  349. { 0xfe, 0xe0, 0x00, 0x1e, 0xfe, 0xf0, 0x00, 0x0e },
  350. { 0xfe, 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e, 0x00 },
  351. { 0xfe, 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0, 0xfe },
  352. { 0xfe, 0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0 }, /*sw*/
  353. { 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00 },
  354. { 0xfe, 0xfe, 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e },
  355. { 0xfe, 0xfe, 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0 },
  356. { 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe } /*w*/
  357. };
  358. static unsigned char weak_keys_chksum[20] = {
  359. 0xD0, 0xCF, 0x07, 0x38, 0x93, 0x70, 0x8A, 0x83, 0x7D, 0xD7,
  360. 0x8A, 0x36, 0x65, 0x29, 0x6C, 0x1F, 0x7C, 0x3F, 0xD3, 0x41
  361. };
  362. /*
  363. * Macro to swap bits across two words.
  364. */
  365. #define DO_PERMUTATION(a, temp, b, offset, mask) \
  366. temp = ((a>>offset) ^ b) & mask; \
  367. b ^= temp; \
  368. a ^= temp<<offset;
  369. /*
  370. * This performs the 'initial permutation' of the data to be encrypted
  371. * or decrypted. Additionally the resulting two words are rotated one bit
  372. * to the left.
  373. */
  374. #define INITIAL_PERMUTATION(left, temp, right) \
  375. DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f) \
  376. DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
  377. DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
  378. DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
  379. right = (right << 1) | (right >> 31); \
  380. temp = (left ^ right) & 0xaaaaaaaa; \
  381. right ^= temp; \
  382. left ^= temp; \
  383. left = (left << 1) | (left >> 31);
  384. /*
  385. * The 'inverse initial permutation'.
  386. */
  387. #define FINAL_PERMUTATION(left, temp, right) \
  388. left = (left << 31) | (left >> 1); \
  389. temp = (left ^ right) & 0xaaaaaaaa; \
  390. left ^= temp; \
  391. right ^= temp; \
  392. right = (right << 31) | (right >> 1); \
  393. DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
  394. DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
  395. DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
  396. DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f)
  397. /*
  398. * A full DES round including 'expansion function', 'sbox substitution'
  399. * and 'primitive function P' but without swapping the left and right word.
  400. * Please note: The data in 'from' and 'to' is already rotated one bit to
  401. * the left, done in the initial permutation.
  402. */
  403. #define DES_ROUND(from, to, work, subkey) \
  404. work = from ^ *subkey++; \
  405. to ^= sbox8[ work & 0x3f ]; \
  406. to ^= sbox6[ (work>>8) & 0x3f ]; \
  407. to ^= sbox4[ (work>>16) & 0x3f ]; \
  408. to ^= sbox2[ (work>>24) & 0x3f ]; \
  409. work = ((from << 28) | (from >> 4)) ^ *subkey++; \
  410. to ^= sbox7[ work & 0x3f ]; \
  411. to ^= sbox5[ (work>>8) & 0x3f ]; \
  412. to ^= sbox3[ (work>>16) & 0x3f ]; \
  413. to ^= sbox1[ (work>>24) & 0x3f ];
  414. /*
  415. * Macros to convert 8 bytes from/to 32bit words.
  416. */
  417. #define READ_64BIT_DATA(data, left, right) \
  418. left = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; \
  419. right = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
  420. #define WRITE_64BIT_DATA(data, left, right) \
  421. data[0] = (left >> 24) &0xff; data[1] = (left >> 16) &0xff; \
  422. data[2] = (left >> 8) &0xff; data[3] = left &0xff; \
  423. data[4] = (right >> 24) &0xff; data[5] = (right >> 16) &0xff; \
  424. data[6] = (right >> 8) &0xff; data[7] = right &0xff;
  425. /*
  426. * Handy macros for encryption and decryption of data
  427. */
  428. #define des_ecb_encrypt(ctx, from, to) des_ecb_crypt(ctx, from, to, 0)
  429. #define des_ecb_decrypt(ctx, from, to) des_ecb_crypt(ctx, from, to, 1)
  430. #define tripledes_ecb_encrypt(ctx, from, to) tripledes_ecb_crypt(ctx,from,to,0)
  431. #define tripledes_ecb_decrypt(ctx, from, to) tripledes_ecb_crypt(ctx,from,to,1)
  432. /*
  433. * des_key_schedule(): Calculate 16 subkeys pairs (even/odd) for
  434. * 16 encryption rounds.
  435. * To calculate subkeys for decryption the caller
  436. * have to reorder the generated subkeys.
  437. *
  438. * rawkey: 8 Bytes of key data
  439. * subkey: Array of at least 32 u32s. Will be filled
  440. * with calculated subkeys.
  441. *
  442. */
  443. static void
  444. des_key_schedule (const byte * rawkey, u32 * subkey)
  445. {
  446. u32 left, right, work;
  447. int round;
  448. READ_64BIT_DATA (rawkey, left, right)
  449. DO_PERMUTATION (right, work, left, 4, 0x0f0f0f0f)
  450. DO_PERMUTATION (right, work, left, 0, 0x10101010)
  451. left = ((leftkey_swap[(left >> 0) & 0xf] << 3)
  452. | (leftkey_swap[(left >> 8) & 0xf] << 2)
  453. | (leftkey_swap[(left >> 16) & 0xf] << 1)
  454. | (leftkey_swap[(left >> 24) & 0xf])
  455. | (leftkey_swap[(left >> 5) & 0xf] << 7)
  456. | (leftkey_swap[(left >> 13) & 0xf] << 6)
  457. | (leftkey_swap[(left >> 21) & 0xf] << 5)
  458. | (leftkey_swap[(left >> 29) & 0xf] << 4));
  459. left &= 0x0fffffff;
  460. right = ((rightkey_swap[(right >> 1) & 0xf] << 3)
  461. | (rightkey_swap[(right >> 9) & 0xf] << 2)
  462. | (rightkey_swap[(right >> 17) & 0xf] << 1)
  463. | (rightkey_swap[(right >> 25) & 0xf])
  464. | (rightkey_swap[(right >> 4) & 0xf] << 7)
  465. | (rightkey_swap[(right >> 12) & 0xf] << 6)
  466. | (rightkey_swap[(right >> 20) & 0xf] << 5)
  467. | (rightkey_swap[(right >> 28) & 0xf] << 4));
  468. right &= 0x0fffffff;
  469. for (round = 0; round < 16; ++round)
  470. {
  471. left = ((left << encrypt_rotate_tab[round])
  472. | (left >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
  473. right = ((right << encrypt_rotate_tab[round])
  474. | (right >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
  475. *subkey++ = (((left << 4) & 0x24000000)
  476. | ((left << 28) & 0x10000000)
  477. | ((left << 14) & 0x08000000)
  478. | ((left << 18) & 0x02080000)
  479. | ((left << 6) & 0x01000000)
  480. | ((left << 9) & 0x00200000)
  481. | ((left >> 1) & 0x00100000)
  482. | ((left << 10) & 0x00040000)
  483. | ((left << 2) & 0x00020000)
  484. | ((left >> 10) & 0x00010000)
  485. | ((right >> 13) & 0x00002000)
  486. | ((right >> 4) & 0x00001000)
  487. | ((right << 6) & 0x00000800)
  488. | ((right >> 1) & 0x00000400)
  489. | ((right >> 14) & 0x00000200)
  490. | (right & 0x00000100)
  491. | ((right >> 5) & 0x00000020)
  492. | ((right >> 10) & 0x00000010)
  493. | ((right >> 3) & 0x00000008)
  494. | ((right >> 18) & 0x00000004)
  495. | ((right >> 26) & 0x00000002)
  496. | ((right >> 24) & 0x00000001));
  497. *subkey++ = (((left << 15) & 0x20000000)
  498. | ((left << 17) & 0x10000000)
  499. | ((left << 10) & 0x08000000)
  500. | ((left << 22) & 0x04000000)
  501. | ((left >> 2) & 0x02000000)
  502. | ((left << 1) & 0x01000000)
  503. | ((left << 16) & 0x00200000)
  504. | ((left << 11) & 0x00100000)
  505. | ((left << 3) & 0x00080000)
  506. | ((left >> 6) & 0x00040000)
  507. | ((left << 15) & 0x00020000)
  508. | ((left >> 4) & 0x00010000)
  509. | ((right >> 2) & 0x00002000)
  510. | ((right << 8) & 0x00001000)
  511. | ((right >> 14) & 0x00000808)
  512. | ((right >> 9) & 0x00000400)
  513. | ((right) & 0x00000200)
  514. | ((right << 7) & 0x00000100)
  515. | ((right >> 7) & 0x00000020)
  516. | ((right >> 3) & 0x00000011)
  517. | ((right << 2) & 0x00000004)
  518. | ((right >> 21) & 0x00000002));
  519. }
  520. }
  521. /*
  522. * Fill a DES context with subkeys calculated from a 64bit key.
  523. * Does not check parity bits, but simply ignore them.
  524. * Does not check for weak keys.
  525. */
  526. static int
  527. des_setkey (struct _des_ctx *ctx, const byte * key)
  528. {
  529. static const char *selftest_failed;
  530. int i;
  531. if (!fips_mode () && !initialized)
  532. {
  533. initialized = 1;
  534. selftest_failed = selftest ();
  535. if (selftest_failed)
  536. log_error ("%s\n", selftest_failed);
  537. }
  538. if (selftest_failed)
  539. return GPG_ERR_SELFTEST_FAILED;
  540. des_key_schedule (key, ctx->encrypt_subkeys);
  541. _gcry_burn_stack (32);
  542. for(i=0; i<32; i+=2)
  543. {
  544. ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[30-i];
  545. ctx->decrypt_subkeys[i+1] = ctx->encrypt_subkeys[31-i];
  546. }
  547. return 0;
  548. }
  549. /*
  550. * Electronic Codebook Mode DES encryption/decryption of data according
  551. * to 'mode'.
  552. */
  553. static int
  554. des_ecb_crypt (struct _des_ctx *ctx, const byte * from, byte * to, int mode)
  555. {
  556. u32 left, right, work;
  557. u32 *keys;
  558. keys = mode ? ctx->decrypt_subkeys : ctx->encrypt_subkeys;
  559. READ_64BIT_DATA (from, left, right)
  560. INITIAL_PERMUTATION (left, work, right)
  561. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  562. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  563. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  564. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  565. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  566. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  567. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  568. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  569. FINAL_PERMUTATION (right, work, left)
  570. WRITE_64BIT_DATA (to, right, left)
  571. return 0;
  572. }
  573. /*
  574. * Fill a Triple-DES context with subkeys calculated from two 64bit keys.
  575. * Does not check the parity bits of the keys, but simply ignore them.
  576. * Does not check for weak keys.
  577. */
  578. static int
  579. tripledes_set2keys (struct _tripledes_ctx *ctx,
  580. const byte * key1,
  581. const byte * key2)
  582. {
  583. int i;
  584. des_key_schedule (key1, ctx->encrypt_subkeys);
  585. des_key_schedule (key2, &(ctx->decrypt_subkeys[32]));
  586. _gcry_burn_stack (32);
  587. for(i=0; i<32; i+=2)
  588. {
  589. ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[30-i];
  590. ctx->decrypt_subkeys[i+1] = ctx->encrypt_subkeys[31-i];
  591. ctx->encrypt_subkeys[i+32] = ctx->decrypt_subkeys[62-i];
  592. ctx->encrypt_subkeys[i+33] = ctx->decrypt_subkeys[63-i];
  593. ctx->encrypt_subkeys[i+64] = ctx->encrypt_subkeys[i];
  594. ctx->encrypt_subkeys[i+65] = ctx->encrypt_subkeys[i+1];
  595. ctx->decrypt_subkeys[i+64] = ctx->decrypt_subkeys[i];
  596. ctx->decrypt_subkeys[i+65] = ctx->decrypt_subkeys[i+1];
  597. }
  598. return 0;
  599. }
  600. /*
  601. * Fill a Triple-DES context with subkeys calculated from three 64bit keys.
  602. * Does not check the parity bits of the keys, but simply ignore them.
  603. * Does not check for weak keys.
  604. */
  605. static int
  606. tripledes_set3keys (struct _tripledes_ctx *ctx,
  607. const byte * key1,
  608. const byte * key2,
  609. const byte * key3)
  610. {
  611. static const char *selftest_failed;
  612. int i;
  613. if (!fips_mode () && !initialized)
  614. {
  615. initialized = 1;
  616. selftest_failed = selftest ();
  617. if (selftest_failed)
  618. log_error ("%s\n", selftest_failed);
  619. }
  620. if (selftest_failed)
  621. return GPG_ERR_SELFTEST_FAILED;
  622. des_key_schedule (key1, ctx->encrypt_subkeys);
  623. des_key_schedule (key2, &(ctx->decrypt_subkeys[32]));
  624. des_key_schedule (key3, &(ctx->encrypt_subkeys[64]));
  625. _gcry_burn_stack (32);
  626. for(i=0; i<32; i+=2)
  627. {
  628. ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[94-i];
  629. ctx->decrypt_subkeys[i+1] = ctx->encrypt_subkeys[95-i];
  630. ctx->encrypt_subkeys[i+32] = ctx->decrypt_subkeys[62-i];
  631. ctx->encrypt_subkeys[i+33] = ctx->decrypt_subkeys[63-i];
  632. ctx->decrypt_subkeys[i+64] = ctx->encrypt_subkeys[30-i];
  633. ctx->decrypt_subkeys[i+65] = ctx->encrypt_subkeys[31-i];
  634. }
  635. return 0;
  636. }
  637. /*
  638. * Electronic Codebook Mode Triple-DES encryption/decryption of data
  639. * according to 'mode'. Sometimes this mode is named 'EDE' mode
  640. * (Encryption-Decryption-Encryption).
  641. */
  642. static int
  643. tripledes_ecb_crypt (struct _tripledes_ctx *ctx, const byte * from,
  644. byte * to, int mode)
  645. {
  646. u32 left, right, work;
  647. u32 *keys;
  648. keys = mode ? ctx->decrypt_subkeys : ctx->encrypt_subkeys;
  649. READ_64BIT_DATA (from, left, right)
  650. INITIAL_PERMUTATION (left, work, right)
  651. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  652. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  653. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  654. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  655. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  656. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  657. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  658. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  659. DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
  660. DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
  661. DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
  662. DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
  663. DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
  664. DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
  665. DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
  666. DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
  667. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  668. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  669. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  670. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  671. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  672. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  673. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  674. DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
  675. FINAL_PERMUTATION (right, work, left)
  676. WRITE_64BIT_DATA (to, right, left)
  677. return 0;
  678. }
  679. /*
  680. * Check whether the 8 byte key is weak.
  681. * Does not check the parity bits of the key but simple ignore them.
  682. */
  683. static int
  684. is_weak_key ( const byte *key )
  685. {
  686. byte work[8];
  687. int i, left, right, middle, cmp_result;
  688. /* clear parity bits */
  689. for(i=0; i<8; ++i)
  690. work[i] = key[i] & 0xfe;
  691. /* binary search in the weak key table */
  692. left = 0;
  693. right = 63;
  694. while(left <= right)
  695. {
  696. middle = (left + right) / 2;
  697. if ( !(cmp_result=working_memcmp(work, weak_keys[middle], 8)) )
  698. return -1;
  699. if ( cmp_result > 0 )
  700. left = middle + 1;
  701. else
  702. right = middle - 1;
  703. }
  704. return 0;
  705. }
  706. /*
  707. * Performs a selftest of this DES/Triple-DES implementation.
  708. * Returns an string with the error text on failure.
  709. * Returns NULL if all is ok.
  710. */
  711. static const char *
  712. selftest (void)
  713. {
  714. /*
  715. * Check if 'u32' is really 32 bits wide. This DES / 3DES implementation
  716. * need this.
  717. */
  718. if (sizeof (u32) != 4)
  719. return "Wrong word size for DES configured.";
  720. /*
  721. * DES Maintenance Test
  722. */
  723. {
  724. int i;
  725. byte key[8] =
  726. {0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
  727. byte input[8] =
  728. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  729. byte result[8] =
  730. {0x24, 0x6e, 0x9d, 0xb9, 0xc5, 0x50, 0x38, 0x1a};
  731. byte temp1[8], temp2[8], temp3[8];
  732. des_ctx des;
  733. for (i = 0; i < 64; ++i)
  734. {
  735. des_setkey (des, key);
  736. des_ecb_encrypt (des, input, temp1);
  737. des_ecb_encrypt (des, temp1, temp2);
  738. des_setkey (des, temp2);
  739. des_ecb_decrypt (des, temp1, temp3);
  740. memcpy (key, temp3, 8);
  741. memcpy (input, temp1, 8);
  742. }
  743. if (memcmp (temp3, result, 8))
  744. return "DES maintenance test failed.";
  745. }
  746. /*
  747. * Self made Triple-DES test (Does somebody know an official test?)
  748. */
  749. {
  750. int i;
  751. byte input[8] =
  752. {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
  753. byte key1[8] =
  754. {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
  755. byte key2[8] =
  756. {0x11, 0x22, 0x33, 0x44, 0xff, 0xaa, 0xcc, 0xdd};
  757. byte result[8] =
  758. {0x7b, 0x38, 0x3b, 0x23, 0xa2, 0x7d, 0x26, 0xd3};
  759. tripledes_ctx des3;
  760. for (i = 0; i < 16; ++i)
  761. {
  762. tripledes_set2keys (des3, key1, key2);
  763. tripledes_ecb_encrypt (des3, input, key1);
  764. tripledes_ecb_decrypt (des3, input, key2);
  765. tripledes_set3keys (des3, key1, input, key2);
  766. tripledes_ecb_encrypt (des3, input, input);
  767. }
  768. if (memcmp (input, result, 8))
  769. return "Triple-DES test failed.";
  770. }
  771. /*
  772. * More Triple-DES test. These are testvectors as used by SSLeay,
  773. * thanks to Jeroen C. van Gelderen.
  774. */
  775. {
  776. struct { byte key[24]; byte plain[8]; byte cipher[8]; } testdata[] = {
  777. { { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  778. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  779. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },
  780. { 0x95,0xF8,0xA5,0xE5,0xDD,0x31,0xD9,0x00 },
  781. { 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
  782. },
  783. { { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  784. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  785. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },
  786. { 0x9D,0x64,0x55,0x5A,0x9A,0x10,0xB8,0x52, },
  787. { 0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00 }
  788. },
  789. { { 0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E,
  790. 0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E,
  791. 0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E },
  792. { 0x51,0x45,0x4B,0x58,0x2D,0xDF,0x44,0x0A },
  793. { 0x71,0x78,0x87,0x6E,0x01,0xF1,0x9B,0x2A }
  794. },
  795. { { 0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6,
  796. 0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6,
  797. 0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6 },
  798. { 0x42,0xFD,0x44,0x30,0x59,0x57,0x7F,0xA2 },
  799. { 0xAF,0x37,0xFB,0x42,0x1F,0x8C,0x40,0x95 }
  800. },
  801. { { 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
  802. 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
  803. 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF },
  804. { 0x73,0x6F,0x6D,0x65,0x64,0x61,0x74,0x61 },
  805. { 0x3D,0x12,0x4F,0xE2,0x19,0x8B,0xA3,0x18 }
  806. },
  807. { { 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
  808. 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
  809. 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF },
  810. { 0x73,0x6F,0x6D,0x65,0x64,0x61,0x74,0x61 },
  811. { 0xFB,0xAB,0xA1,0xFF,0x9D,0x05,0xE9,0xB1 }
  812. },
  813. { { 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
  814. 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
  815. 0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10 },
  816. { 0x73,0x6F,0x6D,0x65,0x64,0x61,0x74,0x61 },
  817. { 0x18,0xd7,0x48,0xe5,0x63,0x62,0x05,0x72 }
  818. },
  819. { { 0x03,0x52,0x02,0x07,0x67,0x20,0x82,0x17,
  820. 0x86,0x02,0x87,0x66,0x59,0x08,0x21,0x98,
  821. 0x64,0x05,0x6A,0xBD,0xFE,0xA9,0x34,0x57 },
  822. { 0x73,0x71,0x75,0x69,0x67,0x67,0x6C,0x65 },
  823. { 0xc0,0x7d,0x2a,0x0f,0xa5,0x66,0xfa,0x30 }
  824. },
  825. { { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  826. 0x80,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  827. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02 },
  828. { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  829. { 0xe6,0xe6,0xdd,0x5b,0x7e,0x72,0x29,0x74 }
  830. },
  831. { { 0x10,0x46,0x10,0x34,0x89,0x98,0x80,0x20,
  832. 0x91,0x07,0xD0,0x15,0x89,0x19,0x01,0x01,
  833. 0x19,0x07,0x92,0x10,0x98,0x1A,0x01,0x01 },
  834. { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  835. { 0xe1,0xef,0x62,0xc3,0x32,0xfe,0x82,0x5b }
  836. }
  837. };
  838. byte result[8];
  839. int i;
  840. tripledes_ctx des3;
  841. for (i=0; i<sizeof(testdata)/sizeof(*testdata); ++i)
  842. {
  843. tripledes_set3keys (des3, testdata[i].key,
  844. testdata[i].key + 8, testdata[i].key + 16);
  845. tripledes_ecb_encrypt (des3, testdata[i].plain, result);
  846. if (memcmp (testdata[i].cipher, result, 8))
  847. return "Triple-DES SSLeay test failed on encryption.";
  848. tripledes_ecb_decrypt (des3, testdata[i].cipher, result);
  849. if (memcmp (testdata[i].plain, result, 8))
  850. return "Triple-DES SSLeay test failed on decryption.";;
  851. }
  852. }
  853. /*
  854. * Check the weak key detection. We simply assume that the table
  855. * with weak keys is ok and check every key in the table if it is
  856. * detected... (This test is a little bit stupid).
  857. */
  858. {
  859. int i;
  860. unsigned char *p;
  861. gcry_md_hd_t h;
  862. if (_gcry_md_open (&h, GCRY_MD_SHA1, 0))
  863. return "SHA1 not available";
  864. for (i = 0; i < 64; ++i)
  865. _gcry_md_write (h, weak_keys[i], 8);
  866. p = _gcry_md_read (h, GCRY_MD_SHA1);
  867. i = memcmp (p, weak_keys_chksum, 20);
  868. _gcry_md_close (h);
  869. if (i)
  870. return "weak key table defect";
  871. for (i = 0; i < 64; ++i)
  872. if (!is_weak_key(weak_keys[i]))
  873. return "DES weak key detection failed";
  874. }
  875. return 0;
  876. }
  877. static gcry_err_code_t
  878. do_tripledes_setkey ( void *context, const byte *key, unsigned keylen )
  879. {
  880. struct _tripledes_ctx *ctx = (struct _tripledes_ctx *) context;
  881. if( keylen != 24 )
  882. return GPG_ERR_INV_KEYLEN;
  883. tripledes_set3keys ( ctx, key, key+8, key+16);
  884. if (ctx->flags.no_weak_key)
  885. ; /* Detection has been disabled. */
  886. else if (is_weak_key (key) || is_weak_key (key+8) || is_weak_key (key+16))
  887. {
  888. _gcry_burn_stack (64);
  889. return GPG_ERR_WEAK_KEY;
  890. }
  891. _gcry_burn_stack (64);
  892. return GPG_ERR_NO_ERROR;
  893. }
  894. static gcry_err_code_t
  895. do_tripledes_set_extra_info (void *context, int what,
  896. const void *buffer, size_t buflen)
  897. {
  898. struct _tripledes_ctx *ctx = (struct _tripledes_ctx *)context;
  899. gpg_err_code_t ec = 0;
  900. (void)buffer;
  901. (void)buflen;
  902. switch (what)
  903. {
  904. case CIPHER_INFO_NO_WEAK_KEY:
  905. ctx->flags.no_weak_key = 1;
  906. break;
  907. default:
  908. ec = GPG_ERR_INV_OP;
  909. break;
  910. }
  911. return ec;
  912. }
  913. static void
  914. do_tripledes_encrypt( void *context, byte *outbuf, const byte *inbuf )
  915. {
  916. struct _tripledes_ctx *ctx = (struct _tripledes_ctx *) context;
  917. tripledes_ecb_encrypt ( ctx, inbuf, outbuf );
  918. _gcry_burn_stack (32);
  919. }
  920. static void
  921. do_tripledes_decrypt( void *context, byte *outbuf, const byte *inbuf )
  922. {
  923. struct _tripledes_ctx *ctx = (struct _tripledes_ctx *) context;
  924. tripledes_ecb_decrypt ( ctx, inbuf, outbuf );
  925. _gcry_burn_stack (32);
  926. }
  927. static gcry_err_code_t
  928. do_des_setkey (void *context, const byte *key, unsigned keylen)
  929. {
  930. struct _des_ctx *ctx = (struct _des_ctx *) context;
  931. if (keylen != 8)
  932. return GPG_ERR_INV_KEYLEN;
  933. des_setkey (ctx, key);
  934. if (is_weak_key (key)) {
  935. _gcry_burn_stack (64);
  936. return GPG_ERR_WEAK_KEY;
  937. }
  938. _gcry_burn_stack (64);
  939. return GPG_ERR_NO_ERROR;
  940. }
  941. static void
  942. do_des_encrypt( void *context, byte *outbuf, const byte *inbuf )
  943. {
  944. struct _des_ctx *ctx = (struct _des_ctx *) context;
  945. des_ecb_encrypt ( ctx, inbuf, outbuf );
  946. _gcry_burn_stack (32);
  947. }
  948. static void
  949. do_des_decrypt( void *context, byte *outbuf, const byte *inbuf )
  950. {
  951. struct _des_ctx *ctx = (struct _des_ctx *) context;
  952. des_ecb_decrypt ( ctx, inbuf, outbuf );
  953. _gcry_burn_stack (32);
  954. }
  955. /*
  956. Self-test section.
  957. */
  958. /* Selftest for TripleDES. */
  959. static gpg_err_code_t
  960. selftest_fips (int extended, selftest_report_func_t report)
  961. {
  962. const char *what;
  963. const char *errtxt;
  964. (void)extended; /* No extended tests available. */
  965. what = "low-level";
  966. errtxt = selftest ();
  967. if (errtxt)
  968. goto failed;
  969. /* The low-level self-tests are quite extensive and thus we can do
  970. without high level tests. This is also justified because we have
  971. no custom block code implementation for 3des but always use the
  972. standard high level block code. */
  973. return 0; /* Succeeded. */
  974. failed:
  975. if (report)
  976. report ("cipher", GCRY_CIPHER_3DES, what, errtxt);
  977. return GPG_ERR_SELFTEST_FAILED;
  978. }
  979. /* Run a full self-test for ALGO and return 0 on success. */
  980. static gpg_err_code_t
  981. run_selftests (int algo, int extended, selftest_report_func_t report)
  982. {
  983. gpg_err_code_t ec;
  984. switch (algo)
  985. {
  986. case GCRY_CIPHER_3DES:
  987. ec = selftest_fips (extended, report);
  988. break;
  989. default:
  990. ec = GPG_ERR_CIPHER_ALGO;
  991. break;
  992. }
  993. return ec;
  994. }
  995. gcry_cipher_spec_t _gcry_cipher_spec_des =
  996. {
  997. "DES", NULL, NULL, 8, 64, sizeof (struct _des_ctx),
  998. do_des_setkey, do_des_encrypt, do_des_decrypt
  999. };
  1000. static gcry_cipher_oid_spec_t oids_tripledes[] =
  1001. {
  1002. { "1.2.840.113549.3.7", GCRY_CIPHER_MODE_CBC },
  1003. /* Teletrust specific OID for 3DES. */
  1004. { "1.3.36.3.1.3.2.1", GCRY_CIPHER_MODE_CBC },
  1005. /* pbeWithSHAAnd3_KeyTripleDES_CBC */
  1006. { "1.2.840.113549.1.12.1.3", GCRY_CIPHER_MODE_CBC },
  1007. { NULL }
  1008. };
  1009. gcry_cipher_spec_t _gcry_cipher_spec_tripledes =
  1010. {
  1011. "3DES", NULL, oids_tripledes, 8, 192, sizeof (struct _tripledes_ctx),
  1012. do_tripledes_setkey, do_tripledes_encrypt, do_tripledes_decrypt
  1013. };
  1014. cipher_extra_spec_t _gcry_cipher_extraspec_tripledes =
  1015. {
  1016. run_selftests,
  1017. do_tripledes_set_extra_info
  1018. };