codec.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // Name: codec.h
  4. // Purpose:
  5. // Author: Ulrich Telle
  6. // Modified by:
  7. // Created: 2006-12-06
  8. // Copyright: (c) Ulrich Telle
  9. // Licence: wxWindows licence
  10. ///////////////////////////////////////////////////////////////////////////////
  11. /// \file codec.h Interface of the codec class
  12. */
  13. #ifndef _CODEC_H_
  14. #define _CODEC_H_
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #if defined(__BORLANDC__)
  19. #define __STDC__ 1
  20. #endif
  21. #if defined(__BORLANDC__)
  22. #undef __STDC__
  23. #endif
  24. /*
  25. // ATTENTION: Macro similar to that in pager.c
  26. // TODO: Check in case of new version of SQLite
  27. */
  28. #define WX_PAGER_MJ_PGNO(x) ((PENDING_BYTE/(x))+1)
  29. #ifdef __cplusplus
  30. } /* End of the 'extern "C"' block */
  31. #endif
  32. #include "rijndael.h"
  33. #define CODEC_TYPE_AES128 1
  34. #define CODEC_TYPE_AES256 2
  35. #ifndef CODEC_TYPE
  36. #define CODEC_TYPE CODEC_TYPE_AES128
  37. #endif
  38. #if CODEC_TYPE == CODEC_TYPE_AES256
  39. #define KEYLENGTH 32
  40. #define CODEC_SHA_ITER 4001
  41. #else
  42. #define KEYLENGTH 16
  43. #endif
  44. typedef struct _Codec
  45. {
  46. int m_isEncrypted;
  47. int m_hasReadKey;
  48. unsigned char m_readKey[KEYLENGTH];
  49. int m_hasWriteKey;
  50. unsigned char m_writeKey[KEYLENGTH];
  51. Rijndael* m_aes;
  52. Btree* m_bt; /* Pointer to B-tree used by DB */
  53. unsigned char m_page[SQLITE_MAX_PAGE_SIZE+24];
  54. } Codec;
  55. void CodecInit(Codec* codec);
  56. void CodecTerm(Codec* codec);
  57. void CodecCopy(Codec* codec, Codec* other);
  58. void CodecGenerateReadKey(Codec* codec, char* userPassword, int passwordLength);
  59. void CodecGenerateWriteKey(Codec* codec, char* userPassword, int passwordLength);
  60. void CodecEncrypt(Codec* codec, int page, unsigned char* data, int len, int useWriteKey);
  61. void CodecDecrypt(Codec* codec, int page, unsigned char* data, int len);
  62. void CodecCopyKey(Codec* codec, int read2write);
  63. void CodecSetIsEncrypted(Codec* codec, int isEncrypted);
  64. void CodecSetHasReadKey(Codec* codec, int hasReadKey);
  65. void CodecSetHasWriteKey(Codec* codec, int hasWriteKey);
  66. void CodecSetBtree(Codec* codec, Btree* bt);
  67. int CodecIsEncrypted(Codec* codec);
  68. int CodecHasReadKey(Codec* codec);
  69. int CodecHasWriteKey(Codec* codec);
  70. Btree* CodecGetBtree(Codec* codec);
  71. unsigned char* CodecGetPageBuffer(Codec* codec);
  72. void CodecGenerateEncryptionKey(Codec* codec, char* userPassword, int passwordLength,
  73. unsigned char encryptionKey[KEYLENGTH]);
  74. void CodecPadPassword(Codec* codec, char* password, int pswdlen, unsigned char pswd[32]);
  75. void CodecRC4(Codec* codec, unsigned char* key, int keylen,
  76. unsigned char* textin, int textlen,
  77. unsigned char* textout);
  78. void CodecGetMD5Binary(Codec* codec, unsigned char* data, int length, unsigned char* digest);
  79. #if CODEC_TYPE == CODEC_TYPE_AES256
  80. void CodecGetSHABinary(Codec* codec, unsigned char* data, int length, unsigned char* digest);
  81. #endif
  82. void CodecGenerateInitialVector(Codec* codec, int seed, unsigned char iv[16]);
  83. void CodecAES(Codec* codec, int page, int encrypt, unsigned char encryptionKey[KEYLENGTH],
  84. unsigned char* datain, int datalen, unsigned char* dataout);
  85. #endif