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

NAC-ABE: Duplicated decryption for reused keys #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 25 additions & 12 deletions src/algo/abe-support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,35 @@ ABESupport::decrypt(oabe::OpenABECryptoContext& context, const PublicParams &pub
const PrivateKey &prvKey, CipherText cipherText)
{
try {
// step 0: set up ABE Context
context.importPublicParams(pubParams.m_pub);

// step 1: import prvKey into OpenABE
context.enableKeyManager("user1");
context.importUserKey("key1", prvKey.m_prv);

// step 2: cpDecrypt cipherText.aesKey, which is the encrypted symmetric key
// search in std::map<std::string, std::string> m_decryptedContentKeyMap for encryptedSymmetricKey and get the decrypted symmetric key and skip step 0,1,2 if exists
std::string decryptedSymmetricKey;
std::string encryptedSymmetricKey(reinterpret_cast<char*>(cipherText.m_contentKey->m_encAesKey.data()));
bool result = context.decrypt(encryptedSymmetricKey, cipherText.m_contentKey->m_aesKey);
if (!result) {
BOOST_THROW_EXCEPTION(NacAlgoError("Decryption error!"));
auto it = m_decryptedContentKeyMap.find(encryptedSymmetricKey);
if (it != m_decryptedContentKeyMap.end()) {
// found in cache
decryptedSymmetricKey = m_decryptedContentKeyMap[encryptedSymmetricKey];
} else {
// step 0: set up ABE Context
context.importPublicParams(pubParams.m_pub);

// step 1: import prvKey into OpenABE
context.enableKeyManager("user1");
context.importUserKey("key1", prvKey.m_prv);

// step 2: cpDecrypt cipherText.aesKey, which is the encrypted symmetric key
// not found in cache, decrypt it and cache it
bool result = context.decrypt(encryptedSymmetricKey, cipherText.m_contentKey->m_aesKey);
if (!result)
{
BOOST_THROW_EXCEPTION(NacAlgoError("Decryption error!"));
}
// put the decrypted key in cache
m_decryptedContentKeyMap[encryptedSymmetricKey] = cipherText.m_contentKey->m_aesKey;
decryptedSymmetricKey = cipherText.m_contentKey->m_aesKey;
}

// step 3: use the decrypted symmetricKey to AES cpDecrypt cipherText.m_content
OpenABESymKeyEnc aes(cipherText.m_contentKey->m_aesKey);
OpenABESymKeyEnc aes(decryptedSymmetricKey);
std::string cipherContentStr(reinterpret_cast<char*>(cipherText.m_content.data()));
std::string recoveredContent = aes.decrypt(cipherContentStr);

Expand Down
3 changes: 3 additions & 0 deletions src/algo/abe-support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class ABESupport
private:
static const char *SCHEMA_CPABE;
static const char *SCHEMA_KPABE;
// cache the decrypted AES content key (std::string) in a map and use the encrypted AES content key as the key
std::map<std::string, std::string> m_decryptedContentKeyMap;


private:
ABESupport();
Expand Down
14 changes: 14 additions & 0 deletions src/consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ Consumer::consume(const Interest& dataInterest,
dataCallback, errorCallback, nackMessage, timeoutMessage));
}

void
Consumer::consume(const Name &dataName,
const Block &dataBlock,
const ConsumptionCallback &consumptionCb,
const ErrorCallback &errorCallback)
{
// ready for decryption
if (!readyForDecryption()) {
errorCallback("Public params or private decryption key doesn't exist");
return;
}
decryptContent(dataName, dataBlock, consumptionCb, errorCallback);
}

void
Consumer::setMaxRetries(int maxRetries)
{
Expand Down
17 changes: 17 additions & 0 deletions src/consumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ class Consumer
const ConsumptionCallback& consumptionCb,
const ErrorCallback& errorCallback);

/**
* @brief Consume an encrypted data block
*
* The function will directly use the dataBlock to retrive the CK.
* After decrypting CK with cached DKEY, the CK will be used to cpDecrypt the data packet.
*
* @param dataName The packet name.
* @param Block Its type is ndn::tlv::content
* @param consumptionCb The success callback.
* @param errorCallback The failure callback.
*/
void
consume(const Name& dataName,
const Block& dataBlock,
const ConsumptionCallback& consumptionCb,
const ErrorCallback& errorCallback);

/**
* @brief Set the maximum number of retries for fetching data packets.
* @param maxRetries The maximum number of retries.
Expand Down
Loading