Path: blob/main/crypto/openssl/demos/cipher/ariacbc.c
34879 views
/*1* Copyright 2012-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* Simple ARIA CBC encryption demonstration program.11*/1213#include <stdio.h>14#include <openssl/err.h>15#include <openssl/bio.h>16#include <openssl/evp.h>17#include <openssl/crypto.h>18#include <openssl/core_names.h>1920/* ARIA key */21static const unsigned char cbc_key[] = {220xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66,230x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69,240xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f25};2627/* Unique initialisation vector */28static const unsigned char cbc_iv[] = {290x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84,300x99, 0xaa, 0x3e, 0x68,31};3233/* Example plaintext to encrypt */34static const unsigned char cbc_pt[] = {350xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea,360xcc, 0x2b, 0xf2, 0xa537};3839/* Expected ciphertext value */40static const unsigned char cbc_ct[] = {410x9a, 0x44, 0xe6, 0x85, 0x94, 0x26, 0xff, 0x30, 0x03, 0xd3, 0x7e, 0xc6,420xb5, 0x4a, 0x09, 0x66, 0x39, 0x28, 0xf3, 0x67, 0x14, 0xbc, 0xe8, 0xe2,430xcf, 0x31, 0xb8, 0x60, 0x42, 0x72, 0x6d, 0xc844};4546/*47* A library context and property query can be used to select & filter48* algorithm implementations. If they are NULL then the default library49* context and properties are used.50*/51static OSSL_LIB_CTX *libctx = NULL;52static const char *propq = NULL;5354static int aria_cbc_encrypt(void)55{56int ret = 0;57EVP_CIPHER_CTX *ctx;58EVP_CIPHER *cipher = NULL;59int outlen, tmplen;60unsigned char outbuf[1024];6162printf("ARIA CBC Encrypt:\n");63printf("Plaintext:\n");64BIO_dump_fp(stdout, cbc_pt, sizeof(cbc_pt));6566/* Create a context for the encrypt operation */67if ((ctx = EVP_CIPHER_CTX_new()) == NULL)68goto err;6970/* Fetch the cipher implementation */71if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)72goto err;7374/*75* Initialise an encrypt operation with the cipher/mode, key and IV.76* We are not setting any custom params so let params be just NULL.77*/78if (!EVP_EncryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))79goto err;8081/* Encrypt plaintext */82if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, cbc_pt, sizeof(cbc_pt)))83goto err;8485/* Finalise: there can be some additional output from padding */86if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))87goto err;88outlen += tmplen;8990/* Output encrypted block */91printf("Ciphertext (outlen:%d):\n", outlen);92BIO_dump_fp(stdout, outbuf, outlen);9394if (sizeof(cbc_ct) == outlen && !CRYPTO_memcmp(outbuf, cbc_ct, outlen))95printf("Final ciphertext matches expected ciphertext\n");96else97printf("Final ciphertext differs from expected ciphertext\n");9899ret = 1;100err:101if (!ret)102ERR_print_errors_fp(stderr);103104EVP_CIPHER_free(cipher);105EVP_CIPHER_CTX_free(ctx);106107return ret;108}109110static int aria_cbc_decrypt(void)111{112int ret = 0;113EVP_CIPHER_CTX *ctx;114EVP_CIPHER *cipher = NULL;115int outlen, tmplen;116unsigned char outbuf[1024];117118printf("ARIA CBC Decrypt:\n");119printf("Ciphertext:\n");120BIO_dump_fp(stdout, cbc_ct, sizeof(cbc_ct));121122if ((ctx = EVP_CIPHER_CTX_new()) == NULL)123goto err;124125/* Fetch the cipher implementation */126if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)127goto err;128129/*130* Initialise an encrypt operation with the cipher/mode, key and IV.131* We are not setting any custom params so let params be just NULL.132*/133if (!EVP_DecryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))134goto err;135136/* Decrypt plaintext */137if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, cbc_ct, sizeof(cbc_ct)))138goto err;139140/* Finalise: there can be some additional output from padding */141if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen))142goto err;143outlen += tmplen;144145/* Output decrypted block */146printf("Plaintext (outlen:%d):\n", outlen);147BIO_dump_fp(stdout, outbuf, outlen);148149if (sizeof(cbc_pt) == outlen && !CRYPTO_memcmp(outbuf, cbc_pt, outlen))150printf("Final plaintext matches original plaintext\n");151else152printf("Final plaintext differs from original plaintext\n");153154ret = 1;155err:156if (!ret)157ERR_print_errors_fp(stderr);158159EVP_CIPHER_free(cipher);160EVP_CIPHER_CTX_free(ctx);161162return ret;163}164165int main(int argc, char **argv)166{167if (!aria_cbc_encrypt())168return EXIT_FAILURE;169170if (!aria_cbc_decrypt())171return EXIT_FAILURE;172173return EXIT_SUCCESS;174}175176177