Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Isolated testcase:
- * BF_ecb_encrypt vs. EVP_bf_ecb
- *
- * g++ -o ecb2 -lcrypto -lssl ecb2.cpp
- **/
- #include <stdlib.h>
- #include <assert.h>
- #include <string.h>
- #include <openssl/evp.h>
- #include <openssl/blowfish.h>
- #include <openssl/err.h>
- #include <openssl/ssl.h>
- BIO* bio_err;
- #define SAMPLE_KEY "SomeRandomBlahz0c5GZVnR"
- #define STRING_TO_ENCRYPT "Hello World!"
- void do_evp()
- {
- unsigned char outbuf[100] = {0};
- int outlen = 0, tmplen = 0;
- const char *key = SAMPLE_KEY;
- const char *intext = STRING_TO_ENCRYPT;
- int inlen = strlen(intext);
- EVP_CIPHER_CTX ctx;
- EVP_CIPHER_CTX_init(&ctx);
- #ifdef BUG
- EVP_CipherInit_ex(&ctx, EVP_bf_ecb(), NULL, (unsigned char*)key, NULL, true);
- #else
- EVP_CipherInit_ex(&ctx, EVP_bf_ecb(), NULL, NULL, NULL, true);
- EVP_CIPHER_CTX_set_key_length(&ctx, strlen(key));
- EVP_CipherInit_ex(&ctx, NULL, NULL, (unsigned char*)key, NULL, true);
- #endif
- EVP_CipherUpdate(&ctx, outbuf, &outlen, (unsigned char*)intext, inlen);
- EVP_CipherFinal_ex(&ctx, outbuf + outlen, &tmplen);
- outlen += tmplen;
- for(int i = 0; i < outlen; i++)
- {
- printf("\\x%02X", outbuf[i]);
- }
- printf("\n");
- EVP_CIPHER_CTX_cleanup(&ctx);
- }
- void do_bf_ecb()
- {
- char outbuf[100] = {0};
- const char *key = SAMPLE_KEY;
- #ifdef BUG
- const char *intext = "\x56\x2B\x60\x0F\xB6\x15\x47\x49\x5B\xB5\x2F\x00\xD3\x30\x67\xF4"; // incorrect output from do_evp()...
- #else
- 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
- #endif
- BF_KEY bf_key;
- BF_set_key(&bf_key, strlen(key), (unsigned char*)key);
- BF_ecb_encrypt((unsigned char*)intext, (unsigned char*)outbuf, &bf_key, false);
- BF_ecb_encrypt((unsigned char*)(intext + 8), (unsigned char*)(outbuf + 8), &bf_key, false);
- printf("Decrypted: %s\n", outbuf);
- }
- int main()
- {
- OpenSSL_add_all_algorithms();
- ERR_load_crypto_strings();
- SSL_load_error_strings();
- bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
- do_evp();
- ERR_print_errors(bio_err);
- do_bf_ecb();
- ERR_print_errors(bio_err);
- EVP_cleanup();
- ERR_free_strings();
- return 0;
- }
Add Comment
Please, Sign In to add comment