xref: /netbsd-src/crypto/external/bsd/openssl/dist/demos/mac/cmac-aes256.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1*b0d17251Schristos /*-
2*b0d17251Schristos  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6*b0d17251Schristos  * in the file LICENSE in the source distribution or at
7*b0d17251Schristos  * https://www.openssl.org/source/license.html
8*b0d17251Schristos  */
9*b0d17251Schristos 
10*b0d17251Schristos /*
11*b0d17251Schristos  * Example of using EVP_MAC_ methods to calculate
12*b0d17251Schristos  * a CMAC of static buffers
13*b0d17251Schristos  */
14*b0d17251Schristos 
15*b0d17251Schristos #include <string.h>
16*b0d17251Schristos #include <stdio.h>
17*b0d17251Schristos #include <openssl/crypto.h>
18*b0d17251Schristos #include <openssl/core_names.h>
19*b0d17251Schristos #include <openssl/err.h>
20*b0d17251Schristos #include <openssl/evp.h>
21*b0d17251Schristos #include <openssl/cmac.h>
22*b0d17251Schristos #include <openssl/params.h>
23*b0d17251Schristos 
24*b0d17251Schristos /*
25*b0d17251Schristos  * Hard coding the key into an application is very bad.
26*b0d17251Schristos  * It is done here solely for educational purposes.
27*b0d17251Schristos  */
28*b0d17251Schristos static unsigned char key[] = {
29*b0d17251Schristos     0x6c, 0xde, 0x14, 0xf5, 0xd5, 0x2a, 0x4a, 0xdf,
30*b0d17251Schristos     0x12, 0x39, 0x1e, 0xbf, 0x36, 0xf9, 0x6a, 0x46,
31*b0d17251Schristos     0x48, 0xd0, 0xb6, 0x51, 0x89, 0xfc, 0x24, 0x85,
32*b0d17251Schristos     0xa8, 0x8d, 0xdf, 0x7e, 0x80, 0x14, 0xc8, 0xce,
33*b0d17251Schristos };
34*b0d17251Schristos 
35*b0d17251Schristos static const unsigned char data[] =
36*b0d17251Schristos     "To be, or not to be, that is the question,\n"
37*b0d17251Schristos     "Whether tis nobler in the minde to suffer\n"
38*b0d17251Schristos     "The ſlings and arrowes of outragious fortune,\n"
39*b0d17251Schristos     "Or to take Armes again in a sea of troubles,\n"
40*b0d17251Schristos     "And by opposing, end them, to die to sleep;\n"
41*b0d17251Schristos     "No more, and by a sleep, to say we end\n"
42*b0d17251Schristos     "The heart-ache, and the thousand natural shocks\n"
43*b0d17251Schristos     "That flesh is heir to? tis a consumation\n"
44*b0d17251Schristos     "Devoutly to be wished. To die to sleep,\n"
45*b0d17251Schristos     "To sleepe, perchance to dreame, Aye, there's the rub,\n"
46*b0d17251Schristos     "For in that sleep of death what dreams may come\n"
47*b0d17251Schristos     "When we haue shuffled off this mortal coil\n"
48*b0d17251Schristos     "Must give us pause. There's the respect\n"
49*b0d17251Schristos     "That makes calamity of so long life:\n"
50*b0d17251Schristos     "For who would bear the Ships and Scorns of time,\n"
51*b0d17251Schristos     "The oppressor's wrong, the proud man's Contumely,\n"
52*b0d17251Schristos     "The pangs of dispised love, the Law's delay,\n"
53*b0d17251Schristos ;
54*b0d17251Schristos 
55*b0d17251Schristos /* The known value of the CMAC/AES256 MAC of the above soliloqy */
56*b0d17251Schristos static const unsigned char expected_output[] = {
57*b0d17251Schristos     0x67, 0x92, 0x32, 0x23, 0x50, 0x3d, 0xc5, 0xba,
58*b0d17251Schristos     0x78, 0xd4, 0x6d, 0x63, 0xf2, 0x2b, 0xe9, 0x56,
59*b0d17251Schristos };
60*b0d17251Schristos 
61*b0d17251Schristos /*
62*b0d17251Schristos  * A property query used for selecting the MAC implementation.
63*b0d17251Schristos  */
64*b0d17251Schristos static const char *propq = NULL;
65*b0d17251Schristos 
main(void)66*b0d17251Schristos int main(void)
67*b0d17251Schristos {
68*b0d17251Schristos     int rv = EXIT_FAILURE;
69*b0d17251Schristos     OSSL_LIB_CTX *library_context = NULL;
70*b0d17251Schristos     EVP_MAC *mac = NULL;
71*b0d17251Schristos     EVP_MAC_CTX *mctx = NULL;
72*b0d17251Schristos     unsigned char *out = NULL;
73*b0d17251Schristos     size_t out_len = 0;
74*b0d17251Schristos     OSSL_PARAM params[4], *p = params;
75*b0d17251Schristos     char cipher_name[] = "aes256";
76*b0d17251Schristos 
77*b0d17251Schristos     library_context = OSSL_LIB_CTX_new();
78*b0d17251Schristos     if (library_context == NULL) {
79*b0d17251Schristos         fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
80*b0d17251Schristos         goto end;
81*b0d17251Schristos     }
82*b0d17251Schristos 
83*b0d17251Schristos     /* Fetch the CMAC implementation */
84*b0d17251Schristos     mac = EVP_MAC_fetch(library_context, "CMAC", propq);
85*b0d17251Schristos     if (mac == NULL) {
86*b0d17251Schristos         fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
87*b0d17251Schristos         goto end;
88*b0d17251Schristos     }
89*b0d17251Schristos 
90*b0d17251Schristos     /* Create a context for the CMAC operation */
91*b0d17251Schristos     mctx = EVP_MAC_CTX_new(mac);
92*b0d17251Schristos     if (mctx == NULL) {
93*b0d17251Schristos         fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
94*b0d17251Schristos         goto end;
95*b0d17251Schristos     }
96*b0d17251Schristos 
97*b0d17251Schristos     /* The underlying cipher to be used */
98*b0d17251Schristos     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, cipher_name,
99*b0d17251Schristos                                             sizeof(cipher_name));
100*b0d17251Schristos     *p = OSSL_PARAM_construct_end();
101*b0d17251Schristos 
102*b0d17251Schristos     /* Initialise the CMAC operation */
103*b0d17251Schristos     if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
104*b0d17251Schristos         fprintf(stderr, "EVP_MAC_init() failed\n");
105*b0d17251Schristos         goto end;
106*b0d17251Schristos     }
107*b0d17251Schristos 
108*b0d17251Schristos     /* Make one or more calls to process the data to be authenticated */
109*b0d17251Schristos     if (!EVP_MAC_update(mctx, data, sizeof(data))) {
110*b0d17251Schristos         fprintf(stderr, "EVP_MAC_update() failed\n");
111*b0d17251Schristos         goto end;
112*b0d17251Schristos     }
113*b0d17251Schristos 
114*b0d17251Schristos     /* Make a call to the final with a NULL buffer to get the length of the MAC */
115*b0d17251Schristos     if (!EVP_MAC_final(mctx, NULL, &out_len, 0)) {
116*b0d17251Schristos         fprintf(stderr, "EVP_MAC_final() failed\n");
117*b0d17251Schristos         goto end;
118*b0d17251Schristos     }
119*b0d17251Schristos     out = OPENSSL_malloc(out_len);
120*b0d17251Schristos     if (out == NULL) {
121*b0d17251Schristos         fprintf(stderr, "malloc failed\n");
122*b0d17251Schristos         goto end;
123*b0d17251Schristos     }
124*b0d17251Schristos     /* Make one call to the final to get the MAC */
125*b0d17251Schristos     if (!EVP_MAC_final(mctx, out, &out_len, out_len)) {
126*b0d17251Schristos         fprintf(stderr, "EVP_MAC_final() failed\n");
127*b0d17251Schristos         goto end;
128*b0d17251Schristos     }
129*b0d17251Schristos 
130*b0d17251Schristos     printf("Generated MAC:\n");
131*b0d17251Schristos     BIO_dump_indent_fp(stdout, out, out_len, 2);
132*b0d17251Schristos     putchar('\n');
133*b0d17251Schristos 
134*b0d17251Schristos     if (out_len != sizeof(expected_output)) {
135*b0d17251Schristos         fprintf(stderr, "Generated MAC has an unexpected length\n");
136*b0d17251Schristos         goto end;
137*b0d17251Schristos     }
138*b0d17251Schristos 
139*b0d17251Schristos     if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
140*b0d17251Schristos         fprintf(stderr, "Generated MAC does not match expected value\n");
141*b0d17251Schristos         goto end;
142*b0d17251Schristos     }
143*b0d17251Schristos 
144*b0d17251Schristos     rv = EXIT_SUCCESS;
145*b0d17251Schristos end:
146*b0d17251Schristos     if (rv != EXIT_SUCCESS)
147*b0d17251Schristos         ERR_print_errors_fp(stderr);
148*b0d17251Schristos     /* OpenSSL free functions will ignore NULL arguments */
149*b0d17251Schristos     OPENSSL_free(out);
150*b0d17251Schristos     EVP_MAC_CTX_free(mctx);
151*b0d17251Schristos     EVP_MAC_free(mac);
152*b0d17251Schristos     OSSL_LIB_CTX_free(library_context);
153*b0d17251Schristos     return rv;
154*b0d17251Schristos }
155