Skip to content

Instantly share code, notes, and snippets.

@sm00th
Created September 19, 2013 12:24
Show Gist options
  • Save sm00th/6622672 to your computer and use it in GitHub Desktop.
Save sm00th/6622672 to your computer and use it in GitHub Desktop.
w2 aes stuff
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
uint8_t cbc_key[] = {0x14, 0x0b, 0x41, 0xb2, 0x2a, 0x29, 0xbe, 0xb4, 0x06, 0x1b, 0xda, 0x66, 0xb6, 0x74, 0x7e, 0x14};
uint8_t cbc_msg1[] = {0x4c, 0xa0, 0x0f, 0xf4, 0xc8, 0x98, 0xd6, 0x1e, 0x1e, 0xdb, 0xf1, 0x80, 0x06, 0x18, 0xfb, 0x28, 0x28, 0xa2, 0x26, 0xd1, 0x60, 0xda, 0xd0, 0x78, 0x83, 0xd0, 0x4e, 0x00, 0x8a, 0x78, 0x97, 0xee, 0x2e, 0x4b, 0x74, 0x65, 0xd5, 0x29, 0x0d, 0x0c, 0x0e, 0x6c, 0x68, 0x22, 0x23, 0x6e, 0x1d, 0xaa, 0xfb, 0x94, 0xff, 0xe0, 0xc5, 0xda, 0x05, 0xd9, 0x47, 0x6b, 0xe0, 0x28, 0xad, 0x7c, 0x1d, 0x81};
uint8_t cbc_msg2[] = {0x5b, 0x68, 0x62, 0x9f, 0xeb, 0x86, 0x06, 0xf9, 0xa6, 0x66, 0x76, 0x70, 0xb7, 0x5b, 0x38, 0xa5, 0xb4, 0x83, 0x2d, 0x0f, 0x26, 0xe1, 0xab, 0x7d, 0xa3, 0x32, 0x49, 0xde, 0x7d, 0x4a, 0xfc, 0x48, 0xe7, 0x13, 0xac, 0x64, 0x6a, 0xce, 0x36, 0xe8, 0x72, 0xad, 0x5f, 0xb8, 0xa5, 0x12, 0x42, 0x8a, 0x6e, 0x21, 0x36, 0x4b, 0x0c, 0x37, 0x4d, 0xf4, 0x55, 0x03, 0x47, 0x3c, 0x52, 0x42, 0xa2, 0x53};
uint8_t ctr_key[] = {0x36, 0xf1, 0x83, 0x57, 0xbe, 0x4d, 0xbd, 0x77, 0xf0, 0x50, 0x51, 0x5c, 0x73, 0xfc, 0xf9, 0xf2};
uint8_t ctr_msg1[] = {0x69, 0xdd, 0xa8, 0x45, 0x5c, 0x7d, 0xd4, 0x25, 0x4b, 0xf3, 0x53, 0xb7, 0x73, 0x30, 0x4e, 0xec, 0x0e, 0xc7, 0x70, 0x23, 0x30, 0x09, 0x8c, 0xe7, 0xf7, 0x52, 0x0d, 0x1c, 0xbb, 0xb2, 0x0f, 0xc3, 0x88, 0xd1, 0xb0, 0xad, 0xb5, 0x05, 0x4d, 0xbd, 0x73, 0x70, 0x84, 0x9d, 0xbf, 0x0b, 0x88, 0xd3, 0x93, 0xf2, 0x52, 0xe7, 0x64, 0xf1, 0xf5, 0xf7, 0xad, 0x97, 0xef, 0x79, 0xd5, 0x9c, 0xe2, 0x9f, 0x5f, 0x51, 0xee, 0xca, 0x32, 0xea, 0xbe, 0xdd, 0x9a, 0xfa, 0x93, 0x29};
uint8_t ctr_msg2[] = {0x77, 0x0b, 0x80, 0x25, 0x9e, 0xc3, 0x3b, 0xeb, 0x25, 0x61, 0x35, 0x8a, 0x9f, 0x2d, 0xc6, 0x17, 0xe4, 0x62, 0x18, 0xc0, 0xa5, 0x3c, 0xbe, 0xca, 0x69, 0x5a, 0xe4, 0x5f, 0xaa, 0x89, 0x52, 0xaa, 0x0e, 0x31, 0x1b, 0xde, 0x9d, 0x4e, 0x01, 0x72, 0x6d, 0x31, 0x84, 0xc3, 0x44, 0x51};
uint8_t cbc_hello[] = {0x67, 0xc6, 0x69, 0x73, 0x51, 0xff, 0x4a, 0xec, 0x29, 0xcd, 0xba, 0xab, 0xf2, 0xfb, 0xe3, 0x46, 0x1a, 0xe0, 0x74, 0x93, 0x49, 0xed, 0x5e, 0x3a, 0xad, 0x85, 0x82, 0xd4, 0x76, 0xf1, 0xd8, 0xf0, 0x83, 0x05, 0xb2, 0xd9, 0x3e, 0x9e, 0xc6, 0x63, 0x91, 0xa2, 0xd3, 0x94, 0x20, 0x6d, 0x1c, 0xec, 0xd7, 0xec, 0x8f, 0x0e, 0x82, 0x04, 0x48, 0x84, 0xda, 0x24, 0x93, 0x9c, 0x5e, 0x35, 0xbc, 0xf5};
uint8_t ctr_hello[] = {0x67, 0xc6, 0x69, 0x73, 0x51, 0xff, 0x4a, 0xec, 0x29, 0xcd, 0xba, 0xab, 0xf2, 0xfb, 0xe3, 0x46, 0x6c, 0x58, 0xe3, 0x8a, 0x3a, 0x54, 0x19, 0x5c, 0xc9, 0xc2, 0xa5, 0xf1, 0x00, 0xa0, 0x69, 0xf6, 0x6b, 0x0c, 0xa3, 0xaf, 0x18, 0xf0, 0x24, 0xbe, 0x3e, 0x36, 0x6f, 0x7d, 0x9d, 0xe3, 0x8f, 0xb1, 0x50, 0x9f, 0x0d, 0x37, 0x7f, 0x33, 0x93, 0x13, 0x7d, 0x17, 0xea, 0xca, 0x63, 0x4f};
void right_xor16(uint8_t *left, uint8_t *right)
{
for(uint8_t i = 0; i < 16; i++) {
right[i] = left[i] ^ right[i];
}
}
void ctr_enc(uint8_t *key, uint8_t *plaintext, uint8_t len)
{
uint8_t iv[AES_BLOCK_SIZE], fiv[AES_BLOCK_SIZE];
uint8_t *cipher = malloc(len + AES_BLOCK_SIZE);
AES_KEY aes_key;
AES_set_encrypt_key(key, 128, &aes_key);
printf("[CTR] Encrypting message of len %d; IV: 0x", len);
for(uint8_t i = 0; i<AES_BLOCK_SIZE; i++) {
uint8_t not_so_random = (uint8_t)random();
printf("%02x", not_so_random);
iv[i] = not_so_random;
}
printf("\n");
memcpy(cipher, iv, AES_BLOCK_SIZE);
for(uint8_t i = 0; i < (len/AES_BLOCK_SIZE) + 1; i++) {
uint8_t tmp_block[AES_BLOCK_SIZE];
AES_encrypt(iv, fiv, &aes_key);
memcpy(tmp_block, plaintext + i*AES_BLOCK_SIZE, AES_BLOCK_SIZE);
right_xor16(fiv, tmp_block);
for(uint8_t j = 0; j<AES_BLOCK_SIZE && i*AES_BLOCK_SIZE + j < len; j++) {
uint8_t pos = AES_BLOCK_SIZE + i * AES_BLOCK_SIZE + j;
cipher[pos] = tmp_block[j];
}
iv[AES_BLOCK_SIZE-1]++; // A little cheat, assuming we will never overflow
// that byte
}
}
memcpy(pblock, cblock, AES_BLOCK_SIZE);
}
printf("HEX: ");
for(uint8_t i = 0; i < cipher_len; i++) {
printf("%02x", cipher[i]);
}
printf("\n\n");
free(cipher);
};
void cbc_dec(uint8_t *key, uint8_t *cipher, uint8_t len)
{
uint8_t pblock[AES_BLOCK_SIZE], cblock[AES_BLOCK_SIZE];
uint8_t *plaintext = malloc(len - AES_BLOCK_SIZE);
AES_KEY aes_key;
AES_set_decrypt_key(key, 128, &aes_key);
memcpy(pblock, cipher, AES_BLOCK_SIZE);
printf("[CBC] Decrypting message of len %d; IV: 0x", len - AES_BLOCK_SIZE);
for(uint8_t i = 0; i<AES_BLOCK_SIZE; i++) {
printf("%02x", pblock[i]);
}
printf("\n");
for(uint8_t i = 1; i < (len/AES_BLOCK_SIZE); i++) {
AES_decrypt(cipher + i*AES_BLOCK_SIZE, cblock, &aes_key);
right_xor16(pblock, cblock);
for(uint8_t j = 0; j<AES_BLOCK_SIZE; j++) {
plaintext[(i-1)*AES_BLOCK_SIZE + j] = cblock[j];
}
memcpy(pblock, cipher + i*AES_BLOCK_SIZE, AES_BLOCK_SIZE);
}
uint8_t padding = plaintext[len - AES_BLOCK_SIZE - 1];
printf("Padding: %d\n", padding);
plaintext[len - AES_BLOCK_SIZE - padding] = '\0';
printf("Decrypted message: %s\n", plaintext);
printf("HEX: ");
for(uint8_t i = 0; i < len - AES_BLOCK_SIZE - padding; i++) {
printf("%02x", plaintext[i]);
}
printf("\n\n");
free(plaintext);
};
int main(int argc, char **argv)
{
cbc_dec(cbc_key, cbc_msg1, sizeof(cbc_msg1));
cbc_dec(cbc_key, cbc_msg2, sizeof(cbc_msg2));
cbc_enc(cbc_key, "Hello there, procrastination is extremely fun.", 46);
cbc_dec(cbc_key, cbc_hello, sizeof(cbc_hello));
ctr_dec(ctr_key, ctr_msg1, sizeof(ctr_msg1));
ctr_dec(ctr_key, ctr_msg2, sizeof(ctr_msg2));
ctr_enc(ctr_key, "Hello there, procrastination is extremely fun.", 46);
ctr_dec(ctr_key, ctr_hello, sizeof(ctr_hello));
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment