Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making mock_ecc build and run successfully #518

Merged
merged 1 commit into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 178 additions & 3 deletions common/crypto/pdo-crypto-c-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ extern "C" {
#endif

extern "C" const unsigned int SYM_KEY_LEN = pdo::crypto::constants::SYM_KEY_LEN;
extern "C" const unsigned int IV_LEN = pdo::crypto::constants::IV_LEN;
extern "C" const unsigned int TAG_LEN = pdo::crypto::constants::TAG_LEN;

extern "C" const unsigned int RSA_PLAINTEXT_LEN = pdo::crypto::constants::RSA_PLAINTEXT_LEN;
extern "C" const unsigned int RSA_KEY_SIZE = pdo::crypto::constants::RSA_KEY_SIZE;

Expand Down Expand Up @@ -54,9 +57,9 @@ bool verify_signature(uint8_t* public_key, uint32_t public_key_len, uint8_t* mes
int r = pk.VerifySignature(msg, sig);
COND2ERR(r != 1);
}
catch(...)
catch(const std::exception& e)
{
COND2ERR(true);
COND2LOGERR(true, e.what());
}

// verification successful
Expand All @@ -66,6 +69,38 @@ bool verify_signature(uint8_t* public_key, uint32_t public_key_len, uint8_t* mes
return false;
}

bool sign_message(uint8_t* private_key,
uint32_t private_key_len,
uint8_t* message,
uint32_t message_len,
uint8_t* signature,
uint32_t signature_len,
uint32_t* signature_actual_len)
{
try
{
std::string prk_string((const char*)private_key, private_key_len);

//deserialize private key
pdo::crypto::sig::PrivateKey prk(prk_string);

ByteArray ba_signature = prk.SignMessage(ByteArray(message, message + message_len));

COND2ERR(ba_signature.size() > signature_len);
memcpy(signature, ba_signature.data(), ba_signature.size());
*signature_actual_len = ba_signature.size();
}
catch(const std::exception& e)
{
COND2LOGERR(true, e.what());
}

return true;

err:
return false;
}

bool pk_encrypt_message(uint8_t* public_key,
uint32_t public_key_len,
uint8_t* message,
Expand All @@ -86,7 +121,6 @@ bool pk_encrypt_message(uint8_t* public_key,
//encrypt message
encr_msg = pk.EncryptMessage(msg);

LOG_DEBUG("encr msg size %d buffer len %d", encr_msg.size(), encrypted_message_len);
COND2LOGERR(encrypted_message_len < encr_msg.size(), "buffer too small for encrypted msg");
memcpy(encrypted_message, encr_msg.data(), encr_msg.size());
*encrypted_message_actual_len = encr_msg.size();
Expand All @@ -103,6 +137,75 @@ bool pk_encrypt_message(uint8_t* public_key,
return false;
}

bool pk_decrypt_message(uint8_t* private_key,
uint32_t private_key_len,
uint8_t* encrypted_message,
uint32_t encrypted_message_len,
uint8_t* message,
uint32_t message_len,
uint32_t* message_actual_len)
{
try
{
std::string prk_string((const char*)private_key, private_key_len);
ByteArray encr_msg(encrypted_message, encrypted_message + encrypted_message_len);
ByteArray msg;

//deserialize private key
pdo::crypto::pkenc::PrivateKey prk(prk_string);

//decrypt message
msg = prk.DecryptMessage(encr_msg);

COND2LOGERR(message_len < msg.size(), "buffer too small for decrypted message");
memcpy(message, msg.data(), msg.size());
*message_actual_len = msg.size();
}
catch(const std::exception& e)
{
COND2LOGERR(true, e.what());
}

// decryption successful
return true;

err:
return false;
}

bool encrypt_message(uint8_t* key,
uint32_t key_len,
uint8_t* message,
uint32_t message_len,
uint8_t* encrypted_message,
uint32_t encrypted_message_len,
uint32_t* encrypted_message_actual_len)
{
try
{
ByteArray ba_key(key, key + key_len);
ByteArray msg(message, message + message_len);
ByteArray encr_msg;

//encrypt message
encr_msg = pdo::crypto::skenc::EncryptMessage(ba_key, msg);

COND2ERR(encrypted_message_len < encr_msg.size());
memcpy(encrypted_message, encr_msg.data(), encr_msg.size());
*encrypted_message_actual_len = encr_msg.size();
}
catch(const std::exception& e)
{
COND2LOGERR(true, e.what());
}

//encryption successful
return true;

err:
return false;
}

bool decrypt_message(uint8_t* key,
uint32_t key_len,
uint8_t* encrypted_message,
Expand Down Expand Up @@ -136,6 +239,78 @@ bool decrypt_message(uint8_t* key,
return false;
}

bool new_rsa_key(uint8_t* public_key,
uint32_t public_key_len,
uint32_t* public_key_actual_len,
uint8_t* private_key,
uint32_t private_key_len,
uint32_t* private_key_actual_len)
{
try
{
pdo::crypto::pkenc::PrivateKey pri_key;
pdo::crypto::pkenc::PublicKey pub_key;
pri_key.Generate();
pub_key = pri_key.GetPublicKey();

std::string puk = pub_key.Serialize();
std::string prk = pri_key.Serialize();

COND2ERR(puk.length() > public_key_len);
COND2ERR(prk.length() > private_key_len);

memcpy(public_key, puk.c_str(), puk.length());
memcpy(private_key, prk.c_str(), prk.length());
*public_key_actual_len = puk.length();
*private_key_actual_len = prk.length();
}
catch(const std::exception& e)
{
COND2LOGERR(true, e.what());
}

return true;

err:
return false;
}

bool new_ecdsa_key(uint8_t* public_key,
uint32_t public_key_len,
uint32_t* public_key_actual_len,
uint8_t* private_key,
uint32_t private_key_len,
uint32_t* private_key_actual_len)
{
try
{
pdo::crypto::sig::PrivateKey pri_key;
pdo::crypto::sig::PublicKey pub_key;
pri_key.Generate();
pub_key = pri_key.GetPublicKey();

std::string puk = pub_key.Serialize();
std::string prk = pri_key.Serialize();

COND2ERR(puk.length() > public_key_len);
COND2ERR(prk.length() > private_key_len);

memcpy(public_key, puk.c_str(), puk.length());
memcpy(private_key, prk.c_str(), prk.length());
*public_key_actual_len = puk.length();
*private_key_actual_len = prk.length();
}
catch(const std::exception& e)
{
COND2LOGERR(true, e.what());
}

return true;

err:
return false;
}

#ifdef __cplusplus
}
#endif /* __cplusplus */
41 changes: 41 additions & 0 deletions common/crypto/pdo-crypto-c-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ extern "C" {
#endif

extern const unsigned int SYM_KEY_LEN;
extern const unsigned int IV_LEN;
extern const unsigned int TAG_LEN;

extern const unsigned int RSA_PLAINTEXT_LEN;
extern const unsigned int RSA_KEY_SIZE;

Expand All @@ -27,6 +30,14 @@ bool verify_signature(uint8_t* public_key,
uint8_t* signature,
uint32_t signature_len);

bool sign_message(uint8_t* private_key,
uint32_t private_key_len,
uint8_t* message,
uint32_t message_len,
uint8_t* signature,
uint32_t signature_len,
uint32_t* signature_actual_len);

bool pk_encrypt_message(uint8_t* public_key,
uint32_t public_key_len,
uint8_t* message,
Expand All @@ -35,6 +46,22 @@ bool pk_encrypt_message(uint8_t* public_key,
uint32_t encrypted_message_len,
uint32_t* encrypted_message_actual_len);

bool pk_decrypt_message(uint8_t* private_key,
uint32_t private_key_len,
uint8_t* encrypted_message,
uint32_t encrypted_message_len,
uint8_t* message,
uint32_t message_len,
uint32_t* message_actual_len);

bool encrypt_message(uint8_t* key,
uint32_t key_len,
uint8_t* message,
uint32_t message_len,
uint8_t* encrypted_message,
uint32_t encrypted_message_len,
uint32_t* encrypted_message_actual_len);

bool decrypt_message(uint8_t* key,
uint32_t key_len,
uint8_t* encrypted_message,
Expand All @@ -43,6 +70,20 @@ bool decrypt_message(uint8_t* key,
uint32_t message_len,
uint32_t* message_actual_len);

bool new_rsa_key(uint8_t* public_key,
uint32_t public_key_len,
uint32_t* public_key_actual_len,
uint8_t* private_key,
uint32_t private_key_len,
uint32_t* private_key_actual_len);

bool new_ecdsa_key(uint8_t* public_key,
uint32_t public_key_len,
uint32_t* public_key_actual_len,
uint8_t* private_key,
uint32_t private_key_len,
uint32_t* private_key_actual_len);

#ifdef __cplusplus
}
#endif
Loading