add simple AES GCM code example
[openssl.git] / demos / evp / aesgcm.c
1 /* Simple AES GCM test program, uses the same NIST data used for the FIPS
2  * self test but uses the application level EVP APIs.
3  */
4 #include <stdio.h>
5 #include <openssl/bio.h>
6 #include <openssl/evp.h>
7
8 /* AES-GCM test data from NIST public test vectors */
9
10 static const unsigned char gcm_key[] = {
11         0xee,0xbc,0x1f,0x57,0x48,0x7f,0x51,0x92,0x1c,0x04,0x65,0x66,
12         0x5f,0x8a,0xe6,0xd1,0x65,0x8b,0xb2,0x6d,0xe6,0xf8,0xa0,0x69,
13         0xa3,0x52,0x02,0x93,0xa5,0x72,0x07,0x8f
14 };
15
16 static const unsigned char gcm_iv[] = {
17         0x99,0xaa,0x3e,0x68,0xed,0x81,0x73,0xa0,0xee,0xd0,0x66,0x84
18 };
19
20 static const unsigned char gcm_pt[] = {
21         0xf5,0x6e,0x87,0x05,0x5b,0xc3,0x2d,0x0e,0xeb,0x31,0xb2,0xea,
22         0xcc,0x2b,0xf2,0xa5
23 };
24
25 static const unsigned char gcm_aad[] = {
26         0x4d,0x23,0xc3,0xce,0xc3,0x34,0xb4,0x9b,0xdb,0x37,0x0c,0x43,
27         0x7f,0xec,0x78,0xde
28 };
29
30 static const unsigned char gcm_ct[] = {
31         0xf7,0x26,0x44,0x13,0xa8,0x4c,0x0e,0x7c,0xd5,0x36,0x86,0x7e,
32         0xb9,0xf2,0x17,0x36
33 };
34
35 static const unsigned char gcm_tag[] = {
36         0x67,0xba,0x05,0x10,0x26,0x2a,0xe4,0x87,0xd7,0x37,0xee,0x62,
37         0x98,0xf7,0x7e,0x0c
38 };
39
40 void aes_gcm_encrypt(void)
41         {
42         EVP_CIPHER_CTX *ctx;
43         int outlen, tmplen;
44         unsigned char outbuf[1024];
45         printf("AES GCM Encrypt:\n");
46         printf("Plaintext:\n");
47         BIO_dump_fp(stdout, gcm_pt, sizeof(gcm_pt));
48         ctx = EVP_CIPHER_CTX_new();
49         /* Set cipher type and mode */
50         EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
51         /* Set IV length if default 96 bits is not appropriate */
52         EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, sizeof(gcm_iv), NULL);
53         /* Initialise key and IV */
54         EVP_EncryptInit_ex(ctx, NULL, NULL, gcm_key, gcm_iv);
55         /* Zero or more calls to specify any AAD */
56         EVP_EncryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad));
57         /* Encrypt plaintext */
58         EVP_EncryptUpdate(ctx, outbuf, &outlen, gcm_pt, sizeof(gcm_pt));
59         /* Output encrypted block */
60         printf("Ciphertext:\n");
61         BIO_dump_fp(stdout, outbuf, outlen);
62         /* Finalise: note get no output for GCM */
63         EVP_EncryptFinal_ex(ctx, outbuf, &outlen);
64         /* Get tag */
65         EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, outbuf);
66         /* Output tag */
67         printf("Tag:\n");
68         BIO_dump_fp(stdout, outbuf, 16);
69         EVP_CIPHER_CTX_free(ctx);
70         }
71
72 void aes_gcm_decrypt(void)
73         {
74         EVP_CIPHER_CTX *ctx;
75         int outlen, tmplen, rv;
76         unsigned char outbuf[1024];
77         printf("AES GCM Derypt:\n");
78         printf("Ciphertext:\n");
79         BIO_dump_fp(stdout, gcm_ct, sizeof(gcm_ct));
80         ctx = EVP_CIPHER_CTX_new();
81         /* Select cipher */
82         EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
83         /* Set IV length, omit for 96 bits */
84         EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, sizeof(gcm_iv), NULL);
85         /* Specify key and IV */
86         EVP_DecryptInit_ex(ctx, NULL, NULL, gcm_key, gcm_iv);
87 #if 0
88         /* Set expected tag value. A restriction in OpenSSL 1.0.1c and earlier
89          * required the tag before any AAD or ciphertext */
90         EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, sizeof(gcm_tag), gcm_tag);
91 #endif
92         /* Zero or more calls to specify any AAD */
93         EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad));
94         /* Decrypt plaintext */
95         EVP_DecryptUpdate(ctx, outbuf, &outlen, gcm_ct, sizeof(gcm_ct));
96         /* Output decrypted block */
97         printf("Plaintext:\n");
98         BIO_dump_fp(stdout, outbuf, outlen);
99         /* Set expected tag value. Works in OpenSSL 1.0.1d and later */
100         EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, sizeof(gcm_tag), gcm_tag);
101         /* Finalise: note get no output for GCM */
102         rv = EVP_DecryptFinal_ex(ctx, outbuf, &outlen);
103         /* Print out return value. If this is not successful authentication
104          * failed and plaintext is not trustworthy.
105          */
106         printf("Tag Verify %s\n", rv > 0 ? "Successful!" : "Failed!");
107         EVP_CIPHER_CTX_free(ctx);
108         }
109
110 int main(int argc, char **argv)
111         {
112         aes_gcm_encrypt();
113         aes_gcm_decrypt();
114         }