📄 netkey.c
字号:
#include <stdlib.h>#include <string.h>#include <stdio.h>
extern long read(int, void*, long);typedef unsigned char uchar;
typedef unsigned long ulong;
#define NAMELEN 28
/*********** auth.h ************/typedef struct Ticket Ticket;typedef struct Ticketreq Ticketreq;typedef struct Authenticator Authenticator;typedef struct Nvrsafe Nvrsafe;typedef struct Passwordreq Passwordreq;typedef struct Chalstate Chalstate;enum{ DOMLEN= 48, /* length of an authentication domain name */ DESKEYLEN= 7, /* length of a des key for encrypt/decrypt */ CHALLEN= 8, /* length of a challenge */ NETCHLEN= 16, /* max network challenge length */ CONFIGLEN= 14, KEYDBLEN= NAMELEN+DESKEYLEN+4+2};/* encryption numberings (anti-replay) */enum{ AuthTreq=1, /* ticket request */ AuthChal=2, /* challenge box request */ AuthPass=3, /* change password */ AuthOK=4, /* reply follows */ AuthErr=5, /* error follows */ AuthTs=64, /* ticket encrypted with server's key */ AuthTc, /* ticket encrypted with client's key */ AuthAs, /* server generated authenticator */ AuthAc /* client generated authenticator */};struct Ticketreq{ char type; char authid[NAMELEN]; /* server's encryption id */ char authdom[DOMLEN]; /* server's authentication domain */ char chal[CHALLEN]; /* challenge from server */ char hostid[NAMELEN]; /* host's encryption id */ char uid[NAMELEN]; /* uid of requesting user on host */};#define TICKREQLEN (3*NAMELEN+CHALLEN+DOMLEN+1)struct Ticket{ char num; /* replay protection */ char chal[CHALLEN]; /* server challenge */ char cuid[NAMELEN]; /* uid on client */ char suid[NAMELEN]; /* uid on server */ char key[DESKEYLEN]; /* nonce DES key */};#define TICKETLEN (CHALLEN+2*NAMELEN+DESKEYLEN+1)struct Authenticator{ char num; /* replay protection */ char chal[CHALLEN]; ulong id; /* authenticator id, ++'d with each auth */};#define AUTHENTLEN (CHALLEN+4+1)struct Passwordreq{ char num; char old[NAMELEN]; char new[NAMELEN];};#define PASSREQLEN (2*NAMELEN+1)struct Nvrsafe{ char machkey[DESKEYLEN]; uchar machsum; char authkey[DESKEYLEN]; uchar authsum; char config[CONFIGLEN]; uchar configsum; char authid[NAMELEN]; uchar authidsum; char authdom[DOMLEN]; uchar authdomsum;};struct Chalstate{ int afd; /* /dev/authenticate */ int asfd; /* authdial() */ char chal[NETCHLEN]; /* challenge/response */};/************ crypt.c *************//* * Data Encryption Standard * D.P.Mitchell 83/06/08. * * block_cipher(key, block, decrypting) */static long ip_low(char [8]);static long ip_high(char [8]);static void fp(long, long, char[8]);static void key_setup(char[DESKEYLEN], char[128]);static void block_cipher(char[128], char[8], int);/* * destructively encrypt the buffer, which * must be at least 8 characters long. */intencrypt9(void *key, void *vbuf, int n){ char ekey[128], *buf; int i, r; if(n < 8) return 0; key_setup(key, ekey); buf = vbuf; n--; r = n % 7; n /= 7; for(i = 0; i < n; i++){ block_cipher(ekey, buf, 0); buf += 7; } if(r) block_cipher(ekey, buf - 7 + r, 0); return 1;}/* * destructively decrypt the buffer, which * must be at least 8 characters long. */intdecrypt(void *key, void *vbuf, int n){ char ekey[128], *buf; int i, r; if(n < 8) return 0; key_setup(key, ekey); buf = vbuf; n--; r = n % 7; n /= 7; buf += n * 7; if(r) block_cipher(ekey, buf - 7 + r, 1); for(i = 0; i < n; i++){ buf -= 7; block_cipher(ekey, buf, 1); } return 1;}/* * Tables for Combined S and P Boxes */static long s0p[] = {0x00410100,0x00010000,0x40400000,0x40410100,0x00400000,0x40010100,0x40010000,0x40400000,0x40010100,0x00410100,0x00410000,0x40000100,0x40400100,0x00400000,0x00000000,0x40010000,0x00010000,0x40000000,0x00400100,0x00010100,0x40410100,0x00410000,0x40000100,0x00400100,0x40000000,0x00000100,0x00010100,0x40410000,0x00000100,0x40400100,0x40410000,0x00000000,0x00000000,0x40410100,0x00400100,0x40010000,0x00410100,0x00010000,0x40000100,0x00400100,0x40410000,0x00000100,0x00010100,0x40400000,0x40010100,0x40000000,0x40400000,0x00410000,0x40410100,0x00010100,0x00410000,0x40400100,0x00400000,0x40000100,0x40010000,0x00000000,0x00010000,0x00400000,0x40400100,0x00410100,0x40000000,0x40410000,0x00000100,0x40010100,};static long s1p[] = {0x08021002,0x00000000,0x00021000,0x08020000,0x08000002,0x00001002,0x08001000,0x00021000,0x00001000,0x08020002,0x00000002,0x08001000,0x00020002,0x08021000,0x08020000,0x00000002,0x00020000,0x08001002,0x08020002,0x00001000,0x00021002,0x08000000,0x00000000,0x00020002,0x08001002,0x00021002,0x08021000,0x08000002,0x08000000,0x00020000,0x00001002,0x08021002,0x00020002,0x08021000,0x08001000,0x00021002,0x08021002,0x00020002,0x08000002,0x00000000,0x08000000,0x00001002,0x00020000,0x08020002,0x00001000,0x08000000,0x00021002,0x08001002,0x08021000,0x00001000,0x00000000,0x08000002,0x00000002,0x08021002,0x00021000,0x08020000,0x08020002,0x00020000,0x00001002,0x08001000,0x08001002,0x00000002,0x08020000,0x00021000,};static long s2p[] = {0x20800000,0x00808020,0x00000020,0x20800020,0x20008000,0x00800000,0x20800020,0x00008020,0x00800020,0x00008000,0x00808000,0x20000000,0x20808020,0x20000020,0x20000000,0x20808000,0x00000000,0x20008000,0x00808020,0x00000020,0x20000020,0x20808020,0x00008000,0x20800000,0x20808000,0x00800020,0x20008020,0x00808000,0x00008020,0x00000000,0x00800000,0x20008020,0x00808020,0x00000020,0x20000000,0x00008000,0x20000020,0x20008000,0x00808000,0x20800020,0x00000000,0x00808020,0x00008020,0x20808000,0x20008000,0x00800000,0x20808020,0x20000000,0x20008020,0x20800000,0x00800000,0x20808020,0x00008000,0x00800020,0x20800020,0x00008020,0x00800020,0x00000000,0x20808000,0x20000020,0x20800000,0x20008020,0x00000020,0x00808000,};static long s3p[] = {0x00080201,0x02000200,0x00000001,0x02080201,0x00000000,0x02080000,0x02000201,0x00080001,0x02080200,0x02000001,0x02000000,0x00000201,0x02000001,0x00080201,0x00080000,0x02000000,0x02080001,0x00080200,0x00000200,0x00000001,0x00080200,0x02000201,0x02080000,0x00000200,0x00000201,0x00000000,0x00080001,0x02080200,0x02000200,0x02080001,0x02080201,0x00080000,0x02080001,0x00000201,0x00080000,0x02000001,0x00080200,0x02000200,0x00000001,0x02080000,0x02000201,0x00000000,0x00000200,0x00080001,0x00000000,0x02080001,0x02080200,0x00000200,0x02000000,0x02080201,0x00080201,0x00080000,0x02080201,0x00000001,0x02000200,0x00080201,0x00080001,0x00080200,0x02080000,0x02000201,0x00000201,0x02000000,0x02000001,0x02080200,};static long s4p[] = {0x01000000,0x00002000,0x00000080,0x01002084,0x01002004,0x01000080,0x00002084,0x01002000,0x00002000,0x00000004,0x01000004,0x00002080,0x01000084,0x01002004,0x01002080,0x00000000,0x00002080,0x01000000,0x00002004,0x00000084,0x01000080,0x00002084,0x00000000,0x01000004,0x00000004,0x01000084,0x01002084,0x00002004,0x01002000,0x00000080,0x00000084,0x01002080,0x01002080,0x01000084,0x00002004,0x01002000,0x00002000,0x00000004,0x01000004,0x01000080,0x01000000,0x00002080,0x01002084,0x00000000,0x00002084,0x01000000,0x00000080,0x00002004,0x01000084,0x00000080,0x00000000,0x01002084,0x01002004,0x01002080,0x00000084,0x00002000,0x00002080,0x01002004,0x01000080,0x00000084,0x00000004,0x00002084,0x01002000,0x01000004,};static long s5p[] = {0x10000008,0x00040008,0x00000000,0x10040400,0x00040008,0x00000400,0x10000408,0x00040000,0x00000408,0x10040408,0x00040400,0x10000000,0x10000400,0x10000008,0x10040000,0x00040408,0x00040000,0x10000408,0x10040008,0x00000000,0x00000400,0x00000008,0x10040400,0x10040008,0x10040408,0x10040000,0x10000000,0x00000408,0x00000008,0x00040400,0x00040408,0x10000400,0x00000408,0x10000000,0x10000400,0x00040408,0x10040400,0x00040008,0x00000000,0x10000400,0x10000000,0x00000400,0x10040008,0x00040000,0x00040008,0x10040408,0x00040400,0x00000008,0x10040408,0x00040400,0x00040000,0x10000408,0x10000008,0x10040000,0x00040408,0x00000000,0x00000400,0x10000008,0x10000408,0x10040400,0x10040000,0x00000408,0x00000008,0x10040008,};static long s6p[] = {0x00000800,0x00000040,0x00200040,0x80200000,0x80200840,0x80000800,0x00000840,0x00000000,0x00200000,0x80200040,0x80000040,0x00200800,0x80000000,0x00200840,0x00200800,0x80000040,0x80200040,0x00000800,0x80000800,0x80200840,0x00000000,0x00200040,0x80200000,0x00000840,0x80200800,0x80000840,0x00200840,0x80000000,0x80000840,0x80200800,0x00000040,0x00200000,0x80000840,0x00200800,0x80200800,0x80000040,0x00000800,0x00000040,0x00200000,0x80200800,0x80200040,0x80000840,0x00000840,0x00000000,0x00000040,0x80200000,0x80000000,0x00200040,0x00000000,0x80200040,0x00200040,0x00000840,0x80000040,0x00000800,0x80200840,0x00200000,0x00200840,0x80000000,0x80000800,0x80200840,0x80200000,0x00200840,0x00200800,0x80000800,};static long s7p[] = {0x04100010,0x04104000,0x00004010,0x00000000,0x04004000,0x00100010,0x04100000,0x04104010,0x00000010,0x04000000,0x00104000,0x00004010,0x00104010,0x04004010,0x04000010,0x04100000,0x00004000,0x00104010,0x00100010,0x04004000,0x04104010,0x04000010,0x00000000,0x00104000,0x04000000,0x00100000,0x04004010,0x04100010,0x00100000,0x00004000,0x04104000,0x00000010,0x00100000,0x00004000,0x04000010,0x04104010,0x00004010,0x04000000,0x00000000,0x00104000,0x04100010,0x04004010,0x04004000,0x00100010,0x04104000,0x00000010,0x00100010,0x04004000,0x04104010,0x00100000,0x04100000,0x04000010,0x00104000,0x00004010,0x04004010,0x04100000,0x00000010,0x04104000,0x00104010,0x00000000,0x04000000,0x04100010,0x00004000,0x00104010,};/* * DES electronic codebook encryption of one block */static voidblock_cipher(char expanded_key[128], char text[8], int decrypting){ char *key; long crypto, temp, right, left; int i, key_offset; key = expanded_key; left = ip_low(text); right = ip_high(text); if (decrypting) { key_offset = 16; key = key + 128 - 8; } else key_offset = 0; for (i = 0; i < 16; i++) { temp = (right << 1) | ((right >> 31) & 1); crypto = s0p[(temp & 0x3f) ^ *key++]; crypto |= s1p[((temp >> 4) & 0x3f) ^ *key++]; crypto |= s2p[((temp >> 8) & 0x3f) ^ *key++]; crypto |= s3p[((temp >> 12) & 0x3f) ^ *key++]; crypto |= s4p[((temp >> 16) & 0x3f) ^ *key++]; crypto |= s5p[((temp >> 20) & 0x3f) ^ *key++]; crypto |= s6p[((temp >> 24) & 0x3f) ^ *key++]; temp = ((right & 1) << 5) | ((right >> 27) & 0x1f); crypto |= s7p[temp ^ *key++]; temp = left; left = right; right = temp ^ crypto; key -= key_offset;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -