Cryptography.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "PuttyIntf.h"
  6. #include "Cryptography.h"
  7. #include <process.h>
  8. /*
  9. ---------------------------------------------------------------------------
  10. Copyright (c) 2002, Dr Brian Gladman <[email protected]>, Worcester, UK.
  11. All rights reserved.
  12. LICENSE TERMS
  13. The free distribution and use of this software in both source and binary
  14. form is allowed (with or without changes) provided that:
  15. 1. distributions of this source code include the above copyright
  16. notice, this list of conditions and the following disclaimer;
  17. 2. distributions in binary form include the above copyright
  18. notice, this list of conditions and the following disclaimer
  19. in the documentation and/or other associated materials;
  20. 3. the copyright holder's name is not used to endorse products
  21. built using this software without specific written permission.
  22. ALTERNATIVELY, provided that this notice is retained in full, this product
  23. may be distributed under the terms of the GNU General Public License (GPL),
  24. in which case the provisions of the GPL apply INSTEAD OF those given above.
  25. DISCLAIMER
  26. This software is provided 'as is' with no explicit or implied warranties
  27. in respect of its properties, including, but not limited to, correctness
  28. and/or fitness for purpose.
  29. -------------------------------------------------------------------------
  30. This file implements password based file encryption and authentication
  31. using AES in CTR mode, HMAC-SHA1 authentication and RFC2898 password
  32. based key derivation.
  33. This is an implementation of HMAC, the FIPS standard keyed hash function
  34. */
  35. #include <memory.h>
  36. #define sha1_ctx SHA_State
  37. #define sha1_begin(ctx) putty_SHA_Init(ctx)
  38. #define sha1_hash(buf, len, ctx) SHA_Bytes(ctx, buf, len)
  39. #define sha1_end(dig, ctx) putty_SHA_Final(ctx, dig)
  40. #define IN_BLOCK_LENGTH 64
  41. #define OUT_BLOCK_LENGTH 20
  42. #define HMAC_IN_DATA 0xffffffff
  43. typedef struct
  44. { unsigned char key[IN_BLOCK_LENGTH];
  45. sha1_ctx ctx[1];
  46. unsigned int klen;
  47. } hmac_ctx;
  48. /* initialise the HMAC context to zero */
  49. static void hmac_sha1_begin(hmac_ctx cx[1])
  50. {
  51. memset(cx, 0, sizeof(hmac_ctx));
  52. }
  53. /* input the HMAC key (can be called multiple times) */
  54. static void hmac_sha1_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1])
  55. {
  56. if(cx->klen + key_len > IN_BLOCK_LENGTH) /* if the key has to be hashed */
  57. {
  58. if(cx->klen <= IN_BLOCK_LENGTH) /* if the hash has not yet been */
  59. { /* started, initialise it and */
  60. sha1_begin(cx->ctx); /* hash stored key characters */
  61. sha1_hash(cx->key, cx->klen, cx->ctx);
  62. }
  63. sha1_hash(const_cast<unsigned char *>(key), key_len, cx->ctx); /* hash long key data into hash */
  64. }
  65. else /* otherwise store key data */
  66. memcpy(cx->key + cx->klen, key, key_len);
  67. cx->klen += key_len; /* update the key length count */
  68. }
  69. /* input the HMAC data (can be called multiple times) - */
  70. /* note that this call terminates the key input phase */
  71. static void hmac_sha1_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1])
  72. { unsigned int i;
  73. if(cx->klen != HMAC_IN_DATA) /* if not yet in data phase */
  74. {
  75. if(cx->klen > IN_BLOCK_LENGTH) /* if key is being hashed */
  76. { /* complete the hash and */
  77. sha1_end(cx->key, cx->ctx); /* store the result as the */
  78. cx->klen = OUT_BLOCK_LENGTH; /* key and set new length */
  79. }
  80. /* pad the key if necessary */
  81. memset(cx->key + cx->klen, 0, IN_BLOCK_LENGTH - cx->klen);
  82. /* xor ipad into key value */
  83. for(i = 0; i < (IN_BLOCK_LENGTH >> 2); ++i)
  84. ((unsigned long*)cx->key)[i] ^= 0x36363636;
  85. /* and start hash operation */
  86. sha1_begin(cx->ctx);
  87. sha1_hash(cx->key, IN_BLOCK_LENGTH, cx->ctx);
  88. /* mark as now in data mode */
  89. cx->klen = HMAC_IN_DATA;
  90. }
  91. /* hash the data (if any) */
  92. if(data_len)
  93. sha1_hash(const_cast<unsigned char *>(data), data_len, cx->ctx);
  94. }
  95. /* compute and output the MAC value */
  96. static void hmac_sha1_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1])
  97. { unsigned char dig[OUT_BLOCK_LENGTH];
  98. unsigned int i;
  99. /* if no data has been entered perform a null data phase */
  100. if(cx->klen != HMAC_IN_DATA)
  101. hmac_sha1_data((const unsigned char*)0, 0, cx);
  102. sha1_end(dig, cx->ctx); /* complete the inner hash */
  103. /* set outer key value using opad and removing ipad */
  104. for(i = 0; i < (IN_BLOCK_LENGTH >> 2); ++i)
  105. ((unsigned long*)cx->key)[i] ^= 0x36363636 ^ 0x5c5c5c5c;
  106. /* perform the outer hash operation */
  107. sha1_begin(cx->ctx);
  108. sha1_hash(cx->key, IN_BLOCK_LENGTH, cx->ctx);
  109. sha1_hash(dig, OUT_BLOCK_LENGTH, cx->ctx);
  110. sha1_end(dig, cx->ctx);
  111. /* output the hash value */
  112. for(i = 0; i < mac_len; ++i)
  113. mac[i] = dig[i];
  114. }
  115. #define BLOCK_SIZE 16
  116. void aes_set_encrypt_key(const unsigned char in_key[], unsigned int klen, void * cx)
  117. {
  118. call_aes_setup(cx, BLOCK_SIZE, const_cast<unsigned char *>(in_key), klen);
  119. }
  120. void aes_encrypt_block(const unsigned char in_blk[], unsigned char out_blk[], void * cx)
  121. {
  122. int Index;
  123. memmove(out_blk, in_blk, BLOCK_SIZE);
  124. for (Index = 0; Index < 4; Index++)
  125. {
  126. unsigned char t;
  127. t = out_blk[Index * 4 + 0];
  128. out_blk[Index * 4 + 0] = out_blk[Index * 4 + 3];
  129. out_blk[Index * 4 + 3] = t;
  130. t = out_blk[Index * 4 + 1];
  131. out_blk[Index * 4 + 1] = out_blk[Index * 4 + 2];
  132. out_blk[Index * 4 + 2] = t;
  133. }
  134. call_aes_encrypt(cx, reinterpret_cast<unsigned int*>(out_blk));
  135. for (Index = 0; Index < 4; Index++)
  136. {
  137. unsigned char t;
  138. t = out_blk[Index * 4 + 0];
  139. out_blk[Index * 4 + 0] = out_blk[Index * 4 + 3];
  140. out_blk[Index * 4 + 3] = t;
  141. t = out_blk[Index * 4 + 1];
  142. out_blk[Index * 4 + 1] = out_blk[Index * 4 + 2];
  143. out_blk[Index * 4 + 2] = t;
  144. }
  145. }
  146. typedef struct
  147. { unsigned char nonce[BLOCK_SIZE]; /* the CTR nonce */
  148. unsigned char encr_bfr[BLOCK_SIZE]; /* encrypt buffer */
  149. void * encr_ctx; /* encryption context */
  150. hmac_ctx auth_ctx; /* authentication context */
  151. unsigned int encr_pos; /* block position (enc) */
  152. unsigned int pwd_len; /* password length */
  153. unsigned int mode; /* File encryption mode */
  154. } fcrypt_ctx;
  155. #define MAX_KEY_LENGTH 32
  156. #define KEYING_ITERATIONS 1000
  157. #define PWD_VER_LENGTH 2
  158. /*
  159. Field lengths (in bytes) versus File Encryption Mode (0 < mode < 4)
  160. Mode Key Salt MAC Overhead
  161. 1 16 8 10 18
  162. 2 24 12 10 22
  163. 3 32 16 10 26
  164. The following macros assume that the mode value is correct.
  165. */
  166. #define KEY_LENGTH(mode) (8 * (mode & 3) + 8)
  167. #define SALT_LENGTH(mode) (4 * (mode & 3) + 4)
  168. #define MAC_LENGTH(mode) (10)
  169. /* subroutine for data encryption/decryption */
  170. /* this could be speeded up a lot by aligning */
  171. /* buffers and using 32 bit operations */
  172. static void derive_key(const unsigned char pwd[], /* the PASSWORD */
  173. unsigned int pwd_len, /* and its length */
  174. const unsigned char salt[], /* the SALT and its */
  175. unsigned int salt_len, /* length */
  176. unsigned int iter, /* the number of iterations */
  177. unsigned char key[], /* space for the output key */
  178. unsigned int key_len)/* and its required length */
  179. {
  180. unsigned int i, j, k, n_blk;
  181. unsigned char uu[OUT_BLOCK_LENGTH], ux[OUT_BLOCK_LENGTH];
  182. hmac_ctx c1[1], c2[1], c3[1];
  183. /* set HMAC context (c1) for password */
  184. hmac_sha1_begin(c1);
  185. hmac_sha1_key(pwd, pwd_len, c1);
  186. /* set HMAC context (c2) for password and salt */
  187. memmove(c2, c1, sizeof(hmac_ctx));
  188. hmac_sha1_data(salt, salt_len, c2);
  189. /* find the number of SHA blocks in the key */
  190. n_blk = 1 + (key_len - 1) / OUT_BLOCK_LENGTH;
  191. for(i = 0; i < n_blk; ++i) /* for each block in key */
  192. {
  193. /* ux[] holds the running xor value */
  194. memset(ux, 0, OUT_BLOCK_LENGTH);
  195. /* set HMAC context (c3) for password and salt */
  196. memmove(c3, c2, sizeof(hmac_ctx));
  197. /* enter additional data for 1st block into uu */
  198. uu[0] = (unsigned char)((i + 1) >> 24);
  199. uu[1] = (unsigned char)((i + 1) >> 16);
  200. uu[2] = (unsigned char)((i + 1) >> 8);
  201. uu[3] = (unsigned char)(i + 1);
  202. /* this is the key mixing iteration */
  203. for(j = 0, k = 4; j < iter; ++j)
  204. {
  205. /* add previous round data to HMAC */
  206. hmac_sha1_data(uu, k, c3);
  207. /* obtain HMAC for uu[] */
  208. hmac_sha1_end(uu, OUT_BLOCK_LENGTH, c3);
  209. /* xor into the running xor block */
  210. for(k = 0; k < OUT_BLOCK_LENGTH; ++k)
  211. ux[k] ^= uu[k];
  212. /* set HMAC context (c3) for password */
  213. memmove(c3, c1, sizeof(hmac_ctx));
  214. }
  215. /* compile key blocks into the key output */
  216. j = 0; k = i * OUT_BLOCK_LENGTH;
  217. while(j < OUT_BLOCK_LENGTH && k < key_len)
  218. key[k++] = ux[j++];
  219. }
  220. }
  221. static void encr_data(unsigned char data[], unsigned long d_len, fcrypt_ctx cx[1])
  222. {
  223. unsigned long i = 0, pos = cx->encr_pos;
  224. while(i < d_len)
  225. {
  226. if(pos == BLOCK_SIZE)
  227. { unsigned int j = 0;
  228. /* increment encryption nonce */
  229. while(j < 8 && !++cx->nonce[j])
  230. ++j;
  231. /* encrypt the nonce to form next xor buffer */
  232. aes_encrypt_block(cx->nonce, cx->encr_bfr, cx->encr_ctx);
  233. pos = 0;
  234. }
  235. data[i++] ^= cx->encr_bfr[pos++];
  236. }
  237. cx->encr_pos = pos;
  238. }
  239. static void fcrypt_init(
  240. int mode, /* the mode to be used (input) */
  241. const unsigned char pwd[], /* the user specified password (input) */
  242. unsigned int pwd_len, /* the length of the password (input) */
  243. const unsigned char salt[], /* the salt (input) */
  244. unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */
  245. fcrypt_ctx cx[1]) /* the file encryption context (output) */
  246. {
  247. unsigned char kbuf[2 * MAX_KEY_LENGTH + PWD_VER_LENGTH];
  248. cx->mode = mode;
  249. cx->pwd_len = pwd_len;
  250. /* derive the encryption and authetication keys and the password verifier */
  251. derive_key(pwd, pwd_len, salt, SALT_LENGTH(mode), KEYING_ITERATIONS,
  252. kbuf, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH);
  253. /* initialise the encryption nonce and buffer pos */
  254. cx->encr_pos = BLOCK_SIZE;
  255. /* if we need a random component in the encryption */
  256. /* nonce, this is where it would have to be set */
  257. memset(cx->nonce, 0, BLOCK_SIZE * sizeof(unsigned char));
  258. /* initialise for encryption using key 1 */
  259. cx->encr_ctx = call_aes_make_context();
  260. aes_set_encrypt_key(kbuf, KEY_LENGTH(mode), cx->encr_ctx);
  261. /* initialise for authentication using key 2 */
  262. hmac_sha1_begin(&cx->auth_ctx);
  263. hmac_sha1_key(kbuf + KEY_LENGTH(mode), KEY_LENGTH(mode), &cx->auth_ctx);
  264. if (pwd_ver != NULL)
  265. {
  266. memmove(pwd_ver, kbuf + 2 * KEY_LENGTH(mode), PWD_VER_LENGTH);
  267. }
  268. }
  269. /* perform 'in place' encryption and authentication */
  270. static void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
  271. {
  272. encr_data(data, data_len, cx);
  273. hmac_sha1_data(data, data_len, &cx->auth_ctx);
  274. }
  275. /* perform 'in place' authentication and decryption */
  276. static void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
  277. {
  278. hmac_sha1_data(data, data_len, &cx->auth_ctx);
  279. encr_data(data, data_len, cx);
  280. }
  281. /* close encryption/decryption and return the MAC value */
  282. static int fcrypt_end(unsigned char mac[], fcrypt_ctx cx[1])
  283. {
  284. hmac_sha1_end(mac, MAC_LENGTH(cx->mode), &cx->auth_ctx);
  285. call_aes_free_context(cx->encr_ctx);
  286. return MAC_LENGTH(cx->mode); /* return MAC length in bytes */
  287. }
  288. //---------------------------------------------------------------------------
  289. #define PASSWORD_MANAGER_AES_MODE 3
  290. //---------------------------------------------------------------------------
  291. static void __fastcall FillBufferWithRandomData(char * Buf, int Len)
  292. {
  293. while (Len > 0)
  294. {
  295. *Buf = static_cast<char>((rand() >> 7) & 0xFF);
  296. Buf++;
  297. Len--;
  298. }
  299. }
  300. //---------------------------------------------------------------------------
  301. static RawByteString __fastcall AES256Salt()
  302. {
  303. RawByteString Result;
  304. Result.SetLength(SALT_LENGTH(PASSWORD_MANAGER_AES_MODE));
  305. FillBufferWithRandomData(Result.c_str(), Result.Length());
  306. return Result;
  307. }
  308. //---------------------------------------------------------------------------
  309. void __fastcall AES256EncyptWithMAC(RawByteString Input, UnicodeString Password,
  310. RawByteString & Salt, RawByteString & Output, RawByteString & Mac)
  311. {
  312. fcrypt_ctx aes;
  313. if (Salt.IsEmpty())
  314. {
  315. Salt = AES256Salt();
  316. }
  317. assert(Salt.Length() == SALT_LENGTH(PASSWORD_MANAGER_AES_MODE));
  318. UTF8String UtfPassword = Password;
  319. fcrypt_init(PASSWORD_MANAGER_AES_MODE,
  320. reinterpret_cast<const unsigned char *>(UtfPassword.c_str()), UtfPassword.Length(),
  321. reinterpret_cast<const unsigned char *>(Salt.c_str()), NULL, &aes);
  322. Output = Input;
  323. Output.Unique();
  324. fcrypt_encrypt(reinterpret_cast<unsigned char *>(Output.c_str()), Output.Length(), &aes);
  325. Mac.SetLength(MAC_LENGTH(PASSWORD_MANAGER_AES_MODE));
  326. fcrypt_end(reinterpret_cast<unsigned char *>(Mac.c_str()), &aes);
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall AES256EncyptWithMAC(RawByteString Input, UnicodeString Password,
  330. RawByteString & Output)
  331. {
  332. RawByteString Salt;
  333. RawByteString Encrypted;
  334. RawByteString Mac;
  335. AES256EncyptWithMAC(Input, Password, Salt, Encrypted, Mac);
  336. Output = Salt + Encrypted + Mac;
  337. }
  338. //---------------------------------------------------------------------------
  339. bool __fastcall AES256DecryptWithMAC(RawByteString Input, UnicodeString Password,
  340. RawByteString Salt, RawByteString & Output, RawByteString Mac)
  341. {
  342. fcrypt_ctx aes;
  343. assert(Salt.Length() == SALT_LENGTH(PASSWORD_MANAGER_AES_MODE));
  344. UTF8String UtfPassword = Password;
  345. fcrypt_init(PASSWORD_MANAGER_AES_MODE,
  346. reinterpret_cast<const unsigned char *>(UtfPassword.c_str()), UtfPassword.Length(),
  347. reinterpret_cast<const unsigned char *>(Salt.c_str()), NULL, &aes);
  348. Output = Input;
  349. Output.Unique();
  350. fcrypt_decrypt(reinterpret_cast<unsigned char *>(Output.c_str()), Output.Length(), &aes);
  351. RawByteString Mac2;
  352. Mac2.SetLength(MAC_LENGTH(PASSWORD_MANAGER_AES_MODE));
  353. assert(Mac.Length() == Mac2.Length());
  354. fcrypt_end(reinterpret_cast<unsigned char *>(Mac2.c_str()), &aes);
  355. return (Mac2 == Mac);
  356. }
  357. //---------------------------------------------------------------------------
  358. bool __fastcall AES256DecryptWithMAC(RawByteString Input, UnicodeString Password,
  359. RawByteString & Output)
  360. {
  361. bool Result =
  362. Input.Length() > SALT_LENGTH(PASSWORD_MANAGER_AES_MODE) + MAC_LENGTH(PASSWORD_MANAGER_AES_MODE);
  363. if (Result)
  364. {
  365. RawByteString Salt = Input.SubString(1, SALT_LENGTH(PASSWORD_MANAGER_AES_MODE));
  366. RawByteString Encrypted =
  367. Input.SubString(SALT_LENGTH(PASSWORD_MANAGER_AES_MODE) + 1,
  368. Input.Length() - SALT_LENGTH(PASSWORD_MANAGER_AES_MODE) - MAC_LENGTH(PASSWORD_MANAGER_AES_MODE));
  369. RawByteString Mac =
  370. Input.SubString(Input.Length() - MAC_LENGTH(PASSWORD_MANAGER_AES_MODE) + 1,
  371. MAC_LENGTH(PASSWORD_MANAGER_AES_MODE));
  372. Result = AES256DecryptWithMAC(Encrypted, Password, Salt, Output, Mac);
  373. }
  374. return Result;
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall AES256CreateVerifier(UnicodeString Input, RawByteString & Verifier)
  378. {
  379. RawByteString Salt = AES256Salt();
  380. RawByteString Dummy = AES256Salt();
  381. RawByteString Encrypted;
  382. RawByteString Mac;
  383. AES256EncyptWithMAC(Dummy, Input, Salt, Encrypted, Mac);
  384. Verifier = Salt + Dummy + Mac;
  385. }
  386. //---------------------------------------------------------------------------
  387. bool __fastcall AES256Verify(UnicodeString Input, RawByteString Verifier)
  388. {
  389. int SaltLength = SALT_LENGTH(PASSWORD_MANAGER_AES_MODE);
  390. RawByteString Salt = Verifier.SubString(1, SaltLength);
  391. RawByteString Dummy = Verifier.SubString(SaltLength + 1, SaltLength);
  392. RawByteString Mac = Verifier.SubString(SaltLength + SaltLength + 1, MAC_LENGTH(PASSWORD_MANAGER_AES_MODE));
  393. RawByteString Encrypted;
  394. RawByteString Mac2;
  395. AES256EncyptWithMAC(Dummy, Input, Salt, Encrypted, Mac2);
  396. assert(Mac2.Length() == Mac.Length());
  397. return (Mac == Mac2);
  398. }
  399. //---------------------------------------------------------------------------
  400. unsigned char SScrambleTable[256] =
  401. {
  402. 0, 223, 235, 233, 240, 185, 88, 102, 22, 130, 27, 53, 79, 125, 66, 201,
  403. 90, 71, 51, 60, 134, 104, 172, 244, 139, 84, 91, 12, 123, 155, 237, 151,
  404. 192, 6, 87, 32, 211, 38, 149, 75, 164, 145, 52, 200, 224, 226, 156, 50,
  405. 136, 190, 232, 63, 129, 209, 181, 120, 28, 99, 168, 94, 198, 40, 238, 112,
  406. 55, 217, 124, 62, 227, 30, 36, 242, 208, 138, 174, 231, 26, 54, 214, 148,
  407. 37, 157, 19, 137, 187, 111, 228, 39, 110, 17, 197, 229, 118, 246, 153, 80,
  408. 21, 128, 69, 117, 234, 35, 58, 67, 92, 7, 132, 189, 5, 103, 10, 15,
  409. 252, 195, 70, 147, 241, 202, 107, 49, 20, 251, 133, 76, 204, 73, 203, 135,
  410. 184, 78, 194, 183, 1, 121, 109, 11, 143, 144, 171, 161, 48, 205, 245, 46,
  411. 31, 72, 169, 131, 239, 160, 25, 207, 218, 146, 43, 140, 127, 255, 81, 98,
  412. 42, 115, 173, 142, 114, 13, 2, 219, 57, 56, 24, 126, 3, 230, 47, 215,
  413. 9, 44, 159, 33, 249, 18, 93, 95, 29, 113, 220, 89, 97, 182, 248, 64,
  414. 68, 34, 4, 82, 74, 196, 213, 165, 179, 250, 108, 254, 59, 14, 236, 175,
  415. 85, 199, 83, 106, 77, 178, 167, 225, 45, 247, 163, 158, 8, 221, 61, 191,
  416. 119, 16, 253, 105, 186, 23, 170, 100, 216, 65, 162, 122, 150, 176, 154, 193,
  417. 206, 222, 188, 152, 210, 243, 96, 41, 86, 180, 101, 177, 166, 141, 212, 116
  418. };
  419. //---------------------------------------------------------------------------
  420. unsigned char * ScrambleTable;
  421. unsigned char * UnscrambleTable;
  422. //---------------------------------------------------------------------------
  423. RawByteString __fastcall ScramblePassword(UnicodeString Password)
  424. {
  425. #define SCRAMBLE_LENGTH_EXTENSION 50
  426. UTF8String UtfPassword = Password;
  427. int Len = UtfPassword.Length();
  428. char * Buf = new char[Len + SCRAMBLE_LENGTH_EXTENSION];
  429. int Padding = (((Len + 3) / 17) * 17 + 17) - 3 - Len;
  430. for (int Index = 0; Index < Padding; Index++)
  431. {
  432. int P = 0;
  433. while ((P <= 0) || (P > 255) || IsDigit(static_cast<wchar_t>(P)))
  434. {
  435. P = (int)((double)rand() / ((double)RAND_MAX / 256.0));
  436. }
  437. Buf[Index] = (unsigned char)P;
  438. }
  439. Buf[Padding] = (char)('0' + (Len % 10));
  440. Buf[Padding + 1] = (char)('0' + ((Len / 10) % 10));
  441. Buf[Padding + 2] = (char)('0' + ((Len / 100) % 10));
  442. strcpy(Buf + Padding + 3, UtfPassword.c_str());
  443. char * S = Buf;
  444. int Last = 31;
  445. while (*S != '\0')
  446. {
  447. Last = (Last + (unsigned char)*S) % 255 + 1;
  448. *S = ScrambleTable[Last];
  449. S++;
  450. }
  451. RawByteString Result = Buf;
  452. memset(Buf, 0, Len + SCRAMBLE_LENGTH_EXTENSION);
  453. delete[] Buf;
  454. return Result;
  455. }
  456. //---------------------------------------------------------------------------
  457. bool __fastcall UnscramblePassword(RawByteString Scrambled, UnicodeString & Password)
  458. {
  459. Scrambled.Unique();
  460. char * S = Scrambled.c_str();
  461. int Last = 31;
  462. while (*S != '\0')
  463. {
  464. int X = (int)UnscrambleTable[(unsigned char)*S] - 1 - (Last % 255);
  465. if (X <= 0)
  466. {
  467. X += 255;
  468. }
  469. *S = (char)X;
  470. Last = (Last + X) % 255 + 1;
  471. S++;
  472. }
  473. S = Scrambled.c_str();
  474. while ((*S != '\0') && ((*S < '0') || (*S > '9')))
  475. {
  476. S++;
  477. }
  478. bool Result = false;
  479. if (strlen(S) >= 3)
  480. {
  481. int Len = (S[0] - '0') + 10 * (S[1] - '0') + 100 * (S[2] - '0');
  482. int Total = (((Len + 3) / 17) * 17 + 17);
  483. if ((Len >= 0) && (Total == Scrambled.Length()) && (Total - (S - Scrambled.c_str()) - 3 == Len))
  484. {
  485. Scrambled.Delete(1, Scrambled.Length() - Len);
  486. Result = true;
  487. }
  488. }
  489. if (Result)
  490. {
  491. Password = UTF8String(Scrambled.c_str(), Scrambled.Length());
  492. }
  493. else
  494. {
  495. Password = "";
  496. }
  497. return Result;
  498. }
  499. //---------------------------------------------------------------------------
  500. void __fastcall CryptographyInitialize()
  501. {
  502. ScrambleTable = SScrambleTable;
  503. UnscrambleTable = new unsigned char[256];
  504. for (int Index = 0; Index < 256; Index++)
  505. {
  506. UnscrambleTable[SScrambleTable[Index]] = (unsigned char)Index;
  507. }
  508. srand((unsigned int)time(NULL) ^ (unsigned int)getpid());
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall CryptographyFinalize()
  512. {
  513. delete[] UnscrambleTable;
  514. UnscrambleTable = NULL;
  515. ScrambleTable = NULL;
  516. }
  517. //---------------------------------------------------------------------------
  518. int __fastcall PasswordMaxLength()
  519. {
  520. return 128;
  521. }
  522. //---------------------------------------------------------------------------
  523. int __fastcall IsValidPassword(UnicodeString Password)
  524. {
  525. if (Password.IsEmpty() || (Password.Length() > PasswordMaxLength()))
  526. {
  527. return -1;
  528. }
  529. else
  530. {
  531. int A = 0;
  532. int B = 0;
  533. int C = 0;
  534. int D = 0;
  535. for (int Index = 1; Index <= Password.Length(); Index++)
  536. {
  537. if ((Password[Index] >= L'a') && (Password[Index] <= L'z'))
  538. {
  539. A = 1;
  540. }
  541. else if ((Password[Index] >= L'A') && (Password[Index] <= L'Z'))
  542. {
  543. B = 1;
  544. }
  545. else if ((Password[Index] >= L'0') && (Password[Index] <= L'9'))
  546. {
  547. C = 1;
  548. }
  549. else
  550. {
  551. D = 1;
  552. }
  553. }
  554. return (Password.Length() >= 6) && ((A + B + C + D) >= 2);
  555. }
  556. }