1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2006-2017 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/* The AES_ige_* functions are deprecated, so we suppress warnings about them */
11e1051a39Sopenharmony_ci#define OPENSSL_SUPPRESS_DEPRECATED
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci#include <openssl/crypto.h>
14e1051a39Sopenharmony_ci#include <openssl/aes.h>
15e1051a39Sopenharmony_ci#include <openssl/rand.h>
16e1051a39Sopenharmony_ci#include <stdio.h>
17e1051a39Sopenharmony_ci#include <string.h>
18e1051a39Sopenharmony_ci#include "internal/nelem.h"
19e1051a39Sopenharmony_ci#include "testutil.h"
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ci# define TEST_SIZE       128
24e1051a39Sopenharmony_ci# define BIG_TEST_SIZE 10240
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ci# if BIG_TEST_SIZE < TEST_SIZE
27e1051a39Sopenharmony_ci#  error BIG_TEST_SIZE is smaller than TEST_SIZE
28e1051a39Sopenharmony_ci# endif
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_cistatic unsigned char rkey[16];
31e1051a39Sopenharmony_cistatic unsigned char rkey2[16];
32e1051a39Sopenharmony_cistatic unsigned char plaintext[BIG_TEST_SIZE];
33e1051a39Sopenharmony_cistatic unsigned char saved_iv[AES_BLOCK_SIZE * 4];
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_ci# define MAX_VECTOR_SIZE 64
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_cistruct ige_test {
38e1051a39Sopenharmony_ci    const unsigned char key[16];
39e1051a39Sopenharmony_ci    const unsigned char iv[32];
40e1051a39Sopenharmony_ci    const unsigned char in[MAX_VECTOR_SIZE];
41e1051a39Sopenharmony_ci    const unsigned char out[MAX_VECTOR_SIZE];
42e1051a39Sopenharmony_ci    const size_t length;
43e1051a39Sopenharmony_ci    const int encrypt;
44e1051a39Sopenharmony_ci};
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_cistatic struct ige_test const ige_test_vectors[] = {
47e1051a39Sopenharmony_ci    {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
48e1051a39Sopenharmony_ci      0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, /* key */
49e1051a39Sopenharmony_ci     {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
50e1051a39Sopenharmony_ci      0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
51e1051a39Sopenharmony_ci      0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
52e1051a39Sopenharmony_ci      0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, /* iv */
53e1051a39Sopenharmony_ci     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54e1051a39Sopenharmony_ci      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55e1051a39Sopenharmony_ci      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56e1051a39Sopenharmony_ci      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* in */
57e1051a39Sopenharmony_ci     {0x1a, 0x85, 0x19, 0xa6, 0x55, 0x7b, 0xe6, 0x52,
58e1051a39Sopenharmony_ci      0xe9, 0xda, 0x8e, 0x43, 0xda, 0x4e, 0xf4, 0x45,
59e1051a39Sopenharmony_ci      0x3c, 0xf4, 0x56, 0xb4, 0xca, 0x48, 0x8a, 0xa3,
60e1051a39Sopenharmony_ci      0x83, 0xc7, 0x9c, 0x98, 0xb3, 0x47, 0x97, 0xcb}, /* out */
61e1051a39Sopenharmony_ci     32, AES_ENCRYPT},          /* test vector 0 */
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ci    {{0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
64e1051a39Sopenharmony_ci      0x61, 0x6e, 0x20, 0x69, 0x6d, 0x70, 0x6c, 0x65}, /* key */
65e1051a39Sopenharmony_ci     {0x6d, 0x65, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f,
66e1051a39Sopenharmony_ci      0x6e, 0x20, 0x6f, 0x66, 0x20, 0x49, 0x47, 0x45,
67e1051a39Sopenharmony_ci      0x20, 0x6d, 0x6f, 0x64, 0x65, 0x20, 0x66, 0x6f,
68e1051a39Sopenharmony_ci      0x72, 0x20, 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53}, /* iv */
69e1051a39Sopenharmony_ci     {0x4c, 0x2e, 0x20, 0x4c, 0x65, 0x74, 0x27, 0x73,
70e1051a39Sopenharmony_ci      0x20, 0x68, 0x6f, 0x70, 0x65, 0x20, 0x42, 0x65,
71e1051a39Sopenharmony_ci      0x6e, 0x20, 0x67, 0x6f, 0x74, 0x20, 0x69, 0x74,
72e1051a39Sopenharmony_ci      0x20, 0x72, 0x69, 0x67, 0x68, 0x74, 0x21, 0x0a}, /* in */
73e1051a39Sopenharmony_ci     {0x99, 0x70, 0x64, 0x87, 0xa1, 0xcd, 0xe6, 0x13,
74e1051a39Sopenharmony_ci      0xbc, 0x6d, 0xe0, 0xb6, 0xf2, 0x4b, 0x1c, 0x7a,
75e1051a39Sopenharmony_ci      0xa4, 0x48, 0xc8, 0xb9, 0xc3, 0x40, 0x3e, 0x34,
76e1051a39Sopenharmony_ci      0x67, 0xa8, 0xca, 0xd8, 0x93, 0x40, 0xf5, 0x3b}, /* out */
77e1051a39Sopenharmony_ci     32, AES_DECRYPT},          /* test vector 1 */
78e1051a39Sopenharmony_ci};
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_cistruct bi_ige_test {
81e1051a39Sopenharmony_ci    const unsigned char key1[32];
82e1051a39Sopenharmony_ci    const unsigned char key2[32];
83e1051a39Sopenharmony_ci    const unsigned char iv[64];
84e1051a39Sopenharmony_ci    const unsigned char in[MAX_VECTOR_SIZE];
85e1051a39Sopenharmony_ci    const unsigned char out[MAX_VECTOR_SIZE];
86e1051a39Sopenharmony_ci    const size_t keysize;
87e1051a39Sopenharmony_ci    const size_t length;
88e1051a39Sopenharmony_ci    const int encrypt;
89e1051a39Sopenharmony_ci};
90e1051a39Sopenharmony_ci
91e1051a39Sopenharmony_cistatic struct bi_ige_test const bi_ige_test_vectors[] = {
92e1051a39Sopenharmony_ci    {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
93e1051a39Sopenharmony_ci      0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, /* key1 */
94e1051a39Sopenharmony_ci     {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
95e1051a39Sopenharmony_ci      0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, /* key2 */
96e1051a39Sopenharmony_ci     {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
97e1051a39Sopenharmony_ci      0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
98e1051a39Sopenharmony_ci      0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
99e1051a39Sopenharmony_ci      0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
100e1051a39Sopenharmony_ci      0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
101e1051a39Sopenharmony_ci      0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
102e1051a39Sopenharmony_ci      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
103e1051a39Sopenharmony_ci      0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f}, /* iv */
104e1051a39Sopenharmony_ci     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105e1051a39Sopenharmony_ci      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
106e1051a39Sopenharmony_ci      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
107e1051a39Sopenharmony_ci      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* in */
108e1051a39Sopenharmony_ci     {0x14, 0x40, 0x6f, 0xae, 0xa2, 0x79, 0xf2, 0x56,
109e1051a39Sopenharmony_ci      0x1f, 0x86, 0xeb, 0x3b, 0x7d, 0xff, 0x53, 0xdc,
110e1051a39Sopenharmony_ci      0x4e, 0x27, 0x0c, 0x03, 0xde, 0x7c, 0xe5, 0x16,
111e1051a39Sopenharmony_ci      0x6a, 0x9c, 0x20, 0x33, 0x9d, 0x33, 0xfe, 0x12}, /* out */
112e1051a39Sopenharmony_ci     16, 32, AES_ENCRYPT},      /* test vector 0 */
113e1051a39Sopenharmony_ci    {{0x58, 0x0a, 0x06, 0xe9, 0x97, 0x07, 0x59, 0x5c,
114e1051a39Sopenharmony_ci      0x9e, 0x19, 0xd2, 0xa7, 0xbb, 0x40, 0x2b, 0x7a,
115e1051a39Sopenharmony_ci      0xc7, 0xd8, 0x11, 0x9e, 0x4c, 0x51, 0x35, 0x75,
116e1051a39Sopenharmony_ci      0x64, 0x28, 0x0f, 0x23, 0xad, 0x74, 0xac, 0x37}, /* key1 */
117e1051a39Sopenharmony_ci     {0xd1, 0x80, 0xa0, 0x31, 0x47, 0xa3, 0x11, 0x13,
118e1051a39Sopenharmony_ci      0x86, 0x26, 0x9e, 0x6d, 0xff, 0xaf, 0x72, 0x74,
119e1051a39Sopenharmony_ci      0x5b, 0xa2, 0x35, 0x81, 0xd2, 0xa6, 0x3d, 0x21,
120e1051a39Sopenharmony_ci      0x67, 0x7b, 0x58, 0xa8, 0x18, 0xf9, 0x72, 0xe4}, /* key2 */
121e1051a39Sopenharmony_ci     {0x80, 0x3d, 0xbd, 0x4c, 0xe6, 0x7b, 0x06, 0xa9,
122e1051a39Sopenharmony_ci      0x53, 0x35, 0xd5, 0x7e, 0x71, 0xc1, 0x70, 0x70,
123e1051a39Sopenharmony_ci      0x74, 0x9a, 0x00, 0x28, 0x0c, 0xbf, 0x6c, 0x42,
124e1051a39Sopenharmony_ci      0x9b, 0xa4, 0xdd, 0x65, 0x11, 0x77, 0x7c, 0x67,
125e1051a39Sopenharmony_ci      0xfe, 0x76, 0x0a, 0xf0, 0xd5, 0xc6, 0x6e, 0x6a,
126e1051a39Sopenharmony_ci      0xe7, 0x5e, 0x4c, 0xf2, 0x7e, 0x9e, 0xf9, 0x20,
127e1051a39Sopenharmony_ci      0x0e, 0x54, 0x6f, 0x2d, 0x8a, 0x8d, 0x7e, 0xbd,
128e1051a39Sopenharmony_ci      0x48, 0x79, 0x37, 0x99, 0xff, 0x27, 0x93, 0xa3}, /* iv */
129e1051a39Sopenharmony_ci     {0xf1, 0x54, 0x3d, 0xca, 0xfe, 0xb5, 0xef, 0x1c,
130e1051a39Sopenharmony_ci      0x4f, 0xa6, 0x43, 0xf6, 0xe6, 0x48, 0x57, 0xf0,
131e1051a39Sopenharmony_ci      0xee, 0x15, 0x7f, 0xe3, 0xe7, 0x2f, 0xd0, 0x2f,
132e1051a39Sopenharmony_ci      0x11, 0x95, 0x7a, 0x17, 0x00, 0xab, 0xa7, 0x0b,
133e1051a39Sopenharmony_ci      0xbe, 0x44, 0x09, 0x9c, 0xcd, 0xac, 0xa8, 0x52,
134e1051a39Sopenharmony_ci      0xa1, 0x8e, 0x7b, 0x75, 0xbc, 0xa4, 0x92, 0x5a,
135e1051a39Sopenharmony_ci      0xab, 0x46, 0xd3, 0x3a, 0xa0, 0xd5, 0x35, 0x1c,
136e1051a39Sopenharmony_ci      0x55, 0xa4, 0xb3, 0xa8, 0x40, 0x81, 0xa5, 0x0b}, /* in */
137e1051a39Sopenharmony_ci     {0x42, 0xe5, 0x28, 0x30, 0x31, 0xc2, 0xa0, 0x23,
138e1051a39Sopenharmony_ci      0x68, 0x49, 0x4e, 0xb3, 0x24, 0x59, 0x92, 0x79,
139e1051a39Sopenharmony_ci      0xc1, 0xa5, 0xcc, 0xe6, 0x76, 0x53, 0xb1, 0xcf,
140e1051a39Sopenharmony_ci      0x20, 0x86, 0x23, 0xe8, 0x72, 0x55, 0x99, 0x92,
141e1051a39Sopenharmony_ci      0x0d, 0x16, 0x1c, 0x5a, 0x2f, 0xce, 0xcb, 0x51,
142e1051a39Sopenharmony_ci      0xe2, 0x67, 0xfa, 0x10, 0xec, 0xcd, 0x3d, 0x67,
143e1051a39Sopenharmony_ci      0xa5, 0xe6, 0xf7, 0x31, 0x26, 0xb0, 0x0d, 0x76,
144e1051a39Sopenharmony_ci      0x5e, 0x28, 0xdc, 0x7f, 0x01, 0xc5, 0xa5, 0x4c}, /* out */
145e1051a39Sopenharmony_ci     32, 64, AES_ENCRYPT},      /* test vector 1 */
146e1051a39Sopenharmony_ci
147e1051a39Sopenharmony_ci};
148e1051a39Sopenharmony_ci
149e1051a39Sopenharmony_cistatic int test_ige_vectors(int n)
150e1051a39Sopenharmony_ci{
151e1051a39Sopenharmony_ci    const struct ige_test *const v = &ige_test_vectors[n];
152e1051a39Sopenharmony_ci    AES_KEY key;
153e1051a39Sopenharmony_ci    unsigned char buf[MAX_VECTOR_SIZE];
154e1051a39Sopenharmony_ci    unsigned char iv[AES_BLOCK_SIZE * 2];
155e1051a39Sopenharmony_ci    int testresult = 1;
156e1051a39Sopenharmony_ci
157e1051a39Sopenharmony_ci    if (!TEST_int_le(v->length, MAX_VECTOR_SIZE))
158e1051a39Sopenharmony_ci        return 0;
159e1051a39Sopenharmony_ci
160e1051a39Sopenharmony_ci    if (v->encrypt == AES_ENCRYPT)
161e1051a39Sopenharmony_ci        AES_set_encrypt_key(v->key, 8 * sizeof(v->key), &key);
162e1051a39Sopenharmony_ci    else
163e1051a39Sopenharmony_ci        AES_set_decrypt_key(v->key, 8 * sizeof(v->key), &key);
164e1051a39Sopenharmony_ci    memcpy(iv, v->iv, sizeof(iv));
165e1051a39Sopenharmony_ci    AES_ige_encrypt(v->in, buf, v->length, &key, iv, v->encrypt);
166e1051a39Sopenharmony_ci
167e1051a39Sopenharmony_ci    if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
168e1051a39Sopenharmony_ci        TEST_info("IGE test vector %d failed", n);
169e1051a39Sopenharmony_ci        test_output_memory("key", v->key, sizeof(v->key));
170e1051a39Sopenharmony_ci        test_output_memory("iv", v->iv, sizeof(v->iv));
171e1051a39Sopenharmony_ci        test_output_memory("in", v->in, v->length);
172e1051a39Sopenharmony_ci        testresult = 0;
173e1051a39Sopenharmony_ci    }
174e1051a39Sopenharmony_ci
175e1051a39Sopenharmony_ci    /* try with in == out */
176e1051a39Sopenharmony_ci    memcpy(iv, v->iv, sizeof(iv));
177e1051a39Sopenharmony_ci    memcpy(buf, v->in, v->length);
178e1051a39Sopenharmony_ci    AES_ige_encrypt(buf, buf, v->length, &key, iv, v->encrypt);
179e1051a39Sopenharmony_ci
180e1051a39Sopenharmony_ci    if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
181e1051a39Sopenharmony_ci        TEST_info("IGE test vector %d failed (with in == out)", n);
182e1051a39Sopenharmony_ci        test_output_memory("key", v->key, sizeof(v->key));
183e1051a39Sopenharmony_ci        test_output_memory("iv", v->iv, sizeof(v->iv));
184e1051a39Sopenharmony_ci        test_output_memory("in", v->in, v->length);
185e1051a39Sopenharmony_ci        testresult = 0;
186e1051a39Sopenharmony_ci    }
187e1051a39Sopenharmony_ci
188e1051a39Sopenharmony_ci    return testresult;
189e1051a39Sopenharmony_ci}
190e1051a39Sopenharmony_ci
191e1051a39Sopenharmony_cistatic int test_bi_ige_vectors(int n)
192e1051a39Sopenharmony_ci{
193e1051a39Sopenharmony_ci    const struct bi_ige_test *const v = &bi_ige_test_vectors[n];
194e1051a39Sopenharmony_ci    AES_KEY key1;
195e1051a39Sopenharmony_ci    AES_KEY key2;
196e1051a39Sopenharmony_ci    unsigned char buf[MAX_VECTOR_SIZE];
197e1051a39Sopenharmony_ci
198e1051a39Sopenharmony_ci        if (!TEST_int_le(v->length, MAX_VECTOR_SIZE))
199e1051a39Sopenharmony_ci            return 0;
200e1051a39Sopenharmony_ci
201e1051a39Sopenharmony_ci    if (v->encrypt == AES_ENCRYPT) {
202e1051a39Sopenharmony_ci        AES_set_encrypt_key(v->key1, 8 * v->keysize, &key1);
203e1051a39Sopenharmony_ci        AES_set_encrypt_key(v->key2, 8 * v->keysize, &key2);
204e1051a39Sopenharmony_ci    } else {
205e1051a39Sopenharmony_ci        AES_set_decrypt_key(v->key1, 8 * v->keysize, &key1);
206e1051a39Sopenharmony_ci        AES_set_decrypt_key(v->key2, 8 * v->keysize, &key2);
207e1051a39Sopenharmony_ci    }
208e1051a39Sopenharmony_ci
209e1051a39Sopenharmony_ci    AES_bi_ige_encrypt(v->in, buf, v->length, &key1, &key2, v->iv,
210e1051a39Sopenharmony_ci                       v->encrypt);
211e1051a39Sopenharmony_ci
212e1051a39Sopenharmony_ci    if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
213e1051a39Sopenharmony_ci        test_output_memory("key 1", v->key1, sizeof(v->key1));
214e1051a39Sopenharmony_ci        test_output_memory("key 2", v->key2, sizeof(v->key2));
215e1051a39Sopenharmony_ci        test_output_memory("iv", v->iv, sizeof(v->iv));
216e1051a39Sopenharmony_ci        test_output_memory("in", v->in, v->length);
217e1051a39Sopenharmony_ci        return 0;
218e1051a39Sopenharmony_ci    }
219e1051a39Sopenharmony_ci
220e1051a39Sopenharmony_ci    return 1;
221e1051a39Sopenharmony_ci}
222e1051a39Sopenharmony_ci
223e1051a39Sopenharmony_cistatic int test_ige_enc_dec(void)
224e1051a39Sopenharmony_ci{
225e1051a39Sopenharmony_ci    AES_KEY key;
226e1051a39Sopenharmony_ci    unsigned char iv[AES_BLOCK_SIZE * 4];
227e1051a39Sopenharmony_ci    unsigned char ciphertext[BIG_TEST_SIZE];
228e1051a39Sopenharmony_ci    unsigned char checktext[BIG_TEST_SIZE];
229e1051a39Sopenharmony_ci
230e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
231e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
232e1051a39Sopenharmony_ci    AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE, &key, iv, AES_ENCRYPT);
233e1051a39Sopenharmony_ci
234e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
235e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
236e1051a39Sopenharmony_ci    AES_ige_encrypt(ciphertext, checktext, TEST_SIZE, &key, iv, AES_DECRYPT);
237e1051a39Sopenharmony_ci
238e1051a39Sopenharmony_ci    return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE);
239e1051a39Sopenharmony_ci}
240e1051a39Sopenharmony_ci
241e1051a39Sopenharmony_cistatic int test_ige_enc_chaining(void)
242e1051a39Sopenharmony_ci{
243e1051a39Sopenharmony_ci    AES_KEY key;
244e1051a39Sopenharmony_ci    unsigned char iv[AES_BLOCK_SIZE * 4];
245e1051a39Sopenharmony_ci    unsigned char ciphertext[BIG_TEST_SIZE];
246e1051a39Sopenharmony_ci    unsigned char checktext[BIG_TEST_SIZE];
247e1051a39Sopenharmony_ci
248e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
249e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
250e1051a39Sopenharmony_ci    AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE / 2, &key, iv,
251e1051a39Sopenharmony_ci                    AES_ENCRYPT);
252e1051a39Sopenharmony_ci    AES_ige_encrypt(plaintext + TEST_SIZE / 2,
253e1051a39Sopenharmony_ci                    ciphertext + TEST_SIZE / 2, TEST_SIZE / 2,
254e1051a39Sopenharmony_ci                    &key, iv, AES_ENCRYPT);
255e1051a39Sopenharmony_ci
256e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
257e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
258e1051a39Sopenharmony_ci    AES_ige_encrypt(ciphertext, checktext, TEST_SIZE, &key, iv, AES_DECRYPT);
259e1051a39Sopenharmony_ci
260e1051a39Sopenharmony_ci    return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE);
261e1051a39Sopenharmony_ci}
262e1051a39Sopenharmony_ci
263e1051a39Sopenharmony_cistatic int test_ige_dec_chaining(void)
264e1051a39Sopenharmony_ci{
265e1051a39Sopenharmony_ci    AES_KEY key;
266e1051a39Sopenharmony_ci    unsigned char iv[AES_BLOCK_SIZE * 4];
267e1051a39Sopenharmony_ci    unsigned char ciphertext[BIG_TEST_SIZE];
268e1051a39Sopenharmony_ci    unsigned char checktext[BIG_TEST_SIZE];
269e1051a39Sopenharmony_ci
270e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
271e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
272e1051a39Sopenharmony_ci    AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE / 2, &key, iv,
273e1051a39Sopenharmony_ci                    AES_ENCRYPT);
274e1051a39Sopenharmony_ci    AES_ige_encrypt(plaintext + TEST_SIZE / 2,
275e1051a39Sopenharmony_ci                    ciphertext + TEST_SIZE / 2, TEST_SIZE / 2,
276e1051a39Sopenharmony_ci                    &key, iv, AES_ENCRYPT);
277e1051a39Sopenharmony_ci
278e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
279e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
280e1051a39Sopenharmony_ci    AES_ige_encrypt(ciphertext, checktext, TEST_SIZE / 2, &key, iv,
281e1051a39Sopenharmony_ci                    AES_DECRYPT);
282e1051a39Sopenharmony_ci    AES_ige_encrypt(ciphertext + TEST_SIZE / 2,
283e1051a39Sopenharmony_ci                    checktext + TEST_SIZE / 2, TEST_SIZE / 2, &key, iv,
284e1051a39Sopenharmony_ci                    AES_DECRYPT);
285e1051a39Sopenharmony_ci
286e1051a39Sopenharmony_ci    return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE);
287e1051a39Sopenharmony_ci}
288e1051a39Sopenharmony_ci
289e1051a39Sopenharmony_cistatic int test_ige_garble_forwards(void)
290e1051a39Sopenharmony_ci{
291e1051a39Sopenharmony_ci    AES_KEY key;
292e1051a39Sopenharmony_ci    unsigned char iv[AES_BLOCK_SIZE * 4];
293e1051a39Sopenharmony_ci    unsigned char ciphertext[BIG_TEST_SIZE];
294e1051a39Sopenharmony_ci    unsigned char checktext[BIG_TEST_SIZE];
295e1051a39Sopenharmony_ci    unsigned int n;
296e1051a39Sopenharmony_ci    int testresult = 1;
297e1051a39Sopenharmony_ci    const size_t ctsize = sizeof(checktext);
298e1051a39Sopenharmony_ci    size_t matches;
299e1051a39Sopenharmony_ci
300e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
301e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
302e1051a39Sopenharmony_ci    AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv,
303e1051a39Sopenharmony_ci                    AES_ENCRYPT);
304e1051a39Sopenharmony_ci
305e1051a39Sopenharmony_ci    /* corrupt halfway through */
306e1051a39Sopenharmony_ci    ++ciphertext[sizeof(ciphertext) / 2];
307e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
308e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
309e1051a39Sopenharmony_ci    AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv,
310e1051a39Sopenharmony_ci                    AES_DECRYPT);
311e1051a39Sopenharmony_ci
312e1051a39Sopenharmony_ci    matches = 0;
313e1051a39Sopenharmony_ci    for (n = 0; n < sizeof(checktext); ++n)
314e1051a39Sopenharmony_ci        if (checktext[n] == plaintext[n])
315e1051a39Sopenharmony_ci            ++matches;
316e1051a39Sopenharmony_ci
317e1051a39Sopenharmony_ci    /* Fail if there is more than 51% matching bytes */
318e1051a39Sopenharmony_ci    if (!TEST_size_t_le(matches, ctsize / 2 + ctsize / 100))
319e1051a39Sopenharmony_ci        testresult = 0;
320e1051a39Sopenharmony_ci
321e1051a39Sopenharmony_ci    /* Fail if the garble goes backwards */
322e1051a39Sopenharmony_ci    if (!TEST_size_t_gt(matches, ctsize / 2))
323e1051a39Sopenharmony_ci        testresult = 0;
324e1051a39Sopenharmony_ci    return testresult;
325e1051a39Sopenharmony_ci}
326e1051a39Sopenharmony_ci
327e1051a39Sopenharmony_cistatic int test_bi_ige_enc_dec(void)
328e1051a39Sopenharmony_ci{
329e1051a39Sopenharmony_ci    AES_KEY key, key2;
330e1051a39Sopenharmony_ci    unsigned char iv[AES_BLOCK_SIZE * 4];
331e1051a39Sopenharmony_ci    unsigned char ciphertext[BIG_TEST_SIZE];
332e1051a39Sopenharmony_ci    unsigned char checktext[BIG_TEST_SIZE];
333e1051a39Sopenharmony_ci
334e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
335e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
336e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
337e1051a39Sopenharmony_ci    AES_bi_ige_encrypt(plaintext, ciphertext, TEST_SIZE, &key, &key2, iv,
338e1051a39Sopenharmony_ci                       AES_ENCRYPT);
339e1051a39Sopenharmony_ci
340e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
341e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
342e1051a39Sopenharmony_ci    AES_bi_ige_encrypt(ciphertext, checktext, TEST_SIZE, &key, &key2, iv,
343e1051a39Sopenharmony_ci                       AES_DECRYPT);
344e1051a39Sopenharmony_ci
345e1051a39Sopenharmony_ci    return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE);
346e1051a39Sopenharmony_ci}
347e1051a39Sopenharmony_ci
348e1051a39Sopenharmony_cistatic int test_bi_ige_garble1(void)
349e1051a39Sopenharmony_ci{
350e1051a39Sopenharmony_ci    AES_KEY key, key2;
351e1051a39Sopenharmony_ci    unsigned char iv[AES_BLOCK_SIZE * 4];
352e1051a39Sopenharmony_ci    unsigned char ciphertext[BIG_TEST_SIZE];
353e1051a39Sopenharmony_ci    unsigned char checktext[BIG_TEST_SIZE];
354e1051a39Sopenharmony_ci    unsigned int n;
355e1051a39Sopenharmony_ci    size_t matches;
356e1051a39Sopenharmony_ci
357e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
358e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
359e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
360e1051a39Sopenharmony_ci    AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv,
361e1051a39Sopenharmony_ci                    AES_ENCRYPT);
362e1051a39Sopenharmony_ci
363e1051a39Sopenharmony_ci    /* corrupt halfway through */
364e1051a39Sopenharmony_ci    ++ciphertext[sizeof(ciphertext) / 2];
365e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
366e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
367e1051a39Sopenharmony_ci    AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv,
368e1051a39Sopenharmony_ci                    AES_DECRYPT);
369e1051a39Sopenharmony_ci
370e1051a39Sopenharmony_ci    matches = 0;
371e1051a39Sopenharmony_ci    for (n = 0; n < sizeof(checktext); ++n)
372e1051a39Sopenharmony_ci        if (checktext[n] == plaintext[n])
373e1051a39Sopenharmony_ci            ++matches;
374e1051a39Sopenharmony_ci
375e1051a39Sopenharmony_ci    /* Fail if there is more than 1% matching bytes */
376e1051a39Sopenharmony_ci    return TEST_size_t_le(matches, sizeof(checktext) / 100);
377e1051a39Sopenharmony_ci}
378e1051a39Sopenharmony_ci
379e1051a39Sopenharmony_cistatic int test_bi_ige_garble2(void)
380e1051a39Sopenharmony_ci{
381e1051a39Sopenharmony_ci    AES_KEY key, key2;
382e1051a39Sopenharmony_ci    unsigned char iv[AES_BLOCK_SIZE * 4];
383e1051a39Sopenharmony_ci    unsigned char ciphertext[BIG_TEST_SIZE];
384e1051a39Sopenharmony_ci    unsigned char checktext[BIG_TEST_SIZE];
385e1051a39Sopenharmony_ci    unsigned int n;
386e1051a39Sopenharmony_ci    size_t matches;
387e1051a39Sopenharmony_ci
388e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
389e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
390e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
391e1051a39Sopenharmony_ci    AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv,
392e1051a39Sopenharmony_ci                    AES_ENCRYPT);
393e1051a39Sopenharmony_ci
394e1051a39Sopenharmony_ci    /* corrupt right at the end */
395e1051a39Sopenharmony_ci    ++ciphertext[sizeof(ciphertext) - 1];
396e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
397e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
398e1051a39Sopenharmony_ci    AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv,
399e1051a39Sopenharmony_ci                    AES_DECRYPT);
400e1051a39Sopenharmony_ci
401e1051a39Sopenharmony_ci    matches = 0;
402e1051a39Sopenharmony_ci    for (n = 0; n < sizeof(checktext); ++n)
403e1051a39Sopenharmony_ci        if (checktext[n] == plaintext[n])
404e1051a39Sopenharmony_ci            ++matches;
405e1051a39Sopenharmony_ci
406e1051a39Sopenharmony_ci    /* Fail if there is more than 1% matching bytes */
407e1051a39Sopenharmony_ci    return TEST_size_t_le(matches, sizeof(checktext) / 100);
408e1051a39Sopenharmony_ci}
409e1051a39Sopenharmony_ci
410e1051a39Sopenharmony_cistatic int test_bi_ige_garble3(void)
411e1051a39Sopenharmony_ci{
412e1051a39Sopenharmony_ci    AES_KEY key, key2;
413e1051a39Sopenharmony_ci    unsigned char iv[AES_BLOCK_SIZE * 4];
414e1051a39Sopenharmony_ci    unsigned char ciphertext[BIG_TEST_SIZE];
415e1051a39Sopenharmony_ci    unsigned char checktext[BIG_TEST_SIZE];
416e1051a39Sopenharmony_ci    unsigned int n;
417e1051a39Sopenharmony_ci    size_t matches;
418e1051a39Sopenharmony_ci
419e1051a39Sopenharmony_ci    memcpy(iv, saved_iv, sizeof(iv));
420e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
421e1051a39Sopenharmony_ci    AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
422e1051a39Sopenharmony_ci    AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv,
423e1051a39Sopenharmony_ci                    AES_ENCRYPT);
424e1051a39Sopenharmony_ci
425e1051a39Sopenharmony_ci    /* corrupt right at the start */
426e1051a39Sopenharmony_ci    ++ciphertext[0];
427e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
428e1051a39Sopenharmony_ci    AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
429e1051a39Sopenharmony_ci    AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv,
430e1051a39Sopenharmony_ci                    AES_DECRYPT);
431e1051a39Sopenharmony_ci
432e1051a39Sopenharmony_ci    matches = 0;
433e1051a39Sopenharmony_ci    for (n = 0; n < sizeof(checktext); ++n)
434e1051a39Sopenharmony_ci        if (checktext[n] == plaintext[n])
435e1051a39Sopenharmony_ci            ++matches;
436e1051a39Sopenharmony_ci
437e1051a39Sopenharmony_ci    /* Fail if there is more than 1% matching bytes */
438e1051a39Sopenharmony_ci    return TEST_size_t_le(matches, sizeof(checktext) / 100);
439e1051a39Sopenharmony_ci}
440e1051a39Sopenharmony_ci#endif
441e1051a39Sopenharmony_ci
442e1051a39Sopenharmony_ciint setup_tests(void)
443e1051a39Sopenharmony_ci{
444e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
445e1051a39Sopenharmony_ci    RAND_bytes(rkey, sizeof(rkey));
446e1051a39Sopenharmony_ci    RAND_bytes(rkey2, sizeof(rkey2));
447e1051a39Sopenharmony_ci    RAND_bytes(plaintext, sizeof(plaintext));
448e1051a39Sopenharmony_ci    RAND_bytes(saved_iv, sizeof(saved_iv));
449e1051a39Sopenharmony_ci
450e1051a39Sopenharmony_ci    ADD_TEST(test_ige_enc_dec);
451e1051a39Sopenharmony_ci    ADD_TEST(test_ige_enc_chaining);
452e1051a39Sopenharmony_ci    ADD_TEST(test_ige_dec_chaining);
453e1051a39Sopenharmony_ci    ADD_TEST(test_ige_garble_forwards);
454e1051a39Sopenharmony_ci    ADD_TEST(test_bi_ige_enc_dec);
455e1051a39Sopenharmony_ci    ADD_TEST(test_bi_ige_garble1);
456e1051a39Sopenharmony_ci    ADD_TEST(test_bi_ige_garble2);
457e1051a39Sopenharmony_ci    ADD_TEST(test_bi_ige_garble3);
458e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ige_vectors, OSSL_NELEM(ige_test_vectors));
459e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_bi_ige_vectors, OSSL_NELEM(bi_ige_test_vectors));
460e1051a39Sopenharmony_ci#endif
461e1051a39Sopenharmony_ci    return 1;
462e1051a39Sopenharmony_ci}
463