|
|
Home » Developing U++ » UppHub » Encrypted storage with streaming (OpenSSL, AES)
Encrypted storage with streaming (OpenSSL, AES) [message #23087] |
Wed, 16 September 2009 22:17 |
Mindtraveller
Messages: 917 Registered: August 2007 Location: Russia, Moscow rgn.
|
Experienced Contributor |
|
|
Sometimes we may have task to store some large file (4+ GB) or small string inside encrypted storage. I tried to make a pair classes which make it easy. This is the first version, so any ideas are welcome.
This package assumes you have OpenSSL library successfully installed and its paths are added to TheIDE.
OK, let me introduce a pair of classes called AESEncoderStream and AESDecoderStream. They support streamed adding and encryption/decryption of data. Encryption is made with AES (Rijndael) with 128, 192 or 256 bit keys.
Encrypted data 32 bytes larger than source length aligned to 16-byte boundary. I.e. if your source data is 170 bytes long, the resulting length is:
170 rounded by 16-byte pieces = 176
plus
32 (header data)
= 176 + 32 = 208 bytes.
Not so ugly for a number of applications especially if source data is large.
Here is a simple self-explanating demo:
#include <Core/Core.h>
#include <openssl/aes.h>
#include <AESStream/AESStream.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
AESInit();
// Generate cryptographically stable key
String key(AESRandomString(32));
// Encryption
String sIn,sOut;
sIn = "qwertyuiop[p\tasdfghjkl;zxcvbnm,./quwiueqiwueoiquweioquweioquweiqwueicuwinuqiweqiwue pqiueci eiqniuriryuweyruweyruewrycuwbrurbywuyrwquiercbbcrebrquwey";
AESEncoderStream aesEncoder(sIn.GetLength(), key);
aesEncoder << sIn.Left(10);
aesEncoder << sIn.Mid(10,10);
aesEncoder << sIn.Right(sIn.GetLength() - 20);
sOut << aesEncoder; //do streamed encoding
// Decryption
//key.Set(0, 'a'); //uncomment to see what happens with wrong key
AESDecoderStream aesDecoder(key);
aesDecoder << sOut.Left(15); //you may add by parts
aesDecoder << sOut.Right(sOut.GetLength() - 15);
try
{
String sDecoded;
sDecoded << aesDecoder; //throw exception if key is wrong
Cout() << (sDecoded == sIn) << "\n\n"; //check if all converted successfully
}
catch (const char *xp)
{
Cout() << "\n!!Error: " << ToSystemCharset(xp);
}
}
[Updated on: Fri, 18 September 2009 11:16] Report message to a moderator
|
|
|
|
Re: Encrypted storage with streaming (OpenSSL, AES) [message #23120 is a reply to message #23103] |
Thu, 17 September 2009 19:57 |
Weras
Messages: 4 Registered: August 2009
|
Junior Member |
|
|
Hi koldo!
I had resolved this problem and I did so.
1. Download openssl-0.9.8k.tar.gz from
https://www.openssl.org/source/
2. Unpack archive to C:\temp\openssl-0.9.8g
3. Download and install ActivePerl
4. Now type in the command line:
1) C:\temp\openssl-0.9.8g>perl Configure VC-WIN32 --prefix=c:/temp/openssl-bin/
2) C:\temp\openssl-0.9.8g>%comspec% /k ""c:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat"" x86
3) C:\temp\openssl-0.9.8g>ms\do_masm.bat
4) if you need static library write
C:\temp\openssl-0.9.8g>nmake -f ms\nt.mak
else, if you need dynamic library write
C:\temp\openssl-0.9.8g>nmake -f ms\ntdll.mak
5. The result is files*.lib & *.dll and include directory
6. Add .../openssl/inc32 as Include and .../openssl as Linker directories
Is enough to work with openssl
[Updated on: Thu, 17 September 2009 20:05] Report message to a moderator
|
|
|
|
|
Re: Encrypted storage with streaming (OpenSSL, AES) [message #23143 is a reply to message #23139] |
Fri, 18 September 2009 12:28 |
|
Mindtraveller wrote on Fri, 18 September 2009 12:20 | Hi, koldo!
It was Weras who instructed me how to install OpenSSL so I asked him to answer yur question.
Thanks for trying Linux version, it's a great effort.
I have just found a pair of small bugs and reuploaded sources. If you wish to try AESStream, pleas rewrite sources with new versions. If you manage to make POSIX version of rdtsc - it would be great too (I'm no professional in GCC-accepted assembler).
I wonder if people need this too. If it is so, someone from authors may upload AESStream to official Bazaar.
|
Very nice package and wiki how to build openssl on win32.
Thank you!
Ion Lupascu (tojocky)
|
|
|
Re: Encrypted storage with streaming (OpenSSL, AES) [message #23144 is a reply to message #23087] |
Fri, 18 September 2009 13:23 |
|
koldo
Messages: 3404 Registered: August 2008
|
Senior Veteran |
|
|
Hello Mindtraveller
About rdtsc, in fact it compiles in gcc as it has an ifdef so that:
- If MSC, it takes a value from rdtsc (time stamp counter 64-bit register)
- Else it takes Random()
It seems that to get a random number our Random() implementation is better than the clock (MT19937 algorithm), so perhaps rdtsc() would have to be changed with Random()
I have tried to compile in MinGW, but I get linking errors, in summary:
Openssl\out32\libeay32.lib(tmp32/ui_openssl.obj),(.text[_rea d_string_inner]+0xb): undefined reference to `__security_cookie'
Openssl\out32\libeay32.lib(tmp32/ui_openssl.obj),(.text[_rea d_string_inner]+0x149): undefined reference to `@__security_check_cookie@4'
Openssl\out32\libeay32.lib(tmp32/ecp_smpl.obj),(.text[_ec_GF p_simple_group_set_curve]+0x6): undefined reference to `_chkstk'
Does anybody know how to solve these problems with chkstk and security_cookie ?
Best regards
Koldo
Best regards
Iñaki
|
|
|
|
|
|
|
|
|
|
Re: Encrypted storage with streaming (OpenSSL, AES) [message #25394 is a reply to message #25392] |
Sun, 21 February 2010 12:01 |
Mindtraveller
Messages: 917 Registered: August 2007 Location: Russia, Moscow rgn.
|
Experienced Contributor |
|
|
Koldo, what is the package uppdev/textssl? Never heard of that before. You mean another OpenSSL-based package?
I also changed package a bit, added tutorial (English and Russian versions), added example. Please be so kind check if it compiles (currently don't have OpenSSL installed) and upload it to bazaar.
[Updated on: Sun, 21 February 2010 12:08] Report message to a moderator
|
|
|
Re: Encrypted storage with streaming (OpenSSL, AES) [message #25395 is a reply to message #25394] |
Sun, 21 February 2010 15:40 |
|
koldo
Messages: 3404 Registered: August 2008
|
Senior Veteran |
|
|
Mindtraveller wrote on Sun, 21 February 2010 12:01 | Koldo, what is the package uppdev/textssl? Never heard of that before. You mean another OpenSSL-based package?
I also changed package a bit, added tutorial (English and Russian versions), added example. Please be so kind check if it compiles (currently don't have OpenSSL installed) and upload it to bazaar.
|
Hello Mindtraveller
Web/SSL package is included in AESStream. A problem is it have memory leaks. I have found a solution to solve it, that is in Web/SSL/util.cpp:
INITBLOCK {
Socket::Init();
CRYPTO_set_mem_functions(SSLAlloc, SSLRealloc, SSLFree);
SSL_load_error_strings();
//SSL_library_init(); //SOLUTION TO MEMORY LEAK !!
}
Probably SSL_library_init(); is not necessary and as it not properly cleaned up, there are memory leaks.
To find this problem I have used uppdev/testssl package and Upp technology to detect memory leaks
I have tested your changes but they do not work well . Check this:
sIn includes this:
Quote: | qwertyuiop[p asdfghjkl;zxcvbnm,./quwiueqiwueoiquweioquweioquweiqwueicuwin uqiweqiwue pqiueci eiqniuriryuweyruweyruewrycuwbrurbywuyrwquiercbbcrebrquwey
|
sDecoded iincludes this:
Quote: | qwertyuiop[p asdfghjkl;zxcvbnm,./quwiueqiwueoiquweioquweioquweiqwueicuwin uqiweqiwue pqiueci eiqniuriryuweyruweyruewrycuwbrurbywuyrwquiercbbcrebr
|
Best regards
Iñaki
|
|
|
|
|
|
|
Goto Forum:
Current Time: Sat Dec 14 12:01:37 CET 2024
Total time taken to generate the page: 0.03722 seconds
|
|
|