Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sgxwallet
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
董子豪
sgxwallet
Commits
a2fdbcb7
Unverified
Commit
a2fdbcb7
authored
Mar 24, 2020
by
kladko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SKALE-2341 Added tags for older commits
parent
4e26af8b
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
392 additions
and
439 deletions
+392
-439
BLSCrypto.cpp
BLSCrypto.cpp
+139
-142
DKGCrypto.cpp
DKGCrypto.cpp
+241
-273
ECDSACrypto.cpp
ECDSACrypto.cpp
+3
-9
LevelDB.cpp
LevelDB.cpp
+8
-11
SEKManager.cpp
SEKManager.cpp
+0
-2
ServerInit.cpp
ServerInit.cpp
+1
-2
No files found.
BLSCrypto.cpp
View file @
a2fdbcb7
...
@@ -55,76 +55,75 @@
...
@@ -55,76 +55,75 @@
#include "common.h"
#include "common.h"
std
::
string
*
FqToString
(
libff
::
alt_bn128_Fq
*
_fq
)
{
std
::
string
*
FqToString
(
libff
::
alt_bn128_Fq
*
_fq
)
{
mpz_t
t
;
mpz_t
t
;
mpz_init
(
t
);
mpz_init
(
t
);
_fq
->
as_bigint
().
to_mpz
(
t
);
_fq
->
as_bigint
().
to_mpz
(
t
);
char
arr
[
mpz_sizeinbase
(
t
,
10
)
+
2
];
char
arr
[
mpz_sizeinbase
(
t
,
10
)
+
2
];
char
*
tmp
=
mpz_get_str
(
arr
,
10
,
t
);
char
*
tmp
=
mpz_get_str
(
arr
,
10
,
t
);
mpz_clear
(
t
);
mpz_clear
(
t
);
return
new
std
::
string
(
tmp
);
return
new
std
::
string
(
tmp
);
}
}
int
char2int
(
char
_input
)
{
int
char2int
(
char
_input
)
{
if
(
_input
>=
'0'
&&
_input
<=
'9'
)
if
(
_input
>=
'0'
&&
_input
<=
'9'
)
return
_input
-
'0'
;
return
_input
-
'0'
;
if
(
_input
>=
'A'
&&
_input
<=
'F'
)
if
(
_input
>=
'A'
&&
_input
<=
'F'
)
return
_input
-
'A'
+
10
;
return
_input
-
'A'
+
10
;
if
(
_input
>=
'a'
&&
_input
<=
'f'
)
if
(
_input
>=
'a'
&&
_input
<=
'f'
)
return
_input
-
'a'
+
10
;
return
_input
-
'a'
+
10
;
return
-
1
;
return
-
1
;
}
}
void
carray2Hex
(
const
unsigned
char
*
d
,
int
_len
,
char
*
_hexArray
)
{
void
carray2Hex
(
const
unsigned
char
*
d
,
int
_len
,
char
*
_hexArray
)
{
char
hexval
[
16
]
=
{
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
};
char
hexval
[
16
]
=
{
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
for
(
int
j
=
0
;
j
<
_len
;
j
++
)
{
'8'
,
'9'
,
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
};
_hexArray
[
j
*
2
]
=
hexval
[((
d
[
j
]
>>
4
)
&
0xF
)];
_hexArray
[
j
*
2
+
1
]
=
hexval
[(
d
[
j
])
&
0x0F
];
for
(
int
j
=
0
;
j
<
_len
;
j
++
)
{
}
_hexArray
[
j
*
2
]
=
hexval
[((
d
[
j
]
>>
4
)
&
0xF
)];
_hexArray
[
j
*
2
+
1
]
=
hexval
[(
d
[
j
])
&
0x0F
];
}
_hexArray
[
_len
*
2
]
=
0
;
_hexArray
[
_len
*
2
]
=
0
;
}
}
bool
hex2carray
(
const
char
*
_hex
,
uint64_t
*
_bin_len
,
bool
hex2carray
(
const
char
*
_hex
,
uint64_t
*
_bin_len
,
uint8_t
*
_bin
)
{
uint8_t
*
_bin
)
{
int
len
=
strnlen
(
_hex
,
2
*
BUF_LEN
);
int
len
=
strnlen
(
_hex
,
2
*
BUF_LEN
);
if
(
len
==
0
&&
len
%
2
==
1
)
if
(
len
==
0
&&
len
%
2
==
1
)
return
false
;
return
false
;
*
_bin_len
=
len
/
2
;
*
_bin_len
=
len
/
2
;
for
(
int
i
=
0
;
i
<
len
/
2
;
i
++
)
{
for
(
int
i
=
0
;
i
<
len
/
2
;
i
++
)
{
int
high
=
char2int
((
char
)
_hex
[
i
*
2
]);
int
high
=
char2int
((
char
)
_hex
[
i
*
2
]);
int
low
=
char2int
((
char
)
_hex
[
i
*
2
+
1
]);
int
low
=
char2int
((
char
)
_hex
[
i
*
2
+
1
]);
if
(
high
<
0
||
low
<
0
)
{
if
(
high
<
0
||
low
<
0
)
{
return
false
;
return
false
;
}
}
_bin
[
i
]
=
(
unsigned
char
)
(
high
*
16
+
low
);
_bin
[
i
]
=
(
unsigned
char
)
(
high
*
16
+
low
);
}
}
return
true
;
return
true
;
}
}
bool
hex2carray2
(
const
char
*
_hex
,
uint64_t
*
_bin_len
,
bool
hex2carray2
(
const
char
*
_hex
,
uint64_t
*
_bin_len
,
uint8_t
*
_bin
,
const
int
_max_length
)
{
uint8_t
*
_bin
,
const
int
_max_length
)
{
int
len
=
strnlen
(
_hex
,
_max_length
);
//2 * BUF_LEN);
int
len
=
strnlen
(
_hex
,
_max_length
);
//2 * BUF_LEN);
...
@@ -135,8 +134,8 @@ bool hex2carray2(const char * _hex, uint64_t *_bin_len,
...
@@ -135,8 +134,8 @@ bool hex2carray2(const char * _hex, uint64_t *_bin_len,
*
_bin_len
=
len
/
2
;
*
_bin_len
=
len
/
2
;
for
(
int
i
=
0
;
i
<
len
/
2
;
i
++
)
{
for
(
int
i
=
0
;
i
<
len
/
2
;
i
++
)
{
int
high
=
char2int
((
char
)
_hex
[
i
*
2
]);
int
high
=
char2int
((
char
)
_hex
[
i
*
2
]);
int
low
=
char2int
((
char
)
_hex
[
i
*
2
+
1
]);
int
low
=
char2int
((
char
)
_hex
[
i
*
2
+
1
]);
if
(
high
<
0
||
low
<
0
)
{
if
(
high
<
0
||
low
<
0
)
{
return
false
;
return
false
;
...
@@ -149,62 +148,62 @@ bool hex2carray2(const char * _hex, uint64_t *_bin_len,
...
@@ -149,62 +148,62 @@ bool hex2carray2(const char * _hex, uint64_t *_bin_len,
}
}
bool
sign
(
const
char
*
_encryptedKeyHex
,
const
char
*
_hashHex
,
size_t
_t
,
size_t
_n
,
size_t
_signerIndex
,
bool
sign
(
const
char
*
_encryptedKeyHex
,
const
char
*
_hashHex
,
size_t
_t
,
size_t
_n
,
size_t
_signerIndex
,
char
*
_sig
)
{
char
*
_sig
)
{
//cerr << "ENTER SIGN" << endl;
//cerr << "ENTER SIGN" << endl;
auto
keyStr
=
make_shared
<
string
>
(
_encryptedKeyHex
);
auto
keyStr
=
make_shared
<
string
>
(
_encryptedKeyHex
);
auto
hash
=
make_shared
<
array
<
uint8_t
,
32
>>
();
auto
hash
=
make_shared
<
array
<
uint8_t
,
32
>>
();
uint64_t
binLen
;
uint64_t
binLen
;
if
(
!
hex2carray
(
_hashHex
,
&
binLen
,
hash
->
data
()))
{
if
(
!
hex2carray
(
_hashHex
,
&
binLen
,
hash
->
data
()))
{
throw
RPCException
(
INVALID_HEX
,
"Invalid hash"
);
throw
RPCException
(
INVALID_HEX
,
"Invalid hash"
);
}
}
// assert(binLen == hash->size());
// assert(binLen == hash->size());
auto
keyShare
=
make_shared
<
BLSPrivateKeyShareSGX
>
(
keyStr
,
_t
,
_n
);
auto
keyShare
=
make_shared
<
BLSPrivateKeyShareSGX
>
(
keyStr
,
_t
,
_n
);
//cerr << "keyShare created" << endl;
//cerr << "keyShare created" << endl;
// {
// {
auto
sigShare
=
keyShare
->
signWithHelperSGX
(
hash
,
_signerIndex
);
auto
sigShare
=
keyShare
->
signWithHelperSGX
(
hash
,
_signerIndex
);
// }
// }
auto
sigShareStr
=
sigShare
->
toString
();
auto
sigShareStr
=
sigShare
->
toString
();
strncpy
(
_sig
,
sigShareStr
->
c_str
(),
BUF_LEN
);
strncpy
(
_sig
,
sigShareStr
->
c_str
(),
BUF_LEN
);
//string sigShareStr = keyShare->signWithHelperSGXstr(hash, _signerIndex);
//string sigShareStr = keyShare->signWithHelperSGXstr(hash, _signerIndex);
//strncpy(_sig, sigShareStr.c_str(), BUF_LEN);
//strncpy(_sig, sigShareStr.c_str(), BUF_LEN);
// string test_sig = "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855:9810286616503120081238481858289626967170509983220853777870754480048381194141:5";
// string test_sig = "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855:9810286616503120081238481858289626967170509983220853777870754480048381194141:5";
// auto sig_ptr = make_shared<string>(test_sig);
// auto sig_ptr = make_shared<string>(test_sig);
// strncpy(_sig, sig_ptr->c_str(), BUF_LEN);
// strncpy(_sig, sig_ptr->c_str(), BUF_LEN);
//cerr<< "sig " << _sig <<endl;
//cerr<< "sig " << _sig <<endl;
return
true
;
return
true
;
}
}
bool
sign_aes
(
const
char
*
_encryptedKeyHex
,
const
char
*
_hashHex
,
size_t
_t
,
size_t
_n
,
size_t
_signerIndex
,
bool
sign_aes
(
const
char
*
_encryptedKeyHex
,
const
char
*
_hashHex
,
size_t
_t
,
size_t
_n
,
size_t
_signerIndex
,
char
*
_sig
)
{
char
*
_sig
)
{
//cerr << "ENTER SIGN" << endl;
//cerr << "ENTER SIGN" << endl;
auto
keyStr
=
make_shared
<
string
>
(
_encryptedKeyHex
);
auto
keyStr
=
make_shared
<
string
>
(
_encryptedKeyHex
);
auto
hash
=
make_shared
<
array
<
uint8_t
,
32
>>
();
auto
hash
=
make_shared
<
array
<
uint8_t
,
32
>>
();
uint64_t
binLen
;
uint64_t
binLen
;
if
(
!
hex2carray
(
_hashHex
,
&
binLen
,
hash
->
data
()))
{
if
(
!
hex2carray
(
_hashHex
,
&
binLen
,
hash
->
data
()))
{
throw
RPCException
(
INVALID_HEX
,
"Invalid hash"
);
throw
RPCException
(
INVALID_HEX
,
"Invalid hash"
);
}
}
// assert(binLen == hash->size());
// assert(binLen == hash->size());
...
@@ -218,103 +217,102 @@ bool sign_aes(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, siz
...
@@ -218,103 +217,102 @@ bool sign_aes(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, siz
// auto sigShareStr = sigShare->toString();
// auto sigShareStr = sigShare->toString();
//
//
// strncpy(_sig, sigShareStr->c_str(), BUF_LEN);
// strncpy(_sig, sigShareStr->c_str(), BUF_LEN);
shared_ptr
<
signatures
::
Bls
>
obj
;
shared_ptr
<
signatures
::
Bls
>
obj
;
obj
=
make_shared
<
signatures
::
Bls
>
(
signatures
::
Bls
(
_t
,
_n
));
obj
=
make_shared
<
signatures
::
Bls
>
(
signatures
::
Bls
(
_t
,
_n
));
std
::
pair
<
libff
::
alt_bn128_G1
,
std
::
string
>
hash_with_hint
=
std
::
pair
<
libff
::
alt_bn128_G1
,
std
::
string
>
hash_with_hint
=
obj
->
HashtoG1withHint
(
hash
);
obj
->
HashtoG1withHint
(
hash
);
int
errStatus
=
0
;
int
errStatus
=
0
;
string
*
xStr
=
FqToString
(
&
(
hash_with_hint
.
first
.
X
));
string
*
xStr
=
FqToString
(
&
(
hash_with_hint
.
first
.
X
));
if
(
xStr
==
nullptr
)
{
if
(
xStr
==
nullptr
)
{
std
::
cerr
<<
"Null xStr"
<<
std
::
endl
;
std
::
cerr
<<
"Null xStr"
<<
std
::
endl
;
BOOST_THROW_EXCEPTION
(
runtime_error
(
"Null xStr"
));
BOOST_THROW_EXCEPTION
(
runtime_error
(
"Null xStr"
));
}
}
string
*
yStr
=
FqToString
(
&
(
hash_with_hint
.
first
.
Y
));
string
*
yStr
=
FqToString
(
&
(
hash_with_hint
.
first
.
Y
));
if
(
yStr
==
nullptr
)
{
if
(
yStr
==
nullptr
)
{
std
::
cerr
<<
"Null yStr"
<<
std
::
endl
;
std
::
cerr
<<
"Null yStr"
<<
std
::
endl
;
BOOST_THROW_EXCEPTION
(
runtime_error
(
"Null yStr"
));
BOOST_THROW_EXCEPTION
(
runtime_error
(
"Null yStr"
));
}
}
char
errMsg
[
BUF_LEN
];
char
errMsg
[
BUF_LEN
];
memset
(
errMsg
,
0
,
BUF_LEN
);
memset
(
errMsg
,
0
,
BUF_LEN
);
char
xStrArg
[
BUF_LEN
];
char
xStrArg
[
BUF_LEN
];
char
yStrArg
[
BUF_LEN
];
char
yStrArg
[
BUF_LEN
];
char
signature
[
BUF_LEN
];
char
signature
[
BUF_LEN
];
memset
(
xStrArg
,
0
,
BUF_LEN
);
memset
(
xStrArg
,
0
,
BUF_LEN
);
memset
(
yStrArg
,
0
,
BUF_LEN
);
memset
(
yStrArg
,
0
,
BUF_LEN
);
strncpy
(
xStrArg
,
xStr
->
c_str
(),
BUF_LEN
);
strncpy
(
xStrArg
,
xStr
->
c_str
(),
BUF_LEN
);
strncpy
(
yStrArg
,
yStr
->
c_str
(),
BUF_LEN
);
strncpy
(
yStrArg
,
yStr
->
c_str
(),
BUF_LEN
);
size_t
sz
=
0
;
size_t
sz
=
0
;
uint8_t
encryptedKey
[
BUF_LEN
];
uint8_t
encryptedKey
[
BUF_LEN
];
bool
result
=
hex2carray
(
_encryptedKeyHex
,
&
sz
,
encryptedKey
);
bool
result
=
hex2carray
(
_encryptedKeyHex
,
&
sz
,
encryptedKey
);
if
(
!
result
)
{
if
(
!
result
)
{
cerr
<<
"Invalid hex encrypted key"
<<
endl
;
cerr
<<
"Invalid hex encrypted key"
<<
endl
;
BOOST_THROW_EXCEPTION
(
std
::
invalid_argument
(
"Invalid hex encrypted key"
));
BOOST_THROW_EXCEPTION
(
std
::
invalid_argument
(
"Invalid hex encrypted key"
));
}
}
sgx_status_t
status
=
sgx_status_t
status
=
bls_sign_message_aes
(
eid
,
&
errStatus
,
errMsg
,
encryptedKey
,
bls_sign_message_aes
(
eid
,
&
errStatus
,
errMsg
,
encryptedKey
,
sz
,
xStrArg
,
yStrArg
,
signature
);
sz
,
xStrArg
,
yStrArg
,
signature
);
if
(
status
!=
SGX_SUCCESS
)
{
if
(
status
!=
SGX_SUCCESS
)
{
cerr
<<
"SGX enclave call to bls_sign_message failed:"
<<
status
<<
std
::
endl
;
cerr
<<
"SGX enclave call to bls_sign_message failed:"
<<
status
<<
std
::
endl
;
BOOST_THROW_EXCEPTION
(
runtime_error
(
"SGX enclave call to bls_sign_message failed"
));
BOOST_THROW_EXCEPTION
(
runtime_error
(
"SGX enclave call to bls_sign_message failed"
));
}
}
std
::
string
hint
=
BLSutils
::
ConvertToString
(
hash_with_hint
.
first
.
Y
)
+
":"
+
std
::
string
hint
=
BLSutils
::
ConvertToString
(
hash_with_hint
.
first
.
Y
)
+
":"
+
hash_with_hint
.
second
;
hash_with_hint
.
second
;
std
::
string
sig
=
signature
;
std
::
string
sig
=
signature
;
sig
.
append
(
":"
);
sig
.
append
(
":"
);
sig
.
append
(
hint
);
sig
.
append
(
hint
);
strncpy
(
_sig
,
sig
.
c_str
(),
BUF_LEN
);
strncpy
(
_sig
,
sig
.
c_str
(),
BUF_LEN
);
printf
(
"_sig is: %s
\n
"
,
sig
.
c_str
());
printf
(
"_sig is: %s
\n
"
,
sig
.
c_str
());
//string sigShareStr = keyShare->signWithHelperSGXstr(hash, _signerIndex);
//string sigShareStr = keyShare->signWithHelperSGXstr(hash, _signerIndex);
//strncpy(_sig, sigShareStr.c_str(), BUF_LEN);
//strncpy(_sig, sigShareStr.c_str(), BUF_LEN);
// string test_sig = "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855:9810286616503120081238481858289626967170509983220853777870754480048381194141:5";
// string test_sig = "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855:9810286616503120081238481858289626967170509983220853777870754480048381194141:5";
// auto sig_ptr = make_shared<string>(test_sig);
// auto sig_ptr = make_shared<string>(test_sig);
// strncpy(_sig, sig_ptr->c_str(), BUF_LEN);
// strncpy(_sig, sig_ptr->c_str(), BUF_LEN);
//cerr<< "sig " << _sig <<endl;
//cerr<< "sig " << _sig <<endl;
return
true
;
return
true
;
}
}
bool
bls_sign
(
const
char
*
_encryptedKeyHex
,
const
char
*
_hashHex
,
size_t
_t
,
size_t
_n
,
size_t
_signerIndex
,
bool
bls_sign
(
const
char
*
_encryptedKeyHex
,
const
char
*
_hashHex
,
size_t
_t
,
size_t
_n
,
size_t
_signerIndex
,
char
*
_sig
)
{
char
*
_sig
)
{
if
(
!
encryptKeys
){
if
(
!
encryptKeys
)
{
return
sign
(
_encryptedKeyHex
,
_hashHex
,
_t
,
_n
,
_signerIndex
,
_sig
);
return
sign
(
_encryptedKeyHex
,
_hashHex
,
_t
,
_n
,
_signerIndex
,
_sig
);
}
}
else
{
else
{
return
sign_aes
(
_encryptedKeyHex
,
_hashHex
,
_t
,
_n
,
_signerIndex
,
_sig
);
return
sign_aes
(
_encryptedKeyHex
,
_hashHex
,
_t
,
_n
,
_signerIndex
,
_sig
);
}
}
}
}
char
*
encryptBLSKeyShare2Hex
(
int
*
errStatus
,
char
*
err_string
,
const
char
*
_key
)
{
char
*
encryptBLSKeyShare2Hex
(
int
*
errStatus
,
char
*
err_string
,
const
char
*
_key
)
{
auto
keyArray
=
make_shared
<
vector
<
char
>>
(
BUF_LEN
,
0
);
auto
keyArray
=
make_shared
<
vector
<
char
>>
(
BUF_LEN
,
0
);
auto
encryptedKey
=
make_shared
<
vector
<
uint8_t
>>
(
BUF_LEN
,
0
);
auto
encryptedKey
=
make_shared
<
vector
<
uint8_t
>>
(
BUF_LEN
,
0
);
auto
errMsg
=
make_shared
<
vector
<
char
>>
(
BUF_LEN
,
0
);
auto
errMsg
=
make_shared
<
vector
<
char
>>
(
BUF_LEN
,
0
);
...
@@ -326,10 +324,9 @@ char* encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key)
...
@@ -326,10 +324,9 @@ char* encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key)
//status = encrypt_key(eid, errStatus, errMsg, keyArray, encryptedKey, &encryptedLen);
//status = encrypt_key(eid, errStatus, errMsg, keyArray, encryptedKey, &encryptedLen);
status
=
encrypt_key_aes
(
eid
,
errStatus
,
errMsg
->
data
(),
keyArray
->
data
(),
encryptedKey
->
data
(),
&
encryptedLen
);
status
=
encrypt_key_aes
(
eid
,
errStatus
,
errMsg
->
data
(),
keyArray
->
data
(),
encryptedKey
->
data
(),
&
encryptedLen
);
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"errStatus is {}"
,
*
errStatus
);
spdlog
::
debug
(
"errStatus is {}"
,
*
errStatus
);
spdlog
::
debug
(
" errMsg is "
,
errMsg
->
data
());
spdlog
::
debug
(
" errMsg is "
,
errMsg
->
data
()
);
}
if
(
status
!=
SGX_SUCCESS
)
{
if
(
status
!=
SGX_SUCCESS
)
{
...
@@ -365,7 +362,7 @@ char *decryptBLSKeyShareFromHex(int *errStatus, char *errMsg, const char *_encry
...
@@ -365,7 +362,7 @@ char *decryptBLSKeyShareFromHex(int *errStatus, char *errMsg, const char *_encry
char
*
plaintextKey
=
(
char
*
)
calloc
(
BUF_LEN
,
1
);
char
*
plaintextKey
=
(
char
*
)
calloc
(
BUF_LEN
,
1
);
//status = decrypt_key(eid, errStatus, errMsg, decoded, decodedLen, plaintextKey);
//status = decrypt_key(eid, errStatus, errMsg, decoded, decodedLen, plaintextKey);
status
=
decrypt_key_aes
(
eid
,
errStatus
,
errMsg
,
decoded
,
decodedLen
,
plaintextKey
);
status
=
decrypt_key_aes
(
eid
,
errStatus
,
errMsg
,
decoded
,
decodedLen
,
plaintextKey
);
if
(
status
!=
SGX_SUCCESS
)
{
if
(
status
!=
SGX_SUCCESS
)
{
return
nullptr
;
return
nullptr
;
...
...
DKGCrypto.cpp
View file @
a2fdbcb7
...
@@ -36,46 +36,44 @@
...
@@ -36,46 +36,44 @@
#include "spdlog/spdlog.h"
#include "spdlog/spdlog.h"
#include "common.h"
#include "common.h"
vector
<
string
>
SplitString
(
const
char
*
koefs
,
const
char
symbol
){
vector
<
string
>
SplitString
(
const
char
*
koefs
,
const
char
symbol
)
{
string
str
(
koefs
);
string
str
(
koefs
);
string
delim
;
string
delim
;
delim
.
push_back
(
symbol
);
delim
.
push_back
(
symbol
);
vector
<
string
>
G2_strings
;
vector
<
string
>
G2_strings
;
size_t
prev
=
0
,
pos
=
0
;
size_t
prev
=
0
,
pos
=
0
;
do
do
{
{
pos
=
str
.
find
(
delim
,
prev
);
pos
=
str
.
find
(
delim
,
prev
);
if
(
pos
==
string
::
npos
)
pos
=
str
.
length
();
if
(
pos
==
string
::
npos
)
pos
=
str
.
length
();
string
token
=
str
.
substr
(
prev
,
pos
-
prev
);
string
token
=
str
.
substr
(
prev
,
pos
-
prev
);
if
(
!
token
.
empty
())
{
if
(
!
token
.
empty
())
{
string
koef
(
token
.
c_str
());
string
koef
(
token
.
c_str
());
G2_strings
.
push_back
(
koef
);
G2_strings
.
push_back
(
koef
);
}
}
prev
=
pos
+
delim
.
length
();
prev
=
pos
+
delim
.
length
();
}
while
(
pos
<
str
.
length
()
&&
prev
<
str
.
length
());
}
while
(
pos
<
str
.
length
()
&&
prev
<
str
.
length
());
return
G2_strings
;
return
G2_strings
;
}
}
template
<
class
T
>
template
<
class
T
>
string
ConvertToString
(
T
field_elem
,
int
base
=
10
)
{
string
ConvertToString
(
T
field_elem
,
int
base
=
10
)
{
mpz_t
t
;
mpz_t
t
;
mpz_init
(
t
);
mpz_init
(
t
);
field_elem
.
as_bigint
().
to_mpz
(
t
);
field_elem
.
as_bigint
().
to_mpz
(
t
);
char
arr
[
mpz_sizeinbase
(
t
,
base
)
+
2
];
char
arr
[
mpz_sizeinbase
(
t
,
base
)
+
2
];
char
*
tmp
=
mpz_get_str
(
arr
,
base
,
t
);
char
*
tmp
=
mpz_get_str
(
arr
,
base
,
t
);
mpz_clear
(
t
);
mpz_clear
(
t
);
string
output
=
tmp
;
string
output
=
tmp
;
return
output
;
return
output
;
}
}
string
gen_dkg_poly
(
int
_t
)
{
string
gen_dkg_poly
(
int
_t
)
{
vector
<
char
>
errMsg
(
1024
,
0
);
vector
<
char
>
errMsg
(
1024
,
0
);
int
err_status
=
0
;
int
err_status
=
0
;
...
@@ -84,27 +82,25 @@ string gen_dkg_poly( int _t){
...
@@ -84,27 +82,25 @@ string gen_dkg_poly( int _t){
uint32_t
enc_len
=
0
;
uint32_t
enc_len
=
0
;
if
(
!
encryptKeys
)
if
(
!
encryptKeys
)
status
=
gen_dkg_secret
(
eid
,
&
err_status
,
errMsg
.
data
(),
encrypted_dkg_secret
.
data
(),
&
enc_len
,
_t
);
status
=
gen_dkg_secret
(
eid
,
&
err_status
,
errMsg
.
data
(),
encrypted_dkg_secret
.
data
(),
&
enc_len
,
_t
);
else
else
status
=
gen_dkg_secret_aes
(
eid
,
&
err_status
,
errMsg
.
data
(),
encrypted_dkg_secret
.
data
(),
&
enc_len
,
_t
);
status
=
gen_dkg_secret_aes
(
eid
,
&
err_status
,
errMsg
.
data
(),
encrypted_dkg_secret
.
data
(),
&
enc_len
,
_t
);
if
(
err_status
!=
0
)
{
if
(
err_status
!=
0
)
{
throw
RPCException
(
-
666
,
errMsg
.
data
()
)
;
throw
RPCException
(
-
666
,
errMsg
.
data
()
)
;
}
}
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"gen_dkg_secret, status {}"
,
err_status
,
" err msg "
,
errMsg
.
data
());
spdlog
::
debug
(
"gen_dkg_secret, status {}"
,
err_status
,
" err msg "
,
errMsg
.
data
());
spdlog
::
debug
(
"in DKGCrypto encr len is {}"
,
enc_len
);
spdlog
::
debug
(
"in DKGCrypto encr len is {}"
,
enc_len
);
}
uint64_t
length
=
DKG_MAX_SEALED_LEN
;
uint64_t
length
=
DKG_MAX_SEALED_LEN
;
if
(
encryptKeys
){
if
(
encryptKeys
)
{
length
=
enc_len
;
length
=
enc_len
;
}
}
//vector<char> hexEncrPoly(DKG_MAX_SEALED_LEN * 2 + 1, 0);//(4*BUF_LEN, 1);
//vector<char> hexEncrPoly(DKG_MAX_SEALED_LEN * 2 + 1, 0);//(4*BUF_LEN, 1);
vector
<
char
>
hexEncrPoly
(
2
*
length
+
1
,
0
);
vector
<
char
>
hexEncrPoly
(
2
*
length
+
1
,
0
);
assert
(
encrypted_dkg_secret
.
size
()
>=
length
);
assert
(
encrypted_dkg_secret
.
size
()
>=
length
);
//carray2Hex(encrypted_dkg_secret.data(), DKG_MAX_SEALED_LEN, hexEncrPoly.data());
//carray2Hex(encrypted_dkg_secret.data(), DKG_MAX_SEALED_LEN, hexEncrPoly.data());
carray2Hex
(
encrypted_dkg_secret
.
data
(),
length
,
hexEncrPoly
.
data
());
carray2Hex
(
encrypted_dkg_secret
.
data
(),
length
,
hexEncrPoly
.
data
());
string
result
(
hexEncrPoly
.
data
());
string
result
(
hexEncrPoly
.
data
());
...
@@ -112,343 +108,315 @@ string gen_dkg_poly( int _t){
...
@@ -112,343 +108,315 @@ string gen_dkg_poly( int _t){
return
result
;
return
result
;
}
}
vector
<
vector
<
string
>>
get_verif_vect
(
const
char
*
encryptedPolyHex
,
int
t
,
int
n
)
{
vector
<
vector
<
string
>>
get_verif_vect
(
const
char
*
encryptedPolyHex
,
int
t
,
int
n
)
{
char
*
errMsg1
=
(
char
*
)
calloc
(
1024
,
1
);
char
*
errMsg1
=
(
char
*
)
calloc
(
1024
,
1
);
//char errMsg1[BUF_LEN];
//char errMsg1[BUF_LEN];
int
err_status
=
0
;
int
err_status
=
0
;
if
(
printDebugInfo
)
{
// cerr << "got encr poly " << encryptedPolyHex << endl;
// cerr << "got encr poly " << encryptedPolyHex << endl;
spdlog
::
debug
(
"got encr poly size {}"
,
char_traits
<
char
>::
length
(
encryptedPolyHex
));
spdlog
::
debug
(
"got encr poly size {}"
,
char_traits
<
char
>::
length
(
encryptedPolyHex
));
}
char
*
public_shares
=
(
char
*
)
calloc
(
10000
,
1
);
memset
(
public_shares
,
0
,
10000
);
// char public_shares[10000];
uint64_t
enc_len
=
0
;
char
*
public_shares
=
(
char
*
)
calloc
(
10000
,
1
);
memset
(
public_shares
,
0
,
10000
);
// char public_shares[10000];
uint8_t
*
encr_dkg_poly
=
(
uint8_t
*
)
calloc
(
DKG_MAX_SEALED_LEN
*
2
,
1
);
uint64_t
enc_len
=
0
;
memset
(
encr_dkg_poly
,
0
,
DKG_MAX_SEALED_LEN
*
2
);
//uint8_t encr_dkg_poly[DKG_MAX_SEALED_LEN];
if
(
!
hex2carray2
(
encryptedPolyHex
,
&
enc_len
,
encr_dkg_poly
,
6100
)){
uint8_t
*
encr_dkg_poly
=
(
uint8_t
*
)
calloc
(
DKG_MAX_SEALED_LEN
*
2
,
1
);
throw
RPCException
(
INVALID_HEX
,
"Invalid encryptedPolyHex"
);
memset
(
encr_dkg_poly
,
0
,
DKG_MAX_SEALED_LEN
*
2
);
}
//uint8_t encr_dkg_poly[DKG_MAX_SEALED_LEN];
if
(
!
hex2carray2
(
encryptedPolyHex
,
&
enc_len
,
encr_dkg_poly
,
6100
))
{
throw
RPCException
(
INVALID_HEX
,
"Invalid encryptedPolyHex"
);
}
if
(
printDebugInfo
)
{
//cerr << "hex_encr_poly is " << encryptedPolyHex << std::endl;
//cerr << "hex_encr_poly is " << encryptedPolyHex << std::endl;
spdlog
::
debug
(
"hex_encr_poly length is {}"
,
strlen
(
encryptedPolyHex
));
spdlog
::
debug
(
"hex_encr_poly length is {}"
,
strlen
(
encryptedPolyHex
));
spdlog
::
debug
(
"enc len {}"
,
enc_len
);
spdlog
::
debug
(
"enc len {}"
,
enc_len
);
// cerr << "encr raw poly: " << endl;
// for ( int i = 0 ; i < 3050; i++)
// printf(" %d ", encr_dkg_poly[i] );
}
uint32_t
len
=
0
;
if
(
!
encryptKeys
)
uint32_t
len
=
0
;
status
=
get_public_shares
(
eid
,
&
err_status
,
errMsg1
,
encr_dkg_poly
,
len
,
public_shares
,
t
,
n
);
else
{
if
(
!
encryptKeys
)
status
=
get_public_shares
(
eid
,
&
err_status
,
errMsg1
,
encr_dkg_poly
,
len
,
public_shares
,
t
,
n
);
else
{
status
=
get_public_shares_aes
(
eid
,
&
err_status
,
errMsg1
,
encr_dkg_poly
,
enc_len
,
public_shares
,
t
,
n
);
}
if
(
err_status
!=
0
)
{
throw
RPCException
(
-
666
,
errMsg1
);
}
status
=
get_public_shares_aes
(
eid
,
&
err_status
,
errMsg1
,
encr_dkg_poly
,
enc_len
,
public_shares
,
t
,
n
);
}
if
(
err_status
!=
0
){
throw
RPCException
(
-
666
,
errMsg1
);
}
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"err msg is {}"
,
errMsg1
);
spdlog
::
debug
(
"err msg is {}"
,
errMsg1
);
spdlog
::
debug
(
"public_shares:"
);
spdlog
::
debug
(
"public_shares:"
);
spdlog
::
debug
(
"{}"
,
public_shares
);
spdlog
::
debug
(
"{}"
,
public_shares
);;
// cerr << "public_shares:" << endl;
// cerr << public_shares << endl;
spdlog
::
debug
(
"get_public_shares status: {}"
,
err_status
);
spdlog
::
debug
(
"get_public_shares status: {}"
,
err_status
);
//printf("\nget_public_shares status: %d error %s \n\n", err_status, errMsg1);
}
vector
<
string
>
G2_strings
=
SplitString
(
public_shares
,
','
);
vector
<
string
>
G2_strings
=
SplitString
(
public_shares
,
','
);
vector
<
vector
<
string
>>
pub_shares_vect
;
vector
<
vector
<
string
>>
pub_shares_vect
;
for
(
uint64_t
i
=
0
;
i
<
G2_strings
.
size
();
i
++
)
{
for
(
uint64_t
i
=
0
;
i
<
G2_strings
.
size
();
i
++
)
{
vector
<
string
>
koef_str
=
SplitString
(
G2_strings
.
at
(
i
).
c_str
(),
':'
);
vector
<
string
>
koef_str
=
SplitString
(
G2_strings
.
at
(
i
).
c_str
(),
':'
);
pub_shares_vect
.
push_back
(
koef_str
);
pub_shares_vect
.
push_back
(
koef_str
);
}
}
free
(
errMsg1
);
free
(
errMsg1
);
free
(
public_shares
);
free
(
public_shares
);
free
(
encr_dkg_poly
);
free
(
encr_dkg_poly
);
return
pub_shares_vect
;
return
pub_shares_vect
;
}
}
string
get_secret_shares
(
const
string
&
polyName
,
const
char
*
encryptedPolyHex
,
const
vector
<
string
>&
publicKeys
,
int
t
,
int
n
){
string
get_secret_shares
(
const
string
&
polyName
,
const
char
*
encryptedPolyHex
,
const
vector
<
string
>
&
publicKeys
,
int
t
,
//char* errMsg1 = (char*) calloc(1024,1);
int
n
)
{
char
errMsg1
[
BUF_LEN
];
//char* errMsg1 = (char*) calloc(1024,1);
int
err_status
=
0
;
char
errMsg1
[
BUF_LEN
];
char
hexEncrKey
[
BUF_LEN
];
int
err_status
=
0
;
memset
(
hexEncrKey
,
0
,
BUF_LEN
);
char
hexEncrKey
[
BUF_LEN
];
uint64_t
enc_len
=
0
;
memset
(
hexEncrKey
,
0
,
BUF_LEN
);
uint64_t
enc_len
=
0
;
// uint8_t* encr_dkg_poly = (uint8_t*) calloc(DKG_MAX_SEALED_LEN, 1);
uint8_t
encr_dkg_poly
[
DKG_MAX_SEALED_LEN
];
// uint8_t* encr_dkg_poly = (uint8_t*) calloc(DKG_MAX_SEALED_LEN, 1);
memset
(
encr_dkg_poly
,
0
,
DKG_MAX_SEALED_LEN
);
uint8_t
encr_dkg_poly
[
DKG_MAX_SEALED_LEN
];
if
(
!
hex2carray2
(
encryptedPolyHex
,
&
enc_len
,
encr_dkg_poly
,
6100
)){
memset
(
encr_dkg_poly
,
0
,
DKG_MAX_SEALED_LEN
);
throw
RPCException
(
INVALID_HEX
,
"Invalid encryptedPolyHex"
);
if
(
!
hex2carray2
(
encryptedPolyHex
,
&
enc_len
,
encr_dkg_poly
,
6100
))
{
}
throw
RPCException
(
INVALID_HEX
,
"Invalid encryptedPolyHex"
);
std
::
cerr
<<
"enc_len is "
<<
enc_len
<<
std
::
endl
;
if
(
!
encryptKeys
)
status
=
set_encrypted_dkg_poly
(
eid
,
&
err_status
,
errMsg1
,
encr_dkg_poly
);
else
status
=
set_encrypted_dkg_poly_aes
(
eid
,
&
err_status
,
errMsg1
,
encr_dkg_poly
,
&
enc_len
);
if
(
status
!=
SGX_SUCCESS
||
err_status
!=
0
){
throw
RPCException
(
-
666
,
errMsg1
);
}
string
result
;
//char *hexEncrKey = (char *) calloc(2 * BUF_LEN, 1);
for
(
int
i
=
0
;
i
<
n
;
i
++
){
uint8_t
encryptedSkey
[
BUF_LEN
];
memset
(
encryptedSkey
,
0
,
BUF_LEN
);
uint32_t
dec_len
;
char
cur_share
[
193
];
char
s_shareG2
[
320
];
string
pub_keyB
=
publicKeys
.
at
(
i
);
//publicKeys.substr(128*i, 128*i + 128);
// if (DEBUG_PRINT) {
// spdlog::info("pub_keyB is {}", pub_keyB);
// }
char
pubKeyB
[
129
];
strncpy
(
pubKeyB
,
pub_keyB
.
c_str
(),
128
);
pubKeyB
[
128
]
=
0
;
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"pubKeyB is {}"
,
pub_keyB
);
}
}
std
::
cerr
<<
"enc_len is "
<<
enc_len
<<
std
::
endl
;
if
(
!
encryptKeys
)
if
(
!
encryptKeys
)
get_encr_sshare
(
eid
,
&
err_status
,
errMsg1
,
encryptedSkey
,
&
dec_len
,
status
=
set_encrypted_dkg_poly
(
eid
,
&
err_status
,
errMsg1
,
encr_dkg_poly
);
cur_share
,
s_shareG2
,
pubKeyB
,
t
,
n
,
i
+
1
);
else
else
get_encr_sshare_aes
(
eid
,
&
err_status
,
errMsg1
,
encryptedSkey
,
&
dec_len
,
status
=
set_encrypted_dkg_poly_aes
(
eid
,
&
err_status
,
errMsg1
,
encr_dkg_poly
,
&
enc_len
);
cur_share
,
s_shareG2
,
pubKeyB
,
t
,
n
,
i
+
1
);
if
(
err_status
!=
0
){
if
(
status
!=
SGX_SUCCESS
||
err_status
!=
0
)
{
throw
RPCException
(
-
666
,
errMsg1
);
throw
RPCException
(
-
666
,
errMsg1
);
}
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"cur_share is {}"
,
cur_share
);
}
}
result
+=
cur_share
;
string
result
;
//char *hexEncrKey = (char *) calloc(2 * BUF_LEN, 1);
//uint32_t enc_len = BUF_LEN;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
if
(
printDebugInfo
)
{
uint8_t
encryptedSkey
[
BUF_LEN
];
spdlog
::
debug
(
"dec len is {}"
,
dec_len
);
memset
(
encryptedSkey
,
0
,
BUF_LEN
);
}
uint32_t
dec_len
;
char
cur_share
[
193
];
char
s_shareG2
[
320
];
string
pub_keyB
=
publicKeys
.
at
(
i
);
//publicKeys.substr(128*i, 128*i + 128);
// if (DEBUG_PRINT) {
// spdlog::info("pub_keyB is {}", pub_keyB);
// }
char
pubKeyB
[
129
];
strncpy
(
pubKeyB
,
pub_keyB
.
c_str
(),
128
);
pubKeyB
[
128
]
=
0
;
carray2Hex
(
encryptedSkey
,
dec_len
,
hexEncrKey
);
spdlog
::
debug
(
"pubKeyB is {}"
,
pub_keyB
);
string
dhKeyName
=
"DKG_DH_KEY_"
+
polyName
+
"_"
+
to_string
(
i
)
+
":"
;
if
(
!
encryptKeys
)
get_encr_sshare
(
eid
,
&
err_status
,
errMsg1
,
encryptedSkey
,
&
dec_len
,
cur_share
,
s_shareG2
,
pubKeyB
,
t
,
n
,
i
+
1
);
else
get_encr_sshare_aes
(
eid
,
&
err_status
,
errMsg1
,
encryptedSkey
,
&
dec_len
,
cur_share
,
s_shareG2
,
pubKeyB
,
t
,
n
,
i
+
1
);
if
(
err_status
!=
0
)
{
throw
RPCException
(
-
666
,
errMsg1
);
}
spdlog
::
debug
(
"hexEncr DH Key: { }"
,
hexEncrKey
);
spdlog
::
debug
(
"cur_share is {}"
,
cur_share
);
SGXWalletServer
::
writeDataToDB
(
dhKeyName
,
hexEncrKey
);
string
shareG2_name
=
"shareG2_"
+
polyName
+
"_"
+
to_string
(
i
)
+
":"
;
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"name to write to db is {}"
,
dhKeyName
);
spdlog
::
debug
(
"name to write to db is {}"
,
shareG2_name
);
spdlog
::
debug
(
"s_shareG2: {}"
,
s_shareG2
);
}
SGXWalletServer
::
writeDataToDB
(
shareG2_name
,
s_shareG2
);
if
(
printDebugInfo
)
{
result
+=
cur_share
;
spdlog
::
debug
(
"errMsg: {}"
,
errMsg1
);
// cerr << "iteration " << i <<" result length is " << result.length() << endl ;
spdlog
::
debug
(
"dec len is {}"
,
dec_len
);
// cerr << "iteration " << i <<" share length is " << strlen(cur_share) << endl;
carray2Hex
(
encryptedSkey
,
dec_len
,
hexEncrKey
);
// cerr << "iteration " << i <<" share is " << cur_share << endl;
string
dhKeyName
=
"DKG_DH_KEY_"
+
polyName
+
"_"
+
to_string
(
i
)
+
":"
;
spdlog
::
debug
(
"hexEncr DH Key: { }"
,
hexEncrKey
);
SGXWalletServer
::
writeDataToDB
(
dhKeyName
,
hexEncrKey
);
string
shareG2_name
=
"shareG2_"
+
polyName
+
"_"
+
to_string
(
i
)
+
":"
;
spdlog
::
debug
(
"name to write to db is {}"
,
dhKeyName
);
spdlog
::
debug
(
"name to write to db is {}"
,
shareG2_name
);
spdlog
::
debug
(
"s_shareG2: {}"
,
s_shareG2
);
SGXWalletServer
::
writeDataToDB
(
shareG2_name
,
s_shareG2
);
spdlog
::
debug
(
"errMsg: {}"
,
errMsg1
);
}
}
}
//result += '\0';
//result += '\0';
//free(encr_dkg_poly);
//free(encr_dkg_poly);
// free(errMsg1);
// free(errMsg1);
//free(hexEncrKey);
//free(hexEncrKey);
return
result
;
return
result
;
}
}
bool
VerifyShares
(
const
char
*
publicShares
,
const
char
*
encr_sshare
,
const
char
*
encryptedKeyHex
,
int
t
,
int
n
,
int
ind
){
bool
VerifyShares
(
const
char
*
publicShares
,
const
char
*
encr_sshare
,
const
char
*
encryptedKeyHex
,
int
t
,
int
n
,
int
ind
)
{
//char* errMsg1 = (char*) calloc(1024,1);
//char* errMsg1 = (char*) calloc(1024,1);
char
errMsg1
[
BUF_LEN
];
char
errMsg1
[
BUF_LEN
];
int
err_status
=
0
;
int
err_status
=
0
;
uint64_t
dec_key_len
;
uint64_t
dec_key_len
;
uint8_t
encr_key
[
BUF_LEN
];
uint8_t
encr_key
[
BUF_LEN
];
memset
(
encr_key
,
0
,
BUF_LEN
);
memset
(
encr_key
,
0
,
BUF_LEN
);
if
(
!
hex2carray
(
encryptedKeyHex
,
&
dec_key_len
,
encr_key
)){
if
(
!
hex2carray
(
encryptedKeyHex
,
&
dec_key_len
,
encr_key
))
{
throw
RPCException
(
INVALID_HEX
,
"Invalid encryptedPolyHex"
);
throw
RPCException
(
INVALID_HEX
,
"Invalid encryptedPolyHex"
);
}
}
int
result
;
int
result
;
if
(
printDebugInfo
)
{
cerr
<<
"encryptedKeyHex "
<<
encryptedKeyHex
<<
endl
;
cerr
<<
"encryptedKeyHex "
<<
encryptedKeyHex
<<
endl
;
cerr
<<
"dec_key_len "
<<
dec_key_len
<<
endl
;
cerr
<<
"dec_key_len "
<<
dec_key_len
<<
endl
;
cerr
<<
"encr_sshare length is "
<<
strlen
(
encr_sshare
)
<<
endl
;
cerr
<<
"encr_sshare length is "
<<
strlen
(
encr_sshare
)
<<
endl
;
//cerr << "public shares " << publicShares << endl;
//cerr << "public shares " << publicShares << endl;
spdlog
::
debug
(
"publicShares length is {}"
,
char_traits
<
char
>::
length
(
publicShares
));
spdlog
::
debug
(
"publicShares length is {}"
,
char_traits
<
char
>::
length
(
publicShares
));
}
char
pshares
[
8193
];
char
pshares
[
8193
];
memset
(
pshares
,
0
,
8193
);
memset
(
pshares
,
0
,
8193
);
strncpy
(
pshares
,
publicShares
,
strlen
(
publicShares
)
);
strncpy
(
pshares
,
publicShares
,
strlen
(
publicShares
));
if
(
!
encryptKeys
)
if
(
!
encryptKeys
)
dkg_verification
(
eid
,
&
err_status
,
errMsg1
,
pshares
,
encr_sshare
,
encr_key
,
dec_key_len
,
t
,
ind
,
&
result
);
dkg_verification
(
eid
,
&
err_status
,
errMsg1
,
pshares
,
encr_sshare
,
encr_key
,
dec_key_len
,
t
,
ind
,
&
result
);
else
else
dkg_verification_aes
(
eid
,
&
err_status
,
errMsg1
,
pshares
,
encr_sshare
,
encr_key
,
dec_key_len
,
t
,
ind
,
&
result
);
dkg_verification_aes
(
eid
,
&
err_status
,
errMsg1
,
pshares
,
encr_sshare
,
encr_key
,
dec_key_len
,
t
,
ind
,
&
result
);
if
(
result
==
2
){
if
(
result
==
2
)
{
throw
RPCException
(
INVALID_HEX
,
"Invalid public shares"
);
throw
RPCException
(
INVALID_HEX
,
"Invalid public shares"
);
}
}
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"errMsg1: {}"
,
errMsg1
);
spdlog
::
debug
(
"errMsg1: {}"
,
errMsg1
);
spdlog
::
debug
(
"result is: {}"
,
result
);
spdlog
::
debug
(
"result is: {}"
,
result
);
}
//free(errMsg1);
//free(errMsg1);
return
result
;
return
result
;
}
}
bool
CreateBLSShare
(
const
string
&
blsKeyName
,
const
char
*
s_shares
,
const
char
*
encryptedKeyHex
)
{
bool
CreateBLSShare
(
const
string
&
blsKeyName
,
const
char
*
s_shares
,
const
char
*
encryptedKeyHex
)
{
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"ENTER CreateBLSShare"
);
spdlog
::
debug
(
"ENTER CreateBLSShare"
);
}
// char* errMsg1 = (char*) calloc(1024,1);
// char* errMsg1 = (char*) calloc(1024,1);
char
errMsg1
[
BUF_LEN
];
char
errMsg1
[
BUF_LEN
];
int
err_status
=
0
;
int
err_status
=
0
;
uint64_t
dec_key_len
;
uint64_t
dec_key_len
;
uint8_t
encr_bls_key
[
BUF_LEN
];
uint8_t
encr_bls_key
[
BUF_LEN
];
memset
(
encr_bls_key
,
0
,
BUF_LEN
);
memset
(
encr_bls_key
,
0
,
BUF_LEN
);
uint8_t
encr_key
[
BUF_LEN
];
uint8_t
encr_key
[
BUF_LEN
];
memset
(
encr_key
,
0
,
BUF_LEN
);
memset
(
encr_key
,
0
,
BUF_LEN
);
if
(
!
hex2carray
(
encryptedKeyHex
,
&
dec_key_len
,
encr_key
)){
if
(
!
hex2carray
(
encryptedKeyHex
,
&
dec_key_len
,
encr_key
))
{
throw
RPCException
(
INVALID_HEX
,
"Invalid encryptedKeyHex"
);
throw
RPCException
(
INVALID_HEX
,
"Invalid encryptedKeyHex"
);
}
}
uint32_t
enc_bls_len
=
0
;
uint32_t
enc_bls_len
=
0
;
//cerr << "BEFORE create_bls_key IN ENCLAVE " << endl;
//cerr << "BEFORE create_bls_key IN ENCLAVE " << endl;
if
(
!
encryptKeys
)
if
(
!
encryptKeys
)
create_bls_key
(
eid
,
&
err_status
,
errMsg1
,
s_shares
,
encr_key
,
dec_key_len
,
encr_bls_key
,
&
enc_bls_len
);
create_bls_key
(
eid
,
&
err_status
,
errMsg1
,
s_shares
,
encr_key
,
dec_key_len
,
encr_bls_key
,
&
enc_bls_len
);
else
else
create_bls_key_aes
(
eid
,
&
err_status
,
errMsg1
,
s_shares
,
encr_key
,
dec_key_len
,
encr_bls_key
,
&
enc_bls_len
);
create_bls_key_aes
(
eid
,
&
err_status
,
errMsg1
,
s_shares
,
encr_key
,
dec_key_len
,
encr_bls_key
,
&
enc_bls_len
);
//cerr << "AFTER create_bls_key IN ENCLAVE er msg is " << errMsg1 << endl;
//cerr << "AFTER create_bls_key IN ENCLAVE er msg is " << errMsg1 << endl;
if
(
err_status
!=
0
){
if
(
err_status
!=
0
)
{
//spdlog::info("ERROR IN ENCLAVE with status {}", err_status);
//spdlog::info("ERROR IN ENCLAVE with status {}", err_status);
spdlog
::
error
(
errMsg1
);
spdlog
::
error
(
errMsg1
);
spdlog
::
error
(
"status {}"
,
err_status
);
spdlog
::
error
(
"status {}"
,
err_status
);
throw
RPCException
(
ERROR_IN_ENCLAVE
,
"Create BLS private key failed in enclave"
);
throw
RPCException
(
ERROR_IN_ENCLAVE
,
"Create BLS private key failed in enclave"
);
}
}
else
{
else
{
//char *hexBLSKey = (char *) calloc(2 * BUF_LEN, 1);
//char *hexBLSKey = (char *) calloc(2 * BUF_LEN, 1);
char
hexBLSKey
[
2
*
BUF_LEN
];
char
hexBLSKey
[
2
*
BUF_LEN
];
//cerr << "BEFORE carray2Hex" << endl;
//cerr << "BEFORE carray2Hex" << endl;
//cerr << "enc_bls_len " << enc_bls_len << endl;
//cerr << "enc_bls_len " << enc_bls_len << endl;
carray2Hex
(
encr_bls_key
,
enc_bls_len
,
hexBLSKey
);
carray2Hex
(
encr_bls_key
,
enc_bls_len
,
hexBLSKey
);
// cerr << "BEFORE WRITE BLS KEY TO DB" << endl;
// cerr << "BEFORE WRITE BLS KEY TO DB" << endl;
SGXWalletServer
::
writeDataToDB
(
blsKeyName
,
hexBLSKey
);
SGXWalletServer
::
writeDataToDB
(
blsKeyName
,
hexBLSKey
);
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"hexBLSKey length is {}"
,
char_traits
<
char
>::
length
(
hexBLSKey
));
spdlog
::
debug
(
"hexBLSKey length is {}"
,
char_traits
<
char
>::
length
(
hexBLSKey
));
spdlog
::
debug
(
"bls key {}"
,
blsKeyName
,
" is "
,
hexBLSKey
);
spdlog
::
debug
(
"bls key {}"
,
blsKeyName
,
" is "
,
hexBLSKey
);
//free(hexBLSKey);
return
true
;
}
}
//free(hexBLSKey);
return
true
;
}
}
}
vector
<
string
>
GetBLSPubKey
(
const
char
*
encryptedKeyHex
)
{
vector
<
string
>
GetBLSPubKey
(
const
char
*
encryptedKeyHex
)
{
//char* errMsg1 = (char*) calloc(1024,1);
//char* errMsg1 = (char*) calloc(1024,1);
char
errMsg1
[
BUF_LEN
];
char
errMsg1
[
BUF_LEN
];
int
err_status
=
0
;
int
err_status
=
0
;
uint64_t
dec_key_len
;
uint64_t
dec_key_len
;
uint8_t
encr_key
[
BUF_LEN
];
uint8_t
encr_key
[
BUF_LEN
];
if
(
!
hex2carray
(
encryptedKeyHex
,
&
dec_key_len
,
encr_key
)){
if
(
!
hex2carray
(
encryptedKeyHex
,
&
dec_key_len
,
encr_key
))
{
throw
RPCException
(
INVALID_HEX
,
"Invalid encryptedKeyHex"
);
throw
RPCException
(
INVALID_HEX
,
"Invalid encryptedKeyHex"
);
}
}
char
pub_key
[
320
];
char
pub_key
[
320
];
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"dec_key_len is {}"
,
dec_key_len
);
spdlog
::
debug
(
"dec_key_len is {}"
,
dec_key_len
);
}
if
(
!
encryptKeys
)
if
(
!
encryptKeys
)
get_bls_pub_key
(
eid
,
&
err_status
,
errMsg1
,
encr_key
,
dec_key_len
,
pub_key
);
get_bls_pub_key
(
eid
,
&
err_status
,
errMsg1
,
encr_key
,
dec_key_len
,
pub_key
);
else
else
get_bls_pub_key_aes
(
eid
,
&
err_status
,
errMsg1
,
encr_key
,
dec_key_len
,
pub_key
);
get_bls_pub_key_aes
(
eid
,
&
err_status
,
errMsg1
,
encr_key
,
dec_key_len
,
pub_key
);
if
(
err_status
!=
0
)
{
if
(
err_status
!=
0
)
{
std
::
cerr
<<
errMsg1
<<
" status is "
<<
err_status
<<
std
::
endl
;
std
::
cerr
<<
errMsg1
<<
" status is "
<<
err_status
<<
std
::
endl
;
throw
RPCException
(
ERROR_IN_ENCLAVE
,
"Failed to get BLS public key in enclave"
);
throw
RPCException
(
ERROR_IN_ENCLAVE
,
"Failed to get BLS public key in enclave"
);
}
}
vector
<
string
>
pub_key_vect
=
SplitString
(
pub_key
,
':'
);
vector
<
string
>
pub_key_vect
=
SplitString
(
pub_key
,
':'
);
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"errMsg1 is {}"
,
errMsg1
);
spdlog
::
debug
(
"errMsg1 is {}"
,
errMsg1
);
spdlog
::
debug
(
"pub key is "
);
spdlog
::
debug
(
"pub key is "
);
for
(
int
i
=
0
;
i
<
4
;
i
++
)
for
(
int
i
=
0
;
i
<
4
;
i
++
)
spdlog
::
debug
(
"{}"
,
pub_key_vect
.
at
(
i
));
spdlog
::
debug
(
"{}"
,
pub_key_vect
.
at
(
i
));
}
return
pub_key_vect
;
return
pub_key_vect
;
}
}
string
decrypt_DHKey
(
const
string
&
polyName
,
int
ind
)
{
string
decrypt_DHKey
(
const
string
&
polyName
,
int
ind
)
{
vector
<
char
>
errMsg1
(
1024
,
0
);
vector
<
char
>
errMsg1
(
1024
,
0
);
int
err_status
=
0
;
int
err_status
=
0
;
string
DH_key_name
=
polyName
+
"_"
+
to_string
(
ind
)
+
":"
;
shared_ptr
<
string
>
hexEncrKey_ptr
=
SGXWalletServer
::
readFromDb
(
DH_key_name
,
"DKG_DH_KEY_"
);
string
DH_key_name
=
polyName
+
"_"
+
to_string
(
ind
)
+
":"
;
shared_ptr
<
string
>
hexEncrKey_ptr
=
SGXWalletServer
::
readFromDb
(
DH_key_name
,
"DKG_DH_KEY_"
);
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"encr DH key is {}"
,
*
hexEncrKey_ptr
);
spdlog
::
debug
(
"encr DH key is {}"
,
*
hexEncrKey_ptr
);
}
vector
<
char
>
hexEncrKey
(
2
*
BUF_LEN
,
0
);
vector
<
char
>
hexEncrKey
(
2
*
BUF_LEN
,
0
);
uint64_t
DH_enc_len
=
0
;
uint64_t
DH_enc_len
=
0
;
uint8_t
encrypted_DHkey
[
BUF_LEN
];
uint8_t
encrypted_DHkey
[
BUF_LEN
];
if
(
!
hex2carray
(
hexEncrKey_ptr
->
c_str
(),
&
DH_enc_len
,
encrypted_DHkey
)){
if
(
!
hex2carray
(
hexEncrKey_ptr
->
c_str
(),
&
DH_enc_len
,
encrypted_DHkey
))
{
throw
RPCException
(
INVALID_HEX
,
"Invalid hexEncrKey"
);
throw
RPCException
(
INVALID_HEX
,
"Invalid hexEncrKey"
);
}
}
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"encr DH key length is {}"
,
DH_enc_len
);
spdlog
::
debug
(
"encr DH key length is {}"
,
DH_enc_len
);
spdlog
::
debug
(
"hex encr DH key length is {}"
,
hexEncrKey_ptr
->
length
());
spdlog
::
debug
(
"hex encr DH key length is {}"
,
hexEncrKey_ptr
->
length
());
}
char
DHKey
[
ECDSA_SKEY_LEN
];
if
(
!
encryptKeys
)
char
DHKey
[
ECDSA_SKEY_LEN
];
decrypt_key
(
eid
,
&
err_status
,
errMsg1
.
data
(),
encrypted_DHkey
,
DH_enc_len
,
DHKey
);
else
if
(
!
encryptKeys
)
decrypt_key_aes
(
eid
,
&
err_status
,
errMsg1
.
data
(),
encrypted_DHkey
,
DH_enc_len
,
DHKey
);
decrypt_key
(
eid
,
&
err_status
,
errMsg1
.
data
(),
encrypted_DHkey
,
DH_enc_len
,
DHKey
);
if
(
err_status
!=
0
){
else
throw
RPCException
(
/*ERROR_IN_ENCLAVE*/
err_status
,
"decrypt key failed in enclave"
);
decrypt_key_aes
(
eid
,
&
err_status
,
errMsg1
.
data
(),
encrypted_DHkey
,
DH_enc_len
,
DHKey
);
}
if
(
err_status
!=
0
)
{
throw
RPCException
(
/*ERROR_IN_ENCLAVE*/
err_status
,
"decrypt key failed in enclave"
);
}
return
DHKey
;
return
DHKey
;
}
}
vector
<
string
>
mult_G2
(
const
string
&
x
)
{
vector
<
string
>
mult_G2
(
const
string
&
x
)
{
vector
<
string
>
result
(
4
);
vector
<
string
>
result
(
4
);
libff
::
init_alt_bn128_params
();
libff
::
init_alt_bn128_params
();
libff
::
alt_bn128_Fr
el
(
x
.
c_str
());
libff
::
alt_bn128_Fr
el
(
x
.
c_str
());
...
...
ECDSACrypto.cpp
View file @
a2fdbcb7
...
@@ -61,13 +61,10 @@ std::vector<std::string> genECDSAKey() {
...
@@ -61,13 +61,10 @@ std::vector<std::string> genECDSAKey() {
throw
RPCException
(
status
,
errMsg
);
throw
RPCException
(
status
,
errMsg
);
}
}
std
::
vector
<
std
::
string
>
keys
(
3
);
std
::
vector
<
std
::
string
>
keys
(
3
);
if
(
printDebugInfo
)
{
std
::
cerr
<<
"account key is "
<<
errMsg
<<
std
::
endl
;
std
::
cerr
<<
"account key is "
<<
errMsg
<<
std
::
endl
;
std
::
cerr
<<
"enc_len is "
<<
enc_len
<<
std
::
endl
;
std
::
cerr
<<
"enc_len is "
<<
enc_len
<<
std
::
endl
;
// std::cerr << "enc_key is " << std::endl;
// for(int i = 0 ; i < 1024; i++)
// std::cerr << (int)encr_pr_key[i] << " " ;
}
char
*
hexEncrKey
=
(
char
*
)
calloc
(
BUF_LEN
*
2
,
1
);
char
*
hexEncrKey
=
(
char
*
)
calloc
(
BUF_LEN
*
2
,
1
);
carray2Hex
(
encr_pr_key
,
enc_len
,
hexEncrKey
);
carray2Hex
(
encr_pr_key
,
enc_len
,
hexEncrKey
);
keys
.
at
(
0
)
=
hexEncrKey
;
keys
.
at
(
0
)
=
hexEncrKey
;
...
@@ -77,10 +74,8 @@ std::vector<std::string> genECDSAKey() {
...
@@ -77,10 +74,8 @@ std::vector<std::string> genECDSAKey() {
unsigned
long
seed
=
randGen
();
unsigned
long
seed
=
randGen
();
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"seed is {}"
,
seed
);
spdlog
::
debug
(
"seed is {}"
,
seed
);
std
::
cerr
<<
"strlen is "
<<
strlen
(
hexEncrKey
)
<<
std
::
endl
;
std
::
cerr
<<
"strlen is "
<<
strlen
(
hexEncrKey
)
<<
std
::
endl
;
}
gmp_randstate_t
state
;
gmp_randstate_t
state
;
gmp_randinit_default
(
state
);
gmp_randinit_default
(
state
);
...
@@ -133,13 +128,12 @@ std::string getECDSAPubKey(const char *_encryptedKeyHex) {
...
@@ -133,13 +128,12 @@ std::string getECDSAPubKey(const char *_encryptedKeyHex) {
}
}
string
pubKey
=
string
(
pubKeyX
.
data
())
+
string
(
pubKeyY
.
data
());
//concatPubKeyWith0x(pub_key_x, pub_key_y);//
string
pubKey
=
string
(
pubKeyX
.
data
())
+
string
(
pubKeyY
.
data
());
//concatPubKeyWith0x(pub_key_x, pub_key_y);//
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"enc_len is {}"
,
enc_len
);
spdlog
::
debug
(
"enc_len is {}"
,
enc_len
);
spdlog
::
debug
(
"pubkey is {}"
,
pubKey
);
spdlog
::
debug
(
"pubkey is {}"
,
pubKey
);
spdlog
::
debug
(
"pubkey length is {}"
,
pubKey
.
length
());
spdlog
::
debug
(
"pubkey length is {}"
,
pubKey
.
length
());
spdlog
::
debug
(
"err str is {}"
,
errMsg
.
data
());
spdlog
::
debug
(
"err str is {}"
,
errMsg
.
data
());
spdlog
::
debug
(
"err status is {}"
,
err_status
);
spdlog
::
debug
(
"err status is {}"
,
err_status
);
}
return
pubKey
;
return
pubKey
;
}
}
...
...
LevelDB.cpp
View file @
a2fdbcb7
...
@@ -62,10 +62,10 @@ std::shared_ptr<string> LevelDB::readString(const string &_key) {
...
@@ -62,10 +62,10 @@ std::shared_ptr<string> LevelDB::readString(const string &_key) {
auto
status
=
db
->
Get
(
readOptions
,
_key
,
&*
result
);
auto
status
=
db
->
Get
(
readOptions
,
_key
,
&*
result
);
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"key to read from db: {}"
,
_key
);
spdlog
::
debug
(
"key to read from db: {}"
,
_key
);
//std::cerr << "key to read from db: " << _key << std::endl;
//std::cerr << "key to read from db: " << _key << std::endl;
}
throwExceptionOnError
(
status
);
throwExceptionOnError
(
status
);
...
@@ -83,10 +83,10 @@ void LevelDB::writeString(const string &_key, const string &_value) {
...
@@ -83,10 +83,10 @@ void LevelDB::writeString(const string &_key, const string &_value) {
throwExceptionOnError
(
status
);
throwExceptionOnError
(
status
);
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"written key: {}"
,
_key
);
spdlog
::
debug
(
"written key: {}"
,
_key
);
// std::cerr << "written key " << _key << std::endl;
// std::cerr << "written key " << _key << std::endl;
}
}
}
...
@@ -100,10 +100,9 @@ void LevelDB::deleteDHDKGKey (const string &_key) {
...
@@ -100,10 +100,9 @@ void LevelDB::deleteDHDKGKey (const string &_key) {
throwExceptionOnError
(
status
);
throwExceptionOnError
(
status
);
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"key deleted: {}"
,
full_key
);
spdlog
::
debug
(
"key deleted: {}"
,
full_key
);
//std::cerr << "key deleted " << full_key << std::endl;
//std::cerr << "key deleted " << full_key << std::endl;
}
}
}
void
LevelDB
::
deleteTempNEK
(
const
string
&
_key
){
void
LevelDB
::
deleteTempNEK
(
const
string
&
_key
){
...
@@ -130,10 +129,9 @@ void LevelDB::deleteKey(const string &_key){
...
@@ -130,10 +129,9 @@ void LevelDB::deleteKey(const string &_key){
throwExceptionOnError
(
status
);
throwExceptionOnError
(
status
);
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"key deleted: {}"
,
_key
);
spdlog
::
debug
(
"key deleted: {}"
,
_key
);
// std::cerr << "key deleted " << _key << std::endl;
// std::cerr << "key deleted " << _key << std::endl;
}
}
}
...
@@ -219,10 +217,9 @@ void LevelDB::writeDataUnique(const string & Name, const string &value) {
...
@@ -219,10 +217,9 @@ void LevelDB::writeDataUnique(const string & Name, const string &value) {
}
}
writeString
(
key
,
value
);
writeString
(
key
,
value
);
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"{}"
,
Name
,
" is written to db"
);
spdlog
::
debug
(
"{}"
,
Name
,
" is written to db"
);
//std::cerr << Name << " is written to db " << std::endl;
}
}
}
...
...
SEKManager.cpp
View file @
a2fdbcb7
...
@@ -231,8 +231,6 @@ void init_SEK(){
...
@@ -231,8 +231,6 @@ void init_SEK(){
gen_SEK
();
gen_SEK
();
}
}
else
{
else
{
if
(
printDebugInfo
)
spdlog
::
info
(
"going to set SEK from db"
);
set_SEK
(
encr_SEK_ptr
);
set_SEK
(
encr_SEK_ptr
);
}
}
}
}
...
...
ServerInit.cpp
View file @
a2fdbcb7
...
@@ -77,9 +77,8 @@ void initEnclave() {
...
@@ -77,9 +77,8 @@ void initEnclave() {
}
}
#endif
#endif
if
(
printDebugInfo
)
{
spdlog
::
debug
(
"SGX_DEBUG_FLAG = {}"
,
SGX_DEBUG_FLAG
);
spdlog
::
debug
(
"SGX_DEBUG_FLAG = {}"
,
SGX_DEBUG_FLAG
);
}
status
=
sgx_create_enclave_search
(
ENCLAVE_NAME
,
SGX_DEBUG_FLAG
,
&
token
,
status
=
sgx_create_enclave_search
(
ENCLAVE_NAME
,
SGX_DEBUG_FLAG
,
&
token
,
&
updated
,
&
eid
,
0
);
&
updated
,
&
eid
,
0
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment