forked from shanet/Crypto-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrypto.h
83 lines (53 loc) · 1.83 KB
/
Crypto.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
74
75
76
77
78
79
80
81
82
83
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <stdio.h>
#include <string>
#include <string.h>
#ifndef CRYPTO_H
#define CRYPTO_H
#define RSA_KEYLEN 2048
#define AES_KEYLEN 256
#define AES_ROUNDS 6
#define PSUEDO_CLIENT
//#define USE_PBKDF
#define SUCCESS 0
#define FAILURE -1
#define KEY_SERVER_PRI 0
#define KEY_SERVER_PUB 1
#define KEY_CLIENT_PUB 2
#define KEY_AES 3
#define KEY_AES_IV 4
class Crypto {
public:
Crypto();
Crypto(unsigned char *remotePubKey, size_t remotePubKeyLen);
~Crypto();
int rsaEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **encMsg, unsigned char **ek, size_t *ekl, unsigned char **iv, size_t *ivl);
int aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **encMsg);
int rsaDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char *ek, size_t ekl, unsigned char *iv, size_t ivl, unsigned char **decMsg);
int aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg);
int writeKeyToFile(FILE *fd, int key);
int getRemotePubKey(unsigned char **pubKey);
int setRemotePubKey(unsigned char *pubKey, size_t pubKeyLen);
int getLocalPubKey(unsigned char **pubKey);
int getLocalPriKey(unsigned char **priKey);
int getAESKey(unsigned char **aesKey);
int setAESKey(unsigned char *aesKey, size_t aesKeyLen);
int getAESIv(unsigned char **aesIv);
int setAESIv(unsigned char *aesIv, size_t aesIvLen);
private:
static EVP_PKEY *localKeypair;
EVP_PKEY *remotePubKey;
EVP_CIPHER_CTX *rsaEncryptCtx;
EVP_CIPHER_CTX *aesEncryptCtx;
EVP_CIPHER_CTX *rsaDecryptCtx;
EVP_CIPHER_CTX *aesDecryptCtx;
unsigned char *aesKey;
unsigned char *aesIV;
int init();
int genTestClientKey();
};
#endif