Debian Codegraph @

contrib/pgcrypto/pgp.h

raw
1 /*
2 * pgp.h
3 * OpenPGP implementation.
4 *
5 * Copyright (c) 2005 Marko Kreen
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * contrib/pgcrypto/pgp.h
30 */
31
32 #include "lib/stringinfo.h"
33
34 #include "mbuf.h"
35 #include "px.h"
36
37 enum PGP_S2K_TYPE
38 {
39 PGP_S2K_SIMPLE = 0,
40 PGP_S2K_SALTED = 1,
41 PGP_S2K_ISALTED = 3,
42 };
43
44 enum PGP_PKT_TYPE
45 {
46 PGP_PKT_RESERVED = 0,
47 PGP_PKT_PUBENCRYPTED_SESSKEY = 1,
48 PGP_PKT_SIGNATURE = 2,
49 PGP_PKT_SYMENCRYPTED_SESSKEY = 3,
50 PGP_PKT_SECRET_KEY = 5,
51 PGP_PKT_PUBLIC_KEY = 6,
52 PGP_PKT_SECRET_SUBKEY = 7,
53 PGP_PKT_COMPRESSED_DATA = 8,
54 PGP_PKT_SYMENCRYPTED_DATA = 9,
55 PGP_PKT_MARKER = 10,
56 PGP_PKT_LITERAL_DATA = 11,
57 PGP_PKT_TRUST = 12,
58 PGP_PKT_USER_ID = 13,
59 PGP_PKT_PUBLIC_SUBKEY = 14,
60 PGP_PKT_USER_ATTR = 17,
61 PGP_PKT_SYMENCRYPTED_DATA_MDC = 18,
62 PGP_PKT_MDC = 19,
63 PGP_PKT_PRIV_61 = 61, /* occurs in gpg secring */
64 };
65
66 enum PGP_PUB_ALGO_TYPE
67 {
68 PGP_PUB_RSA_ENCRYPT_SIGN = 1,
69 PGP_PUB_RSA_ENCRYPT = 2,
70 PGP_PUB_RSA_SIGN = 3,
71 PGP_PUB_ELG_ENCRYPT = 16,
72 PGP_PUB_DSA_SIGN = 17,
73 };
74
75 enum PGP_SYMENC_TYPE
76 {
77 PGP_SYM_PLAIN = 0, /* ?? */
78 PGP_SYM_IDEA = 1, /* obsolete, PGP 2.6 compat */
79 PGP_SYM_DES3 = 2, /* must */
80 PGP_SYM_CAST5 = 3, /* should */
81 PGP_SYM_BLOWFISH = 4,
82 PGP_SYM_SAFER_SK128 = 5, /* obsolete */
83 PGP_SYM_DES_SK = 6, /* obsolete */
84 PGP_SYM_AES_128 = 7, /* should */
85 PGP_SYM_AES_192 = 8,
86 PGP_SYM_AES_256 = 9,
87 PGP_SYM_TWOFISH = 10,
88 };
89
90 enum PGP_COMPR_TYPE
91 {
92 PGP_COMPR_NONE = 0, /* must */
93 PGP_COMPR_ZIP = 1, /* should */
94 PGP_COMPR_ZLIB = 2,
95 PGP_COMPR_BZIP2 = 3,
96 };
97
98 enum PGP_DIGEST_TYPE
99 {
100 PGP_DIGEST_MD5 = 1, /* should, deprecated */
101 PGP_DIGEST_SHA1 = 2, /* must */
102 PGP_DIGEST_RIPEMD160 = 3,
103 PGP_DIGEST_XSHA = 4, /* obsolete */
104 PGP_DIGEST_MD2 = 5, /* obsolete */
105 PGP_DIGEST_TIGER192 = 6, /* obsolete */
106 PGP_DIGEST_HAVAL5_160 = 7, /* obsolete */
107 PGP_DIGEST_SHA256 = 8,
108 PGP_DIGEST_SHA384 = 9,
109 PGP_DIGEST_SHA512 = 10,
110 };
111
112 #define PGP_MAX_KEY (256/8)
113 #define PGP_MAX_BLOCK (256/8)
114 #define PGP_MAX_DIGEST (512/8)
115 #define PGP_S2K_SALT 8
116
117 typedef struct PGP_MPI PGP_MPI;
118 typedef struct PGP_PubKey PGP_PubKey;
119 typedef struct PGP_Context PGP_Context;
120 typedef struct PGP_S2K PGP_S2K;
121
122 struct PGP_S2K
123 {
124 uint8 mode;
125 uint8 digest_algo;
126 uint8 salt[8];
127 uint8 iter; /* encoded (one-octet) count */
128 /* calculated: */
129 uint8 key[PGP_MAX_KEY];
130 uint8 key_len;
131 };
132
133
134 struct PGP_Context
135 {
136 /*
137 * parameters
138 */
139 PGP_S2K s2k;
140 int s2k_mode;
141 int s2k_count; /* 4-byte decoded count */
142 int s2k_digest_algo;
143 int s2k_cipher_algo;
144 int cipher_algo;
145 int compress_algo;
146 int compress_level;
147 int disable_mdc;
148 int use_sess_key;
149 int text_mode;
150 int convert_crlf;
151 int unicode_mode;
152
153 /* DANGEROUS recovery aid for CVE-2026-14663. Applies only to decryption. */
154 int ignore_cipher_failure;
155
156 /*
157 * internal variables
158 */
159 int mdc_checked;
160 int corrupt_prefix; /* prefix failed RFC 4880 "quick check" */
161 int unsupported_compr; /* has bzip2 compression */
162 int unexpected_binary; /* binary data seen in text_mode */
163 int in_mdc_pkt;
164 int use_mdcbuf_filter;
165 PX_MD *mdc_ctx;
166
167 PGP_PubKey *pub_key; /* ctx owns it */
168 const uint8 *sym_key; /* ctx does not own it */
169 int sym_key_len;
170
171 /*
172 * read or generated data
173 */
174 uint8 sess_key[PGP_MAX_KEY];
175 unsigned sess_key_len;
176 };
177
178 /* from RFC 4880 3.7.1.3 */
179 #define s2k_decode_count(cval) \
180 (((unsigned) 16 + (cval & 15)) << ((cval >> 4) + 6))
181
182 struct PGP_MPI
183 {
184 uint8 *data;
185 int bits;
186 int bytes;
187 };
188
189 struct PGP_PubKey
190 {
191 uint8 ver;
192 uint8 time[4];
193 uint8 algo;
194
195 /* public part */
196 union
197 {
198 struct
199 {
200 PGP_MPI *p;
201 PGP_MPI *g;
202 PGP_MPI *y;
203 } elg;
204 struct
205 {
206 PGP_MPI *n;
207 PGP_MPI *e;
208 } rsa;
209 struct
210 {
211 PGP_MPI *p;
212 PGP_MPI *q;
213 PGP_MPI *g;
214 PGP_MPI *y;
215 } dsa;
216 } pub;
217
218 /* secret part */
219 union
220 {
221 struct
222 {
223 PGP_MPI *x;
224 } elg;
225 struct
226 {
227 PGP_MPI *d;
228 PGP_MPI *p;
229 PGP_MPI *q;
230 PGP_MPI *u;
231 } rsa;
232 struct
233 {
234 PGP_MPI *x;
235 } dsa;
236 } sec;
237
238 uint8 key_id[8];
239 int can_encrypt;
240 };
241
242 int pgp_init(PGP_Context **ctx_p);
243 int pgp_encrypt(PGP_Context *ctx, MBuf *src, MBuf *dst);
244 int pgp_decrypt(PGP_Context *ctx, MBuf *msrc, MBuf *mdst);
245 int pgp_free(PGP_Context *ctx);
246
247 int pgp_get_digest_code(const char *name);
248 int pgp_get_cipher_code(const char *name);
249 const char *pgp_get_digest_name(int code);
250
251 int pgp_set_cipher_algo(PGP_Context *ctx, const char *name);
252 int pgp_set_s2k_mode(PGP_Context *ctx, int mode);
253 int pgp_set_s2k_count(PGP_Context *ctx, int count);
254 int pgp_set_s2k_cipher_algo(PGP_Context *ctx, const char *name);
255 int pgp_set_s2k_digest_algo(PGP_Context *ctx, const char *name);
256 int pgp_set_convert_crlf(PGP_Context *ctx, int doit);
257 int pgp_disable_mdc(PGP_Context *ctx, int disable);
258 int pgp_set_sess_key(PGP_Context *ctx, int use);
259 int pgp_set_compress_algo(PGP_Context *ctx, int algo);
260 int pgp_set_compress_level(PGP_Context *ctx, int level);
261 int pgp_set_text_mode(PGP_Context *ctx, int mode);
262 int pgp_set_unicode_mode(PGP_Context *ctx, int mode);
263 int pgp_get_unicode_mode(PGP_Context *ctx);
264 int pgp_set_ignore_cipher_failure(PGP_Context *ctx, int ignore);
265
266 int pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int len);
267 int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt,
268 const uint8 *key, int key_len, int pubtype);
269
270 int pgp_get_keyid(MBuf *pgp_data, char *dst);
271
272 /* internal functions */
273
274 int pgp_load_digest(int code, PX_MD **res);
275 int pgp_load_cipher(int code, PX_Cipher **res);
276 int pgp_get_cipher_key_size(int code);
277 int pgp_get_cipher_block_size(int code);
278
279 int pgp_s2k_fill(PGP_S2K *s2k, int mode, int digest_algo, int count);
280 int pgp_s2k_read(PullFilter *src, PGP_S2K *s2k);
281 int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int key_len);
282
283 typedef struct PGP_CFB PGP_CFB;
284 int pgp_cfb_create(PGP_CFB **ctx_p, int algo,
285 const uint8 *key, int key_len, int resync, uint8 *iv,
286 int ignore_decrypt_cipher_failure);
287 void pgp_cfb_free(PGP_CFB *ctx);
288 int pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst);
289 int pgp_cfb_decrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst);
290
291 void pgp_armor_encode(const uint8 *src, unsigned len, StringInfo dst,
292 int num_headers, char **keys, char **values);
293 int pgp_armor_decode(const uint8 *src, int len, StringInfo dst);
294 int pgp_extract_armor_headers(const uint8 *src, unsigned len,
295 int *nheaders, char ***keys, char ***values);
296
297 int pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst);
298 int pgp_decompress_filter(PullFilter **res, PGP_Context *ctx, PullFilter *src);
299
300 int pgp_key_alloc(PGP_PubKey **pk_p);
301 void pgp_key_free(PGP_PubKey *pk);
302 int _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p);
303
304 int pgp_parse_pubenc_sesskey(PGP_Context *ctx, PullFilter *pkt);
305 int pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len,
306 int pkttype, PGP_Context *ctx);
307 int pgp_parse_pkt_hdr(PullFilter *src, uint8 *tag, int *len_p,
308 int allow_ctx);
309
310 int pgp_skip_packet(PullFilter *pkt);
311 int pgp_expect_packet_end(PullFilter *pkt);
312
313 int pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst);
314 int pgp_create_pkt_writer(PushFilter *dst, int tag, PushFilter **res_p);
315
316 int pgp_mpi_alloc(int bits, PGP_MPI **mpi);
317 int pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi);
318 int pgp_mpi_free(PGP_MPI *mpi);
319 int pgp_mpi_read(PullFilter *src, PGP_MPI **mpi);
320 int pgp_mpi_write(PushFilter *dst, PGP_MPI *n);
321 int pgp_mpi_hash(PX_MD *md, PGP_MPI *n);
322 unsigned pgp_mpi_cksum(unsigned cksum, PGP_MPI *n);
323
324 int pgp_elgamal_encrypt(PGP_PubKey *pk, PGP_MPI *_m,
325 PGP_MPI **c1_p, PGP_MPI **c2_p);
326 int pgp_elgamal_decrypt(PGP_PubKey *pk, PGP_MPI *_c1, PGP_MPI *_c2,
327 PGP_MPI **msg_p);
328 int pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *_m, PGP_MPI **c_p);
329 int pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *_c, PGP_MPI **m_p);
330
331 extern struct PullFilterOps pgp_decrypt_filter;