Remove CR in keygen.c

This commit is contained in:
Giulio 2020-09-19 18:21:57 +02:00
parent aacab67bb8
commit 6cde529c76

View File

@ -1,279 +1,279 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <stdint.h> #include <stdint.h>
typedef union uwb typedef union uwb
{ {
unsigned w; unsigned w;
unsigned char b[4]; unsigned char b[4];
} }
WBunion; WBunion;
typedef unsigned Digest[4]; typedef unsigned Digest[4];
unsigned f0(unsigned abcd[]) unsigned f0(unsigned abcd[])
{ {
return (abcd[1] &abcd[2]) | (~abcd[1] &abcd[3]); return (abcd[1] &abcd[2]) | (~abcd[1] &abcd[3]);
} }
unsigned f1(unsigned abcd[]) unsigned f1(unsigned abcd[])
{ {
return (abcd[3] &abcd[1]) | (~abcd[3] &abcd[2]); return (abcd[3] &abcd[1]) | (~abcd[3] &abcd[2]);
} }
unsigned f2(unsigned abcd[]) unsigned f2(unsigned abcd[])
{ {
return abcd[1] ^ abcd[2] ^ abcd[3]; return abcd[1] ^ abcd[2] ^ abcd[3];
} }
unsigned f3(unsigned abcd[]) unsigned f3(unsigned abcd[])
{ {
return abcd[2] ^ (abcd[1] | ~abcd[3]); return abcd[2] ^ (abcd[1] | ~abcd[3]);
} }
typedef unsigned(*DgstFctn)(unsigned a[]); typedef unsigned(*DgstFctn)(unsigned a[]);
unsigned* calcKs(unsigned *k) unsigned* calcKs(unsigned *k)
{ {
double s, pwr; double s, pwr;
int i; int i;
pwr = pow(2, 32); pwr = pow(2, 32);
for (i = 0; i < 64; i++) for (i = 0; i < 64; i++)
{ {
s = fabs(sin(1 + i)); s = fabs(sin(1 + i));
k[i] = (unsigned)(s *pwr); k[i] = (unsigned)(s *pwr);
} }
return k; return k;
} }
// ROtate v Left by amt bits // ROtate v Left by amt bits
unsigned rol(unsigned v, short amt) unsigned rol(unsigned v, short amt)
{ {
unsigned msk1 = (1 << amt) - 1; unsigned msk1 = (1 << amt) - 1;
return ((v >> (32 - amt)) &msk1) | ((v << amt) &~msk1); return ((v >> (32 - amt)) &msk1) | ((v << amt) &~msk1);
} }
unsigned* md5(const char *msg, int mlen) unsigned* md5(const char *msg, int mlen)
{ {
static Digest h0 = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476 static Digest h0 = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476
}; };
//static Digest h0 = { 0x01234567, 0x89ABCDEF, 0xFEDCBA98, 0x76543210 }; //static Digest h0 = { 0x01234567, 0x89ABCDEF, 0xFEDCBA98, 0x76543210 };
static DgstFctn ff[] = { &f0, &f1, &f2, &f3 static DgstFctn ff[] = { &f0, &f1, &f2, &f3
}; };
static short M[] = { 1, 5, 3, 7 }; static short M[] = { 1, 5, 3, 7 };
static short O[] = { 0, 1, 5, 0 }; static short O[] = { 0, 1, 5, 0 };
static short rot0[] = { 7, 12, 17, 22 }; static short rot0[] = { 7, 12, 17, 22 };
static short rot1[] = { 5, 9, 14, 20 }; static short rot1[] = { 5, 9, 14, 20 };
static short rot2[] = { 4, 11, 16, 23 }; static short rot2[] = { 4, 11, 16, 23 };
static short rot3[] = { 6, 10, 15, 21 }; static short rot3[] = { 6, 10, 15, 21 };
static short *rots[] = { rot0, rot1, rot2, rot3 static short *rots[] = { rot0, rot1, rot2, rot3
}; };
static unsigned kspace[64]; static unsigned kspace[64];
static unsigned * k; static unsigned * k;
static Digest h; static Digest h;
Digest abcd; Digest abcd;
DgstFctn fctn; DgstFctn fctn;
short m, o, g; short m, o, g;
unsigned f; unsigned f;
short * rotn; short * rotn;
union union
{ {
unsigned w[16]; unsigned w[16];
char b[64]; char b[64];
} }
mm; mm;
int os = 0; int os = 0;
int grp, grps, q, p; int grp, grps, q, p;
unsigned char *msg2; unsigned char *msg2;
if (k == NULL) k = calcKs(kspace); if (k == NULL) k = calcKs(kspace);
for (q = 0; q < 4; q++) h[q] = h0[q]; for (q = 0; q < 4; q++) h[q] = h0[q];
{ {
grps = 1 + (mlen + 8) / 64; grps = 1 + (mlen + 8) / 64;
msg2 = malloc(64 *grps); msg2 = malloc(64 *grps);
memcpy(msg2, msg, mlen); memcpy(msg2, msg, mlen);
msg2[mlen] = (unsigned char) 0x80; msg2[mlen] = (unsigned char) 0x80;
q = mlen + 1; q = mlen + 1;
while (q < 64 *grps) while (q < 64 *grps)
{ {
msg2[q] = 0; msg2[q] = 0;
q++; q++;
} }
{ {
WBunion u; WBunion u;
u.w = 8 * mlen; u.w = 8 * mlen;
q -= 8; q -= 8;
memcpy(msg2 + q, &u.w, 4); memcpy(msg2 + q, &u.w, 4);
} }
} }
for (grp = 0; grp < grps; grp++) for (grp = 0; grp < grps; grp++)
{ {
memcpy(mm.b, msg2 + os, 64); memcpy(mm.b, msg2 + os, 64);
for (q = 0; q < 4; q++) abcd[q] = h[q]; for (q = 0; q < 4; q++) abcd[q] = h[q];
for (p = 0; p < 4; p++) for (p = 0; p < 4; p++)
{ {
fctn = ff[p]; fctn = ff[p];
rotn = rots[p]; rotn = rots[p];
m = M[p]; m = M[p];
o = O[p]; o = O[p];
for (q = 0; q < 16; q++) for (q = 0; q < 16; q++)
{ {
g = (m *q + o) % 16; g = (m *q + o) % 16;
f = abcd[1] + rol(abcd[0] + fctn(abcd) + k[q + 16 *p] + mm.w[g], rotn[q % 4]); f = abcd[1] + rol(abcd[0] + fctn(abcd) + k[q + 16 *p] + mm.w[g], rotn[q % 4]);
abcd[0] = abcd[3]; abcd[0] = abcd[3];
abcd[3] = abcd[2]; abcd[3] = abcd[2];
abcd[2] = abcd[1]; abcd[2] = abcd[1];
abcd[1] = f; abcd[1] = f;
} }
} }
for (p = 0; p < 4; p++) for (p = 0; p < 4; p++)
h[p] += abcd[p]; h[p] += abcd[p];
os += 64; os += 64;
} }
if (msg2) if (msg2)
free(msg2); free(msg2);
return h; return h;
} }
char *get_mac() char *get_mac()
{ {
FILE * fp; FILE * fp;
char *mac = malloc(18* sizeof(char)); char *mac = malloc(18* sizeof(char));
fp = fopen("/sys/class/net/ap0/address", "r"); fp = fopen("/sys/class/net/ap0/address", "r");
fgets(mac, 17, fp); fgets(mac, 17, fp);
fclose(fp); fclose(fp);
return mac; return mac;
} }
char *get_serial() char *get_serial()
{ {
FILE * fp; FILE * fp;
char *serial = malloc(21* sizeof(char)); char *serial = malloc(21* sizeof(char));
fp = fopen("/etc/serial", "r"); fp = fopen("/etc/serial", "r");
fgets(serial, 20, fp); fgets(serial, 20, fp);
return serial; return serial;
} }
int gen_serial() int gen_serial()
{ {
// hexdump -n 16 -e '4/4 "%08X" 1 "\n"' /dev/random // hexdump -n 16 -e '4/4 "%08X" 1 "\n"' /dev/random
if (fopen("/etc/serial", "r") == NULL) if (fopen("/etc/serial", "r") == NULL)
{ {
system("/usr/bin/hexdump -n 16 -e '4/4 \"%08X\"' /dev/urandom > /etc/serial"); system("/usr/bin/hexdump -n 16 -e '4/4 \"%08X\"' /dev/urandom > /etc/serial");
} }
} }
int gen_ssid() int gen_ssid()
{ {
char *serial; char *serial;
FILE * fp; FILE * fp;
char ssid[20] = "CyberChallenge-"; char ssid[20] = "CyberChallenge-";
serial = get_serial(); serial = get_serial();
strncat(ssid, serial, 4); strncat(ssid, serial, 4);
fp = fopen("/etc/ssid", "w"); fp = fopen("/etc/ssid", "w");
fprintf(fp, "%s", ssid); fprintf(fp, "%s", ssid);
fclose(fp); fclose(fp);
} }
int gen_key(char *serial, char *mac) int gen_key(char *serial, char *mac)
{ {
FILE * fp; FILE * fp;
char md5a[33]; char md5a[33];
char md5b[33]; char md5b[33];
char tmp[3]; char tmp[3];
char key[21]; char key[21];
char wstr[9]; char wstr[9];
char xstr[9]; char xstr[9];
char ystr[9]; char ystr[9];
char zstr[9]; char zstr[9];
uint32_t w, x, y, z; uint32_t w, x, y, z;
long long int seed; long long int seed;
int j, k; int j, k;
unsigned long int h; unsigned long int h;
WBunion u; WBunion u;
md5a[0] = '\0'; md5a[0] = '\0';
md5b[0] = '\0'; md5b[0] = '\0';
wstr[8] = '\0'; wstr[8] = '\0';
xstr[8] = '\0'; xstr[8] = '\0';
ystr[8] = '\0'; ystr[8] = '\0';
zstr[8] = '\0'; zstr[8] = '\0';
unsigned *d = md5(mac, strlen(mac)); unsigned *d = md5(mac, strlen(mac));
for (j = 0; j < 4; j++) for (j = 0; j < 4; j++)
{ {
u.w = d[j]; u.w = d[j];
for (k = 0; k < 4; k++) for (k = 0; k < 4; k++)
{ {
snprintf(tmp, 3, "%02x", u.b[k]); snprintf(tmp, 3, "%02x", u.b[k]);
strncat(md5a, tmp, 2); strncat(md5a, tmp, 2);
} }
} }
unsigned *f = md5(serial, strlen(serial)); unsigned *f = md5(serial, strlen(serial));
for (j = 0; j < 4; j++) for (j = 0; j < 4; j++)
{ {
u.w = f[j]; u.w = f[j];
for (k = 0; k < 4; k++) for (k = 0; k < 4; k++)
{ {
snprintf(tmp, 3, "%02x", u.b[k]); snprintf(tmp, 3, "%02x", u.b[k]);
strncat(md5b, tmp, 2); strncat(md5b, tmp, 2);
} }
} }
strncpy(wstr, md5a, 8); strncpy(wstr, md5a, 8);
strncpy(xstr, md5a + 8, 8); strncpy(xstr, md5a + 8, 8);
strncpy(ystr, md5a + 16, 8); strncpy(ystr, md5a + 16, 8);
strncpy(zstr, md5a + 24, 8); strncpy(zstr, md5a + 24, 8);
w = strtoul(wstr, NULL, 16); w = strtoul(wstr, NULL, 16);
x = strtoul(xstr, NULL, 16); x = strtoul(xstr, NULL, 16);
y = strtoul(ystr, NULL, 16); y = strtoul(ystr, NULL, 16);
z = strtoul(zstr, NULL, 16); z = strtoul(zstr, NULL, 16);
for (int i = 0; i < 20; ++i) for (int i = 0; i < 20; ++i)
{ {
uint32_t t = x; uint32_t t = x;
t ^= t << 11U; t ^= t << 11U;
t ^= t >> 8U; t ^= t >> 8U;
x = y; x = y;
y = z; y = z;
z = w; z = w;
w ^= w >> 19U; w ^= w >> 19U;
w ^= t; w ^= t;
key[i] = md5b[t % 20]; key[i] = md5b[t % 20];
} }
key[20] = '\0'; key[20] = '\0';
fp = fopen("/etc/wpa", "w"); fp = fopen("/etc/wpa", "w");
fprintf(fp, "%s", key); fprintf(fp, "%s", key);
fclose(fp); fclose(fp);
} }
int main() int main()
{ {
char *mac; char *mac;
char *serial; char *serial;
gen_serial(); gen_serial();
gen_ssid(); gen_ssid();
mac = get_mac(); mac = get_mac();
serial = get_serial(); serial = get_serial();
gen_key(mac, serial); gen_key(mac, serial);
return 0; return 0;
} }