Skip to content

Commit

Permalink
crypto: add aesBuildEncrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Béraud committed Dec 4, 2023
1 parent 297e182 commit 0592b9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
18 changes: 18 additions & 0 deletions include/opendht/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down Expand Up @@ -833,6 +839,18 @@ 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);
}

}
}
13 changes: 10 additions & 3 deletions src/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0592b9f

Please sign in to comment.