rsa_test.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #include <tomcrypt_test.h>
  2. #ifdef LTC_MRSA
  3. #define RSA_MSGSIZE 78
  4. /* These are test keys [see file test.key] that I use to test my import/export against */
  5. static const unsigned char openssl_private_rsa[] = {
  6. 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde, 0x64, 0x8a,
  7. 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7, 0xa1, 0xb7,
  8. 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96, 0x65, 0xe5,
  9. 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60, 0x12, 0x8a,
  10. 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b, 0xbf, 0x12,
  11. 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68, 0x7c, 0x61,
  12. 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4, 0xe2, 0x76,
  13. 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a, 0x60, 0x3f,
  14. 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01, 0x00, 0x01,
  15. 0x02, 0x81, 0x81, 0x00, 0xc8, 0x62, 0xb9, 0xea, 0xde, 0x44, 0x53, 0x1d, 0x56, 0x97, 0xd9, 0x97,
  16. 0x9e, 0x1a, 0xcf, 0x30, 0x1e, 0x0a, 0x88, 0x45, 0x86, 0x29, 0x30, 0xa3, 0x4d, 0x9f, 0x61, 0x65,
  17. 0x73, 0xe0, 0xd6, 0x87, 0x8f, 0xb6, 0xf3, 0x06, 0xa3, 0x82, 0xdc, 0x7c, 0xac, 0xfe, 0x9b, 0x28,
  18. 0x9a, 0xae, 0xfd, 0xfb, 0xfe, 0x2f, 0x0e, 0xd8, 0x97, 0x04, 0xe3, 0xbb, 0x1f, 0xd1, 0xec, 0x0d,
  19. 0xba, 0xa3, 0x49, 0x7f, 0x47, 0xac, 0x8a, 0x44, 0x04, 0x7e, 0x86, 0xb7, 0x39, 0x42, 0x3f, 0xad,
  20. 0x1e, 0xb7, 0x0e, 0xa5, 0x51, 0xf4, 0x40, 0x63, 0x1e, 0xfd, 0xbd, 0xea, 0x9f, 0x41, 0x9f, 0xa8,
  21. 0x90, 0x1d, 0x6f, 0x0a, 0x5a, 0x95, 0x13, 0x11, 0x0d, 0x80, 0xaf, 0x5f, 0x64, 0x98, 0x8a, 0x2c,
  22. 0x78, 0x68, 0x65, 0xb0, 0x2b, 0x8b, 0xa2, 0x53, 0x87, 0xca, 0xf1, 0x64, 0x04, 0xab, 0xf2, 0x7b,
  23. 0xdb, 0x83, 0xc8, 0x81, 0x02, 0x41, 0x00, 0xf7, 0xbe, 0x5e, 0x23, 0xc3, 0x32, 0x3f, 0xbf, 0x8b,
  24. 0x8e, 0x3a, 0xee, 0xfc, 0xfc, 0xcb, 0xe5, 0xf7, 0xf1, 0x0b, 0xbc, 0x42, 0x82, 0xae, 0xd5, 0x7a,
  25. 0x3e, 0xca, 0xf7, 0xd5, 0x69, 0x3f, 0x64, 0x25, 0xa2, 0x1f, 0xb7, 0x75, 0x75, 0x05, 0x92, 0x42,
  26. 0xeb, 0xb8, 0xf1, 0xf3, 0x0a, 0x05, 0xe3, 0x94, 0xd1, 0x55, 0x78, 0x35, 0xa0, 0x36, 0xa0, 0x9b,
  27. 0x7c, 0x92, 0x84, 0x6c, 0xdd, 0xdc, 0x4d, 0x02, 0x41, 0x00, 0xd6, 0x86, 0x0e, 0x85, 0x42, 0x0b,
  28. 0x04, 0x08, 0x84, 0x21, 0x60, 0xf0, 0x0e, 0x0d, 0x88, 0xfd, 0x1e, 0x36, 0x10, 0x65, 0x4f, 0x1e,
  29. 0x53, 0xb4, 0x08, 0x72, 0x80, 0x5c, 0x3f, 0x59, 0x66, 0x17, 0xe6, 0x98, 0xf2, 0xe9, 0x6c, 0x7a,
  30. 0x06, 0x4c, 0xac, 0x76, 0x3d, 0xed, 0x8c, 0xa1, 0xce, 0xad, 0x1b, 0xbd, 0xb4, 0x7d, 0x28, 0xbc,
  31. 0xe3, 0x0e, 0x38, 0x8d, 0x99, 0xd8, 0x05, 0xb5, 0xa3, 0x71, 0x02, 0x40, 0x6d, 0xeb, 0xc3, 0x2d,
  32. 0x2e, 0xf0, 0x5e, 0xa4, 0x88, 0x31, 0x05, 0x29, 0x00, 0x8a, 0xd1, 0x95, 0x29, 0x9b, 0x83, 0xcf,
  33. 0x75, 0xdb, 0x31, 0xe3, 0x7a, 0x27, 0xde, 0x3a, 0x74, 0x30, 0x0c, 0x76, 0x4c, 0xd4, 0x50, 0x2a,
  34. 0x40, 0x2d, 0x39, 0xd9, 0x99, 0x63, 0xa9, 0x5d, 0x80, 0xae, 0x53, 0xca, 0x94, 0x3f, 0x05, 0x23,
  35. 0x1e, 0xf8, 0x05, 0x04, 0xe1, 0xb8, 0x35, 0xf2, 0x17, 0xb3, 0xa0, 0x89, 0x02, 0x41, 0x00, 0xab,
  36. 0x90, 0x88, 0xfa, 0x60, 0x08, 0x29, 0x50, 0x9a, 0x43, 0x8b, 0xa0, 0x50, 0xcc, 0xd8, 0x5a, 0xfe,
  37. 0x97, 0x64, 0x63, 0x71, 0x74, 0x22, 0xa3, 0x20, 0x02, 0x5a, 0xcf, 0xeb, 0xc6, 0x16, 0x95, 0x54,
  38. 0xd1, 0xcb, 0xab, 0x8d, 0x1a, 0xc6, 0x00, 0xfa, 0x08, 0x92, 0x9c, 0x71, 0xd5, 0x52, 0x52, 0x35,
  39. 0x96, 0x71, 0x4b, 0x8b, 0x92, 0x0c, 0xd0, 0xe9, 0xbf, 0xad, 0x63, 0x0b, 0xa5, 0xe9, 0xb1, 0x02,
  40. 0x41, 0x00, 0xdc, 0xcc, 0x27, 0xc8, 0xe4, 0xdc, 0x62, 0x48, 0xd5, 0x9b, 0xaf, 0xf5, 0xab, 0x60,
  41. 0xf6, 0x21, 0xfd, 0x53, 0xe2, 0xb7, 0x5d, 0x09, 0xc9, 0x1a, 0xa1, 0x04, 0xa9, 0xfc, 0x61, 0x2c,
  42. 0x5d, 0x04, 0x58, 0x3a, 0x5a, 0x39, 0xf1, 0x4a, 0x21, 0x56, 0x67, 0xfd, 0xcc, 0x20, 0xa3, 0x8f,
  43. 0x78, 0x18, 0x5a, 0x79, 0x3d, 0x2e, 0x8e, 0x7e, 0x86, 0x0a, 0xe6, 0xa8, 0x33, 0xc1, 0x04, 0x17,
  44. 0x4a, 0x9f, };
  45. /*** openssl public RSA key in DER format */
  46. static const unsigned char openssl_public_rsa[] = {
  47. 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  48. 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde,
  49. 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, 0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7,
  50. 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, 0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96,
  51. 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, 0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60,
  52. 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, 0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b,
  53. 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, 0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68,
  54. 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, 0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4,
  55. 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, 0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a,
  56. 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, 0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01,
  57. 0x00, 0x01, };
  58. static int rsa_compat_test(void)
  59. {
  60. rsa_key key;
  61. unsigned char buf[1024];
  62. unsigned long len;
  63. /* try reading the key */
  64. DO(rsa_import(openssl_private_rsa, sizeof(openssl_private_rsa), &key));
  65. /* now try to export private/public and compare */
  66. len = sizeof(buf);
  67. DO(rsa_export(buf, &len, PK_PRIVATE, &key));
  68. if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) {
  69. fprintf(stderr, "RSA private export failed to match OpenSSL output, %lu, %lu\n", len, (unsigned long)sizeof(openssl_private_rsa));
  70. return 1;
  71. }
  72. len = sizeof(buf);
  73. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  74. if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) {
  75. fprintf(stderr, "RSA(private) public export failed to match OpenSSL output\n");
  76. return 1;
  77. }
  78. rsa_free(&key);
  79. /* try reading the public key */
  80. DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
  81. len = sizeof(buf);
  82. DO(rsa_export(buf, &len, PK_PUBLIC, &key));
  83. if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) {
  84. fprintf(stderr, "RSA(public) SSL public import failed to match OpenSSL output\n");
  85. return 1;
  86. }
  87. rsa_free(&key);
  88. return 0;
  89. }
  90. int rsa_test(void)
  91. {
  92. unsigned char in[1024], out[1024], tmp[1024];
  93. rsa_key key, privKey, pubKey;
  94. int hash_idx, prng_idx, stat, stat2, i;
  95. unsigned long rsa_msgsize, len, len2, len3, cnt, cnt2;
  96. static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
  97. if (rsa_compat_test() != 0) {
  98. return 1;
  99. }
  100. hash_idx = find_hash("sha1");
  101. prng_idx = find_prng("yarrow");
  102. if (hash_idx == -1 || prng_idx == -1) {
  103. fprintf(stderr, "rsa_test requires LTC_SHA1 and yarrow");
  104. return 1;
  105. }
  106. /* make 10 random key */
  107. for (cnt = 0; cnt < 10; cnt++) {
  108. DO(rsa_make_key(&yarrow_prng, prng_idx, 1024/8, 65537, &key));
  109. if (mp_count_bits(key.N) != 1024) {
  110. fprintf(stderr, "rsa_1024 key modulus has %d bits\n", mp_count_bits(key.N));
  111. len = mp_unsigned_bin_size(key.N);
  112. mp_to_unsigned_bin(key.N, tmp);
  113. fprintf(stderr, "N == \n");
  114. for (cnt = 0; cnt < len; ) {
  115. fprintf(stderr, "%02x ", tmp[cnt]);
  116. if (!(++cnt & 15)) fprintf(stderr, "\n");
  117. }
  118. len = mp_unsigned_bin_size(key.p);
  119. mp_to_unsigned_bin(key.p, tmp);
  120. fprintf(stderr, "p == \n");
  121. for (cnt = 0; cnt < len; ) {
  122. fprintf(stderr, "%02x ", tmp[cnt]);
  123. if (!(++cnt & 15)) fprintf(stderr, "\n");
  124. }
  125. len = mp_unsigned_bin_size(key.q);
  126. mp_to_unsigned_bin(key.q, tmp);
  127. fprintf(stderr, "\nq == \n");
  128. for (cnt = 0; cnt < len; ) {
  129. fprintf(stderr, "%02x ", tmp[cnt]);
  130. if (!(++cnt & 15)) fprintf(stderr, "\n");
  131. }
  132. fprintf(stderr, "\n");
  133. return 1;
  134. }
  135. if (cnt != 9) {
  136. rsa_free(&key);
  137. }
  138. }
  139. /* encrypt the key (without lparam) */
  140. for (cnt = 0; cnt < 4; cnt++) {
  141. for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
  142. /* make a random key/msg */
  143. yarrow_read(in, rsa_msgsize, &yarrow_prng);
  144. len = sizeof(out);
  145. len2 = rsa_msgsize;
  146. DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, hash_idx, &key));
  147. /* change a byte */
  148. out[8] ^= 1;
  149. DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat2, &key));
  150. /* change a byte back */
  151. out[8] ^= 1;
  152. if (len2 != rsa_msgsize) {
  153. fprintf(stderr, "\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2);
  154. return 1;
  155. }
  156. len2 = rsa_msgsize;
  157. DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat, &key));
  158. if (!(stat == 1 && stat2 == 0)) {
  159. fprintf(stderr, "rsa_decrypt_key (without lparam) failed (rsa_msgsize = %lu)", rsa_msgsize);
  160. fprintf(stderr, "\n stat: %i stat2: %i", stat, stat2);
  161. return 1;
  162. }
  163. if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
  164. fprintf(stderr, "\nrsa_decrypt_key mismatch, len %lu (second decrypt)\n", len2);
  165. print_hex("Original", in, rsa_msgsize);
  166. print_hex("Output", tmp, len2);
  167. return 1;
  168. }
  169. }
  170. }
  171. /* encrypt the key (with lparam) */
  172. for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
  173. len = sizeof(out);
  174. len2 = rsa_msgsize;
  175. DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, lparam, sizeof(lparam), &yarrow_prng, prng_idx, hash_idx, &key));
  176. /* change a byte */
  177. out[8] ^= 1;
  178. DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat2, &key));
  179. if (len2 != rsa_msgsize) {
  180. fprintf(stderr, "\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2);
  181. return 1;
  182. }
  183. /* change a byte back */
  184. out[8] ^= 1;
  185. len2 = rsa_msgsize;
  186. DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat, &key));
  187. if (!(stat == 1 && stat2 == 0)) {
  188. fprintf(stderr, "rsa_decrypt_key (with lparam) failed (rsa_msgsize = %lu)", rsa_msgsize);
  189. return 1;
  190. }
  191. if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
  192. fprintf(stderr, "rsa_decrypt_key mismatch len %lu", len2);
  193. print_hex("Original", in, rsa_msgsize);
  194. print_hex("Output", tmp, len2);
  195. return 1;
  196. }
  197. }
  198. /* encrypt the key PKCS #1 v1.5 (payload from 1 to 117 bytes) */
  199. for (rsa_msgsize = 1; rsa_msgsize <= 117; rsa_msgsize++) {
  200. len = sizeof(out);
  201. len2 = rsa_msgsize;
  202. /* make a random key/msg */
  203. yarrow_read(in, rsa_msgsize, &yarrow_prng);
  204. DO(rsa_encrypt_key_ex(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, 0, LTC_PKCS_1_V1_5, &key));
  205. len2 = rsa_msgsize;
  206. DO(rsa_decrypt_key_ex(out, len, tmp, &len2, NULL, 0, 0, LTC_PKCS_1_V1_5, &stat, &key));
  207. if (stat != 1) {
  208. fprintf(stderr, "rsa_decrypt_key_ex failed, %d, %d", stat, stat2);
  209. return 1;
  210. }
  211. if (len2 != rsa_msgsize) {
  212. fprintf(stderr, "rsa_decrypt_key_ex mismatch len %lu", len2);
  213. return 1;
  214. }
  215. if (memcmp(tmp, in, rsa_msgsize)) {
  216. fprintf(stderr, "rsa_decrypt_key_ex mismatch data");
  217. print_hex("Original", in, rsa_msgsize);
  218. print_hex("Output", tmp, rsa_msgsize);
  219. return 1;
  220. }
  221. }
  222. /* sign a message (unsalted, lower cholestorol and Atkins approved) now */
  223. len = sizeof(out);
  224. DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 0, &key));
  225. /* export key and import as both private and public */
  226. len2 = sizeof(tmp);
  227. DO(rsa_export(tmp, &len2, PK_PRIVATE, &key));
  228. DO(rsa_import(tmp, len2, &privKey));
  229. len2 = sizeof(tmp);
  230. DO(rsa_export(tmp, &len2, PK_PUBLIC, &key));
  231. DO(rsa_import(tmp, len2, &pubKey));
  232. /* verify with original */
  233. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &key));
  234. /* change a byte */
  235. in[0] ^= 1;
  236. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &key));
  237. if (!(stat == 1 && stat2 == 0)) {
  238. fprintf(stderr, "rsa_verify_hash (unsalted, origKey) failed, %d, %d", stat, stat2);
  239. rsa_free(&key);
  240. rsa_free(&pubKey);
  241. rsa_free(&privKey);
  242. return 1;
  243. }
  244. /* verify with privKey */
  245. /* change byte back to original */
  246. in[0] ^= 1;
  247. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &privKey));
  248. /* change a byte */
  249. in[0] ^= 1;
  250. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &privKey));
  251. if (!(stat == 1 && stat2 == 0)) {
  252. fprintf(stderr, "rsa_verify_hash (unsalted, privKey) failed, %d, %d", stat, stat2);
  253. rsa_free(&key);
  254. rsa_free(&pubKey);
  255. rsa_free(&privKey);
  256. return 1;
  257. }
  258. /* verify with pubKey */
  259. /* change byte back to original */
  260. in[0] ^= 1;
  261. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat, &pubKey));
  262. /* change a byte */
  263. in[0] ^= 1;
  264. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &pubKey));
  265. if (!(stat == 1 && stat2 == 0)) {
  266. fprintf(stderr, "rsa_verify_hash (unsalted, pubkey) failed, %d, %d", stat, stat2);
  267. rsa_free(&key);
  268. rsa_free(&pubKey);
  269. rsa_free(&privKey);
  270. return 1;
  271. }
  272. /* sign a message (salted) now (use privKey to make, pubKey to verify) */
  273. len = sizeof(out);
  274. DO(rsa_sign_hash(in, 20, out, &len, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  275. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat, &pubKey));
  276. /* change a byte */
  277. in[0] ^= 1;
  278. DO(rsa_verify_hash(out, len, in, 20, hash_idx, 8, &stat2, &pubKey));
  279. if (!(stat == 1 && stat2 == 0)) {
  280. fprintf(stderr, "rsa_verify_hash (salted) failed, %d, %d", stat, stat2);
  281. rsa_free(&key);
  282. rsa_free(&pubKey);
  283. rsa_free(&privKey);
  284. return 1;
  285. }
  286. /* sign a message with PKCS #1 v1.5 */
  287. len = sizeof(out);
  288. DO(rsa_sign_hash_ex(in, 20, out, &len, LTC_PKCS_1_V1_5, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  289. DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat, &pubKey));
  290. /* change a byte */
  291. in[0] ^= 1;
  292. DO(rsa_verify_hash_ex(out, len, in, 20, LTC_PKCS_1_V1_5, hash_idx, 8, &stat2, &pubKey));
  293. if (!(stat == 1 && stat2 == 0)) {
  294. fprintf(stderr, "rsa_verify_hash_ex failed, %d, %d", stat, stat2);
  295. rsa_free(&key);
  296. rsa_free(&pubKey);
  297. rsa_free(&privKey);
  298. return 1;
  299. }
  300. /* Testcase for Bleichenbacher attack
  301. *
  302. * (1) Create a valid signature
  303. * (2) Check that it can be verified
  304. * (3) Decrypt the package to fetch plain text
  305. * (4) Forge the structure of PKCS#1-EMSA encoded data
  306. * (4.1) Search for start and end of the padding string
  307. * (4.2) Move the signature to the front of the padding string
  308. * (4.3) Zero the message until the end
  309. * (5) Encrypt the package again
  310. * (6) Profit :)
  311. * For PS lengths < 8: the verification process should fail
  312. * For PS lengths >= 8: the verification process should succeed
  313. * For all PS lengths: the result should not be valid
  314. */
  315. unsigned char* p = in;
  316. unsigned char* p2 = out;
  317. unsigned char* p3 = tmp;
  318. for (i = 0; i < 9; ++i) {
  319. len = sizeof(in);
  320. len2 = sizeof(out);
  321. cnt = rsa_get_size(&key);
  322. /* (1) */
  323. DO(rsa_sign_hash_ex(p, 20, p2, &len2, LTC_PKCS_1_V1_5, &yarrow_prng, prng_idx, hash_idx, 8, &privKey));
  324. /* (2) */
  325. DOX(rsa_verify_hash_ex(p2, len2, p, 20, LTC_PKCS_1_V1_5, hash_idx, -1, &stat, &pubKey), "should succeed");
  326. DOX(stat == 1?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, "should succeed");
  327. len3 = sizeof(tmp);
  328. /* (3) */
  329. DO(ltc_mp.rsa_me(p2, len2, p3, &len3, PK_PUBLIC, &key));
  330. /* (4) */
  331. #if 0
  332. printf("\nBefore:");
  333. for (cnt = 0; cnt < len3; ++cnt) {
  334. if (cnt%32 == 0)
  335. printf("\n%3lu:", cnt);
  336. printf(" %02x", p3[cnt]);
  337. }
  338. #endif
  339. /* (4.1) */
  340. for (cnt = 0; cnt < len3; ++cnt) {
  341. if (p3[cnt] == 0xff)
  342. break;
  343. }
  344. for (cnt2 = cnt+1; cnt2 < len3; ++cnt2) {
  345. if (p3[cnt2] != 0xff)
  346. break;
  347. }
  348. /* (4.2) */
  349. memmove(&p3[cnt+i], &p3[cnt2], len3-cnt2);
  350. /* (4.3) */
  351. for (cnt = cnt + len3-cnt2+i; cnt < len; ++cnt) {
  352. p3[cnt] = 0;
  353. }
  354. #if 0
  355. printf("\nAfter:");
  356. for (cnt = 0; cnt < len3; ++cnt) {
  357. if (cnt%32 == 0)
  358. printf("\n%3lu:", cnt);
  359. printf(" %02x", p3[cnt]);
  360. }
  361. printf("\n");
  362. #endif
  363. len2 = sizeof(out);
  364. /* (5) */
  365. DO(ltc_mp.rsa_me(p3, len3, p2, &len2, PK_PRIVATE, &key));
  366. len3 = sizeof(tmp);
  367. /* (6) */
  368. if (i < 8)
  369. DOX(rsa_verify_hash_ex(p2, len2, p, 20, LTC_PKCS_1_V1_5, hash_idx, -1, &stat, &pubKey)
  370. == CRYPT_INVALID_PACKET ? CRYPT_OK:CRYPT_INVALID_PACKET, "should fail");
  371. else
  372. DOX(rsa_verify_hash_ex(p2, len2, p, 20, LTC_PKCS_1_V1_5, hash_idx, -1, &stat, &pubKey), "should succeed");
  373. DOX(stat == 0?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, "should fail");
  374. }
  375. /* free the key and return */
  376. rsa_free(&key);
  377. rsa_free(&pubKey);
  378. rsa_free(&privKey);
  379. return 0;
  380. }
  381. #else
  382. int rsa_test(void)
  383. {
  384. fprintf(stderr, "NOP");
  385. return 0;
  386. }
  387. #endif
  388. /* $Source$ */
  389. /* $Revision$ */
  390. /* $Date$ */