xref: /freebsd/crypto/openssl/demos/mac/gmac.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2021 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 #include <stdio.h>
11*e0c4386eSCy Schubert #include <stdlib.h>
12*e0c4386eSCy Schubert #include <openssl/core_names.h>
13*e0c4386eSCy Schubert #include <openssl/evp.h>
14*e0c4386eSCy Schubert #include <openssl/params.h>
15*e0c4386eSCy Schubert #include <openssl/err.h>
16*e0c4386eSCy Schubert 
17*e0c4386eSCy Schubert /*
18*e0c4386eSCy Schubert  * Taken from NIST's GCM Test Vectors
19*e0c4386eSCy Schubert  * http://csrc.nist.gov/groups/STM/cavp/
20*e0c4386eSCy Schubert  */
21*e0c4386eSCy Schubert 
22*e0c4386eSCy Schubert /*
23*e0c4386eSCy Schubert  * Hard coding the key into an application is very bad.
24*e0c4386eSCy Schubert  * It is done here solely for educational purposes.
25*e0c4386eSCy Schubert  */
26*e0c4386eSCy Schubert static unsigned char key[] = {
27*e0c4386eSCy Schubert     0x77, 0xbe, 0x63, 0x70, 0x89, 0x71, 0xc4, 0xe2,
28*e0c4386eSCy Schubert     0x40, 0xd1, 0xcb, 0x79, 0xe8, 0xd7, 0x7f, 0xeb
29*e0c4386eSCy Schubert };
30*e0c4386eSCy Schubert 
31*e0c4386eSCy Schubert /*
32*e0c4386eSCy Schubert  * The initialisation vector (IV) is better not being hard coded too.
33*e0c4386eSCy Schubert  * Repeating password/IV pairs compromises the integrity of GMAC.
34*e0c4386eSCy Schubert  * The IV is not considered secret information and is safe to store with
35*e0c4386eSCy Schubert  * an encrypted password.
36*e0c4386eSCy Schubert  */
37*e0c4386eSCy Schubert static unsigned char iv[] = {
38*e0c4386eSCy Schubert     0xe0, 0xe0, 0x0f, 0x19, 0xfe, 0xd7, 0xba,
39*e0c4386eSCy Schubert     0x01, 0x36, 0xa7, 0x97, 0xf3
40*e0c4386eSCy Schubert };
41*e0c4386eSCy Schubert 
42*e0c4386eSCy Schubert static unsigned char data[] = {
43*e0c4386eSCy Schubert     0x7a, 0x43, 0xec, 0x1d, 0x9c, 0x0a, 0x5a, 0x78,
44*e0c4386eSCy Schubert     0xa0, 0xb1, 0x65, 0x33, 0xa6, 0x21, 0x3c, 0xab
45*e0c4386eSCy Schubert };
46*e0c4386eSCy Schubert 
47*e0c4386eSCy Schubert static const unsigned char expected_output[] = {
48*e0c4386eSCy Schubert     0x20, 0x9f, 0xcc, 0x8d, 0x36, 0x75, 0xed, 0x93,
49*e0c4386eSCy Schubert     0x8e, 0x9c, 0x71, 0x66, 0x70, 0x9d, 0xd9, 0x46
50*e0c4386eSCy Schubert };
51*e0c4386eSCy Schubert 
52*e0c4386eSCy Schubert /*
53*e0c4386eSCy Schubert  * A property query used for selecting the GMAC implementation and the
54*e0c4386eSCy Schubert  * underlying GCM mode cipher.
55*e0c4386eSCy Schubert  */
56*e0c4386eSCy Schubert static char *propq = NULL;
57*e0c4386eSCy Schubert 
main(int argc,char ** argv)58*e0c4386eSCy Schubert int main(int argc, char **argv)
59*e0c4386eSCy Schubert {
60*e0c4386eSCy Schubert     int rv = EXIT_FAILURE;
61*e0c4386eSCy Schubert     EVP_MAC *mac = NULL;
62*e0c4386eSCy Schubert     EVP_MAC_CTX *mctx = NULL;
63*e0c4386eSCy Schubert     unsigned char out[16];
64*e0c4386eSCy Schubert     OSSL_PARAM params[4], *p = params;
65*e0c4386eSCy Schubert     OSSL_LIB_CTX *library_context = NULL;
66*e0c4386eSCy Schubert     size_t out_len = 0;
67*e0c4386eSCy Schubert 
68*e0c4386eSCy Schubert     library_context = OSSL_LIB_CTX_new();
69*e0c4386eSCy Schubert     if (library_context == NULL) {
70*e0c4386eSCy Schubert         fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
71*e0c4386eSCy Schubert         goto end;
72*e0c4386eSCy Schubert     }
73*e0c4386eSCy Schubert 
74*e0c4386eSCy Schubert     /* Fetch the GMAC implementation */
75*e0c4386eSCy Schubert     mac = EVP_MAC_fetch(library_context, "GMAC", propq);
76*e0c4386eSCy Schubert     if (mac == NULL) {
77*e0c4386eSCy Schubert         fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
78*e0c4386eSCy Schubert         goto end;
79*e0c4386eSCy Schubert     }
80*e0c4386eSCy Schubert 
81*e0c4386eSCy Schubert     /* Create a context for the GMAC operation */
82*e0c4386eSCy Schubert     mctx = EVP_MAC_CTX_new(mac);
83*e0c4386eSCy Schubert     if (mctx == NULL) {
84*e0c4386eSCy Schubert         fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
85*e0c4386eSCy Schubert         goto end;
86*e0c4386eSCy Schubert     }
87*e0c4386eSCy Schubert 
88*e0c4386eSCy Schubert     /* GMAC requries a GCM mode cipher to be specified */
89*e0c4386eSCy Schubert     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
90*e0c4386eSCy Schubert                                             "AES-128-GCM", 0);
91*e0c4386eSCy Schubert 
92*e0c4386eSCy Schubert     /*
93*e0c4386eSCy Schubert      * If a non-default property query is required when fetching the GCM mode
94*e0c4386eSCy Schubert      * cipher, it needs to be specified too.
95*e0c4386eSCy Schubert      */
96*e0c4386eSCy Schubert     if (propq != NULL)
97*e0c4386eSCy Schubert         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
98*e0c4386eSCy Schubert                                                 propq, 0);
99*e0c4386eSCy Schubert 
100*e0c4386eSCy Schubert     /* Set the initialisation vector (IV) */
101*e0c4386eSCy Schubert     *p++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV,
102*e0c4386eSCy Schubert                                              iv, sizeof(iv));
103*e0c4386eSCy Schubert     *p = OSSL_PARAM_construct_end();
104*e0c4386eSCy Schubert 
105*e0c4386eSCy Schubert     /* Initialise the GMAC operation */
106*e0c4386eSCy Schubert     if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
107*e0c4386eSCy Schubert         fprintf(stderr, "EVP_MAC_init() failed\n");
108*e0c4386eSCy Schubert         goto end;
109*e0c4386eSCy Schubert     }
110*e0c4386eSCy Schubert 
111*e0c4386eSCy Schubert     /* Make one or more calls to process the data to be authenticated */
112*e0c4386eSCy Schubert     if (!EVP_MAC_update(mctx, data, sizeof(data))) {
113*e0c4386eSCy Schubert         fprintf(stderr, "EVP_MAC_update() failed\n");
114*e0c4386eSCy Schubert         goto end;
115*e0c4386eSCy Schubert     }
116*e0c4386eSCy Schubert 
117*e0c4386eSCy Schubert     /* Make one call to the final to get the MAC */
118*e0c4386eSCy Schubert     if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) {
119*e0c4386eSCy Schubert         fprintf(stderr, "EVP_MAC_final() failed\n");
120*e0c4386eSCy Schubert         goto end;
121*e0c4386eSCy Schubert     }
122*e0c4386eSCy Schubert 
123*e0c4386eSCy Schubert     printf("Generated MAC:\n");
124*e0c4386eSCy Schubert     BIO_dump_indent_fp(stdout, out, out_len, 2);
125*e0c4386eSCy Schubert     putchar('\n');
126*e0c4386eSCy Schubert 
127*e0c4386eSCy Schubert     if (out_len != sizeof(expected_output)) {
128*e0c4386eSCy Schubert         fprintf(stderr, "Generated MAC has an unexpected length\n");
129*e0c4386eSCy Schubert         goto end;
130*e0c4386eSCy Schubert     }
131*e0c4386eSCy Schubert 
132*e0c4386eSCy Schubert     if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
133*e0c4386eSCy Schubert         fprintf(stderr, "Generated MAC does not match expected value\n");
134*e0c4386eSCy Schubert         goto end;
135*e0c4386eSCy Schubert     }
136*e0c4386eSCy Schubert 
137*e0c4386eSCy Schubert     rv = EXIT_SUCCESS;
138*e0c4386eSCy Schubert end:
139*e0c4386eSCy Schubert     EVP_MAC_CTX_free(mctx);
140*e0c4386eSCy Schubert     EVP_MAC_free(mac);
141*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(library_context);
142*e0c4386eSCy Schubert     if (rv != EXIT_SUCCESS)
143*e0c4386eSCy Schubert         ERR_print_errors_fp(stderr);
144*e0c4386eSCy Schubert     return rv;
145*e0c4386eSCy Schubert }
146