forked from zeutro/openabe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.cpp
143 lines (126 loc) · 4.98 KB
/
setup.cpp
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
///
/// Copyright (c) 2018 Zeutro, LLC. All rights reserved.
///
/// This file is part of Zeutro's OpenABE.
///
/// OpenABE is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as published by
/// the Free Software Foundation, either version 3 of the License, or
/// (at your option) any later version.
///
/// OpenABE 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 Affero General Public License for more details.
///
/// You should have received a copy of the GNU Affero General Public
/// License along with OpenABE. If not, see <http://www.gnu.org/licenses/>.
///
/// You can be released from the requirements of the GNU Affero General
/// Public License and obtain additional features by purchasing a
/// commercial license. Buying such a license is mandatory if you
/// engage in commercial activities involving OpenABE that do not
/// comply with the open source requirements of the GNU Affero General
/// Public License. For more information on commerical licenses,
/// visit <http://www.zeutro.com>.
///
/// \file setup.cpp
///
/// \brief Generate the ABE global system parameters.
///
/// \author J. Ayo Akinyele
///
#include "common.h"
using namespace std;
using namespace oabe;
#define USAGE \
"usage: [ -s scheme ] [ -p prefix ] -v\n\n" \
"\t-v : turn on verbose mode\n" \
"\t-s : scheme types are 'CP' or 'KP'\n" \
"\t-p : prefix string for generated authority public and secret parameter files (optional)\n\n"
void runSetup(OpenABE_SCHEME scheme_type, string& prefix, string& suffix, bool verbose)
{
try {
OpenABEByteString mpkBlob, mskBlob;
string mpkFile = MPK_ID + suffix, mskFile = MSK_ID + suffix;
if(prefix != "") {
mpkFile = prefix + mpkFile;
mskFile = prefix + mskFile;
}
// abeSetup(scheme_type, prefix, mpkBlob, mskBlob);
std::unique_ptr<OpenABEContextSchemeCCA> schemeContext = nullptr;
// default parameters
string mpkID = MPK_ID, mskID = MSK_ID;
if (prefix != "") {
mpkID = prefix + mpkID;
mskID = prefix + mskID;
}
// Initialize a OpenABEContext structure
schemeContext = OpenABE_createContextABESchemeCCA(scheme_type);
if (schemeContext == nullptr) {
cerr << "unable to create a new context" << endl;
return;
}
// Generate a set of parameters for an ABE authority
if (schemeContext->generateParams(DEFAULT_PARAMETER_STRING, mpkID, mskID) != OpenABE_NOERROR) {
cerr << "unable to generate parameters" << endl;
return;
}
// don't password protect the master public parameters (not necessary here)
if (schemeContext->exportKey(mpkID, mpkBlob) != OpenABE_NOERROR) {
cerr << "unable to export public parameters" << endl;
return;
}
if (schemeContext->exportKey(mskID, mskBlob) != OpenABE_NOERROR) {
cerr << "unable to export master secret parameters" << endl;
return;
}
// cout << "MPK: " << mpkBlob.toHex() << endl;
// cout << "MSK: " << mskBlob.toHex() << endl;
cout << "writing " << mpkBlob.size() << " bytes to " << mpkFile << endl;
WriteToFile(mpkFile.c_str(), MPK_BEGIN_HEADER + Base64Encode(mpkBlob.getInternalPtr(), mpkBlob.size()) + MPK_END_HEADER);
cout << "writing " << mskBlob.size() << " bytes to " << mskFile << endl;
WriteToFile(mskFile.c_str(), MSK_BEGIN_HEADER + Base64Encode(mskBlob.getInternalPtr(), mskBlob.size()) + MSK_END_HEADER);
} catch (OpenABE_ERROR& error) {
cout << "caught exception: " << OpenABE_errorToString(error) << endl;
return;
}
return;
}
int main(int argc, char **argv)
{
// if interactive flag set, then enter password via stdin (instead of command line)
bool verbose_flag = false;
string scheme_type = "", prefix = "", suffix = "";
int c;
if(argc <= 1) {
cout << OpenABE_CLI_STRING << "system setup utility, v" << (OpenABE_LIBRARY_VERSION / 100.) << endl;
fprintf(stderr, USAGE);
return -1;
}
while((c = getopt(argc, argv, "vs:p:")) != -1) {
switch(c) {
case 's': scheme_type = string(optarg); break;
case 'p': prefix = string(optarg); break;
case 'v': verbose_flag = true; break;
case '?': fprintf(stderr, USAGE);
default: cout<<endl; exit(-1);
}
}
// check prefix ending
addNameSeparator(prefix);
// validate scheme type
OpenABE_SCHEME scheme = checkForScheme(scheme_type, suffix);
if (scheme == OpenABE_SCHEME_NONE) {
cerr << "selected an invalid scheme type. Try again with -s option." << endl;
return -1;
} else if (scheme == OpenABE_SCHEME_PK_OPDH) {
cerr << "PK encryption does not require setup. Can simply proceed with keygen." << endl;
return -1;
}
InitializeOpenABE();
// KP or CP
runSetup(scheme, prefix, suffix, verbose_flag);
ShutdownOpenABE();
return 0;
}