Guest User

Untitled

a guest
Aug 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. /**
  2. * Isolated testcase:
  3. * BF_ecb_encrypt vs. EVP_bf_ecb
  4. *
  5. * g++ -o ecb2 -lcrypto -lssl ecb2.cpp
  6. **/
  7.  
  8. #include <stdlib.h>
  9. #include <assert.h>
  10. #include <string.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/blowfish.h>
  13. #include <openssl/err.h>
  14. #include <openssl/ssl.h>
  15.  
  16. BIO* bio_err;
  17.  
  18. #define SAMPLE_KEY "SomeRandomBlahz0c5GZVnR"
  19. #define STRING_TO_ENCRYPT "Hello World!"
  20.  
  21. void do_evp()
  22. {
  23. unsigned char outbuf[100] = {0};
  24. int outlen = 0, tmplen = 0;
  25.  
  26. const char *key = SAMPLE_KEY;
  27. const char *intext = STRING_TO_ENCRYPT;
  28. int inlen = strlen(intext);
  29.  
  30. EVP_CIPHER_CTX ctx;
  31. EVP_CIPHER_CTX_init(&ctx);
  32.  
  33. #ifdef BUG
  34.  
  35. EVP_CipherInit_ex(&ctx, EVP_bf_ecb(), NULL, (unsigned char*)key, NULL, true);
  36.  
  37. #else
  38.  
  39. EVP_CipherInit_ex(&ctx, EVP_bf_ecb(), NULL, NULL, NULL, true);
  40.  
  41. EVP_CIPHER_CTX_set_key_length(&ctx, strlen(key));
  42.  
  43. EVP_CipherInit_ex(&ctx, NULL, NULL, (unsigned char*)key, NULL, true);
  44.  
  45. #endif
  46.  
  47. EVP_CipherUpdate(&ctx, outbuf, &outlen, (unsigned char*)intext, inlen);
  48.  
  49. EVP_CipherFinal_ex(&ctx, outbuf + outlen, &tmplen);
  50. outlen += tmplen;
  51.  
  52. for(int i = 0; i < outlen; i++)
  53. {
  54. printf("\\x%02X", outbuf[i]);
  55. }
  56. printf("\n");
  57.  
  58. EVP_CIPHER_CTX_cleanup(&ctx);
  59. }
  60.  
  61.  
  62. void do_bf_ecb()
  63. {
  64. char outbuf[100] = {0};
  65.  
  66. const char *key = SAMPLE_KEY;
  67. #ifdef BUG
  68. const char *intext = "\x56\x2B\x60\x0F\xB6\x15\x47\x49\x5B\xB5\x2F\x00\xD3\x30\x67\xF4"; // incorrect output from do_evp()...
  69. #else
  70. const char *intext = "\x6D\x38\x5F\x42\x4A\xAB\x0C\xFB\xF0\xBB\x86\xE0\x7F\xFB\x7D\x71"; // (correct) output from do_evp(), length = 16 bytes
  71. #endif
  72.  
  73. BF_KEY bf_key;
  74. BF_set_key(&bf_key, strlen(key), (unsigned char*)key);
  75.  
  76. BF_ecb_encrypt((unsigned char*)intext, (unsigned char*)outbuf, &bf_key, false);
  77. BF_ecb_encrypt((unsigned char*)(intext + 8), (unsigned char*)(outbuf + 8), &bf_key, false);
  78.  
  79. printf("Decrypted: %s\n", outbuf);
  80. }
  81.  
  82.  
  83. int main()
  84. {
  85. OpenSSL_add_all_algorithms();
  86. ERR_load_crypto_strings();
  87. SSL_load_error_strings();
  88.  
  89. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  90.  
  91. do_evp();
  92. ERR_print_errors(bio_err);
  93.  
  94. do_bf_ecb();
  95. ERR_print_errors(bio_err);
  96.  
  97. EVP_cleanup();
  98. ERR_free_strings();
  99.  
  100. return 0;
  101. }
Add Comment
Please, Sign In to add comment