comparison mupdf-source/include/mupdf/fitz/crypt.h @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright (C) 2004-2025 Artifex Software, Inc.
2 //
3 // This file is part of MuPDF.
4 //
5 // MuPDF is free software: you can redistribute it and/or modify it under the
6 // terms of the GNU Affero General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or (at your option)
8 // any later version.
9 //
10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17 //
18 // Alternative licensing terms are available from the licensor.
19 // For commercial licensing, see <https://www.artifex.com/> or contact
20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21 // CA 94129, USA, for further information.
22
23 #ifndef MUPDF_FITZ_CRYPT_H
24 #define MUPDF_FITZ_CRYPT_H
25
26 #include "mupdf/fitz/system.h"
27
28 /* md5 digests */
29
30 /**
31 Structure definition is public to enable stack
32 based allocation. Do not access the members directly.
33 */
34 typedef struct
35 {
36 uint32_t lo, hi;
37 uint32_t a, b, c, d;
38 unsigned char buffer[64];
39 } fz_md5;
40
41 /**
42 MD5 initialization. Begins an MD5 operation, writing a new
43 context.
44
45 Never throws an exception.
46 */
47 void fz_md5_init(fz_md5 *state);
48
49 /**
50 MD5 block update operation. Continues an MD5 message-digest
51 operation, processing another message block, and updating the
52 context.
53
54 Never throws an exception.
55 */
56 void fz_md5_update(fz_md5 *state, const unsigned char *input, size_t inlen);
57
58 /**
59 MD5 block update operation. Continues an MD5 message-digest
60 operation, processing an int64, and updating the context.
61
62 Never throws an exception.
63 */
64 void fz_md5_update_int64(fz_md5 *state, int64_t i);
65
66 /**
67 MD5 finalization. Ends an MD5 message-digest operation, writing
68 the message digest and zeroizing the context.
69
70 Never throws an exception.
71 */
72 void fz_md5_final(fz_md5 *state, unsigned char digest[16]);
73
74 /* sha-256 digests */
75
76 /**
77 Structure definition is public to enable stack
78 based allocation. Do not access the members directly.
79 */
80 typedef struct
81 {
82 unsigned int state[8];
83 unsigned int count[2];
84 union {
85 unsigned char u8[64];
86 unsigned int u32[16];
87 } buffer;
88 } fz_sha256;
89
90 /**
91 SHA256 initialization. Begins an SHA256 operation, initialising
92 the supplied context.
93
94 Never throws an exception.
95 */
96 void fz_sha256_init(fz_sha256 *state);
97
98 /**
99 SHA256 block update operation. Continues an SHA256 message-
100 digest operation, processing another message block, and updating
101 the context.
102
103 Never throws an exception.
104 */
105 void fz_sha256_update(fz_sha256 *state, const unsigned char *input, size_t inlen);
106
107 /**
108 MD5 finalization. Ends an MD5 message-digest operation, writing
109 the message digest and zeroizing the context.
110
111 Never throws an exception.
112 */
113 void fz_sha256_final(fz_sha256 *state, unsigned char digest[32]);
114
115 /* sha-512 digests */
116
117 /**
118 Structure definition is public to enable stack
119 based allocation. Do not access the members directly.
120 */
121 typedef struct
122 {
123 uint64_t state[8];
124 unsigned int count[2];
125 union {
126 unsigned char u8[128];
127 uint64_t u64[16];
128 } buffer;
129 } fz_sha512;
130
131 /**
132 SHA512 initialization. Begins an SHA512 operation, initialising
133 the supplied context.
134
135 Never throws an exception.
136 */
137 void fz_sha512_init(fz_sha512 *state);
138
139 /**
140 SHA512 block update operation. Continues an SHA512 message-
141 digest operation, processing another message block, and updating
142 the context.
143
144 Never throws an exception.
145 */
146 void fz_sha512_update(fz_sha512 *state, const unsigned char *input, size_t inlen);
147
148 /**
149 SHA512 finalization. Ends an SHA512 message-digest operation,
150 writing the message digest and zeroizing the context.
151
152 Never throws an exception.
153 */
154 void fz_sha512_final(fz_sha512 *state, unsigned char digest[64]);
155
156 /* sha-384 digests */
157
158 typedef fz_sha512 fz_sha384;
159
160 /**
161 SHA384 initialization. Begins an SHA384 operation, initialising
162 the supplied context.
163
164 Never throws an exception.
165 */
166 void fz_sha384_init(fz_sha384 *state);
167
168 /**
169 SHA384 block update operation. Continues an SHA384 message-
170 digest operation, processing another message block, and updating
171 the context.
172
173 Never throws an exception.
174 */
175 void fz_sha384_update(fz_sha384 *state, const unsigned char *input, size_t inlen);
176
177 /**
178 SHA384 finalization. Ends an SHA384 message-digest operation,
179 writing the message digest and zeroizing the context.
180
181 Never throws an exception.
182 */
183 void fz_sha384_final(fz_sha384 *state, unsigned char digest[64]);
184
185 /* arc4 crypto */
186
187 /**
188 Structure definition is public to enable stack
189 based allocation. Do not access the members directly.
190 */
191 typedef struct
192 {
193 unsigned x;
194 unsigned y;
195 unsigned char state[256];
196 } fz_arc4;
197
198 /**
199 RC4 initialization. Begins an RC4 operation, writing a new
200 context.
201
202 Never throws an exception.
203 */
204 void fz_arc4_init(fz_arc4 *state, const unsigned char *key, size_t len);
205
206 /**
207 RC4 block encrypt operation; encrypt src into dst (both of
208 length len) updating the RC4 state as we go.
209
210 Never throws an exception.
211 */
212 void fz_arc4_encrypt(fz_arc4 *state, unsigned char *dest, const unsigned char *src, size_t len);
213
214 /**
215 RC4 finalization. Zero the context.
216
217 Never throws an exception.
218 */
219 void fz_arc4_final(fz_arc4 *state);
220
221 /* AES block cipher implementation from XYSSL */
222
223 /**
224 Structure definitions are public to enable stack
225 based allocation. Do not access the members directly.
226 */
227 typedef struct
228 {
229 int nr; /* number of rounds */
230 uint32_t *rk; /* AES round keys */
231 uint32_t buf[68]; /* unaligned data */
232 } fz_aes;
233
234 #define FZ_AES_DECRYPT 0
235 #define FZ_AES_ENCRYPT 1
236
237 /**
238 AES encryption initialization. Fills in the supplied context
239 and prepares for encryption using the given key.
240
241 Returns non-zero for error (key size other than 128/192/256).
242
243 Never throws an exception.
244 */
245 int fz_aes_setkey_enc(fz_aes *ctx, const unsigned char *key, int keysize);
246
247 /**
248 AES decryption initialization. Fills in the supplied context
249 and prepares for decryption using the given key.
250
251 Returns non-zero for error (key size other than 128/192/256).
252
253 Never throws an exception.
254 */
255 int fz_aes_setkey_dec(fz_aes *ctx, const unsigned char *key, int keysize);
256
257 /**
258 AES block processing. Encrypts or Decrypts (according to mode,
259 which must match what was initially set up) length bytes (which
260 must be a multiple of 16), using (and modifying) the insertion
261 vector iv, reading from input, and writing to output.
262
263 Never throws an exception.
264 */
265 void fz_aes_crypt_cbc(fz_aes *ctx, int mode, size_t length,
266 unsigned char iv[16],
267 const unsigned char *input,
268 unsigned char *output );
269
270 #endif