8 #include <openssl/pem.h>
9 #include <openssl/rsa.h>
10 #include <openssl/evp.h>
11 #include <openssl/err.h>
13 #include <arpa/inet.h>
24 FILE* inputKeyFile = fopen(pKey.c_str(),
"r");
28 std::stringstream msg;
29 msg<<
"Cannot open file: "<<pKey;
30 err.SetDescription(__FILE__,__LINE__,msg.str());
35 if (!PEM_read_RSA_PUBKEY(inputKeyFile, &rsa_pkey, NULL, NULL)) {
37 err.SetDescription(__FILE__,__LINE__,
"Error loading RSA Public Key File");
41 if (!PEM_read_RSAPrivateKey(inputKeyFile, &rsa_pkey, NULL, NULL)) {
43 err.SetDescription(__FILE__,__LINE__,
"Error loading RSA Private Key File");
48 err.SetDescription(__FILE__,__LINE__,
"Mode must be encrypt or decrypt");
51 if(inputKeyFile) fclose(inputKeyFile);
53 fPKey = EVP_PKEY_new();
54 if (!EVP_PKEY_assign_RSA(
fPKey, rsa_pkey)) {
56 err.SetDescription(
"__FILE__,__LINE__,EVP_PKEY_assign_RSA: failed.");
60 fCTX = EVP_CIPHER_CTX_new();
61 EVP_CIPHER_CTX_init(
fCTX);
70 EVP_CIPHER_CTX_free(
fCTX);
79 err.SetDescription(
"__FILE__,__LINE__,string message allowed only when encrypting");
82 std::vector<unsigned char> output(message.size());
83 for(
size_t i = 0; i < message.size(); i++) {
84 output[i] = message[i];
95 unsigned char buffer_out[4096 + EVP_MAX_IV_LENGTH*2];
97 totLength = EVP_CIPHER_iv_length(EVP_aes_128_cbc());
98 std::cout<<
"IV:length: "<<totLength<<std::endl;
100 unsigned char* ek = (
unsigned char*) malloc(4096);
102 if (!EVP_SealInit(
fCTX, EVP_aes_128_cbc(), &ek, &eklen, buffer_out, &
fPKey, 1)) {
104 err.SetDescription(__FILE__,__LINE__,
"EVP_SealInit: failed.");
109 if(eklen < 0 ) eklen = 0;
110 std::cout<<
"E:length: "<<eklen<<std::endl;
114 uint32_t eklen_n = htonl(eklen);
115 memcpy(buffer_out+totLength, &eklen_n,
sizeof(eklen_n));
117 totLength +=
sizeof(eklen_n);
118 memcpy(buffer_out+totLength,ek,eklen);
123 unsigned char* buffer_in =
Convert(message);
124 size_t n = message.size();
126 if (!EVP_SealUpdate(
fCTX, buffer_out+totLength, &len_out, buffer_in, n )) {
129 err.SetDescription(__FILE__,__LINE__,
"EVP_SealUpdate: failed.");
133 totLength += len_out;
135 if (!EVP_SealFinal(
fCTX, buffer_out+totLength, &len_out))
138 err.SetDescription(__FILE__,__LINE__,
"EVP_SealFinal: failed.");
141 totLength += len_out;
145 unsigned char* buffer_in =
Convert(message);
146 size_t n = message.size();
147 totLength = EVP_CIPHER_iv_length(EVP_aes_128_cbc());
148 unsigned char* iv = buffer_in;
153 memcpy(&eklen_n,buffer_in+totLength,
sizeof(eklen_n));
154 totLength +=
sizeof(eklen_n);
155 eklen = ntohl(eklen_n);
156 unsigned char* ek = buffer_in+totLength;
163 if (!EVP_OpenInit(
fCTX, EVP_aes_128_cbc(), ek, eklen, iv,
fPKey))
166 err.SetDescription(__FILE__,__LINE__,
"EVP_OpenInit: failed.");
172 if (!EVP_OpenUpdate(
fCTX, buffer_out, &len_out, buffer_in+totLength, n-totLength))
175 err.SetDescription(__FILE__,__LINE__,
"EVP_OpenUpdate: failed.");
180 if (!EVP_OpenFinal(
fCTX, buffer_out+len_out, &len_out))
183 err.SetDescription(__FILE__,__LINE__,
"EVP_OpenFinal: failed.");
186 totLength += len_out;
190 return Convert(buffer_out,totLength);
196 unsigned char* output = (
unsigned char*) malloc(
sizeof(
char)*input.size());
197 for(
size_t i = 0; i < input.size(); i++) {
198 output[i] = input[i];
205 std::vector<unsigned char> output(n);
206 for(
size_t i = 0; i < n; i++) {
207 output[i] = input[i];
215 printf(
"%s: ",header);
216 for(
size_t c = 0; c < n; c++) printf(
"%x",data[c]);
ClassImp(QCryptoRSAEnvelope) QCryptoRSAEnvelope
QCryptoRSAEnvelope(const std::string &pKey, const Mode mode)
std::vector< unsigned char > Process(const std::vector< unsigned char > &message)
void Print(const char *header, const unsigned char *data, const size_t n)
virtual ~QCryptoRSAEnvelope()
unsigned char * Convert(const std::vector< unsigned char > &input)
error class with error type and description