igetest.c 16 KB

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