Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

cipher_test.c 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <stdio.h>
  57. #include <openssl/bio.h>
  58. #include <openssl/cipher.h>
  59. #include <openssl/crypto.h>
  60. #include <openssl/err.h>
  61. static void hexdump(FILE *f, const char *title, const uint8_t *s, int l) {
  62. int n = 0;
  63. fprintf(f, "%s", title);
  64. for (; n < l; ++n) {
  65. if ((n % 16) == 0)
  66. fprintf(f, "\n%04x", n);
  67. fprintf(f, " %02x", s[n]);
  68. }
  69. fprintf(f, "\n");
  70. }
  71. static int convert(uint8_t *s) {
  72. uint8_t *d;
  73. for (d = s; *s; s += 2, ++d) {
  74. unsigned int n;
  75. if (!s[1]) {
  76. fprintf(stderr, "Odd number of hex digits!");
  77. exit(4);
  78. }
  79. sscanf((char *)s, "%2x", &n);
  80. *d = (uint8_t)n;
  81. }
  82. return s - d;
  83. }
  84. static char *sstrsep(char **string, const char *delim) {
  85. char isdelim[256];
  86. char *token = *string;
  87. if (**string == 0) {
  88. return NULL;
  89. }
  90. memset(isdelim, 0, 256);
  91. isdelim[0] = 1;
  92. while (*delim) {
  93. isdelim[(uint8_t)(*delim)] = 1;
  94. delim++;
  95. }
  96. while (!isdelim[(uint8_t)(**string)]) {
  97. (*string)++;
  98. }
  99. if (**string) {
  100. **string = 0;
  101. (*string)++;
  102. }
  103. return token;
  104. }
  105. static uint8_t *ustrsep(char **p, const char *sep) {
  106. return (uint8_t *)sstrsep(p, sep);
  107. }
  108. static void test1(const EVP_CIPHER *c, const uint8_t *key, int kn,
  109. const uint8_t *iv, int in, const uint8_t *plaintext, int pn,
  110. const uint8_t *ciphertext, int cn, const uint8_t *aad, int an,
  111. const uint8_t *tag, int tn, int encdec) {
  112. EVP_CIPHER_CTX ctx;
  113. uint8_t out[4096];
  114. int outl, outl2, mode;
  115. printf("Testing cipher %s%s\n", EVP_CIPHER_name(c),
  116. (encdec == 1 ? "(encrypt)"
  117. : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
  118. hexdump(stdout, "Key", key, kn);
  119. if (in) {
  120. hexdump(stdout, "IV", iv, in);
  121. }
  122. hexdump(stdout, "Plaintext", plaintext, pn);
  123. hexdump(stdout, "Ciphertext", ciphertext, cn);
  124. if (an) {
  125. hexdump(stdout, "AAD", aad, an);
  126. }
  127. if (tn) {
  128. hexdump(stdout, "Tag", tag, tn);
  129. }
  130. mode = EVP_CIPHER_mode(c);
  131. if (kn != EVP_CIPHER_key_length(c)) {
  132. fprintf(stderr, "Key length doesn't match, got %d expected %lu\n", kn,
  133. (unsigned long)EVP_CIPHER_key_length(c));
  134. exit(5);
  135. }
  136. EVP_CIPHER_CTX_init(&ctx);
  137. if (encdec != 0) {
  138. if (mode == EVP_CIPH_GCM_MODE) {
  139. if (!EVP_EncryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
  140. fprintf(stderr, "EncryptInit failed\n");
  141. BIO_print_errors_fp(stderr);
  142. exit(10);
  143. }
  144. if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, in, NULL)) {
  145. fprintf(stderr, "IV length set failed\n");
  146. BIO_print_errors_fp(stderr);
  147. exit(11);
  148. }
  149. if (!EVP_EncryptInit_ex(&ctx, NULL, NULL, key, iv)) {
  150. fprintf(stderr, "Key/IV set failed\n");
  151. BIO_print_errors_fp(stderr);
  152. exit(12);
  153. }
  154. if (an && !EVP_EncryptUpdate(&ctx, NULL, &outl, aad, an)) {
  155. fprintf(stderr, "AAD set failed\n");
  156. BIO_print_errors_fp(stderr);
  157. exit(13);
  158. }
  159. } else if (!EVP_EncryptInit_ex(&ctx, c, NULL, key, iv)) {
  160. fprintf(stderr, "EncryptInit failed\n");
  161. BIO_print_errors_fp(stderr);
  162. exit(10);
  163. }
  164. EVP_CIPHER_CTX_set_padding(&ctx, 0);
  165. if (!EVP_EncryptUpdate(&ctx, out, &outl, plaintext, pn)) {
  166. fprintf(stderr, "Encrypt failed\n");
  167. BIO_print_errors_fp(stderr);
  168. exit(6);
  169. }
  170. if (!EVP_EncryptFinal_ex(&ctx, out + outl, &outl2)) {
  171. fprintf(stderr, "EncryptFinal failed\n");
  172. BIO_print_errors_fp(stderr);
  173. exit(7);
  174. }
  175. if (outl + outl2 != cn) {
  176. fprintf(stderr, "Ciphertext length mismatch got %d expected %d\n",
  177. outl + outl2, cn);
  178. exit(8);
  179. }
  180. if (memcmp(out, ciphertext, cn)) {
  181. fprintf(stderr, "Ciphertext mismatch\n");
  182. hexdump(stderr, "Got", out, cn);
  183. hexdump(stderr, "Expected", ciphertext, cn);
  184. exit(9);
  185. }
  186. if (mode == EVP_CIPH_GCM_MODE) {
  187. uint8_t rtag[16];
  188. /* Note: EVP_CTRL_CCM_GET_TAG has same value as
  189. * EVP_CTRL_GCM_GET_TAG
  190. */
  191. if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_GET_TAG, tn, rtag)) {
  192. fprintf(stderr, "Get tag failed\n");
  193. BIO_print_errors_fp(stderr);
  194. exit(14);
  195. }
  196. if (memcmp(rtag, tag, tn)) {
  197. fprintf(stderr, "Tag mismatch\n");
  198. hexdump(stderr, "Got", rtag, tn);
  199. hexdump(stderr, "Expected", tag, tn);
  200. exit(9);
  201. }
  202. }
  203. }
  204. if (encdec <= 0) {
  205. if (mode == EVP_CIPH_GCM_MODE) {
  206. if (!EVP_DecryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
  207. fprintf(stderr, "EncryptInit failed\n");
  208. BIO_print_errors_fp(stderr);
  209. exit(10);
  210. }
  211. if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, in, NULL)) {
  212. fprintf(stderr, "IV length set failed\n");
  213. BIO_print_errors_fp(stderr);
  214. exit(11);
  215. }
  216. if (!EVP_DecryptInit_ex(&ctx, NULL, NULL, key, iv)) {
  217. fprintf(stderr, "Key/IV set failed\n");
  218. BIO_print_errors_fp(stderr);
  219. exit(12);
  220. }
  221. if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_TAG, tn, (void *)tag)) {
  222. fprintf(stderr, "Set tag failed\n");
  223. BIO_print_errors_fp(stderr);
  224. exit(14);
  225. }
  226. if (an && !EVP_DecryptUpdate(&ctx, NULL, &outl, aad, an)) {
  227. fprintf(stderr, "AAD set failed\n");
  228. BIO_print_errors_fp(stderr);
  229. exit(13);
  230. }
  231. } else if (!EVP_DecryptInit_ex(&ctx, c, NULL, key, iv)) {
  232. fprintf(stderr, "DecryptInit failed\n");
  233. BIO_print_errors_fp(stderr);
  234. exit(11);
  235. }
  236. EVP_CIPHER_CTX_set_padding(&ctx, 0);
  237. if (!EVP_DecryptUpdate(&ctx, out, &outl, ciphertext, cn)) {
  238. fprintf(stderr, "Decrypt failed\n");
  239. BIO_print_errors_fp(stderr);
  240. exit(6);
  241. }
  242. outl2 = 0;
  243. if (!EVP_DecryptFinal_ex(&ctx, out + outl, &outl2)) {
  244. fprintf(stderr, "DecryptFinal failed\n");
  245. BIO_print_errors_fp(stderr);
  246. exit(7);
  247. }
  248. if (outl + outl2 != pn) {
  249. fprintf(stderr, "Plaintext length mismatch got %d expected %d\n",
  250. outl + outl2, pn);
  251. exit(8);
  252. }
  253. if (memcmp(out, plaintext, pn)) {
  254. fprintf(stderr, "Plaintext mismatch\n");
  255. hexdump(stderr, "Got", out, pn);
  256. hexdump(stderr, "Expected", plaintext, pn);
  257. exit(9);
  258. }
  259. }
  260. EVP_CIPHER_CTX_cleanup(&ctx);
  261. printf("\n");
  262. }
  263. static int test_cipher(const char *cipher, const uint8_t *key, int kn,
  264. const uint8_t *iv, int in, const uint8_t *plaintext,
  265. int pn, const uint8_t *ciphertext, int cn,
  266. const uint8_t *aad, int an, const uint8_t *tag, int tn,
  267. int encdec) {
  268. const EVP_CIPHER *c;
  269. if (strcmp(cipher, "DES-CBC") == 0) {
  270. c = EVP_des_cbc();
  271. } else if (strcmp(cipher, "DES-EDE3-CBC") == 0) {
  272. c = EVP_des_ede3_cbc();
  273. } else if (strcmp(cipher, "RC4") == 0) {
  274. c = EVP_rc4();
  275. } else if (strcmp(cipher, "AES-128-ECB") == 0) {
  276. c = EVP_aes_128_ecb();
  277. } else if (strcmp(cipher, "AES-256-ECB") == 0) {
  278. c = EVP_aes_256_ecb();
  279. } else if (strcmp(cipher, "AES-128-CBC") == 0) {
  280. c = EVP_aes_128_cbc();
  281. } else if (strcmp(cipher, "AES-128-GCM") == 0) {
  282. c = EVP_aes_128_gcm();
  283. } else if (strcmp(cipher, "AES-256-CBC") == 0) {
  284. c = EVP_aes_256_cbc();
  285. } else if (strcmp(cipher, "AES-128-CTR") == 0) {
  286. c = EVP_aes_128_ctr();
  287. } else if (strcmp(cipher, "AES-256-CTR") == 0) {
  288. c = EVP_aes_256_ctr();
  289. } else if (strcmp(cipher, "AES-256-GCM") == 0) {
  290. c = EVP_aes_256_gcm();
  291. } else {
  292. fprintf(stderr, "Unknown cipher type %s\n", cipher);
  293. return 0;
  294. }
  295. test1(c, key, kn, iv, in, plaintext, pn, ciphertext, cn, aad, an, tag, tn,
  296. encdec);
  297. return 1;
  298. }
  299. int main(int argc, char **argv) {
  300. const char *input_file;
  301. FILE *f;
  302. CRYPTO_library_init();
  303. if (argc != 2) {
  304. fprintf(stderr, "%s <test file>\n", argv[0]);
  305. return 1;
  306. }
  307. input_file = argv[1];
  308. f = fopen(input_file, "r");
  309. if (!f) {
  310. perror(input_file);
  311. return 2;
  312. }
  313. ERR_load_crypto_strings();
  314. for (;;) {
  315. char line[4096];
  316. char *p;
  317. char *cipher;
  318. uint8_t *iv, *key, *plaintext, *ciphertext, *aad, *tag;
  319. int encdec;
  320. int kn, in, pn, cn;
  321. int an = 0;
  322. int tn = 0;
  323. if (!fgets((char *)line, sizeof line, f)) {
  324. break;
  325. }
  326. if (line[0] == '#' || line[0] == '\n') {
  327. continue;
  328. }
  329. p = line;
  330. cipher = sstrsep(&p, ":");
  331. key = ustrsep(&p, ":");
  332. iv = ustrsep(&p, ":");
  333. plaintext = ustrsep(&p, ":");
  334. ciphertext = ustrsep(&p, ":");
  335. if (p[-1] == '\n') {
  336. encdec = -1;
  337. p[-1] = '\0';
  338. tag = aad = NULL;
  339. an = tn = 0;
  340. } else {
  341. aad = ustrsep(&p, ":");
  342. tag = ustrsep(&p, ":");
  343. if (tag == NULL) {
  344. p = (char *)aad;
  345. tag = aad = NULL;
  346. an = tn = 0;
  347. }
  348. if (p[-1] == '\n') {
  349. encdec = -1;
  350. p[-1] = '\0';
  351. } else
  352. encdec = atoi(sstrsep(&p, "\n"));
  353. }
  354. kn = convert(key);
  355. in = convert(iv);
  356. pn = convert(plaintext);
  357. cn = convert(ciphertext);
  358. if (aad) {
  359. an = convert(aad);
  360. tn = convert(tag);
  361. }
  362. if (!test_cipher(cipher, key, kn, iv, in, plaintext, pn, ciphertext, cn,
  363. aad, an, tag, tn, encdec)) {
  364. return 3;
  365. }
  366. }
  367. fclose(f);
  368. printf("PASS\n");
  369. return 0;
  370. }