Created
April 5, 2020 20:57
-
-
Save DimanNe/bf4cd3df96a89531bf538b2e637cb321 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace std { | |
using gibi = std::ratio<1024LL * 1024 * 1024, 1LL>; // https://en.wikipedia.org/wiki/Binary_prefix | |
} | |
int main() { | |
const ctu::Status status = ct::AeadConfig::Register(); | |
if(!status.ok()) { | |
std::cout << status.error_message() << std::endl; | |
return 0; | |
} | |
// ================================================================================================== | |
// First, given a password, generate a key, as per https://stackoverflow.com/a/52196373/758986 | |
// aka perform "key stretching" | |
const std::string Password = "Password"; | |
const std::string Salt = "1234"; | |
std::string Key; | |
Key.resize(32); | |
// https://crypto.stackexchange.com/questions/35423/appropriate-scrypt-parameters-when-generating-an-scrypt-hash?newreg=c73ca93cedae4729b07a9ab776e4904b | |
const int ScryptResult = EVP_PBE_scrypt(Password.data(), | |
Password.size(), | |
reinterpret_cast<const uint8_t *>(Salt.data()), | |
Salt.size(), | |
1 << 20, // N => 128Nr bytes, 2Nr rounds | |
16, // r | |
1, // uint64_t p, | |
5ull * std::gibi::num, // max_mem | |
reinterpret_cast<uint8_t *>(Key.data()), | |
Key.size()); | |
std::cout << "Scrypt return: " << ScryptResult << "\nKey:\n" << Key << std::endl; | |
// ================================================================================================== | |
// Encrypt | |
ctu::StatusOr<std::unique_ptr<ct::Aead>> StatusOrAead = ct::subtle::AesGcmBoringSsl::New(Key); | |
if(StatusOrAead.ok() == false) { | |
std::cout << StatusOrAead.status().error_message() << std::endl; | |
return 0; | |
} | |
std::unique_ptr<ct::Aead> Aead = std::move(StatusOrAead.ValueOrDie()); | |
const std::string PlainText = "Hello world!"; | |
ctu::StatusOr<std::string> StatusOrCiphertext = Aead->Encrypt(PlainText, {}); | |
if(StatusOrAead.ok() == false) { | |
std::cout << StatusOrCiphertext.status().error_message() << std::endl; | |
return 0; | |
} | |
const std::string Ciphertext = std::move(StatusOrCiphertext.ValueOrDie()); | |
// ================================================================================================== | |
// Decrypt | |
ctu::StatusOr<std::unique_ptr<ct::Aead>> StatusOrAead2 = ct::subtle::AesGcmBoringSsl::New(Key); | |
if(StatusOrAead2.ok() == false) { | |
std::cout << StatusOrAead2.status().error_message() << std::endl; | |
return 0; | |
} | |
std::unique_ptr<ct::Aead> Aead2 = std::move(StatusOrAead2.ValueOrDie()); | |
ctu::StatusOr<std::string> StatusOrPlainText = Aead->Decrypt(Ciphertext, {}); | |
if(StatusOrPlainText.ok() == false) { | |
std::cout << StatusOrPlainText.status().error_message() << std::endl; | |
return 0; | |
} | |
std::cout << "Initial plaintext: " << PlainText << "\n" | |
<< "Restored plaintext: " << StatusOrPlainText.ValueOrDie() << std::endl; | |
// Authenticated Encryption modes | |
// https://blog.cryptographyengineering.com/2012/05/19/how-to-choose-authenticated-encryption/ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment