crypt.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #ifndef MUPDF_FITZ_CRYPT_H
  23. #define MUPDF_FITZ_CRYPT_H
  24. #include "mupdf/fitz/system.h"
  25. /* md5 digests */
  26. /**
  27. Structure definition is public to enable stack
  28. based allocation. Do not access the members directly.
  29. */
  30. typedef struct
  31. {
  32. uint32_t lo, hi;
  33. uint32_t a, b, c, d;
  34. unsigned char buffer[64];
  35. } fz_md5;
  36. /**
  37. MD5 initialization. Begins an MD5 operation, writing a new
  38. context.
  39. Never throws an exception.
  40. */
  41. void fz_md5_init(fz_md5 *state);
  42. /**
  43. MD5 block update operation. Continues an MD5 message-digest
  44. operation, processing another message block, and updating the
  45. context.
  46. Never throws an exception.
  47. */
  48. void fz_md5_update(fz_md5 *state, const unsigned char *input, size_t inlen);
  49. /**
  50. MD5 block update operation. Continues an MD5 message-digest
  51. operation, processing an int64, and updating the context.
  52. Never throws an exception.
  53. */
  54. void fz_md5_update_int64(fz_md5 *state, int64_t i);
  55. /**
  56. MD5 finalization. Ends an MD5 message-digest operation, writing
  57. the message digest and zeroizing the context.
  58. Never throws an exception.
  59. */
  60. void fz_md5_final(fz_md5 *state, unsigned char digest[16]);
  61. /* sha-256 digests */
  62. /**
  63. Structure definition is public to enable stack
  64. based allocation. Do not access the members directly.
  65. */
  66. typedef struct
  67. {
  68. unsigned int state[8];
  69. unsigned int count[2];
  70. union {
  71. unsigned char u8[64];
  72. unsigned int u32[16];
  73. } buffer;
  74. } fz_sha256;
  75. /**
  76. SHA256 initialization. Begins an SHA256 operation, initialising
  77. the supplied context.
  78. Never throws an exception.
  79. */
  80. void fz_sha256_init(fz_sha256 *state);
  81. /**
  82. SHA256 block update operation. Continues an SHA256 message-
  83. digest operation, processing another message block, and updating
  84. the context.
  85. Never throws an exception.
  86. */
  87. void fz_sha256_update(fz_sha256 *state, const unsigned char *input, size_t inlen);
  88. /**
  89. MD5 finalization. Ends an MD5 message-digest operation, writing
  90. the message digest and zeroizing the context.
  91. Never throws an exception.
  92. */
  93. void fz_sha256_final(fz_sha256 *state, unsigned char digest[32]);
  94. /* sha-512 digests */
  95. /**
  96. Structure definition is public to enable stack
  97. based allocation. Do not access the members directly.
  98. */
  99. typedef struct
  100. {
  101. uint64_t state[8];
  102. unsigned int count[2];
  103. union {
  104. unsigned char u8[128];
  105. uint64_t u64[16];
  106. } buffer;
  107. } fz_sha512;
  108. /**
  109. SHA512 initialization. Begins an SHA512 operation, initialising
  110. the supplied context.
  111. Never throws an exception.
  112. */
  113. void fz_sha512_init(fz_sha512 *state);
  114. /**
  115. SHA512 block update operation. Continues an SHA512 message-
  116. digest operation, processing another message block, and updating
  117. the context.
  118. Never throws an exception.
  119. */
  120. void fz_sha512_update(fz_sha512 *state, const unsigned char *input, size_t inlen);
  121. /**
  122. SHA512 finalization. Ends an SHA512 message-digest operation,
  123. writing the message digest and zeroizing the context.
  124. Never throws an exception.
  125. */
  126. void fz_sha512_final(fz_sha512 *state, unsigned char digest[64]);
  127. /* sha-384 digests */
  128. typedef fz_sha512 fz_sha384;
  129. /**
  130. SHA384 initialization. Begins an SHA384 operation, initialising
  131. the supplied context.
  132. Never throws an exception.
  133. */
  134. void fz_sha384_init(fz_sha384 *state);
  135. /**
  136. SHA384 block update operation. Continues an SHA384 message-
  137. digest operation, processing another message block, and updating
  138. the context.
  139. Never throws an exception.
  140. */
  141. void fz_sha384_update(fz_sha384 *state, const unsigned char *input, size_t inlen);
  142. /**
  143. SHA384 finalization. Ends an SHA384 message-digest operation,
  144. writing the message digest and zeroizing the context.
  145. Never throws an exception.
  146. */
  147. void fz_sha384_final(fz_sha384 *state, unsigned char digest[64]);
  148. /* arc4 crypto */
  149. /**
  150. Structure definition is public to enable stack
  151. based allocation. Do not access the members directly.
  152. */
  153. typedef struct
  154. {
  155. unsigned x;
  156. unsigned y;
  157. unsigned char state[256];
  158. } fz_arc4;
  159. /**
  160. RC4 initialization. Begins an RC4 operation, writing a new
  161. context.
  162. Never throws an exception.
  163. */
  164. void fz_arc4_init(fz_arc4 *state, const unsigned char *key, size_t len);
  165. /**
  166. RC4 block encrypt operation; encrypt src into dst (both of
  167. length len) updating the RC4 state as we go.
  168. Never throws an exception.
  169. */
  170. void fz_arc4_encrypt(fz_arc4 *state, unsigned char *dest, const unsigned char *src, size_t len);
  171. /**
  172. RC4 finalization. Zero the context.
  173. Never throws an exception.
  174. */
  175. void fz_arc4_final(fz_arc4 *state);
  176. /* AES block cipher implementation from XYSSL */
  177. /**
  178. Structure definitions are public to enable stack
  179. based allocation. Do not access the members directly.
  180. */
  181. typedef struct
  182. {
  183. int nr; /* number of rounds */
  184. uint32_t *rk; /* AES round keys */
  185. uint32_t buf[68]; /* unaligned data */
  186. } fz_aes;
  187. #define FZ_AES_DECRYPT 0
  188. #define FZ_AES_ENCRYPT 1
  189. /**
  190. AES encryption initialization. Fills in the supplied context
  191. and prepares for encryption using the given key.
  192. Returns non-zero for error (key size other than 128/192/256).
  193. Never throws an exception.
  194. */
  195. int fz_aes_setkey_enc(fz_aes *ctx, const unsigned char *key, int keysize);
  196. /**
  197. AES decryption initialization. Fills in the supplied context
  198. and prepares for decryption using the given key.
  199. Returns non-zero for error (key size other than 128/192/256).
  200. Never throws an exception.
  201. */
  202. int fz_aes_setkey_dec(fz_aes *ctx, const unsigned char *key, int keysize);
  203. /**
  204. AES block processing. Encrypts or Decrypts (according to mode,
  205. which must match what was initially set up) length bytes (which
  206. must be a multiple of 16), using (and modifying) the insertion
  207. vector iv, reading from input, and writing to output.
  208. Never throws an exception.
  209. */
  210. void fz_aes_crypt_cbc(fz_aes *ctx, int mode, size_t length,
  211. unsigned char iv[16],
  212. const unsigned char *input,
  213. unsigned char *output );
  214. #endif