summaryrefslogtreecommitdiff
path: root/lib/lib3.c
blob: 150c6ea51bcef7b56b17fca30fbd0e850d8d2141 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "lib3.h"
#include "lib2.h"
#include "lib.h"


#define NR_STIRNGS_CHALLENGE17 10

char *challenge17_encrypt(int *length)
{
	int i, t;
	char **string;
	char **decoded;
	string = malloc(sizeof(char *)*NR_STIRNGS_CHALLENGE17);
	decoded = malloc(sizeof(char *)*NR_STIRNGS_CHALLENGE17);
	char filename[] = "task17_0";	

	for(i=0;i<10;i++) {
		filename[strlen(filename)-1] = (char) (i+'0');
		t = read_base64_file(filename, &string[i]);
		decoded[i] = malloc(t);
		length[i] = decode_base64(string[i], decoded[i]);
		printf("read: %s\n", string[i]);
	}
	// choose one randomly
	int random = rand() % NR_STIRNGS_CHALLENGE17;
	int padding;
	printf("plaintext: %s\n", string[random]);
	*length = strlen(string[random]);
	char *padded_string = __pkcs7_padding(string[random], *length, 16, &padding);
	char *encrypted = malloc(strlen(padded_string));
	*length += padding;
	aes_cbc(padded_string, strlen(padded_string), encrypted, key, iv, 1);
	return encrypted;	
}

int cbc_padding_oracle(char *encrypted, int length)
{
	char *decrypted = malloc(length);
	char *unpadded= malloc(length);
	
	aes_cbc(encrypted, length, decrypted, key, iv, 0);

	int valid = valid_pkcs7_padding(decrypted, length, unpadded, 16);
	free(decrypted);
	free(unpadded);
	return valid;

}

/**
  * format is: 64 nonce concat with 64 bit counter
  * calle has to make sure that nonce is at least 8 bytes
  * its all little endian
  */
int aes_ctr(char *in, int length_in, char *out, char *string_key, char *nonce)
{
	long counter;
	unsigned char tmp[16];
	char keystream[16];
	long nr_blocks = length_in / BLOCKSIZE;
	int length_last_block = length_in % BLOCKSIZE;
	memcpy(tmp, nonce, 16);

	for(counter=0;counter<nr_blocks-1;counter++) {
		// right now it only works for 256 block :P 
		tmp[8] = counter % 256;
		// encrypt nonce and counter and produce keystream
		aes_ecb(tmp, 16, keystream, string_key, 16, 1);
		// xor against keystream to encrpy/decrypt
		xor_string(&in[counter*BLOCKSIZE], keystream, &out[counter*BLOCKSIZE], 16, 16); 
	}

	// do last block
	tmp[8] = counter % 256;
	aes_ecb(tmp, 16, keystream, string_key, 16, 1);
	xor_string(&in[counter*BLOCKSIZE], keystream, &out[counter*BLOCKSIZE],
			16, length_last_block);

}

void mt_19937_seed(unsigned int seed, struct mt_19937_state *mt_19937)
{
	int i;

	mt_19937->index = MT_19937_N;
	mt_19937->mt[0] = seed;
	memset(&mt_19937->mt[1], 0, 623);
	for(i=1;i<=MT_19937_N;i++) {
		mt_19937->mt[i] = (1812433253 * (mt_19937->mt[i-1]
			^ (mt_19937->mt[i-1] >> 30))) + i;
		// only the last 32 bit
		mt_19937->mt[i] &= 0xFFFFFFFF;
	}
}

void mt_19937_generate(struct mt_19937_state *mt_19937)
{
	unsigned int i, x;

	for(i=0;i<=MT_19937_N;i++) {
		// x is the first bit of mt[i] plus the last 31 bits of the
		// next number
		x = (((mt_19937->mt[i] & MT_19937_UPPER_MASK) + 
			(mt_19937->mt[(i+1) % 624] & MT_19937_LOWER_MASK)
		   	)& 0xFFFFFFFF);
		
		mt_19937->mt[i] = mt_19937->mt[(i+397) % MT_19937_N] ^ x >> 1;	

		if (x % 2)
			mt_19937->mt[i] ^= 0x9908b0df;
	}
	mt_19937->index = 0;
}


unsigned int mt_19937(struct mt_19937_state *mt_19937_st)
{
	unsigned int y = 0x0;

	if(mt_19937_st->index >= MT_19937_N)
		mt_19937_generate(mt_19937_st);

	y = mt_19937_st->mt[mt_19937_st->index];

	y = y ^ (y >> 11);
	y = y ^ ((y << 7) & 0x9D2C5680);
	y = y ^ ((y << 15) & 0xEFC60000);
	y = y ^ (y >> 18);

	mt_19937_st->index++;

	return (y & 0xFFFFFFFF);
}

