2
* cipher.cpp - Simple wrapper to 3DES,AES128/256 CBC ciphers
3
* Copyright (C) 2003 Justin Karneges
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
#include<openssl/evp.h>
24
#include<openssl/rsa.h>
25
#include"bytestream.h"
28
static bool lib_encryptArray(const EVP_CIPHER *type, const QByteArray &buf, const QByteArray &key, const QByteArray &iv, bool pad, QByteArray *out)
30
QByteArray result(buf.size()+type->block_size);
34
unsigned char *ivp = NULL;
36
ivp = (unsigned char *)iv.data();
37
EVP_CIPHER_CTX_init(&c);
38
//EVP_CIPHER_CTX_set_padding(&c, pad ? 1: 0);
39
if(!EVP_EncryptInit_ex(&c, type, NULL, (unsigned char *)key.data(), ivp))
41
if(!EVP_EncryptUpdate(&c, (unsigned char *)result.data(), &len, (unsigned char *)buf.data(), buf.size()))
45
QByteArray last(type->block_size);
46
if(!EVP_EncryptFinal_ex(&c, (unsigned char *)last.data(), &len))
49
ByteStream::appendArray(&result, last);
52
memset(&c, 0, sizeof(EVP_CIPHER_CTX));
57
static bool lib_decryptArray(const EVP_CIPHER *type, const QByteArray &buf, const QByteArray &key, const QByteArray &iv, bool pad, QByteArray *out)
59
QByteArray result(buf.size()+type->block_size);
63
unsigned char *ivp = NULL;
65
ivp = (unsigned char *)iv.data();
66
EVP_CIPHER_CTX_init(&c);
67
//EVP_CIPHER_CTX_set_padding(&c, pad ? 1: 0);
68
if(!EVP_DecryptInit_ex(&c, type, NULL, (unsigned char *)key.data(), ivp))
71
if(!EVP_EncryptUpdate(&c, (unsigned char *)result.data(), &len, (unsigned char *)buf.data(), buf.size()))
75
if(!EVP_DecryptUpdate(&c, (unsigned char *)result.data(), &len, (unsigned char *)buf.data(), buf.size()))
80
QByteArray last(type->block_size);
81
if(!EVP_DecryptFinal_ex(&c, (unsigned char *)last.data(), &len))
84
ByteStream::appendArray(&result, last);
87
memset(&c, 0, sizeof(EVP_CIPHER_CTX));
92
static bool lib_generateKeyIV(const EVP_CIPHER *type, const QByteArray &data, const QByteArray &salt, QByteArray *key, QByteArray *iv)
95
unsigned char *kp = 0;
96
unsigned char *ip = 0;
98
k.resize(type->key_len);
99
kp = (unsigned char *)k.data();
102
i.resize(type->iv_len);
103
ip = (unsigned char *)i.data();
105
if(!EVP_BytesToKey(type, EVP_sha1(), (unsigned char *)salt.data(), (unsigned char *)data.data(), data.size(), 1, kp, ip))
114
static const EVP_CIPHER * typeToCIPHER(Cipher::Type t)
116
if(t == Cipher::TripleDES)
117
return EVP_des_ede3_cbc();
118
else if(t == Cipher::AES_128)
119
return EVP_aes_128_cbc();
120
else if(t == Cipher::AES_256)
121
return EVP_aes_256_cbc();
126
Cipher::Key Cipher::generateKey(Type t)
129
const EVP_CIPHER *type = typeToCIPHER(t);
133
if(!lib_generateKeyIV(type, QRandom::randomArray(128), QRandom::randomArray(2), &out, 0))
140
QByteArray Cipher::generateIV(Type t)
142
const EVP_CIPHER *type = typeToCIPHER(t);
146
if(!lib_generateKeyIV(type, QCString("Get this man an iv!"), QByteArray(), 0, &out))
151
int Cipher::ivSize(Type t)
153
const EVP_CIPHER *type = typeToCIPHER(t);
159
QByteArray Cipher::encrypt(const QByteArray &buf, const Key &key, const QByteArray &iv, bool pad, bool *ok)
163
const EVP_CIPHER *type = typeToCIPHER(key.type());
167
if(!lib_encryptArray(type, buf, key.data(), iv, pad, &out))
175
QByteArray Cipher::decrypt(const QByteArray &buf, const Key &key, const QByteArray &iv, bool pad, bool *ok)
179
const EVP_CIPHER *type = typeToCIPHER(key.type());
183
if(!lib_decryptArray(type, buf, key.data(), iv, pad, &out))
192
class RSAKey::Private
206
RSAKey::RSAKey(const RSAKey &from)
212
RSAKey & RSAKey::operator=(const RSAKey &from)
229
bool RSAKey::isNull() const
231
return d ? false: true;
234
void * RSAKey::data() const
237
return (void *)d->rsa;
242
void RSAKey::setData(void *p)
266
RSAKey generateRSAKey()
268
RSA *rsa = RSA_generate_key(1024, RSA_F4, NULL, NULL);
275
QByteArray encryptRSA(const QByteArray &buf, const RSAKey &key, bool *ok)
280
int size = RSA_size((RSA *)key.data());
281
int flen = buf.size();
282
if(flen >= size - 11)
284
QByteArray result(size);
285
unsigned char *from = (unsigned char *)buf.data();
286
unsigned char *to = (unsigned char *)result.data();
287
int r = RSA_public_encrypt(flen, from, to, (RSA *)key.data(), RSA_PKCS1_PADDING);
297
QByteArray decryptRSA(const QByteArray &buf, const RSAKey &key, bool *ok)
302
int size = RSA_size((RSA *)key.data());
303
int flen = buf.size();
304
QByteArray result(size);
305
unsigned char *from = (unsigned char *)buf.data();
306
unsigned char *to = (unsigned char *)result.data();
307
int r = RSA_private_decrypt(flen, from, to, (RSA *)key.data(), RSA_PKCS1_PADDING);
317
QByteArray encryptRSA2(const QByteArray &buf, const RSAKey &key, bool *ok)
322
int size = RSA_size((RSA *)key.data());
323
int flen = buf.size();
324
if(flen >= size - 41)
326
QByteArray result(size);
327
unsigned char *from = (unsigned char *)buf.data();
328
unsigned char *to = (unsigned char *)result.data();
329
int r = RSA_public_encrypt(flen, from, to, (RSA *)key.data(), RSA_PKCS1_OAEP_PADDING);
339
QByteArray decryptRSA2(const QByteArray &buf, const RSAKey &key, bool *ok)
344
int size = RSA_size((RSA *)key.data());
345
int flen = buf.size();
346
QByteArray result(size);
347
unsigned char *from = (unsigned char *)buf.data();
348
unsigned char *to = (unsigned char *)result.data();
349
int r = RSA_private_decrypt(flen, from, to, (RSA *)key.data(), RSA_PKCS1_OAEP_PADDING);