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