hpdf_encrypt.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * << Haru Free PDF Library >> -- hpdf_encrypt.h
  3. *
  4. * URL: http://libharu.org
  5. *
  6. * Copyright (c) 1999-2006 Takeshi Kanno <[email protected]>
  7. * Copyright (c) 2007-2009 Antony Dovgal <[email protected]>
  8. *
  9. * Permission to use, copy, modify, distribute and sell this software
  10. * and its documentation for any purpose is hereby granted without fee,
  11. * provided that the above copyright notice appear in all copies and
  12. * that both that copyright notice and this permission notice appear
  13. * in supporting documentation.
  14. * It is provided "as is" without express or implied warranty.
  15. *
  16. *------------------------------------------------------------------------------
  17. *
  18. * The code implements MD5 message-digest algorithm is based on the code
  19. * written by Colin Plumb.
  20. * The copyright of it is as follows.
  21. *
  22. * This code implements the MD5 message-digest algorithm.
  23. * The algorithm is due to Ron Rivest. This code was
  24. * written by Colin Plumb in 1993, no copyright is claimed.
  25. * This code is in the public domain; do with it what you wish.
  26. *
  27. * Equivalent code is available from RSA Data Security, Inc.
  28. * This code has been tested against that, and is equivalent,
  29. * except that you don't need to include two pages of legalese
  30. * with every copy.
  31. *
  32. * To compute the message digest of a chunk of bytes, declare an
  33. * MD5Context structure, pass it to MD5Init, call MD5Update as
  34. * needed on buffers full of bytes, and then call MD5Final, which
  35. * will fill a supplied 16-byte array with the digest.
  36. *
  37. *---------------------------------------------------------------------------*/
  38. #ifndef HPDF_ENCRYPT_H
  39. #define HPDF_ENCRYPT_H
  40. #include "hpdf_mmgr.h"
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /*----------------------------------------------------------------------------*/
  45. /*----- encrypt-dict ---------------------------------------------------------*/
  46. #define HPDF_ID_LEN 16
  47. #define HPDF_PASSWD_LEN 32
  48. #define HPDF_ENCRYPT_KEY_MAX 16
  49. #define HPDF_MD5_KEY_LEN 16
  50. #define HPDF_PERMISSION_PAD 0xFFFFFFC0
  51. #define HPDF_ARC4_BUF_SIZE 256
  52. typedef struct HPDF_MD5Context
  53. {
  54. HPDF_UINT32 buf[4];
  55. HPDF_UINT32 bits[2];
  56. HPDF_BYTE in[64];
  57. } HPDF_MD5_CTX;
  58. typedef struct _HPDF_ARC4_Ctx_Rec {
  59. HPDF_BYTE idx1;
  60. HPDF_BYTE idx2;
  61. HPDF_BYTE state[HPDF_ARC4_BUF_SIZE];
  62. } HPDF_ARC4_Ctx_Rec;
  63. typedef struct _HPDF_Encrypt_Rec *HPDF_Encrypt;
  64. typedef struct _HPDF_Encrypt_Rec {
  65. HPDF_EncryptMode mode;
  66. /* key_len must be a multiple of 8, and between 40 to 128 */
  67. HPDF_UINT key_len;
  68. /* owner-password (not encrypted) */
  69. HPDF_BYTE owner_passwd[HPDF_PASSWD_LEN];
  70. /* user-password (not encrypted) */
  71. HPDF_BYTE user_passwd[HPDF_PASSWD_LEN];
  72. /* owner-password (encrypted) */
  73. HPDF_BYTE owner_key[HPDF_PASSWD_LEN];
  74. /* user-password (encrypted) */
  75. HPDF_BYTE user_key[HPDF_PASSWD_LEN];
  76. HPDF_INT permission;
  77. HPDF_BYTE encrypt_id[HPDF_ID_LEN];
  78. HPDF_BYTE encryption_key[HPDF_MD5_KEY_LEN + 5];
  79. HPDF_BYTE md5_encryption_key[HPDF_MD5_KEY_LEN];
  80. HPDF_ARC4_Ctx_Rec arc4ctx;
  81. } HPDF_Encrypt_Rec;
  82. void
  83. HPDF_MD5Init (struct HPDF_MD5Context *ctx);
  84. void
  85. HPDF_MD5Update (struct HPDF_MD5Context *ctx,
  86. const HPDF_BYTE *buf,
  87. HPDF_UINT32 len);
  88. void
  89. HPDF_MD5Final (HPDF_BYTE digest[16],
  90. struct HPDF_MD5Context *ctx);
  91. void
  92. HPDF_PadOrTrancatePasswd (const char *pwd,
  93. HPDF_BYTE *new_pwd);
  94. void
  95. HPDF_Encrypt_Init (HPDF_Encrypt attr);
  96. void
  97. HPDF_Encrypt_CreateUserKey (HPDF_Encrypt attr);
  98. void
  99. HPDF_Encrypt_CreateOwnerKey (HPDF_Encrypt attr);
  100. void
  101. HPDF_Encrypt_CreateEncryptionKey (HPDF_Encrypt attr);
  102. void
  103. HPDF_Encrypt_InitKey (HPDF_Encrypt attr,
  104. HPDF_UINT32 object_id,
  105. HPDF_UINT16 gen_no);
  106. void
  107. HPDF_Encrypt_Reset (HPDF_Encrypt attr);
  108. void
  109. HPDF_Encrypt_CryptBuf (HPDF_Encrypt attr,
  110. const HPDF_BYTE *src,
  111. HPDF_BYTE *dst,
  112. HPDF_UINT len);
  113. #ifdef __cplusplus
  114. }
  115. #endif /* __cplusplus */
  116. #endif /* _HPDF_ENCRYPT_H */