Good morning, I want to test the AES CTR128 encryption of openSSL but infortunately i could not find one. I have tested one but still i got errors in the output of the program. here is the code i tested (i found it on the internet), plz can anyone provide a really simple test program for the counter mode of AES in openSSL, or simply see what s the problem in that source code. Thank you very much for consideration.
#include <stdio.h> #include <string.h> #include < openssl <http://osdir.com/ml/encryption.openssl.devel/2002-07/msg00358.html#>/aes.h> #define KEY_LENGTH 16 #define TEXT_LENGTH 36 #define COUNTER_LENGTH 16 void mirror(const unsigned char *in, unsigned char *out, const unsigned long length, const AES_KEY *key, unsigned char *counter, unsigned int *num) { unsigned int n; unsigned long l=length; unsigned char tmp[AES_BLOCK_SIZE]; memset(tmp, 0xFF, AES_BLOCK_SIZE); } int main(void) { unsigned char key[KEY_LENGTH + 1] = "9dfe5ea4c3f84ae3"; unsigned char plaintext1[(TEXT_LENGTH / 2) + 1] = "123456789012345678"; unsigned char plaintext2[(TEXT_LENGTH / 2) + 1] = "876543210987654321"; unsigned char ciphertext[TEXT_LENGTH + 1]; unsigned char plaintext[TEXT_LENGTH + 1]; unsigned char ecounter[COUNTER_LENGTH + 1] = "6543210987654321"; unsigned char dcounter[COUNTER_LENGTH + 1] = "6543210987654321"; AES_KEY k; int num; int retval; retval = AES_set_encrypt_key(key, strlen(key) * 8, &k); if (retval < 0) { printf("couldn't set encrypt key: %d\n", retval); return -1; } num = 0; AES_ctr128_encrypt(plaintext1, ciphertext, strlen(plaintext1), &k, ecounter, &num); mirror(NULL, NULL, 0, NULL, NULL, NULL); AES_ctr128_encrypt(plaintext2, ciphertext + strlen(plaintext1), strlen(plaintext2), &k, ecounter, &num); num = 0; AES_ctr128_encrypt(ciphertext, plaintext, strlen(plaintext1) + strlen(plaintext2), &k, dcounter, &num); retval = strncmp(plaintext, plaintext1, strlen(plaintext1)); if (retval != 0) { printf("error\n"); return -1; } retval = strncmp(plaintext + strlen(plaintext1), plaintext2, strlen(plaintext2)); if (retval != 0) { printf("error\n"); return -1; } printf("success\n"); return 0; }