1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2012-2022 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert * Simple ARIA CBC encryption demonstration program.
12*e0c4386eSCy Schubert */
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubert #include <stdio.h>
15*e0c4386eSCy Schubert #include <openssl/err.h>
16*e0c4386eSCy Schubert #include <openssl/bio.h>
17*e0c4386eSCy Schubert #include <openssl/evp.h>
18*e0c4386eSCy Schubert #include <openssl/crypto.h>
19*e0c4386eSCy Schubert #include <openssl/core_names.h>
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubert /* ARIA key */
22*e0c4386eSCy Schubert static const unsigned char cbc_key[] = {
23*e0c4386eSCy Schubert 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66,
24*e0c4386eSCy Schubert 0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69,
25*e0c4386eSCy Schubert 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f
26*e0c4386eSCy Schubert };
27*e0c4386eSCy Schubert
28*e0c4386eSCy Schubert /* Unique initialisation vector */
29*e0c4386eSCy Schubert static const unsigned char cbc_iv[] = {
30*e0c4386eSCy Schubert 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84,
31*e0c4386eSCy Schubert 0x99, 0xaa, 0x3e, 0x68,
32*e0c4386eSCy Schubert };
33*e0c4386eSCy Schubert
34*e0c4386eSCy Schubert /* Example plaintext to encrypt */
35*e0c4386eSCy Schubert static const unsigned char cbc_pt[] = {
36*e0c4386eSCy Schubert 0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea,
37*e0c4386eSCy Schubert 0xcc, 0x2b, 0xf2, 0xa5
38*e0c4386eSCy Schubert };
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubert /* Expected ciphertext value */
41*e0c4386eSCy Schubert static const unsigned char cbc_ct[] = {
42*e0c4386eSCy Schubert 0x9a, 0x44, 0xe6, 0x85, 0x94, 0x26, 0xff, 0x30, 0x03, 0xd3, 0x7e, 0xc6,
43*e0c4386eSCy Schubert 0xb5, 0x4a, 0x09, 0x66, 0x39, 0x28, 0xf3, 0x67, 0x14, 0xbc, 0xe8, 0xe2,
44*e0c4386eSCy Schubert 0xcf, 0x31, 0xb8, 0x60, 0x42, 0x72, 0x6d, 0xc8
45*e0c4386eSCy Schubert };
46*e0c4386eSCy Schubert
47*e0c4386eSCy Schubert /*
48*e0c4386eSCy Schubert * A library context and property query can be used to select & filter
49*e0c4386eSCy Schubert * algorithm implementations. If they are NULL then the default library
50*e0c4386eSCy Schubert * context and properties are used.
51*e0c4386eSCy Schubert */
52*e0c4386eSCy Schubert OSSL_LIB_CTX *libctx = NULL;
53*e0c4386eSCy Schubert const char *propq = NULL;
54*e0c4386eSCy Schubert
aria_cbc_encrypt(void)55*e0c4386eSCy Schubert int aria_cbc_encrypt(void)
56*e0c4386eSCy Schubert {
57*e0c4386eSCy Schubert int ret = 0;
58*e0c4386eSCy Schubert EVP_CIPHER_CTX *ctx;
59*e0c4386eSCy Schubert EVP_CIPHER *cipher = NULL;
60*e0c4386eSCy Schubert int outlen, tmplen;
61*e0c4386eSCy Schubert size_t cbc_ivlen = sizeof(cbc_iv);
62*e0c4386eSCy Schubert unsigned char outbuf[1024];
63*e0c4386eSCy Schubert unsigned char outtag[16];
64*e0c4386eSCy Schubert
65*e0c4386eSCy Schubert printf("ARIA CBC Encrypt:\n");
66*e0c4386eSCy Schubert printf("Plaintext:\n");
67*e0c4386eSCy Schubert BIO_dump_fp(stdout, cbc_pt, sizeof(cbc_pt));
68*e0c4386eSCy Schubert
69*e0c4386eSCy Schubert /* Create a context for the encrypt operation */
70*e0c4386eSCy Schubert if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
71*e0c4386eSCy Schubert goto err;
72*e0c4386eSCy Schubert
73*e0c4386eSCy Schubert /* Fetch the cipher implementation */
74*e0c4386eSCy Schubert if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)
75*e0c4386eSCy Schubert goto err;
76*e0c4386eSCy Schubert
77*e0c4386eSCy Schubert /*
78*e0c4386eSCy Schubert * Initialise an encrypt operation with the cipher/mode, key and IV.
79*e0c4386eSCy Schubert * We are not setting any custom params so let params be just NULL.
80*e0c4386eSCy Schubert */
81*e0c4386eSCy Schubert if (!EVP_EncryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))
82*e0c4386eSCy Schubert goto err;
83*e0c4386eSCy Schubert
84*e0c4386eSCy Schubert /* Encrypt plaintext */
85*e0c4386eSCy Schubert if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, cbc_pt, sizeof(cbc_pt)))
86*e0c4386eSCy Schubert goto err;
87*e0c4386eSCy Schubert
88*e0c4386eSCy Schubert /* Finalise: there can be some additional output from padding */
89*e0c4386eSCy Schubert if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))
90*e0c4386eSCy Schubert goto err;
91*e0c4386eSCy Schubert outlen += tmplen;
92*e0c4386eSCy Schubert
93*e0c4386eSCy Schubert /* Output encrypted block */
94*e0c4386eSCy Schubert printf("Ciphertext (outlen:%d):\n", outlen);
95*e0c4386eSCy Schubert BIO_dump_fp(stdout, outbuf, outlen);
96*e0c4386eSCy Schubert
97*e0c4386eSCy Schubert if (sizeof(cbc_ct) == outlen && !CRYPTO_memcmp(outbuf, cbc_ct, outlen))
98*e0c4386eSCy Schubert printf("Final ciphertext matches expected ciphertext\n");
99*e0c4386eSCy Schubert else
100*e0c4386eSCy Schubert printf("Final ciphertext differs from expected ciphertext\n");
101*e0c4386eSCy Schubert
102*e0c4386eSCy Schubert ret = 1;
103*e0c4386eSCy Schubert err:
104*e0c4386eSCy Schubert if (!ret)
105*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
106*e0c4386eSCy Schubert
107*e0c4386eSCy Schubert EVP_CIPHER_free(cipher);
108*e0c4386eSCy Schubert EVP_CIPHER_CTX_free(ctx);
109*e0c4386eSCy Schubert
110*e0c4386eSCy Schubert return ret;
111*e0c4386eSCy Schubert }
112*e0c4386eSCy Schubert
aria_cbc_decrypt(void)113*e0c4386eSCy Schubert int aria_cbc_decrypt(void)
114*e0c4386eSCy Schubert {
115*e0c4386eSCy Schubert int ret = 0;
116*e0c4386eSCy Schubert EVP_CIPHER_CTX *ctx;
117*e0c4386eSCy Schubert EVP_CIPHER *cipher = NULL;
118*e0c4386eSCy Schubert int outlen, tmplen, rv;
119*e0c4386eSCy Schubert size_t cbc_ivlen = sizeof(cbc_iv);
120*e0c4386eSCy Schubert unsigned char outbuf[1024];
121*e0c4386eSCy Schubert
122*e0c4386eSCy Schubert printf("ARIA CBC Decrypt:\n");
123*e0c4386eSCy Schubert printf("Ciphertext:\n");
124*e0c4386eSCy Schubert BIO_dump_fp(stdout, cbc_ct, sizeof(cbc_ct));
125*e0c4386eSCy Schubert
126*e0c4386eSCy Schubert if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
127*e0c4386eSCy Schubert goto err;
128*e0c4386eSCy Schubert
129*e0c4386eSCy Schubert /* Fetch the cipher implementation */
130*e0c4386eSCy Schubert if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)
131*e0c4386eSCy Schubert goto err;
132*e0c4386eSCy Schubert
133*e0c4386eSCy Schubert /*
134*e0c4386eSCy Schubert * Initialise an encrypt operation with the cipher/mode, key and IV.
135*e0c4386eSCy Schubert * We are not setting any custom params so let params be just NULL.
136*e0c4386eSCy Schubert */
137*e0c4386eSCy Schubert if (!EVP_DecryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))
138*e0c4386eSCy Schubert goto err;
139*e0c4386eSCy Schubert
140*e0c4386eSCy Schubert /* Decrypt plaintext */
141*e0c4386eSCy Schubert if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, cbc_ct, sizeof(cbc_ct)))
142*e0c4386eSCy Schubert goto err;
143*e0c4386eSCy Schubert
144*e0c4386eSCy Schubert /* Finalise: there can be some additional output from padding */
145*e0c4386eSCy Schubert if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen))
146*e0c4386eSCy Schubert goto err;
147*e0c4386eSCy Schubert outlen += tmplen;
148*e0c4386eSCy Schubert
149*e0c4386eSCy Schubert /* Output decrypted block */
150*e0c4386eSCy Schubert printf("Plaintext (outlen:%d):\n", outlen);
151*e0c4386eSCy Schubert BIO_dump_fp(stdout, outbuf, outlen);
152*e0c4386eSCy Schubert
153*e0c4386eSCy Schubert if (sizeof(cbc_pt) == outlen && !CRYPTO_memcmp(outbuf, cbc_pt, outlen))
154*e0c4386eSCy Schubert printf("Final plaintext matches original plaintext\n");
155*e0c4386eSCy Schubert else
156*e0c4386eSCy Schubert printf("Final plaintext differs from original plaintext\n");
157*e0c4386eSCy Schubert
158*e0c4386eSCy Schubert ret = 1;
159*e0c4386eSCy Schubert err:
160*e0c4386eSCy Schubert if (!ret)
161*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
162*e0c4386eSCy Schubert
163*e0c4386eSCy Schubert EVP_CIPHER_free(cipher);
164*e0c4386eSCy Schubert EVP_CIPHER_CTX_free(ctx);
165*e0c4386eSCy Schubert
166*e0c4386eSCy Schubert return ret;
167*e0c4386eSCy Schubert }
168*e0c4386eSCy Schubert
main(int argc,char ** argv)169*e0c4386eSCy Schubert int main(int argc, char **argv)
170*e0c4386eSCy Schubert {
171*e0c4386eSCy Schubert if (!aria_cbc_encrypt())
172*e0c4386eSCy Schubert return 1;
173*e0c4386eSCy Schubert
174*e0c4386eSCy Schubert if (!aria_cbc_decrypt())
175*e0c4386eSCy Schubert return 1;
176*e0c4386eSCy Schubert
177*e0c4386eSCy Schubert return 0;
178*e0c4386eSCy Schubert }
179