aes.c (9209B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | #include "aes.h" int next_block(int length, size_t block_size) { return (length + block_size) & ~(block_size - 1); } struct bytes pkcs7(const char *data, const size_t data_len, size_t block_size) { size_t res_len = next_block(data_len, block_size); unsigned char *res = (unsigned char *)malloc(res_len * sizeof(unsigned char)); unsigned char padding = res_len - data_len; memcpy(res, data, data_len); for (size_t i = data_len; i < res_len; i++) res[i] = padding; return (struct bytes){res, res_len}; } struct bytes pkcs7_strip(unsigned char *data, size_t data_len) { if (!data_len) return (struct bytes){NULL, 0}; size_t padding = data[data_len - 1]; if (!padding || padding > data_len) { unsigned char *res = (unsigned char *)malloc(data_len * sizeof(unsigned char)); memcpy(res, data, data_len); return (struct bytes){res, data_len}; } for (size_t i = data_len - padding; i < data_len; i++) if (data[i] != padding) { unsigned char *res = (unsigned char *)malloc(data_len * sizeof(unsigned char)); memcpy(res, data, data_len); return (struct bytes){res, data_len}; } size_t res_len = data_len - padding; unsigned char *res = (unsigned char *)malloc(res_len * sizeof(unsigned char)); memcpy(res, data, res_len); return (struct bytes){res, res_len}; } bool pkcs7_validate(unsigned char *data, size_t data_len) { size_t padding = data[data_len - 1]; if (!padding || padding > data_len) return false; for (size_t i = 1; i < padding; i++) if (padding != data[data_len - 1 - i]) return false; return true; } struct bytes pkcs7_validate_strip(unsigned char *data, size_t data_len) { struct bytes res = {NULL, 0}; if (pkcs7_validate(data, data_len)) res = pkcs7_strip(data, data_len); return res; } struct bytes aes_encrypt_ecb(char *data, const size_t data_len, const unsigned char *key, const size_t key_len) { EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); const EVP_CIPHER *cipher; switch (key_len) { case 16: cipher = EVP_aes_128_ecb(); break; case 24: cipher = EVP_aes_192_ecb(); break; case 32: cipher = EVP_aes_256_ecb(); break; default: return (struct bytes){NULL, 0}; } if (EVP_EncryptInit_ex(ctx, cipher, NULL, key, NULL) != 1) { EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } EVP_CIPHER_CTX_set_padding(ctx, 0); struct bytes padded = pkcs7(data, data_len, AES_BLOCK_SIZE); if (!padded.data) { EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } unsigned char *ciphertext = (unsigned char *)malloc(padded.len * sizeof(unsigned char)); int len; if (EVP_EncryptUpdate(ctx, ciphertext, &len, padded.data, padded.len) != 1) { free(padded.data); free(ciphertext); EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } int ciphertext_len = len; if (EVP_EncryptFinal(ctx, ciphertext + len, &len) != 1) { free(padded.data); free(ciphertext); EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } ciphertext_len += len; free(padded.data); EVP_CIPHER_CTX_free(ctx); return (struct bytes){ciphertext, ciphertext_len}; } struct bytes aes_decrypt_ecb(const unsigned char *data, size_t data_len, const unsigned char *key, size_t key_len) { if (data_len == 0 || data_len % AES_BLOCK_SIZE) return (struct bytes){NULL, 0}; EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); const EVP_CIPHER *cipher; switch (key_len) { case 16: cipher = EVP_aes_128_ecb(); break; case 24: cipher = EVP_aes_192_ecb(); break; case 32: cipher = EVP_aes_256_ecb(); break; default: return (struct bytes){NULL, 0}; } if (EVP_DecryptInit_ex(ctx, cipher, NULL, key, NULL) != 1) { EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } EVP_CIPHER_CTX_set_padding(ctx, 0); unsigned char *plaintext = (unsigned char *)malloc(data_len * sizeof(unsigned char)); int len; if (EVP_DecryptUpdate(ctx, plaintext, &len, data, data_len) != 1) { free(plaintext); EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } int plaintext_len = len; if (EVP_DecryptFinal_ex(ctx, plaintext + len, &len) != 1) { free(plaintext); EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } plaintext_len += len; EVP_CIPHER_CTX_free(ctx); struct bytes unpadded = pkcs7_strip(plaintext, plaintext_len); free(plaintext); return unpadded; } struct bytes aes_encrypt_cbc(char *data, const size_t data_len, const unsigned char *key, const size_t key_len, unsigned char *iv) { EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); const EVP_CIPHER *cipher; switch (key_len) { case 16: cipher = EVP_aes_128_cbc(); break; case 24: cipher = EVP_aes_192_cbc(); break; case 32: cipher = EVP_aes_256_cbc(); break; default: return (struct bytes){NULL, 0}; } if (EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv) != 1) { EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } struct bytes padded = pkcs7(data, data_len, AES_BLOCK_SIZE); if (!padded.data) { EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } EVP_CIPHER_CTX_set_padding(ctx, 0); unsigned char *ciphertext = (unsigned char *)malloc(padded.len * sizeof(unsigned char)); int len; if (EVP_EncryptUpdate(ctx, ciphertext, &len, padded.data, padded.len) != 1) { free(padded.data); free(ciphertext); EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } int ciphertext_len = len; if (EVP_EncryptFinal_ex(ctx, ciphertext + len, &len) != 1) { free(padded.data); free(ciphertext); EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } ciphertext_len += len; free(padded.data); EVP_CIPHER_CTX_free(ctx); return (struct bytes){ciphertext, ciphertext_len}; } struct bytes aes_decrypt_cbc(unsigned char *data, size_t data_len, const unsigned char *key, size_t key_len, unsigned char *iv) { if (!data_len || data_len % AES_BLOCK_SIZE) return (struct bytes){NULL, 0}; EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); const EVP_CIPHER *cipher; switch (key_len) { case 16: cipher = EVP_aes_128_cbc(); break; case 24: cipher = EVP_aes_192_cbc(); break; case 32: cipher = EVP_aes_256_cbc(); break; default: return (struct bytes){NULL, 0}; } if (EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv) != 1) { EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } EVP_CIPHER_CTX_set_padding(ctx, 0); unsigned char *plaintext = (unsigned char *)malloc(data_len * sizeof(unsigned char)); int len; if (EVP_DecryptUpdate(ctx, plaintext, &len, data, data_len) != 1) { free(plaintext); EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } int plaintext_len = len; if (EVP_DecryptFinal_ex(ctx, plaintext + len, &len) != 1) { free(plaintext); EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } plaintext_len += len; EVP_CIPHER_CTX_free(ctx); struct bytes res = pkcs7_validate_strip(plaintext, plaintext_len); free(plaintext); return res; } char *aes_oracle(unsigned char *encoded, size_t block_size) { if (!memcmp(encoded + block_size, encoded + block_size * 2, block_size)) return "ECB"; else return "CBC"; } union aes_ctr_counter { unsigned char buf[AES_BLOCK_SIZE]; struct nonce_counter { uint64_t nonce; uint64_t counter; } nc; }; struct bytes aes_ctr(const unsigned char *data, size_t data_len, const unsigned char *key, size_t key_len, const uint64_t nonce) { const EVP_CIPHER *cipher; switch (key_len) { case 16: cipher = EVP_aes_128_ecb(); break; case 24: cipher = EVP_aes_192_ecb(); break; case 32: cipher = EVP_aes_256_ecb(); break; default: return (struct bytes){NULL, 0}; } EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); if (EVP_EncryptInit_ex(ctx, cipher, NULL, key, NULL) != 1) { EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } EVP_CIPHER_CTX_set_padding(ctx, 0); unsigned char *decrypted = (unsigned char *)calloc(data_len + 1, sizeof(unsigned char)); unsigned char keystream[AES_BLOCK_SIZE]; union aes_ctr_counter ctr_counter = {.nc = {.nonce = nonce, .counter = 0}}; for (size_t count = 0; count < data_len; count += AES_BLOCK_SIZE) { size_t block_len = (data_len - count < AES_BLOCK_SIZE) ? data_len - count : AES_BLOCK_SIZE; ctr_counter.nc.counter = count / AES_BLOCK_SIZE; int tmp_len = 0; if (EVP_EncryptUpdate(ctx, keystream, &tmp_len, ctr_counter.buf, AES_BLOCK_SIZE) != 1) { free(decrypted); EVP_CIPHER_CTX_free(ctx); return (struct bytes){NULL, 0}; } for (size_t i = 0; i < block_len; i++) decrypted[count + i] = data[count + i] ^ keystream[i]; } EVP_CIPHER_CTX_free(ctx); return (struct bytes){decrypted, data_len}; } |