unsigned int mt_19937_timestamp_orcale(struct mt_19937_state *mt_19937_st)
{
	sleep(random_number_between(40,1000));

	unsigned int seed = time(NULL);
	printf("timestamp orcale seed: %u\n", seed);
	mt_19937_seed(seed, mt_19937_st);

	sleep(random_number_between(40,1000));

	return  mt_19937(mt_19937_st);
}
/***
  * is there a more clever way to do this than brute force?
  *
  **/
void mt_19937_brute_force_timestamp()
{
	struct mt_19937_state mt_state;
	unsigned int start = time(NULL);
	int rnd = mt_19937_timestamp_orcale(&mt_state);
	unsigned int stop = time(NULL);
	unsigned int i;
	// try every seed between start and stop
	for(i=start;start<=stop;i++) {
		mt_19937_seed(i, &mt_state);
		if (rnd == mt_19937(&mt_state)) {
			printf("found seed: %u\n", i);
			break;
		}
	}
}

int unshift_right_xor(int number, int shifts)
{
	/**
	  * restore shift bits in every round
	 **/
	int rounds = 0;
	int restore = 0;

	while(rounds*shifts < 32) {
		// take #shift bit be on, begin on the left and go to the right
		unsigned int tmp = (0xFFFFFFFF << (32-shifts)) >> (rounds*shifts);
		unsigned int tmp2 = number & tmp;
		number ^= tmp2 >> shifts;
		restore |= tmp2;
		rounds++;
	}
	return restore;
}

/***
  * why the fuck is the reverse AND working?
  **/
int unshift_left_xor(int number, int shifts, unsigned int mask)
{
	int rounds = 0;
	int restore = 0;

	while(rounds*shifts < 32) {
		// take #shift bit be on, begin on the right and shift it to left every round
		unsigned int tmp = (0xFFFFFFFF >> (32-shifts)) << (rounds*shifts);
		unsigned int tmp2 = number & tmp;
		number ^= tmp2 << shifts & mask;
		restore |= tmp2;
		rounds++;
	}
	return restore;
}


int mt_19937_stream_cipher(char *in, int length_in, char *out, int seed)
{
	struct mt_19937_state mt_state;
	char keystream;
	int i, tmp;

	mt_19937_seed(seed, &mt_state);

	for(i=0;i<length_in;i++) {
		tmp = mt_19937(&mt_state);
		keystream = tmp & 0xFF;
		out[i] = in[i] ^ keystream;
	}
}

int mt_19937_stream_cipher_oracle(char *in, int length_in, char *out)
{
	int prefix_length = random_number_between(0,50);
	// only 16 bit seed, 0 would not be a good seed, since the keystream
	// would allways be 0
	int seed = random_number_between(1,65536);
	char *plaintext_full = malloc(length_in + prefix_length);

	printf("use seed: %i\n", seed);
	// generate random number of random bytes
	generate_random_bytes(plaintext_full, prefix_length);
	memcpy(&plaintext_full[prefix_length], in, length_in);

	mt_19937_stream_cipher(plaintext_full, (length_in+prefix_length),
			out, seed);

	return (length_in + prefix_length);
}


int crack_mt_19937_stream_cipher_16_bit_seed(char *ciphertext, int length_ciphertext,
		char *plaintext, char *match)
{
	int i;
	int seed;
	int match_len = strlen(match);
	for(seed=1;seed<65536;seed++) {
		mt_19937_stream_cipher(ciphertext, length_ciphertext, plaintext, seed);
		for(i=1;i<match_len;i++) {
			if(plaintext[length_ciphertext-i] != match[match_len-i]) {
				break;
			}
			else if (i == (match_len-1))
				goto out;
		}
	}
out:
	printf("found seed: %i\n", seed);
}

unsigned int mt_19937_password_token()
{
	int seed = time(NULL);
	struct mt_19937_state mt_state;
	printf("password token seed: %i\n", seed);	
	mt_19937_seed(seed, &mt_state);

	return mt_19937(&mt_state);
}

int mt_19937_password_token_time_based(unsigned int password_token, int time_window)
{
	int start_time = time(NULL);
	struct mt_19937_state mt_state;
	int seed;
	for(seed=start_time-(time_window*60);seed<start_time+(time_window*60);seed++) {
		mt_19937_seed(seed, &mt_state);
		if(password_token == mt_19937(&mt_state))
			return 1;
	}
	return 0;
}