-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathCrypto.h
More file actions
76 lines (67 loc) · 3.31 KB
/
Crypto.h
File metadata and controls
76 lines (67 loc) · 3.31 KB
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
74
75
76
/*
* QDigiDocClient
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#pragma once
#include <QtCore/QCryptographicHash>
#include <QtCore/QLoggingCategory>
#include <memory>
using EVP_CIPHER = struct evp_cipher_st;
using EVP_CIPHER_CTX = struct evp_cipher_ctx_st;
using EVP_PKEY = struct evp_pkey_st;
using puchar = uchar *;
using pcuchar = const uchar *;
class QSslKey;
#define SCOPE(TYPE, DATA) std::unique_ptr<TYPE,decltype(&TYPE##_free)>(static_cast<TYPE*>(DATA), TYPE##_free)
Q_DECLARE_LOGGING_CATEGORY(CRYPTO)
class Crypto
{
public:
struct Cipher {
std::unique_ptr<EVP_CIPHER_CTX,void (*)(EVP_CIPHER_CTX*)> ctx;
Cipher(const EVP_CIPHER *cipher, const QByteArray &key, const QByteArray &iv, bool encrypt = true);
bool updateAAD(const QByteArray &data) const;
QByteArray update(const QByteArray &data) const;
bool update(char *data, int size) const;
bool result() const;
QByteArray tag() const;
static constexpr int tagLen() { return 16; }
bool setTag(const QByteArray &data) const;
};
static QByteArray aes_wrap(const QByteArray &key, const QByteArray &data, bool encrypt);
static QByteArray cipher(const EVP_CIPHER *cipher, const QByteArray &key, QByteArray &data, bool encrypt);
static QByteArray curve_oid(EVP_PKEY *key);
static QByteArray concatKDF(QCryptographicHash::Algorithm digestMethod, const QByteArray &z, const QByteArray &otherInfo);
static QByteArray derive(EVP_PKEY *priv, EVP_PKEY *pub);
static QByteArray encrypt(EVP_PKEY *pub, int padding, const QByteArray &data);
static QByteArray expand(const QByteArray &key, const QByteArray &info, int len = 32);
static QByteArray extract(const QByteArray &key, const QByteArray &salt, int len = 32);
static std::unique_ptr<EVP_PKEY, void (*)(EVP_PKEY *)> fromPUBKeyDer(const QByteArray &key);
static std::unique_ptr<EVP_PKEY, void (*)(EVP_PKEY *)> fromECPublicKeyDer(const QByteArray &key, int curveName);
static std::unique_ptr<EVP_PKEY, void (*)(EVP_PKEY *)> fromRSAPublicKeyDer(const QByteArray &key);
static std::unique_ptr<EVP_PKEY, void (*)(EVP_PKEY *)> genECKey(EVP_PKEY *params);
static QByteArray genKey(const EVP_CIPHER *cipher);
static QByteArray hkdf(const QByteArray &key, const QByteArray &salt, const QByteArray &info, int len = 32, int mode = 0);
static QByteArray sign_hmac(const QByteArray &key, const QByteArray &data);
static QByteArray hmacSha256(const QByteArray& key, const QByteArray& data);
static QByteArray toPublicKeyDer(EVP_PKEY *key);
static QByteArray toPublicKeyDer(const QSslKey &key);
static QByteArray random(int len = 32);
static QByteArray xor_data(const QByteArray &a, const QByteArray &b);
private:
static bool isError(int err);
};