summaryrefslogtreecommitdiff
path: root/plugins/CryptoPP/src/cryptopp.h
blob: 8657e29c6a806de42ba4478d9ad9057d29d218fb (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#ifndef __CRYPTOPP_H__
#define __CRYPTOPP_H__

#include <queue>
#include <deque>
#include <list>
#include <map>

#pragma warning(disable: 4231 4250 4251 4275 4660 4661 4700 4706 4786 4355)
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include "../crypto/src/modes.h"
#include "../crypto/src/osrng.h"
#include "../crypto/src/rsa.h"
#include "../crypto/src/aes.h"
#include "../crypto/src/dh.h"
#include "../crypto/src/crc.h"
#include "../crypto/src/ripemd.h"
#include "../crypto/src/sha.h"
#include "../crypto/src/tiger.h"
#include "../crypto/src/gzip.h"
#include "../crypto/src/zlib.h"
#include "../crypto/src/files.h"

USING_NAMESPACE(CryptoPP);
USING_NAMESPACE(std)

#define KEYSIZE 256
#define DEFMSGS 4096

#define HEADER 0xABCD1234
#define FOOTER 0x9876FEDC
#define EMPTYH 0xF1E2D3C4

typedef struct __CNTX {
	u_int	header;		// HEADER
	short	mode;		// mode of encoding
	short	features;	// features of client
	short	error;		// error code of last operation
	PBYTE	pdata;		// data block
	PVOID	udata;		// user data
	LPSTR	tmp;		// return string
	u_int	deleted;	// delete time&flag to delete
	u_int	footer;		// FOOTER
} CNTX;
typedef CNTX* pCNTX;


#define FEATURES_UTF8			0x01
#define FEATURES_BASE64			0x02
#define FEATURES_GZIP			0x04
#define FEATURES_CRC32			0x08
#define FEATURES_PSK			0x10
#define FEATURES_NEWPG			0x20
#define FEATURES_RSA			0x40

#define MODE_BASE16			0x0000
#define MODE_BASE64			0x0001
#define MODE_PGP			0x0002
#define MODE_GPG			0x0004
#define MODE_GPG_ANSI			0x0008
#define MODE_PRIV_KEY			0x0010
#define MODE_RSA_2048			0x0020
#define MODE_RSA_4096			0x0040
#define MODE_RSA			MODE_RSA_4096
#define MODE_RSA_ONLY			0x0080
#define MODE_RSA_ZLIB			0x0100
#define MODE_RSA_BER 			0x0200

#define DATA_GZIP			1

typedef struct __SIMDATA {
	DH	*dh;		// diffie-hellman
	PBYTE	PubA;		// public keyA 2048 bit
	PBYTE	KeyA;		// private keyA 2048 bit
	PBYTE	KeyB;		// public keyB 2048 bit
	PBYTE	KeyX;		// secret keyX 192 bit
	PBYTE	KeyP;		// pre-shared keyP 192 bit
} SIMDATA;
typedef SIMDATA* pSIMDATA;


typedef struct __PGPDATA {
	PBYTE	pgpKeyID; // PGP KeyID
	PBYTE	pgpKey;   // PGP Key
} PGPDATA;
typedef PGPDATA* pPGPDATA;


typedef struct __GPGDATA {
	BYTE	*gpgKeyID; // GPG KeyID
} GPGDATA;
typedef GPGDATA* pGPGDATA;


#define RSA_KEYSIZE		SHA1::DIGESTSIZE
#define RSA_CalculateDigest	SHA1().CalculateDigest


typedef struct __RSAPRIV {
	string	priv_k;	// private key string
	string	priv_s;	// hash(priv_k)
	RSA::PrivateKey priv; // private key
	string	pub_k;	// public key string
	string	pub_s;	// hash(pub_k)
} RSAPRIV;
typedef RSAPRIV* pRSAPRIV;


typedef deque<string, allocator<string> > STRINGDEQUE;
typedef queue<string,STRINGDEQUE> STRINGQUEUE;


typedef struct __RSADATA {
	short			state;	// 0 - íèôèãà íåò, 1..6 - keyexchange, 7 - ñîåäèíåíèå óñòàíîâëåíî
	u_int			time;	// äëÿ ïðåðûâàíèÿ keyexchange, åñëè äîëãî íåò îòâåòà
	string			pub_k;	// public key string
	string			pub_s;	// hash(pub_k)
	RSA::PublicKey		pub;	// public key
	string			aes_k;	// aes key
	string			aes_v;	// aes iv
	HANDLE	 		thread; // thread handle
	BOOL			thread_exit;
 	HANDLE			event;	// thread event
	STRINGQUEUE		*queue;  // thread queue
} RSADATA;
typedef RSADATA* pRSADATA;


#define ERROR_NONE			0
#define ERROR_SEH			1
#define ERROR_NO_KEYA			2
#define ERROR_NO_KEYB			3
#define ERROR_NO_KEYX			4
#define ERROR_BAD_LEN			5
#define ERROR_BAD_CRC			6
#define ERROR_NO_PSK			7
#define ERROR_BAD_PSK			8
#define ERROR_BAD_KEYB			9
#define ERROR_NO_PGP_KEY		10
#define ERROR_NO_PGP_PASS		11
#define ERROR_NO_GPG_KEY		12
#define ERROR_NO_GPG_PASS		13

#if defined(_DEBUG)
#define FEATURES (FEATURES_UTF8 | FEATURES_BASE64 | FEATURES_GZIP | FEATURES_CRC32 | FEATURES_NEWPG)
#else
#define FEATURES (FEATURES_UTF8 | FEATURES_BASE64 | FEATURES_GZIP | FEATURES_CRC32 | FEATURES_NEWPG | FEATURES_RSA)
#endif

#define DLLEXPORT __declspec(dllexport)

extern LPCSTR szModuleName;
extern LPCSTR szVersionStr;
extern HINSTANCE g_hInst;

pCNTX get_context_on_id(int);
pCNTX get_context_on_id(HANDLE);
void cpp_free_keys(pCNTX);
BYTE *cpp_gzip(BYTE*,size_t,size_t&);
BYTE *cpp_gunzip(BYTE*,size_t,size_t&);
string cpp_zlibc(string&);
string cpp_zlibd(string&);

typedef struct {
    int (__cdecl *rsa_gen_keypair)(short);				// ãåíåðèò RSA-êëþ÷è äëÿ óêàçàííîé äëèíû (ëèáî òîêà 2048, ëèáî 2048 è 4096)
    int (__cdecl *rsa_get_keypair)(short,PBYTE,int*,PBYTE,int*);	// âîçâðàùàåò ïàðó êëþ÷åé äëÿ óêàçàííîé äëèíû
    int (__cdecl *rsa_get_keyhash)(short,PBYTE,int*,PBYTE,int*);	// âîçâðàùàåò hash ïàðû êëþ÷åé äëÿ óêàçàííîé äëèíû
    int (__cdecl *rsa_set_keypair)(short,PBYTE,int);			// óñòàíàâëèâàåò êëþ÷è, óêàçàííîé äëèíû
    int (__cdecl *rsa_get_pubkey)(HANDLE,PBYTE,int*);			// âîçâðàùàåò ïàáëèê êëþ÷ èç óêàçàííîãî êîíòåêñòà
    int (__cdecl *rsa_set_pubkey)(HANDLE,PBYTE,int);			// çàãðóæàåò ïàáëèê êëþ÷ äëÿ óêàçàííîãî êîíòåêñòà
    void (__cdecl *rsa_set_timeout)(int);				// óñòàíîâèòü òàéìàóò äëÿ óñòàíîâêè ñåêþðíîãî ñîåäèíåíèÿ
    int (__cdecl *rsa_get_state)(HANDLE);				// ïîëó÷èòü ñòàòóñ óêàçàííîãî êîíòåêñòà
    int (__cdecl *rsa_get_hash)(PBYTE,int,PBYTE,int*);			// âû÷èñëÿåò SHA1(key)
    int (__cdecl *rsa_connect)(HANDLE);					// çàïóñêàåò ïðîöåññ óñòàíîâêè ñîäèíåíèÿ ñ óêàçàííûì êîíòåêñòîì
    int (__cdecl *rsa_disconnect)(HANDLE);				// ðàçðûâàåò ñîåäèíåíèå ñ óêàçàííûì êîíòåêñòîì
    int (__cdecl *rsa_disabled)(HANDLE);				// ðàçðûâàåò ñîåäèíåíèå ïî ïðè÷èíå "disabled"
    LPSTR (__cdecl *rsa_recv)(HANDLE,LPCSTR);				// íåîáõîäèìî ïåðåäàâàòü ñþäà âñå âõîäÿùèå ïðîòîêîëüíûå ñîîáùåíèÿ
    int   (__cdecl *rsa_send)(HANDLE,LPCSTR);				// âûçûâàåì äëÿ îòïðàâêè ñîîáùåíèÿ êëèåíòó
    int (__cdecl *rsa_encrypt_file)(HANDLE,LPCSTR,LPCSTR);
    int (__cdecl *rsa_decrypt_file)(HANDLE,LPCSTR,LPCSTR);
    LPSTR  (__cdecl *utf8encode)(LPCWSTR);
    LPWSTR (__cdecl *utf8decode)(LPCSTR);
    int (__cdecl *is_7bit_string)(LPCSTR);
    int (__cdecl *is_utf8_string)(LPCSTR);
    int (__cdecl *rsa_export_keypair)(short,LPSTR,LPSTR,LPSTR);		// export private key
    int (__cdecl *rsa_import_keypair)(short,LPSTR,LPSTR);		// import & activate private key
    int (__cdecl *rsa_export_pubkey)(HANDLE,LPSTR);			// export public key from context
    int (__cdecl *rsa_import_pubkey)(HANDLE,LPSTR);			// import public key into context
} RSA_EXPORT;
typedef RSA_EXPORT* pRSA_EXPORT;

typedef struct {
    int  (__cdecl *rsa_inject)(HANDLE,LPCSTR);			// âñòàâëÿåò ñîîáùåíèå â î÷åðåäü íà îòïðàâêó
    int  (__cdecl *rsa_check_pub)(HANDLE,PBYTE,int,PBYTE,int);	// ïðîâåðÿåò èíòåðàêòèâíî SHA è ñîõðàíÿåò êëþ÷, åñëè âñå íîðìàëüíî
    void (__cdecl *rsa_notify)(HANDLE,int);			// íîòèôèêàöèÿ î ñìåíå ñîñòîÿíèÿ
} RSA_IMPORT;
typedef RSA_IMPORT* pRSA_IMPORT;

NAMESPACE_BEGIN(CryptoPP)
typedef RSASS<PKCS1v15, SHA256>::Signer RSASSA_PKCS1v15_SHA256_Signer;
typedef RSASS<PKCS1v15, SHA256>::Verifier RSASSA_PKCS1v15_SHA256_Verifier;
NAMESPACE_END

#endif