1 /* 2 * << Haru Free PDF Library >> -- hpdf_encrypt.h 3 * 4 * URL: http://libharu.org 5 * 6 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp> 7 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org> 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 39 #ifndef HPDF_ENCRYPT_H 40 #define HPDF_ENCRYPT_H 41 42 #include "hpdf_mmgr.h" 43 44 #ifdef __cplusplus 45 extern "C" { 46 #endif 47 48 /*----------------------------------------------------------------------------*/ 49 /*----- encrypt-dict ---------------------------------------------------------*/ 50 51 #define HPDF_ID_LEN 16 52 #define HPDF_PASSWD_LEN 32 53 #define HPDF_ENCRYPT_KEY_MAX 16 54 #define HPDF_MD5_KEY_LEN 16 55 #define HPDF_PERMISSION_PAD 0xFFFFFFC0 56 #define HPDF_ARC4_BUF_SIZE 256 57 58 59 typedef struct HPDF_MD5Context 60 { 61 HPDF_UINT32 buf[4]; 62 HPDF_UINT32 bits[2]; 63 HPDF_BYTE in[64]; 64 } HPDF_MD5_CTX; 65 66 67 typedef struct _HPDF_ARC4_Ctx_Rec { 68 HPDF_BYTE idx1; 69 HPDF_BYTE idx2; 70 HPDF_BYTE state[HPDF_ARC4_BUF_SIZE]; 71 } HPDF_ARC4_Ctx_Rec; 72 73 74 typedef struct _HPDF_Encrypt_Rec *HPDF_Encrypt; 75 76 typedef struct _HPDF_Encrypt_Rec { 77 HPDF_EncryptMode mode; 78 79 /* key_len must be a multiple of 8, and between 40 to 128 */ 80 HPDF_UINT key_len; 81 82 /* owner-password (not encrypted) */ 83 HPDF_BYTE owner_passwd[HPDF_PASSWD_LEN]; 84 85 /* user-password (not encrypted) */ 86 HPDF_BYTE user_passwd[HPDF_PASSWD_LEN]; 87 88 /* owner-password (encrypted) */ 89 HPDF_BYTE owner_key[HPDF_PASSWD_LEN]; 90 91 /* user-password (encrypted) */ 92 HPDF_BYTE user_key[HPDF_PASSWD_LEN]; 93 94 HPDF_INT permission; 95 HPDF_BYTE encrypt_id[HPDF_ID_LEN]; 96 HPDF_BYTE encryption_key[HPDF_MD5_KEY_LEN + 5]; 97 HPDF_BYTE md5_encryption_key[HPDF_MD5_KEY_LEN]; 98 HPDF_ARC4_Ctx_Rec arc4ctx; 99 } HPDF_Encrypt_Rec; 100 101 102 void 103 HPDF_MD5Init (struct HPDF_MD5Context *ctx); 104 105 106 void 107 HPDF_MD5Update (struct HPDF_MD5Context *ctx, 108 const HPDF_BYTE *buf, 109 HPDF_UINT32 len); 110 111 112 void 113 HPDF_MD5Final (HPDF_BYTE digest[16], 114 struct HPDF_MD5Context *ctx); 115 116 void 117 HPDF_PadOrTrancatePasswd (const char *pwd, 118 HPDF_BYTE *new_pwd); 119 120 121 void 122 HPDF_Encrypt_Init (HPDF_Encrypt attr); 123 124 125 void 126 HPDF_Encrypt_CreateUserKey (HPDF_Encrypt attr); 127 128 129 void 130 HPDF_Encrypt_CreateOwnerKey (HPDF_Encrypt attr); 131 132 133 void 134 HPDF_Encrypt_CreateEncryptionKey (HPDF_Encrypt attr); 135 136 137 void 138 HPDF_Encrypt_InitKey (HPDF_Encrypt attr, 139 HPDF_UINT32 object_id, 140 HPDF_UINT16 gen_no); 141 142 143 void 144 HPDF_Encrypt_Reset (HPDF_Encrypt attr); 145 146 147 void 148 HPDF_Encrypt_CryptBuf (HPDF_Encrypt attr, 149 const HPDF_BYTE *src, 150 HPDF_BYTE *dst, 151 HPDF_UINT len); 152 153 #ifdef __cplusplus 154 } 155 #endif /* __cplusplus */ 156 157 #endif /* _HPDF_ENCRYPT_H */ 158 159 160