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