summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/wlan-ng26-utils/tmp/trunk/add-ons/keygen/.svn/text-base/keygen.c.svn-base
blob: fe2973811f0e2e11225764785a58416d1777e1fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * keygen.c
 *	WEP Key Generators
 *
 * This program generates WEP keys using de facto standard key
 * generators for 40 and 128 bit keys.
 *
 * Link against OpenSSL's libcrypto.a
 *
 * I place this code in the public domain.
 * May 2001, Tim Newsham
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <openssl/md5.h>

#define WEPKEYSIZE      	5
#define WEPSTRONGKEYSIZE	13
#define WEPKEYS         	4
#define WEPKEYSTORE     	(WEPKEYSIZE * WEPKEYS)
#define WEPSTRONGKEYSTORE	(WEPSTRONGKEYSIZE * WEPKEYS)

/*
 * generate four subkeys from a seed using the defacto standard
 */
void
wep_seedkeygen(int val, u_char *keys)
{
    int i;

    for(i = 0; i < WEPKEYSTORE; i++) {
        val *= 0x343fd;
        val += 0x269ec3;
        keys[i] = val >> 16;
    }
    return;
}

/*
 * generate one key from a string using the de facto standard
 *
 * resultant key is stored in
 *   one 128 bit key: keys[0-15]
 *
 * (NOTE: I'm not sure why, but it seems that only values 0-12 are used,
 * resulting in 104 bits of keying, not 128)
 */
void
wep_keygen128(char *str, u_char *keys)
{
    MD5_CTX ctx;
    u_char buf[64];
    int i, j;

    /* repeat str until buf is full */
    j = 0;
    for(i = 0; i < 64; i++) {
        if(str[j] == 0)
            j = 0;
        buf[i] = str[j++];
    }

    MD5_Init(&ctx);
    MD5_Update(&ctx, buf, sizeof buf);
    MD5_Final(buf, &ctx);

    memcpy(keys, buf, WEPSTRONGKEYSTORE);
    for(i = 0; i < WEPSTRONGKEYSIZE; i++) {
        keys[i] = buf[i];
    }
    for(; i < WEPSTRONGKEYSTORE; i++) {
        keys[i] = 0;
    }
    return;
}

/* 
 * generate four subkeys from a string using the defacto standard
 *
 * resultant keys are stored in 
 *   four 40 bit keys: keys[0-4], keys[5-9], keys[10-14] and keys[15-20]
 */
void
wep_keygen40(char *str, u_char *keys) 
{
    int val, i, shift;

    /*
     * seed is generated by xor'ing in the keystring bytes
     * into the four bytes of the seed, starting at the little end
     */
    val = 0;
    for(i = 0; str[i]; i++) {
        shift = i & 0x3;
        val ^= (str[i] << (shift * 8));
    }

    wep_seedkeygen(val, keys);
    return;
}

void
wep_keyprint40(u_char *keys)
{
    int i;
    char sepchar;

    for(i = 0; i < WEPKEYSTORE; i++) {
        sepchar = (i % WEPKEYSIZE == WEPKEYSIZE - 1) ? '\n' : ':';
        printf("%02x%c", keys[i], sepchar);
    }
    return;
}

void
wep_keyprint128(u_char *keys)
{
    int i;
    char sepchar;

    for(i = 0; i < WEPSTRONGKEYSTORE; i++) {
        sepchar = (i % WEPSTRONGKEYSIZE == WEPSTRONGKEYSIZE - 1) ? '\n' : ':';
        printf("%02x%c", keys[i], sepchar);
    }
    return;
}
void
usage(char *prog)
{
    printf("Usage:  %s keystring [-s || 5 || 13]\n", prog);
    exit(1);
}

int
main(int argc, char **argv) 
{
    u_char keys[WEPKEYSTORE];
    u_char strongkeys[WEPSTRONGKEYSTORE];
    char *prog, *genstr;
    int strong, ch;

    prog = argv[0];
    strong = 0;
    while((ch = getopt(argc, argv, "s")) != EOF) {
        switch(ch) {
        case 's':
            strong ++;
            break;
        default:
            usage(prog);
        }
    }
    argc -= optind;
    argv += optind;

    if(argc==2) 
    	if(!strcmp(argv[1],"13")) 
	{ 
		strong++; 
		argc--; 
	}
	else argc--;

    if(argc != 1)
        usage(prog);
    genstr = argv[0];

    if(strong)
    {
        wep_keygen128(genstr, strongkeys);
        wep_keyprint128(strongkeys);
    }
    else
    {
        wep_keygen40(genstr, keys);
        wep_keyprint40(keys);
    }
    return 0;
}