| 47 | } |
| 48 | |
| 49 | void Aes_ctr_encryptor::process (const unsigned char* in, unsigned char* out, size_t len) |
| 50 | { |
| 51 | for (size_t i = 0; i < len; ++i) { |
| 52 | if (byte_counter % BLOCK_LEN == 0) { |
| 53 | // Set last 4 bytes of CTR to the (big-endian) block number (sequentially increasing with each block) |
| 54 | store_be32(ctr_value + NONCE_LEN, byte_counter / BLOCK_LEN); |
| 55 | |
| 56 | // Generate a new pad |
| 57 | ecb.encrypt(ctr_value, pad); |
| 58 | } |
| 59 | |
| 60 | // encrypt one byte |
| 61 | out[i] = in[i] ^ pad[byte_counter++ % BLOCK_LEN]; |
| 62 | |
| 63 | if (byte_counter == 0) { |
| 64 | throw Crypto_error("Aes_ctr_encryptor::process", "Too much data to encrypt securely"); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Encrypt/decrypt an entire input stream, writing to the given output stream |
| 70 | void Aes_ctr_encryptor::process_stream (std::istream& in, std::ostream& out, const unsigned char* key, const unsigned char* nonce) |
no test coverage detected