-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c~
executable file
·105 lines (82 loc) · 2.9 KB
/
main.c~
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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <omp.h>
#include "cbc.h"
#include "utils.h"
//******************************************************************************
int main(int argc, char *argv[]) {
int nTh = 1, len, alen;
double dt;
uint8_t *in, *out, *inout;
uint8_t key[16] = {
11, 0, 0, 0, 0, 0, 0, 0,
35, 123, 7, 3, 7, 1, 9, 34
};
uint8_t iv[16] = {0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01};
if (argc > 1)
nTh = atoi(argv[1]);
// encrypt / decrypt: function test -------------------------------------
alen = 1;
uint8_t *str = (uint8_t *) "Das sichtbaren letzten beinahe doch. Indem gemacht zurücksehnte weil zu, ihn sich auf sie einem, wie wieder zugetraut wenn sich, die Helga niemals und erfreutem denn, aber hatte unbedeutende sie Weise. Er das daran erkannte als, das worden nehmen wieder und, bevor Tag der benommen klopfte.";
len = getData(&in, str, &alen);
inout = (uint8_t *) malloc(len * sizeof (uint8_t));
out = (uint8_t *) malloc(len * sizeof (uint8_t));
encryptCBC(in, inout, len, key, iv);
decryptCBC(inout, out, len, key, iv, 1);
for (int i = 0; i < alen; i++) {
printf("%c = %c\n", in[i], out[i]);
assert(out[i] == in[i]);
}
for (int i = 0; i < alen; i++)
printf("%c", out[i]);
printf("\n");
free(in);
free(out);
free(inout);
// encrypt / decrypt: speed test ----------------------------------------
alen = 0;
len = getData(&in, "dada.dat", &alen);
inout = (uint8_t *) malloc(len * sizeof (uint8_t));
out = (uint8_t *) malloc(len * sizeof (uint8_t));
// "heat" CPU
for (int i = 0; i < 10; i++) {
encryptCBC(in, inout, len, key, iv);
decryptCBC(inout, out, len, key, iv, nTh);
}
// now measure time
encryptCBC(in, inout, len, key, iv);
dt = omp_get_wtime();
decryptCBC(inout, out, len, key, iv, nTh);
dt = omp_get_wtime() - dt;
printf("Elapsed: %2.2lf ms (%d)\n", 1000.0 * dt, nTh);
for (int i = 0; i < alen; i++)
assert(out[i] == in[i]);
printf("\n");
free(in);
free(out);
free(inout);
//try our cracker:
alen = 0;
len = getData(&in, "test.txt", &alen);
inout = (uint8_t *) malloc(len * sizeof (uint8_t));
out = (uint8_t *) malloc(len * sizeof (uint8_t));
encryptCBC(in, inout, len, key, iv);
dt = omp_get_wtime();
int64_t crackedKey = attackCBC(inout, len, key, iv, INT_MAX, 0, 0);
dt = omp_get_wtime() - dt;
printf("\nElapsed: %2.2lf ms (%d)\n", 1000.0 * dt, nTh);
printf("\nfound key: %" PRId64 "\n", crackedKey);
for (int i = 0; i < alen; i++)
printf("%c", out[i]);
printf("\n");
free(in);
free(out);
free(inout);
}