2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Simple AES GMAC test program, uses the same NIST data used for the FIPS
12 * self test but uses the application level EVP APIs.
17 #include <openssl/bio.h>
18 #include <openssl/evp.h>
20 /* AES-GMAC test data from NIST public test vectors */
22 static const unsigned char gmac_key[] = { 0x77, 0xbe, 0x63, 0x70, 0x89, 0x71, 0xc4, 0xe2,
23 0x40, 0xd1, 0xcb, 0x79, 0xe8, 0xd7, 0x7f, 0xeb };
24 static const unsigned char gmac_iv[] = { 0xe0, 0xe0, 0x0f, 0x19, 0xfe, 0xd7, 0xba, 0x01,
25 0x36, 0xa7, 0x97, 0xf3 };
26 static const unsigned char gmac_aad[] = { 0x7a, 0x43, 0xec, 0x1d, 0x9c, 0x0a, 0x5a, 0x78,
27 0xa0, 0xb1, 0x65, 0x33, 0xa6, 0x21, 0x3c, 0xab };
29 static const unsigned char gmac_tag[] = { 0x20, 0x9f, 0xcc, 0x8d, 0x36, 0x75, 0xed, 0x93,
30 0x8e, 0x9c, 0x71, 0x66, 0x70, 0x9d, 0xd9, 0x46 };
32 static int aes_gmac(void)
36 unsigned char outbuf[1024];
39 printf("AES GMAC:\n");
40 printf("Authenticated Data:\n");
41 BIO_dump_fp(stdout, gmac_aad, sizeof(gmac_aad));
43 if ((ctx = EVP_CIPHER_CTX_new()) == NULL) {
44 printf("EVP_CIPHER_CTX_new: failed\n");
48 /* Set cipher type and mode */
49 if (!EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL)) {
50 printf("EVP_EncryptInit_ex: failed\n");
54 /* Set IV length if default 96 bits is not appropriate */
55 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(gmac_iv),
57 printf("EVP_CIPHER_CTX_ctrl: set IV length failed\n");
61 /* Initialise key and IV */
62 if (!EVP_EncryptInit_ex(ctx, NULL, NULL, gmac_key, gmac_iv)) {
63 printf("EVP_EncryptInit_ex: set key and IV failed\n");
67 /* Zero or more calls to specify any AAD */
68 if (!EVP_EncryptUpdate(ctx, NULL, &outlen, gmac_aad, sizeof(gmac_aad))) {
69 printf("EVP_EncryptUpdate: setting AAD failed\n");
73 /* Finalise: note get no output for GMAC */
74 if (!EVP_EncryptFinal_ex(ctx, outbuf, &outlen)) {
75 printf("EVP_EncryptFinal_ex: failed\n");
80 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16, outbuf)) {
81 printf("EVP_CIPHER_CTX_ctrl: failed\n");
87 BIO_dump_fp(stdout, outbuf, 16);
89 /* Is the tag correct? */
90 if (memcmp(outbuf, gmac_tag, sizeof(gmac_tag)) != 0) {
91 printf("Expected:\n");
92 BIO_dump_fp(stdout, gmac_tag, sizeof(gmac_tag));
96 EVP_CIPHER_CTX_free(ctx);
100 int main(int argc, char **argv)
102 return aes_gmac() ? EXIT_SUCCESS : EXIT_FAILURE;