Untitled
unknown
plain_text
8 months ago
1.6 kB
5
Indexable
/* -*-c++-*- */
#include "myccm.hh"
#include <bitset>
bool MyCCM::decrypt(const std::string &ciphertext, std::string &plaintext) {
// Cropping the tag out of the plaintext
std::string tag(ciphertext.end() - mac_tag_length, ciphertext.end());
std::string cropped_cipher = ciphertext.substr(0, ciphertext.size() - mac_tag_length);
BlockA A(l_length, nonce);
++A;
//Decrypt text with XOR
size_t pos = 0;
while(pos < cropped_cipher.length())
{
BlockS keystream;
encrypt_block(A, keystream);
++A;
for(size_t i = 0; i < std::min(Block::size(), cropped_cipher.length() - pos); i++)
{
plaintext += cropped_cipher[pos + i] ^ keystream.data()[i];
}
pos += Block::size();
}
return true;
}
bool MyCCM::encrypt(const std::string &plaintext, std::string &ciphertext) {
//Compute Xn
BlockB B(mac_tag_length, l_length, nonce, plaintext);
BlockX X;
X.type(Block::Type::X);
encrypt_block(B, X);
while(++B)
{
xor_blocks(X, B, X);
encrypt_block(X, X);
}
//Encrypt message
BlockA A(l_length, nonce);
++A;
ciphertext.clear();
size_t pos = 0;
while(pos < plaintext.length())
{
BlockS keystream;
encrypt_block(A, keystream);
++A;
for(size_t i = 0; i < std::min(Block::size(), plaintext.length() - pos); i++)
{
ciphertext += (plaintext[pos + i] ^ keystream.data()[i]);
}
pos += Block::size();
}
//Add MAC tag
BlockS S;
BlockA A_temp(l_length, nonce);
encrypt_block(A_temp, S);
BlockT T(mac_tag_length, S, X);
ciphertext += T.tag();
return true;
}
Editor is loading...
Leave a Comment