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