BaH.Crypto

BlitzMax Forums/Brucey's Modules/BaH.Crypto

Brucey(Posted 2008) [#1]
Well, it's been such a long time since I came out with a new module (if you don't count the one from a week or so ago :-p ), that I thought I should just knock together something for the weekend.

The Crypto module uses OpenSSL's crypto libs ( "libcrypto" on Linux/Mac and "eay32.dll" on Win32) to do all the hard work of ciphering.

It currently supports BlowFish, CAST, DES, RC2, RC4 and RC5, in various modes (see HERE for a good description of the modes and how they work).

Currently only tested on OS X, which has OpenSSL installed by default. Linux should be okay too, as most distros package OpenSSL, although you may need to download the Dev package.

On Win32, you'll need the OpenSSL DLLs. I've shipped the MinGW .a and includes, so it might compile. (Haven't tested this yet.. Later today hopefully - once Eskom turn the power back on - bloody rolling blackouts!)

Available in SVN only until it's all working fine on all platforms...

http://code.google.com/p/maxmods/wiki/CryptoModule

:o)


Oh, and I hope to be adding Digest support in the not too distant future too. (eg. HMAC, MD2, MD5, MDC2, SHA and RIPEMD-160)


Brucey(Posted 2008) [#2]
Now working fine on Win32... Yay!

Seems RC5 isn't compiled into the "usual" libs on Windows, so I've left it out of the module on that platform.

Downloading and installing "Win32 OpenSSL v0.9.8g Light" from HERE is enough to get the examples running. Note, if you want to distribute the libs with your app, you might want to include the libeay32.dll that gets installed into the system32 folder.

:o)


