-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
376 lines (307 loc) · 7.36 KB
/
common.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*! \file common.c
*
* \brief Routines for the KP-ABE tools.
*
* Copyright 2011 Yao Zheng.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <glib.h>
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <pbc.h>
#include "common.h"
/*!
* Initialize parameters for AES symmetric-key encryption
*
* @param k Sercet message from KP-ABE
* @param enc Set encrypt key when enc = 1; Set decrypt key when enc = 0;
* @param key Pointer to the AES_KEY
* @param iv Salt
* @return None
*/
void
init_aes( element_t k, int enc, AES_KEY* key, unsigned char* iv )
{
int key_len;
unsigned char* key_buf;
key_len = element_length_in_bytes(k) < 17 ? 17 : element_length_in_bytes(k);
key_buf = (unsigned char*) malloc(key_len);
element_to_bytes(key_buf, k);
if( enc )
AES_set_encrypt_key(key_buf + 1, 128, key);
else
AES_set_decrypt_key(key_buf + 1, 128, key);
free(key_buf);
memset(iv, 0, 16);
}
/*!
* AES 128bit CBC mode encryption
*
* @param pt GByteArrary of plaintext
* @param k Sercet message from KP-ABE
* @return GByteArray of ciphertext
*/
GByteArray*
aes_128_cbc_encrypt( GByteArray* pt, element_t k )
{
AES_KEY key;
unsigned char iv[16];
GByteArray* ct;
guint8 len[4];
guint8 zero;
init_aes(k, 1, &key, iv);
/* TODO make less crufty */
/* stuff in real length (big endian) before padding */
len[0] = (pt->len & 0xff000000)>>24;
len[1] = (pt->len & 0xff0000)>>16;
len[2] = (pt->len & 0xff00)>>8;
len[3] = (pt->len & 0xff)>>0;
g_byte_array_prepend(pt, len, 4);
/* pad out to multiple of 128 bit (16 byte) blocks */
zero = 0;
while( pt->len % 16 )
g_byte_array_append(pt, &zero, 1);
ct = g_byte_array_new();
g_byte_array_set_size(ct, pt->len);
AES_cbc_encrypt(pt->data, ct->data, pt->len, &key, iv, AES_ENCRYPT);
return ct;
}
/*!
* AES 128bit CBC mode decryption
*
* @param pt GByteArrary of ciphertext
* @param k Sercet message from KP-ABE
* @return GByteArray of plaintext
*/
GByteArray*
aes_128_cbc_decrypt( GByteArray* ct, element_t k )
{
AES_KEY key;
unsigned char iv[16];
GByteArray* pt;
unsigned int len;
init_aes(k, 0, &key, iv);
pt = g_byte_array_new();
g_byte_array_set_size(pt, ct->len);
AES_cbc_encrypt(ct->data, pt->data, ct->len, &key, iv, AES_DECRYPT);
/* TODO make less crufty */
/* get real length */
len = 0;
len = len
| ((pt->data[0])<<24) | ((pt->data[1])<<16)
| ((pt->data[2])<<8) | ((pt->data[3])<<0);
g_byte_array_remove_index(pt, 0);
g_byte_array_remove_index(pt, 0);
g_byte_array_remove_index(pt, 0);
g_byte_array_remove_index(pt, 0);
/* truncate any garbage from the padding */
g_byte_array_set_size(pt, len);
return pt;
}
/*!
* Open file with read mode or die
*
* @param file File name
* @return File handler
*/
FILE*
fopen_read_or_die( char* file )
{
FILE* f;
if( !(f = fopen(file, "r")) )
die("can't read file: %s\n", file);
return f;
}
/*!
* Open file with write mode or die
*
* @param file File name
* @return File handler
*/
FILE*
fopen_write_or_die( char* file )
{
FILE* f;
if( !(f = fopen(file, "w")) )
die("can't write file: %s\n", file);
return f;
}
/*!
* Open file and turn it into a GByteArray
*
* @param file File name
* @return GByteArray
*/
GByteArray*
suck_file( char* file )
{
FILE* f;
GByteArray* a;
struct stat s;
a = g_byte_array_new();
stat(file, &s);
g_byte_array_set_size(a, s.st_size);
f = fopen_read_or_die(file);
fread(a->data, 1, s.st_size, f);
fclose(f);
return a;
}
/*!
* Open file and turn it into a String
*
* @param file File name
* @return String
*/
char*
suck_file_str( char* file )
{
GByteArray* a;
char* s;
unsigned char zero;
a = suck_file(file);
zero = 0;
g_byte_array_append(a, &zero, 1);
s = (char*) a->data;
g_byte_array_free(a, 0);
return s;
}
/*!
* Get input from stdin and turn it into a String
*
* @return String
*/
char*
suck_stdin()
{
GString* s;
char* r;
int c;
s = g_string_new("");
while( (c = fgetc(stdin)) != EOF )
g_string_append_c(s, c);
r = s->str;
g_string_free(s, 0);
return r;
}
/*!
* Output GByteArray into a file, if free is one, free the GByteArray
*
* @param file File name
* @param b GByteArray
* @param free Free flag
* @return None
*/
void
spit_file( char* file, GByteArray* b, int free )
{
FILE* f;
f = fopen_write_or_die(file);
fwrite(b->data, 1, b->len, f);
fclose(f);
if( free )
g_byte_array_free(b, 1);
}
/*!
* Read serialized KP-ABE ciphertext from input file and put it into a buffer
* Read AES encrypted data from input file and put it into a buffer
*
* @param file File name
* @param cph_buf Pointer to Ciphertext buffer
* @param file_len Read file len
* @param aes_buf Pointer to AES buffer
* @return None
*/
void read_kpabe_file( char* file, GByteArray** cph_buf,
int* file_len, GByteArray** aes_buf )
{
FILE* f;
int i;
int len;
*cph_buf = g_byte_array_new();
*aes_buf = g_byte_array_new();
f = fopen_read_or_die(file);
/* read real file len as 32-bit big endian int */
*file_len = 0;
for( i = 3; i >= 0; i-- )
*file_len |= fgetc(f)<<(i*8);
/* read aes buf */
len = 0;
for( i = 3; i >= 0; i-- )
len |= fgetc(f)<<(i*8);
g_byte_array_set_size(*aes_buf, len);
fread((*aes_buf)->data, 1, len, f);
/* read cph buf */
len = 0;
for( i = 3; i >= 0; i-- )
len |= fgetc(f)<<(i*8);
g_byte_array_set_size(*cph_buf, len);
fread((*cph_buf)->data, 1, len, f);
fclose(f);
}
/*!
* write serialized KP-ABE ciphertext from buffer to file
* Read AES encrypted data from buffer to file
*
* @param file File name
* @param cph_buf Pointer to Ciphertext buffer
* @param file_len Write file len
* @param aes_buf Pointer to AES buffer
* @return None
*/
void
write_kpabe_file( char* file, GByteArray* cph_buf,
int file_len, GByteArray* aes_buf )
{
FILE* f;
int i;
f = fopen_write_or_die(file);
/* write real file len as 32-bit big endian int */
for( i = 3; i >= 0; i-- )
fputc((file_len & 0xff<<(i*8))>>(i*8), f);
/* write aes_buf */
for( i = 3; i >= 0; i-- )
fputc((aes_buf->len & 0xff<<(i*8))>>(i*8), f);
fwrite(aes_buf->data, 1, aes_buf->len, f);
/* write cph_buf */
for( i = 3; i >= 0; i-- )
fputc((cph_buf->len & 0xff<<(i*8))>>(i*8), f);
fwrite(cph_buf->data, 1, cph_buf->len, f);
fclose(f);
}
/*!
* Terminate program with error message
*
* @param fmt Error message
* @return None
*/
void
die(char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(1);
}