Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

413 rader
16 KiB

  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 <string.h>
  50. #include <openssl/aes.h>
  51. #include <openssl/crypto.h>
  52. #include <openssl/mem.h>
  53. #include <openssl/modes.h>
  54. #include "internal.h"
  55. #include "../test/test_util.h"
  56. struct test_case {
  57. const char *key;
  58. const char *plaintext;
  59. const char *additional_data;
  60. const char *nonce;
  61. const char *ciphertext;
  62. const char *tag;
  63. };
  64. static const struct test_case test_cases[] = {
  65. {
  66. "00000000000000000000000000000000",
  67. NULL,
  68. NULL,
  69. "000000000000000000000000",
  70. NULL,
  71. "58e2fccefa7e3061367f1d57a4e7455a",
  72. },
  73. {
  74. "00000000000000000000000000000000",
  75. "00000000000000000000000000000000",
  76. NULL,
  77. "000000000000000000000000",
  78. "0388dace60b6a392f328c2b971b2fe78",
  79. "ab6e47d42cec13bdf53a67b21257bddf",
  80. },
  81. {
  82. "feffe9928665731c6d6a8f9467308308",
  83. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  84. NULL,
  85. "cafebabefacedbaddecaf888",
  86. "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985",
  87. "4d5c2af327cd64a62cf35abd2ba6fab4",
  88. },
  89. {
  90. "feffe9928665731c6d6a8f9467308308",
  91. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  92. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  93. "cafebabefacedbaddecaf888",
  94. "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091",
  95. "5bc94fbc3221a5db94fae95ae7121a47",
  96. },
  97. {
  98. "feffe9928665731c6d6a8f9467308308",
  99. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  100. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  101. "cafebabefacedbad",
  102. "61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598",
  103. "3612d2e79e3b0785561be14aaca2fccb",
  104. },
  105. {
  106. "feffe9928665731c6d6a8f9467308308",
  107. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  108. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  109. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  110. "8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5",
  111. "619cc5aefffe0bfa462af43c1699d050",
  112. },
  113. {
  114. "000000000000000000000000000000000000000000000000",
  115. NULL,
  116. NULL,
  117. "000000000000000000000000",
  118. NULL,
  119. "cd33b28ac773f74ba00ed1f312572435",
  120. },
  121. {
  122. "000000000000000000000000000000000000000000000000",
  123. "00000000000000000000000000000000",
  124. NULL,
  125. "000000000000000000000000",
  126. "98e7247c07f0fe411c267e4384b0f600",
  127. "2ff58d80033927ab8ef4d4587514f0fb",
  128. },
  129. {
  130. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  131. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  132. NULL,
  133. "cafebabefacedbaddecaf888",
  134. "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710acade256",
  135. "9924a7c8587336bfb118024db8674a14",
  136. },
  137. {
  138. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  139. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  140. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  141. "cafebabefacedbaddecaf888",
  142. "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710",
  143. "2519498e80f1478f37ba55bd6d27618c",
  144. },
  145. {
  146. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  147. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  148. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  149. "cafebabefacedbad",
  150. "0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7",
  151. "65dcc57fcf623a24094fcca40d3533f8",
  152. },
  153. {
  154. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  155. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  156. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  157. "cafebabefacedbad",
  158. "0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7",
  159. "65dcc57fcf623a24094fcca40d3533f8",
  160. },
  161. {
  162. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  163. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  164. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  165. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  166. "d27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b",
  167. "dcf566ff291c25bbb8568fc3d376a6d9",
  168. },
  169. {
  170. "0000000000000000000000000000000000000000000000000000000000000000",
  171. NULL,
  172. NULL,
  173. "000000000000000000000000",
  174. NULL,
  175. "530f8afbc74536b9a963b4f1c4cb738b",
  176. },
  177. {
  178. "0000000000000000000000000000000000000000000000000000000000000000",
  179. "00000000000000000000000000000000",
  180. NULL,
  181. "000000000000000000000000",
  182. "cea7403d4d606b6e074ec5d3baf39d18",
  183. "d0d1c8a799996bf0265b98b5d48ab919",
  184. },
  185. {
  186. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  187. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  188. NULL,
  189. "cafebabefacedbaddecaf888",
  190. "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662898015ad",
  191. "b094dac5d93471bdec1a502270e3cc6c",
  192. },
  193. {
  194. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  195. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  196. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  197. "cafebabefacedbaddecaf888",
  198. "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662",
  199. "76fc6ece0f4e1768cddf8853bb2d551b",
  200. },
  201. {
  202. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  203. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  204. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  205. "cafebabefacedbad",
  206. "c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f",
  207. "3a337dbf46a792c45e454913fe2ea8f2",
  208. },
  209. {
  210. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  211. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  212. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  213. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  214. "5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f",
  215. "a44a8266ee1c8eb0c8b5d4cf5ae9f19a",
  216. },
  217. {
  218. "00000000000000000000000000000000",
  219. NULL,
  220. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662898015ad",
  221. "000000000000000000000000",
  222. NULL,
  223. "5fea793a2d6f974d37e68e0cb8ff9492",
  224. },
  225. {
  226. "00000000000000000000000000000000",
  227. "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  228. NULL,
  229. /* This nonce results in 0xfff in counter LSB. */
  230. "ffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  231. "56b3373ca9ef6e4a2b64fe1e9a17b61425f10d47a75a5fce13efc6bc784af24f4141bdd48cf7c770887afd573cca5418a9aeffcd7c5ceddfc6a78397b9a85b499da558257267caab2ad0b23ca476a53cb17fb41c4b8b475cb4f3f7165094c229c9e8c4dc0a2a5ff1903e501511221376a1cdb8364c5061a20cae74bc4acd76ceb0abc9fd3217ef9f8c90be402ddf6d8697f4f880dff15bfb7a6b28241ec8fe183c2d59e3f9dfff653c7126f0acb9e64211f42bae12af462b1070bef1ab5e3606872ca10dee15b3249b1a1b958f23134c4bccb7d03200bce420a2f8eb66dcf3644d1423c1b5699003c13ecef4bf38a3b60eedc34033bac1902783dc6d89e2e774188a439c7ebcc0672dbda4ddcfb2794613b0be41315ef778708a70ee7d75165c",
  232. "8b307f6b33286d0ab026a9ed3fe1e85f",
  233. },
  234. };
  235. static int from_hex(uint8_t *out, char in) {
  236. if (in >= '0' && in <= '9') {
  237. *out = in - '0';
  238. return 1;
  239. }
  240. if (in >= 'a' && in <= 'f') {
  241. *out = in - 'a' + 10;
  242. return 1;
  243. }
  244. if (in >= 'A' && in <= 'F') {
  245. *out = in - 'A' + 10;
  246. return 1;
  247. }
  248. return 0;
  249. }
  250. static int decode_hex(uint8_t **out, size_t *out_len, const char *in,
  251. unsigned test_num, const char *description) {
  252. uint8_t *buf = NULL;
  253. size_t i;
  254. if (in == NULL) {
  255. *out = NULL;
  256. *out_len = 0;
  257. return 1;
  258. }
  259. size_t len = strlen(in);
  260. if (len & 1) {
  261. fprintf(stderr, "%u: Odd-length %s input.\n", test_num, description);
  262. goto err;
  263. }
  264. buf = OPENSSL_malloc(len / 2);
  265. if (buf == NULL) {
  266. fprintf(stderr, "%u: malloc failure.\n", test_num);
  267. goto err;
  268. }
  269. for (i = 0; i < len; i += 2) {
  270. uint8_t v, v2;
  271. if (!from_hex(&v, in[i]) ||
  272. !from_hex(&v2, in[i+1])) {
  273. fprintf(stderr, "%u: invalid hex digit in %s around offset %u.\n",
  274. test_num, description, (unsigned)i);
  275. goto err;
  276. }
  277. buf[i/2] = (v << 4) | v2;
  278. }
  279. *out = buf;
  280. *out_len = len/2;
  281. return 1;
  282. err:
  283. OPENSSL_free(buf);
  284. return 0;
  285. }
  286. static int run_test_case(unsigned test_num, const struct test_case *test) {
  287. size_t key_len, plaintext_len, additional_data_len, nonce_len, ciphertext_len,
  288. tag_len;
  289. uint8_t *key = NULL, *plaintext = NULL, *additional_data = NULL,
  290. *nonce = NULL, *ciphertext = NULL, *tag = NULL, *out = NULL;
  291. int ret = 0;
  292. AES_KEY aes_key;
  293. GCM128_CONTEXT ctx;
  294. if (!decode_hex(&key, &key_len, test->key, test_num, "key") ||
  295. !decode_hex(&plaintext, &plaintext_len, test->plaintext, test_num,
  296. "plaintext") ||
  297. !decode_hex(&additional_data, &additional_data_len, test->additional_data,
  298. test_num, "additional_data") ||
  299. !decode_hex(&nonce, &nonce_len, test->nonce, test_num, "nonce") ||
  300. !decode_hex(&ciphertext, &ciphertext_len, test->ciphertext, test_num,
  301. "ciphertext") ||
  302. !decode_hex(&tag, &tag_len, test->tag, test_num, "tag")) {
  303. goto out;
  304. }
  305. if (plaintext_len != ciphertext_len) {
  306. fprintf(stderr, "%u: plaintext and ciphertext have differing lengths.\n",
  307. test_num);
  308. goto out;
  309. }
  310. if (key_len != 16 && key_len != 24 && key_len != 32) {
  311. fprintf(stderr, "%u: bad key length.\n", test_num);
  312. goto out;
  313. }
  314. if (tag_len != 16) {
  315. fprintf(stderr, "%u: bad tag length.\n", test_num);
  316. goto out;
  317. }
  318. out = OPENSSL_malloc(plaintext_len);
  319. if (out == NULL) {
  320. goto out;
  321. }
  322. if (AES_set_encrypt_key(key, key_len*8, &aes_key)) {
  323. fprintf(stderr, "%u: AES_set_encrypt_key failed.\n", test_num);
  324. goto out;
  325. }
  326. CRYPTO_gcm128_init(&ctx, &aes_key, (block128_f) AES_encrypt);
  327. CRYPTO_gcm128_setiv(&ctx, nonce, nonce_len);
  328. memset(out, 0, plaintext_len);
  329. if (additional_data) {
  330. CRYPTO_gcm128_aad(&ctx, additional_data, additional_data_len);
  331. }
  332. if (plaintext) {
  333. CRYPTO_gcm128_encrypt(&ctx, plaintext, out, plaintext_len);
  334. }
  335. if (!CRYPTO_gcm128_finish(&ctx, tag, tag_len) ||
  336. (ciphertext && memcmp(out, ciphertext, plaintext_len) != 0)) {
  337. fprintf(stderr, "%u: encrypt failed.\n", test_num);
  338. hexdump(stderr, "got :", out, plaintext_len);
  339. hexdump(stderr, "want:", ciphertext, plaintext_len);
  340. goto out;
  341. }
  342. CRYPTO_gcm128_setiv(&ctx, nonce, nonce_len);
  343. memset(out, 0, plaintext_len);
  344. if (additional_data) {
  345. CRYPTO_gcm128_aad(&ctx, additional_data, additional_data_len);
  346. }
  347. if (ciphertext) {
  348. CRYPTO_gcm128_decrypt(&ctx, ciphertext, out, plaintext_len);
  349. }
  350. if (!CRYPTO_gcm128_finish(&ctx, tag, tag_len)) {
  351. fprintf(stderr, "%u: decrypt failed.\n", test_num);
  352. goto out;
  353. }
  354. if (plaintext && memcmp(out, plaintext, plaintext_len)) {
  355. fprintf(stderr, "%u: plaintext doesn't match.\n", test_num);
  356. goto out;
  357. }
  358. ret = 1;
  359. out:
  360. OPENSSL_free(key);
  361. OPENSSL_free(plaintext);
  362. OPENSSL_free(additional_data);
  363. OPENSSL_free(nonce);
  364. OPENSSL_free(ciphertext);
  365. OPENSSL_free(tag);
  366. OPENSSL_free(out);
  367. return ret;
  368. }
  369. int main(void) {
  370. int ret = 0;
  371. unsigned i;
  372. CRYPTO_library_init();
  373. for (i = 0; i < sizeof(test_cases) / sizeof(struct test_case); i++) {
  374. if (!run_test_case(i, &test_cases[i])) {
  375. ret = 1;
  376. }
  377. }
  378. if (ret == 0) {
  379. printf("PASS\n");
  380. }
  381. return ret;
  382. }