Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

gcm_test.c 16 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* ====================================================================
  2. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ==================================================================== */
  48. #include <stdio.h>
  49. #include <openssl/aes.h>
  50. #include <openssl/mem.h>
  51. #include <openssl/modes.h>
  52. #include "internal.h"
  53. struct test_case {
  54. const char *key;
  55. const char *plaintext;
  56. const char *additional_data;
  57. const char *nonce;
  58. const char *ciphertext;
  59. const char *tag;
  60. };
  61. static const struct test_case test_cases[] = {
  62. {
  63. "00000000000000000000000000000000",
  64. NULL,
  65. NULL,
  66. "000000000000000000000000",
  67. NULL,
  68. "58e2fccefa7e3061367f1d57a4e7455a",
  69. },
  70. {
  71. "00000000000000000000000000000000",
  72. "00000000000000000000000000000000",
  73. NULL,
  74. "000000000000000000000000",
  75. "0388dace60b6a392f328c2b971b2fe78",
  76. "ab6e47d42cec13bdf53a67b21257bddf",
  77. },
  78. {
  79. "feffe9928665731c6d6a8f9467308308",
  80. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  81. NULL,
  82. "cafebabefacedbaddecaf888",
  83. "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985",
  84. "4d5c2af327cd64a62cf35abd2ba6fab4",
  85. },
  86. {
  87. "feffe9928665731c6d6a8f9467308308",
  88. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  89. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  90. "cafebabefacedbaddecaf888",
  91. "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091",
  92. "5bc94fbc3221a5db94fae95ae7121a47",
  93. },
  94. {
  95. "feffe9928665731c6d6a8f9467308308",
  96. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  97. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  98. "cafebabefacedbad",
  99. "61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598",
  100. "3612d2e79e3b0785561be14aaca2fccb",
  101. },
  102. {
  103. "feffe9928665731c6d6a8f9467308308",
  104. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  105. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  106. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  107. "8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5",
  108. "619cc5aefffe0bfa462af43c1699d050",
  109. },
  110. {
  111. "000000000000000000000000000000000000000000000000",
  112. NULL,
  113. NULL,
  114. "000000000000000000000000",
  115. NULL,
  116. "cd33b28ac773f74ba00ed1f312572435",
  117. },
  118. {
  119. "000000000000000000000000000000000000000000000000",
  120. "00000000000000000000000000000000",
  121. NULL,
  122. "000000000000000000000000",
  123. "98e7247c07f0fe411c267e4384b0f600",
  124. "2ff58d80033927ab8ef4d4587514f0fb",
  125. },
  126. {
  127. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  128. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  129. NULL,
  130. "cafebabefacedbaddecaf888",
  131. "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710acade256",
  132. "9924a7c8587336bfb118024db8674a14",
  133. },
  134. {
  135. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  136. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  137. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  138. "cafebabefacedbaddecaf888",
  139. "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710",
  140. "2519498e80f1478f37ba55bd6d27618c",
  141. },
  142. {
  143. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  144. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  145. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  146. "cafebabefacedbad",
  147. "0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7",
  148. "65dcc57fcf623a24094fcca40d3533f8",
  149. },
  150. {
  151. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  152. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  153. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  154. "cafebabefacedbad",
  155. "0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7",
  156. "65dcc57fcf623a24094fcca40d3533f8",
  157. },
  158. {
  159. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  160. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  161. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  162. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  163. "d27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b",
  164. "dcf566ff291c25bbb8568fc3d376a6d9",
  165. },
  166. {
  167. "0000000000000000000000000000000000000000000000000000000000000000",
  168. NULL,
  169. NULL,
  170. "000000000000000000000000",
  171. NULL,
  172. "530f8afbc74536b9a963b4f1c4cb738b",
  173. },
  174. {
  175. "0000000000000000000000000000000000000000000000000000000000000000",
  176. "00000000000000000000000000000000",
  177. NULL,
  178. "000000000000000000000000",
  179. "cea7403d4d606b6e074ec5d3baf39d18",
  180. "d0d1c8a799996bf0265b98b5d48ab919",
  181. },
  182. {
  183. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  184. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  185. NULL,
  186. "cafebabefacedbaddecaf888",
  187. "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662898015ad",
  188. "b094dac5d93471bdec1a502270e3cc6c",
  189. },
  190. {
  191. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  192. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  193. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  194. "cafebabefacedbaddecaf888",
  195. "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662",
  196. "76fc6ece0f4e1768cddf8853bb2d551b",
  197. },
  198. {
  199. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  200. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  201. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  202. "cafebabefacedbad",
  203. "c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f",
  204. "3a337dbf46a792c45e454913fe2ea8f2",
  205. },
  206. {
  207. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  208. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  209. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  210. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  211. "5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f",
  212. "a44a8266ee1c8eb0c8b5d4cf5ae9f19a",
  213. },
  214. {
  215. "00000000000000000000000000000000",
  216. NULL,
  217. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662898015ad",
  218. "000000000000000000000000",
  219. NULL,
  220. "5fea793a2d6f974d37e68e0cb8ff9492",
  221. },
  222. {
  223. "00000000000000000000000000000000",
  224. "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  225. NULL,
  226. /* This nonce results in 0xfff in counter LSB. */
  227. "ffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  228. "56b3373ca9ef6e4a2b64fe1e9a17b61425f10d47a75a5fce13efc6bc784af24f4141bdd48cf7c770887afd573cca5418a9aeffcd7c5ceddfc6a78397b9a85b499da558257267caab2ad0b23ca476a53cb17fb41c4b8b475cb4f3f7165094c229c9e8c4dc0a2a5ff1903e501511221376a1cdb8364c5061a20cae74bc4acd76ceb0abc9fd3217ef9f8c90be402ddf6d8697f4f880dff15bfb7a6b28241ec8fe183c2d59e3f9dfff653c7126f0acb9e64211f42bae12af462b1070bef1ab5e3606872ca10dee15b3249b1a1b958f23134c4bccb7d03200bce420a2f8eb66dcf3644d1423c1b5699003c13ecef4bf38a3b60eedc34033bac1902783dc6d89e2e774188a439c7ebcc0672dbda4ddcfb2794613b0be41315ef778708a70ee7d75165c",
  229. "8b307f6b33286d0ab026a9ed3fe1e85f",
  230. },
  231. };
  232. static int from_hex(uint8_t *out, char in) {
  233. if (in >= '0' && in <= '9') {
  234. *out = in - '0';
  235. return 1;
  236. }
  237. if (in >= 'a' && in <= 'f') {
  238. *out = in - 'a' + 10;
  239. return 1;
  240. }
  241. if (in >= 'A' && in <= 'F') {
  242. *out = in - 'A' + 10;
  243. return 1;
  244. }
  245. return 0;
  246. }
  247. static int decode_hex(uint8_t **out, size_t *out_len, const char *in,
  248. unsigned test_num, const char *description) {
  249. uint8_t *buf = NULL;
  250. size_t i;
  251. if (in == NULL) {
  252. *out = NULL;
  253. *out_len = 0;
  254. return 1;
  255. }
  256. size_t len = strlen(in);
  257. if (len & 1) {
  258. fprintf(stderr, "%u: Odd-length %s input.\n", test_num, description);
  259. goto err;
  260. }
  261. buf = OPENSSL_malloc(len / 2);
  262. if (buf == NULL) {
  263. fprintf(stderr, "%u: malloc failure.\n", test_num);
  264. goto err;
  265. }
  266. for (i = 0; i < len; i += 2) {
  267. uint8_t v, v2;
  268. if (!from_hex(&v, in[i]) ||
  269. !from_hex(&v2, in[i+1])) {
  270. fprintf(stderr, "%u: invalid hex digit in %s around offset %u.\n",
  271. test_num, description, (unsigned)i);
  272. goto err;
  273. }
  274. buf[i/2] = (v << 4) | v2;
  275. }
  276. *out = buf;
  277. *out_len = len/2;
  278. return 1;
  279. err:
  280. if (buf) {
  281. OPENSSL_free(buf);
  282. }
  283. return 0;
  284. }
  285. void hexdump(const char *msg, const void *in, size_t len) {
  286. const uint8_t *data = in;
  287. size_t i;
  288. fprintf(stderr, "%s: ", msg);
  289. for (i = 0; i < len; i++) {
  290. fprintf(stderr, "%02x", data[i]);
  291. }
  292. fprintf(stderr, "\n");
  293. }
  294. static int run_test_case(unsigned test_num, const struct test_case *test) {
  295. size_t key_len, plaintext_len, additional_data_len, nonce_len, ciphertext_len,
  296. tag_len;
  297. uint8_t *key = NULL, *plaintext = NULL, *additional_data = NULL,
  298. *nonce = NULL, *ciphertext = NULL, *tag = NULL, *out = NULL;
  299. int ret = 0;
  300. AES_KEY aes_key;
  301. GCM128_CONTEXT ctx;
  302. if (!decode_hex(&key, &key_len, test->key, test_num, "key") ||
  303. !decode_hex(&plaintext, &plaintext_len, test->plaintext, test_num,
  304. "plaintext") ||
  305. !decode_hex(&additional_data, &additional_data_len, test->additional_data,
  306. test_num, "additional_data") ||
  307. !decode_hex(&nonce, &nonce_len, test->nonce, test_num, "nonce") ||
  308. !decode_hex(&ciphertext, &ciphertext_len, test->ciphertext, test_num,
  309. "ciphertext") ||
  310. !decode_hex(&tag, &tag_len, test->tag, test_num, "tag")) {
  311. goto out;
  312. }
  313. if (plaintext_len != ciphertext_len) {
  314. fprintf(stderr, "%u: plaintext and ciphertext have differing lengths.\n",
  315. test_num);
  316. goto out;
  317. }
  318. if (key_len != 16 && key_len != 24 && key_len != 32) {
  319. fprintf(stderr, "%u: bad key length.\n", test_num);
  320. goto out;
  321. }
  322. if (tag_len != 16) {
  323. fprintf(stderr, "%u: bad tag length.\n", test_num);
  324. goto out;
  325. }
  326. out = OPENSSL_malloc(plaintext_len);
  327. if (AES_set_encrypt_key(key, key_len*8, &aes_key)) {
  328. fprintf(stderr, "%u: AES_set_encrypt_key failed.\n", test_num);
  329. goto out;
  330. }
  331. CRYPTO_gcm128_init(&ctx, &aes_key, (block128_f) AES_encrypt);
  332. CRYPTO_gcm128_setiv(&ctx, nonce, nonce_len);
  333. memset(out, 0, plaintext_len);
  334. if (additional_data) {
  335. CRYPTO_gcm128_aad(&ctx, additional_data, additional_data_len);
  336. }
  337. if (plaintext) {
  338. CRYPTO_gcm128_encrypt(&ctx, plaintext, out, plaintext_len);
  339. }
  340. if (!CRYPTO_gcm128_finish(&ctx, tag, tag_len) ||
  341. (ciphertext && memcmp(out, ciphertext, plaintext_len) != 0)) {
  342. fprintf(stderr, "%u: encrypt failed.\n", test_num);
  343. hexdump("got ", out, plaintext_len);
  344. hexdump("want", ciphertext, plaintext_len);
  345. goto out;
  346. }
  347. CRYPTO_gcm128_setiv(&ctx, nonce, nonce_len);
  348. memset(out, 0, plaintext_len);
  349. if (additional_data) {
  350. CRYPTO_gcm128_aad(&ctx, additional_data, additional_data_len);
  351. }
  352. if (ciphertext) {
  353. CRYPTO_gcm128_decrypt(&ctx, ciphertext, out, plaintext_len);
  354. }
  355. if (!CRYPTO_gcm128_finish(&ctx, tag, tag_len)) {
  356. fprintf(stderr, "%u: decrypt failed.\n", test_num);
  357. goto out;
  358. }
  359. if (plaintext && memcmp(out, plaintext, plaintext_len)) {
  360. fprintf(stderr, "%u: plaintext doesn't match.\n", test_num);
  361. goto out;
  362. }
  363. ret = 1;
  364. out:
  365. if (key) {
  366. OPENSSL_free(key);
  367. }
  368. if (plaintext) {
  369. OPENSSL_free(plaintext);
  370. }
  371. if (additional_data) {
  372. OPENSSL_free(additional_data);
  373. }
  374. if (nonce) {
  375. OPENSSL_free(nonce);
  376. }
  377. if (ciphertext) {
  378. OPENSSL_free(ciphertext);
  379. }
  380. if (tag) {
  381. OPENSSL_free(tag);
  382. }
  383. if (out) {
  384. OPENSSL_free(out);
  385. }
  386. return ret;
  387. }
  388. int main() {
  389. int ret = 0;
  390. unsigned i;
  391. for (i = 0; i < sizeof(test_cases) / sizeof(struct test_case); i++) {
  392. if (!run_test_case(i, &test_cases[i])) {
  393. ret = 1;
  394. }
  395. }
  396. if (ret == 0) {
  397. printf("PASS\n");
  398. }
  399. return ret;
  400. }