Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aes openssl #18010

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions common/dllserver/thorplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,7 @@ extern DLLSERVER_API bool decompressResource(size32_t len, const void *data, Str
extern DLLSERVER_API void appendResource(MemoryBuffer & mb, size32_t len, const void *data, bool compress)
{
mb.append((byte)0x80).append(resourceHeaderVersion);
if (compress)
compressToBuffer(mb, len, data);
else
appendToBuffer(mb, len, data);
compressToBuffer(mb, len, data, compress ? COMPRESS_METHOD_LZW : COMPRESS_METHOD_NONE);
}

extern DLLSERVER_API void compressResource(MemoryBuffer & compressed, size32_t len, const void *data)
Expand Down
129 changes: 129 additions & 0 deletions system/jlib/jencrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#ifdef _USE_OPENSSL

#include "ske.hpp"
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>

#endif

Expand Down Expand Up @@ -1829,18 +1833,107 @@ size_t aesDecrypt(const void *key, size_t keylen, const void *input, size_t inle

} // end of namespace jlib

#ifdef _USE_OPENSSL
MemoryBuffer &aesEncrypt_ssl(const void *key, unsigned keylen, const void *plaintext, size_t plaintext_len, MemoryBuffer &output)
{
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx)
throw makeStringException(0, "Crap");
unsigned char iv[16] = { 0 };
switch (keylen)
{
case 32:
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char *) key, iv))
throw makeStringException(0, "Crap");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the most informative error message....

break;
case 24:
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_192_cbc(), NULL, (const unsigned char *) key, iv))
throw makeStringException(0, "Crap");
break;
case 16:
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, (const unsigned char *) key, iv))
throw makeStringException(0, "Crap");
break;
default:
throw makeStringException(0, "Crap");
}
byte *ciphertext = (byte *) output.reserve(plaintext_len + 100);
int ciphertext_len = 0;
int thislen = 0;
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &thislen, (const unsigned char *) plaintext, plaintext_len))
throw makeStringException(0, "Crap");
ciphertext_len += thislen;
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + ciphertext_len, &thislen))
throw makeStringException(0, "Crap");
ciphertext_len += thislen;
EVP_CIPHER_CTX_free(ctx);
output.setLength(ciphertext_len);
return output;
}

MemoryBuffer &aesDecrypt_ssl(const void *key, size_t keylen, const void *ciphertext, size_t ciphertext_len, MemoryBuffer &output)
{
EVP_CIPHER_CTX *ctx;

int thislen = 0;
int plaintext_len = 0;

if(!(ctx = EVP_CIPHER_CTX_new()))
throw makeStringException(0, "Crap");

unsigned char iv[16] = { 0 };
switch (keylen)
{
case 32:
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char *) key, iv))
throw makeStringException(0, "Crap");
break;
case 24:
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_192_cbc(), NULL, (const unsigned char *) key, iv))
throw makeStringException(0, "Crap");
break;
case 16:
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, (const unsigned char *) key, iv))
throw makeStringException(0, "Crap");
break;
default:
throw makeStringException(0, "Crap");
}
byte *plaintext = (byte *) output.reserve(ciphertext_len + 100);
if(1 != EVP_DecryptUpdate(ctx, plaintext, &thislen, (const unsigned char *) ciphertext, ciphertext_len))
throw makeStringException(0, "Crap");
plaintext_len += thislen;

if(1 != EVP_DecryptFinal_ex(ctx, plaintext + plaintext_len, &thislen))
throw makeStringException(0, "Crap");
plaintext_len += thislen;
EVP_CIPHER_CTX_free(ctx);
return output;
}

#endif

MemoryBuffer &aesEncrypt(const void *key, size_t keylen, const void *input, size_t inlen, MemoryBuffer &output)
{
#ifdef _USE_OPENSSL
return aesEncrypt_ssl(key, keylen, input, inlen, output);
#else
return jlib::aesEncrypt(key, keylen, input, inlen, output);
#endif
}

MemoryBuffer &aesDecrypt(const void *key, size_t keylen, const void *input, size_t inlen, MemoryBuffer &output)
{
#ifdef _USE_OPENSSL
return aesDecrypt_ssl(key, keylen, input, inlen, output);
#else
return jlib::aesDecrypt(key, keylen, input, inlen, output);
#endif
}

size_t aesDecrypt(const void *key, size_t keylen, const void *input, size_t inlen, void *output, size_t outlen)
{
// MORE - add openssl version!
return jlib::aesDecrypt(key, keylen, input, inlen, output, outlen);
}

Expand Down Expand Up @@ -1874,3 +1967,39 @@ void decrypt(StringBuffer &ret, const char *in)
}
}

void xmain (void)
{
/*
* Set up the key and iv. Do I need to say to not hard code these in a
* real application? :-)
*/

/* A 256 bit key */
unsigned char key[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31
};

/* Message to be encrypted */
unsigned char *plaintext = (unsigned char *)"The quick brown fox jumps over the lazy dog";

MemoryBuffer ciphertext, decrypted;

//aesEncrypt_ssl(key, 32, plaintext, strlen ((char *)plaintext), ciphertext);
jlib::aesEncrypt(key, 32, plaintext, strlen ((char *)plaintext), ciphertext);

printf("Ciphertext is:\n");
BIO_dump_fp (stdout, ciphertext.bytes(), ciphertext.length());

/* Decrypt the ciphertext */
aesDecrypt_ssl(key, 32, ciphertext.bytes(), ciphertext.length(), decrypted);

/* Add a NULL terminator. We are expecting printable text */
decrypted.append('\0');

/* Show the decrypted text */
printf("Decrypted text is:\n");
printf("%s\n", (const char *) decrypted.bytes());
}

1 change: 1 addition & 0 deletions system/jlib/jencrypt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extern jlib_decl size_t aesDecrypt(const void *key, size_t keylen, const void *i
extern jlib_decl void encrypt(StringBuffer &ret, const char *in);
extern jlib_decl void decrypt(StringBuffer &ret, const char *in);

extern jlib_decl void xmain();

// simple inline block scrambler (shouldn't need jlib_decl)
class Csimplecrypt
Expand Down
Loading
Loading