hpdf 2.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History +57 -0
- data/License +25 -0
- data/README +165 -0
- data/examples/arc_demo.rb +82 -0
- data/examples/demo.rb +92 -0
- data/examples/encryption.rb +41 -0
- data/examples/ext_gstater_demo.rb +171 -0
- data/examples/font_demo.rb +67 -0
- data/examples/jpfont_demo.rb +122 -0
- data/examples/line_demo.rb +301 -0
- data/examples/slide_show_demo.rb +139 -0
- data/examples/text_demo2.rb +189 -0
- data/examples/ttfont_demo.rb +106 -0
- data/ext/extconf.rb +6 -0
- data/ext/hpdf.c +3779 -0
- data/ext/hpdf_annotation.c +415 -0
- data/ext/hpdf_array.c +344 -0
- data/ext/hpdf_binary.c +117 -0
- data/ext/hpdf_boolean.c +47 -0
- data/ext/hpdf_catalog.c +354 -0
- data/ext/hpdf_destination.c +339 -0
- data/ext/hpdf_dict.c +488 -0
- data/ext/hpdf_doc.c +2020 -0
- data/ext/hpdf_doc_png.c +137 -0
- data/ext/hpdf_encoder.c +2991 -0
- data/ext/hpdf_encoder_cns.c +36359 -0
- data/ext/hpdf_encoder_cnt.c +15307 -0
- data/ext/hpdf_encoder_jp.c +16125 -0
- data/ext/hpdf_encoder_kr.c +27978 -0
- data/ext/hpdf_encrypt.c +632 -0
- data/ext/hpdf_encryptdict.c +240 -0
- data/ext/hpdf_error.c +114 -0
- data/ext/hpdf_ext_gstate.c +150 -0
- data/ext/hpdf_font.c +229 -0
- data/ext/hpdf_font_cid.c +1030 -0
- data/ext/hpdf_font_tt.c +406 -0
- data/ext/hpdf_font_type1.c +391 -0
- data/ext/hpdf_fontdef.c +56 -0
- data/ext/hpdf_fontdef_base14.c +4506 -0
- data/ext/hpdf_fontdef_cid.c +194 -0
- data/ext/hpdf_fontdef_cns.c +471 -0
- data/ext/hpdf_fontdef_cnt.c +250 -0
- data/ext/hpdf_fontdef_jp.c +1904 -0
- data/ext/hpdf_fontdef_kr.c +1572 -0
- data/ext/hpdf_fontdef_tt.c +2230 -0
- data/ext/hpdf_fontdef_type1.c +524 -0
- data/ext/hpdf_gstate.c +119 -0
- data/ext/hpdf_image.c +568 -0
- data/ext/hpdf_image_png.c +424 -0
- data/ext/hpdf_info.c +164 -0
- data/ext/hpdf_list.c +333 -0
- data/ext/hpdf_mmgr.c +243 -0
- data/ext/hpdf_name.c +71 -0
- data/ext/hpdf_null.c +31 -0
- data/ext/hpdf_number.c +49 -0
- data/ext/hpdf_objects.c +172 -0
- data/ext/hpdf_outline.c +329 -0
- data/ext/hpdf_page_label.c +74 -0
- data/ext/hpdf_page_operator.c +2764 -0
- data/ext/hpdf_pages.c +1508 -0
- data/ext/hpdf_real.c +61 -0
- data/ext/hpdf_streams.c +1435 -0
- data/ext/hpdf_string.c +189 -0
- data/ext/hpdf_utils.c +438 -0
- data/ext/hpdf_xref.c +348 -0
- data/ext/include/hpdf.h +1227 -0
- data/ext/include/hpdf_annotation.h +74 -0
- data/ext/include/hpdf_catalog.h +81 -0
- data/ext/include/hpdf_conf.h +76 -0
- data/ext/include/hpdf_consts.h +549 -0
- data/ext/include/hpdf_destination.h +41 -0
- data/ext/include/hpdf_doc.h +159 -0
- data/ext/include/hpdf_encoder.h +311 -0
- data/ext/include/hpdf_encrypt.h +156 -0
- data/ext/include/hpdf_encryptdict.h +66 -0
- data/ext/include/hpdf_error.h +201 -0
- data/ext/include/hpdf_ext_gstate.h +38 -0
- data/ext/include/hpdf_font.h +112 -0
- data/ext/include/hpdf_fontdef.h +403 -0
- data/ext/include/hpdf_gstate.h +80 -0
- data/ext/include/hpdf_image.h +72 -0
- data/ext/include/hpdf_info.h +48 -0
- data/ext/include/hpdf_list.h +85 -0
- data/ext/include/hpdf_mmgr.h +82 -0
- data/ext/include/hpdf_objects.h +587 -0
- data/ext/include/hpdf_outline.h +74 -0
- data/ext/include/hpdf_page_label.h +35 -0
- data/ext/include/hpdf_pages.h +128 -0
- data/ext/include/hpdf_streams.h +276 -0
- data/ext/include/hpdf_types.h +488 -0
- data/ext/include/hpdf_utils.h +161 -0
- data/tests/arc_demo.rb +82 -0
- data/tests/demo.rb +91 -0
- data/tests/encryption.rb +41 -0
- data/tests/ext_gstater_demo.rb +171 -0
- data/tests/font_demo.rb +67 -0
- data/tests/line_demo.rb +301 -0
- data/tests/slide_show_demo.rb +139 -0
- data/tests/test_all_examples.rb +31 -0
- data/tests/text_demo2.rb +189 -0
- metadata +147 -0
data/ext/hpdf_encrypt.c
ADDED
@@ -0,0 +1,632 @@
|
|
1
|
+
/*
|
2
|
+
* << Haru Free PDF Library 2.0.0 >> -- hpdf_encryor.c
|
3
|
+
*
|
4
|
+
* Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
|
5
|
+
*
|
6
|
+
* Permission to use, copy, modify, distribute and sell this software
|
7
|
+
* and its documentation for any purpose is hereby granted without fee,
|
8
|
+
* provided that the above copyright notice appear in all copies and
|
9
|
+
* that both that copyright notice and this permission notice appear
|
10
|
+
* in supporting documentation.
|
11
|
+
* It is provided "as is" without express or implied warranty.
|
12
|
+
*
|
13
|
+
*------------------------------------------------------------------------------
|
14
|
+
*
|
15
|
+
* The code implements MD5 message-digest algorithm is based on the code
|
16
|
+
* written by Colin Plumb.
|
17
|
+
* The copyright of it is as follows.
|
18
|
+
*
|
19
|
+
* This code implements the MD5 message-digest algorithm.
|
20
|
+
* The algorithm is due to Ron Rivest. This code was
|
21
|
+
* written by Colin Plumb in 1993, no copyright is claimed.
|
22
|
+
* This code is in the public domain; do with it what you wish.
|
23
|
+
*
|
24
|
+
* Equivalent code is available from RSA Data Security, Inc.
|
25
|
+
* This code has been tested against that, and is equivalent,
|
26
|
+
* except that you don't need to include two pages of legalese
|
27
|
+
* with every copy.
|
28
|
+
*
|
29
|
+
* To compute the message digest of a chunk of bytes, declare an
|
30
|
+
* MD5Context structure, pass it to MD5Init, call MD5Update as
|
31
|
+
* needed on buffers full of bytes, and then call MD5Final, which
|
32
|
+
* will fill a supplied 16-byte array with the digest.
|
33
|
+
*
|
34
|
+
*---------------------------------------------------------------------------*/
|
35
|
+
|
36
|
+
#include "hpdf_conf.h"
|
37
|
+
#include "hpdf_consts.h"
|
38
|
+
#include "hpdf_utils.h"
|
39
|
+
#include "hpdf_encrypt.h"
|
40
|
+
|
41
|
+
static const HPDF_BYTE HPDF_PADDING_STRING[] = {
|
42
|
+
0x28, 0xBF, 0x4E, 0x5E, 0x4E, 0x75, 0x8A, 0x41,
|
43
|
+
0x64, 0x00, 0x4E, 0x56, 0xFF, 0xFA, 0x01, 0x08,
|
44
|
+
0x2E, 0x2E, 0x00, 0xB6, 0xD0, 0x68, 0x3E, 0x80,
|
45
|
+
0x2F, 0x0C, 0xA9, 0xFE, 0x64, 0x53, 0x69, 0x7A
|
46
|
+
};
|
47
|
+
|
48
|
+
|
49
|
+
/*---------------------------------------------------------------------------*/
|
50
|
+
/*------ MD5 message-digest algorithm ---------------------------------------*/
|
51
|
+
|
52
|
+
static void
|
53
|
+
MD5Transform (HPDF_UINT32 buf[4],
|
54
|
+
const HPDF_UINT32 in[16]);
|
55
|
+
|
56
|
+
|
57
|
+
static void
|
58
|
+
MD5ByteReverse (HPDF_BYTE *buf,
|
59
|
+
HPDF_UINT32 longs);
|
60
|
+
|
61
|
+
|
62
|
+
void
|
63
|
+
HPDF_MD5Init (struct HPDF_MD5Context *ctx)
|
64
|
+
{
|
65
|
+
ctx->buf[0] = 0x67452301;
|
66
|
+
ctx->buf[1] = 0xefcdab89;
|
67
|
+
ctx->buf[2] = 0x98badcfe;
|
68
|
+
ctx->buf[3] = 0x10325476;
|
69
|
+
|
70
|
+
ctx->bits[0] = 0;
|
71
|
+
ctx->bits[1] = 0;
|
72
|
+
}
|
73
|
+
|
74
|
+
|
75
|
+
void
|
76
|
+
HPDF_MD5Update (struct HPDF_MD5Context *ctx,
|
77
|
+
const HPDF_BYTE *buf,
|
78
|
+
HPDF_UINT32 len)
|
79
|
+
{
|
80
|
+
HPDF_UINT32 t;
|
81
|
+
|
82
|
+
/* Update bitcount */
|
83
|
+
|
84
|
+
t = ctx->bits[0];
|
85
|
+
if ((ctx->bits[0] = t + ((HPDF_UINT32) len << 3)) < t)
|
86
|
+
ctx->bits[1]++; /* Carry from low to high */
|
87
|
+
ctx->bits[1] += len >> 29;
|
88
|
+
|
89
|
+
t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
|
90
|
+
|
91
|
+
/* Handle any leading odd-sized chunks */
|
92
|
+
|
93
|
+
if (t) {
|
94
|
+
HPDF_BYTE *p = (HPDF_BYTE *) ctx->in + t;
|
95
|
+
|
96
|
+
t = 64 - t;
|
97
|
+
if (len < t)
|
98
|
+
{
|
99
|
+
HPDF_MemCpy (p, buf, len);
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
HPDF_MemCpy (p, buf, t);
|
103
|
+
MD5ByteReverse (ctx->in, 16);
|
104
|
+
MD5Transform (ctx->buf, (HPDF_UINT32 *) ctx->in);
|
105
|
+
buf += t;
|
106
|
+
len -= t;
|
107
|
+
}
|
108
|
+
/* Process data in 64-byte chunks */
|
109
|
+
|
110
|
+
while (len >= 64) {
|
111
|
+
HPDF_MemCpy (ctx->in, buf, 64);
|
112
|
+
MD5ByteReverse (ctx->in, 16);
|
113
|
+
MD5Transform (ctx->buf, (HPDF_UINT32 *) ctx->in);
|
114
|
+
buf += 64;
|
115
|
+
len -= 64;
|
116
|
+
}
|
117
|
+
|
118
|
+
/* Handle any remaining bytes of data. */
|
119
|
+
|
120
|
+
HPDF_MemCpy (ctx->in, buf, len);
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
/*
|
125
|
+
* Final wrapup - pad to 64-byte boundary with the bit pattern
|
126
|
+
* 1 0* (64-bit count of bits processed, MSB-first)
|
127
|
+
*/
|
128
|
+
void
|
129
|
+
HPDF_MD5Final (HPDF_BYTE digest[16],
|
130
|
+
struct HPDF_MD5Context *ctx)
|
131
|
+
{
|
132
|
+
HPDF_UINT32 count;
|
133
|
+
HPDF_BYTE *p;
|
134
|
+
|
135
|
+
/* Compute number of bytes mod 64 */
|
136
|
+
count = (ctx->bits[0] >> 3) & 0x3F;
|
137
|
+
|
138
|
+
/* Set the first char of padding to 0x80. This is safe since there is
|
139
|
+
always at least one byte free */
|
140
|
+
p = ctx->in + count;
|
141
|
+
*p++ = 0x80;
|
142
|
+
|
143
|
+
/* Bytes of padding needed to make 64 bytes */
|
144
|
+
count = 64 - 1 - count;
|
145
|
+
|
146
|
+
/* Pad out to 56 mod 64 */
|
147
|
+
if (count < 8) {
|
148
|
+
/* Two lots of padding: Pad the first block to 64 bytes */
|
149
|
+
HPDF_MemSet (p, 0, count);
|
150
|
+
MD5ByteReverse (ctx->in, 16);
|
151
|
+
MD5Transform (ctx->buf, (HPDF_UINT32 *) ctx->in);
|
152
|
+
|
153
|
+
/* Now fill the next block with 56 bytes */
|
154
|
+
HPDF_MemSet (ctx->in, 0, 56);
|
155
|
+
} else {
|
156
|
+
/* Pad block to 56 bytes */
|
157
|
+
HPDF_MemSet (p, 0, count - 8);
|
158
|
+
}
|
159
|
+
MD5ByteReverse (ctx->in, 14);
|
160
|
+
|
161
|
+
/* Append length in bits and transform */
|
162
|
+
((HPDF_UINT32 *) ctx->in)[14] = ctx->bits[0];
|
163
|
+
((HPDF_UINT32 *) ctx->in)[15] = ctx->bits[1];
|
164
|
+
|
165
|
+
MD5Transform (ctx->buf, (HPDF_UINT32 *) ctx->in);
|
166
|
+
MD5ByteReverse ((HPDF_BYTE *) ctx->buf, 4);
|
167
|
+
HPDF_MemCpy ((HPDF_BYTE *)digest, (HPDF_BYTE *)ctx->buf, 16);
|
168
|
+
HPDF_MemSet ((HPDF_BYTE *)ctx, 0, sizeof (ctx)); /* In case it's sensitive */
|
169
|
+
}
|
170
|
+
|
171
|
+
/* The four core functions - F1 is optimized somewhat */
|
172
|
+
|
173
|
+
/* #define F1(x, y, z) (x & y | ~x & z) */
|
174
|
+
#define F1(x, y, z) (z ^ (x & (y ^ z)))
|
175
|
+
#define F2(x, y, z) F1(z, x, y)
|
176
|
+
#define F3(x, y, z) (x ^ y ^ z)
|
177
|
+
#define F4(x, y, z) (y ^ (x | ~z))
|
178
|
+
|
179
|
+
/* This is the central step in the HPDF_MD5 algorithm. */
|
180
|
+
#define HPDF_MD5STEP(f, w, x, y, z, data, s) \
|
181
|
+
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
|
182
|
+
|
183
|
+
|
184
|
+
/*
|
185
|
+
* The core of the MD5 algorithm, this alters an existing MD5 hash to
|
186
|
+
* reflect the addition of 16 longwords of new data. MD5Update blocks
|
187
|
+
* the data and converts bytes into longwords for this routine.
|
188
|
+
*/
|
189
|
+
static void
|
190
|
+
MD5Transform (HPDF_UINT32 buf[4],
|
191
|
+
const HPDF_UINT32 in[16])
|
192
|
+
{
|
193
|
+
register HPDF_UINT32 a, b, c, d;
|
194
|
+
|
195
|
+
a = buf[0];
|
196
|
+
b = buf[1];
|
197
|
+
c = buf[2];
|
198
|
+
d = buf[3];
|
199
|
+
|
200
|
+
HPDF_MD5STEP (F1, a, b, c, d, in[0] + 0xd76aa478, 7);
|
201
|
+
HPDF_MD5STEP (F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
|
202
|
+
HPDF_MD5STEP (F1, c, d, a, b, in[2] + 0x242070db, 17);
|
203
|
+
HPDF_MD5STEP (F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
|
204
|
+
HPDF_MD5STEP (F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
|
205
|
+
HPDF_MD5STEP (F1, d, a, b, c, in[5] + 0x4787c62a, 12);
|
206
|
+
HPDF_MD5STEP (F1, c, d, a, b, in[6] + 0xa8304613, 17);
|
207
|
+
HPDF_MD5STEP (F1, b, c, d, a, in[7] + 0xfd469501, 22);
|
208
|
+
HPDF_MD5STEP (F1, a, b, c, d, in[8] + 0x698098d8, 7);
|
209
|
+
HPDF_MD5STEP (F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
|
210
|
+
HPDF_MD5STEP (F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
|
211
|
+
HPDF_MD5STEP (F1, b, c, d, a, in[11] + 0x895cd7be, 22);
|
212
|
+
HPDF_MD5STEP (F1, a, b, c, d, in[12] + 0x6b901122, 7);
|
213
|
+
HPDF_MD5STEP (F1, d, a, b, c, in[13] + 0xfd987193, 12);
|
214
|
+
HPDF_MD5STEP (F1, c, d, a, b, in[14] + 0xa679438e, 17);
|
215
|
+
HPDF_MD5STEP (F1, b, c, d, a, in[15] + 0x49b40821, 22);
|
216
|
+
|
217
|
+
HPDF_MD5STEP (F2, a, b, c, d, in[1] + 0xf61e2562, 5);
|
218
|
+
HPDF_MD5STEP (F2, d, a, b, c, in[6] + 0xc040b340, 9);
|
219
|
+
HPDF_MD5STEP (F2, c, d, a, b, in[11] + 0x265e5a51, 14);
|
220
|
+
HPDF_MD5STEP (F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
|
221
|
+
HPDF_MD5STEP (F2, a, b, c, d, in[5] + 0xd62f105d, 5);
|
222
|
+
HPDF_MD5STEP (F2, d, a, b, c, in[10] + 0x02441453, 9);
|
223
|
+
HPDF_MD5STEP (F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
|
224
|
+
HPDF_MD5STEP (F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
|
225
|
+
HPDF_MD5STEP (F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
|
226
|
+
HPDF_MD5STEP (F2, d, a, b, c, in[14] + 0xc33707d6, 9);
|
227
|
+
HPDF_MD5STEP (F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
|
228
|
+
HPDF_MD5STEP (F2, b, c, d, a, in[8] + 0x455a14ed, 20);
|
229
|
+
HPDF_MD5STEP (F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
|
230
|
+
HPDF_MD5STEP (F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
|
231
|
+
HPDF_MD5STEP (F2, c, d, a, b, in[7] + 0x676f02d9, 14);
|
232
|
+
HPDF_MD5STEP (F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
|
233
|
+
|
234
|
+
HPDF_MD5STEP (F3, a, b, c, d, in[5] + 0xfffa3942, 4);
|
235
|
+
HPDF_MD5STEP (F3, d, a, b, c, in[8] + 0x8771f681, 11);
|
236
|
+
HPDF_MD5STEP (F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
|
237
|
+
HPDF_MD5STEP (F3, b, c, d, a, in[14] + 0xfde5380c, 23);
|
238
|
+
HPDF_MD5STEP (F3, a, b, c, d, in[1] + 0xa4beea44, 4);
|
239
|
+
HPDF_MD5STEP (F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
|
240
|
+
HPDF_MD5STEP (F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
|
241
|
+
HPDF_MD5STEP (F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
|
242
|
+
HPDF_MD5STEP (F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
|
243
|
+
HPDF_MD5STEP (F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
|
244
|
+
HPDF_MD5STEP (F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
|
245
|
+
HPDF_MD5STEP (F3, b, c, d, a, in[6] + 0x04881d05, 23);
|
246
|
+
HPDF_MD5STEP (F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
|
247
|
+
HPDF_MD5STEP (F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
|
248
|
+
HPDF_MD5STEP (F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
|
249
|
+
HPDF_MD5STEP (F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
|
250
|
+
|
251
|
+
HPDF_MD5STEP (F4, a, b, c, d, in[0] + 0xf4292244, 6);
|
252
|
+
HPDF_MD5STEP (F4, d, a, b, c, in[7] + 0x432aff97, 10);
|
253
|
+
HPDF_MD5STEP (F4, c, d, a, b, in[14] + 0xab9423a7, 15);
|
254
|
+
HPDF_MD5STEP (F4, b, c, d, a, in[5] + 0xfc93a039, 21);
|
255
|
+
HPDF_MD5STEP (F4, a, b, c, d, in[12] + 0x655b59c3, 6);
|
256
|
+
HPDF_MD5STEP (F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
|
257
|
+
HPDF_MD5STEP (F4, c, d, a, b, in[10] + 0xffeff47d, 15);
|
258
|
+
HPDF_MD5STEP (F4, b, c, d, a, in[1] + 0x85845dd1, 21);
|
259
|
+
HPDF_MD5STEP (F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
|
260
|
+
HPDF_MD5STEP (F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
|
261
|
+
HPDF_MD5STEP (F4, c, d, a, b, in[6] + 0xa3014314, 15);
|
262
|
+
HPDF_MD5STEP (F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
|
263
|
+
HPDF_MD5STEP (F4, a, b, c, d, in[4] + 0xf7537e82, 6);
|
264
|
+
HPDF_MD5STEP (F4, d, a, b, c, in[11] + 0xbd3af235, 10);
|
265
|
+
HPDF_MD5STEP (F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
|
266
|
+
HPDF_MD5STEP (F4, b, c, d, a, in[9] + 0xeb86d391, 21);
|
267
|
+
|
268
|
+
buf[0] += a;
|
269
|
+
buf[1] += b;
|
270
|
+
buf[2] += c;
|
271
|
+
buf[3] += d;
|
272
|
+
}
|
273
|
+
|
274
|
+
|
275
|
+
static void
|
276
|
+
MD5ByteReverse (HPDF_BYTE *buf,
|
277
|
+
HPDF_UINT32 longs)
|
278
|
+
{
|
279
|
+
HPDF_UINT32 t;
|
280
|
+
do
|
281
|
+
{
|
282
|
+
t = (HPDF_UINT32) ((HPDF_UINT32) buf[3] << 8 | buf[2]) << 16 |
|
283
|
+
((HPDF_UINT32) buf[1] << 8 | buf[0]);
|
284
|
+
*(HPDF_UINT32 *) buf = t;
|
285
|
+
buf += 4;
|
286
|
+
}
|
287
|
+
while (--longs);
|
288
|
+
}
|
289
|
+
|
290
|
+
/*----- encrypt-obj ---------------------------------------------------------*/
|
291
|
+
|
292
|
+
static void
|
293
|
+
ARC4Init (HPDF_ARC4_Ctx_Rec *ctx,
|
294
|
+
const HPDF_BYTE *key,
|
295
|
+
HPDF_UINT key_len);
|
296
|
+
|
297
|
+
|
298
|
+
static void
|
299
|
+
ARC4CryptBuf (HPDF_ARC4_Ctx_Rec *ctx,
|
300
|
+
const HPDF_BYTE *in,
|
301
|
+
HPDF_BYTE *out,
|
302
|
+
HPDF_UINT len);
|
303
|
+
|
304
|
+
|
305
|
+
/*---------------------------------------------------------------------------*/
|
306
|
+
|
307
|
+
void
|
308
|
+
HPDF_PadOrTrancatePasswd (const char *pwd,
|
309
|
+
HPDF_BYTE *new_pwd)
|
310
|
+
{
|
311
|
+
HPDF_UINT len = HPDF_StrLen (pwd, HPDF_PASSWD_LEN + 1);
|
312
|
+
|
313
|
+
HPDF_PTRACE((" HPDF_PadOrTrancatePasswd\n"));
|
314
|
+
|
315
|
+
HPDF_MemSet (new_pwd, 0x00, HPDF_PASSWD_LEN);
|
316
|
+
|
317
|
+
if (len >= HPDF_PASSWD_LEN)
|
318
|
+
HPDF_MemCpy (new_pwd, pwd, HPDF_PASSWD_LEN);
|
319
|
+
else {
|
320
|
+
if (len > 0)
|
321
|
+
HPDF_MemCpy (new_pwd, pwd, len);
|
322
|
+
HPDF_MemCpy (new_pwd + len, HPDF_PADDING_STRING,
|
323
|
+
HPDF_PASSWD_LEN - len);
|
324
|
+
}
|
325
|
+
}
|
326
|
+
|
327
|
+
|
328
|
+
void
|
329
|
+
HPDF_Encrypt_Init (HPDF_Encrypt attr)
|
330
|
+
{
|
331
|
+
HPDF_MemSet (attr, 0, sizeof(HPDF_Encrypt_Rec));
|
332
|
+
attr->mode = HPDF_ENCRYPT_R2;
|
333
|
+
attr->key_len = 5;
|
334
|
+
HPDF_MemCpy (attr->owner_passwd, HPDF_PADDING_STRING, HPDF_PASSWD_LEN);
|
335
|
+
HPDF_MemCpy (attr->user_passwd, HPDF_PADDING_STRING, HPDF_PASSWD_LEN);
|
336
|
+
attr->permission = HPDF_ENABLE_PRINT | HPDF_ENABLE_EDIT_ALL |
|
337
|
+
HPDF_ENABLE_COPY | HPDF_ENABLE_EDIT | HPDF_PERMISSION_PAD;
|
338
|
+
}
|
339
|
+
|
340
|
+
|
341
|
+
void
|
342
|
+
HPDF_Encrypt_CreateOwnerKey (HPDF_Encrypt attr)
|
343
|
+
{
|
344
|
+
HPDF_ARC4_Ctx_Rec rc4_ctx;
|
345
|
+
HPDF_MD5_CTX md5_ctx;
|
346
|
+
HPDF_BYTE digest[HPDF_MD5_KEY_LEN];
|
347
|
+
HPDF_BYTE tmppwd[HPDF_PASSWD_LEN];
|
348
|
+
|
349
|
+
HPDF_PTRACE((" HPDF_Encrypt_CreateOwnerKey\n"));
|
350
|
+
|
351
|
+
/* create md5-digest using the value of owner_passwd */
|
352
|
+
|
353
|
+
/* Algorithm 3.3 step 2 */
|
354
|
+
HPDF_MD5Init(&md5_ctx);
|
355
|
+
HPDF_MD5Update(&md5_ctx, attr->owner_passwd, HPDF_PASSWD_LEN);
|
356
|
+
|
357
|
+
HPDF_PTRACE(("@ Algorithm 3.3 step 2\n"));
|
358
|
+
|
359
|
+
HPDF_MD5Final(digest, &md5_ctx);
|
360
|
+
|
361
|
+
/* Algorithm 3.3 step 3 (Revision 3 only) */
|
362
|
+
if (attr->mode == HPDF_ENCRYPT_R3) {
|
363
|
+
HPDF_UINT i;
|
364
|
+
|
365
|
+
for (i = 0; i < 50; i++) {
|
366
|
+
HPDF_MD5Init(&md5_ctx);
|
367
|
+
|
368
|
+
//HPDF_MD5Update (&md5_ctx, digest, HPDF_MD5_KEY_LEN);
|
369
|
+
HPDF_MD5Update (&md5_ctx, digest, attr->key_len);
|
370
|
+
HPDF_MD5Final(digest, &md5_ctx);
|
371
|
+
|
372
|
+
HPDF_PTRACE(("@ Algorithm 3.3 step 3 loop %u\n", i));
|
373
|
+
}
|
374
|
+
}
|
375
|
+
|
376
|
+
/* Algorithm 3.3 step 4 */
|
377
|
+
HPDF_PTRACE(("@ Algorithm 3.3 step 7 loop 0\n"));
|
378
|
+
|
379
|
+
ARC4Init (&rc4_ctx, digest, attr->key_len);
|
380
|
+
|
381
|
+
HPDF_PTRACE(("@ Algorithm 3.3 step 5\n"));
|
382
|
+
|
383
|
+
/* Algorithm 3.3 step 6 */
|
384
|
+
HPDF_PTRACE(("@ Algorithm 3.3 step 6\n"));
|
385
|
+
ARC4CryptBuf (&rc4_ctx, attr->user_passwd, tmppwd, HPDF_PASSWD_LEN);
|
386
|
+
|
387
|
+
/* Algorithm 3.3 step 7 */
|
388
|
+
HPDF_PTRACE(("@ Algorithm 3.3 step 7\n"));
|
389
|
+
if (attr->mode == HPDF_ENCRYPT_R3) {
|
390
|
+
HPDF_BYTE tmppwd2[HPDF_PASSWD_LEN];
|
391
|
+
HPDF_UINT i;
|
392
|
+
|
393
|
+
for (i = 1; i <= 19; i++) {
|
394
|
+
HPDF_UINT j;
|
395
|
+
HPDF_BYTE new_key[HPDF_MD5_KEY_LEN];
|
396
|
+
|
397
|
+
for (j = 0; j < attr->key_len; j++)
|
398
|
+
new_key[j] = digest[j] ^ i;
|
399
|
+
|
400
|
+
HPDF_PTRACE(("@ Algorithm 3.3 step 7 loop %u\n", i));
|
401
|
+
|
402
|
+
HPDF_MemCpy (tmppwd2, tmppwd, HPDF_PASSWD_LEN);
|
403
|
+
ARC4Init(&rc4_ctx, new_key, attr->key_len);
|
404
|
+
ARC4CryptBuf(&rc4_ctx, tmppwd2, tmppwd, HPDF_PASSWD_LEN);
|
405
|
+
}
|
406
|
+
}
|
407
|
+
|
408
|
+
/* Algorithm 3.3 step 8 */
|
409
|
+
HPDF_PTRACE(("@ Algorithm 3.3 step 8\n"));
|
410
|
+
HPDF_MemCpy (attr->owner_key, tmppwd, HPDF_PASSWD_LEN);
|
411
|
+
}
|
412
|
+
|
413
|
+
|
414
|
+
void
|
415
|
+
HPDF_Encrypt_CreateEncryptionKey (HPDF_Encrypt attr)
|
416
|
+
{
|
417
|
+
HPDF_MD5_CTX md5_ctx;
|
418
|
+
HPDF_BYTE tmp_flg[4];
|
419
|
+
|
420
|
+
HPDF_PTRACE((" HPDF_Encrypt_CreateEncryptionKey\n"));
|
421
|
+
|
422
|
+
/* Algorithm3.2 step2 */
|
423
|
+
HPDF_MD5Init(&md5_ctx);
|
424
|
+
HPDF_MD5Update(&md5_ctx, attr->user_passwd, HPDF_PASSWD_LEN);
|
425
|
+
|
426
|
+
/* Algorithm3.2 step3 */
|
427
|
+
HPDF_MD5Update(&md5_ctx, attr->owner_key, HPDF_PASSWD_LEN);
|
428
|
+
|
429
|
+
|
430
|
+
/* Algorithm3.2 step4 */
|
431
|
+
HPDF_PTRACE(("@@@ permission =%d\n", attr->permission));
|
432
|
+
tmp_flg[0] = attr->permission;
|
433
|
+
tmp_flg[1] = (attr->permission >> 8);
|
434
|
+
tmp_flg[2] = (attr->permission >> 16);
|
435
|
+
tmp_flg[3] = (attr->permission >> 24);
|
436
|
+
|
437
|
+
HPDF_MD5Update(&md5_ctx, tmp_flg, 4);
|
438
|
+
|
439
|
+
/* Algorithm3.2 step5 */
|
440
|
+
HPDF_PTRACE(("@ Algorithm 3.2 step 5\n"));
|
441
|
+
|
442
|
+
HPDF_MD5Update(&md5_ctx, attr->encrypt_id, HPDF_ID_LEN);
|
443
|
+
HPDF_MD5Final(attr->encryption_key, &md5_ctx);
|
444
|
+
|
445
|
+
/* Algorithm 3.2 step6 (Revision 3 only) */
|
446
|
+
if (attr->mode == HPDF_ENCRYPT_R3) {
|
447
|
+
HPDF_UINT i;
|
448
|
+
|
449
|
+
for (i = 0; i < 50; i++) {
|
450
|
+
HPDF_PTRACE(("@ Algorithm 3.3 step 6 loop %u\n", i));
|
451
|
+
HPDF_MD5Init(&md5_ctx);
|
452
|
+
HPDF_MD5Update (&md5_ctx, attr->encryption_key, attr->key_len);
|
453
|
+
HPDF_MD5Final(attr->encryption_key, &md5_ctx);
|
454
|
+
}
|
455
|
+
}
|
456
|
+
}
|
457
|
+
|
458
|
+
|
459
|
+
void
|
460
|
+
HPDF_Encrypt_CreateUserKey (HPDF_Encrypt attr)
|
461
|
+
{
|
462
|
+
HPDF_ARC4_Ctx_Rec ctx;
|
463
|
+
|
464
|
+
HPDF_PTRACE((" HPDF_Encrypt_CreateUserKey\n"));
|
465
|
+
|
466
|
+
/* Algorithm 3.4/5 step1 */
|
467
|
+
|
468
|
+
/* Algorithm 3.4 step2 */
|
469
|
+
ARC4Init(&ctx, attr->encryption_key, attr->key_len);
|
470
|
+
ARC4CryptBuf(&ctx, HPDF_PADDING_STRING, attr->user_key, HPDF_PASSWD_LEN);
|
471
|
+
|
472
|
+
if (attr->mode == HPDF_ENCRYPT_R3) {
|
473
|
+
HPDF_MD5_CTX md5_ctx;
|
474
|
+
HPDF_BYTE digest[HPDF_MD5_KEY_LEN];
|
475
|
+
HPDF_BYTE digest2[HPDF_MD5_KEY_LEN];
|
476
|
+
HPDF_UINT i;
|
477
|
+
|
478
|
+
/* Algorithm 3.5 step2 (same as Algorithm3.2 step2) */
|
479
|
+
HPDF_MD5Init(&md5_ctx);
|
480
|
+
HPDF_MD5Update(&md5_ctx, HPDF_PADDING_STRING, HPDF_PASSWD_LEN);
|
481
|
+
|
482
|
+
/* Algorithm 3.5 step3 */
|
483
|
+
HPDF_MD5Update(&md5_ctx, attr->encrypt_id, HPDF_ID_LEN);
|
484
|
+
HPDF_MD5Final(digest, &md5_ctx);
|
485
|
+
|
486
|
+
HPDF_PTRACE(("@ Algorithm 3.5 step 3\n"));
|
487
|
+
|
488
|
+
/* Algorithm 3.5 step4 */
|
489
|
+
ARC4Init(&ctx, attr->encryption_key, attr->key_len);
|
490
|
+
ARC4CryptBuf(&ctx, digest, digest2, HPDF_MD5_KEY_LEN);
|
491
|
+
|
492
|
+
HPDF_PTRACE(("@ Algorithm 3.5 step 4\n"));
|
493
|
+
|
494
|
+
/* Algorithm 3.5 step5 */
|
495
|
+
for (i = 1; i <= 19; i++) {
|
496
|
+
HPDF_UINT j;
|
497
|
+
HPDF_BYTE new_key[HPDF_MD5_KEY_LEN];
|
498
|
+
|
499
|
+
HPDF_PTRACE(("@ Algorithm 3.5 step 5 loop %u\n", i));
|
500
|
+
|
501
|
+
for (j = 0; j < attr->key_len; j++)
|
502
|
+
new_key[j] = attr->encryption_key[j] ^ i;
|
503
|
+
|
504
|
+
HPDF_MemCpy (digest, digest2, HPDF_MD5_KEY_LEN);
|
505
|
+
|
506
|
+
ARC4Init(&ctx, new_key, attr->key_len);
|
507
|
+
ARC4CryptBuf(&ctx, digest, digest2, HPDF_MD5_KEY_LEN);
|
508
|
+
}
|
509
|
+
|
510
|
+
/* use the result of Algorithm 3.4 as 'arbitrary padding' */
|
511
|
+
HPDF_MemSet (attr->user_key, 0, HPDF_PASSWD_LEN);
|
512
|
+
HPDF_MemCpy (attr->user_key, digest2, HPDF_MD5_KEY_LEN);
|
513
|
+
}
|
514
|
+
}
|
515
|
+
|
516
|
+
|
517
|
+
void
|
518
|
+
ARC4Init (HPDF_ARC4_Ctx_Rec *ctx,
|
519
|
+
const HPDF_BYTE *key,
|
520
|
+
HPDF_UINT key_len)
|
521
|
+
{
|
522
|
+
HPDF_BYTE tmp_array[HPDF_ARC4_BUF_SIZE];
|
523
|
+
HPDF_UINT i;
|
524
|
+
HPDF_UINT j = 0;
|
525
|
+
|
526
|
+
HPDF_PTRACE((" ARC4Init\n"));
|
527
|
+
|
528
|
+
for (i = 0; i < HPDF_ARC4_BUF_SIZE; i++)
|
529
|
+
ctx->state[i] = i;
|
530
|
+
|
531
|
+
for (i = 0; i < HPDF_ARC4_BUF_SIZE; i++)
|
532
|
+
tmp_array[i] = key[i % key_len];
|
533
|
+
|
534
|
+
for (i = 0; i < HPDF_ARC4_BUF_SIZE; i++) {
|
535
|
+
HPDF_BYTE tmp;
|
536
|
+
|
537
|
+
j = (j + ctx->state[i] + tmp_array[i]) % HPDF_ARC4_BUF_SIZE;
|
538
|
+
|
539
|
+
tmp = ctx->state[i];
|
540
|
+
ctx->state[i] = ctx->state[j];
|
541
|
+
ctx->state[j] = tmp;
|
542
|
+
}
|
543
|
+
|
544
|
+
ctx->idx1 = 0;
|
545
|
+
ctx->idx2 = 0;
|
546
|
+
}
|
547
|
+
|
548
|
+
|
549
|
+
void
|
550
|
+
ARC4CryptBuf (HPDF_ARC4_Ctx_Rec *ctx,
|
551
|
+
const HPDF_BYTE *in,
|
552
|
+
HPDF_BYTE *out,
|
553
|
+
HPDF_UINT len)
|
554
|
+
{
|
555
|
+
HPDF_UINT i;
|
556
|
+
HPDF_UINT t;
|
557
|
+
HPDF_BYTE K;
|
558
|
+
|
559
|
+
HPDF_PTRACE((" ARC4CryptBuf\n"));
|
560
|
+
|
561
|
+
for (i = 0; i < len; i++) {
|
562
|
+
HPDF_BYTE tmp;
|
563
|
+
|
564
|
+
ctx->idx1 = (ctx->idx1 + 1) % 256;
|
565
|
+
ctx->idx2 = (ctx->idx2 + ctx->state[ctx->idx1]) % 256;
|
566
|
+
|
567
|
+
tmp = ctx->state[ctx->idx1];
|
568
|
+
ctx->state[ctx->idx1] = ctx->state[ctx->idx2];
|
569
|
+
ctx->state[ctx->idx2] = tmp;
|
570
|
+
|
571
|
+
t = (ctx->state[ctx->idx1] + ctx->state[ctx->idx2]) % 256;
|
572
|
+
K = ctx->state[t];
|
573
|
+
|
574
|
+
out[i] = in[i] ^ K;
|
575
|
+
}
|
576
|
+
}
|
577
|
+
|
578
|
+
|
579
|
+
void
|
580
|
+
HPDF_Encrypt_InitKey (HPDF_Encrypt attr,
|
581
|
+
HPDF_UINT32 object_id,
|
582
|
+
HPDF_UINT16 gen_no)
|
583
|
+
{
|
584
|
+
HPDF_MD5_CTX ctx;
|
585
|
+
HPDF_UINT key_len;
|
586
|
+
|
587
|
+
HPDF_PTRACE((" HPDF_Encrypt_Init\n"));
|
588
|
+
|
589
|
+
attr->encryption_key[attr->key_len] = object_id;
|
590
|
+
attr->encryption_key[attr->key_len + 1] = (object_id >> 8);
|
591
|
+
attr->encryption_key[attr->key_len + 2] = (object_id >> 16);
|
592
|
+
attr->encryption_key[attr->key_len + 3] = gen_no;
|
593
|
+
attr->encryption_key[attr->key_len + 4] = (gen_no >> 8);
|
594
|
+
|
595
|
+
HPDF_PTRACE(("@@@ OID=%u, gen_no=%u\n", (HPDF_INT)object_id, gen_no));
|
596
|
+
|
597
|
+
HPDF_MD5Init(&ctx);
|
598
|
+
HPDF_MD5Update(&ctx, attr->encryption_key, attr->key_len + 5);
|
599
|
+
HPDF_MD5Final(attr->md5_encryption_key, &ctx);
|
600
|
+
|
601
|
+
key_len = (attr->key_len + 5 > HPDF_ENCRYPT_KEY_MAX) ?
|
602
|
+
HPDF_ENCRYPT_KEY_MAX : attr->key_len + 5;
|
603
|
+
|
604
|
+
ARC4Init(&attr->arc4ctx, attr->md5_encryption_key, key_len);
|
605
|
+
}
|
606
|
+
|
607
|
+
|
608
|
+
void
|
609
|
+
HPDF_Encrypt_Reset (HPDF_Encrypt attr)
|
610
|
+
{
|
611
|
+
HPDF_UINT key_len = (attr->key_len + 5 > HPDF_ENCRYPT_KEY_MAX) ?
|
612
|
+
HPDF_ENCRYPT_KEY_MAX : attr->key_len + 5;
|
613
|
+
|
614
|
+
HPDF_PTRACE((" HPDF_Encrypt_Reset\n"));
|
615
|
+
|
616
|
+
ARC4Init(&attr->arc4ctx, attr->md5_encryption_key, key_len);
|
617
|
+
}
|
618
|
+
|
619
|
+
|
620
|
+
void
|
621
|
+
HPDF_Encrypt_CryptBuf (HPDF_Encrypt attr,
|
622
|
+
const HPDF_BYTE *src,
|
623
|
+
HPDF_BYTE *dst,
|
624
|
+
HPDF_UINT len)
|
625
|
+
{
|
626
|
+
ARC4CryptBuf(&attr->arc4ctx, src, dst, len);
|
627
|
+
}
|
628
|
+
|
629
|
+
|
630
|
+
/*--------------------------------------------------------------------------*/
|
631
|
+
/*--------------------------------------------------------------------------*/
|
632
|
+
|