Nonce jest częścią chunka danych
This commit is contained in:
parent
58c3870c53
commit
855f079bf7
2 changed files with 30 additions and 35 deletions
|
|
@ -13,21 +13,27 @@ EncryptionManager::EncryptionManager()
|
|||
|
||||
std::vector<char> EncryptionManager::encrypt(const std::vector<char>& raw)
|
||||
{
|
||||
std::vector<char> crypt(raw.size());
|
||||
|
||||
// Generowanie kluczy
|
||||
// generateKeys();
|
||||
std::array<uint8_t, crypto_stream_chacha20_ietf_NONCEBYTES> nonce_local;
|
||||
randombytes_buf(nonce_local.data(), nonce_local.size());
|
||||
|
||||
std::vector<char> tmp(raw.size());
|
||||
if (crypto_stream_chacha20_ietf_xor_ic(
|
||||
reinterpret_cast<unsigned char*>(crypt.data()),
|
||||
reinterpret_cast<unsigned char*>(tmp.data()),
|
||||
reinterpret_cast<const unsigned char*>(raw.data()),
|
||||
static_cast<unsigned long long>(raw.size()),
|
||||
nonce.data(), 0, key.data()) != 0)
|
||||
static_cast<uint64_t>(raw.size()),
|
||||
nonce_local.data(), 0, key.data()) != 0)
|
||||
{
|
||||
throw std::runtime_error("crypto_stream_chacha20_ietf_xor_ic failed");
|
||||
}
|
||||
|
||||
return crypt;
|
||||
std::vector<char> output;
|
||||
output.insert(output.end(),
|
||||
reinterpret_cast<const char*>(nonce_local.data()),
|
||||
reinterpret_cast<const char*>(nonce_local.data()) + nonce_local.size());
|
||||
|
||||
output.insert(output.end(), tmp.begin(), tmp.end());
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
void EncryptionManager::generateKeys()
|
||||
|
|
@ -36,7 +42,6 @@ void EncryptionManager::generateKeys()
|
|||
|
||||
//randombytes_buf(key.data(), key.size());
|
||||
crypto_stream_chacha20_ietf_keygen(key.data());
|
||||
randombytes_buf(nonce.data(), nonce.size());
|
||||
|
||||
keyReady = true;
|
||||
}
|
||||
|
|
@ -54,11 +59,7 @@ void EncryptionManager::saveKey(const std::string& path, bool hpp)
|
|||
std::vector<char> keyVec(reinterpret_cast<const char*>(key.data()),
|
||||
reinterpret_cast<const char*>(key.data()) + key.size());
|
||||
|
||||
std::vector<char> nonceVec(reinterpret_cast<const char*>(nonce.data()),
|
||||
reinterpret_cast<const char*>(nonce.data()) + nonce.size());
|
||||
|
||||
const uint64_t crcKey = XXH64(keyVec.data(), keyVec.size(), VERSION);
|
||||
const uint64_t crcNonce = XXH64(nonceVec.data(), nonceVec.size(), VERSION);
|
||||
|
||||
// Zapisz ten œmietnik do pliku KEY
|
||||
std::ofstream file(path + ".key", std::ios::binary);
|
||||
|
|
@ -69,8 +70,6 @@ void EncryptionManager::saveKey(const std::string& path, bool hpp)
|
|||
file.write(reinterpret_cast<const char*>(&time), sizeof(time));
|
||||
file.write(reinterpret_cast<const char*>(keyVec.data()), keyVec.size());
|
||||
file.write(reinterpret_cast<const char*>(&crcKey), sizeof(crcKey));
|
||||
file.write(reinterpret_cast<const char*>(nonceVec.data()), nonceVec.size());
|
||||
file.write(reinterpret_cast<const char*>(&crcNonce), sizeof(crcNonce));
|
||||
|
||||
file.close();
|
||||
|
||||
|
|
@ -80,11 +79,7 @@ void EncryptionManager::saveKey(const std::string& path, bool hpp)
|
|||
// Generowanie pliku nag³ówkowego CPP z kluczem i nonce
|
||||
void EncryptionManager::saveCppHeadFile(const std::string& path)
|
||||
{
|
||||
std::vector<unsigned char> keyVec(key.begin(), key.end());
|
||||
std::vector<unsigned char> nonceVec(nonce.begin(), nonce.end());
|
||||
|
||||
const uint32_t keySize = crypto_stream_chacha20_ietf_KEYBYTES;
|
||||
const uint32_t nonceSize = crypto_stream_chacha20_ietf_NONCEBYTES;
|
||||
|
||||
std::ofstream file(path + ".hpp");
|
||||
|
||||
|
|
@ -102,10 +97,6 @@ void EncryptionManager::saveCppHeadFile(const std::string& path)
|
|||
file << " " << toHex(key.data(), key.size()) << std::endl;
|
||||
file << " };" << std::endl;
|
||||
file << std::endl;
|
||||
file << " // Ci¹g nonce" << std::endl;
|
||||
file << " const std::array<uint8_t, " << nonceSize << "> nonce{" << std::endl;
|
||||
file << " " << toHex(nonce.data(), nonce.size()) << std::endl;
|
||||
file << " }; " << std::endl;
|
||||
file << "} //namespace" << std::endl;
|
||||
|
||||
file.close();
|
||||
|
|
@ -143,19 +134,14 @@ void EncryptionManager::loadKey(const std::string& path)
|
|||
}
|
||||
|
||||
std::vector<char> keyVec(key.size());
|
||||
std::vector<char> nonceVec(nonce.size());
|
||||
uint64_t crcKey;
|
||||
uint64_t crcNonce;
|
||||
|
||||
file.read(reinterpret_cast<char*>(&time), sizeof(time));
|
||||
file.read(keyVec.data(), keyVec.size());
|
||||
file.read(reinterpret_cast<char*>(&crcKey), sizeof(crcKey));
|
||||
file.read(nonceVec.data(), nonceVec.size());
|
||||
file.read(reinterpret_cast<char*>(&crcNonce), sizeof(crcNonce));
|
||||
|
||||
// SprawdŸ integralnoœæ klucza
|
||||
if (XXH64(keyVec.data(), keyVec.size(), VERSION) != crcKey
|
||||
|| XXH64(nonceVec.data(), nonceVec.size(), VERSION) != crcNonce)
|
||||
if (XXH64(keyVec.data(), keyVec.size(), VERSION) != crcKey)
|
||||
{
|
||||
throw std::runtime_error("Key integrity error!");
|
||||
}
|
||||
|
|
@ -164,19 +150,29 @@ void EncryptionManager::loadKey(const std::string& path)
|
|||
|
||||
// Przekonwertuj vector na array
|
||||
key = toArray<crypto_stream_chacha20_ietf_KEYBYTES>(keyVec);
|
||||
nonce = toArray<crypto_stream_chacha20_ietf_NONCEBYTES>(nonceVec);
|
||||
}
|
||||
|
||||
// Deszyfracja
|
||||
std::vector<char> EncryptionManager::decrypt(const std::vector<char>& crypt)
|
||||
{
|
||||
std::vector<char> raw(crypt.size());
|
||||
const size_t cryptoSize = crypto_stream_chacha20_ietf_NONCEBYTES;
|
||||
|
||||
std::array<uint8_t, cryptoSize> nonce_local;
|
||||
std::memcpy(nonce_local.data(),
|
||||
reinterpret_cast<const uint8_t*>(crypt.data()), cryptoSize);
|
||||
|
||||
|
||||
const size_t rawSize = crypt.size() - cryptoSize;
|
||||
std::vector<char> tmp(rawSize);
|
||||
std::memcpy(tmp.data(), crypt.data() + cryptoSize, rawSize);
|
||||
|
||||
std::vector<char> raw(rawSize);
|
||||
|
||||
if (crypto_stream_chacha20_ietf_xor(
|
||||
reinterpret_cast<unsigned char*>(raw.data()),
|
||||
reinterpret_cast<const unsigned char*>(crypt.data()),
|
||||
static_cast<unsigned long long>(crypt.size()),
|
||||
nonce.data(), key.data()) != 0)
|
||||
reinterpret_cast<const unsigned char*>(tmp.data()),
|
||||
static_cast<unsigned long long>(tmp.size()),
|
||||
nonce_local.data(), key.data()) != 0)
|
||||
{
|
||||
throw std::runtime_error("Data decryption error!");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue