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
6d82dafc
Unverified
Commit
6d82dafc
authored
Sep 09, 2019
by
kladkogex
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed problem
parent
f51ca696
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
240 additions
and
255 deletions
+240
-255
BLSUtils.cpp
secure_enclave/BLSUtils.cpp
+56
-75
BLSUtils.h
secure_enclave/BLSUtils.h
+1
-1
secure_enclave.c
secure_enclave/secure_enclave.c
+183
-163
sgxwallet_common.h
sgxwallet_common.h
+0
-16
No files found.
secure_enclave/BLSUtils.cpp
View file @
6d82dafc
...
...
@@ -3,7 +3,6 @@
//
#define GMP_WITH_SGX
#include <string.h>
#include <cstdint>
#include "../sgxwallet_common.h"
...
...
@@ -28,7 +27,7 @@ std::string *stringFromKey(libff::alt_bn128_Fr *_key) {
return
new
std
::
string
(
tmp
);
}
std
::
string
*
stringFromFq
(
libff
::
alt_bn128_Fq
*
_fq
)
{
std
::
string
*
stringFromFq
(
libff
::
alt_bn128_Fq
*
_fq
)
{
mpz_t
t
;
mpz_init
(
t
);
...
...
@@ -53,72 +52,53 @@ std::string *stringFromG1(libff::alt_bn128_G1 *_g1) {
auto
sG1
=
new
std
::
string
(
*
sX
+
":"
+
*
sY
);
delete
(
sX
);
delete
(
sY
);
delete
(
sX
);
delete
(
sY
);
return
sG1
;
}
libff
::
alt_bn128_Fr
*
keyFromString
(
const
char
*
_keyString
)
{
return
new
libff
::
alt_bn128_Fr
(
_keyString
);
}
void
check_key
(
int
*
err_status
,
char
*
err_string
,
const
char
*
_keyString
)
{
*
err_status
=
UNKNOWN_ERROR
;
uint64_t
keyLen
=
strnlen
(
_keyString
,
MAX_KEY_LENGTH
);
// check that key is zero terminated string
if
(
keyLen
==
MAX_KEY_LENGTH
)
{
*
err_status
=
PLAINTEXT_KEY_TOO_LONG
;
snprintf
(
err_string
,
MAX_ERR_LEN
,
"Plaintext key too long"
);
return
;
}
libff
::
alt_bn128_Fr
*
keyFromString
(
const
char
*
_keyString
)
{
return
new
libff
::
alt_bn128_Fr
(
_keyString
);
}
if
(
_keyString
==
nullptr
)
{
*
err_status
=
NULL_KEY
;
snprintf
(
err_string
,
BUF_LEN
,
"Null key string"
);
return
;
}
bool
check_key
(
const
char
*
_keyString
)
{
for
(
int
i
=
keyLen
;
i
<
MAX_KEY_LENGTH
;
i
++
)
{
if
(
_keyString
[
i
]
!=
0
)
{
*
err_status
=
UNPADDED_KEY
;
snprintf
(
err_string
,
BUF_LEN
,
"Unpadded key passed to wrap"
);
return
;
}
}
libff
::
init_alt_bn128_params
();
if
(
_keyString
==
nullptr
)
return
false
;
std
::
string
ks
(
_keyString
);
// std::string keyString =
// "4160780231445160889237664391382223604184857153814275770598791864649971919844";
// std::string keyString =
// "4160780231445160889237664391382223604184857153814275770598791864649971919844";
auto
key
=
keyFromString
(
ks
.
c_str
());
auto
s1
=
stringFromKey
(
key
);
if
(
s1
->
compare
(
ks
)
!=
0
)
{
*
err_status
=
INCORRECT_STRING_CONVERSION
;
snprintf
(
err_string
,
BUF_LEN
,
"Incorrect string conversion"
);
return
;
}
if
(
s1
->
compare
(
ks
)
!=
0
)
return
false
;
if
(
s1
->
size
()
<
10
)
return
false
;
*
err_status
=
0
;
if
(
s1
->
size
()
>=
100
)
return
false
;
return
true
;
}
bool
sign
(
const
char
*
_keyString
,
const
char
*
_hashXString
,
const
char
*
_hashYString
,
bool
sign
(
const
char
*
_keyString
,
const
char
*
_hashXString
,
const
char
*
_hashYString
,
char
sig
[
BUF_LEN
])
{
auto
key
=
keyFromString
(
_keyString
);
...
...
@@ -146,6 +126,7 @@ bool sign(const char *_keyString, const char *_hashXString, const char *_hashYSt
return
true
;
}
...
...
secure_enclave/BLSUtils.h
View file @
6d82dafc
...
...
@@ -13,7 +13,7 @@
#define EXTERNC
#endif
EXTERNC
void
check_key
(
int
*
err_status
,
char
*
err_string
,
const
char
*
_keyString
);
EXTERNC
bool
check_key
(
const
char
*
_keyString
);
EXTERNC
bool
sign
(
const
char
*
_keyString
,
const
char
*
_hashXString
,
const
char
*
_hashYString
,
char
*
_sig
);
...
...
secure_enclave/secure_enclave.c
View file @
6d82dafc
...
...
@@ -50,17 +50,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../sgxwallet_common.h"
void
*
(
*
gmp_realloc_func
)(
void
*
,
size_t
,
size_t
);
void
*
(
*
oc_realloc_func
)(
void
*
,
size_t
,
size_t
);
void
(
*
gmp_free_func
)(
void
*
,
size_t
);
void
(
*
oc_free_func
)(
void
*
,
size_t
);
void
*
reallocate_function
(
void
*
,
size_t
,
size_t
);
void
free_function
(
void
*
,
size_t
);
...
...
@@ -102,10 +97,10 @@ void *reallocate_function(void *ptr, size_t osize, size_t nsize) {
* free() and try again, but would you trust the OS at this point?
*/
if
(
!
sgx_is_outside_enclave
((
void
*
)
ptr
,
nsize
))
if
(
!
sgx_is_outside_enclave
((
void
*
)
ptr
,
nsize
))
abort
();
return
(
void
*
)
nptr
;
return
(
void
*
)
nptr
;
}
void
e_mpz_add
(
mpz_t
*
c_un
,
mpz_t
*
a_un
,
mpz_t
*
b_un
)
{}
...
...
@@ -125,76 +120,99 @@ void generate_ecdsa_key(int *err_status, char *err_string,
void
encrypt_key
(
int
*
err_status
,
char
*
err_string
,
char
*
key
,
uint8_t
*
encrypted_key
,
uint32_t
*
enc_len
)
{
*
err_status
=
UNKNOWN_ERROR
;
*
err_status
=
-
1
;
uint64_t
keyLen
=
strnlen
(
key
,
MAX_KEY_LENGTH
);
check_key
(
err_status
,
err_string
,
key
);
// check that key is zero terminated string
if
(
*
err_status
!=
0
)
{
snprintf
(
err_string
+
strlen
(
err_string
),
BUF_LEN
,
":check_key failed"
);
if
(
keyLen
==
MAX_KEY_LENGTH
)
{
snprintf
(
err_string
,
MAX_ERR_LEN
,
"keyLen != MAX_KEY_LENGTH"
);
return
;
}
*
err_status
=
-
2
;
// check that key is padded with 0s
for
(
int
i
=
keyLen
;
i
<
MAX_KEY_LENGTH
;
i
++
)
{
if
(
key
[
i
]
!=
0
)
{
snprintf
(
err_string
,
BUF_LEN
,
"Unpadded key"
);
return
;
}
}
*
err_status
=
-
3
;
if
(
!
check_key
(
key
))
{
snprintf
(
err_string
,
BUF_LEN
,
"check_key failed"
);
return
;
}
uint32_t
sealedLen
=
sgx_calc_sealed_data_size
(
0
,
MAX_KEY_LENGTH
);
*
err_status
=
-
4
;
if
(
sealedLen
>
BUF_LEN
)
{
snprintf
(
err_string
,
BUF_LEN
,
"sealedLen > BUF_LEN
"
);
snprintf
(
err_string
,
BUF_LEN
,
"sealedLen > MAX_ENCRYPTED_KEY_LENGTH
"
);
return
;
}
sgx_status_t
status
;
*
err_status
=
-
5
;
sgx_seal_data
(
0
,
NULL
,
MAX_KEY_LENGTH
,
(
uint8_t
*
)
key
,
sealedLen
,
(
sgx_sealed_data_t
*
)
encrypted_key
);
memset
(
encrypted_key
,
0
,
BUF_LEN
);
if
(
status
!=
SGX_SUCCESS
)
{
snprintf
(
err_string
,
BUF_LEN
,
"SGX seal data failed with status %d"
,
status
);
err_status
=
SGX_SEAL_DATA_FAILED
;
if
(
sgx_seal_data
(
0
,
NULL
,
MAX_KEY_LENGTH
,
(
uint8_t
*
)
key
,
sealedLen
,
(
sgx_sealed_data_t
*
)
encrypted_key
)
!=
SGX_SUCCESS
)
{
snprintf
(
err_string
,
BUF_LEN
,
"SGX seal data failed"
)
;
return
;
}
*
enc_len
=
sealedLen
;
char
decryptedKey
[
BUF_LEN
];
decrypt_key
(
err_status
,
err_string
,
encrypted_key
,
sealedLen
,
decryptedKey
);
char
key2
[
BUF_LEN
];
memset
(
key2
,
0
,
BUF_LEN
);
decrypt_key
(
err_status
,
err_string
,
encrypted_key
,
sealedLen
,
key2
);
if
(
*
err_status
!=
0
)
{
snprintf
(
err_string
+
strlen
(
err_string
),
BUF_LEN
,
":decrypt_key failed"
);
snprintf
(
err_string
+
strlen
(
err_string
),
BUF_LEN
,
":decrypt_key failed"
);
return
;
}
uint64_t
decryptedKeyLen
=
strnlen
(
decryptedKey
,
MAX_KEY_LENGTH
);
if
(
decryptedKeyLen
==
MAX_KEY_LENGTH
)
{
*
err_status
=
STRING_NOT_NULL_TERMINATED
;
snprintf
(
err_string
,
MAX_ERR_LEN
,
"Key2 is not null terminated"
);
uint64_t
key2Len
=
strnlen
(
key2
,
MAX_KEY_LENGTH
);
if
(
key2Len
==
MAX_KEY_LENGTH
)
{
snprintf
(
err_string
,
MAX_ERR_LEN
,
"Key2 is not null terminated"
);
return
;
}
if
(
strncmp
(
key
,
decryptedKey
,
MAX_KEY_LENGTH
)
!=
0
)
{
*
err_status
=
ENCRYPTION_DECRYPTION_MISMATCH
;
snprintf
(
err_string
,
MAX_ERR_LEN
,
"Decrypted key does not match original"
);
*
err_status
=
-
8
;
if
(
strncmp
(
key
,
key2
,
MAX_KEY_LENGTH
)
!=
0
)
return
;
}
*
err_status
=
0
;
}
void
decrypt_key
(
int
*
err_status
,
char
*
err_string
,
uint8_t
*
encrypted_key
,
uint32_t
enc_len
,
char
*
key
)
{
uint32_t
enc_len
,
char
*
key
)
{
uint32_t
decLen
;
*
err_status
=
-
9
;
memset
(
key
,
0
,
BUF_LEN
);
sgx_status_t
status
=
sgx_unseal_data
(
(
const
sgx_sealed_data_t
*
)
encrypted_key
,
NULL
,
0
,
(
uint8_t
*
)
key
,
&
decLen
);
(
const
sgx_sealed_data_t
*
)
encrypted_key
,
NULL
,
0
,
(
uint8_t
*
)
key
,
&
decLen
);
if
(
status
!=
SGX_SUCCESS
)
{
snprintf
(
err_string
,
BUF_LEN
,
"sgx_unseal_data failed with status %d"
,
status
);
snprintf
(
err_string
,
BUF_LEN
,
"sgx_unseal_data failed with status %d"
,
status
);
return
;
}
...
...
@@ -219,7 +237,7 @@ void decrypt_key(int *err_status, char *err_string, uint8_t *encrypted_key,
for
(
int
i
=
keyLen
;
i
<
MAX_KEY_LENGTH
;
i
++
)
{
if
(
key
[
i
]
!=
0
)
{
snprintf
(
err_string
,
BUF_LEN
,
"Unpadded key"
);
snprintf
(
err_string
,
BUF_LEN
,
"Unpadded key"
);
return
;
}
}
...
...
@@ -230,9 +248,11 @@ void decrypt_key(int *err_status, char *err_string, uint8_t *encrypted_key,
}
void
bls_sign_message
(
int
*
err_status
,
char
*
err_string
,
uint8_t
*
encrypted_key
,
uint32_t
enc_len
,
char
*
_hashX
,
char
*
_hashY
,
char
*
signature
)
{
char
*
_hashY
,
char
*
signature
)
{
char
key
[
BUF_LEN
];
...
...
@@ -244,7 +264,7 @@ void bls_sign_message(int *err_status, char *err_string, uint8_t *encrypted_key,
return
;
}
sign
(
key
,
_hashX
,
_hashY
,
sig
);
sign
(
key
,
_hashX
,
_hashY
,
sig
);
strncpy
(
signature
,
sig
,
BUF_LEN
);
...
...
@@ -283,7 +303,7 @@ void ecdsa_sign_message(int *err_status, char *err_string, uint8_t *encrypted_ke
RAND_add
(
entropy_buf
,
sizeof
(
entropy_buf
),
ADD_ENTROPY_SIZE
);
RAND_seed
(
entropy_buf
,
sizeof
(
entropy_buf
));
EC_KEY
*
ec
=
NULL
;
EC_KEY
*
ec
=
NULL
;
int
eccgroup
;
eccgroup
=
OBJ_txt2nid
(
"secp384r1"
);
ec
=
EC_KEY_new_by_curve_name
(
eccgroup
);
...
...
@@ -313,7 +333,7 @@ void ecdsa_sign_message(int *err_status, char *err_string, uint8_t *encrypted_ke
for
(
i
=
0
;
i
<
1000
;
i
++
)
{
// Add context
EVP_MD_CTX
*
context
=
EVP_MD_CTX_new
();
EVP_MD_CTX
*
context
=
EVP_MD_CTX_new
();
// Init, update, final
EVP_SignInit_ex
(
context
,
EVP_sha1
(),
NULL
);
EVP_SignUpdate
(
context
,
&
buffer
,
100
);
...
...
sgxwallet_common.h
View file @
6d82dafc
...
...
@@ -27,21 +27,5 @@
#define ADD_ENTROPY_SIZE 32
#define UNKNOWN_ERROR -1
#define PLAINTEXT_KEY_TOO_LONG -2
#define UNPADDED_KEY -3
#define NULL_KEY -4
#define INCORRECT_STRING_CONVERSION -5
#define SEALED_LEN_TOO_LARGE -6
#define SGX_SEAL_DATA_FAILED -7
#define STRING_NOT_NULL_TERMINATED -8
#define ENCRYPTION_DECRYPTION_MISMATCH -9
#endif //SGXWALLET_SGXWALLET_COMMON_H
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