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
d1af3589
Unverified
Commit
d1af3589
authored
Sep 08, 2020
by
Stan Kladko
Committed by
GitHub
Sep 08, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #176 from skalenetwork/SKALE-3205
Skale 3205
parents
da89bfe3
24667d66
Changes
17
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
467 additions
and
292 deletions
+467
-292
BLSCrypto.cpp
BLSCrypto.cpp
+38
-26
BLSPrivateKeyShareSGX.cpp
BLSPrivateKeyShareSGX.cpp
+10
-8
DKGCrypto.cpp
DKGCrypto.cpp
+66
-40
ECDSACrypto.cpp
ECDSACrypto.cpp
+23
-10
SEKManager.cpp
SEKManager.cpp
+50
-15
SEKManager.h
SEKManager.h
+7
-0
SGXWalletServer.cpp
SGXWalletServer.cpp
+4
-1
ServerInit.cpp
ServerInit.cpp
+38
-17
ServerInit.h
ServerInit.h
+1
-1
VERSION
VERSION
+1
-1
common.h
common.h
+15
-0
AESUtils.c
secure_enclave/AESUtils.c
+45
-85
AESUtils.h
secure_enclave/AESUtils.h
+12
-6
secure_enclave.c
secure_enclave/secure_enclave.c
+115
-60
secure_enclave.edl
secure_enclave/secure_enclave.edl
+15
-15
sgxwallet_common.h
sgxwallet_common.h
+19
-0
testw.cpp
testw.cpp
+8
-7
No files found.
BLSCrypto.cpp
View file @
d1af3589
...
...
@@ -29,14 +29,12 @@
#include "third_party/intel/create_enclave.h"
#include "bls.h"
#include <bls/BLSutils.h>
#include "BLSPrivateKeyShareSGX.h"
#include "sgxwallet_common.h"
#include "sgxwallet.h"
#include "SGXException.h"
...
...
@@ -44,8 +42,10 @@
#include "common.h"
#include "SGXWalletServer.h"
#include "BLSCrypto.h"
#include "SEKManager.h"
#include "LevelDB.h"
#include "ServerInit.h"
#include "BLSCrypto.h"
string
*
FqToString
(
libff
::
alt_bn128_Fq
*
_fq
)
{
...
...
@@ -57,7 +57,7 @@ string *FqToString(libff::alt_bn128_Fq *_fq) {
_fq
->
as_bigint
().
to_mpz
(
t
);
SAFE_CHAR_BUF
(
arr
,
mpz_sizeinbase
(
t
,
10
)
+
2
);
SAFE_CHAR_BUF
(
arr
,
mpz_sizeinbase
(
t
,
10
)
+
2
);
mpz_get_str
(
arr
,
10
,
t
);
mpz_clear
(
t
);
...
...
@@ -86,7 +86,7 @@ void carray2Hex(const unsigned char *d, uint64_t _len, char *_hexArray,
CHECK_STATE
(
_hexArrayLen
>
2
*
_len
);
for
(
in
t
j
=
0
;
j
<
_len
;
j
++
)
{
for
(
uint64_
t
j
=
0
;
j
<
_len
;
j
++
)
{
_hexArray
[
j
*
2
]
=
hexval
[((
d
[
j
]
>>
4
)
&
0xF
)];
_hexArray
[
j
*
2
+
1
]
=
hexval
[(
d
[
j
])
&
0x0F
];
}
...
...
@@ -95,9 +95,8 @@ void carray2Hex(const unsigned char *d, uint64_t _len, char *_hexArray,
}
bool
hex2carray
(
const
char
*
_hex
,
uint64_t
*
_bin_len
,
uint8_t
*
_bin
,
uint64_t
_max_length
)
{
uint8_t
*
_bin
,
uint64_t
_max_length
)
{
CHECK_STATE
(
_hex
);
...
...
@@ -105,11 +104,11 @@ bool hex2carray(const char *_hex, uint64_t *_bin_len,
CHECK_STATE
(
_bin_len
)
in
t
len
=
strnlen
(
_hex
,
2
*
_max_length
+
1
);
uint64_
t
len
=
strnlen
(
_hex
,
2
*
_max_length
+
1
);
CHECK_STATE
(
len
!=
2
*
_max_length
+
1
);
CHECK_STATE
(
len
<=
2
*
_max_length
);
CHECK_STATE
(
len
<=
2
*
_max_length
);
if
(
len
==
0
&&
len
%
2
==
1
)
...
...
@@ -117,7 +116,7 @@ bool hex2carray(const char *_hex, uint64_t *_bin_len,
*
_bin_len
=
len
/
2
;
for
(
in
t
i
=
0
;
i
<
len
/
2
;
i
++
)
{
for
(
uint64_
t
i
=
0
;
i
<
len
/
2
;
i
++
)
{
int
high
=
char2int
((
char
)
_hex
[
i
*
2
]);
int
low
=
char2int
((
char
)
_hex
[
i
*
2
+
1
]);
...
...
@@ -141,7 +140,8 @@ bool sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t
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
;
...
...
@@ -166,7 +166,8 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
CHECK_STATE
(
_hashHex
);
CHECK_STATE
(
_sig
);
auto
hash
=
make_shared
<
array
<
uint8_t
,
32
>>
();
auto
hash
=
make_shared
<
array
<
uint8_t
,
32
>>
();
uint64_t
binLen
;
...
...
@@ -174,10 +175,10 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
throw
SGXException
(
INVALID_HEX
,
"Invalid hash"
);
}
shared_ptr
<
signatures
::
Bls
>
obj
;
shared_ptr
<
signatures
::
Bls
>
obj
;
obj
=
make_shared
<
signatures
::
Bls
>
(
signatures
::
Bls
(
_t
,
_n
));
pair
<
libff
::
alt_bn128_G1
,
string
>
hash_with_hint
=
obj
->
HashtoG1withHint
(
hash
);
pair
<
libff
::
alt_bn128_G1
,
string
>
hash_with_hint
=
obj
->
HashtoG1withHint
(
hash
);
string
*
xStr
=
FqToString
(
&
(
hash_with_hint
.
first
.
X
));
...
...
@@ -190,11 +191,9 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
BOOST_THROW_EXCEPTION
(
runtime_error
(
"Null yStr"
));
}
vector
<
char
>
errMsg
(
BUF_LEN
,
0
);
vector
<
char
>
errMsg
(
BUF_LEN
,
0
);
SAFE_CHAR_BUF
(
xStrArg
,
BUF_LEN
);
SAFE_CHAR_BUF
(
yStrArg
,
BUF_LEN
);
SAFE_CHAR_BUF
(
signature
,
BUF_LEN
);
SAFE_CHAR_BUF
(
xStrArg
,
BUF_LEN
);
SAFE_CHAR_BUF
(
yStrArg
,
BUF_LEN
);
SAFE_CHAR_BUF
(
signature
,
BUF_LEN
);
strncpy
(
xStrArg
,
xStr
->
c_str
(),
BUF_LEN
);
strncpy
(
yStrArg
,
yStr
->
c_str
(),
BUF_LEN
);
...
...
@@ -204,7 +203,7 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
size_t
sz
=
0
;
SAFE_UINT8_BUF
(
encryptedKey
,
BUF_LEN
);
SAFE_UINT8_BUF
(
encryptedKey
,
BUF_LEN
);
bool
result
=
hex2carray
(
_encryptedKeyHex
,
&
sz
,
encryptedKey
,
BUF_LEN
);
...
...
@@ -213,9 +212,16 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
}
int
errStatus
=
0
;
sgx_status_t
status
=
trustedBlsSignMessageAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encryptedKey
,
sz
,
xStrArg
,
yStrArg
,
signature
);
sgx_status_t
status
=
SGX_SUCCESS
;
RESTART_BEGIN
status
=
trustedBlsSignMessageAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encryptedKey
,
sz
,
xStrArg
,
yStrArg
,
signature
);
RESTART_END
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg
.
data
());
string
hint
=
BLSutils
::
ConvertToString
(
hash_with_hint
.
first
.
Y
)
+
":"
+
hash_with_hint
.
second
;
...
...
@@ -240,16 +246,22 @@ string encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key
CHECK_STATE
(
errStatus
);
CHECK_STATE
(
err_string
);
CHECK_STATE
(
_key
);
auto
keyArray
=
make_shared
<
vector
<
char
>>
(
BUF_LEN
,
0
);
auto
encryptedKey
=
make_shared
<
vector
<
uint8_t
>>
(
BUF_LEN
,
0
);
auto
keyArray
=
make_shared
<
vector
<
char
>>
(
BUF_LEN
,
0
);
auto
encryptedKey
=
make_shared
<
vector
<
uint8_t
>>
(
BUF_LEN
,
0
);
vector
<
char
>
errMsg
(
BUF_LEN
,
0
);
strncpy
(
keyArray
->
data
(),
_key
,
BUF_LEN
);
*
errStatus
=
0
;
unsigned
int
encryptedLen
=
0
;
sgx_status_t
status
=
trustedEncryptKeyAES
(
eid
,
errStatus
,
errMsg
.
data
(),
keyArray
->
data
(),
encryptedKey
->
data
(),
&
encryptedLen
);
uint64_t
encryptedLen
=
0
;
sgx_status_t
status
=
SGX_SUCCESS
;
RESTART_BEGIN
status
=
trustedEncryptKeyAES
(
eid
,
errStatus
,
errMsg
.
data
(),
keyArray
->
data
(),
encryptedKey
->
data
(),
&
encryptedLen
);
RESTART_END
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
*
errStatus
,
errMsg
.
data
());
...
...
BLSPrivateKeyShareSGX.cpp
View file @
d1af3589
...
...
@@ -26,15 +26,14 @@
#include "BLSutils.h"
#include "third_party/spdlog/spdlog.h"
#include "common.h"
#include "secure_enclave_u.h"
#include "sgxwallet_common.h"
#include "sgxwallet.h"
#include "BLSCrypto.h"
#include "ServerInit.h"
#include "common.h"
#include "SEKManager.h"
#include "BLSPrivateKeyShareSGX.h"
string
*
stringFromFq
(
libff
::
alt_bn128_Fq
*
_fq
)
{
...
...
@@ -148,9 +147,12 @@ string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
BOOST_THROW_EXCEPTION
(
invalid_argument
(
"Invalid hex encrypted key"
));
}
sgx_status_t
status
=
trustedBlsSignMessageAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encryptedKey
,
encryptedKeyHex
->
size
()
/
2
,
xStrArg
,
yStrArg
,
signature
);
sgx_status_t
status
=
SGX_SUCCESS
;
RESTART_BEGIN
status
=
trustedBlsSignMessageAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encryptedKey
,
encryptedKeyHex
->
size
()
/
2
,
xStrArg
,
yStrArg
,
signature
);
RESTART_END
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg
.
data
());
...
...
@@ -162,7 +164,7 @@ string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
}
string
hint
=
BLSutils
::
ConvertToString
(
hash_with_hint
.
first
.
Y
)
+
":"
+
hash_with_hint
.
second
;
hash_with_hint
.
second
;
string
sig
=
signature
;
...
...
@@ -183,7 +185,7 @@ shared_ptr <BLSSigShare> BLSPrivateKeyShareSGX::signWithHelperSGX(
auto
sig
=
make_shared
<
string
>
(
signature
);
shared_ptr
<
BLSSigShare
>
s
=
make_shared
<
BLSSigShare
>
(
sig
,
_signerIndex
,
requiredSigners
,
totalSigners
);
totalSigners
);
return
s
;
}
DKGCrypto.cpp
View file @
d1af3589
...
...
@@ -26,22 +26,15 @@
#include <memory>
#include "third_party/spdlog/spdlog.h"
#include "common.h"
#include "sgxwallet.h"
#include "SGXException.h"
#include "common.h"
#include "SGXWalletServer.hpp"
#include "DKGCrypto.h"
#include "SGXWalletServer.hpp"
#include "BLSCrypto.h"
#include "SEKManager.h"
#include "DKGCrypto.h"
vector
<
string
>
splitString
(
const
char
*
coeffs
,
const
char
symbol
)
{
CHECK_STATE
(
coeffs
);
...
...
@@ -71,7 +64,7 @@ string ConvertToString(T field_elem, int base = 10) {
field_elem
.
as_bigint
().
to_mpz
(
t
);
SAFE_CHAR_BUF
(
arr
,
mpz_sizeinbase
(
t
,
base
)
+
2
);
SAFE_CHAR_BUF
(
arr
,
mpz_sizeinbase
(
t
,
base
)
+
2
);
mpz_get_str
(
arr
,
base
,
t
);
mpz_clear
(
t
);
...
...
@@ -93,7 +86,7 @@ string convertHexToDec(const string &hex_str) {
return
ret
;
}
SAFE_CHAR_BUF
(
arr
,
mpz_sizeinbase
(
dec
,
10
)
+
2
);
SAFE_CHAR_BUF
(
arr
,
mpz_sizeinbase
(
dec
,
10
)
+
2
);
mpz_get_str
(
arr
,
10
,
dec
);
ret
=
arr
;
}
catch
(
exception
&
e
)
{
...
...
@@ -135,13 +128,18 @@ string convertG2ToString(const libff::alt_bn128_G2 &elem, int base, const string
string
gen_dkg_poly
(
int
_t
)
{
vector
<
char
>
errMsg
(
BUF_LEN
,
0
);
int
errStatus
=
0
;
uint
32
_t
enc_len
=
0
;
uint
64
_t
enc_len
=
0
;
vector
<
uint8_t
>
encrypted_dkg_secret
(
BUF_LEN
,
0
);
sgx_status_t
status
=
SGX_SUCCESS
;
RESTART_BEGIN
status
=
trustedGenDkgSecretAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encrypted_dkg_secret
.
data
(),
&
enc_len
,
_t
);
RESTART_END
sgx_status_t
status
=
trustedGenDkgSecretAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encrypted_dkg_secret
.
data
(),
&
enc_len
,
_t
);
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg
.
data
());
uint64_t
length
=
enc_len
;;
...
...
@@ -173,9 +171,14 @@ vector <vector<string>> get_verif_vect(const char *encryptedPolyHex, int t, int
}
sgx_status_t
status
=
SGX_SUCCESS
;
RESTART_BEGIN
status
=
trustedGetPublicSharesAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encrDKGPoly
.
data
(),
encLen
,
pubShares
.
data
(),
t
,
n
);
RESTART_END
sgx_status_t
status
=
trustedGetPublicSharesAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encrDKGPoly
.
data
(),
encLen
,
pubShares
.
data
(),
t
,
n
);
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg
.
data
());
vector
<
string
>
g2Strings
=
splitString
(
pubShares
.
data
(),
','
);
...
...
@@ -190,8 +193,8 @@ vector <vector<string>> get_verif_vect(const char *encryptedPolyHex, int t, int
string
getSecretShares
(
const
string
&
_polyName
,
const
char
*
_encryptedPolyHex
,
const
vector
<
string
>
&
_publicKeys
,
int
_t
,
int
_n
)
{
int
_t
,
int
_n
)
{
CHECK_STATE
(
_encryptedPolyHex
);
...
...
@@ -202,19 +205,23 @@ getSecretShares(const string &_polyName, const char *_encryptedPolyHex, const ve
uint64_t
encLen
=
0
;
if
(
!
hex2carray
(
_encryptedPolyHex
,
&
encLen
,
encrDKGPoly
.
data
(),
BUF_LEN
))
{
throw
SGXException
(
INVALID_HEX
,
"Invalid encryptedPolyHex"
);
}
sgx_status_t
status
=
trustedSetEncryptedDkgPolyAES
(
eid
,
&
errStatus
,
errMsg1
.
data
(),
encrDKGPoly
.
data
(),
encLen
);
sgx_status_t
status
=
SGX_SUCCESS
;
READ_LOCK
(
initMutex
);
status
=
trustedSetEncryptedDkgPolyAES
(
eid
,
&
errStatus
,
errMsg1
.
data
(),
encrDKGPoly
.
data
(),
encLen
);
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg1
.
data
());
string
result
;
for
(
int
i
=
0
;
i
<
_n
;
i
++
)
{
vector
<
uint8_t
>
encryptedSkey
(
BUF_LEN
,
0
);
uint
32
_t
decLen
;
uint
64
_t
decLen
;
vector
<
char
>
currentShare
(
193
,
0
);
vector
<
char
>
sShareG2
(
320
,
0
);
...
...
@@ -226,8 +233,11 @@ getSecretShares(const string &_polyName, const char *_encryptedPolyHex, const ve
spdlog
::
debug
(
"pubKeyB is {}"
,
pub_keyB
);
sgx_status_t
status
=
trustedGetEncryptedSecretShareAES
(
eid
,
&
errStatus
,
errMsg1
.
data
(),
encryptedSkey
.
data
(),
&
decLen
,
currentShare
.
data
(),
sShareG2
.
data
(),
pubKeyB
.
data
(),
_t
,
_n
,
i
+
1
);
sgx_status_t
status
=
SGX_SUCCESS
;
status
=
trustedGetEncryptedSecretShareAES
(
eid
,
&
errStatus
,
errMsg1
.
data
(),
encryptedSkey
.
data
(),
&
decLen
,
currentShare
.
data
(),
sShareG2
.
data
(),
pubKeyB
.
data
(),
_t
,
_n
,
i
+
1
);
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg1
.
data
());
spdlog
::
debug
(
"cur_share is {}"
,
currentShare
.
data
());
...
...
@@ -271,11 +281,16 @@ verifyShares(const char *publicShares, const char *encr_sshare, const char *encr
throw
SGXException
(
INVALID_HEX
,
"Invalid encryptedPolyHex"
);
}
SAFE_CHAR_BUF
(
pshares
,
8193
);
SAFE_CHAR_BUF
(
pshares
,
8193
);
strncpy
(
pshares
,
publicShares
,
strlen
(
publicShares
));
sgx_status_t
status
=
trustedDkgVerifyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
pshares
,
encr_sshare
,
encr_key
,
decKeyLen
,
t
,
ind
,
&
result
);
sgx_status_t
status
=
SGX_SUCCESS
;
RESTART_BEGIN
status
=
trustedDkgVerifyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
pshares
,
encr_sshare
,
encr_key
,
decKeyLen
,
t
,
ind
,
&
result
);
RESTART_END
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg
.
data
());
if
(
result
==
2
)
{
...
...
@@ -290,24 +305,26 @@ bool createBLSShare(const string &blsKeyName, const char *s_shares, const char *
CHECK_STATE
(
s_shares
);
CHECK_STATE
(
encryptedKeyHex
);
vector
<
char
>
errMsg
(
BUF_LEN
,
0
);
vector
<
char
>
errMsg
(
BUF_LEN
,
0
);
int
errStatus
=
0
;
uint64_t
decKeyLen
;
SAFE_UINT8_BUF
(
encr_bls_key
,
BUF_LEN
);
SAFE_UINT8_BUF
(
encr_key
,
BUF_LEN
);
uint64_t
decKeyLen
;
SAFE_UINT8_BUF
(
encr_bls_key
,
BUF_LEN
);
SAFE_UINT8_BUF
(
encr_key
,
BUF_LEN
);
if
(
!
hex2carray
(
encryptedKeyHex
,
&
decKeyLen
,
encr_key
,
BUF_LEN
))
{
throw
SGXException
(
INVALID_HEX
,
"Invalid encryptedKeyHex"
);
}
uint
32
_t
enc_bls_len
=
0
;
uint
64
_t
enc_bls_len
=
0
;
sgx_status_t
status
=
trustedCreateBlsKeyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
s_shares
,
encr_key
,
decKeyLen
,
encr_bls_key
,
&
enc_bls_len
);
sgx_status_t
status
=
SGX_SUCCESS
;
RESTART_BEGIN
status
=
trustedCreateBlsKeyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
s_shares
,
encr_key
,
decKeyLen
,
encr_bls_key
,
&
enc_bls_len
);
RESTART_END
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg
.
data
());
SAFE_CHAR_BUF
(
hexBLSKey
,
2
*
BUF_LEN
)
SAFE_CHAR_BUF
(
hexBLSKey
,
2
*
BUF_LEN
)
carray2Hex
(
encr_bls_key
,
enc_bls_len
,
hexBLSKey
,
2
*
BUF_LEN
);
...
...
@@ -331,9 +348,15 @@ vector <string> getBLSPubKey(const char *encryptedKeyHex) {
throw
SGXException
(
INVALID_HEX
,
"Invalid encryptedKeyHex"
);
}
SAFE_CHAR_BUF
(
pubKey
,
320
)
SAFE_CHAR_BUF
(
pubKey
,
320
)
sgx_status_t
status
=
SGX_SUCCESS
;
RESTART_BEGIN
status
=
trustedGetBlsPubKeyAES
(
eid
,
&
errStatus
,
errMsg1
.
data
(),
encrKey
,
decKeyLen
,
pubKey
);
RESTART_END
sgx_status_t
status
=
trustedGetBlsPubKeyAES
(
eid
,
&
errStatus
,
errMsg1
.
data
(),
encrKey
,
decKeyLen
,
pubKey
);
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg1
.
data
());
vector
<
string
>
pubKeyVect
=
splitString
(
pubKey
,
':'
);
...
...
@@ -405,8 +428,7 @@ string decryptDHKey(const string &polyName, int ind) {
vector
<
char
>
hexEncrKey
(
2
*
BUF_LEN
,
0
);
uint64_t
dhEncLen
=
0
;
SAFE_UINT8_BUF
(
encryptedDHKey
,
BUF_LEN
);
uint64_t
dhEncLen
=
0
;
SAFE_UINT8_BUF
(
encryptedDHKey
,
BUF_LEN
);
if
(
!
hex2carray
(
hexEncrKeyPtr
->
c_str
(),
&
dhEncLen
,
encryptedDHKey
,
BUF_LEN
))
{
throw
SGXException
(
INVALID_HEX
,
"Invalid hexEncrKey"
);
}
...
...
@@ -415,7 +437,11 @@ string decryptDHKey(const string &polyName, int ind) {
SAFE_CHAR_BUF
(
DHKey
,
ECDSA_SKEY_LEN
);
sgx_status_t
status
=
trustedDecryptKeyAES
(
eid
,
&
errStatus
,
errMsg1
.
data
(),
encryptedDHKey
,
dhEncLen
,
DHKey
);
sgx_status_t
status
=
SGX_SUCCESS
;
RESTART_BEGIN
status
=
trustedDecryptKeyAES
(
eid
,
&
errStatus
,
errMsg1
.
data
(),
encryptedDHKey
,
dhEncLen
,
DHKey
);
RESTART_END
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg1
.
data
());
return
DHKey
;
...
...
ECDSACrypto.cpp
View file @
d1af3589
...
...
@@ -38,6 +38,7 @@
#include "BLSCrypto.h"
#include "SEKManager.h"
#include "ECDSACrypto.h"
void
fillRandomBuffer
(
vector
<
unsigned
char
>
&
_buffer
)
{
...
...
@@ -54,11 +55,15 @@ vector <string> genECDSAKey() {
vector
<
char
>
pub_key_x
(
BUF_LEN
,
0
);
vector
<
char
>
pub_key_y
(
BUF_LEN
,
0
);
uint32_t
enc_len
=
0
;
uint64_t
enc_len
=
0
;
sgx_status_t
status
=
SGX_SUCCESS
;
sgx_status_t
status
=
trustedGenerateEcdsaKeyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encr_pr_key
.
data
(),
&
enc_len
,
pub_key_x
.
data
(),
pub_key_y
.
data
());
RESTART_BEGIN
status
=
trustedGenerateEcdsaKeyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encr_pr_key
.
data
(),
&
enc_len
,
pub_key_x
.
data
(),
pub_key_y
.
data
());
RESTART_END
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg
.
data
());
...
...
@@ -99,8 +104,12 @@ string getECDSAPubKey(const std::string& _encryptedKeyHex) {
throw
SGXException
(
INVALID_HEX
,
"Invalid encryptedKeyHex"
);
}
sgx_status_t
status
=
trustedGetPublicEcdsaKeyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encrPrKey
.
data
(),
enc_len
,
pubKeyX
.
data
(),
pubKeyY
.
data
());
sgx_status_t
status
=
SGX_SUCCESS
;
RESTART_BEGIN
status
=
trustedGetPublicEcdsaKeyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encrPrKey
.
data
(),
enc_len
,
pubKeyX
.
data
(),
pubKeyY
.
data
());
RESTART_END
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg
.
data
())
...
...
@@ -184,10 +193,14 @@ vector <string> ecdsaSignHash(const std::string& encryptedKeyHex, const char *ha
throw
SGXException
(
INVALID_HEX
,
"Invalid encryptedKeyHex"
);
}
sgx_status_t
status
=
trustedEcdsaSignAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encryptedKey
.
data
(),
decLen
,
hashHex
,
signatureR
.
data
(),
signatureS
.
data
(),
&
signatureV
,
base
);
sgx_status_t
status
=
SGX_SUCCESS
;
RESTART_BEGIN
status
=
trustedEcdsaSignAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encryptedKey
.
data
(),
decLen
,
hashHex
,
signatureR
.
data
(),
signatureS
.
data
(),
&
signatureV
,
base
);
RESTART_END
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg
.
data
());
...
...
SEKManager.cpp
View file @
d1af3589
...
...
@@ -21,21 +21,25 @@
@date 2020
*/
#include "SEKManager.h"
#include "SGXException.h"
#include "BLSCrypto.h"
#include "LevelDB.h"
#include <fstream>
#include <iostream>
#include <algorithm>
#include "third_party/spdlog/spdlog.h"
#include "sgxwallet_common.h"
#include "common.h"
#include "sgxwallet.h"
#include "SGXException.h"
#include "BLSCrypto.h"
#include "LevelDB.h"
#include "ServerDataChecker.h"
#include "third_party/spdlog/spdlog.h"
#include "ServerInit.h"
#include "SEKManager.h"
using
namespace
std
;
...
...
@@ -52,13 +56,18 @@ bool case_insensitive_match(string s1, string s2) {
void
create_test_key
()
{
int
errStatus
=
0
;
vector
<
char
>
errMsg
(
1024
,
0
);
uint
32
_t
enc_len
;
uint
64
_t
enc_len
;
SAFE_UINT8_BUF
(
encrypted_key
,
BUF_LEN
);
string
key
=
TEST_VALUE
;
sgx_status_t
status
=
trustedEncryptKeyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
key
.
c_str
(),
encrypted_key
,
&
enc_len
);
sgx_status_t
status
=
SGX_SUCCESS
;
{
READ_LOCK
(
initMutex
);
status
=
trustedEncryptKeyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
key
.
c_str
(),
encrypted_key
,
&
enc_len
);
}
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
errStatus
,
errMsg
.
data
());
...
...
@@ -86,7 +95,12 @@ void validate_SEK() {
exit
(
-
1
);
}
sgx_status_t
status
=
trustedDecryptKeyAES
(
eid
,
&
err_status
,
errMsg
.
data
(),
encr_test_key
.
data
(),
len
,
decr_key
.
data
());
sgx_status_t
status
=
SGX_SUCCESS
;
{
READ_LOCK
(
initMutex
);
status
=
trustedDecryptKeyAES
(
eid
,
&
err_status
,
errMsg
.
data
(),
encr_test_key
.
data
(),
len
,
decr_key
.
data
());
}
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
err_status
,
errMsg
.
data
());
...
...
@@ -109,15 +123,21 @@ shared_ptr <vector<uint8_t>> check_and_set_SEK(const string &SEK) {
auto
encrypted_SEK
=
make_shared
<
vector
<
uint8_t
>>
(
BUF_LEN
,
0
);
uint
32
_t
l
=
0
;
uint
64
_t
l
=
0
;
sgx_status_t
status
=
trustedSetSEK_backup
(
eid
,
&
err_status
,
errMsg
.
data
(),
encrypted_SEK
->
data
(),
&
l
,
SEK
.
c_str
());
sgx_status_t
status
=
SGX_SUCCESS
;
{
READ_LOCK
(
initMutex
);
status
=
trustedSetSEK_backup
(
eid
,
&
err_status
,
errMsg
.
data
(),
encrypted_SEK
->
data
(),
&
l
,
SEK
.
c_str
());
}
encrypted_SEK
->
resize
(
l
);
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
err_status
,
errMsg
.
data
());
encrypted_SEK
->
resize
(
l
);
validate_SEK
();
return
encrypted_SEK
;
...
...
@@ -127,13 +147,18 @@ void gen_SEK() {
vector
<
char
>
errMsg
(
1024
,
0
);
int
err_status
=
0
;
vector
<
uint8_t
>
encrypted_SEK
(
1024
,
0
);
uint
32
_t
enc_len
=
0
;
uint
64
_t
enc_len
=
0
;
SAFE_CHAR_BUF
(
SEK
,
65
);
spdlog
::
info
(
"Generating backup key. Will be stored in backup_key.txt ... "
);
sgx_status_t
status
=
trustedGenerateSEK
(
eid
,
&
err_status
,
errMsg
.
data
(),
encrypted_SEK
.
data
(),
&
enc_len
,
SEK
);
sgx_status_t
status
=
SGX_SUCCESS
;
{
status
=
trustedGenerateSEK
(
eid
,
&
err_status
,
errMsg
.
data
(),
encrypted_SEK
.
data
(),
&
enc_len
,
SEK
);
}
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
err_status
,
errMsg
.
data
());
...
...
@@ -184,6 +209,13 @@ void gen_SEK() {
}
void
reinitEnclave
()
{
WRITE_LOCK
(
initMutex
);
initEnclave
();
shared_ptr
<
string
>
encrypted_SEK_ptr
=
LevelDB
::
getLevelDb
()
->
readString
(
"SEK"
);
setSEK
(
encrypted_SEK_ptr
);
}
void
setSEK
(
shared_ptr
<
string
>
hex_encrypted_SEK
)
{
CHECK_STATE
(
hex_encrypted_SEK
);
...
...
@@ -200,7 +232,10 @@ void setSEK(shared_ptr <string> hex_encrypted_SEK) {
throw
SGXException
(
INVALID_HEX
,
"Invalid encrypted SEK Hex"
);
}
sgx_status_t
status
=
trustedSetSEK
(
eid
,
&
err_status
,
errMsg
.
data
(),
encrypted_SEK
);
sgx_status_t
status
=
SGX_SUCCESS
;
{
status
=
trustedSetSEK
(
eid
,
&
err_status
,
errMsg
.
data
(),
encrypted_SEK
);
}
HANDLE_TRUSTED_FUNCTION_ERROR
(
status
,
err_status
,
errMsg
.
data
());
...
...
SEKManager.h
View file @
d1af3589
...
...
@@ -45,4 +45,11 @@ EXTERNC void enter_SEK();
EXTERNC
void
initSEK
();
EXTERNC
void
setSEK
();
EXTERNC
void
reinitEnclave
();
#endif //SGXD_SEKMANAGER_H
SGXWalletServer.cpp
View file @
d1af3589
...
...
@@ -54,6 +54,9 @@
using
namespace
std
;
std
::
shared_timed_mutex
initMutex
;
uint64_t
initTime
;
void
setFullOptions
(
uint64_t
_logLevel
,
int
_useHTTPS
,
int
_autoconfirm
,
int
_enterBackupKey
)
{
spdlog
::
info
(
"Entering {}"
,
__FUNCTION__
);
...
...
@@ -563,7 +566,7 @@ Json::Value SGXWalletServer::calculateAllBLSPublicKeysImpl(const Json::Value& pu
vector
<
string
>
public_keys
=
calculateAllBlsPublicKeys
(
public_shares
);
if
(
public_keys
.
size
()
!=
n
)
{
if
(
public_keys
.
size
()
!=
(
uint64_t
)
n
)
{
throw
SGXException
(
UNKNOWN_ERROR
,
""
);
}
...
...
ServerInit.cpp
View file @
d1af3589
...
...
@@ -57,6 +57,8 @@
#include "SGXException.h"
#include "SGXWalletServer.hpp"
uint32_t
enclaveLogLevel
=
0
;
void
initUserSpace
()
{
libff
::
inhibit_profiling_counters
=
true
;
...
...
@@ -66,9 +68,8 @@ void initUserSpace() {
LevelDB
::
initDataFolderAndDBs
();
}
void
initEnclave
(
uint32_t
_logLevel
)
{
eid
=
0
;
updated
=
0
;
void
initEnclave
()
{
#ifndef SGX_HW_SIM
unsigned
long
support
;
...
...
@@ -81,22 +82,38 @@ void initEnclave(uint32_t _logLevel) {
spdlog
::
info
(
"SGX_DEBUG_FLAG = {}"
,
SGX_DEBUG_FLAG
);
sgx_status_t
status
=
sgx_create_enclave_search
(
ENCLAVE_NAME
,
SGX_DEBUG_FLAG
,
&
token
,
&
updated
,
&
eid
,
0
);
sgx_status_t
status
=
SGX_SUCCESS
;
if
(
status
!=
SGX_SUCCESS
)
{
if
(
status
==
SGX_ERROR_ENCLAVE_FILE_ACCESS
)
{
spdlog
::
error
(
"sgx_create_enclave: {}: file not found"
,
ENCLAVE_NAME
);
spdlog
::
error
(
"Did you forget to set LD_LIBRARY_PATH?"
);
}
else
{
spdlog
::
error
(
"sgx_create_enclave_search failed {} {}"
,
ENCLAVE_NAME
,
status
);
{
WRITE_LOCK
(
initMutex
);
if
(
eid
!=
0
)
{
if
(
sgx_destroy_enclave
(
eid
)
!=
SGX_SUCCESS
)
{
spdlog
::
error
(
"Could not destroy enclave"
);
return
;
}
}
exit
(
1
);
}
spdlog
::
info
(
"Enclave created and started successfully"
);
eid
=
0
;
updated
=
0
;
status
=
trustedEnclaveInit
(
eid
,
_logLevel
);
status
=
sgx_create_enclave_search
(
ENCLAVE_NAME
,
SGX_DEBUG_FLAG
,
&
token
,
&
updated
,
&
eid
,
0
);
if
(
status
!=
SGX_SUCCESS
)
{
if
(
status
==
SGX_ERROR_ENCLAVE_FILE_ACCESS
)
{
spdlog
::
error
(
"sgx_create_enclave: {}: file not found"
,
ENCLAVE_NAME
);
spdlog
::
error
(
"Did you forget to set LD_LIBRARY_PATH?"
);
}
else
{
spdlog
::
error
(
"sgx_create_enclave_search failed {} {}"
,
ENCLAVE_NAME
,
status
);
}
exit
(
1
);
}
spdlog
::
info
(
"Enclave created and started successfully"
);
status
=
trustedEnclaveInit
(
eid
,
enclaveLogLevel
);
}
if
(
status
!=
SGX_SUCCESS
)
{
spdlog
::
error
(
"trustedEnclaveInit failed: {}"
,
status
);
...
...
@@ -107,10 +124,13 @@ void initEnclave(uint32_t _logLevel) {
}
void
initAll
(
uint32_t
_logLevel
,
bool
_checkCert
,
bool
_autoSign
)
{
static
atomic
<
bool
>
sgxServerInited
(
false
);
static
mutex
initMutex
;
enclaveLogLevel
=
_logLevel
;
lock_guard
<
mutex
>
lock
(
initMutex
);
...
...
@@ -123,7 +143,8 @@ void initAll(uint32_t _logLevel, bool _checkCert, bool _autoSign) {
CHECK_STATE
(
sgxServerInited
!=
1
)
sgxServerInited
=
1
;
initEnclave
(
_logLevel
);
initEnclave
();
initUserSpace
();
initSEK
();
...
...
@@ -146,6 +167,6 @@ void initAll(uint32_t _logLevel, bool _checkCert, bool _autoSign) {
exception_ptr
p
=
current_exception
();
printf
(
"Exception %s
\n
"
,
p
.
__cxa_exception_type
()
->
name
());
spdlog
::
error
(
"Unknown exception"
);
exit
(
-
1
);
exit
(
-
1
);
}
};
ServerInit.h
View file @
d1af3589
...
...
@@ -36,7 +36,7 @@ EXTERNC void initAll(uint32_t _logLevel, bool _checkCert, bool _autoSign);
EXTERNC
void
initUserSpace
();
EXTERNC
void
initEnclave
(
uint32_t
_logLevel
);
EXTERNC
void
initEnclave
();
...
...
VERSION
View file @
d1af3589
1.58.1
\ No newline at end of file
1.58.2
\ No newline at end of file
common.h
View file @
d1af3589
...
...
@@ -96,5 +96,20 @@ BOOST_THROW_EXCEPTION(runtime_error(__ERR_STRING__)); \
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
#define SAFE_UINT8_BUF(__X__, __Y__) ;uint8_t __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
#include <shared_mutex>
extern
std
::
shared_timed_mutex
initMutex
;
extern
uint64_t
initTime
;
#if SGX_MODE == SIM
#define ENCLAVE_RESTART_PERIOD_S 5
#else
#define ENCLAVE_RESTART_PERIOD_S 60 * 10
#endif
#define READ_LOCK(__X__) std::shared_lock<std::shared_timed_mutex> __LOCK__(__X__);
#define WRITE_LOCK(__X__) std::unique_lock<std::shared_timed_mutex> __LOCK__(__X__);
#endif //SGXWALLET_COMMON_H
secure_enclave/AESUtils.c
View file @
d1af3589
...
...
@@ -27,12 +27,24 @@
#include "stdlib.h"
#include <string.h>
#include "AESUtils.h"
sgx_aes_gcm_128bit_key_t
AES_key
;
sgx_aes_gcm_128bit_key_t
AES_DH_key
;
int
AES_encrypt
(
char
*
message
,
uint8_t
*
encr_message
,
uint64_t
encrLen
)
{
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
int
AES_encrypt
(
char
*
message
,
uint8_t
*
encr_message
,
uint64_t
encrBufLen
,
unsigned
char
type
,
unsigned
char
exportable
,
uint64_t
*
resultLen
)
{
if
(
!
type
)
{
LOG_ERROR
(
"Null type in AES_encrypt"
);
return
-
1
;
}
if
(
!
message
)
{
LOG_ERROR
(
"Null message in AES_encrypt"
);
...
...
@@ -46,23 +58,36 @@ int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen) {
uint64_t
len
=
strlen
(
message
)
+
1
;
if
(
len
+
SGX_AESGCM_MAC_SIZE
+
SGX_AESGCM_IV_SIZE
>
encr
Len
)
{
if
(
2
+
len
+
SGX_AESGCM_MAC_SIZE
+
SGX_AESGCM_IV_SIZE
>
encrBuf
Len
)
{
LOG_ERROR
(
"Output buffer too small"
);
return
-
3
;
}
SAFE_CHAR_BUF
(
fullMessage
,
len
+
2
);
fullMessage
[
0
]
=
type
;
fullMessage
[
1
]
=
exportable
;
strncpy
(
fullMessage
+
2
,
message
,
len
);
len
=
len
+
2
;
message
=
fullMessage
;
sgx_read_rand
(
encr_message
+
SGX_AESGCM_MAC_SIZE
,
SGX_AESGCM_IV_SIZE
);
sgx_status_t
status
=
sgx_rijndael128GCM_encrypt
(
&
AES_key
,
(
uint8_t
*
)
message
,
strlen
(
message
)
,
sgx_status_t
status
=
sgx_rijndael128GCM_encrypt
(
&
AES_key
,
(
uint8_t
*
)
message
,
len
,
encr_message
+
SGX_AESGCM_MAC_SIZE
+
SGX_AESGCM_IV_SIZE
,
encr_message
+
SGX_AESGCM_MAC_SIZE
,
SGX_AESGCM_IV_SIZE
,
NULL
,
0
,
(
sgx_aes_gcm_128bit_tag_t
*
)
encr_message
);
*
resultLen
=
len
+
SGX_AESGCM_MAC_SIZE
+
SGX_AESGCM_IV_SIZE
;
return
status
;
}
int
AES_decrypt
(
uint8_t
*
encr_message
,
uint64_t
length
,
char
*
message
,
uint64_t
msgLen
)
{
int
AES_decrypt
(
uint8_t
*
encr_message
,
uint64_t
length
,
char
*
message
,
uint64_t
msgLen
,
uint8_t
*
type
,
uint8_t
*
exportable
){
if
(
!
message
)
{
LOG_ERROR
(
"Null message in AES_encrypt"
);
...
...
@@ -74,6 +99,16 @@ int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message, uint64_t
return
-
2
;
}
if
(
!
type
)
{
LOG_ERROR
(
"Null type in AES_encrypt"
);
return
-
3
;
}
if
(
!
encr_message
)
{
LOG_ERROR
(
"Null exportable in AES_encrypt"
);
return
-
4
;
}
if
(
length
<
SGX_AESGCM_MAC_SIZE
+
SGX_AESGCM_IV_SIZE
)
{
LOG_ERROR
(
"length < SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE"
);
...
...
@@ -96,86 +131,11 @@ int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message, uint64_t
NULL
,
0
,
(
sgx_aes_gcm_128bit_tag_t
*
)
encr_message
);
return
status
;
}
int
AES_encrypt_DH
(
char
*
message
,
uint8_t
*
encr_message
,
uint64_t
encrLen
)
{
if
(
!
message
)
{
LOG_ERROR
(
"Null message in AES_encrypt_DH"
);
return
-
1
;
}
if
(
!
encr_message
)
{
LOG_ERROR
(
"Null encr message in AES_encrypt_DH"
);
return
-
2
;
}
uint64_t
len
=
strlen
(
message
)
+
1
;
if
(
len
+
SGX_AESGCM_MAC_SIZE
+
SGX_AESGCM_IV_SIZE
>
encrLen
)
{
LOG_ERROR
(
"Output buffer too small"
);
return
-
3
;
}
sgx_read_rand
(
encr_message
+
SGX_AESGCM_MAC_SIZE
,
SGX_AESGCM_IV_SIZE
);
sgx_status_t
status
=
sgx_rijndael128GCM_encrypt
(
&
AES_DH_key
,
(
uint8_t
*
)
message
,
strlen
(
message
),
encr_message
+
SGX_AESGCM_MAC_SIZE
+
SGX_AESGCM_IV_SIZE
,
encr_message
+
SGX_AESGCM_MAC_SIZE
,
SGX_AESGCM_IV_SIZE
,
NULL
,
0
,
(
sgx_aes_gcm_128bit_tag_t
*
)
encr_message
);
return
status
;
}
int
AES_decrypt_DH
(
uint8_t
*
encr_message
,
uint64_t
length
,
char
*
message
,
uint64_t
msgLen
)
{
if
(
!
message
)
{
LOG_ERROR
(
"Null message in AES_encrypt_DH"
);
return
-
1
;
}
if
(
!
encr_message
)
{
LOG_ERROR
(
"Null encr message in AES_encrypt_DH"
);
return
-
2
;
}
if
(
length
<
SGX_AESGCM_MAC_SIZE
+
SGX_AESGCM_IV_SIZE
)
{
LOG_ERROR
(
"length < SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE"
);
return
-
1
;
}
uint64_t
len
=
length
-
SGX_AESGCM_MAC_SIZE
-
SGX_AESGCM_IV_SIZE
;
if
(
msgLen
<
len
)
{
LOG_ERROR
(
"Output buffer not large enough"
);
return
-
2
;
}
sgx_status_t
status
=
sgx_rijndael128GCM_decrypt
(
&
AES_DH_key
,
encr_message
+
SGX_AESGCM_MAC_SIZE
+
SGX_AESGCM_IV_SIZE
,
len
,
(
unsigned
char
*
)
message
,
encr_message
+
SGX_AESGCM_MAC_SIZE
,
SGX_AESGCM_IV_SIZE
,
NULL
,
0
,
(
sgx_aes_gcm_128bit_tag_t
*
)
encr_message
);
return
status
;
}
*
type
=
message
[
0
];
*
exportable
=
message
[
1
];
for
(
int
i
=
2
;
i
<
strlen
(
message
)
+
1
;
i
++
)
{
message
[
i
-
2
]
=
message
[
i
];
}
void
derive_DH_Key
()
{
memcpy
(
AES_DH_key
,
AES_key
,
SGX_AESGCM_KEY_SIZE
);
/*AES_DH_key[1] = 1;AES_DH_key[2] = 2;*/
return
status
;
}
secure_enclave/AESUtils.h
View file @
d1af3589
...
...
@@ -25,15 +25,21 @@
#define SGXD_AESUTILS_H
extern
sgx_aes_gcm_128bit_key_t
AES_key
;
extern
sgx_aes_gcm_128bit_key_t
AES_DH_key
;
int
AES_encrypt
(
char
*
message
,
uint8_t
*
encr_message
,
uint64_t
encrLen
);
int
AES_decrypt
(
uint8_t
*
encr_message
,
uint64_t
length
,
char
*
message
,
uint64_t
msgLen
)
;
int
AES_encrypt
(
char
*
message
,
uint8_t
*
encr_message
,
uint64_t
encrLen
,
unsigned
char
type
,
unsigned
char
exportable
,
uint64_t
*
resultLen
);
int
AES_decrypt
(
uint8_t
*
encr_message
,
uint64_t
length
,
char
*
message
,
uint64_t
msgLen
,
uint8_t
*
type
,
uint8_t
*
exportable
)
;
int
AES_encrypt_DH
(
char
*
message
,
uint8_t
*
encr_message
,
uint64_t
encrLen
);
int
AES_decrypt_DH
(
uint8_t
*
encr_message
,
uint64_t
length
,
char
*
message
,
uint64_t
msgLen
)
;
void
derive_DH_Key
();
#define ECDSA '1'
#define BLS '2'
#define DKG '3'
#define EXPORTABLE '1'
#define NON_EXPORTABLE '2'
#endif //SGXD_AESUTILS_H
secure_enclave/secure_enclave.c
View file @
d1af3589
This diff is collapsed.
Click to expand it.
secure_enclave/secure_enclave.edl
View file @
d1af3589
...
...
@@ -11,14 +11,14 @@ enclave {
trusted {
include "sgx_tgmp.h"
public void trustedEnclaveInit(uint
32
_t _logLevel);
public void trustedEnclaveInit(uint
64
_t _logLevel);
public void trustedGenerateSEK(
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char *err_string,
[out, count = SMALL_BUF_SIZE] uint8_t *encrypted_SEK,
[out] uint
32
_t *enc_len,
[out] uint
64
_t *enc_len,
[out, count = 65] char* hex_SEK);
public void trustedSetSEK(
...
...
@@ -30,14 +30,14 @@ enclave {
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char *err_string,
[out, count = SMALL_BUF_SIZE] uint8_t *encrypted_SEK,
[out] uint
32
_t *enc_len,
[out] uint
64
_t *enc_len,
[in, string] const char* SEK_hex);
public void trustedGenerateEcdsaKeyAES (
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char* err_string,
[out, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
[out] uint
32
_t *enc_len,
[out] uint
64
_t *enc_len,
[out, count = SMALL_BUF_SIZE] char * pub_key_x,
[out, count = SMALL_BUF_SIZE] char * pub_key_y);
...
...
@@ -45,7 +45,7 @@ enclave {
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char* err_string,
[in, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
uint
32
_t dec_len,
uint
64
_t dec_len,
[out, count = SMALL_BUF_SIZE] char * pub_key_x,
[out, count = SMALL_BUF_SIZE] char * pub_key_y);
...
...
@@ -53,7 +53,7 @@ enclave {
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char* err_string,
[in, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
uint
32
_t enc_len,
uint
64
_t enc_len,
[in, string] const char* hash,
[out, count = SMALL_BUF_SIZE] char* sig_r,
[out, count = SMALL_BUF_SIZE] char* sig_s,
...
...
@@ -65,26 +65,26 @@ enclave {
[out, count = SMALL_BUF_SIZE] char* err_string,
[in, count = SMALL_BUF_SIZE] const char* key,
[out, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
[out] uint
32
_t *enc_len);
[out] uint
64
_t *enc_len);
public void trustedDecryptKeyAES (
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char* err_string,
[in, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
uint
32
_t enc_len,
uint
64
_t enc_len,
[out, count = SMALL_BUF_SIZE] char* key );
public void trustedGenDkgSecretAES (
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char* err_string,
[out, count = 3072] uint8_t* encrypted_dkg_secret,
[out] uint
32
_t * enc_len, size_t _t);
[out] uint
64
_t * enc_len, size_t _t);
public void trustedDecryptDkgSecretAES (
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char* err_string,
[in, count = 3050] uint8_t* encrypted_dkg_secret,
uint
32
_t enc_len,
uint
64
_t enc_len,
[out, count = 3072] uint8_t* decrypted_dkg_secret
);
...
...
@@ -92,13 +92,13 @@ enclave {
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char* err_string,
[in, count = 3050] uint8_t* encrypted_poly,
uint
32
_t enc_len);
uint
64
_t enc_len);
public void trustedGetEncryptedSecretShareAES(
[out]int *errStatus,
[out, count = SMALL_BUF_SIZE] char *err_string,
[out, count = SMALL_BUF_SIZE] uint8_t *encrypted_skey,
[out] uint
32
_t* dec_len,
[out] uint
64
_t* dec_len,
[out, count = 193] char* result_str,
[out, count = 320] char* s_shareG2,
[in, string] char* pub_keyB,
...
...
@@ -110,7 +110,7 @@ enclave {
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char* err_string,
[in, count = 3050] uint8_t* encrypted_dkg_secret,
uint
32
_t enc_len,
uint
64
_t enc_len,
[out, count = 10000] char* public_shares,
unsigned _t,
unsigned _n);
...
...
@@ -133,13 +133,13 @@ enclave {
[in, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
uint64_t key_len,
[out, count = SMALL_BUF_SIZE] uint8_t * encr_bls_key,
[out] uint
32
_t *enc_bls_key_len);
[out] uint
64
_t *enc_bls_key_len);
public void trustedBlsSignMessageAES (
[out] int *errStatus,
[out, count = TINY_BUF_SIZE] char* err_string,
[in, count = TINY_BUF_SIZE] uint8_t* encrypted_key,
uint
32
_t enc_len,
uint
64
_t enc_len,
[in, string] char* hashX ,
[in, string] char* hashY,
[out, count = SMALL_BUF_SIZE] char* signature);
...
...
sgxwallet_common.h
View file @
d1af3589
...
...
@@ -109,4 +109,23 @@ extern bool autoconfirm;
#define TEST_VALUE "1234567890"
#define RESTART_BEGIN \
int __ATTEMPTS__ = 0; \
do {\
__ATTEMPTS__++; \
{\
READ_LOCK(initMutex);
#define RESTART_END \
} \
if (status != SGX_SUCCESS) { \
spdlog::error(__FUNCTION__); \
spdlog::error("Restarting sgx ..."); \
reinitEnclave(); \
} \
} while (status != SGX_SUCCESS && __ATTEMPTS__ < 2);
#endif //SGXWALLET_SGXWALLET_COMMON_H
testw.cpp
View file @
d1af3589
...
...
@@ -127,7 +127,7 @@ TEST_CASE_METHOD(TestFixture, "ECDSA AES keygen and signature test", "[ecdsa-aes
vector
<
char
>
pubKeyX
(
BUF_LEN
,
0
);
vector
<
char
>
pubKeyY
(
BUF_LEN
,
0
);
uint
32
_t
encLen
=
0
;
uint
64
_t
encLen
=
0
;
PRINT_SRC_LINE
auto
status
=
trustedGenerateEcdsaKeyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encrPrivKey
.
data
(),
&
encLen
,
pubKeyX
.
data
(),
...
...
@@ -160,7 +160,7 @@ TEST_CASE_METHOD(TestFixture, "ECDSA AES key gen", "[ecdsa-aes-key-gen]") {
vector
<
uint8_t
>
encrPrivKey
(
BUF_LEN
,
0
);
vector
<
char
>
pubKeyX
(
BUF_LEN
,
0
);
vector
<
char
>
pubKeyY
(
BUF_LEN
,
0
);
uint
32
_t
encLen
=
0
;
uint
64
_t
encLen
=
0
;
PRINT_SRC_LINE
auto
status
=
trustedGenerateEcdsaKeyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encrPrivKey
.
data
(),
&
encLen
,
pubKeyX
.
data
(),
...
...
@@ -177,7 +177,8 @@ TEST_CASE_METHOD(TestFixture, "ECDSA AES get public key", "[ecdsa-aes-get-pub-ke
vector
<
uint8_t
>
encPrivKey
(
BUF_LEN
,
0
);
vector
<
char
>
pubKeyX
(
BUF_LEN
,
0
);
vector
<
char
>
pubKeyY
(
BUF_LEN
,
0
);
uint32_t
encLen
=
0
;
uint64_t
encLen
=
0
;
PRINT_SRC_LINE
auto
status
=
trustedGenerateEcdsaKeyAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encPrivKey
.
data
(),
&
encLen
,
pubKeyX
.
data
(),
...
...
@@ -291,7 +292,7 @@ TEST_CASE_METHOD(TestFixture, "DKG AES gen test", "[dkg-aes-gen]") {
vector
<
char
>
errMsg
(
BUF_LEN
,
0
);
int
errStatus
=
0
;
uint
32
_t
encLen
=
0
;
uint
64
_t
encLen
=
0
;
PRINT_SRC_LINE
auto
status
=
trustedGenDkgSecretAES
(
eid
,
&
errStatus
,
errMsg
.
data
(),
encryptedDKGSecret
.
data
(),
&
encLen
,
32
);
...
...
@@ -314,7 +315,7 @@ TEST_CASE_METHOD(TestFixture, "DKG AES public shares test", "[dkg-aes-pub-shares
vector
<
char
>
errMsg
(
BUF_LEN
,
0
);
int
errStatus
=
0
;
uint
32
_t
encLen
=
0
;
uint
64
_t
encLen
=
0
;
unsigned
t
=
32
,
n
=
32
;
PRINT_SRC_LINE
...
...
@@ -363,7 +364,7 @@ TEST_CASE_METHOD(TestFixture, "DKG AES encrypted secret shares test", "[dkg-aes-
vector
<
char
>
result
(
BUF_LEN
,
0
);
int
errStatus
=
0
;
uint
32
_t
encLen
=
0
;
uint
64
_t
encLen
=
0
;
vector
<
uint8_t
>
encryptedDKGSecret
(
BUF_LEN
,
0
);
PRINT_SRC_LINE
...
...
@@ -694,7 +695,7 @@ TEST_CASE_METHOD(TestFixture, "AES_DKG test", "[aes-dkg]") {
TEST_CASE_METHOD
(
TestFixture
,
"AES encrypt/decrypt"
,
"[aes-encrypt-decrypt]"
)
{
int
errStatus
=
0
;
vector
<
char
>
errMsg
(
BUF_LEN
,
0
);
uint
32
_t
encLen
;
uint
64
_t
encLen
;
string
key
=
SAMPLE_AES_KEY
;
vector
<
uint8_t
>
encrypted_key
(
BUF_LEN
,
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