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