webmoney 0.0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ #ifndef __RSALIB1_H_INCLUDE__
2
+ #define __RSALIB1_H_INCLUDE__
3
+ #include "stdafx.h"
4
+
5
+ typedef unsigned char boolean;
6
+ typedef unsigned char byte;
7
+ typedef unsigned short word16;
8
+ typedef word16 unit;
9
+ typedef unit *unitptr;
10
+ typedef short signedunit;
11
+ typedef unsigned long int ulint;
12
+
13
+ class CRSALib
14
+ {
15
+ public:
16
+
17
+ enum { MAX_BIT_PRECISION = 2048, MAX_UNIT_PRECISION = MAX_BIT_PRECISION/(sizeof(word16)*8) };
18
+ private:
19
+ unit moduli_buf[16][(MAX_UNIT_PRECISION)];
20
+ unitptr moduli[16+1];
21
+
22
+ unit msu_moduli[16+1];
23
+ unit nmsu_moduli[16+1];
24
+ unit mpdbuf[16-1][(MAX_UNIT_PRECISION)];
25
+ unitptr mpd[16];
26
+
27
+ public:
28
+ short global_precision;
29
+ void set_precision(short prec);
30
+ CRSALib(short glob_pres=0);
31
+
32
+ boolean mp_addc(register unitptr r1,register unitptr r2,register boolean carry);
33
+
34
+ boolean mp_subb(register unitptr r1,register unitptr r2,register boolean borrow);
35
+ boolean mp_rotate_left(register unitptr r1,register boolean carry);
36
+ boolean mp_rotate_right(register unitptr r1,register boolean carry);
37
+ short mp_compare(register unitptr r1,register unitptr r2);
38
+ boolean mp_inc(register unitptr r);
39
+ boolean mp_dec(register unitptr r);
40
+ void mp_neg(register unitptr r);
41
+ void mp_move(register unitptr dst,register unitptr src);
42
+ void mp_init(register unitptr r, word16 value);
43
+ short significance(register unitptr r);
44
+ void unitfill0(unitptr r,word16 unitcount);
45
+ int mp_udiv(register unitptr remainder,register unitptr quotient,
46
+ register unitptr dividend,register unitptr divisor);
47
+ int mp_div(register unitptr remainder,register unitptr quotient,
48
+ register unitptr dividend,register unitptr divisor);
49
+ word16 mp_shortdiv(register unitptr quotient,
50
+ register unitptr dividend,register word16 divisor);
51
+ int mp_mod(register unitptr remainder,
52
+ register unitptr dividend,register unitptr divisor);
53
+ word16 mp_shortmod(register unitptr dividend,register word16 divisor);
54
+ int mp_mult(register unitptr prod,
55
+ register unitptr multiplicand,register unitptr multiplier);
56
+
57
+ private:
58
+ void mp_lshift_unit(register unitptr r1);
59
+ void stage_mp_images(unitptr images[16],unitptr r);
60
+
61
+ public:
62
+ int stage_merritt_modulus(unitptr n);
63
+ int merritt_modmult(register unitptr prod,
64
+ unitptr multiplicand,register unitptr multiplier);
65
+
66
+ private:
67
+ void merritt_burn(void);
68
+
69
+ public:
70
+ int countbits(unitptr r);
71
+
72
+ int mp_modexp(register unitptr expout,register unitptr expin,
73
+ register unitptr exponent,register unitptr modulus);
74
+
75
+ int rsa_decrypt(unitptr M, unitptr C, unitptr d, unitptr p, unitptr q, unitptr u);
76
+ int mp_sqrt(unitptr quotient,unitptr dividend);
77
+ };
78
+
79
+ inline void CRSALib::set_precision(short prec) {global_precision = prec;}
80
+ #endif
81
+ //---
@@ -0,0 +1,315 @@
1
+ #include "stdafx.h"
2
+ #include <stdio.h>
3
+ #include <fcntl.h>
4
+ #include <stdlib.h>
5
+ #include <time.h>
6
+ #include "signer.h"
7
+
8
+ #ifdef _WIN32
9
+ #include <io.h>
10
+ #include <sys/types.h>
11
+ #include <sys/stat.h>
12
+ #include <errno.h>
13
+ #define __open _open
14
+ #define __read _read
15
+ #define __close _close
16
+ #else
17
+ #include <sys/types.h>
18
+ #include <sys/stat.h>
19
+ #include <sys/uio.h>
20
+ #include <unistd.h>
21
+ #include <errno.h>
22
+ #define __open open
23
+ #define __read read
24
+ #define __close close
25
+ #endif
26
+
27
+ #ifndef TRUE
28
+ #define TRUE 1
29
+ #endif
30
+ #ifndef FALSE
31
+ #define FALSE 0
32
+ #endif
33
+
34
+ bool Signer::SecureKeyByIDPW(char *buf, DWORD dwBuf)
35
+ {
36
+ if(((KeyFileFormat *)buf)->wSignFlag == 0)
37
+ {
38
+ m_siErrorCode = -2;
39
+ return false;
40
+ };
41
+ DWORD dwCRC[4];
42
+ szptr szIDPW = m_szUserName;
43
+ szIDPW += m_szPassword;
44
+ Keys::CountCrcMD4(dwCRC, szIDPW, szIDPW.strlen());
45
+
46
+ dwCRC[0] = SwitchIndian(dwCRC[0]);
47
+ dwCRC[1] = SwitchIndian(dwCRC[1]);
48
+ dwCRC[2] = SwitchIndian(dwCRC[2]);
49
+ dwCRC[3] = SwitchIndian(dwCRC[3]);
50
+
51
+ char *ptrKey = ((KeyFileFormat *)buf)->ptrBuffer;
52
+ DWORD dwKeyLen = dwBuf-(ptrKey-buf) - 6;
53
+ ptrKey += 6;
54
+ for(DWORD dwProc=0; dwProc<dwKeyLen; dwProc+=sizeof(dwCRC))
55
+ for(int k=0; k<sizeof(dwCRC)&&(dwProc+k)<dwKeyLen; k++)
56
+ *(ptrKey+dwProc+k) ^= ((char *)dwCRC)[k];
57
+ return true;
58
+ }
59
+
60
+
61
+ bool Signer::SecureKeyByIDPWHalf(char *buf, DWORD dwBuf)
62
+ {
63
+ if(((KeyFileFormat *)buf)->wSignFlag == 0)
64
+ {
65
+ m_siErrorCode = -2;
66
+ return false;
67
+ };
68
+ DWORD dwCRC[4];
69
+ szptr szIDPW = m_szUserName;
70
+ int len = (int) strlen(m_szPassword)/2 + 1;
71
+ if (len > 1)
72
+ {
73
+ char* pBuf = new char[len];
74
+ strncpy(pBuf, m_szPassword, len-1);
75
+ pBuf[len-1] = '\0';
76
+ szIDPW += pBuf;
77
+
78
+ delete [] pBuf;
79
+ }
80
+ Keys::CountCrcMD4(dwCRC, szIDPW, szIDPW.strlen());
81
+
82
+ dwCRC[0] = SwitchIndian(dwCRC[0]);
83
+ dwCRC[1] = SwitchIndian(dwCRC[1]);
84
+ dwCRC[2] = SwitchIndian(dwCRC[2]);
85
+ dwCRC[3] = SwitchIndian(dwCRC[3]);
86
+
87
+ char *ptrKey = ((KeyFileFormat *)buf)->ptrBuffer;
88
+ DWORD dwKeyLen = dwBuf-(ptrKey-buf) - 6;
89
+ ptrKey += 6;
90
+ for(DWORD dwProc=0; dwProc<dwKeyLen; dwProc+=sizeof(dwCRC))
91
+ for(int k=0; k<sizeof(dwCRC)&&(dwProc+k)<dwKeyLen; k++)
92
+ *(ptrKey+dwProc+k) ^= ((char *)dwCRC)[k];
93
+ return true;
94
+ }
95
+ //---------------------------------------------------------
96
+ void Signer::SetKeyFromCL( int flag, char *KeyBuf )
97
+ {
98
+ KeyFromCL = FALSE;
99
+ if( flag == TRUE ) KeyFromCL = TRUE;
100
+ memcpy( (void *) szKeyData, (const void *)KeyBuf, 164 );
101
+ }
102
+ //---------------------------------------------------------
103
+
104
+ int Signer::LoadKeys()
105
+ {
106
+ bool bKeysReaded = false, bNotOldFmt = false;
107
+ int nReaden;
108
+ int errLoadKey;
109
+ int fh = -1;
110
+ int st_size = 0;
111
+ const int nMaxBufLen = sizeof(Keys) + KeyFileFormat::sizeof_header;
112
+ char *pBufRead = new char[nMaxBufLen]; // Here Keys must be
113
+ m_siErrorCode = 0;
114
+ KeyFromCL = FALSE;
115
+
116
+ if( (!isIgnoreKeyFile) && (Key64Flag == FALSE) ) {
117
+ #ifdef O_BINARY
118
+ fh = __open( m_szKeyFileName, O_RDONLY | O_BINARY);
119
+ #else
120
+ fh = __open( m_szKeyFileName, O_RDONLY);
121
+ #endif
122
+
123
+ if( fh == -1 )
124
+ {
125
+ m_siErrorCode = errno;
126
+ return false;
127
+ }
128
+
129
+ st_size = lseek(fh, 0, SEEK_END);
130
+ lseek(fh, 0, SEEK_SET);
131
+ if (st_size == lMinKeyFileSize)
132
+ {
133
+ // load 164 bytes from "small" keys file
134
+ nReaden = __read( fh, pBufRead, nMaxBufLen );
135
+ bKeysReaded = (nReaden == lMinKeyFileSize);
136
+ }
137
+ __close( fh );
138
+ }
139
+ else {
140
+ bKeysReaded = true;
141
+ nReaden = lMinKeyFileSize;
142
+ memcpy( pBufRead, szKeyData, lMinKeyFileSize);
143
+ }
144
+
145
+ //*************************************************************************
146
+
147
+ if(bKeysReaded)
148
+ {
149
+ SecureKeyByIDPWHalf(pBufRead, lMinKeyFileSize);
150
+ WORD old_SignFlag;
151
+ old_SignFlag = ((KeyFileFormat *)pBufRead)->wSignFlag;
152
+ ((KeyFileFormat *)pBufRead)->wSignFlag = 0;
153
+ errLoadKey = keys.LoadFromBuffer( pBufRead, lMinKeyFileSize );
154
+ if(errLoadKey)
155
+ {
156
+ // Restore for correct Loading (CRC) !
157
+ ((KeyFileFormat *)pBufRead)->wSignFlag = old_SignFlag;
158
+ SecureKeyByIDPWHalf(pBufRead, lMinKeyFileSize); // restore buffer
159
+
160
+ SecureKeyByIDPW(pBufRead, lMinKeyFileSize);
161
+
162
+ ((KeyFileFormat *)pBufRead)->wSignFlag = 0;
163
+ errLoadKey = keys.LoadFromBuffer( pBufRead, lMinKeyFileSize );
164
+ }
165
+
166
+ delete[] pBufRead;
167
+ if( !errLoadKey )
168
+ bKeysReaded = true;
169
+ else
170
+ {
171
+ Keys flushKey;
172
+ keys = flushKey;
173
+ m_siErrorCode = -3;
174
+ }
175
+ }
176
+
177
+ return bKeysReaded;
178
+ }
179
+
180
+ Signer::Signer(const char * szLogin, const char *szPassword, const char *szKeyFileName)
181
+ : m_szUserName(szLogin), m_szPassword(szPassword), m_szKeyFileName(szKeyFileName)
182
+ {
183
+ m_siErrorCode = 0;
184
+ isIgnoreKeyFile = false;
185
+ isIgnoreIniFile = false;
186
+ isKWMFileFromCL = false;
187
+ memset(szKeyData, 0, MAXBUF+1);
188
+ Key64Flag = FALSE;
189
+ }
190
+
191
+ short Signer::ErrorCode()
192
+ {
193
+ return m_siErrorCode;
194
+ }
195
+
196
+ bool Signer::Sign(const char *szIn, szptr& szSign)
197
+ {
198
+ DWORD dwCRC[14];
199
+ #ifdef _DEBUG
200
+ printf("\n\rSign - Start !");
201
+ #endif
202
+
203
+ if (!LoadKeys())
204
+ {
205
+ puts("!LoadKeys");
206
+ return false;
207
+ }
208
+ #ifdef _DEBUG
209
+ printf("\n\rSign - Load Keys");
210
+ #endif
211
+
212
+ if(!keys.wEKeyBase || !keys.wNKeyBase)
213
+ return false;
214
+
215
+ #ifdef _DEBUG
216
+ char *szInHex = new char [(strlen(szIn)+1)*2+1];
217
+ us2sz((const unsigned short *)szIn, (int)(strlen(szIn)+1)/2, szInHex);
218
+ puts("\n\rInput:\n");
219
+ puts(szIn);
220
+ puts("\nin hex:\n");
221
+ puts(szInHex);
222
+ puts("\n");
223
+ delete [] szInHex;
224
+ #endif
225
+
226
+ if(Keys::CountCrcMD4(dwCRC, szIn, (DWORD)strlen(szIn)))
227
+ {
228
+ DWORD dwCrpSize = GetCLenB(sizeof(dwCRC), keys.arwNKey);
229
+ char *ptrCrpBlock = new char[dwCrpSize];
230
+ #ifdef _DEBUG
231
+ for(int i=4; i<14; i++) dwCRC[i] = 0;
232
+ #else
233
+ srand((unsigned)time(NULL));
234
+ for(int i=4; i<14; i++) dwCRC[i] = rand();
235
+ #endif
236
+ dwCRC[0] = SwitchIndian(dwCRC[0]);
237
+ dwCRC[1] = SwitchIndian(dwCRC[1]);
238
+ dwCRC[2] = SwitchIndian(dwCRC[2]);
239
+ dwCRC[3] = SwitchIndian(dwCRC[3]);
240
+ #ifdef _DEBUG
241
+ for(int h=0;h<sizeof(dwCRC);h++)
242
+ { printf("packing%d: %x\n", h, ((char*)dwCRC)[h]); }
243
+ #endif
244
+ #ifdef _DEBUG
245
+ printf("\n\rCalling CrpB() - start");
246
+ #endif
247
+ CrpB(ptrCrpBlock, (char *)dwCRC, sizeof(dwCRC), keys.arwEKey, keys.arwNKey);
248
+ #ifdef _DEBUG
249
+ printf("\n\rCalling CrpB() - end");
250
+ #endif
251
+ char *charCrpBlock = new char[dwCrpSize*2+1];
252
+ us2sz((const unsigned short *)ptrCrpBlock, dwCrpSize/2, charCrpBlock);
253
+ szSign = charCrpBlock;
254
+ #ifdef _DEBUG
255
+ printf("\n\rSign - prepare end");
256
+ #endif
257
+
258
+ delete [] charCrpBlock;
259
+ delete [] ptrCrpBlock;
260
+
261
+ #ifdef _DEBUG
262
+ printf("\n\rSign - end return true");
263
+ #endif
264
+
265
+ return true;
266
+ }
267
+
268
+ #ifdef _DEBUG
269
+ printf("\n\rSign - end return false");
270
+ #endif
271
+ return false;
272
+ }
273
+
274
+ Signer2::Signer2(const char *szLogin, const char *szPassword, const char *szKeyData)
275
+ :Signer(szLogin, szPassword, ""), m_strKeyData(szKeyData)
276
+ {
277
+ m_siErrorCode = 0;
278
+ }
279
+
280
+ int Signer2::LoadKeys()
281
+ {
282
+ bool bKeysReaded = false, bNotOldFmt = false;
283
+ int errLoadKey;
284
+
285
+ int nStrKeyDataLen = m_strKeyData.strlen();
286
+ const int nMaxBufLen = sizeof(Keys) + KeyFileFormat::sizeof_header;
287
+ if ((nStrKeyDataLen>0) && (nStrKeyDataLen < nMaxBufLen*2))
288
+ {
289
+ BYTE *bKeyData = new BYTE[nMaxBufLen];
290
+ sz2us(m_strKeyData, (unsigned short*)bKeyData);
291
+ SecureKeyByIDPW((char*)bKeyData, nStrKeyDataLen / 2);
292
+ ((KeyFileFormat *)bKeyData)->wSignFlag = 0;
293
+ errLoadKey = keys.LoadFromBuffer((char*)bKeyData, nStrKeyDataLen / 2);
294
+ delete bKeyData;
295
+ if( !errLoadKey )
296
+ bKeysReaded = true;
297
+ else {
298
+ Keys flushKey;
299
+ keys = flushKey;
300
+ m_siErrorCode = -2;
301
+ }
302
+ }
303
+ else
304
+ {
305
+ errLoadKey = -1;
306
+ m_siErrorCode = -1;
307
+ }
308
+ return (bKeysReaded);
309
+ }
310
+
311
+ short Signer2::ErrorCode()
312
+ {
313
+ return m_siErrorCode;
314
+ }
315
+ //----
@@ -0,0 +1,63 @@
1
+ #ifndef __SIGNER_H_INCLUDE__
2
+ #define __SIGNER_H_INCLUDE__
3
+ #include "stdafx.h"
4
+ #include "cmdbase.h"
5
+
6
+ #define MAXBUF 4096
7
+
8
+ const long lMinKeyFileSize = 164;//smallest size of the keysfaile
9
+ const long lKiloByte = 1024;//1 kilobyte in bytes
10
+ const long lMegaByte = lKiloByte*lKiloByte;//1 megabyte in bytes
11
+ const long lScrollZoom = 100;
12
+ const long lMaxKeyFileSize = 10*lMegaByte;//10 Mb in this version
13
+ const szptr szOptionKeyFileSize = "KeyFileSize";
14
+ const unsigned int uiKWNHeaderOffset = 2;
15
+ const unsigned int uiKWNHeaderSize = uiKWNHeaderOffset + lMinKeyFileSize;
16
+ const unsigned int uiBlockSizeOffset = 1;
17
+
18
+ class Signer
19
+ {
20
+
21
+ public:
22
+ bool isIgnoreKeyFile;
23
+ bool isIgnoreIniFile;
24
+ bool isKWMFileFromCL;
25
+ char szKeyData[MAXBUF+1]; /* Buffer for Signre-s key */
26
+ int Key64Flag;
27
+
28
+ protected:
29
+ szptr m_szUserName;
30
+ szptr m_szPassword;
31
+ szptr m_szKeyFileName;
32
+ short m_siErrorCode;
33
+ Keys keys;
34
+ bool SecureKeyByIDPW(char *buf, DWORD dwBuf);
35
+ bool SecureKeyByIDPWHalf(char *buf, DWORD dwBuf);
36
+ int virtual LoadKeys();
37
+
38
+ public:
39
+ Signer(const char *szLogin, const char *szPassword, const char *szKeyFileName);
40
+ bool Sign(const char *szIn, szptr& szSign);
41
+ short ErrorCode();
42
+ //-------------------------------------------------
43
+ public:
44
+ int KeyFromCL;
45
+ char KeyBuffer[164];
46
+ void SetKeyFromCL( int flag, char *KeyBuf );
47
+ //-------------------------------------------------
48
+ };
49
+
50
+ class Signer2: public Signer
51
+ {
52
+ protected:
53
+ szptr m_strKeyData;
54
+ short m_siErrorCode;
55
+ int virtual LoadKeys();
56
+
57
+ public:
58
+ Signer2(const char *szLogin, const char *szPassword, const char *szKeyData);
59
+ short ErrorCode();
60
+ };
61
+
62
+ #endif
63
+ //-----