Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

512 linhas
17 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 <openssl/rsa.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <openssl/bn.h>
  60. #include <openssl/crypto.h>
  61. #include <openssl/err.h>
  62. #include <openssl/obj.h>
  63. #define SetKey \
  64. key->n = BN_bin2bn(n, sizeof(n) - 1, key->n); \
  65. key->e = BN_bin2bn(e, sizeof(e) - 1, key->e); \
  66. key->d = BN_bin2bn(d, sizeof(d) - 1, key->d); \
  67. key->p = BN_bin2bn(p, sizeof(p) - 1, key->p); \
  68. key->q = BN_bin2bn(q, sizeof(q) - 1, key->q); \
  69. key->dmp1 = BN_bin2bn(dmp1, sizeof(dmp1) - 1, key->dmp1); \
  70. key->dmq1 = BN_bin2bn(dmq1, sizeof(dmq1) - 1, key->dmq1); \
  71. key->iqmp = BN_bin2bn(iqmp, sizeof(iqmp) - 1, key->iqmp); \
  72. memcpy(c, ctext_ex, sizeof(ctext_ex) - 1); \
  73. return (sizeof(ctext_ex) - 1);
  74. static int key1(RSA *key, unsigned char *c) {
  75. static unsigned char n[] =
  76. "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F"
  77. "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5"
  78. "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93"
  79. "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1"
  80. "\xF5";
  81. static unsigned char e[] = "\x11";
  82. static unsigned char d[] =
  83. "\x0A\x03\x37\x48\x62\x64\x87\x69\x5F\x5F\x30\xBC\x38\xB9\x8B\x44"
  84. "\xC2\xCD\x2D\xFF\x43\x40\x98\xCD\x20\xD8\xA1\x38\xD0\x90\xBF\x64"
  85. "\x79\x7C\x3F\xA7\xA2\xCD\xCB\x3C\xD1\xE0\xBD\xBA\x26\x54\xB4\xF9"
  86. "\xDF\x8E\x8A\xE5\x9D\x73\x3D\x9F\x33\xB3\x01\x62\x4A\xFD\x1D\x51";
  87. static unsigned char p[] =
  88. "\x00\xD8\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5"
  89. "\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x12"
  90. "\x0D";
  91. static unsigned char q[] =
  92. "\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9"
  93. "\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D"
  94. "\x89";
  95. static unsigned char dmp1[] =
  96. "\x59\x0B\x95\x72\xA2\xC2\xA9\xC4\x06\x05\x9D\xC2\xAB\x2F\x1D\xAF"
  97. "\xEB\x7E\x8B\x4F\x10\xA7\x54\x9E\x8E\xED\xF5\xB4\xFC\xE0\x9E\x05";
  98. static unsigned char dmq1[] =
  99. "\x00\x8E\x3C\x05\x21\xFE\x15\xE0\xEA\x06\xA3\x6F\xF0\xF1\x0C\x99"
  100. "\x52\xC3\x5B\x7A\x75\x14\xFD\x32\x38\xB8\x0A\xAD\x52\x98\x62\x8D"
  101. "\x51";
  102. static unsigned char iqmp[] =
  103. "\x36\x3F\xF7\x18\x9D\xA8\xE9\x0B\x1D\x34\x1F\x71\xD0\x9B\x76\xA8"
  104. "\xA9\x43\xE1\x1D\x10\xB2\x4D\x24\x9F\x2D\xEA\xFE\xF8\x0C\x18\x26";
  105. static unsigned char ctext_ex[] =
  106. "\x1b\x8f\x05\xf9\xca\x1a\x79\x52\x6e\x53\xf3\xcc\x51\x4f\xdb\x89"
  107. "\x2b\xfb\x91\x93\x23\x1e\x78\xb9\x92\xe6\x8d\x50\xa4\x80\xcb\x52"
  108. "\x33\x89\x5c\x74\x95\x8d\x5d\x02\xab\x8c\x0f\xd0\x40\xeb\x58\x44"
  109. "\xb0\x05\xc3\x9e\xd8\x27\x4a\x9d\xbf\xa8\x06\x71\x40\x94\x39\xd2";
  110. SetKey;
  111. }
  112. static int key2(RSA *key, unsigned char *c) {
  113. static unsigned char n[] =
  114. "\x00\xA3\x07\x9A\x90\xDF\x0D\xFD\x72\xAC\x09\x0C\xCC\x2A\x78\xB8"
  115. "\x74\x13\x13\x3E\x40\x75\x9C\x98\xFA\xF8\x20\x4F\x35\x8A\x0B\x26"
  116. "\x3C\x67\x70\xE7\x83\xA9\x3B\x69\x71\xB7\x37\x79\xD2\x71\x7B\xE8"
  117. "\x34\x77\xCF";
  118. static unsigned char e[] = "\x3";
  119. static unsigned char d[] =
  120. "\x6C\xAF\xBC\x60\x94\xB3\xFE\x4C\x72\xB0\xB3\x32\xC6\xFB\x25\xA2"
  121. "\xB7\x62\x29\x80\x4E\x68\x65\xFC\xA4\x5A\x74\xDF\x0F\x8F\xB8\x41"
  122. "\x3B\x52\xC0\xD0\xE5\x3D\x9B\x59\x0F\xF1\x9B\xE7\x9F\x49\xDD\x21"
  123. "\xE5\xEB";
  124. static unsigned char p[] =
  125. "\x00\xCF\x20\x35\x02\x8B\x9D\x86\x98\x40\xB4\x16\x66\xB4\x2E\x92"
  126. "\xEA\x0D\xA3\xB4\x32\x04\xB5\xCF\xCE\x91";
  127. static unsigned char q[] =
  128. "\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9"
  129. "\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5F";
  130. static unsigned char dmp1[] =
  131. "\x00\x8A\x15\x78\xAC\x5D\x13\xAF\x10\x2B\x22\xB9\x99\xCD\x74\x61"
  132. "\xF1\x5E\x6D\x22\xCC\x03\x23\xDF\xDF\x0B";
  133. static unsigned char dmq1[] =
  134. "\x00\x86\x55\x21\x4A\xC5\x4D\x8D\x4E\xCD\x61\x77\xF1\xC7\x36\x90"
  135. "\xCE\x2A\x48\x2C\x8B\x05\x99\xCB\xE0\x3F";
  136. static unsigned char iqmp[] =
  137. "\x00\x83\xEF\xEF\xB8\xA9\xA4\x0D\x1D\xB6\xED\x98\xAD\x84\xED\x13"
  138. "\x35\xDC\xC1\x08\xF3\x22\xD0\x57\xCF\x8D";
  139. static unsigned char ctext_ex[] =
  140. "\x14\xbd\xdd\x28\xc9\x83\x35\x19\x23\x80\xe8\xe5\x49\xb1\x58\x2a"
  141. "\x8b\x40\xb4\x48\x6d\x03\xa6\xa5\x31\x1f\x1f\xd5\xf0\xa1\x80\xe4"
  142. "\x17\x53\x03\x29\xa9\x34\x90\x74\xb1\x52\x13\x54\x29\x08\x24\x52"
  143. "\x62\x51";
  144. SetKey;
  145. }
  146. static int key3(RSA *key, unsigned char *c) {
  147. static unsigned char n[] =
  148. "\x00\xBB\xF8\x2F\x09\x06\x82\xCE\x9C\x23\x38\xAC\x2B\x9D\xA8\x71"
  149. "\xF7\x36\x8D\x07\xEE\xD4\x10\x43\xA4\x40\xD6\xB6\xF0\x74\x54\xF5"
  150. "\x1F\xB8\xDF\xBA\xAF\x03\x5C\x02\xAB\x61\xEA\x48\xCE\xEB\x6F\xCD"
  151. "\x48\x76\xED\x52\x0D\x60\xE1\xEC\x46\x19\x71\x9D\x8A\x5B\x8B\x80"
  152. "\x7F\xAF\xB8\xE0\xA3\xDF\xC7\x37\x72\x3E\xE6\xB4\xB7\xD9\x3A\x25"
  153. "\x84\xEE\x6A\x64\x9D\x06\x09\x53\x74\x88\x34\xB2\x45\x45\x98\x39"
  154. "\x4E\xE0\xAA\xB1\x2D\x7B\x61\xA5\x1F\x52\x7A\x9A\x41\xF6\xC1\x68"
  155. "\x7F\xE2\x53\x72\x98\xCA\x2A\x8F\x59\x46\xF8\xE5\xFD\x09\x1D\xBD"
  156. "\xCB";
  157. static unsigned char e[] = "\x11";
  158. static unsigned char d[] =
  159. "\x00\xA5\xDA\xFC\x53\x41\xFA\xF2\x89\xC4\xB9\x88\xDB\x30\xC1\xCD"
  160. "\xF8\x3F\x31\x25\x1E\x06\x68\xB4\x27\x84\x81\x38\x01\x57\x96\x41"
  161. "\xB2\x94\x10\xB3\xC7\x99\x8D\x6B\xC4\x65\x74\x5E\x5C\x39\x26\x69"
  162. "\xD6\x87\x0D\xA2\xC0\x82\xA9\x39\xE3\x7F\xDC\xB8\x2E\xC9\x3E\xDA"
  163. "\xC9\x7F\xF3\xAD\x59\x50\xAC\xCF\xBC\x11\x1C\x76\xF1\xA9\x52\x94"
  164. "\x44\xE5\x6A\xAF\x68\xC5\x6C\x09\x2C\xD3\x8D\xC3\xBE\xF5\xD2\x0A"
  165. "\x93\x99\x26\xED\x4F\x74\xA1\x3E\xDD\xFB\xE1\xA1\xCE\xCC\x48\x94"
  166. "\xAF\x94\x28\xC2\xB7\xB8\x88\x3F\xE4\x46\x3A\x4B\xC8\x5B\x1C\xB3"
  167. "\xC1";
  168. static unsigned char p[] =
  169. "\x00\xEE\xCF\xAE\x81\xB1\xB9\xB3\xC9\x08\x81\x0B\x10\xA1\xB5\x60"
  170. "\x01\x99\xEB\x9F\x44\xAE\xF4\xFD\xA4\x93\xB8\x1A\x9E\x3D\x84\xF6"
  171. "\x32\x12\x4E\xF0\x23\x6E\x5D\x1E\x3B\x7E\x28\xFA\xE7\xAA\x04\x0A"
  172. "\x2D\x5B\x25\x21\x76\x45\x9D\x1F\x39\x75\x41\xBA\x2A\x58\xFB\x65"
  173. "\x99";
  174. static unsigned char q[] =
  175. "\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9"
  176. "\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D"
  177. "\x86\x98\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5"
  178. "\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x15"
  179. "\x03";
  180. static unsigned char dmp1[] =
  181. "\x54\x49\x4C\xA6\x3E\xBA\x03\x37\xE4\xE2\x40\x23\xFC\xD6\x9A\x5A"
  182. "\xEB\x07\xDD\xDC\x01\x83\xA4\xD0\xAC\x9B\x54\xB0\x51\xF2\xB1\x3E"
  183. "\xD9\x49\x09\x75\xEA\xB7\x74\x14\xFF\x59\xC1\xF7\x69\x2E\x9A\x2E"
  184. "\x20\x2B\x38\xFC\x91\x0A\x47\x41\x74\xAD\xC9\x3C\x1F\x67\xC9\x81";
  185. static unsigned char dmq1[] =
  186. "\x47\x1E\x02\x90\xFF\x0A\xF0\x75\x03\x51\xB7\xF8\x78\x86\x4C\xA9"
  187. "\x61\xAD\xBD\x3A\x8A\x7E\x99\x1C\x5C\x05\x56\xA9\x4C\x31\x46\xA7"
  188. "\xF9\x80\x3F\x8F\x6F\x8A\xE3\x42\xE9\x31\xFD\x8A\xE4\x7A\x22\x0D"
  189. "\x1B\x99\xA4\x95\x84\x98\x07\xFE\x39\xF9\x24\x5A\x98\x36\xDA\x3D";
  190. static unsigned char iqmp[] =
  191. "\x00\xB0\x6C\x4F\xDA\xBB\x63\x01\x19\x8D\x26\x5B\xDB\xAE\x94\x23"
  192. "\xB3\x80\xF2\x71\xF7\x34\x53\x88\x50\x93\x07\x7F\xCD\x39\xE2\x11"
  193. "\x9F\xC9\x86\x32\x15\x4F\x58\x83\xB1\x67\xA9\x67\xBF\x40\x2B\x4E"
  194. "\x9E\x2E\x0F\x96\x56\xE6\x98\xEA\x36\x66\xED\xFB\x25\x79\x80\x39"
  195. "\xF7";
  196. static unsigned char ctext_ex[] =
  197. "\xb8\x24\x6b\x56\xa6\xed\x58\x81\xae\xb5\x85\xd9\xa2\x5b\x2a\xd7"
  198. "\x90\xc4\x17\xe0\x80\x68\x1b\xf1\xac\x2b\xc3\xde\xb6\x9d\x8b\xce"
  199. "\xf0\xc4\x36\x6f\xec\x40\x0a\xf0\x52\xa7\x2e\x9b\x0e\xff\xb5\xb3"
  200. "\xf2\xf1\x92\xdb\xea\xca\x03\xc1\x27\x40\x05\x71\x13\xbf\x1f\x06"
  201. "\x69\xac\x22\xe9\xf3\xa7\x85\x2e\x3c\x15\xd9\x13\xca\xb0\xb8\x86"
  202. "\x3a\x95\xc9\x92\x94\xce\x86\x74\x21\x49\x54\x61\x03\x46\xf4\xd4"
  203. "\x74\xb2\x6f\x7c\x48\xb4\x2e\xe6\x8e\x1f\x57\x2a\x1f\xc4\x02\x6a"
  204. "\xc4\x56\xb4\xf5\x9f\x7b\x62\x1e\xa1\xb9\xd8\x8f\x64\x20\x2f\xb1";
  205. SetKey;
  206. }
  207. static int test_bad_key(void) {
  208. RSA *key = RSA_new();
  209. BIGNUM e;
  210. BN_init(&e);
  211. BN_set_word(&e, RSA_F4);
  212. if (!RSA_generate_key_ex(key, 512, &e, NULL)) {
  213. fprintf(stderr, "RSA_generate_key_ex failed.\n");
  214. ERR_print_errors_fp(stderr);
  215. return 0;
  216. }
  217. if (!BN_add(key->p, key->p, BN_value_one())) {
  218. fprintf(stderr, "BN error.\n");
  219. ERR_print_errors_fp(stderr);
  220. return 0;
  221. }
  222. if (RSA_check_key(key)) {
  223. fprintf(stderr, "RSA_check_key passed with invalid key!\n");
  224. return 0;
  225. }
  226. ERR_clear_error();
  227. BN_free(&e);
  228. RSA_free(key);
  229. return 1;
  230. }
  231. static int test_only_d_given(void) {
  232. RSA *key = RSA_new();
  233. uint8_t buf[64];
  234. unsigned buf_len = sizeof(buf);
  235. const uint8_t kDummyHash[16] = {0};
  236. int ret = 0;
  237. if (!BN_hex2bn(&key->n,
  238. "00e77bbf3889d4ef36a9a25d4d69f3f632eb4362214c74517da6d6aeaa9bd"
  239. "09ac42b26621cd88f3a6eb013772fc3bf9f83914b6467231c630202c35b3e"
  240. "5808c659") ||
  241. !BN_hex2bn(&key->e, "010001") ||
  242. !BN_hex2bn(&key->d,
  243. "0365db9eb6d73b53b015c40cd8db4de7dd7035c68b5ac1bf786d7a4ee2cea"
  244. "316eaeca21a73ac365e58713195f2ae9849348525ca855386b6d028e437a9"
  245. "495a01") ||
  246. RSA_size(key) > sizeof(buf)) {
  247. goto err;
  248. }
  249. if (!RSA_check_key(key)) {
  250. fprintf(stderr, "RSA_check_key failed with only d given.\n");
  251. ERR_print_errors_fp(stderr);
  252. goto err;
  253. }
  254. if (!RSA_sign(NID_sha256, kDummyHash, sizeof(kDummyHash), buf, &buf_len,
  255. key)) {
  256. fprintf(stderr, "RSA_sign failed with only d given.\n");
  257. ERR_print_errors_fp(stderr);
  258. goto err;
  259. }
  260. if (!RSA_verify(NID_sha256, kDummyHash, sizeof(kDummyHash), buf, buf_len,
  261. key)) {
  262. fprintf(stderr, "RSA_verify failed with only d given.\n");
  263. ERR_print_errors_fp(stderr);
  264. goto err;
  265. }
  266. ret = 1;
  267. err:
  268. RSA_free(key);
  269. return ret;
  270. }
  271. static int test_recover_crt_params(void) {
  272. RSA *key1, *key2;
  273. BIGNUM *e = BN_new();
  274. uint8_t buf[128];
  275. unsigned buf_len = sizeof(buf);
  276. const uint8_t kDummyHash[16] = {0};
  277. unsigned i;
  278. BN_set_word(e, RSA_F4);
  279. ERR_clear_error();
  280. for (i = 0; i < 1; i++) {
  281. key1 = RSA_new();
  282. if (!RSA_generate_key_ex(key1, 512, e, NULL)) {
  283. fprintf(stderr, "RSA_generate_key_ex failed.\n");
  284. ERR_print_errors_fp(stderr);
  285. return 0;
  286. }
  287. if (!RSA_check_key(key1)) {
  288. fprintf(stderr, "RSA_check_key failed with original key.\n");
  289. ERR_print_errors_fp(stderr);
  290. return 0;
  291. }
  292. key2 = RSA_new();
  293. key2->n = BN_dup(key1->n);
  294. key2->e = BN_dup(key1->e);
  295. key2->d = BN_dup(key1->d);
  296. RSA_free(key1);
  297. if (!RSA_recover_crt_params(key2)) {
  298. fprintf(stderr, "RSA_recover_crt_params failed.\n");
  299. ERR_print_errors_fp(stderr);
  300. return 0;
  301. }
  302. if (RSA_size(key2) > buf_len) {
  303. return 0;
  304. }
  305. if (!RSA_check_key(key2)) {
  306. fprintf(stderr, "RSA_check_key failed with recovered key.\n");
  307. ERR_print_errors_fp(stderr);
  308. return 0;
  309. }
  310. if (!RSA_sign(NID_sha256, kDummyHash, sizeof(kDummyHash), buf, &buf_len,
  311. key2)) {
  312. fprintf(stderr, "RSA_sign failed with recovered key.\n");
  313. ERR_print_errors_fp(stderr);
  314. return 0;
  315. }
  316. if (!RSA_verify(NID_sha256, kDummyHash, sizeof(kDummyHash), buf, buf_len,
  317. key2)) {
  318. fprintf(stderr, "RSA_verify failed with recovered key.\n");
  319. ERR_print_errors_fp(stderr);
  320. return 0;
  321. }
  322. RSA_free(key2);
  323. }
  324. BN_free(e);
  325. return 1;
  326. }
  327. int main(int argc, char *argv[]) {
  328. int err = 0;
  329. int v;
  330. RSA *key;
  331. unsigned char ptext[256];
  332. unsigned char ctext[256];
  333. static unsigned char ptext_ex[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
  334. unsigned char ctext_ex[256];
  335. int plen;
  336. int clen = 0;
  337. int num;
  338. int n;
  339. CRYPTO_library_init();
  340. plen = sizeof(ptext_ex) - 1;
  341. for (v = 0; v < 3; v++) {
  342. key = RSA_new();
  343. switch (v) {
  344. case 0:
  345. clen = key1(key, ctext_ex);
  346. break;
  347. case 1:
  348. clen = key2(key, ctext_ex);
  349. break;
  350. case 2:
  351. clen = key3(key, ctext_ex);
  352. break;
  353. default:
  354. abort();
  355. }
  356. if (!RSA_check_key(key)) {
  357. printf("%d: RSA_check_key failed\n", v);
  358. err = 1;
  359. goto oaep;
  360. }
  361. num = RSA_public_encrypt(plen, ptext_ex, ctext, key, RSA_PKCS1_PADDING);
  362. if (num != clen) {
  363. printf("PKCS#1 v1.5 encryption failed!\n");
  364. err = 1;
  365. goto oaep;
  366. }
  367. num = RSA_private_decrypt(num, ctext, ptext, key, RSA_PKCS1_PADDING);
  368. if (num != plen || memcmp(ptext, ptext_ex, num) != 0) {
  369. printf("PKCS#1 v1.5 decryption failed!\n");
  370. err = 1;
  371. } else {
  372. printf("PKCS #1 v1.5 encryption/decryption ok\n");
  373. }
  374. oaep:
  375. ERR_clear_error();
  376. num =
  377. RSA_public_encrypt(plen, ptext_ex, ctext, key, RSA_PKCS1_OAEP_PADDING);
  378. if (num == -1) {
  379. printf("No OAEP support\n");
  380. goto next;
  381. }
  382. if (num != clen) {
  383. printf("OAEP encryption failed!\n");
  384. err = 1;
  385. goto next;
  386. }
  387. num = RSA_private_decrypt(num, ctext, ptext, key, RSA_PKCS1_OAEP_PADDING);
  388. if (num != plen || memcmp(ptext, ptext_ex, num) != 0) {
  389. printf("OAEP decryption (encrypted data) failed!\n");
  390. err = 1;
  391. } else if (memcmp(ctext, ctext_ex, num) == 0) {
  392. printf("OAEP test vector %d passed!\n", v);
  393. }
  394. /* Different ciphertexts (rsa_oaep.c without -DPKCS_TESTVECT).
  395. Try decrypting ctext_ex */
  396. num =
  397. RSA_private_decrypt(clen, ctext_ex, ptext, key, RSA_PKCS1_OAEP_PADDING);
  398. if (num != plen || memcmp(ptext, ptext_ex, num) != 0) {
  399. printf("OAEP decryption (test vector data) failed!\n");
  400. err = 1;
  401. } else {
  402. printf("OAEP encryption/decryption ok\n");
  403. }
  404. /* Try decrypting corrupted ciphertexts */
  405. for (n = 0; n < clen; ++n) {
  406. int b;
  407. unsigned char saved = ctext[n];
  408. for (b = 0; b < 256; ++b) {
  409. if (b == saved) {
  410. continue;
  411. }
  412. ctext[n] = b;
  413. num =
  414. RSA_private_decrypt(num, ctext, ptext, key, RSA_PKCS1_OAEP_PADDING);
  415. if (num > 0) {
  416. printf("Corrupt data decrypted!\n");
  417. err = 1;
  418. }
  419. }
  420. }
  421. next:
  422. RSA_free(key);
  423. }
  424. if (err != 0 ||
  425. !test_only_d_given() ||
  426. !test_recover_crt_params() ||
  427. !test_bad_key()) {
  428. err = 1;
  429. }
  430. if (err == 0) {
  431. printf("PASS\n");
  432. }
  433. return err;
  434. }