-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbc.c
executable file
·205 lines (156 loc) · 5.4 KB
/
cbc.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
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
//****************************************************************************/
// MPC-project FS2016
// Purpose: CBC-AES128 encryption / decryption
// File: cbc.c
// Author: M. Thaler, ZHAW, 2/2016
//****************************************************************************/
#include <stdint.h>
#include <stdio.h>
#include "string.h"
#include "cbc.h"
#include "aes128.h"
#define KEYLEN 16
// encrypt, CBC mode ----------------------------------------------------------
void encryptCBC(uint8_t *in, uint8_t *out, int dlen, uint8_t *key, uint8_t *iv) {
uint8_t expandedKey[176];
// the expanded key must be passed to encryptAES128
expandKey(expandedKey, key);
// ...
uintptr_t i;
//uint8_t remainders = dlen % KEYLEN; /* Remaining bytes in the last non-full block */
for (i = 0; i < dlen; i += KEYLEN) {
BlockCopy(out, in);
BlockXor(out, iv);
encryptAES128(out, expandedKey);
iv = out;
in += KEYLEN;
out += KEYLEN;
}
/*if (remainders) {
BlockCopy(out, in);
memset(out + remainders, 0, KEYLEN - remainders); //add 0-padding
encryptAES128(out, expandedKey);
}*/
}
// decrypt, CBC mode ----------------------------------------------------------
void decryptCBC(uint8_t *in, uint8_t *out, int dlen, uint8_t *key, uint8_t *iv, int nTh) {
uint8_t expandedKey[176];
// the expanded key must be passed to decryptAES128
expandKey(expandedKey, key);
// ...
uintptr_t i;
//uint8_t remainders = dlen % KEYLEN; /* Remaining bytes in the last non-full block */
//#pragma omp parallel num_threads(nTh)
{
//#pragma omp for
for (i = 0; i < dlen; i += KEYLEN) {
uint8_t* tempIn = in + i;
uint8_t* tempOut = out + i;
uint8_t* tempIv;
if (i == 0) {
tempIv = iv;
} else {
tempIv = tempIn - KEYLEN;
}
BlockCopy(tempOut, tempIn);
decryptAES128(tempOut, expandedKey);
BlockXor(tempOut, tempIv);
}
}
/*if (remainders) {
printf("REMAINDERS :-(");
BlockCopy(out, in);
memset(out + remainders, 0, KEYLEN - remainders); // add 0-padding
decryptAES128(out, expandedKey);
}*/
}
// hack, CBC mode -------------------------------------------------------------
int64_t attackCBC(uint8_t *in, int dlen, uint8_t *key, uint8_t *iv, int64_t nKeys, int64_t keyOffset, int nTh) {
uint8_t expandedKey[176];
// the expanded key must be passed to decryptAES128
expandKey(expandedKey, key);
dlen = KEYLEN * 3;
//the valid text has at least 50% valid chars
int treshHoldForValidChars = (int) dlen / 2;
int go = 1;
uint8_t testKey[16] = {0};
int startIndexOfThread = 0;
for (int j = 8; j < 16; j++) {
testKey[j] = key[j];
}
int64_t* correctKey = (int64_t*) (&testKey);
*correctKey = -1;
int64_t* keyAdd;
uint8_t* testKeyCopy;
uint8_t* decrypted;
#pragma omp parallel num_threads(nTh) private(testKeyCopy, keyAdd, decrypted)
{
//copy the key and decrpyted variable for each thread
decrypted = malloc(dlen * sizeof (uint8_t));
testKeyCopy = malloc(16 * sizeof (uint8_t));
keyAdd = (int64_t*) memcpy(testKeyCopy, testKey, 16 * sizeof (uint8_t));
(*keyAdd) += keyOffset;
startIndexOfThread++;
(*keyAdd) += startIndexOfThread;
int st;
while ((*keyAdd) < nKeys && go) {
decryptCBC(in, decrypted, dlen, testKeyCopy, iv, 0);
// Counting the valid chars of decrypted text
st = 0;
for (int i = 0; i < dlen; i++) {
st += ((decrypted[i] > 'A') & (decrypted[i] < 'z'));
}
if (st > treshHoldForValidChars) {
//printf("\n Thread (%i) KeyAdd(%i) decrypted Text", omp_get_thread_num(), *keyAdd);
/*for (int k = 0; k < dlen; k++){
printf("%c", decrypted[k]);
}*/
//copy the key into a public variable
memcpy(correctKey, keyAdd, sizeof (int64_t));
go = 0;
}
(*keyAdd) += nTh;
}
//printf("\n Thread (%i) KeyAdd(%i)", omp_get_thread_num(), *keyAdd);
}
return *correctKey;
}
// ----------------------------------------------------------------------------
/*int countValidChars(uint8_t* decrypted, int dlen, int nTh){
int st =0;
{
for (int i = 0; i < dlen; i++) {
// A = 65, z = 122
st += ((decrypted[i] > 'A') & (decrypted[i] < 'z'));
}
}
return st;
}
double calculateEntropy(uint8_t* decrypted, int dlen, int nTh) {
int frequencies[256] = {0};
#pragma omp parallel num_threads(nTh)
{
#pragma omp for
for (int i = 0; i < dlen; i++) {
frequencies[decrypted[i]]++;
}
}
double entropy = 0;
for (int i = 0; i < 256; i++) {
double frequency = (frequencies[i] / (double) dlen);
if (frequency > 0.0) {
entropy -= frequency * (log2(frequency) / log2(2.0));
}
}
return entropy;
}*/
void BlockCopy(uint8_t* output, uint8_t* input) {
for (int i = 0; i < KEYLEN; ++i) {
output[i] = input[i];
}
}
void BlockXor(uint8_t* buf, uint8_t* iv) {
for (int i = 0; i < KEYLEN; ++i) {
buf[i] ^= iv[i];
}
}