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