xref: /openssl/demos/cipher/ariacbc.c (revision 7ed6de99)
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, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84,
31     0x99, 0xaa, 0x3e, 0x68,
32 };
33 
34 /* Example plaintext to encrypt */
35 static const unsigned char cbc_pt[] = {
36     0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea,
37     0xcc, 0x2b, 0xf2, 0xa5
38 };
39 
40 /* Expected ciphertext value */
41 static const unsigned char cbc_ct[] = {
42     0x9a, 0x44, 0xe6, 0x85, 0x94, 0x26, 0xff, 0x30, 0x03, 0xd3, 0x7e, 0xc6,
43     0xb5, 0x4a, 0x09, 0x66, 0x39, 0x28, 0xf3, 0x67, 0x14, 0xbc, 0xe8, 0xe2,
44     0xcf, 0x31, 0xb8, 0x60, 0x42, 0x72, 0x6d, 0xc8
45 };
46 
47 /*
48  * A library context and property query can be used to select & filter
49  * algorithm implementations. If they are NULL then the default library
50  * context and properties are used.
51  */
52 static OSSL_LIB_CTX *libctx = NULL;
53 static const char *propq = NULL;
54 
aria_cbc_encrypt(void)55 static int aria_cbc_encrypt(void)
56 {
57     int ret = 0;
58     EVP_CIPHER_CTX *ctx;
59     EVP_CIPHER *cipher = NULL;
60     int outlen, tmplen;
61     unsigned char outbuf[1024];
62 
63     printf("ARIA CBC Encrypt:\n");
64     printf("Plaintext:\n");
65     BIO_dump_fp(stdout, cbc_pt, sizeof(cbc_pt));
66 
67     /* Create a context for the encrypt operation */
68     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
69         goto err;
70 
71     /* Fetch the cipher implementation */
72     if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)
73         goto err;
74 
75     /*
76      * Initialise an encrypt operation with the cipher/mode, key and IV.
77      * We are not setting any custom params so let params be just NULL.
78      */
79     if (!EVP_EncryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))
80         goto err;
81 
82     /* Encrypt plaintext */
83     if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, cbc_pt, sizeof(cbc_pt)))
84         goto err;
85 
86     /* Finalise: there can be some additional output from padding */
87     if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))
88         goto err;
89     outlen += tmplen;
90 
91     /* Output encrypted block */
92     printf("Ciphertext (outlen:%d):\n", outlen);
93     BIO_dump_fp(stdout, outbuf, outlen);
94 
95     if (sizeof(cbc_ct) == outlen && !CRYPTO_memcmp(outbuf, cbc_ct, outlen))
96         printf("Final ciphertext matches expected ciphertext\n");
97     else
98         printf("Final ciphertext differs from expected ciphertext\n");
99 
100     ret = 1;
101 err:
102     if (!ret)
103         ERR_print_errors_fp(stderr);
104 
105     EVP_CIPHER_free(cipher);
106     EVP_CIPHER_CTX_free(ctx);
107 
108     return ret;
109 }
110 
aria_cbc_decrypt(void)111 static int aria_cbc_decrypt(void)
112 {
113     int ret = 0;
114     EVP_CIPHER_CTX *ctx;
115     EVP_CIPHER *cipher = NULL;
116     int outlen, tmplen;
117     unsigned char outbuf[1024];
118 
119     printf("ARIA CBC Decrypt:\n");
120     printf("Ciphertext:\n");
121     BIO_dump_fp(stdout, cbc_ct, sizeof(cbc_ct));
122 
123     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
124         goto err;
125 
126     /* Fetch the cipher implementation */
127     if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)
128         goto err;
129 
130     /*
131      * Initialise an encrypt operation with the cipher/mode, key and IV.
132      * We are not setting any custom params so let params be just NULL.
133      */
134     if (!EVP_DecryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))
135         goto err;
136 
137     /* Decrypt plaintext */
138     if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, cbc_ct, sizeof(cbc_ct)))
139         goto err;
140 
141     /* Finalise: there can be some additional output from padding */
142     if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen))
143         goto err;
144     outlen += tmplen;
145 
146     /* Output decrypted block */
147     printf("Plaintext (outlen:%d):\n", outlen);
148     BIO_dump_fp(stdout, outbuf, outlen);
149 
150     if (sizeof(cbc_pt) == outlen && !CRYPTO_memcmp(outbuf, cbc_pt, outlen))
151         printf("Final plaintext matches original plaintext\n");
152     else
153         printf("Final plaintext differs from original plaintext\n");
154 
155     ret = 1;
156 err:
157     if (!ret)
158         ERR_print_errors_fp(stderr);
159 
160     EVP_CIPHER_free(cipher);
161     EVP_CIPHER_CTX_free(ctx);
162 
163     return ret;
164 }
165 
main(int argc,char ** argv)166 int main(int argc, char **argv)
167 {
168     if (!aria_cbc_encrypt())
169         return EXIT_FAILURE;
170 
171     if (!aria_cbc_decrypt())
172         return EXIT_FAILURE;
173 
174     return EXIT_SUCCESS;
175 }
176