X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fkey.cpp;h=6c0a57dd131e21b2ae8989bdfcf2a173444128b6;hb=9d14e64825d95061ea0857267646dfcb3d62e07a;hp=afe63cb4a318e5ae286089e5000fd2d1b5aa46d3;hpb=27275f6bf78b909b253530f77e00206924e54130;p=novacoin.git diff --git a/src/key.cpp b/src/key.cpp index afe63cb..6c0a57d 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -6,7 +6,6 @@ #include #include -#include #include "key.h" #include "base58.h" @@ -590,6 +589,12 @@ bool CPoint::setBytes(const std::vector &vchBytes) return true; } +// Initialize from octets stream +bool CPoint::setPubKey(const CPubKey &vchPubKey) +{ + return setBytes(vchPubKey.Raw()); +} + // Serialize to octets stream bool CPoint::getBytes(std::vector &vchBytes) { @@ -664,7 +669,7 @@ void CMalleablePubKey::GetVariant(CPubKey &R, CPubKey &vchPubKeyVariant) EC_KEY_free(eckey); CPoint point; - if (!point.setBytes(pubKeyL.Raw())) { + if (!point.setPubKey(pubKeyL)) { throw key_error("CMalleablePubKey::GetVariant() : Unable to decode L value"); } @@ -681,7 +686,7 @@ void CMalleablePubKey::GetVariant(CPubKey &R, CPubKey &vchPubKeyVariant) bnHash.setuint160(Hash160(vchLr)); CPoint pointH; - pointH.setBytes(pubKeyH.Raw()); + pointH.setPubKey(pubKeyH); CPoint P; // Calculate P = Hash(L*r)*G + H @@ -697,7 +702,7 @@ void CMalleablePubKey::GetVariant(CPubKey &R, CPubKey &vchPubKeyVariant) vchPubKeyVariant = CPubKey(vchResult); } -std::string CMalleablePubKey::ToString() +std::string CMalleablePubKey::ToString() const { CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION); ssKey << *this; @@ -706,6 +711,15 @@ std::string CMalleablePubKey::ToString() return EncodeBase58Check(vch); } +std::vector CMalleablePubKey::Raw() const +{ + CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION); + ssKey << *this; + std::vector vch(ssKey.begin(), ssKey.end()); + + return vch; +} + bool CMalleablePubKey::SetString(const std::string& strMalleablePubKey) { std::vector vchTemp; @@ -766,13 +780,6 @@ CMalleableKey::CMalleableKey(const CSecret &L, const CSecret &H) SetSecrets(L, H); } -CMalleableKey& CMalleableKey::operator=(const CMalleableKey &b) -{ - SetSecrets(b.vchSecretL, b.vchSecretH); - - return (*this); -} - CMalleableKey::~CMalleableKey() { } @@ -787,7 +794,7 @@ bool CMalleableKey::SetSecrets(const CSecret &pvchSecretL, const CSecret &pvchSe Reset(); CKey L, H; - if (!L.SetSecret(pvchSecretL, true) || !H.SetSecret(pvchSecretH, true)) + if (pvchSecretL.size() != 32 || pvchSecretH.size() != 32 || !L.SetSecret(pvchSecretL, true) || !H.SetSecret(pvchSecretH, true)) { nVersion = 0; return false; @@ -800,12 +807,6 @@ bool CMalleableKey::SetSecrets(const CSecret &pvchSecretL, const CSecret &pvchSe return true; } -void CMalleableKey::GetSecrets(CSecret &pvchSecretL, CSecret &pvchSecretH) const -{ - pvchSecretL = vchSecretL; - pvchSecretH = vchSecretH; -} - CMalleablePubKey CMalleableKey::GetMalleablePubKey() const { CKey L, H; @@ -819,7 +820,7 @@ CMalleablePubKey CMalleableKey::GetMalleablePubKey() const } // Check ownership -bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) +bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const { if (IsNull()) { throw key_error("CMalleableKey::CheckKeyVariant() : Attempting to run on NULL key object."); @@ -834,7 +835,7 @@ bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVa } CPoint point_R; - if (!point_R.setBytes(R.Raw())) { + if (!point_R.setPubKey(R)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode R value"); } @@ -843,12 +844,12 @@ bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVa std::vector vchPubKeyH = H.GetPubKey().Raw(); CPoint point_H; - if (!point_H.setBytes(vchPubKeyH)) { + if (!point_H.setPubKey(vchPubKeyH)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode H value"); } CPoint point_P; - if (!point_P.setBytes(vchPubKeyVariant.Raw())) { + if (!point_P.setPubKey(vchPubKeyVariant)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode P value"); } @@ -889,7 +890,7 @@ bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVa } // Check ownership and restore private key -bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant, CKey &privKeyVariant) +bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant, CKey &privKeyVariant) const { if (IsNull()) { throw key_error("CMalleableKey::CheckKeyVariant() : Attempting to run on NULL key object."); @@ -904,7 +905,7 @@ bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVa } CPoint point_R; - if (!point_R.setBytes(R.Raw())) { + if (!point_R.setPubKey(R)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode R value"); } @@ -913,12 +914,12 @@ bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVa std::vector vchPubKeyH = H.GetPubKey().Raw(); CPoint point_H; - if (!point_H.setBytes(vchPubKeyH)) { + if (!point_H.setPubKey(vchPubKeyH)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode H value"); } CPoint point_P; - if (!point_P.setBytes(vchPubKeyVariant.Raw())) { + if (!point_P.setPubKey(vchPubKeyVariant)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode P value"); } @@ -968,7 +969,7 @@ bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVa return true; } -std::string CMalleableKey::ToString() +std::string CMalleableKey::ToString() const { CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION); ssKey << *this; @@ -977,6 +978,15 @@ std::string CMalleableKey::ToString() return EncodeBase58Check(vch); } +std::vector CMalleableKey::Raw() const +{ + CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION); + ssKey << *this; + std::vector vch(ssKey.begin(), ssKey.end()); + + return vch; +} + bool CMalleableKey::SetString(const std::string& strMutableKey) { std::vector vchTemp; @@ -987,35 +997,48 @@ bool CMalleableKey::SetString(const std::string& strMutableKey) CDataStream ssKey(vchTemp, SER_NETWORK, PROTOCOL_VERSION); ssKey >> *this; - return IsNull(); + return IsValid(); } // CMalleableKeyView +CMalleableKeyView::CMalleableKeyView(const std::string &strMalleableKey) +{ + SetString(strMalleableKey); +} + CMalleableKeyView::CMalleableKeyView(const CMalleableKey &b) { - assert(b.nVersion == CURRENT_VERSION); + if (b.vchSecretL.size() != 32) + throw key_error("CMalleableKeyView::CMalleableKeyView() : L size must be 32 bytes"); + + if (b.vchSecretH.size() != 32) + throw key_error("CMalleableKeyView::CMalleableKeyView() : L size must be 32 bytes"); + vchSecretL = b.vchSecretL; CKey H; H.SetSecret(b.vchSecretH, true); + vchPubKeyH = H.GetPubKey().Raw(); + nVersion = b.nVersion; } -CMalleableKeyView::CMalleableKeyView(const CSecret &L, const CPubKey &pvchPubKeyH) +CMalleableKeyView::CMalleableKeyView(const CMalleableKeyView &b) { - vchSecretL = L; - vchPubKeyH = pvchPubKeyH.Raw(); + vchSecretL = b.vchSecretL; + vchPubKeyH = b.vchPubKeyH; + nVersion = CURRENT_VERSION; } CMalleableKeyView& CMalleableKeyView::operator=(const CMalleableKey &b) { - assert(b.nVersion == CURRENT_VERSION); vchSecretL = b.vchSecretL; CKey H; H.SetSecret(b.vchSecretH, true); vchPubKeyH = H.GetPubKey().Raw(); + nVersion = b.nVersion; return (*this); } @@ -1032,7 +1055,7 @@ CMalleablePubKey CMalleableKeyView::GetMalleablePubKey() const } // Check ownership -bool CMalleableKeyView::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) +bool CMalleableKeyView::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const { if (!R.IsValid()) { throw key_error("CMalleableKeyView::CheckKeyVariant() : R is invalid"); @@ -1043,17 +1066,17 @@ bool CMalleableKeyView::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubK } CPoint point_R; - if (!point_R.setBytes(R.Raw())) { + if (!point_R.setPubKey(R)) { throw key_error("CMalleableKeyView::CheckKeyVariant() : Unable to decode R value"); } CPoint point_H; - if (!point_H.setBytes(vchPubKeyH)) { + if (!point_H.setPubKey(vchPubKeyH)) { throw key_error("CMalleableKeyView::CheckKeyVariant() : Unable to decode H value"); } CPoint point_P; - if (!point_P.setBytes(vchPubKeyVariant.Raw())) { + if (!point_P.setPubKey(vchPubKeyVariant)) { throw key_error("CMalleableKeyView::CheckKeyVariant() : Unable to decode P value"); } @@ -1093,18 +1116,104 @@ bool CMalleableKeyView::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubK return true; } +std::string CMalleableKeyView::ToString() const +{ + CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION); + ssKey << *this; + std::vector vch(ssKey.begin(), ssKey.end()); + + return EncodeBase58Check(vch); +} + +bool CMalleableKeyView::SetString(const std::string& strMutableKey) +{ + std::vector vchTemp; + if (!DecodeBase58Check(strMutableKey, vchTemp)) { + throw key_error("CMalleableKeyView::SetString() : Provided key data seems corrupted."); + } + + CDataStream ssKey(vchTemp, SER_NETWORK, PROTOCOL_VERSION); + ssKey >> *this; + + return IsValid(); +} + +std::vector CMalleableKeyView::Raw() const +{ + CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION); + ssKey << *this; + std::vector vch(ssKey.begin(), ssKey.end()); + + return vch; +} + + +bool CMalleableKeyView::IsNull() const +{ + return nVersion != CURRENT_VERSION; +} + //// Asymmetric encryption -bool CPubKey::EncryptData(const std::vector& data, std::vector& encrypted) +void CPubKey::EncryptData(const std::vector& data, std::vector& encrypted) { - // TODO + CKey key; + key.SetPubKey(*this); - return true; + key.EncryptData(data, encrypted); } -bool CKey::DecryptData(const std::vector& encrypted, std::vector& data) +void CKey::EncryptData(const std::vector& data, std::vector& encrypted) { - // TODO + ies_ctx_t *ctx; + char error[1024] = "Unknown error"; + cryptogram_t *cryptogram; + + ctx = create_context(pkey); + if (!EC_KEY_get0_public_key(ctx->user_key)) + throw key_error("Given EC key is not public key"); + + cryptogram = ecies_encrypt(ctx, (unsigned char*)&data[0], data.size(), error); + if (cryptogram == NULL) { + free(ctx); + ctx = NULL; + throw key_error(std::string("Error in encryption: %s") + error); + } - return true; + encrypted.resize(cryptogram_data_sum_length(cryptogram)); + unsigned char *key_data = cryptogram_key_data(cryptogram); + memcpy(&encrypted[0], key_data, encrypted.size()); + cryptogram_free(cryptogram); + free(ctx); +} + +void CKey::DecryptData(const std::vector& encrypted, std::vector& data) +{ + ies_ctx_t *ctx; + char error[1024] = "Unknown error"; + cryptogram_t *cryptogram; + size_t length; + unsigned char *decrypted; + + ctx = create_context(pkey); + if (!EC_KEY_get0_private_key(ctx->user_key)) + throw key_error("Given EC key is not private key"); + + size_t key_length = ctx->stored_key_length; + size_t mac_length = EVP_MD_size(ctx->md); + cryptogram = cryptogram_alloc(key_length, mac_length, encrypted.size() - key_length - mac_length); + + memcpy(cryptogram_key_data(cryptogram), &encrypted[0], encrypted.size()); + + decrypted = ecies_decrypt(ctx, cryptogram, &length, error); + cryptogram_free(cryptogram); + free(ctx); + + if (decrypted == NULL) { + throw key_error(std::string("Error in decryption: %s") + error); + } + + data.resize(length); + memcpy(&data[0], decrypted, length); + free(decrypted); }