2
0

datatest.c.old 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "crypto.h"
  2. #include <string.h>
  3. #include <stdlib.h>
  4. //#define DEBUG_TEST
  5. typedef enum {
  6. encrypt, decrypt
  7. } CryptoMode;
  8. void hex_dump(const char* header, const unsigned char* data, const unsigned int data_length)
  9. {
  10. unsigned int byte_count;
  11. printf("%s (%d bytes):\n", header, data_length);
  12. for(byte_count = 0; byte_count < data_length; ++byte_count)
  13. {
  14. printf("%02X", data[byte_count]);
  15. }
  16. printf("\n");
  17. }
  18. void do_rsa(const CryptoMode crypto_mode,
  19. const unsigned char* data, const unsigned int data_length,
  20. const unsigned char* modulus, const unsigned int modulus_length,
  21. const unsigned char* exponent, const unsigned int exponent_length,
  22. unsigned char* result, const unsigned int result_length)
  23. {
  24. RSA_CTX* rsa_context = NULL;
  25. BI_CTX *bi_ctx;
  26. bigint *plaintext_bi;
  27. bigint *enc_data_bi, *dec_data_bi;
  28. #ifdef DEBUG_TEST
  29. printf("do_rsa:\n");
  30. hex_dump("data", data, data_length);
  31. hex_dump("modulus", modulus, modulus_length);
  32. hex_dump("exponent", exponent, exponent_length);
  33. #endif
  34. RSA_priv_key_new(&rsa_context, modulus, modulus_length, exponent, exponent_length, exponent, exponent_length);
  35. memset(result, 0, result_length);
  36. bi_ctx = rsa_context->bi_ctx;
  37. switch(crypto_mode)
  38. {
  39. case encrypt:
  40. #ifdef DEBUG_TEST
  41. printf("encrypt\n");
  42. #endif
  43. plaintext_bi = bi_import(bi_ctx, data, data_length);
  44. enc_data_bi = RSA_public(rsa_context, plaintext_bi);
  45. bi_export(bi_ctx, enc_data_bi, result, result_length);
  46. break;
  47. case decrypt:
  48. #ifdef DEBUG_TEST
  49. printf("decrypt\n");
  50. #endif
  51. plaintext_bi = bi_import(bi_ctx, data, data_length);
  52. dec_data_bi = RSA_private(rsa_context, plaintext_bi);
  53. bi_export(bi_ctx, dec_data_bi, result, result_length);
  54. break;
  55. }
  56. #ifdef DEBUG_TEST
  57. hex_dump("result", result, result_length);
  58. #endif
  59. RSA_free(rsa_context);
  60. }
  61. void test_matching(char* test_description,
  62. const unsigned char* expected, const unsigned int expected_length,
  63. const unsigned char* result, const unsigned int result_length)
  64. {
  65. int test_result = memcmp(expected, result, expected_length);
  66. printf("Testing %s ... ", test_description);
  67. if(test_result == 0)
  68. {
  69. printf("ok.\n");
  70. }
  71. else
  72. {
  73. printf("failed!\n");
  74. hex_dump("should be", expected, expected_length);
  75. hex_dump("but is", result, result_length);
  76. }
  77. }
  78. void encrypt_decrypt_should_yield_original(char* test_description,
  79. const unsigned char* data, const unsigned int data_length,
  80. const unsigned char* modulus, const unsigned int modulus_length,
  81. const unsigned char* private_exponent, const unsigned int private_exponent_length,
  82. const unsigned char* public_exponent, const unsigned int public_exponent_length,
  83. const unsigned char* cryptogram, const unsigned int cryptogram_length)
  84. {
  85. const unsigned int calculated_cryptogram_length = modulus_length;
  86. unsigned char* calculated_cryptogram = malloc(calculated_cryptogram_length);
  87. const unsigned int decrypted_data_length = modulus_length;
  88. unsigned char* decrypted_data = malloc(decrypted_data_length);
  89. printf("\nRunning \"%s\" ...\n", test_description);
  90. #ifdef DEBUG_TEST
  91. printf("encrypt_decrypt_should_yield_original:\n");
  92. hex_dump("data", data, data_length);
  93. hex_dump("modulus", modulus, modulus_length);
  94. hex_dump("private_exponent", private_exponent, private_exponent_length);
  95. hex_dump("public_exponent", public_exponent, public_exponent_length);
  96. hex_dump("cryptogram", cryptogram, cryptogram_length);
  97. #endif
  98. do_rsa(encrypt, data, data_length,
  99. modulus, modulus_length,
  100. private_exponent, private_exponent_length,
  101. calculated_cryptogram, calculated_cryptogram_length);
  102. #ifdef DEBUG_TEST
  103. hex_dump("calculated_cryptogram", calculated_cryptogram, calculated_cryptogram_length);
  104. #endif
  105. if(cryptogram != NULL)
  106. {
  107. test_matching("cryptogram", cryptogram, cryptogram_length,
  108. calculated_cryptogram, calculated_cryptogram_length);
  109. }
  110. do_rsa(decrypt, calculated_cryptogram, calculated_cryptogram_length,
  111. modulus, modulus_length,
  112. public_exponent, public_exponent_length,
  113. decrypted_data, decrypted_data_length);
  114. test_matching("decrypted plaintext", data, data_length,
  115. decrypted_data, decrypted_data_length);
  116. free(calculated_cryptogram);
  117. free(decrypted_data);
  118. }
  119. /* configure without CRT!
  120. prepare data with:
  121. > echo "<string>" |
  122. ruby -ne '$_.gsub!(/ /, "").scan(/../).each_with_index \
  123. { |b, i| print "\"\n\"" if i % 16 == 0; print "\\x" + b;}' */
  124. int main(int argc, char *argv[])
  125. {
  126. #if 0
  127. unsigned char stuff[] = {
  128. 0x22, 0x33, 0x44, 0x81,
  129. 0xF1, 0xFF, 0xAA, 0xBB,
  130. 0xCC, 0xDD, 0xEE , 0x01,
  131. 0x45, 0x44, 0xfa, 0x8d,
  132. 0xfa, 0x20, 0x99, 0xFF,
  133. 0xab, 0xda, 0xac, 0x40 };
  134. unsigned char resA[sizeof(stuff)*2], resB[sizeof(stuff)*2];
  135. BI_CTX *bi_ctx = bi_initialize();
  136. bigint *bi_data1, *bi_data2, *res1, *res2;
  137. bi_data1 = bi_import(bi_ctx, stuff, sizeof(stuff));
  138. bi_data2 = bi_import(bi_ctx, stuff, sizeof(stuff));
  139. res1 = bi_multiply(bi_ctx, bi_copy(bi_data1), bi_copy(bi_data2));
  140. res2 = bi_multiply(bi_ctx, bi_data1, bi_data2);
  141. bi_print("MULTIPLY", res1);
  142. bi_print("SQUARE", res2);
  143. bi_export(bi_ctx, res1, resA, sizeof(resA));
  144. bi_export(bi_ctx, res2, resB, sizeof(resB));
  145. if (memcmp(resA, resB, sizeof(resA)))
  146. printf("OUCH - difference!\n");
  147. bi_terminate(bi_ctx);
  148. exit(0);
  149. #endif
  150. encrypt_decrypt_should_yield_original("Works only with Montgomery",
  151. (const unsigned char*) /* data */
  152. "\xBC\xD3\x12\x6C\x93\x13\x14\x4C\x00\x5D\xFD\xBF\xDE\xE4\xD3\x60"
  153. "\x29\xB8\xAE\x47\xBE\x0B\xB6\x0A\x39\x88\xB7\x93\x19\x14\xE8\x88"
  154. "\x4A\xDE\x00\x46\x89\x5A\x11\x1A\xC4\x8F\xE8\xF7\x27\xAC\x59\x80"
  155. "\x03\xC1\x93\x14\x01\x00\x93\x15\x07\x00\x00\x00\x01\x01\x05\x20"
  156. "\x93\x16\x0F\x42\x34\x33\x3A\x58\x30\x30\x30\x31\x30\x31\x30\x30"
  157. "\x30\x31\x92\x6B\x10\x6C\x69\x62\x65\x6C\x6D\x65\x74\x72\x65\x65"
  158. "\x2E\x73\x6F\x2E\x30\x93\x18\x02\xA5\x92\x92\x6C\x03\x96\xE3\x0C"
  159. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xB7\xBE", 128,
  160. (const unsigned char*) /* modulus */
  161. "\xc4\x5a\xcb\x35\x95\xad\x32\x4a\xcf\x9c\x82\x45\x13\xb7\x42\x35"
  162. "\x22\x32\x6d\x2e\x6d\x26\x2e\x6d\x00\x9b\xae\x2d\x9e\x78\x1e\xdd"
  163. "\x40\x23\x17\xa8\xbb\xa1\x07\x86\xb4\x3c\xbc\xe8\xd5\xfc\xd9\xeb"
  164. "\x3c\xad\x63\x11\xf3\x1d\x64\x81\x96\xf2\xf5\xfe\xca\x5a\xf7\x8a"
  165. "\x15\xcb\x90\x81\x68\xae\x59\xb4\xe1\xa4\x41\x99\xcd\xf3\x98\xbd"
  166. "\x3c\x48\x37\xdb\xa1\xc3\x1c\x6f\x43\xd1\x89\x23\xe5\x3d\xa3\xa5"
  167. "\x92\x7b\x19\x14\x1e\x7a\xf3\x88\x8a\x36\x21\x3e\x16\x40\x3c\xd7"
  168. "\xd3\xdb\x13\xaf\xc9\x68\x45\x84\xb3\x39\x8f\x02\xed\x28\x02\x5f", 128,
  169. (const unsigned char*) /* private exponent */
  170. "\x5d\x19\xb7\xb4\x66\x8d\xc2\x84\xda\x3f\x99\x3c\xeb\x86\x3e\xec"
  171. "\x36\x94\xb6\x54\x07\x08\xcd\x86\x7d\x7d\x53\x6e\xe9\xee\x86\xa3"
  172. "\xdd\x5f\x46\x3e\x89\x08\x67\x2b\x25\x96\x8e\xf3\xcf\x52\x9e\x78"
  173. "\xfd\x42\x30\xf1\x37\xd6\xbd\xea\xfc\x09\xa3\x3d\xf5\xf0\x7f\xe1"
  174. "\xb1\xe0\x69\x13\x44\xf9\x8b\x95\x58\x2a\x81\xb3\xa8\x15\xce\x7e"
  175. "\xd3\xea\x97\x0a\xa2\x14\xd4\xae\xc7\x75\xbb\x9f\x68\xa5\x53\x0e"
  176. "\x85\x29\x88\x48\x6c\xc9\xcc\xde\x72\x40\x3a\x4c\x82\xde\x3c\xfb"
  177. "\x08\xf8\x2c\x26\xb5\xd4\xea\xc4\xca\x98\x6e\x43\x3e\x67\x54\xc1", 128,
  178. (const unsigned char*) /* public exponent */
  179. "\x01\x00\x01", 3,
  180. (const unsigned char*) /* precalculated encrypted data */
  181. "\x93\xE8\x1F\xF9\x70\xFA\xAA\xED\x54\xFD\x48\x37\xC9\x71\x9A\x11"
  182. "\x69\x80\xB4\x22\x0C\xAD\x5A\x95\x65\xCA\x7C\xF7\x70\x56\x92\xCB"
  183. "\x45\x6D\x58\x84\x21\x80\x23\x76\x21\x4A\x61\x99\xC1\x11\x9C\x0F"
  184. "\x40\xED\x80\x9C\x8F\x3A\x4F\x01\xB5\x72\xC3\x24\xAE\xF3\x6B\x98"
  185. "\xA8\x60\xAC\xAF\x95\x98\x9A\xAA\xA4\x28\xF2\x02\x05\xFC\xF3\xDD"
  186. "\xB0\x5A\x4E\xDE\x3C\x41\x4B\x1C\x5B\x1F\xF6\x3D\xAF\x93\x43\xCB"
  187. "\xD8\xC7\x24\x97\x8F\x49\xE5\x5B\x10\x51\x3B\x1E\xA6\x39\xEA\x4E"
  188. "\xA5\xE0\x71\x8C\xCA\x34\x8C\x2F\x6C\x5C\x78\x34\x86\x7C\x54\x6A", 128);
  189. encrypt_decrypt_should_yield_original("Works only with Barrett",
  190. (const unsigned char*) /* data */
  191. "\x36\x42\x32\xe4\x1e\x78\x02\x8e\xfb\x64\x5f\x0c\xfc\x5a\xd7\x5c"
  192. "\xe4\xb5\x91\x5c\x4b\x00\x87\x28\x87\x9b\xa0\x4b\x09\xc2\x6b\x64"
  193. "\xac\x4b\xcf\xa5\xee\x8a\xb7\xc9\xc9\x90\x02\xc1\xa3\x47\x5c\x6b"
  194. "\x71\x5d\x5d\x49\x27\xe1\x15\xc6\xcf\x37\x9e\xa7\x0f\xa1\xad\x96"
  195. "\x83\xef\x4b\x53\x68\xcd\x77\xfc\x14\x5f\xf5\xb7\x78\xb0\x10\xeb"
  196. "\x0d\x61\x94\x01\xf6\xaa\x1b\x19\x23\x39\xa7\xcc\x6c\x42\x4a\x87"
  197. "\x79\x27\x04\xc6\xec\x8e\x50\xba\xb9\x26\x89\xd4\x00\x01\x25\xe5"
  198. "\xf3\x9e\x98\x0c\x8d\x2e\x43\x1e\xe9\x29\x90\xd2\x75\x61\x85\xe7", 128,
  199. (const unsigned char*) /* modulus */
  200. "\x37\x0c\x32\xe4\x1e\x78\x02\x8e\xfb\x64\x5f\x0c\xfc\x5a\xd7\x5c"
  201. "\xe4\xb5\x91\x5c\x4b\x00\x87\x28\x87\x9b\xa0\x4b\x09\xc2\x6b\x64"
  202. "\xac\x4b\xcf\xa5\xee\x8a\xb7\xc9\xc9\x90\x02\xc1\xa3\x47\x5c\x6b"
  203. "\x71\x5d\x5d\x49\x27\xe1\x15\xc6\xcf\x37\x9e\xa7\x0f\xa1\xad\x96"
  204. "\x83\xef\x4b\x53\x68\xcd\x77\xfc\x14\x5f\xf5\xb7\x78\xb0\x10\xeb"
  205. "\x0d\x61\x94\x01\xf6\xaa\x1b\x19\x23\x39\xa7\xcc\x6c\x42\x4a\x87"
  206. "\x79\x27\x04\xc6\xec\x8e\x50\xba\xb9\x26\x89\xd4\x00\x01\x25\xe5"
  207. "\xf3\x9e\x98\x0c\x8d\x2e\x43\x1e\xe9\x29\x90\xd2\x75\x61\x85\xe7", 128,
  208. (const unsigned char*) /* private exponent */
  209. "\x16\x3a\x76\xd2\x66\xfb\x4f\x0d\x2d\xb6\x7a\x2b\x64\x3b\xca\x7b"
  210. "\x58\x5f\x79\x33\x2b\x96\x2a\xfd\xd2\xc4\xa5\x15\xa7\xfb\x3a\x22"
  211. "\x8c\xf0\x90\x09\x11\x2a\x32\xcc\xe8\xf7\x9e\x25\x53\x29\x9d\xc8"
  212. "\x45\x1e\xce\x6c\x9c\x0d\xe8\x1d\x3f\xcf\xd5\xe0\xe0\x0f\x09\x69"
  213. "\x2d\xe7\xd5\xe6\xe5\x10\xd9\x4e\x20\xdb\xbd\xa1\x04\x6b\xe6\x1d"
  214. "\x4c\x79\x28\x47\x30\x11\xde\x14\xb4\x6e\x35\x98\x38\x50\x44\x82"
  215. "\xbd\xc4\xfb\x03\xb3\xf6\x5e\x5a\x29\xfa\x29\xaa\xde\xe4\xfd\x15"
  216. "\xbe\xed\x4f\x93\x9d\x0d\x29\xe8\xd7\xa3\xf4\x18\xc8\x98\xb1\x01", 128,
  217. (const unsigned char*) /* public exponent */
  218. "\x01\x00\x01", 3,
  219. NULL, 0);
  220. encrypt_decrypt_should_yield_original("Works always",
  221. (const unsigned char*) /* data */
  222. "\xB9\x42\x32\xe4\x1e\x78\x02\x8e\xfb\x64\x5f\x0c\xfc\x5a\xd7\x5c"
  223. "\xe4\xb5\x91\x5c\x4b\x00\x87\x28\x87\x9b\xa0\x4b\x09\xc2\x6b\x64"
  224. "\xac\x4b\xcf\xa5\xee\x8a\xb7\xc9\xc9\x90\x02\xc1\xa3\x47\x5c\x6b"
  225. "\x71\x5d\x5d\x49\x27\xe1\x15\xc6\xcf\x37\x9e\xa7\x0f\xa1\xad\x96"
  226. "\x83\xef\x4b\x53\x68\xcd\x77\xfc\x14\x5f\xf5\xb7\x78\xb0\x10\xeb"
  227. "\x0d\x61\x94\x01\xf6\xaa\x1b\x19\x23\x39\xa7\xcc\x6c\x42\x4a\x87"
  228. "\x79\x27\x04\xc6\xec\x8e\x50\xba\xb9\x26\x89\xd4\x00\x01\x25\xe5"
  229. "\xf3\x9e\x98\x0c\x8d\x2e\x43\x1e\xe9\x29\x90\xd2\x75\x61\x85\xe7", 128,
  230. (const unsigned char*) /* modulus */
  231. "\xB9\x77\xEC\x83\x95\xAF\xB1\xF8\x21\x21\xFF\x05\x5E\x0C\x91\x0C"
  232. "\x2E\xD5\xD2\x94\x1C\x38\x5E\xED\x5A\xCF\x84\xD0\x12\x8B\xAA\x4B"
  233. "\x3A\x63\x65\x78\x13\xED\x24\x4E\x83\xF2\xF5\x02\x66\x5D\xFC\xC1"
  234. "\x80\x5B\x78\x78\xB4\x0B\x45\xE5\x22\xC6\xCD\xEB\xCC\x74\x0B\x0B"
  235. "\xD8\x8B\x91\x99\x48\x8E\x74\xA9\xD0\x1A\x39\x94\xC2\xD4\x2E\x9A"
  236. "\x8C\x0C\x35\x0D\x97\x8F\xC4\x62\x20\xE9\x78\x40\x97\x05\x98\xE6"
  237. "\x22\x48\x3D\x3D\xCA\x6A\x3F\xEF\xB0\x23\x14\x30\xDA\x35\x46\x65"
  238. "\x55\xEF\xEB\xA1\xA9\xCF\x83\xE7\xEF\xF2\x83\x6D\x38\xEA\x88\xED", 128,
  239. (const unsigned char*) /* private exponent */
  240. "\x52\x2A\x68\xE3\x9A\xAA\xED\xA3\x49\xBA\x6F\xEA\x86\xD1\xF6\x68"
  241. "\x79\x4F\x4D\x2D\x44\x9B\x4C\xA2\xC6\xBA\x6C\xD2\x69\x84\xEA\x7A"
  242. "\xCD\x71\x3F\x80\xC5\x03\x28\x34\x88\x8C\x58\x33\x29\xFA\xB5\x81"
  243. "\x5C\x46\x29\xC6\xFF\xAC\x86\xD8\x8E\x61\x98\xD4\xC0\x0D\x20\xDE"
  244. "\xEB\x61\x1C\x0C\x3C\x19\xA3\x75\x10\x7D\xDA\xA9\x55\xA7\x64\x5F"
  245. "\xE0\xB6\x35\x62\x00\xD9\xD2\xF7\xA4\xDF\x85\xFF\xDF\x86\x75\x29"
  246. "\x66\x16\x03\x8C\xC0\xB0\x3F\xAB\xBA\x41\xB3\x3C\x76\x58\xB6\xE2"
  247. "\x1F\x36\x47\x5F\x1F\x0E\x4C\xB5\x29\x90\xDC\xA1\xF8\xFA\x58\x19", 128,
  248. (const unsigned char*) /* public exponent */
  249. "\x01\x00\x01", 3,
  250. NULL, 0);
  251. return 0;
  252. }