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