Advertisement
Sweetening

Untitled

Oct 30th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #include "e_os.h"
  5.  
  6. #include <openssl/crypto.h>
  7. #include <openssl/err.h>
  8. #include <openssl/rand.h>
  9. #ifdef OPENSSL_NO_RSA
  10. int main(int argc, char *argv[])
  11. {
  12. printf("No RSA support\n");
  13. return(0);
  14. }
  15. #else
  16. #include <openssl/rsa.h>
  17.  
  18. #define SetKey \
  19. key->n = BN_bin2bn(n, sizeof(n)-1, key->n); \
  20. key->e = BN_bin2bn(e, sizeof(e)-1, key->e); \
  21. key->d = BN_bin2bn(d, sizeof(d)-1, key->d); \
  22. key->p = BN_bin2bn(p, sizeof(p)-1, key->p); \
  23. key->q = BN_bin2bn(q, sizeof(q)-1, key->q); \
  24. key->dmp1 = BN_bin2bn(dmp1, sizeof(dmp1)-1, key->dmp1); \
  25. key->dmq1 = BN_bin2bn(dmq1, sizeof(dmq1)-1, key->dmq1); \
  26. key->iqmp = BN_bin2bn(iqmp, sizeof(iqmp)-1, key->iqmp); \
  27. memcpy(c, ctext_ex, sizeof(ctext_ex) - 1); \
  28. return (sizeof(ctext_ex) - 1);
  29.  
  30. static int key1(RSA *key, unsigned char *c)
  31. {
  32. static unsigned char n[] =
  33. "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F"
  34. "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5"
  35. "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93"
  36. "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1"
  37. "\xF5";
  38.  
  39. static unsigned char e[] = "\x11";
  40.  
  41. static unsigned char d[] =
  42. "\x0A\x03\x37\x48\x62\x64\x87\x69\x5F\x5F\x30\xBC\x38\xB9\x8B\x44"
  43. "\xC2\xCD\x2D\xFF\x43\x40\x98\xCD\x20\xD8\xA1\x38\xD0\x90\xBF\x64"
  44. "\x79\x7C\x3F\xA7\xA2\xCD\xCB\x3C\xD1\xE0\xBD\xBA\x26\x54\xB4\xF9"
  45. "\xDF\x8E\x8A\xE5\x9D\x73\x3D\x9F\x33\xB3\x01\x62\x4A\xFD\x1D\x51";
  46.  
  47. static unsigned char p[] =
  48. "\x00\xD8\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5"
  49. "\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x12"
  50. "\x0D";
  51.  
  52. static unsigned char q[] =
  53. "\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9"
  54. "\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D"
  55. "\x89";
  56.  
  57. static unsigned char dmp1[] =
  58. "\x59\x0B\x95\x72\xA2\xC2\xA9\xC4\x06\x05\x9D\xC2\xAB\x2F\x1D\xAF"
  59. "\xEB\x7E\x8B\x4F\x10\xA7\x54\x9E\x8E\xED\xF5\xB4\xFC\xE0\x9E\x05";
  60.  
  61. static unsigned char dmq1[] =
  62. "\x00\x8E\x3C\x05\x21\xFE\x15\xE0\xEA\x06\xA3\x6F\xF0\xF1\x0C\x99"
  63. "\x52\xC3\x5B\x7A\x75\x14\xFD\x32\x38\xB8\x0A\xAD\x52\x98\x62\x8D"
  64. "\x51";
  65.  
  66. static unsigned char iqmp[] =
  67. "\x36\x3F\xF7\x18\x9D\xA8\xE9\x0B\x1D\x34\x1F\x71\xD0\x9B\x76\xA8"
  68. "\xA9\x43\xE1\x1D\x10\xB2\x4D\x24\x9F\x2D\xEA\xFE\xF8\x0C\x18\x26";
  69.  
  70. static unsigned char ctext_ex[] =
  71. "\x1b\x8f\x05\xf9\xca\x1a\x79\x52\x6e\x53\xf3\xcc\x51\x4f\xdb\x89"
  72. "\x2b\xfb\x91\x93\x23\x1e\x78\xb9\x92\xe6\x8d\x50\xa4\x80\xcb\x52"
  73. "\x33\x89\x5c\x74\x95\x8d\x5d\x02\xab\x8c\x0f\xd0\x40\xeb\x58\x44"
  74. "\xb0\x05\xc3\x9e\xd8\x27\x4a\x9d\xbf\xa8\x06\x71\x40\x94\x39\xd2";
  75.  
  76. SetKey;
  77. }
  78.  
  79. // Add similar key2 and key3 functions here
  80.  
  81. static int pad_unknown(void)
  82. {
  83. unsigned long l;
  84. while ((l = ERR_get_error()) != 0)
  85. if (ERR_GET_REASON(l) == RSA_R_UNKNOWN_PADDING_TYPE)
  86. return(1);
  87. return(0);
  88. }
  89.  
  90. static const char rnd_seed[] = "string to make the random number generator think it has entropy";
  91.  
  92. int main(int argc, char *argv[])
  93. {
  94. int err=0;
  95. int v;
  96. RSA *key;
  97. unsigned char ptext[256];
  98. unsigned char ctext[256];
  99. static unsigned char ptext_ex[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
  100. unsigned char ctext_ex[256];
  101. int plen;
  102. int clen = 0;
  103. int num;
  104.  
  105. CRYPTO_malloc_debug_init();
  106. CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
  107. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  108.  
  109. RAND_seed(rnd_seed, sizeof rnd_seed); /* or OAEP may fail */
  110.  
  111. plen = sizeof(ptext_ex) - 1;
  112.  
  113. for (v = 0; v < 3; v++)
  114. {
  115. key = RSA_new();
  116. switch (v) {
  117. case 0:
  118. clen = key1(key, ctext_ex);
  119. break;
  120. case 1:
  121. clen = key2(key, ctext_ex);
  122. break;
  123. case 2:
  124. clen = key3(key, ctext_ex);
  125. break;
  126. }
  127.  
  128. num = RSA_public_encrypt(plen, ptext_ex, ctext, key,
  129. RSA_PKCS1_PADDING);
  130. if (num != clen)
  131. {
  132. printf("PKCS#1 v1.5 encryption failed!\n");
  133. err=1;
  134. goto oaep;
  135. }
  136.  
  137. num = RSA_private_decrypt(num, ctext, ptext, key,
  138. RSA_PKCS1_PADDING);
  139. if (num != plen || memcmp(ptext, ptext_ex, num) != 0)
  140. {
  141. printf("PKCS#1 v1.5 decryption failed!\n");
  142. err=1;
  143. }
  144. else
  145. printf("PKCS #1 v1.5 encryption/decryption ok");
  146.  
  147. oaep:
  148. ERR_clear_error();
  149. num = RSA_public_encrypt(plen, ptext_ex, ctext, key,
  150. RSA_PKCS1_OAEP_PADDING);
  151. if (num == -1 && pad_unknown())
  152. {
  153. printf("No OAEP support\n");
  154. goto next;
  155. }
  156. if (num != clen)
  157. {
  158. printf("OAEP encryption failed!\n");
  159. err=1;
  160. goto next;
  161. }
  162.  
  163. num = RSA_private_decrypt(num, ctext, ptext, key,
  164. RSA_PKCS1_OAEP_PADDING);
  165. if (num != plen || memcmp(ptext, ptext_ex, num) != 0)
  166. {
  167. printf("OAEP decryption (encrypted data) failed!\n");
  168. err=1;
  169. }
  170. else if (memcmp(ctext, ctext_ex, num) == 0)
  171. {
  172. printf("OAEP test vector %d passed!\n", v);
  173. goto next;
  174. }
  175.  
  176. /* Different ciphertexts (rsa_oaep.c without -DPKCS_TESTVECT).
  177. Try decrypting ctext_ex */
  178.  
  179. num = RSA_private_decrypt(clen, ctext_ex, ptext, key,
  180. RSA_PKCS1_OAEP_PADDING);
  181.  
  182. if (num != plen || memcmp(ptext, ptext_ex, num) != 0)
  183. {
  184. printf("OAEP decryption (test vector data) failed!\n");
  185. err=1;
  186. }
  187. else
  188. printf("OAEP encryption/decryption ok\n");
  189. next:
  190. RSA_free(key);
  191. }
  192.  
  193. CRYPTO_cleanup_all_ex_data();
  194. ERR_remove_state(0);
  195.  
  196. CRYPTO_mem_leaks_fp(stderr);
  197.  
  198. return err;
  199. }
  200. #endif
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement