2
0

igetest.c 16 KB

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