|
| 1 | +/* |
| 2 | + * Implement CBC mode |
| 3 | + * |
| 4 | + * CBC mode is a block cipher mode that allows us to encrypt |
| 5 | + * irregularly-sized messages, despite the fact that a block |
| 6 | + * cipher natively only transforms individual blocks. |
| 7 | + * |
| 8 | + * In CBC mode, each ciphertext block is added to the next |
| 9 | + * plaintext block before the next call to the cipher core. |
| 10 | + * |
| 11 | + * The first plaintext block, which has no associated previous |
| 12 | + * ciphertext block, is added to a "fake 0th ciphertext block" |
| 13 | + * called the initialization vector, or IV. |
| 14 | + * |
| 15 | + * Implement CBC mode by hand by taking the ECB function you |
| 16 | + * wrote earlier, making it encrypt instead of decrypt (verify |
| 17 | + * this by decrypting whatever you encrypt to test), and using |
| 18 | + * your XOR function from the previous exercise to combine them. |
| 19 | + * |
| 20 | + * The file here is intelligible (somewhat) when CBC decrypted |
| 21 | + * against "YELLOW SUBMARINE" with an IV of all ASCII 0 |
| 22 | + * (\x00\x00\x00 &c) |
| 23 | + */ |
| 24 | + |
| 25 | +#include <stdio.h> |
| 26 | +#include <string.h> |
| 27 | +#include <stdlib.h> |
| 28 | + |
| 29 | +#include <openssl/conf.h> |
| 30 | +#include <openssl/evp.h> |
| 31 | +#include <openssl/err.h> |
| 32 | + |
| 33 | +#include "utils.h" |
| 34 | +#include "common.h" |
| 35 | + |
| 36 | + |
| 37 | +static const int NO_PADDING = 0; |
| 38 | +static const int BLOCK_SIZE = 16; |
| 39 | + |
| 40 | +void handleErrors(void){ |
| 41 | + ERR_print_errors_fp(stderr); |
| 42 | + abort(); |
| 43 | +} |
| 44 | + |
| 45 | +void aes_encrypt_block(const unsigned char* plaintext, const unsigned char* key, unsigned char* ciphertext){ |
| 46 | + EVP_CIPHER_CTX *ctx; |
| 47 | + int outlen, tmplen; |
| 48 | + |
| 49 | + // Create and initialise the context |
| 50 | + if(!(ctx = EVP_CIPHER_CTX_new())){ |
| 51 | + handleErrors(); |
| 52 | + } |
| 53 | + |
| 54 | + // Initialises context with cipher |
| 55 | + if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, NULL)){ |
| 56 | + handleErrors(); |
| 57 | + } |
| 58 | + |
| 59 | + // Don't PCKS5 pad |
| 60 | + EVP_CIPHER_CTX_set_padding(ctx, NO_PADDING); |
| 61 | + |
| 62 | + // Encrypt plaintext |
| 63 | + if(1 != EVP_EncryptUpdate(ctx, ciphertext, &outlen , plaintext, BLOCK_SIZE)){ |
| 64 | + handleErrors(); |
| 65 | + } |
| 66 | + |
| 67 | + // Finalise the encryption. |
| 68 | + if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + outlen, &tmplen)){ |
| 69 | + handleErrors(); |
| 70 | + } |
| 71 | + |
| 72 | + // Clean up |
| 73 | + EVP_CIPHER_CTX_free(ctx); |
| 74 | +} |
| 75 | + |
| 76 | +void aes_decrypt_block(const unsigned char* ciphertext, const unsigned char* key, unsigned char* plaintext){ |
| 77 | + EVP_CIPHER_CTX *ctx; |
| 78 | + int outlen, tmplen; |
| 79 | + |
| 80 | + // Create and initialise the context |
| 81 | + if(!(ctx = EVP_CIPHER_CTX_new())){ |
| 82 | + handleErrors(); |
| 83 | + } |
| 84 | + |
| 85 | + // Initialises context with cipher |
| 86 | + if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, NULL)){ |
| 87 | + handleErrors(); |
| 88 | + } |
| 89 | + |
| 90 | + // Don't PCKS5 pad |
| 91 | + EVP_CIPHER_CTX_set_padding(ctx, NO_PADDING); |
| 92 | + |
| 93 | + // Encrypt plaintext |
| 94 | + if(1 != EVP_DecryptUpdate(ctx, plaintext, &outlen , ciphertext, BLOCK_SIZE)){ |
| 95 | + handleErrors(); |
| 96 | + } |
| 97 | + |
| 98 | + // Finalise the encryption. |
| 99 | + if(1 != EVP_DecryptFinal_ex(ctx, plaintext + outlen, &tmplen)){ |
| 100 | + handleErrors(); |
| 101 | + } |
| 102 | + |
| 103 | + // Clean up |
| 104 | + EVP_CIPHER_CTX_free(ctx); |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +void test_basic_block_encryption(){ |
| 110 | + unsigned char original[] = "TO BE OR NOT TO "; |
| 111 | + unsigned char key[] = "YELLOW SUBMARINE"; |
| 112 | + |
| 113 | + unsigned char ciphertext[64]; |
| 114 | + unsigned char plaintext[64]; |
| 115 | + memset(ciphertext, 0, 64); |
| 116 | + memset(plaintext, 0, 64); |
| 117 | + |
| 118 | + printf("ORIGINAL: >%s<\n", original); |
| 119 | + printf("ORIGINAL: ");print_hex(original);printf("\n"); |
| 120 | + aes_encrypt_block(original, key, ciphertext); |
| 121 | + aes_decrypt_block(ciphertext, key, plaintext); |
| 122 | + printf("CIPHERTEXT: ");print_hex(ciphertext);printf("\n"); |
| 123 | + printf("PLAINTEXT: ");print_hex(plaintext);printf("\n"); |
| 124 | + printf("PLAINTEXT: >%s<\n", plaintext); |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +void decrypt_ecb( |
| 129 | + const unsigned char* ciphertext, |
| 130 | + const unsigned char* key, |
| 131 | + const int len, |
| 132 | + unsigned char* plaintext |
| 133 | +){ |
| 134 | + for( int offset = 0; offset < len; offset+=BLOCK_SIZE){ |
| 135 | + aes_decrypt_block(ciphertext+offset, key, plaintext+offset); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | + |
| 140 | +int main (int argc, char** agrv){ |
| 141 | + // test_basic_block_encryption(); |
| 142 | + unsigned char * ciphertext = NULL; |
| 143 | + long length = read_to_buffer("aes-test.dat", &ciphertext); |
| 144 | + |
| 145 | + if (!ciphertext){ |
| 146 | + printf("No ciphertext found!\n"); |
| 147 | + return 1; |
| 148 | + } |
| 149 | + |
| 150 | + unsigned char* plaintext = calloc(length, sizeof(unsigned char)+BLOCK_SIZE+BLOCK_SIZE); |
| 151 | + |
| 152 | + if(!plaintext){ |
| 153 | + printf("Calloc returned null for some reason o_O?\n"); |
| 154 | + free(ciphertext); |
| 155 | + return 1; |
| 156 | + } |
| 157 | + |
| 158 | + decrypt_ecb(ciphertext, (unsigned char *)"YELLOW SUBMARINE", length, plaintext); |
| 159 | + printf(">>%s\n", plaintext); |
| 160 | + |
| 161 | + free(plaintext); |
| 162 | + free(ciphertext); |
| 163 | + return 0; |
| 164 | +} |
0 commit comments