Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Aria CBC Test OpenSSL
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/engine.h>
#include <string.h>
#define MAX_MSG_LEN 256
static void handleErrors()
{
printf("ERROR!!\n");
exit(-1);
}
static int encrypt(const EVP_CIPHER *cipher, unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
/*
* Initialise the encryption operation.
*/
if(1 != EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv))
handleErrors();
/*
* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
/*
* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
handleErrors();
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
static int decrypt(const EVP_CIPHER *cipher, unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
/*
* Initialise the decryption operation.
*/
if(1 != EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv))
handleErrors();
/*
* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary.
*/
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;
/*
* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
handleErrors();
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
static void EncDecTest(const EVP_CIPHER *cipher){
int plaintext_len, ciphertext_len, decryptedtext_len;
plaintext_len = 1 + rand() % MAX_MSG_LEN;
unsigned char key[256];
unsigned char iv[16];
unsigned char plaintext[plaintext_len];
unsigned char ciphertext[plaintext_len*2];
unsigned char decryptedtext[plaintext_len];
RAND_bytes(iv, 16);
RAND_bytes(key, 256/8);
RAND_bytes(plaintext, plaintext_len);
printf("======================================================\n");
printf("%s\n",EVP_CIPHER_name(cipher));
printf("Input length: %d\n", plaintext_len);
// printf("Plaintext:\n");
// BIO_dump_fp (stdout, (const char *)plaintext, plaintext_len);
/* Encrypt the plaintext */
ciphertext_len = encrypt (cipher, plaintext, plaintext_len, key, iv, ciphertext);
// printf("Ciphertext:\n");
// BIO_dump_fp (stdout, (const char *)ciphertext, ciphertext_len);
/* Decrypt the ciphertext */
decryptedtext_len = decrypt(cipher, ciphertext, ciphertext_len, key, iv,
decryptedtext);
/* Show the decrypted text */
// printf("Decrypted:\n");
// BIO_dump_fp (stdout, (const char *)decryptedtext, decryptedtext_len);
if(!memcmp(decryptedtext,plaintext,plaintext_len)){
printf("PASSED!\n");
}
else{
printf("FAILED!\n");
}
}
int main (void)
{
srand(time(NULL));
printf("ARIA CBC Test\n");
EncDecTest(EVP_aria_128_cbc());
EncDecTest(EVP_aria_192_cbc());
EncDecTest(EVP_aria_256_cbc());
/* Release the functional reference from ENGINE_init() */
/* Release the structural reference from ENGINE_by_id() */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment