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