forked from altu341com/kpabe
/
keygen.c
166 lines (136 loc) · 3.87 KB
/
keygen.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
/*! \file keygen.c
*
* \brief Routines for the KP-ABE Key Generation.
*
* 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 <stdlib.h>
#include <string.h>
#include <strings.h>
#include <glib.h>
#include <pbc.h>
#include <pbc_random.h>
#include "celia.h"
#include "common.h"
#include "policy_lang.h"
char* usage =
"Usage: kpabe-keygen [OPTION ...] PUB_KEY MASTER_KEY [POLICY]\n"
"\n"
"Generate a key under the decryption policy POLICY using public key\n"
"PUB_KEY and master secret key MASTER_KEY. Output will be written to the file\n"
"\"priv_key\" unless the -o option is specified.\n"
"\n"
"If POLICY is not specified, the policy will be read from stdin.\n"
"\n"
"Mandatory arguments to long options are mandatory for short options too.\n\n"
" -h, --help print this message\n\n"
" -v, --version print version information\n\n"
" -o, --output FILE write resulting key to FILE\n\n"
" -d, --deterministic use deterministic \"random\" numbers\n"
" (only for debugging)\n\n"
"";
/*
TODO ensure we don't give out the same attribute more than once (esp
as different numerical values)
*/
char* pub_file = 0;
char* msk_file = 0;
char* policy = 0;
char* out_file = "priv_key";
void
parse_args( int argc, char** argv )
{
int i;
for( i = 1; i < argc; i++ )
if( !strcmp(argv[i], "-h") || !strcmp(argv[i], "--help") )
{
printf("%s", usage);
exit(0);
}
else if( !strcmp(argv[i], "-v") || !strcmp(argv[i], "--version") )
{
printf(KPABE_VERSION, "-keygen");
exit(0);
}
else if( !strcmp(argv[i], "-o") || !strcmp(argv[i], "--output") )
{
if( ++i >= argc )
die(usage);
else
out_file = argv[i];
}
else if( !strcmp(argv[i], "-d") || !strcmp(argv[i], "--deterministic") )
{
pbc_random_set_deterministic(0);
}
else if( !pub_file )
{
pub_file = argv[i];
}
else if( !msk_file )
{
msk_file = argv[i];
}
else if( !policy )
{
policy = parse_policy_lang(argv[i]);
}
else
die(usage);
if( !pub_file || !msk_file )
die(usage);
if( !policy )
policy = parse_policy_lang(suck_stdin());
}
double
to_milliseconds(struct timespec* time)
{
return (double)time->tv_sec*1000 + ((double)time->tv_nsec/1000000);
}
void
diff(struct timespec* res,struct timespec *start,struct timespec *end)
{
if (res == NULL) return;
res -> tv_sec = end -> tv_sec - start -> tv_sec;
res -> tv_nsec = end -> tv_nsec - start -> tv_nsec;
if (res -> tv_nsec < 0)
{
res -> tv_nsec = res -> tv_nsec + 1000000000;
res -> tv_sec --;
}
}
int
main( int argc, char** argv )
{
kpabe_pub_t* pub;
kpabe_msk_t* msk;
kpabe_prv_t* prv;
parse_args(argc, argv);
pub = kpabe_pub_unserialize(suck_file(pub_file), 1);
msk = kpabe_msk_unserialize(pub, suck_file(msk_file), 1);
struct timespec start,d,end;
clock_gettime(CLOCK_REALTIME,&start);
if( !(prv = kpabe_keygen(pub, msk, policy)) ) {
die("%s", kpabe_error());
}
clock_gettime(CLOCK_REALTIME,&end);
diff(&d,&start,&end);
printf("%lf", to_milliseconds(&d));
free(policy);
spit_file(out_file, kpabe_prv_serialize(prv), 1);
return 0;
}