Compare commits

...
Author SHA1 Message Date
Adrien Béraud ef0bd6db77 search: return if added node is actually removed 2023-12-08 09:52:20 -05:00
Adrien Béraud 111bfea6f3 build: update version to 3.1.4 2023-12-03 22:09:34 -05:00
Adrien Béraud e35a11a495 tests: add test for aesBuildEncrypted 2023-12-03 22:03:22 -05:00
Adrien Béraud 0592b9ff51 crypto: add aesBuildEncrypted 2023-12-03 22:03:22 -05:00
Adrien Béraud 297e182576 crypto: fix typo in comment 2023-11-28 19:57:01 -05:00
Adrien Béraud b285e6fd2b ci/github: use macOS 12 2023-11-28 18:34:37 -05:00
8 changed files with 57 additions and 9 deletions

No files matched your search

+1 -1
View File
@@ -57,7 +57,7 @@ jobs:
build-macos:
name: macOS/Clang build
runs-on: macos-11
runs-on: macos-12
steps:
- uses: actions/checkout@v3
- name: Dependencies
+1 -1
View File
@@ -15,7 +15,7 @@ include(cmake/CheckAtomic.cmake)
include(CTest)
set (opendht_VERSION_MAJOR 3)
set (opendht_VERSION_MINOR 1.3)
set (opendht_VERSION_MINOR 1.4)
set (opendht_VERSION ${opendht_VERSION_MAJOR}.${opendht_VERSION_MINOR})
set (PACKAGE_VERSION ${opendht_VERSION})
set (VERSION "${opendht_VERSION}")
+1 -1
View File
@@ -1,7 +1,7 @@
dnl define macros
m4_define([opendht_major_version], 3)
m4_define([opendht_minor_version], 1)
m4_define([opendht_patch_version], 3)
m4_define([opendht_patch_version], 4)
m4_define([opendht_version],
[opendht_major_version.opendht_minor_version.opendht_patch_version])
+19 -1
View File
@@ -802,6 +802,12 @@ OPENDHT_PUBLIC Blob aesEncrypt(const uint8_t* data, size_t data_length, const Bl
OPENDHT_PUBLIC inline Blob aesEncrypt(const Blob& data, const Blob& key) {
return aesEncrypt(data.data(), data.size(), key);
}
/**
* AES-GCM encryption with argon2 key derivation.
* This function uses `stretchKey` to generate an AES key from the password and a random salt.
* The result is a bundle including the salt that can be decrypted with `aesDecrypt(data, password)`.
* If needed, the salt or encrypted data can be individually extracted from the bundle with `aesGetSalt` and `aesGetEncrypted`.
*/
OPENDHT_PUBLIC Blob aesEncrypt(const Blob& data, std::string_view password);
/**
@@ -827,12 +833,24 @@ OPENDHT_PUBLIC Blob aesGetSalt(const uint8_t* data, size_t data_length);
OPENDHT_PUBLIC Blob inline aesGetSalt(const Blob& data) {
return aesGetSalt(data.data(), data.size());
}
/** Get the salt part of data password-encrypted with `aesEncrypt(data, password)` */
/** Get the encrypted data part of data password-encrypted with `aesEncrypt(data, password)` */
OPENDHT_PUBLIC std::string_view aesGetEncrypted(const uint8_t* data, size_t data_length);
OPENDHT_PUBLIC std::string_view inline aesGetEncrypted(const Blob& data) {
return aesGetEncrypted(data.data(), data.size());
}
/** Build an encrypted bundle that can be decrypted with aesDecrypt(data, password).
* @param encryptedData: result of `aesEncrypt(data, key)` or `aesGetEncrypted`
* @param salt: should match the encryption key and password so that `stretchKey(password, salk) == key`.
* Can be obtained from an existing bundle with `aesGetSalt`.
**/
OPENDHT_PUBLIC Blob aesBuildEncrypted(const uint8_t* encryptedData, size_t data_length, const Blob& salt);
OPENDHT_PUBLIC Blob inline aesBuildEncrypted(const Blob& encryptedData, const Blob& salt) {
return aesBuildEncrypted(encryptedData.data(), encryptedData.size(), salt);
}
OPENDHT_PUBLIC Blob inline aesBuildEncrypted(std::string_view encryptedData, const Blob& salt) {
return aesBuildEncrypted((const uint8_t*)encryptedData.data(), encryptedData.size(), salt);
}
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
project('opendht', 'c', 'cpp',
version: '3.1.3',
version: '3.1.4',
default_options: [
'cpp_std=c++17',
'warning_level=3'
+10 -3
View File
@@ -107,9 +107,7 @@ Blob aesEncrypt(const Blob& data, std::string_view password)
{
Blob salt;
Blob key = stretchKey(password, salt, 256 / 8);
Blob encrypted = aesEncrypt(data, key);
encrypted.insert(encrypted.begin(), salt.begin(), salt.end());
return encrypted;
return aesBuildEncrypted(aesEncrypt(data, key), salt);
}
Blob aesDecrypt(const uint8_t* data, size_t data_length, const Blob& key)
@@ -174,6 +172,15 @@ std::string_view aesGetEncrypted(const uint8_t* data, size_t data_length)
return std::string_view((const char*)(data+PASSWORD_SALT_LENGTH), data_length - PASSWORD_SALT_LENGTH);
}
Blob aesBuildEncrypted(const uint8_t* data, size_t data_length, const Blob& salt)
{
Blob ret;
ret.reserve(data_length + salt.size());
ret.insert(ret.end(), salt.begin(), salt.end());
ret.insert(ret.end(), data, data + data_length);
return ret;
}
Blob aesGetKey(const uint8_t* data, size_t data_length, std::string_view password)
{
Blob salt = aesGetSalt(data, data_length);
+6 -1
View File
@@ -854,12 +854,17 @@ Dht::Search::insertNode(const Sp<Node>& snode, time_point now, const Blob& token
expired = false;
}
while (nodes.size() - bad > SEARCH_NODES) {
while (nodes.size() - bad > SEARCH_NODES) {
bool removingNode = nodes.back()->node == snode;
if (not expired and nodes.back()->isBad())
bad--;
nodes.pop_back();
if (removingNode)
return false;
}
}
if (n == nodes.end() or not *n)
return false;
if (not token.empty()) {
(*n)->candidate = false;
(*n)->last_get_reply = now;
+18
View File
@@ -208,6 +208,12 @@ void CryptoTester::testAesEncryption() {
CPPUNIT_ASSERT(salt1 != salt2);
auto encrypted11 = dht::crypto::aesBuildEncrypted(encrypted1_data, salt1);
auto encrypted22 = dht::crypto::aesBuildEncrypted(encrypted2_data, salt2);
CPPUNIT_ASSERT(encrypted11 == encrypted1);
CPPUNIT_ASSERT(encrypted22 == encrypted2);
auto key12 = dht::crypto::stretchKey(password, salt1, 256/8);
auto key22 = dht::crypto::stretchKey(password, salt2, 256/8);
@@ -219,6 +225,18 @@ void CryptoTester::testAesEncryption() {
CPPUNIT_ASSERT(data1 == decrypted1);
CPPUNIT_ASSERT(data2 == decrypted2);
auto encrypted12_data = dht::crypto::aesEncrypt(data1, key12);
auto encrypted22_data = dht::crypto::aesEncrypt(data2, key22);
encrypted11 = dht::crypto::aesBuildEncrypted(encrypted12_data, salt1);
encrypted22 = dht::crypto::aesBuildEncrypted(encrypted22_data, salt2);
decrypted1 = dht::crypto::aesDecrypt(encrypted11, password);
decrypted2 = dht::crypto::aesDecrypt(encrypted22, password);
CPPUNIT_ASSERT(data1 == decrypted1);
CPPUNIT_ASSERT(data2 == decrypted2);
}
void