Path: blob/main/crypto/openssl/demos/cipher/ariacbc.c
105641 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,300xaa,310x3e,320x68,330xed,340x81,350x73,360xa0,370xee,380xd0,390x66,400x84,410x99,420xaa,430x3e,440x68,45};4647/* Example plaintext to encrypt */48static const unsigned char cbc_pt[] = {490xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea,500xcc, 0x2b, 0xf2, 0xa551};5253/* Expected ciphertext value */54static const unsigned char cbc_ct[] = {550x9a, 0x44, 0xe6, 0x85, 0x94, 0x26, 0xff, 0x30, 0x03, 0xd3, 0x7e, 0xc6,560xb5, 0x4a, 0x09, 0x66, 0x39, 0x28, 0xf3, 0x67, 0x14, 0xbc, 0xe8, 0xe2,570xcf, 0x31, 0xb8, 0x60, 0x42, 0x72, 0x6d, 0xc858};5960/*61* A library context and property query can be used to select & filter62* algorithm implementations. If they are NULL then the default library63* context and properties are used.64*/65static OSSL_LIB_CTX *libctx = NULL;66static const char *propq = NULL;6768static int aria_cbc_encrypt(void)69{70int ret = 0;71EVP_CIPHER_CTX *ctx;72EVP_CIPHER *cipher = NULL;73int outlen, tmplen;74unsigned char outbuf[1024];7576printf("ARIA CBC Encrypt:\n");77printf("Plaintext:\n");78BIO_dump_fp(stdout, cbc_pt, sizeof(cbc_pt));7980/* Create a context for the encrypt operation */81if ((ctx = EVP_CIPHER_CTX_new()) == NULL)82goto err;8384/* Fetch the cipher implementation */85if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)86goto err;8788/*89* Initialise an encrypt operation with the cipher/mode, key and IV.90* We are not setting any custom params so let params be just NULL.91*/92if (!EVP_EncryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))93goto err;9495/* Encrypt plaintext */96if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, cbc_pt, sizeof(cbc_pt)))97goto err;9899/* Finalise: there can be some additional output from padding */100if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))101goto err;102outlen += tmplen;103104/* Output encrypted block */105printf("Ciphertext (outlen:%d):\n", outlen);106BIO_dump_fp(stdout, outbuf, outlen);107108if (sizeof(cbc_ct) == outlen && !CRYPTO_memcmp(outbuf, cbc_ct, outlen))109printf("Final ciphertext matches expected ciphertext\n");110else111printf("Final ciphertext differs from expected ciphertext\n");112113ret = 1;114err:115if (!ret)116ERR_print_errors_fp(stderr);117118EVP_CIPHER_free(cipher);119EVP_CIPHER_CTX_free(ctx);120121return ret;122}123124static int aria_cbc_decrypt(void)125{126int ret = 0;127EVP_CIPHER_CTX *ctx;128EVP_CIPHER *cipher = NULL;129int outlen, tmplen;130unsigned char outbuf[1024];131132printf("ARIA CBC Decrypt:\n");133printf("Ciphertext:\n");134BIO_dump_fp(stdout, cbc_ct, sizeof(cbc_ct));135136if ((ctx = EVP_CIPHER_CTX_new()) == NULL)137goto err;138139/* Fetch the cipher implementation */140if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)141goto err;142143/*144* Initialise an encrypt operation with the cipher/mode, key and IV.145* We are not setting any custom params so let params be just NULL.146*/147if (!EVP_DecryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))148goto err;149150/* Decrypt plaintext */151if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, cbc_ct, sizeof(cbc_ct)))152goto err;153154/* Finalise: there can be some additional output from padding */155if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen))156goto err;157outlen += tmplen;158159/* Output decrypted block */160printf("Plaintext (outlen:%d):\n", outlen);161BIO_dump_fp(stdout, outbuf, outlen);162163if (sizeof(cbc_pt) == outlen && !CRYPTO_memcmp(outbuf, cbc_pt, outlen))164printf("Final plaintext matches original plaintext\n");165else166printf("Final plaintext differs from original plaintext\n");167168ret = 1;169err:170if (!ret)171ERR_print_errors_fp(stderr);172173EVP_CIPHER_free(cipher);174EVP_CIPHER_CTX_free(ctx);175176return ret;177}178179int main(int argc, char **argv)180{181if (!aria_cbc_encrypt())182return EXIT_FAILURE;183184if (!aria_cbc_decrypt())185return EXIT_FAILURE;186187return EXIT_SUCCESS;188}189190191