You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

410 lines
12 KiB

  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/cipher.h>
  58. #include <openssl/err.h>
  59. #include <openssl/bio.h>
  60. static void hexdump(FILE *f, const char *title, const uint8_t *s, int l) {
  61. int n = 0;
  62. fprintf(f, "%s", title);
  63. for (; n < l; ++n) {
  64. if ((n % 16) == 0)
  65. fprintf(f, "\n%04x", n);
  66. fprintf(f, " %02x", s[n]);
  67. }
  68. fprintf(f, "\n");
  69. }
  70. static int convert(uint8_t *s) {
  71. uint8_t *d;
  72. for (d = s; *s; s += 2, ++d) {
  73. unsigned int n;
  74. if (!s[1]) {
  75. fprintf(stderr, "Odd number of hex digits!");
  76. exit(4);
  77. }
  78. sscanf((char *)s, "%2x", &n);
  79. *d = (uint8_t)n;
  80. }
  81. return s - d;
  82. }
  83. static char *sstrsep(char **string, const char *delim) {
  84. char isdelim[256];
  85. char *token = *string;
  86. if (**string == 0) {
  87. return NULL;
  88. }
  89. memset(isdelim, 0, 256);
  90. isdelim[0] = 1;
  91. while (*delim) {
  92. isdelim[(uint8_t)(*delim)] = 1;
  93. delim++;
  94. }
  95. while (!isdelim[(uint8_t)(**string)]) {
  96. (*string)++;
  97. }
  98. if (**string) {
  99. **string = 0;
  100. (*string)++;
  101. }
  102. return token;
  103. }
  104. static uint8_t *ustrsep(char **p, const char *sep) {
  105. return (uint8_t *)sstrsep(p, sep);
  106. }
  107. static void test1(const EVP_CIPHER *c, const uint8_t *key, int kn,
  108. const uint8_t *iv, int in, const uint8_t *plaintext, int pn,
  109. const uint8_t *ciphertext, int cn, const uint8_t *aad, int an,
  110. const uint8_t *tag, int tn, int encdec) {
  111. EVP_CIPHER_CTX ctx;
  112. uint8_t out[4096];
  113. int outl, outl2, mode;
  114. printf("Testing cipher %s%s\n", EVP_CIPHER_name(c),
  115. (encdec == 1 ? "(encrypt)"
  116. : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
  117. hexdump(stdout, "Key", key, kn);
  118. if (in) {
  119. hexdump(stdout, "IV", iv, in);
  120. }
  121. hexdump(stdout, "Plaintext", plaintext, pn);
  122. hexdump(stdout, "Ciphertext", ciphertext, cn);
  123. if (an) {
  124. hexdump(stdout, "AAD", aad, an);
  125. }
  126. if (tn) {
  127. hexdump(stdout, "Tag", tag, tn);
  128. }
  129. mode = EVP_CIPHER_mode(c);
  130. if (kn != EVP_CIPHER_key_length(c)) {
  131. fprintf(stderr, "Key length doesn't match, got %d expected %lu\n", kn,
  132. (unsigned long)EVP_CIPHER_key_length(c));
  133. exit(5);
  134. }
  135. EVP_CIPHER_CTX_init(&ctx);
  136. if (encdec != 0) {
  137. if (mode == EVP_CIPH_GCM_MODE) {
  138. if (!EVP_EncryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
  139. fprintf(stderr, "EncryptInit failed\n");
  140. BIO_print_errors_fp(stderr);
  141. exit(10);
  142. }
  143. if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, in, NULL)) {
  144. fprintf(stderr, "IV length set failed\n");
  145. BIO_print_errors_fp(stderr);
  146. exit(11);
  147. }
  148. if (!EVP_EncryptInit_ex(&ctx, NULL, NULL, key, iv)) {
  149. fprintf(stderr, "Key/IV set failed\n");
  150. BIO_print_errors_fp(stderr);
  151. exit(12);
  152. }
  153. if (an && !EVP_EncryptUpdate(&ctx, NULL, &outl, aad, an)) {
  154. fprintf(stderr, "AAD set failed\n");
  155. BIO_print_errors_fp(stderr);
  156. exit(13);
  157. }
  158. } else if (!EVP_EncryptInit_ex(&ctx, c, NULL, key, iv)) {
  159. fprintf(stderr, "EncryptInit failed\n");
  160. BIO_print_errors_fp(stderr);
  161. exit(10);
  162. }
  163. EVP_CIPHER_CTX_set_padding(&ctx, 0);
  164. if (!EVP_EncryptUpdate(&ctx, out, &outl, plaintext, pn)) {
  165. fprintf(stderr, "Encrypt failed\n");
  166. BIO_print_errors_fp(stderr);
  167. exit(6);
  168. }
  169. if (!EVP_EncryptFinal_ex(&ctx, out + outl, &outl2)) {
  170. fprintf(stderr, "EncryptFinal failed\n");
  171. BIO_print_errors_fp(stderr);
  172. exit(7);
  173. }
  174. if (outl + outl2 != cn) {
  175. fprintf(stderr, "Ciphertext length mismatch got %d expected %d\n",
  176. outl + outl2, cn);
  177. exit(8);
  178. }
  179. if (memcmp(out, ciphertext, cn)) {
  180. fprintf(stderr, "Ciphertext mismatch\n");
  181. hexdump(stderr, "Got", out, cn);
  182. hexdump(stderr, "Expected", ciphertext, cn);
  183. exit(9);
  184. }
  185. if (mode == EVP_CIPH_GCM_MODE) {
  186. uint8_t rtag[16];
  187. /* Note: EVP_CTRL_CCM_GET_TAG has same value as
  188. * EVP_CTRL_GCM_GET_TAG
  189. */
  190. if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_GET_TAG, tn, rtag)) {
  191. fprintf(stderr, "Get tag failed\n");
  192. BIO_print_errors_fp(stderr);
  193. exit(14);
  194. }
  195. if (memcmp(rtag, tag, tn)) {
  196. fprintf(stderr, "Tag mismatch\n");
  197. hexdump(stderr, "Got", rtag, tn);
  198. hexdump(stderr, "Expected", tag, tn);
  199. exit(9);
  200. }
  201. }
  202. }
  203. if (encdec <= 0) {
  204. if (mode == EVP_CIPH_GCM_MODE) {
  205. if (!EVP_DecryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
  206. fprintf(stderr, "EncryptInit failed\n");
  207. BIO_print_errors_fp(stderr);
  208. exit(10);
  209. }
  210. if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, in, NULL)) {
  211. fprintf(stderr, "IV length set failed\n");
  212. BIO_print_errors_fp(stderr);
  213. exit(11);
  214. }
  215. if (!EVP_DecryptInit_ex(&ctx, NULL, NULL, key, iv)) {
  216. fprintf(stderr, "Key/IV set failed\n");
  217. BIO_print_errors_fp(stderr);
  218. exit(12);
  219. }
  220. if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_TAG, tn, (void *)tag)) {
  221. fprintf(stderr, "Set tag failed\n");
  222. BIO_print_errors_fp(stderr);
  223. exit(14);
  224. }
  225. if (an && !EVP_DecryptUpdate(&ctx, NULL, &outl, aad, an)) {
  226. fprintf(stderr, "AAD set failed\n");
  227. BIO_print_errors_fp(stderr);
  228. exit(13);
  229. }
  230. } else if (!EVP_DecryptInit_ex(&ctx, c, NULL, key, iv)) {
  231. fprintf(stderr, "DecryptInit failed\n");
  232. BIO_print_errors_fp(stderr);
  233. exit(11);
  234. }
  235. EVP_CIPHER_CTX_set_padding(&ctx, 0);
  236. if (!EVP_DecryptUpdate(&ctx, out, &outl, ciphertext, cn)) {
  237. fprintf(stderr, "Decrypt failed\n");
  238. BIO_print_errors_fp(stderr);
  239. exit(6);
  240. }
  241. outl2 = 0;
  242. if (!EVP_DecryptFinal_ex(&ctx, out + outl, &outl2)) {
  243. fprintf(stderr, "DecryptFinal failed\n");
  244. BIO_print_errors_fp(stderr);
  245. exit(7);
  246. }
  247. if (outl + outl2 != pn) {
  248. fprintf(stderr, "Plaintext length mismatch got %d expected %d\n",
  249. outl + outl2, pn);
  250. exit(8);
  251. }
  252. if (memcmp(out, plaintext, pn)) {
  253. fprintf(stderr, "Plaintext mismatch\n");
  254. hexdump(stderr, "Got", out, pn);
  255. hexdump(stderr, "Expected", plaintext, pn);
  256. exit(9);
  257. }
  258. }
  259. EVP_CIPHER_CTX_cleanup(&ctx);
  260. printf("\n");
  261. }
  262. static int test_cipher(const char *cipher, const uint8_t *key, int kn,
  263. const uint8_t *iv, int in, const uint8_t *plaintext,
  264. int pn, const uint8_t *ciphertext, int cn,
  265. const uint8_t *aad, int an, const uint8_t *tag, int tn,
  266. int encdec) {
  267. const EVP_CIPHER *c;
  268. if (strcmp(cipher, "DES-CBC") == 0) {
  269. c = EVP_des_cbc();
  270. } else if (strcmp(cipher, "DES-EDE3-CBC") == 0) {
  271. c = EVP_des_ede3_cbc();
  272. } else if (strcmp(cipher, "RC4") == 0) {
  273. c = EVP_rc4();
  274. } else if (strcmp(cipher, "AES-128-ECB") == 0) {
  275. c = EVP_aes_128_ecb();
  276. } else if (strcmp(cipher, "AES-256-ECB") == 0) {
  277. c = EVP_aes_256_ecb();
  278. } else if (strcmp(cipher, "AES-128-CBC") == 0) {
  279. c = EVP_aes_128_cbc();
  280. } else if (strcmp(cipher, "AES-128-GCM") == 0) {
  281. c = EVP_aes_128_gcm();
  282. } else if (strcmp(cipher, "AES-256-CBC") == 0) {
  283. c = EVP_aes_256_cbc();
  284. } else if (strcmp(cipher, "AES-128-CTR") == 0) {
  285. c = EVP_aes_128_ctr();
  286. } else if (strcmp(cipher, "AES-256-CTR") == 0) {
  287. c = EVP_aes_256_ctr();
  288. } else if (strcmp(cipher, "AES-256-GCM") == 0) {
  289. c = EVP_aes_256_gcm();
  290. } else {
  291. fprintf(stderr, "Unknown cipher type %s\n", cipher);
  292. return 0;
  293. }
  294. test1(c, key, kn, iv, in, plaintext, pn, ciphertext, cn, aad, an, tag, tn,
  295. encdec);
  296. return 1;
  297. }
  298. int main(int argc, char **argv) {
  299. const char *input_file;
  300. FILE *f;
  301. if (argc != 2) {
  302. fprintf(stderr, "%s <test file>\n", argv[0]);
  303. return 1;
  304. }
  305. input_file = argv[1];
  306. f = fopen(input_file, "r");
  307. if (!f) {
  308. perror(input_file);
  309. return 2;
  310. }
  311. ERR_load_crypto_strings();
  312. for (;;) {
  313. char line[4096];
  314. char *p;
  315. char *cipher;
  316. uint8_t *iv, *key, *plaintext, *ciphertext, *aad, *tag;
  317. int encdec;
  318. int kn, in, pn, cn;
  319. int an = 0;
  320. int tn = 0;
  321. if (!fgets((char *)line, sizeof line, f)) {
  322. break;
  323. }
  324. if (line[0] == '#' || line[0] == '\n') {
  325. continue;
  326. }
  327. p = line;
  328. cipher = sstrsep(&p, ":");
  329. key = ustrsep(&p, ":");
  330. iv = ustrsep(&p, ":");
  331. plaintext = ustrsep(&p, ":");
  332. ciphertext = ustrsep(&p, ":");
  333. if (p[-1] == '\n') {
  334. encdec = -1;
  335. p[-1] = '\0';
  336. tag = aad = NULL;
  337. an = tn = 0;
  338. } else {
  339. aad = ustrsep(&p, ":");
  340. tag = ustrsep(&p, ":");
  341. if (tag == NULL) {
  342. p = (char *)aad;
  343. tag = aad = NULL;
  344. an = tn = 0;
  345. }
  346. if (p[-1] == '\n') {
  347. encdec = -1;
  348. p[-1] = '\0';
  349. } else
  350. encdec = atoi(sstrsep(&p, "\n"));
  351. }
  352. kn = convert(key);
  353. in = convert(iv);
  354. pn = convert(plaintext);
  355. cn = convert(ciphertext);
  356. if (aad) {
  357. an = convert(aad);
  358. tn = convert(tag);
  359. }
  360. if (!test_cipher(cipher, key, kn, iv, in, plaintext, pn, ciphertext, cn,
  361. aad, an, tag, tn, encdec)) {
  362. return 3;
  363. }
  364. }
  365. fclose(f);
  366. printf("PASS\n");
  367. return 0;
  368. }