codec.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. ///////////////////////////////////////////////////////////////////////////////
  3. ///////////////////////////////////////////////////////////////////////////////
  4. // Name: codec.cpp
  5. // Purpose:
  6. // Author: Ulrich Telle
  7. // Modified by:
  8. // Created: 2006-12-06
  9. // RCS-ID: $$
  10. // Copyright: (c) Ulrich Telle
  11. // Licence: wxWindows licence + RSA Data Security license
  12. ///////////////////////////////////////////////////////////////////////////////
  13. /// \file codec.cpp Implementation of MD5, RC4 and AES algorithms
  14. */
  15. /*
  16. **********************************************************************
  17. ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
  18. ** **
  19. ** License to copy and use this software is granted provided that **
  20. ** it is identified as the "RSA Data Security, Inc. MD5 Message **
  21. ** Digest Algorithm" in all material mentioning or referencing this **
  22. ** software or this function. **
  23. ** **
  24. ** License is also granted to make and use derivative works **
  25. ** provided that such works are identified as "derived from the RSA **
  26. ** Data Security, Inc. MD5 Message Digest Algorithm" in all **
  27. ** material mentioning or referencing the derived work. **
  28. ** **
  29. ** RSA Data Security, Inc. makes no representations concerning **
  30. ** either the merchantability of this software or the suitability **
  31. ** of this software for any particular purpose. It is provided "as **
  32. ** is" without express or implied warranty of any kind. **
  33. ** **
  34. ** These notices must be retained in any copies of any part of this **
  35. ** documentation and/or software. **
  36. **********************************************************************
  37. */
  38. #include "codec.h"
  39. #ifndef SQLITE_USER_AUTHENTICATION
  40. #if CODEC_TYPE == CODEC_TYPE_AES256
  41. #include "sha2.h"
  42. #include "sha2.c"
  43. #endif
  44. #endif
  45. /*
  46. // ----------------
  47. // MD5 by RSA
  48. // ----------------
  49. // C headers for MD5
  50. */
  51. #include <sys/types.h>
  52. #include <string.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #define MD5_HASHBYTES 16
  56. /*
  57. /// Structure representing an MD5 context while ecrypting. (For internal use only)
  58. */
  59. typedef struct MD5Context
  60. {
  61. unsigned int buf[4];
  62. unsigned int bits[2];
  63. unsigned char in[64];
  64. } MD5_CTX;
  65. static void MD5Init(MD5_CTX *context);
  66. static void MD5Update(MD5_CTX *context, unsigned char *buf, unsigned len);
  67. static void MD5Final(unsigned char digest[MD5_HASHBYTES], MD5_CTX *context);
  68. static void MD5Transform(unsigned int buf[4], unsigned int in[16]);
  69. static void byteReverse(unsigned char *buf, unsigned longs);
  70. /*
  71. * Note: this code is harmless on little-endian machines.
  72. */
  73. static void byteReverse(unsigned char *buf, unsigned longs)
  74. {
  75. static int littleEndian = -1;
  76. if (littleEndian < 0)
  77. {
  78. /* Are we little or big endian? This method is from Harbison & Steele. */
  79. union
  80. {
  81. long l;
  82. char c[sizeof(long)];
  83. } u;
  84. u.l = 1;
  85. littleEndian = (u.c[0] == 1) ? 1 : 0;
  86. }
  87. if (littleEndian != 1)
  88. {
  89. unsigned int t;
  90. do
  91. {
  92. t = (unsigned int) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
  93. ((unsigned) buf[1] << 8 | buf[0]);
  94. *(unsigned int *) buf = t;
  95. buf += 4;
  96. }
  97. while (--longs);
  98. }
  99. }
  100. #if 0
  101. static char* MD5End(MD5_CTX *, char *);
  102. static char* MD5End(MD5_CTX *ctx, char *buf)
  103. {
  104. int i;
  105. unsigned char digest[MD5_HASHBYTES];
  106. char hex[]="0123456789abcdef";
  107. if (!buf)
  108. {
  109. buf = (char *)malloc(33);
  110. }
  111. if (!buf)
  112. {
  113. return 0;
  114. }
  115. MD5Final(digest,ctx);
  116. for (i=0;i<MD5_HASHBYTES;i++)
  117. {
  118. buf[i+i] = hex[digest[i] >> 4];
  119. buf[i+i+1] = hex[digest[i] & 0x0f];
  120. }
  121. buf[i+i] = '\0';
  122. return buf;
  123. }
  124. #endif
  125. /*
  126. * Final wrapup - pad to 64-byte boundary with the bit pattern
  127. * 1 0* (64-bit count of bits processed, MSB-first)
  128. */
  129. static void MD5Final(unsigned char digest[16], MD5_CTX *ctx)
  130. {
  131. unsigned count;
  132. unsigned char *p;
  133. /* Compute number of bytes mod 64 */
  134. count = (ctx->bits[0] >> 3) & 0x3F;
  135. /* Set the first char of padding to 0x80. This is safe since there is
  136. always at least one byte free */
  137. p = ctx->in + count;
  138. *p++ = 0x80;
  139. /* Bytes of padding needed to make 64 bytes */
  140. count = 64 - 1 - count;
  141. /* Pad out to 56 mod 64 */
  142. if (count < 8)
  143. {
  144. /* Two lots of padding: Pad the first block to 64 bytes */
  145. memset(p, 0, count);
  146. byteReverse(ctx->in, 16);
  147. MD5Transform(ctx->buf, (unsigned int *) ctx->in);
  148. /* Now fill the next block with 56 bytes */
  149. memset(ctx->in, 0, 56);
  150. }
  151. else
  152. {
  153. /* Pad block to 56 bytes */
  154. memset(p, 0, count - 8);
  155. }
  156. byteReverse(ctx->in, 14);
  157. /* Append length in bits and transform */
  158. ((unsigned int *) ctx->in)[14] = ctx->bits[0];
  159. ((unsigned int *) ctx->in)[15] = ctx->bits[1];
  160. MD5Transform(ctx->buf, (unsigned int *) ctx->in);
  161. byteReverse((unsigned char *) ctx->buf, 4);
  162. memcpy(digest, ctx->buf, 16);
  163. memset((char *) ctx, 0, sizeof(ctx)); /* In case it's sensitive */
  164. }
  165. static void MD5Init(MD5_CTX *ctx)
  166. {
  167. ctx->buf[0] = 0x67452301;
  168. ctx->buf[1] = 0xefcdab89;
  169. ctx->buf[2] = 0x98badcfe;
  170. ctx->buf[3] = 0x10325476;
  171. ctx->bits[0] = 0;
  172. ctx->bits[1] = 0;
  173. }
  174. static void MD5Update(MD5_CTX *ctx, unsigned char *buf, unsigned len)
  175. {
  176. unsigned int t;
  177. /* Update bitcount */
  178. t = ctx->bits[0];
  179. if ((ctx->bits[0] = t + ((unsigned int) len << 3)) < t)
  180. {
  181. ctx->bits[1]++; /* Carry from low to high */
  182. }
  183. ctx->bits[1] += len >> 29;
  184. t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
  185. /* Handle any leading odd-sized chunks */
  186. if (t)
  187. {
  188. unsigned char *p = (unsigned char *) ctx->in + t;
  189. t = 64 - t;
  190. if (len < t)
  191. {
  192. memcpy(p, buf, len);
  193. return;
  194. }
  195. memcpy(p, buf, t);
  196. byteReverse(ctx->in, 16);
  197. MD5Transform(ctx->buf, (unsigned int *) ctx->in);
  198. buf += t;
  199. len -= t;
  200. }
  201. /* Process data in 64-byte chunks */
  202. while (len >= 64)
  203. {
  204. memcpy(ctx->in, buf, 64);
  205. byteReverse(ctx->in, 16);
  206. MD5Transform(ctx->buf, (unsigned int *) ctx->in);
  207. buf += 64;
  208. len -= 64;
  209. }
  210. /* Handle any remaining bytes of data. */
  211. memcpy(ctx->in, buf, len);
  212. }
  213. /* #define F1(x, y, z) (x & y | ~x & z) */
  214. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  215. #define F2(x, y, z) F1(z, x, y)
  216. #define F3(x, y, z) (x ^ y ^ z)
  217. #define F4(x, y, z) (y ^ (x | ~z))
  218. /* This is the central step in the MD5 algorithm. */
  219. #define MD5STEP(f, w, x, y, z, data, s) \
  220. ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
  221. /*
  222. * The core of the MD5 algorithm, this alters an existing MD5 hash to
  223. * reflect the addition of 16 longwords of new data. MD5Update blocks
  224. * the data and converts bytes into longwords for this routine.
  225. */
  226. static void MD5Transform(unsigned int buf[4], unsigned int in[16])
  227. {
  228. register unsigned int a, b, c, d;
  229. a = buf[0];
  230. b = buf[1];
  231. c = buf[2];
  232. d = buf[3];
  233. MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  234. MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  235. MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  236. MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  237. MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  238. MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  239. MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  240. MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  241. MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  242. MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  243. MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  244. MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  245. MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  246. MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  247. MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  248. MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  249. MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  250. MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  251. MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  252. MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  253. MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  254. MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  255. MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  256. MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  257. MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  258. MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  259. MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  260. MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  261. MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  262. MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  263. MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  264. MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  265. MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  266. MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  267. MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  268. MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  269. MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  270. MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  271. MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  272. MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  273. MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  274. MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  275. MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  276. MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  277. MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  278. MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  279. MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  280. MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  281. MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  282. MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  283. MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  284. MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  285. MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  286. MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  287. MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  288. MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  289. MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  290. MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  291. MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  292. MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  293. MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  294. MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  295. MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  296. MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  297. buf[0] += a;
  298. buf[1] += b;
  299. buf[2] += c;
  300. buf[3] += d;
  301. }
  302. /*
  303. // ---------------------------
  304. // RC4 implementation
  305. // ---------------------------
  306. */
  307. /**
  308. * RC4 is the standard encryption algorithm used in PDF format
  309. */
  310. void
  311. CodecRC4(Codec* codec, unsigned char* key, int keylen,
  312. unsigned char* textin, int textlen,
  313. unsigned char* textout)
  314. {
  315. int i;
  316. int j;
  317. int t;
  318. unsigned char rc4[256];
  319. int a = 0;
  320. int b = 0;
  321. unsigned char k;
  322. for (i = 0; i < 256; i++)
  323. {
  324. rc4[i] = i;
  325. }
  326. j = 0;
  327. for (i = 0; i < 256; i++)
  328. {
  329. t = rc4[i];
  330. j = (j + t + key[i % keylen]) % 256;
  331. rc4[i] = rc4[j];
  332. rc4[j] = t;
  333. }
  334. for (i = 0; i < textlen; i++)
  335. {
  336. a = (a + 1) % 256;
  337. t = rc4[a];
  338. b = (b + t) % 256;
  339. rc4[a] = rc4[b];
  340. rc4[b] = t;
  341. k = rc4[(rc4[a] + rc4[b]) % 256];
  342. textout[i] = textin[i] ^ k;
  343. }
  344. }
  345. void
  346. CodecGetMD5Binary(Codec* codec, unsigned char* data, int length, unsigned char* digest)
  347. {
  348. MD5_CTX ctx;
  349. MD5Init(&ctx);
  350. MD5Update(&ctx, data, length);
  351. MD5Final(digest,&ctx);
  352. }
  353. #if CODEC_TYPE == CODEC_TYPE_AES256
  354. void
  355. CodecGetSHABinary(Codec* codec, unsigned char* data, int length, unsigned char* digest)
  356. {
  357. sha256(data, (unsigned int) length, digest);
  358. }
  359. #endif
  360. #define MODMULT(a, b, c, m, s) q = s / a; s = b * (s - a * q) - c * q; if (s < 0) s += m
  361. void
  362. CodecGenerateInitialVector(Codec* codec, int seed, unsigned char iv[16])
  363. {
  364. unsigned char initkey[16];
  365. int j, q;
  366. int z = seed + 1;
  367. for (j = 0; j < 4; j++)
  368. {
  369. MODMULT(52774, 40692, 3791, 2147483399L, z);
  370. initkey[4*j+0] = 0xff & z;
  371. initkey[4*j+1] = 0xff & (z >> 8);
  372. initkey[4*j+2] = 0xff & (z >> 16);
  373. initkey[4*j+3] = 0xff & (z >> 24);
  374. }
  375. CodecGetMD5Binary(codec, (unsigned char*) initkey, 16, iv);
  376. }
  377. void
  378. CodecAES(Codec* codec, int page, int encrypt, unsigned char encryptionKey[KEYLENGTH],
  379. unsigned char* datain, int datalen, unsigned char* dataout)
  380. {
  381. unsigned char initial[16];
  382. unsigned char pagekey[KEYLENGTH];
  383. unsigned char nkey[KEYLENGTH+4+4];
  384. int keyLength = KEYLENGTH;
  385. int nkeylen = keyLength + 4 + 4;
  386. int j;
  387. int direction = (encrypt) ? RIJNDAEL_Direction_Encrypt : RIJNDAEL_Direction_Decrypt;
  388. int len = 0;
  389. for (j = 0; j < keyLength; j++)
  390. {
  391. nkey[j] = encryptionKey[j];
  392. }
  393. nkey[keyLength+0] = 0xff & page;
  394. nkey[keyLength+1] = 0xff & (page >> 8);
  395. nkey[keyLength+2] = 0xff & (page >> 16);
  396. nkey[keyLength+3] = 0xff & (page >> 24);
  397. /* AES encryption needs some 'salt' */
  398. nkey[keyLength+4] = 0x73;
  399. nkey[keyLength+5] = 0x41;
  400. nkey[keyLength+6] = 0x6c;
  401. nkey[keyLength+7] = 0x54;
  402. #if CODEC_TYPE == CODEC_TYPE_AES256
  403. CodecGetSHABinary(codec, nkey, nkeylen, pagekey);
  404. #else
  405. CodecGetMD5Binary(codec, nkey, nkeylen, pagekey);
  406. #endif
  407. CodecGenerateInitialVector(codec, page, initial);
  408. #if CODEC_TYPE == CODEC_TYPE_AES256
  409. RijndaelInit(codec->m_aes, RIJNDAEL_Direction_Mode_CBC, direction, pagekey, RIJNDAEL_Direction_KeyLength_Key32Bytes, initial);
  410. #else
  411. RijndaelInit(codec->m_aes, RIJNDAEL_Direction_Mode_CBC, direction, pagekey, RIJNDAEL_Direction_KeyLength_Key16Bytes, initial);
  412. #endif
  413. if (encrypt)
  414. {
  415. len = RijndaelBlockEncrypt(codec->m_aes, datain, datalen*8, dataout);
  416. }
  417. else
  418. {
  419. len = RijndaelBlockDecrypt(codec->m_aes, datain, datalen*8, dataout);
  420. }
  421. /* It is a good idea to check the error code */
  422. if (len < 0)
  423. {
  424. /* AES: Error on encrypting. */
  425. }
  426. }
  427. static unsigned char padding[] =
  428. "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
  429. void
  430. CodecInit(Codec* codec)
  431. {
  432. codec->m_isEncrypted = 0;
  433. codec->m_hasReadKey = 0;
  434. codec->m_hasWriteKey = 0;
  435. codec->m_aes = (Rijndael*) sqlite3_malloc(sizeof(Rijndael));
  436. RijndaelCreate(codec->m_aes);
  437. }
  438. void
  439. CodecTerm(Codec* codec)
  440. {
  441. sqlite3_free(codec->m_aes);
  442. }
  443. void
  444. CodecSetIsEncrypted(Codec* codec, int isEncrypted)
  445. {
  446. codec->m_isEncrypted = isEncrypted;
  447. }
  448. void
  449. CodecSetHasReadKey(Codec* codec, int hasReadKey)
  450. {
  451. codec->m_hasReadKey = hasReadKey;
  452. }
  453. void
  454. CodecSetHasWriteKey(Codec* codec, int hasWriteKey)
  455. {
  456. codec->m_hasWriteKey = hasWriteKey;
  457. }
  458. void
  459. CodecSetBtree(Codec* codec, Btree* bt)
  460. {
  461. codec->m_bt = bt;
  462. }
  463. int
  464. CodecIsEncrypted(Codec* codec)
  465. {
  466. return codec->m_isEncrypted;
  467. }
  468. int
  469. CodecHasReadKey(Codec* codec)
  470. {
  471. return codec->m_hasReadKey;
  472. }
  473. int
  474. CodecHasWriteKey(Codec* codec)
  475. {
  476. return codec->m_hasWriteKey;
  477. }
  478. Btree*
  479. CodecGetBtree(Codec* codec)
  480. {
  481. return codec->m_bt;
  482. }
  483. unsigned char*
  484. CodecGetPageBuffer(Codec* codec)
  485. {
  486. return &codec->m_page[4];
  487. }
  488. void
  489. CodecCopy(Codec* codec, Codec* other)
  490. {
  491. int j;
  492. codec->m_isEncrypted = other->m_isEncrypted;
  493. codec->m_hasReadKey = other->m_hasReadKey;
  494. codec->m_hasWriteKey = other->m_hasWriteKey;
  495. for (j = 0; j < KEYLENGTH; j++)
  496. {
  497. codec->m_readKey[j] = other->m_readKey[j];
  498. codec->m_writeKey[j] = other->m_writeKey[j];
  499. }
  500. codec->m_bt = other->m_bt;
  501. RijndaelInvalidate(codec->m_aes);
  502. }
  503. void
  504. CodecCopyKey(Codec* codec, int read2write)
  505. {
  506. int j;
  507. if (read2write)
  508. {
  509. for (j = 0; j < KEYLENGTH; j++)
  510. {
  511. codec->m_writeKey[j] = codec->m_readKey[j];
  512. }
  513. }
  514. else
  515. {
  516. for (j = 0; j < KEYLENGTH; j++)
  517. {
  518. codec->m_readKey[j] = codec->m_writeKey[j];
  519. }
  520. }
  521. }
  522. void
  523. CodecPadPassword(Codec* codec, char* password, int pswdlen, unsigned char pswd[32])
  524. {
  525. int j;
  526. int p = 0;
  527. int m = pswdlen;
  528. if (m > 32) m = 32;
  529. for (j = 0; j < m; j++)
  530. {
  531. pswd[p++] = (unsigned char) password[j];
  532. }
  533. for (j = 0; p < 32 && j < 32; j++)
  534. {
  535. pswd[p++] = padding[j];
  536. }
  537. }
  538. void
  539. CodecGenerateReadKey(Codec* codec, char* userPassword, int passwordLength)
  540. {
  541. CodecGenerateEncryptionKey(codec, userPassword, passwordLength, codec->m_readKey);
  542. }
  543. void
  544. CodecGenerateWriteKey(Codec* codec, char* userPassword, int passwordLength)
  545. {
  546. CodecGenerateEncryptionKey(codec, userPassword, passwordLength, codec->m_writeKey);
  547. }
  548. void
  549. CodecGenerateEncryptionKey(Codec* codec, char* userPassword, int passwordLength,
  550. unsigned char encryptionKey[KEYLENGTH])
  551. {
  552. #if CODEC_TYPE == CODEC_TYPE_AES256
  553. unsigned char userPad[32];
  554. unsigned char digest[KEYLENGTH];
  555. int keyLength = KEYLENGTH;
  556. int k;
  557. /* Pad password */
  558. CodecPadPassword(codec, userPassword, passwordLength, userPad);
  559. sha256(userPad, 32, digest);
  560. for (k = 0; k < CODEC_SHA_ITER; ++k)
  561. {
  562. sha256(digest, KEYLENGTH, digest);
  563. }
  564. memcpy(encryptionKey, digest, keyLength);
  565. #else
  566. unsigned char userPad[32];
  567. unsigned char ownerPad[32];
  568. unsigned char ownerKey[32];
  569. unsigned char mkey[MD5_HASHBYTES];
  570. unsigned char digest[MD5_HASHBYTES];
  571. int keyLength = MD5_HASHBYTES;
  572. int i, j, k;
  573. MD5_CTX ctx;
  574. /* Pad passwords */
  575. CodecPadPassword(codec, userPassword, passwordLength, userPad);
  576. CodecPadPassword(codec, "", 0, ownerPad);
  577. /* Compute owner key */
  578. MD5Init(&ctx);
  579. MD5Update(&ctx, ownerPad, 32);
  580. MD5Final(digest,&ctx);
  581. /* only use for the input as many bit as the key consists of */
  582. for (k = 0; k < 50; ++k)
  583. {
  584. MD5Init(&ctx);
  585. MD5Update(&ctx, digest, keyLength);
  586. MD5Final(digest,&ctx);
  587. }
  588. memcpy(ownerKey, userPad, 32);
  589. for (i = 0; i < 20; ++i)
  590. {
  591. for (j = 0; j < keyLength ; ++j)
  592. {
  593. mkey[j] = (digest[j] ^ i);
  594. }
  595. CodecRC4(codec, mkey, keyLength, ownerKey, 32, ownerKey);
  596. }
  597. /* Compute encryption key */
  598. MD5Init(&ctx);
  599. MD5Update(&ctx, userPad, 32);
  600. MD5Update(&ctx, ownerKey, 32);
  601. MD5Final(digest,&ctx);
  602. /* only use the really needed bits as input for the hash */
  603. for (k = 0; k < 50; ++k)
  604. {
  605. MD5Init(&ctx);
  606. MD5Update(&ctx, digest, keyLength);
  607. MD5Final(digest, &ctx);
  608. }
  609. memcpy(encryptionKey, digest, keyLength);
  610. #endif
  611. }
  612. void
  613. CodecEncrypt(Codec* codec, int page, unsigned char* data, int len, int useWriteKey)
  614. {
  615. #ifdef WXSQLITE3_USE_OLD_ENCRYPTION_SCHEME
  616. /* Use the previous encryption scheme */
  617. unsigned char* key = (useWriteKey) ? codec->m_writeKey : codec->m_readKey;
  618. CodecAES(codec, page, 1, key, data, len, data);
  619. #else
  620. unsigned char dbHeader[8];
  621. int offset = 0;
  622. unsigned char* key = (useWriteKey) ? codec->m_writeKey : codec->m_readKey;
  623. if (page == 1)
  624. {
  625. /* Save the header bytes remaining unencrypted */
  626. memcpy(dbHeader, data+16, 8);
  627. offset = 16;
  628. CodecAES(codec, page, 1, key, data, 16, data);
  629. }
  630. CodecAES(codec, page, 1, key, data+offset, len-offset, data+offset);
  631. if (page == 1)
  632. {
  633. /* Move the encrypted header bytes 16..23 to a safe position */
  634. memcpy(data+8, data+16, 8);
  635. /* Restore the unencrypted header bytes 16..23 */
  636. memcpy(data+16, dbHeader, 8);
  637. }
  638. #endif
  639. }
  640. void
  641. CodecDecrypt(Codec* codec, int page, unsigned char* data, int len)
  642. {
  643. #ifdef WXSQLITE3_USE_OLD_ENCRYPTION_SCHEME
  644. /* Use the previous encryption scheme */
  645. CodecAES(codec, page, 0, codec->m_readKey, data, len, data);
  646. #else
  647. unsigned char dbHeader[8];
  648. int dbPageSize;
  649. int offset = 0;
  650. if (page == 1)
  651. {
  652. /* Save (unencrypted) header bytes 16..23 */
  653. memcpy(dbHeader, data+16, 8);
  654. /* Determine page size */
  655. dbPageSize = (dbHeader[0] << 8) | (dbHeader[1] << 16);
  656. /* Check whether the database header is valid */
  657. /* If yes, the database follows the new encryption scheme, otherwise use the previous encryption scheme */
  658. if ((dbPageSize >= 512) && (dbPageSize <= SQLITE_MAX_PAGE_SIZE) && (((dbPageSize-1) & dbPageSize) == 0) &&
  659. (dbHeader[5] == 0x40) && (dbHeader[6] == 0x20) && (dbHeader[7] == 0x20))
  660. {
  661. /* Restore encrypted bytes 16..23 for new encryption scheme */
  662. memcpy(data+16, data+8, 8);
  663. offset = 16;
  664. }
  665. }
  666. CodecAES(codec, page, 0, codec->m_readKey, data+offset, len-offset, data+offset);
  667. if (page == 1 && offset != 0)
  668. {
  669. /* Verify the database header */
  670. if (memcmp(dbHeader, data+16, 8) == 0)
  671. {
  672. memcpy(data, SQLITE_FILE_HEADER, 16);
  673. }
  674. }
  675. #endif
  676. }