Brucey(Posted 2008) [#3]
Since everything *appears* to be working as expected so far... the first release is now available from HERE.


plash(Posted 2008) [#4]
Compiled mod, then installed "Win32 OpenSSL v0.9.8g Light".

BOTH examples give me this error.
The procedure entry point EVP_bf_cfb64 could not be located in the dynamic link library libeay32.dll.


Did I miss anything?


Brucey(Posted 2008) [#5]
Haw... that's strange.

Originally tested it on XP, worked okay.
Just installed OpenSSL on Win2k and it is also working as expected.

Are you sure that you are using that latest dll? (perhaps there is another on your PATH that is found first?)

Try copying it into the app dir and see if that works better?
(when I installed v0.9.8g onto the win2k, it did ask if I wanted to overwrite the DLLs in System32 - which I said yes to).


plash(Posted 2008) [#6]
Ahh.. the problem was, I initially installed an older version (didn't look to much at the instructions you gave) of the OpenSSL dll, which I placed in the app and sys32 dir.

After installing v0.9.8g I didn't replace the one in the app dir with the new dll (which the installer placed in sys32).

Works now :)


Chroma(Posted 2008) [#7]
Any idea when IDEA will be supported Brucey?

Also, which encryption in the mod is the fastest?


Brucey(Posted 2008) [#8]
IDEA seems to excluded from most default builds of OpenSSL because of patent issues with Ascom-Tech AG. It appears you need a license for commercial apps.

As for the fastest, I haven't given them all a thorough test. You might wish to try the different combinations of the algorithms with the various modes.


slenkar(Posted 2008) [#9]
is this module used to encrypt files?


Brucey(Posted 2008) [#10]
If you like.

There's an example which shows how to use a TStream for input/output.
In theory you could then also use a RamStream and do stuff in-memory, assuming you setup enough space for it (streaming out of ram shouldn't be a problem).

The other example shows encryption from/to a String.

Basically, all you need is some kind of data to feed it.


Sub_Zero(Posted 2011) [#11]
Hi and thank you very much for this module, it is working great.

However, I've encountered a bug in v1.02/v1.03 when it comes to encrypting larger (>3000 bytes) of data. Basically what i was trying to do was encrypt all my .bmx files, and encountered a segmentation fault on some of the larger files... When calling encryptupdate and or encryptfinal sometimes dumped alot of crap onscreen and then a segmentation fault, some just threw a segmentation fault, and the rest of the files encrypted ok... I did not use the TStream method, instead i used loadtext and then stringtobytearray before encrypting.

I also looked over you code, and found no errors whatsoever, it looked perfectly clean, so I tried to build the module against various subversions of libssl 0.9.8 stable without any luck. I also tried to compile the module on different stations (intel/amd), but still no luck.

I also tried passing 2048 bytes at a time to encryptupdate before doing a encryptfinal, but still segmentation fault.

Strange this segmentation fault, since it happens only to 5% of the files i tried to encrypt, but it seems it happens to only of the larger files.

Also i tried different ciphers, aes128cbc, aes256cbc, bf-cbc, etc... Still the same result.

So i beg you please can we try to figure out this problem.... And ofcourse, I'm running linux.

You've done alot of wonderful work since the time I suggested you to make the gtkwebmozilla module ;) Good work indeed.

Kind regards from norway, Sub Zero

Last edited 2011


Brucey(Posted 2011) [#12]
The "crap" is part of the memory dump when linux seg faults.

I'll write up a little test which can handle large files and see how it performs for me.
The encrypt_decrypt example isn't really appropriate because you don't really want to do it in a single hit...


Brucey(Posted 2011) [#13]
Hi again.

It appears to be a problem with your code. I've just written an example which encrypts (and decrypts) the entire text of war and peace (about 3.3mb) and it does this without crashing. Sure, it did take me a while to work out the best method, but if the code is written correctly, it works as you'd expect. :)




Sub_Zero(Posted 2011) [#14]
Ah, thanks alot for that.... I forgot to post, I also solved the problem using cipherinit/update/final instead, according to your example...

But thanks, keep up the good work, you're doing great.


Sub_Zero(Posted 2013) [#15]
Looking back, it might have been because the keylength was wrong (oops)

By the way, now when i tried to compile your example, i get an error stating GetBlockSize() not found....

Would this be the equivalent to EVP_MAX_BLOCK_LENGTH ?
EVP_MAX_BLOCK_LENGTH

Edit: Now I know what's wrong... I've downloaded v1.02 from googlecode.... Why don't you put the v1.03 version in hte download section instead ;)


Brucey(Posted 2013) [#16]
Yes, it's probably a good idea. :-)


Sub_Zero(Posted 2013) [#17]
Thanks alot :)


Sub_Zero(Posted 2013) [#18]
I'm having some problems again:



Encryption/decryption breaks somehow.... Not sure why.. I have a another example where the almost exact same function works, but I'll have to email it to you. There is some strange behaviour.... And the exact same thing happens in windows aswell... I'm guessing it might be a openssl bug for now....

By the way I tried to send you an email, but it wouldn't let me....

So I'm posting an example that works like a charm here:




Brucey(Posted 2013) [#19]
Well, I'm not sure why it isn't working in your first example, but if I use the decrypt functions, it seems to work.


Sub_Zero(Posted 2013) [#20]
I can't get it to work even with your large file example when integrating in my larger programs... sometimes it works, sometimes it doesn't, and when turning on debug mode it breaks, and sometimes it works when commenting out import BRL.Eventqueue ! So I have no clue any more


Sub_Zero(Posted 2013) [#21]
Hi

I've installed the latest ubuntu wich uses libssl-dev_1.0.1c, and i get this message when trying to compile a program using BaH.Crypto:

/home/BlitzMax/mod/bah.mod/crypto.mod/crypto.debug.linux.x86.a(crypto.bmx.debug.linux.x86.o): In function `_bah_crypto_EVP_CIPHER_des_cfb':
(code+0x1262): undefined reference to `EVP_des_cfb'
/home/BlitzMax/mod/bah.mod/crypto.mod/crypto.debug.linux.x86.a(crypto.bmx.debug.linux.x86.o): In function `_bah_crypto_EVP_CIPHER_des_ede_cfb':
(code+0x14e7): undefined reference to `EVP_des_ede_cfb'
/home/BlitzMax/mod/bah.mod/crypto.mod/crypto.debug.linux.x86.a(crypto.bmx.debug.linux.x86.o): In function `_bah_crypto_EVP_CIPHER_des_ede3_cfb':
(code+0x16eb): undefined reference to `EVP_des_ede3_cfb'
/home/BlitzMax/mod/bah.mod/crypto.mod/crypto.debug.linux.x86.a(crypto.bmx.debug.linux.x86.o): In function `_bah_crypto_EVP_CIPHER_bf_cfb':
(code+0x18ef): undefined reference to `EVP_bf_cfb'
/home/BlitzMax/mod/bah.mod/crypto.mod/crypto.debug.linux.x86.a(crypto.bmx.debug.linux.x86.o): In function `_bah_crypto_EVP_CIPHER_rc2_cfb':
(code+0x1b74): undefined reference to `EVP_rc2_cfb'
/home/BlitzMax/mod/bah.mod/crypto.mod/crypto.debug.linux.x86.a(crypto.bmx.debug.linux.x86.o): In function `_bah_crypto_EVP_CIPHER_cast5_cfb':
(code+0x1d78): undefined reference to `EVP_cast5_cfb'


Might be some changes in the latest libssl-dev?