util/mkdef.pl: Produce version scripts from unversioned symbols
[openssl.git] / demos / evp / gmac.c
1 /*
2  * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
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.
13  */
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <openssl/bio.h>
18 #include <openssl/evp.h>
19
20 /* AES-GMAC test data from NIST public test vectors */
21
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 };
28
29 static const unsigned char gmac_tag[] = { 0x20, 0x9f, 0xcc, 0x8d, 0x36, 0x75, 0xed, 0x93,
30                0x8e, 0x9c, 0x71, 0x66, 0x70, 0x9d, 0xd9, 0x46 };
31
32 static int aes_gmac(void)
33 {
34     EVP_CIPHER_CTX *ctx;
35     int outlen, tmplen;
36     unsigned char outbuf[1024];
37     int ret = 0;
38
39     printf("AES GMAC:\n");
40     printf("Authenticated Data:\n");
41     BIO_dump_fp(stdout, gmac_aad, sizeof(gmac_aad));
42
43     if ((ctx = EVP_CIPHER_CTX_new()) == NULL) {
44         printf("EVP_CIPHER_CTX_new: failed\n");
45         goto err;
46     }
47
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");
51         goto err;
52     }
53
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),
56                              NULL)) {
57         printf("EVP_CIPHER_CTX_ctrl: set IV length failed\n");
58         goto err;
59     }
60
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");
64         goto err;
65     }
66
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");
70         goto err;
71     }
72
73     /* Finalise: note get no output for GMAC */
74     if (!EVP_EncryptFinal_ex(ctx, outbuf, &outlen)) {
75         printf("EVP_EncryptFinal_ex: failed\n");
76         goto err;
77     }
78
79     /* Get tag */
80     if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16, outbuf)) {
81         printf("EVP_CIPHER_CTX_ctrl: failed\n");
82         goto err;
83     }
84
85     /* Output tag */
86     printf("Tag:\n");
87     BIO_dump_fp(stdout, outbuf, 16);
88
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));
93     } else 
94         ret = 1;
95 err:
96     EVP_CIPHER_CTX_free(ctx);
97     return ret;
98 }
99
100 int main(int argc, char **argv)
101 {
102     return aes_gmac() ? EXIT_SUCCESS : EXIT_FAILURE;
103 }