xref: /freebsd/crypto/openssl/demos/cipher/aeskeywrap.c (revision 1523ccfd9c8c254f7928143d31c305384b05fd11)
1 /*
2  * Copyright 2022-2026 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 aes wrap 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 /* aes key */
22 static const unsigned char wrap_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 wrap_iv[] = {
30     0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0,
31     0x66, 0x84, 0x99, 0xaa, 0x3e, 0x68
32 };
33 
34 /* Example plaintext to encrypt */
35 static const unsigned char wrap_pt[] = {
36     0xad, 0x4f, 0xc9, 0xfc, 0x77, 0x69, 0xc9, 0xea, 0xfc, 0xdf,
37     0x00, 0xac, 0x34, 0xec, 0x40, 0xbc, 0x28, 0x3f, 0xa4, 0x5e,
38     0xd8, 0x99, 0xe4, 0x5d, 0x5e, 0x7a, 0xc4, 0xe6, 0xca, 0x7b,
39     0xa5, 0xb7
40 };
41 
42 /* Expected ciphertext value */
43 static const unsigned char wrap_ct[] = {
44     0x97, 0x99, 0x55, 0xca, 0xf6, 0x3e, 0x95, 0x54, 0x39, 0xd6,
45     0xaf, 0x63, 0xff, 0x2c, 0xe3, 0x96, 0xf7, 0x0d, 0x2c, 0x9c,
46     0xc7, 0x43, 0xc0, 0xb6, 0x31, 0x43, 0xb9, 0x20, 0xac, 0x6b,
47     0xd3, 0x67, 0xad, 0x01, 0xaf, 0xa7, 0x32, 0x74, 0x26, 0x92
48 };
49 
50 /*
51  * A library context and property query can be used to select & filter
52  * algorithm implementations. If they are NULL then the default library
53  * context and properties are used.
54  */
55 static OSSL_LIB_CTX *libctx = NULL;
56 static const char *propq = NULL;
57 
58 static int aes_wrap_encrypt(void)
59 {
60     int ret = 0;
61     EVP_CIPHER_CTX *ctx;
62     EVP_CIPHER *cipher = NULL;
63     int outlen, tmplen;
64     unsigned char outbuf[1024];
65 
66     printf("aes wrap Encrypt:\n");
67     printf("Plaintext:\n");
68     BIO_dump_fp(stdout, wrap_pt, sizeof(wrap_pt));
69 
70     /* Create a context for the encrypt operation */
71     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
72         goto err;
73 
74     EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
75 
76     /* Fetch the cipher implementation */
77     if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-WRAP", propq)) == NULL)
78         goto err;
79 
80     /*
81      * Initialise an encrypt operation with the cipher/mode, key and IV.
82      * We are not setting any custom params so let params be just NULL.
83      */
84     if (!EVP_EncryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL))
85         goto err;
86 
87     /* Encrypt plaintext */
88     if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, wrap_pt, sizeof(wrap_pt)))
89         goto err;
90 
91     /* Finalise: there can be some additional output from padding */
92     if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))
93         goto err;
94     outlen += tmplen;
95 
96     /* Output encrypted block */
97     printf("Ciphertext (outlen:%d):\n", outlen);
98     BIO_dump_fp(stdout, outbuf, outlen);
99 
100     if (sizeof(wrap_ct) == outlen && !CRYPTO_memcmp(outbuf, wrap_ct, outlen))
101         printf("Final ciphertext matches expected ciphertext\n");
102     else
103         printf("Final ciphertext differs from expected ciphertext\n");
104 
105     ret = 1;
106 err:
107     if (!ret)
108         ERR_print_errors_fp(stderr);
109 
110     EVP_CIPHER_free(cipher);
111     EVP_CIPHER_CTX_free(ctx);
112 
113     return ret;
114 }
115 
116 static int aes_wrap_decrypt(void)
117 {
118     int ret = 0;
119     EVP_CIPHER_CTX *ctx;
120     EVP_CIPHER *cipher = NULL;
121     int outlen, tmplen;
122     unsigned char outbuf[1024];
123 
124     printf("aes wrap Decrypt:\n");
125     printf("Ciphertext:\n");
126     BIO_dump_fp(stdout, wrap_ct, sizeof(wrap_ct));
127 
128     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
129         goto err;
130 
131     EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
132 
133     /* Fetch the cipher implementation */
134     if ((cipher = EVP_CIPHER_fetch(libctx, "aes-256-wrap", propq)) == NULL)
135         goto err;
136 
137     /*
138      * Initialise an encrypt operation with the cipher/mode, key and IV.
139      * We are not setting any custom params so let params be just NULL.
140      */
141     if (!EVP_DecryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL))
142         goto err;
143 
144     /* Decrypt plaintext */
145     if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, wrap_ct, sizeof(wrap_ct)))
146         goto err;
147 
148     /* Finalise: there can be some additional output from padding */
149     if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen))
150         goto err;
151     outlen += tmplen;
152 
153     /* Output decrypted block */
154     printf("Plaintext (outlen:%d):\n", outlen);
155     BIO_dump_fp(stdout, outbuf, outlen);
156 
157     if (sizeof(wrap_pt) == outlen && !CRYPTO_memcmp(outbuf, wrap_pt, outlen))
158         printf("Final plaintext matches original plaintext\n");
159     else
160         printf("Final plaintext differs from original plaintext\n");
161 
162     ret = 1;
163 err:
164     if (!ret)
165         ERR_print_errors_fp(stderr);
166 
167     EVP_CIPHER_free(cipher);
168     EVP_CIPHER_CTX_free(ctx);
169 
170     return ret;
171 }
172 
173 int main(int argc, char **argv)
174 {
175     if (!aes_wrap_encrypt())
176         return EXIT_FAILURE;
177 
178     if (!aes_wrap_decrypt())
179         return EXIT_FAILURE;
180 
181     return EXIT_SUCCESS;
182 }
183