netkey.c 21 KB

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