This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
CipherContext.h
73 lines (60 loc) · 2.45 KB
/
CipherContext.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
#include <QByteArray>
#include <openssl/evp.h>
#include <openssl/err.h>
class CipherContext {
public:
CipherContext() {
EVP_CIPHER_CTX_init(&context);
}
~CipherContext() {
EVP_CIPHER_CTX_cleanup(&context);
}
QByteArray aesDecrypt(const QByteArray& cipherKey, const QByteArray& iv, const QByteArray& message, int& error) {
return decrypt(EVP_aes_256_cbc(), cipherKey, iv, message, error);
}
QByteArray aesEncrypt(const QByteArray& cipherKey, const QByteArray& iv, const QByteArray& message, int& error) {
return encrypt(EVP_aes_256_cbc(), cipherKey, iv, message, error);
}
QByteArray decrypt(const EVP_CIPHER *cipher, const QByteArray& cipherKey, const QByteArray& iv, const QByteArray& message, int& error) {
return perform(
EVP_DecryptInit_ex, EVP_DecryptUpdate, EVP_DecryptFinal_ex,
cipher, cipherKey, iv, message, error);
}
QByteArray encrypt(const EVP_CIPHER *cipher, const QByteArray& cipherKey, const QByteArray& iv, const QByteArray& message, int& error) {
return perform(
EVP_EncryptInit_ex, EVP_EncryptUpdate, EVP_EncryptFinal_ex,
cipher, cipherKey, iv, message, error);
}
static QByteArray emptyResult(int& error) {
error = ERR_get_error();
return QByteArray();
}
protected:
QByteArray perform(
int (*init)(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, const unsigned char *key, const unsigned char *iv),
int (*update)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl),
int (*final)(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl),
const EVP_CIPHER *cipher, const QByteArray& cipherKey, const QByteArray& iv, const QByteArray& sourceData,
int& error) {
error = 0;
if (!init(&context, cipher, 0, (const unsigned char*)cipherKey.constData(), (const unsigned char*)iv.constData())) {
return emptyResult(error);
}
QByteArray resultData;
resultData.resize(sourceData.size() + EVP_CIPHER_block_size(cipher));
int len = 0;
if (!update(&context, (unsigned char *)resultData.data(), &len, (unsigned char*)sourceData.constData(), sourceData.size())) {
return emptyResult(error);
}
int finalLen;
if (!final(&context, (unsigned char *)resultData.data() + len, &finalLen)) {
return emptyResult(error);
}
resultData.resize(len + finalLen);
return resultData;
}
operator EVP_CIPHER_CTX* () { return &context; }
private:
EVP_CIPHER_CTX context;
};