netkey.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. extern int32_t read(int, void*, int32_t);
  13. typedef unsigned char uchar;
  14. typedef unsigned long ulong;
  15. #define NAMELEN 28
  16. /*********** auth.h ************/
  17. typedef struct Ticket Ticket;
  18. typedef struct Ticketreq Ticketreq;
  19. typedef struct Authenticator Authenticator;
  20. typedef struct Nvrsafe Nvrsafe;
  21. typedef struct Passwordreq Passwordreq;
  22. typedef struct Chalstate Chalstate;
  23. enum
  24. {
  25. DOMLEN= 48, /* length of an authentication domain name */
  26. DESKEYLEN= 7, /* length of a des key for encrypt/decrypt */
  27. CHALLEN= 8, /* length of a challenge */
  28. NETCHLEN= 16, /* max network challenge length */
  29. CONFIGLEN= 14,
  30. KEYDBLEN= NAMELEN+DESKEYLEN+4+2
  31. };
  32. /* encryption numberings (anti-replay) */
  33. enum
  34. {
  35. AuthTreq=1, /* ticket request */
  36. AuthChal=2, /* challenge box request */
  37. AuthPass=3, /* change password */
  38. AuthOK=4, /* reply follows */
  39. AuthErr=5, /* error follows */
  40. AuthTs=64, /* ticket encrypted with server's key */
  41. AuthTc, /* ticket encrypted with client's key */
  42. AuthAs, /* server generated authenticator */
  43. AuthAc /* client generated authenticator */
  44. };
  45. struct Ticketreq
  46. {
  47. char type;
  48. char authid[NAMELEN]; /* server's encryption id */
  49. char authdom[DOMLEN]; /* server's authentication domain */
  50. char chal[CHALLEN]; /* challenge from server */
  51. char hostid[NAMELEN]; /* host's encryption id */
  52. char uid[NAMELEN]; /* uid of requesting user on host */
  53. };
  54. #define TICKREQLEN (3*NAMELEN+CHALLEN+DOMLEN+1)
  55. struct Ticket
  56. {
  57. char num; /* replay protection */
  58. char chal[CHALLEN]; /* server challenge */
  59. char cuid[NAMELEN]; /* uid on client */
  60. char suid[NAMELEN]; /* uid on server */
  61. char key[DESKEYLEN]; /* nonce DES key */
  62. };
  63. #define TICKETLEN (CHALLEN+2*NAMELEN+DESKEYLEN+1)
  64. struct Authenticator
  65. {
  66. char num; /* replay protection */
  67. char chal[CHALLEN];
  68. uint32_t id; /* authenticator id, ++'d with each auth */
  69. };
  70. #define AUTHENTLEN (CHALLEN+4+1)
  71. struct Passwordreq
  72. {
  73. char num;
  74. char old[NAMELEN];
  75. char new[NAMELEN];
  76. };
  77. #define PASSREQLEN (2*NAMELEN+1)
  78. struct Nvrsafe
  79. {
  80. char machkey[DESKEYLEN];
  81. uint8_t machsum;
  82. char authkey[DESKEYLEN];
  83. uint8_t authsum;
  84. char config[CONFIGLEN];
  85. uint8_t configsum;
  86. char authid[NAMELEN];
  87. uint8_t authidsum;
  88. char authdom[DOMLEN];
  89. uint8_t authdomsum;
  90. };
  91. struct Chalstate
  92. {
  93. int afd; /* /dev/authenticate */
  94. int asfd; /* authdial() */
  95. char chal[NETCHLEN]; /* challenge/response */
  96. };
  97. /************ crypt.c *************/
  98. /*
  99. * Data Encryption Standard
  100. * D.P.Mitchell 83/06/08.
  101. *
  102. * block_cipher(key, block, decrypting)
  103. */
  104. static int32_t ip_low(char [8]);
  105. static int32_t ip_high(char [8]);
  106. static void fp(int32_t, int32_t, char[8]);
  107. static void key_setup(char[DESKEYLEN], char[128]);
  108. static void block_cipher(char[128], char[8], int);
  109. /*
  110. * destructively encrypt the buffer, which
  111. * must be at least 8 characters long.
  112. */
  113. int
  114. encrypt9(void *key, void *vbuf, int n)
  115. {
  116. char ekey[128], *buf;
  117. int i, r;
  118. if(n < 8)
  119. return 0;
  120. key_setup(key, ekey);
  121. buf = vbuf;
  122. n--;
  123. r = n % 7;
  124. n /= 7;
  125. for(i = 0; i < n; i++){
  126. block_cipher(ekey, buf, 0);
  127. buf += 7;
  128. }
  129. if(r)
  130. block_cipher(ekey, buf - 7 + r, 0);
  131. return 1;
  132. }
  133. /*
  134. * destructively decrypt the buffer, which
  135. * must be at least 8 characters long.
  136. */
  137. int
  138. decrypt(void *key, void *vbuf, int n)
  139. {
  140. char ekey[128], *buf;
  141. int i, r;
  142. if(n < 8)
  143. return 0;
  144. key_setup(key, ekey);
  145. buf = vbuf;
  146. n--;
  147. r = n % 7;
  148. n /= 7;
  149. buf += n * 7;
  150. if(r)
  151. block_cipher(ekey, buf - 7 + r, 1);
  152. for(i = 0; i < n; i++){
  153. buf -= 7;
  154. block_cipher(ekey, buf, 1);
  155. }
  156. return 1;
  157. }
  158. /*
  159. * Tables for Combined S and P Boxes
  160. */
  161. static int32_t s0p[] = {
  162. 0x00410100,0x00010000,0x40400000,0x40410100,0x00400000,0x40010100,0x40010000,0x40400000,
  163. 0x40010100,0x00410100,0x00410000,0x40000100,0x40400100,0x00400000,0x00000000,0x40010000,
  164. 0x00010000,0x40000000,0x00400100,0x00010100,0x40410100,0x00410000,0x40000100,0x00400100,
  165. 0x40000000,0x00000100,0x00010100,0x40410000,0x00000100,0x40400100,0x40410000,0x00000000,
  166. 0x00000000,0x40410100,0x00400100,0x40010000,0x00410100,0x00010000,0x40000100,0x00400100,
  167. 0x40410000,0x00000100,0x00010100,0x40400000,0x40010100,0x40000000,0x40400000,0x00410000,
  168. 0x40410100,0x00010100,0x00410000,0x40400100,0x00400000,0x40000100,0x40010000,0x00000000,
  169. 0x00010000,0x00400000,0x40400100,0x00410100,0x40000000,0x40410000,0x00000100,0x40010100,
  170. };
  171. static int32_t s1p[] = {
  172. 0x08021002,0x00000000,0x00021000,0x08020000,0x08000002,0x00001002,0x08001000,0x00021000,
  173. 0x00001000,0x08020002,0x00000002,0x08001000,0x00020002,0x08021000,0x08020000,0x00000002,
  174. 0x00020000,0x08001002,0x08020002,0x00001000,0x00021002,0x08000000,0x00000000,0x00020002,
  175. 0x08001002,0x00021002,0x08021000,0x08000002,0x08000000,0x00020000,0x00001002,0x08021002,
  176. 0x00020002,0x08021000,0x08001000,0x00021002,0x08021002,0x00020002,0x08000002,0x00000000,
  177. 0x08000000,0x00001002,0x00020000,0x08020002,0x00001000,0x08000000,0x00021002,0x08001002,
  178. 0x08021000,0x00001000,0x00000000,0x08000002,0x00000002,0x08021002,0x00021000,0x08020000,
  179. 0x08020002,0x00020000,0x00001002,0x08001000,0x08001002,0x00000002,0x08020000,0x00021000,
  180. };
  181. static int32_t s2p[] = {
  182. 0x20800000,0x00808020,0x00000020,0x20800020,0x20008000,0x00800000,0x20800020,0x00008020,
  183. 0x00800020,0x00008000,0x00808000,0x20000000,0x20808020,0x20000020,0x20000000,0x20808000,
  184. 0x00000000,0x20008000,0x00808020,0x00000020,0x20000020,0x20808020,0x00008000,0x20800000,
  185. 0x20808000,0x00800020,0x20008020,0x00808000,0x00008020,0x00000000,0x00800000,0x20008020,
  186. 0x00808020,0x00000020,0x20000000,0x00008000,0x20000020,0x20008000,0x00808000,0x20800020,
  187. 0x00000000,0x00808020,0x00008020,0x20808000,0x20008000,0x00800000,0x20808020,0x20000000,
  188. 0x20008020,0x20800000,0x00800000,0x20808020,0x00008000,0x00800020,0x20800020,0x00008020,
  189. 0x00800020,0x00000000,0x20808000,0x20000020,0x20800000,0x20008020,0x00000020,0x00808000,
  190. };
  191. static int32_t s3p[] = {
  192. 0x00080201,0x02000200,0x00000001,0x02080201,0x00000000,0x02080000,0x02000201,0x00080001,
  193. 0x02080200,0x02000001,0x02000000,0x00000201,0x02000001,0x00080201,0x00080000,0x02000000,
  194. 0x02080001,0x00080200,0x00000200,0x00000001,0x00080200,0x02000201,0x02080000,0x00000200,
  195. 0x00000201,0x00000000,0x00080001,0x02080200,0x02000200,0x02080001,0x02080201,0x00080000,
  196. 0x02080001,0x00000201,0x00080000,0x02000001,0x00080200,0x02000200,0x00000001,0x02080000,
  197. 0x02000201,0x00000000,0x00000200,0x00080001,0x00000000,0x02080001,0x02080200,0x00000200,
  198. 0x02000000,0x02080201,0x00080201,0x00080000,0x02080201,0x00000001,0x02000200,0x00080201,
  199. 0x00080001,0x00080200,0x02080000,0x02000201,0x00000201,0x02000000,0x02000001,0x02080200,
  200. };
  201. static int32_t s4p[] = {
  202. 0x01000000,0x00002000,0x00000080,0x01002084,0x01002004,0x01000080,0x00002084,0x01002000,
  203. 0x00002000,0x00000004,0x01000004,0x00002080,0x01000084,0x01002004,0x01002080,0x00000000,
  204. 0x00002080,0x01000000,0x00002004,0x00000084,0x01000080,0x00002084,0x00000000,0x01000004,
  205. 0x00000004,0x01000084,0x01002084,0x00002004,0x01002000,0x00000080,0x00000084,0x01002080,
  206. 0x01002080,0x01000084,0x00002004,0x01002000,0x00002000,0x00000004,0x01000004,0x01000080,
  207. 0x01000000,0x00002080,0x01002084,0x00000000,0x00002084,0x01000000,0x00000080,0x00002004,
  208. 0x01000084,0x00000080,0x00000000,0x01002084,0x01002004,0x01002080,0x00000084,0x00002000,
  209. 0x00002080,0x01002004,0x01000080,0x00000084,0x00000004,0x00002084,0x01002000,0x01000004,
  210. };
  211. static int32_t s5p[] = {
  212. 0x10000008,0x00040008,0x00000000,0x10040400,0x00040008,0x00000400,0x10000408,0x00040000,
  213. 0x00000408,0x10040408,0x00040400,0x10000000,0x10000400,0x10000008,0x10040000,0x00040408,
  214. 0x00040000,0x10000408,0x10040008,0x00000000,0x00000400,0x00000008,0x10040400,0x10040008,
  215. 0x10040408,0x10040000,0x10000000,0x00000408,0x00000008,0x00040400,0x00040408,0x10000400,
  216. 0x00000408,0x10000000,0x10000400,0x00040408,0x10040400,0x00040008,0x00000000,0x10000400,
  217. 0x10000000,0x00000400,0x10040008,0x00040000,0x00040008,0x10040408,0x00040400,0x00000008,
  218. 0x10040408,0x00040400,0x00040000,0x10000408,0x10000008,0x10040000,0x00040408,0x00000000,
  219. 0x00000400,0x10000008,0x10000408,0x10040400,0x10040000,0x00000408,0x00000008,0x10040008,
  220. };
  221. static int32_t s6p[] = {
  222. 0x00000800,0x00000040,0x00200040,0x80200000,0x80200840,0x80000800,0x00000840,0x00000000,
  223. 0x00200000,0x80200040,0x80000040,0x00200800,0x80000000,0x00200840,0x00200800,0x80000040,
  224. 0x80200040,0x00000800,0x80000800,0x80200840,0x00000000,0x00200040,0x80200000,0x00000840,
  225. 0x80200800,0x80000840,0x00200840,0x80000000,0x80000840,0x80200800,0x00000040,0x00200000,
  226. 0x80000840,0x00200800,0x80200800,0x80000040,0x00000800,0x00000040,0x00200000,0x80200800,
  227. 0x80200040,0x80000840,0x00000840,0x00000000,0x00000040,0x80200000,0x80000000,0x00200040,
  228. 0x00000000,0x80200040,0x00200040,0x00000840,0x80000040,0x00000800,0x80200840,0x00200000,
  229. 0x00200840,0x80000000,0x80000800,0x80200840,0x80200000,0x00200840,0x00200800,0x80000800,
  230. };
  231. static int32_t s7p[] = {
  232. 0x04100010,0x04104000,0x00004010,0x00000000,0x04004000,0x00100010,0x04100000,0x04104010,
  233. 0x00000010,0x04000000,0x00104000,0x00004010,0x00104010,0x04004010,0x04000010,0x04100000,
  234. 0x00004000,0x00104010,0x00100010,0x04004000,0x04104010,0x04000010,0x00000000,0x00104000,
  235. 0x04000000,0x00100000,0x04004010,0x04100010,0x00100000,0x00004000,0x04104000,0x00000010,
  236. 0x00100000,0x00004000,0x04000010,0x04104010,0x00004010,0x04000000,0x00000000,0x00104000,
  237. 0x04100010,0x04004010,0x04004000,0x00100010,0x04104000,0x00000010,0x00100010,0x04004000,
  238. 0x04104010,0x00100000,0x04100000,0x04000010,0x00104000,0x00004010,0x04004010,0x04100000,
  239. 0x00000010,0x04104000,0x00104010,0x00000000,0x04000000,0x04100010,0x00004000,0x00104010,
  240. };
  241. /*
  242. * DES electronic codebook encryption of one block
  243. */
  244. static void
  245. block_cipher(char expanded_key[128], char text[8], int decrypting)
  246. {
  247. char *key;
  248. int32_t crypto, temp, right, left;
  249. int i, key_offset;
  250. key = expanded_key;
  251. left = ip_low(text);
  252. right = ip_high(text);
  253. if (decrypting) {
  254. key_offset = 16;
  255. key = key + 128 - 8;
  256. } else
  257. key_offset = 0;
  258. for (i = 0; i < 16; i++) {
  259. temp = (right << 1) | ((right >> 31) & 1);
  260. crypto = s0p[(temp & 0x3f) ^ *key++];
  261. crypto |= s1p[((temp >> 4) & 0x3f) ^ *key++];
  262. crypto |= s2p[((temp >> 8) & 0x3f) ^ *key++];
  263. crypto |= s3p[((temp >> 12) & 0x3f) ^ *key++];
  264. crypto |= s4p[((temp >> 16) & 0x3f) ^ *key++];
  265. crypto |= s5p[((temp >> 20) & 0x3f) ^ *key++];
  266. crypto |= s6p[((temp >> 24) & 0x3f) ^ *key++];
  267. temp = ((right & 1) << 5) | ((right >> 27) & 0x1f);
  268. crypto |= s7p[temp ^ *key++];
  269. temp = left;
  270. left = right;
  271. right = temp ^ crypto;
  272. key -= key_offset;
  273. }
  274. /*
  275. * standard final permutation (IPI)
  276. * left and right are reversed here
  277. */
  278. fp(right, left, text);
  279. }
  280. /*
  281. * Initial Permutation
  282. */
  283. static int32_t iptab[] = {
  284. 0x00000000, 0x00008000, 0x00000000, 0x00008000,
  285. 0x00000080, 0x00008080, 0x00000080, 0x00008080
  286. };
  287. static int32_t
  288. ip_low(char block[8])
  289. {
  290. int i;
  291. int32_t l;
  292. l = 0;
  293. for(i = 0; i < 8; i++){
  294. l |= iptab[(block[i] >> 4) & 7] >> i;
  295. l |= iptab[block[i] & 7] << (16 - i);
  296. }
  297. return l;
  298. }
  299. static int32_t
  300. ip_high(char block[8])
  301. {
  302. int i;
  303. int32_t l;
  304. l = 0;
  305. for(i = 0; i < 8; i++){
  306. l |= iptab[(block[i] >> 5) & 7] >> i;
  307. l |= iptab[(block[i] >> 1) & 7] << (16 - i);
  308. }
  309. return l;
  310. }
  311. /*
  312. * Final Permutation
  313. */
  314. static unsigned long fptab[] = {
  315. 0x00000000,0x80000000,0x00800000,0x80800000,0x00008000,0x80008000,0x00808000,0x80808000,
  316. 0x00000080,0x80000080,0x00800080,0x80800080,0x00008080,0x80008080,0x00808080,0x80808080,
  317. };
  318. static void
  319. fp(int32_t left, int32_t right, char text[8])
  320. {
  321. unsigned long ta[2], t, v[2];
  322. int i, j, sh;
  323. ta[0] = right;
  324. ta[1] = left;
  325. v[0] = v[1] = 0;
  326. for(i = 0; i < 2; i++){
  327. t = ta[i];
  328. sh = i;
  329. for(j = 0; j < 4; j++){
  330. v[1] |= fptab[t & 0xf] >> sh;
  331. t >>= 4;
  332. v[0] |= fptab[t & 0xf] >> sh;
  333. t >>= 4;
  334. sh += 2;
  335. }
  336. }
  337. for(i = 0; i < 2; i++)
  338. for(j = 0; j < 4; j++){
  339. *text++ = (char)(v[i]&0xff);
  340. v[i] >>= 8;
  341. }
  342. }
  343. /*
  344. * Key set-up
  345. */
  346. static uint8_t keyexpand[][15][2] = {
  347. { 3, 2, 9, 8, 18, 8, 27, 32, 33, 2, 42, 16, 48, 8, 65, 16,
  348. 74, 2, 80, 2, 89, 4, 99, 16, 104, 4, 122, 32, 0, 0, },
  349. { 1, 4, 8, 1, 18, 4, 25, 32, 34, 32, 41, 8, 50, 8, 59, 32,
  350. 64, 16, 75, 4, 90, 1, 97, 16, 106, 2, 112, 2, 123, 1, },
  351. { 2, 1, 19, 8, 35, 1, 40, 1, 50, 4, 57, 32, 75, 2, 80, 32,
  352. 89, 1, 96, 16, 107, 4, 120, 8, 0, 0, 0, 0, 0, 0, },
  353. { 4, 32, 20, 2, 31, 4, 37, 32, 47, 1, 54, 1, 63, 2, 68, 1,
  354. 78, 4, 84, 8, 101, 16, 108, 4, 119, 16, 126, 8, 0, 0, },
  355. { 5, 4, 15, 4, 21, 32, 31, 1, 38, 1, 47, 2, 53, 2, 68, 8,
  356. 85, 16, 92, 4, 103, 16, 108, 32, 118, 32, 124, 2, 0, 0, },
  357. { 15, 2, 21, 2, 39, 8, 46, 16, 55, 32, 61, 1, 71, 16, 76, 32,
  358. 86, 32, 93, 4, 102, 2, 108, 16, 117, 8, 126, 1, 0, 0, },
  359. { 14, 16, 23, 32, 29, 1, 38, 8, 52, 2, 63, 4, 70, 2, 76, 16,
  360. 85, 8, 100, 1, 110, 4, 116, 8, 127, 8, 0, 0, 0, 0, },
  361. { 1, 8, 8, 32, 17, 1, 24, 16, 35, 4, 50, 1, 57, 16, 67, 8,
  362. 83, 1, 88, 1, 98, 4, 105, 32, 114, 32, 123, 2, 0, 0, },
  363. { 0, 1, 11, 16, 16, 4, 35, 2, 40, 32, 49, 1, 56, 16, 65, 2,
  364. 74, 16, 80, 8, 99, 8, 115, 1, 121, 4, 0, 0, 0, 0, },
  365. { 9, 16, 18, 2, 24, 2, 33, 4, 43, 16, 48, 4, 66, 32, 73, 8,
  366. 82, 8, 91, 32, 97, 2, 106, 16, 112, 8, 122, 1, 0, 0, },
  367. { 14, 32, 21, 4, 30, 2, 36, 16, 45, 8, 60, 1, 69, 2, 87, 8,
  368. 94, 16, 103, 32, 109, 1, 118, 8, 124, 32, 0, 0, 0, 0, },
  369. { 7, 4, 14, 2, 20, 16, 29, 8, 44, 1, 54, 4, 60, 8, 71, 8,
  370. 78, 16, 87, 32, 93, 1, 102, 8, 116, 2, 125, 4, 0, 0, },
  371. { 7, 2, 12, 1, 22, 4, 28, 8, 45, 16, 52, 4, 63, 16, 70, 8,
  372. 84, 2, 95, 4, 101, 32, 111, 1, 118, 1, 0, 0, 0, 0, },
  373. { 6, 16, 13, 16, 20, 4, 31, 16, 36, 32, 46, 32, 53, 4, 62, 2,
  374. 69, 32, 79, 1, 86, 1, 95, 2, 101, 2, 119, 8, 0, 0, },
  375. { 0, 32, 10, 8, 19, 32, 25, 2, 34, 16, 40, 8, 59, 8, 66, 2,
  376. 72, 2, 81, 4, 91, 16, 96, 4, 115, 2, 121, 8, 0, 0, },
  377. { 3, 16, 10, 4, 17, 32, 26, 32, 33, 8, 42, 8, 51, 32, 57, 2,
  378. 67, 4, 82, 1, 89, 16, 98, 2, 104, 2, 113, 4, 120, 1, },
  379. { 1, 16, 11, 8, 27, 1, 32, 1, 42, 4, 49, 32, 58, 32, 67, 2,
  380. 72, 32, 81, 1, 88, 16, 99, 4, 114, 1, 0, 0, 0, 0, },
  381. { 6, 32, 12, 2, 23, 4, 29, 32, 39, 1, 46, 1, 55, 2, 61, 2,
  382. 70, 4, 76, 8, 93, 16, 100, 4, 111, 16, 116, 32, 0, 0, },
  383. { 6, 2, 13, 32, 23, 1, 30, 1, 39, 2, 45, 2, 63, 8, 77, 16,
  384. 84, 4, 95, 16, 100, 32, 110, 32, 117, 4, 127, 4, 0, 0, },
  385. { 4, 1, 13, 2, 31, 8, 38, 16, 47, 32, 53, 1, 62, 8, 68, 32,
  386. 78, 32, 85, 4, 94, 2, 100, 16, 109, 8, 127, 2, 0, 0, },
  387. { 5, 16, 15, 32, 21, 1, 30, 8, 44, 2, 55, 4, 61, 32, 68, 16,
  388. 77, 8, 92, 1, 102, 4, 108, 8, 126, 16, 0, 0, 0, 0, },
  389. { 2, 8, 9, 1, 16, 16, 27, 4, 42, 1, 49, 16, 58, 2, 75, 1,
  390. 80, 1, 90, 4, 97, 32, 106, 32, 113, 8, 120, 32, 0, 0, },
  391. { 2, 4, 8, 4, 27, 2, 32, 32, 41, 1, 48, 16, 59, 4, 66, 16,
  392. 72, 8, 91, 8, 107, 1, 112, 1, 123, 16, 0, 0, 0, 0, },
  393. { 3, 8, 10, 2, 16, 2, 25, 4, 35, 16, 40, 4, 59, 2, 65, 8,
  394. 74, 8, 83, 32, 89, 2, 98, 16, 104, 8, 121, 16, 0, 0, },
  395. { 4, 2, 13, 4, 22, 2, 28, 16, 37, 8, 52, 1, 62, 4, 79, 8,
  396. 86, 16, 95, 32, 101, 1, 110, 8, 126, 32, 0, 0, 0, 0, },
  397. { 5, 32, 12, 16, 21, 8, 36, 1, 46, 4, 52, 8, 70, 16, 79, 32,
  398. 85, 1, 94, 8, 108, 2, 119, 4, 126, 2, 0, 0, 0, 0, },
  399. { 5, 2, 14, 4, 20, 8, 37, 16, 44, 4, 55, 16, 60, 32, 76, 2,
  400. 87, 4, 93, 32, 103, 1, 110, 1, 119, 2, 124, 1, 0, 0, },
  401. { 7, 32, 12, 4, 23, 16, 28, 32, 38, 32, 45, 4, 54, 2, 60, 16,
  402. 71, 1, 78, 1, 87, 2, 93, 2, 111, 8, 118, 16, 125, 16, },
  403. { 1, 1, 11, 32, 17, 2, 26, 16, 32, 8, 51, 8, 64, 2, 73, 4,
  404. 83, 16, 88, 4, 107, 2, 112, 32, 122, 8, 0, 0, 0, 0, },
  405. { 0, 4, 9, 32, 18, 32, 25, 8, 34, 8, 43, 32, 49, 2, 58, 16,
  406. 74, 1, 81, 16, 90, 2, 96, 2, 105, 4, 115, 16, 122, 4, },
  407. { 2, 2, 19, 1, 24, 1, 34, 4, 41, 32, 50, 32, 57, 8, 64, 32,
  408. 73, 1, 80, 16, 91, 4, 106, 1, 113, 16, 123, 8, 0, 0, },
  409. { 3, 4, 10, 16, 16, 8, 35, 8, 51, 1, 56, 1, 67, 16, 72, 4,
  410. 91, 2, 96, 32, 105, 1, 112, 16, 121, 2, 0, 0, 0, 0, },
  411. { 4, 16, 15, 1, 22, 1, 31, 2, 37, 2, 55, 8, 62, 16, 69, 16,
  412. 76, 4, 87, 16, 92, 32, 102, 32, 109, 4, 118, 2, 125, 32, },
  413. { 6, 4, 23, 8, 30, 16, 39, 32, 45, 1, 54, 8, 70, 32, 77, 4,
  414. 86, 2, 92, 16, 101, 8, 116, 1, 125, 2, 0, 0, 0, 0, },
  415. { 4, 4, 13, 1, 22, 8, 36, 2, 47, 4, 53, 32, 63, 1, 69, 8,
  416. 84, 1, 94, 4, 100, 8, 117, 16, 127, 32, 0, 0, 0, 0, },
  417. { 3, 32, 8, 16, 19, 4, 34, 1, 41, 16, 50, 2, 56, 2, 67, 1,
  418. 72, 1, 82, 4, 89, 32, 98, 32, 105, 8, 114, 8, 121, 1, },
  419. { 1, 32, 19, 2, 24, 32, 33, 1, 40, 16, 51, 4, 64, 8, 83, 8,
  420. 99, 1, 104, 1, 114, 4, 120, 4, 0, 0, 0, 0, 0, 0, },
  421. { 8, 2, 17, 4, 27, 16, 32, 4, 51, 2, 56, 32, 66, 8, 75, 32,
  422. 81, 2, 90, 16, 96, 8, 115, 8, 122, 2, 0, 0, 0, 0, },
  423. { 2, 16, 18, 1, 25, 16, 34, 2, 40, 2, 49, 4, 59, 16, 66, 4,
  424. 73, 32, 82, 32, 89, 8, 98, 8, 107, 32, 113, 2, 123, 4, },
  425. { 7, 1, 13, 8, 28, 1, 38, 4, 44, 8, 61, 16, 71, 32, 77, 1,
  426. 86, 8, 100, 2, 111, 4, 117, 32, 124, 16, 0, 0, 0, 0, },
  427. { 12, 8, 29, 16, 36, 4, 47, 16, 52, 32, 62, 32, 68, 2, 79, 4,
  428. 85, 32, 95, 1, 102, 1, 111, 2, 117, 2, 126, 4, 0, 0, },
  429. { 5, 1, 15, 16, 20, 32, 30, 32, 37, 4, 46, 2, 52, 16, 61, 8,
  430. 70, 1, 79, 2, 85, 2, 103, 8, 110, 16, 119, 32, 124, 4, },
  431. { 0, 16, 9, 2, 18, 16, 24, 8, 43, 8, 59, 1, 65, 4, 75, 16,
  432. 80, 4, 99, 2, 104, 32, 113, 1, 123, 32, 0, 0, 0, 0, },
  433. { 10, 32, 17, 8, 26, 8, 35, 32, 41, 2, 50, 16, 56, 8, 66, 1,
  434. 73, 16, 82, 2, 88, 2, 97, 4, 107, 16, 112, 4, 121, 32, },
  435. { 0, 2, 11, 1, 16, 1, 26, 4, 33, 32, 42, 32, 49, 8, 58, 8,
  436. 65, 1, 72, 16, 83, 4, 98, 1, 105, 16, 114, 2, 0, 0, },
  437. { 8, 8, 27, 8, 43, 1, 48, 1, 58, 4, 64, 4, 83, 2, 88, 32,
  438. 97, 1, 104, 16, 115, 4, 122, 16, 0, 0, 0, 0, 0, 0, },
  439. { 5, 8, 14, 1, 23, 2, 29, 2, 47, 8, 54, 16, 63, 32, 68, 4,
  440. 79, 16, 84, 32, 94, 32, 101, 4, 110, 2, 116, 16, 127, 1, },
  441. { 4, 8, 15, 8, 22, 16, 31, 32, 37, 1, 46, 8, 60, 2, 69, 4,
  442. 78, 2, 84, 16, 93, 8, 108, 1, 118, 4, 0, 0, 0, 0, },
  443. { 7, 16, 14, 8, 28, 2, 39, 4, 45, 32, 55, 1, 62, 1, 76, 1,
  444. 86, 4, 92, 8, 109, 16, 116, 4, 125, 1, 0, 0, 0, 0, },
  445. { 1, 2, 11, 4, 26, 1, 33, 16, 42, 2, 48, 2, 57, 4, 64, 1,
  446. 74, 4, 81, 32, 90, 32, 97, 8, 106, 8, 115, 32, 120, 16, },
  447. { 2, 32, 11, 2, 16, 32, 25, 1, 32, 16, 43, 4, 58, 1, 75, 8,
  448. 91, 1, 96, 1, 106, 4, 113, 32, 0, 0, 0, 0, 0, 0, },
  449. { 3, 1, 9, 4, 19, 16, 24, 4, 43, 2, 48, 32, 57, 1, 67, 32,
  450. 73, 2, 82, 16, 88, 8, 107, 8, 120, 2, 0, 0, 0, 0, },
  451. { 0, 8, 10, 1, 17, 16, 26, 2, 32, 2, 41, 4, 51, 16, 56, 4,
  452. 65, 32, 74, 32, 81, 8, 90, 8, 99, 32, 105, 2, 114, 16, },
  453. { 6, 1, 20, 1, 30, 4, 36, 8, 53, 16, 60, 4, 69, 1, 78, 8,
  454. 92, 2, 103, 4, 109, 32, 119, 1, 125, 8, 0, 0, 0, 0, },
  455. { 7, 8, 21, 16, 28, 4, 39, 16, 44, 32, 54, 32, 61, 4, 71, 4,
  456. 77, 32, 87, 1, 94, 1, 103, 2, 109, 2, 124, 8, 0, 0, },
  457. { 6, 8, 12, 32, 22, 32, 29, 4, 38, 2, 44, 16, 53, 8, 71, 2,
  458. 77, 2, 95, 8, 102, 16, 111, 32, 117, 1, 127, 16, 0, 0, }
  459. };
  460. static void
  461. key_setup(char key[DESKEYLEN], char *ek)
  462. {
  463. int i, j, k, mask;
  464. uint8_t (*x)[2];
  465. memset(ek, 0, 128);
  466. x = keyexpand[0];
  467. for(i = 0; i < 7; i++){
  468. k = key[i];
  469. for(mask = 0x80; mask; mask >>= 1){
  470. if(k & mask)
  471. for(j = 0; j < 15; j++)
  472. ek[x[j][0]] |= x[j][1];
  473. x += 15;
  474. }
  475. }
  476. }
  477. /************ netkey main.c *************/
  478. int
  479. passtokey(char *key, char *p)
  480. {
  481. uint8_t buf[NAMELEN], *t;
  482. int i, n;
  483. n = strlen(p);
  484. if(n >= NAMELEN)
  485. n = NAMELEN-1;
  486. memset(buf, ' ', 8);
  487. t = buf;
  488. strncpy((char*)t, p, n);
  489. t[n] = '\0';
  490. memset(key, 0, DESKEYLEN);
  491. for(;;){
  492. for(i = 0; i < DESKEYLEN; i++)
  493. key[i] = (t[i] >> i) + (t[i+1] << (8 - (i+1)));
  494. if(n <= 8)
  495. return 1;
  496. n -= 8;
  497. t += 8;
  498. if(n < 8){
  499. t -= 8 - n;
  500. n = 8;
  501. }
  502. encrypt9(key, t, 8);
  503. }
  504. }
  505. int
  506. netcrypt(void *key, void *chal)
  507. {
  508. uint8_t buf[8], *p;
  509. strncpy((char*)buf, chal, 7);
  510. buf[7] = '\0';
  511. for(p = buf; *p && *p != '\n'; p++)
  512. ;
  513. *p = '\0';
  514. encrypt9(key, buf, 8);
  515. sprintf(chal, "%.2x%.2x%.2x%.2x", buf[0], buf[1], buf[2], buf[3]);
  516. return 1;
  517. }
  518. void
  519. main(int argc, char *argv[])
  520. {
  521. char buf[32], pass[32], key[DESKEYLEN];
  522. int n;
  523. printf("Run this directly on the local processor, NOT in a\n");
  524. printf(" window to a computer across the network.\n");
  525. printf("Type when no one else is looking.\n\n");
  526. printf("password: ");
  527. fflush(stdout);
  528. n = read(0, pass, sizeof pass - 1);
  529. if(n <= 0)
  530. exit(0);
  531. pass[n] = 0;
  532. if(pass[n-1]=='\n')
  533. pass[--n] = 0;
  534. if(pass[n-1]=='\r')
  535. pass[--n] = 0;
  536. passtokey(key,pass);
  537. for(;;){
  538. printf("challenge: ");
  539. fflush(stdout);
  540. n = read(0, buf, sizeof buf - 1);
  541. if(n <= 0)
  542. exit(0);
  543. buf[n] = '\0';
  544. netcrypt(key, buf);
  545. printf("response: %s\n", buf);
  546. }
  547. }