Aes_ecb_encryptor::Aes_ecb_encryptor (const unsigned char* raw_key) : impl(new Aes_impl) { if (AES_set_encrypt_key(raw_key, KEY_LEN * 8, &(impl->key)) != 0) { throw Crypto_error("Aes_ctr_encryptor::Aes_ctr_encryptor", "AES_set_encrypt_key failed"); } }
Aes_ctr_encryptor::Aes_ctr_encryptor (const unsigned char* raw_key, const unsigned char* arg_nonce) { if (AES_set_encrypt_key(raw_key, KEY_LEN * 8, &key) != 0) { throw Crypto_error("Aes_ctr_encryptor::Aes_ctr_encryptor", "AES_set_encrypt_key failed"); } std::memcpy(nonce, arg_nonce, NONCE_LEN); byte_counter = 0; std::memset(otp, '\0', sizeof(otp)); }
void random_bytes (unsigned char* buffer, size_t len) { if (RAND_bytes(buffer, len) != 1) { std::ostringstream message; while (unsigned long code = ERR_get_error()) { char error_string[120]; ERR_error_string_n(code, error_string, sizeof(error_string)); message << "OpenSSL Error: " << error_string << "; "; } throw Crypto_error("random_bytes", message.str()); } }
void Aes_ctr_encryptor::process (const unsigned char* in, unsigned char* out, size_t len) { for (size_t i = 0; i < len; ++i) { if (byte_counter % BLOCK_LEN == 0) { unsigned char ctr[BLOCK_LEN]; // First 12 bytes of CTR: nonce std::memcpy(ctr, nonce, NONCE_LEN); // Last 4 bytes of CTR: block number (sequentially increasing with each block) (big endian) store_be32(ctr + NONCE_LEN, byte_counter / BLOCK_LEN); // Generate a new OTP AES_encrypt(ctr, otp, &key); } // encrypt one byte out[i] = in[i] ^ otp[byte_counter++ % BLOCK_LEN]; if (byte_counter == 0) { throw Crypto_error("Aes_ctr_encryptor::process", "Too much data to encrypt securely"); } } }