Unverified Commit d6943929 authored by svetaro's avatar svetaro

Add generate keys to api

parent 466e8b7d
...@@ -3,19 +3,27 @@ ...@@ -3,19 +3,27 @@
// //
#include "ECDSACrypto.h" #include "ECDSACrypto.h"
#include "BLSCrypto.h"
#include "sgxwallet.h" #include "sgxwallet.h"
#include <iostream>
char* gen_ecdsa_key(){ std::vector<std::string> gen_ecdsa_key(){
char *errMsg = (char *)calloc(1024, 1); char *errMsg = (char *)calloc(1024, 1);
int err_status = 0; int err_status = 0;
char* encr_pr_key = (char *)calloc(1024, 1); uint8_t* encr_pr_key = (uint8_t *)calloc(1024, 1);
char *pub_key_x = (char *)calloc(1024, 1); char *pub_key_x = (char *)calloc(1024, 1);
char *pub_key_y = (char *)calloc(1024, 1); char *pub_key_y = (char *)calloc(1024, 1);
uint32_t enc_len = 0; uint32_t enc_len = 0;
status = generate_ecdsa_key(eid, &err_status, errMsg, (uint8_t*)encr_pr_key, &enc_len, pub_key_x, pub_key_y ); status = generate_ecdsa_key(eid, &err_status, errMsg, (uint8_t*)encr_pr_key, &enc_len, pub_key_x, pub_key_y );
std::vector<std::string> keys(2);
return encr_pr_key;
char *hexEncrKey = (char *) calloc(2 * BUF_LEN, 1);
carray2Hex(encr_pr_key, enc_len, hexEncrKey);
keys.at(0) = hexEncrKey;
keys.at(1) = std::string(pub_key_x) + std::string(pub_key_y);
std::cerr << "in ECDSACrypto encr key x " << keys.at(0) << std::endl;
return keys;
} }
std::vector<std::string> ecdsa_sign_hash(const char* encryptedKey, const char* hashHex){ std::vector<std::string> ecdsa_sign_hash(const char* encryptedKey, const char* hashHex){
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#define EXTERNC #define EXTERNC
#endif*/ #endif*/
char* gen_ecdsa_key(); std::vector<std::string> gen_ecdsa_key();
std::vector<std::string> ecdsa_sign_hash(const char* encryptedKey, const char* hashHex); std::vector<std::string> ecdsa_sign_hash(const char* encryptedKey, const char* hashHex);
......
...@@ -154,21 +154,25 @@ Json::Value generateECDSAKeyImpl(const std::string &_keyName) { ...@@ -154,21 +154,25 @@ Json::Value generateECDSAKeyImpl(const std::string &_keyName) {
cerr << "Calling method" << endl; cerr << "Calling method" << endl;
char* encryptedKey = nullptr; std::vector<std::string>keys;
try { try {
encryptedKey = gen_ecdsa_key(); keys = gen_ecdsa_key();
if (encryptedKey == nullptr) { if (keys.size() == 0 ) {
throw RPCException(UNKNOWN_ERROR, ""); throw RPCException(UNKNOWN_ERROR, "");
} }
writeECDSAKey(_keyName, encryptedKey); writeECDSAKey(_keyName, keys.at(0));
} catch (RPCException &_e) { } catch (RPCException &_e) {
result["status"] = _e.status; result["status"] = _e.status;
result["errorMessage"] = _e.errString; result["errorMessage"] = _e.errString;
} }
result["encryptedKey"] = encryptedKey; result["encryptedKey"] = keys.at(0);
result["PublicKey"] = keys.at(1);
std::cerr << "in SGXWalletServer encr key x " << keys.at(0) << std::endl;
return result; return result;
} }
......
...@@ -156,16 +156,18 @@ void generate_ecdsa_key(int *err_status, char *err_string, ...@@ -156,16 +156,18 @@ void generate_ecdsa_key(int *err_status, char *err_string,
signature_generate_key(Pkey, skey, curve); signature_generate_key(Pkey, skey, curve);
int len = mpz_sizeinbase (Pkey->x, 10) + 2; uint8_t base = 16;
int len = mpz_sizeinbase (Pkey->x, base) + 2;
//snprintf(err_string, BUF_LEN, "len = %d\n", len); //snprintf(err_string, BUF_LEN, "len = %d\n", len);
char arr_x[len]; char arr_x[len];
char* px = mpz_get_str(arr_x, 10, Pkey->x); char* px = mpz_get_str(arr_x, base, Pkey->x);
//snprintf(err_string, BUF_LEN, "arr=%p px=%p\n", arr_x, px); //snprintf(err_string, BUF_LEN, "arr=%p px=%p\n", arr_x, px);
strncpy(pub_key_x, arr_x, 1024); strncpy(pub_key_x, arr_x, 1024);
char arr_y[mpz_sizeinbase (Pkey->y, 10) + 2]; char arr_y[mpz_sizeinbase (Pkey->y, base) + 2];
char* py = mpz_get_str(arr_y, 10, Pkey->y); char* py = mpz_get_str(arr_y, base, Pkey->y);
strncpy(pub_key_y, arr_y, 1024); strncpy(pub_key_y, arr_y, 1024);
char skey_str[mpz_sizeinbase (skey, 10) + 2]; char skey_str[mpz_sizeinbase (skey, 10) + 2];
......
...@@ -51,7 +51,8 @@ ...@@ -51,7 +51,8 @@
"returns": { "returns": {
"status": 0, "status": 0,
"errorMessage": "12345", "errorMessage": "12345",
"encryptedKey": "12345" "encryptedKey": "12345",
"PublicKey": "12345"
} }
}, },
...@@ -66,7 +67,9 @@ ...@@ -66,7 +67,9 @@
"returns": { "returns": {
"status": 0, "status": 0,
"errorMessage": "12345", "errorMessage": "12345",
"signature": "12345" "signature_v": "12345",
"signature_r": "12345",
"signature_s": "12345"
} }
} }
] ]
\ No newline at end of file
...@@ -414,7 +414,7 @@ using namespace std; ...@@ -414,7 +414,7 @@ using namespace std;
TEST_CASE("API test", "[api_test]") { TEST_CASE("API test", "[api_test]") {
cerr << "API test started" << endl; cerr << "API test started" << endl;
// init_all(); init_all();
//HttpServer httpserver(1025); //HttpServer httpserver(1025);
//SGXWalletServer s(httpserver, //SGXWalletServer s(httpserver,
...@@ -427,7 +427,7 @@ TEST_CASE("API test", "[api_test]") { ...@@ -427,7 +427,7 @@ TEST_CASE("API test", "[api_test]") {
cerr << "Client inited" << endl; cerr << "Client inited" << endl;
try { try {
cout << c.generateECDSAKey("test_key") << endl; cout << c.generateECDSAKey("test_key1") << endl;
} catch (JsonRpcException &e) { } catch (JsonRpcException &e) {
cerr << e.what() << endl; cerr << e.what() << endl;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment