xref: /src/crypto/openssl/test/aesgcmtest.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 #include <openssl/evp.h>
11 #include "testutil.h"
12 
13 static const unsigned char gcm_key[] = {
14     0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66,
15     0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69,
16     0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f
17 };
18 static const unsigned char gcm_iv[] = {
19     0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84
20 };
21 static const unsigned char gcm_pt[] = {
22     0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea,
23     0xcc, 0x2b, 0xf2, 0xa5
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 static const unsigned char gcm_ct[] = {
30     0xf7, 0x26, 0x44, 0x13, 0xa8, 0x4c, 0x0e, 0x7c, 0xd5, 0x36, 0x86, 0x7e,
31     0xb9, 0xf2, 0x17, 0x36
32 };
33 static const unsigned char gcm_tag[] = {
34     0x67, 0xba, 0x05, 0x10, 0x26, 0x2a, 0xe4, 0x87, 0xd7, 0x37, 0xee, 0x62,
35     0x98, 0xf7, 0x7e, 0x0c
36 };
37 
do_encrypt(unsigned char * iv_gen,unsigned char * ct,int * ct_len,unsigned char * tag,int * tag_len)38 static int do_encrypt(unsigned char *iv_gen, unsigned char *ct, int *ct_len,
39     unsigned char *tag, int *tag_len)
40 {
41     int ret = 0;
42     EVP_CIPHER_CTX *ctx = NULL;
43     int outlen;
44     unsigned char outbuf[64];
45 
46     *tag_len = 16;
47     ret = TEST_ptr(ctx = EVP_CIPHER_CTX_new())
48         && TEST_true(EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL,
49                          NULL)
50             > 0)
51         && TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, gcm_key,
52                          iv_gen != NULL ? NULL : gcm_iv)
53             > 0)
54         && TEST_true(EVP_EncryptUpdate(ctx, NULL, &outlen, gcm_aad,
55                          sizeof(gcm_aad))
56             > 0)
57         && TEST_true(EVP_EncryptUpdate(ctx, ct, ct_len, gcm_pt,
58                          sizeof(gcm_pt))
59             > 0)
60         && TEST_true(EVP_EncryptFinal_ex(ctx, outbuf, &outlen) > 0)
61         && TEST_int_eq(EVP_CIPHER_CTX_get_tag_length(ctx), 16)
62         && TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
63                          tag)
64             > 0)
65         && TEST_true(iv_gen == NULL
66             || EVP_CIPHER_CTX_get_original_iv(ctx, iv_gen, 12));
67     EVP_CIPHER_CTX_free(ctx);
68     return ret;
69 }
70 
do_decrypt(const unsigned char * iv,const unsigned char * ct,int ct_len,const unsigned char * tag,int tag_len)71 static int do_decrypt(const unsigned char *iv, const unsigned char *ct,
72     int ct_len, const unsigned char *tag, int tag_len)
73 {
74     int ret = 0;
75     EVP_CIPHER_CTX *ctx = NULL;
76     int outlen, ptlen;
77     unsigned char pt[32];
78     unsigned char outbuf[32];
79 
80     ret = TEST_ptr(ctx = EVP_CIPHER_CTX_new())
81         && TEST_true(EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL,
82                          NULL, NULL)
83             > 0)
84         && TEST_true(EVP_DecryptInit_ex(ctx, NULL, NULL, gcm_key, iv) > 0)
85         && TEST_int_eq(EVP_CIPHER_CTX_get_tag_length(ctx), 16)
86         && TEST_true(EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad,
87                          sizeof(gcm_aad))
88             > 0)
89         && TEST_true(EVP_DecryptUpdate(ctx, pt, &ptlen, ct,
90                          ct_len)
91             > 0)
92         && TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
93                          tag_len, (void *)tag)
94             > 0)
95         && TEST_true(EVP_DecryptFinal_ex(ctx, outbuf, &outlen) > 0)
96         && TEST_mem_eq(gcm_pt, sizeof(gcm_pt), pt, ptlen);
97 
98     EVP_CIPHER_CTX_free(ctx);
99     return ret;
100 }
101 
kat_test(void)102 static int kat_test(void)
103 {
104     unsigned char tag[32];
105     unsigned char ct[32];
106     int ctlen = 0, taglen = 0;
107 
108     return do_encrypt(NULL, ct, &ctlen, tag, &taglen)
109         && TEST_mem_eq(gcm_ct, sizeof(gcm_ct), ct, ctlen)
110         && TEST_mem_eq(gcm_tag, sizeof(gcm_tag), tag, taglen)
111         && do_decrypt(gcm_iv, ct, ctlen, tag, taglen);
112 }
113 
badkeylen_test(void)114 static int badkeylen_test(void)
115 {
116     int ret;
117     EVP_CIPHER_CTX *ctx = NULL;
118     const EVP_CIPHER *cipher;
119 
120     ret = TEST_ptr(cipher = EVP_aes_192_gcm())
121         && TEST_ptr(ctx = EVP_CIPHER_CTX_new())
122         && TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL))
123         && TEST_int_le(EVP_CIPHER_CTX_set_key_length(ctx, 2), 0);
124     EVP_CIPHER_CTX_free(ctx);
125     return ret;
126 }
127 
ivgen_test(void)128 static int ivgen_test(void)
129 {
130     unsigned char iv_gen[16];
131     unsigned char tag[32];
132     unsigned char ct[32];
133     int ctlen = 0, taglen = 0;
134 
135     return do_encrypt(iv_gen, ct, &ctlen, tag, &taglen)
136         && do_decrypt(iv_gen, ct, ctlen, tag, taglen);
137 }
138 
setup_tests(void)139 int setup_tests(void)
140 {
141     ADD_TEST(kat_test);
142     ADD_TEST(badkeylen_test);
143     ADD_TEST(ivgen_test);
144     return 1;
145 }
146