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