2
0

igetest.c 16 KB

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