~ubuntu-branches/ubuntu/hardy/postgresql-8.4/hardy-backports

« back to all changes in this revision

Viewing changes to contrib/pgcrypto/pgp.h

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * $PostgreSQL$
 
30
 */
 
31
 
 
32
enum PGP_S2K_TYPE
 
33
{
 
34
        PGP_S2K_SIMPLE = 0,
 
35
        PGP_S2K_SALTED = 1,
 
36
        PGP_S2K_ISALTED = 3
 
37
};
 
38
 
 
39
enum PGP_PKT_TYPE
 
40
{
 
41
        PGP_PKT_RESERVED = 0,
 
42
        PGP_PKT_PUBENCRYPTED_SESSKEY = 1,
 
43
        PGP_PKT_SIGNATURE = 2,
 
44
        PGP_PKT_SYMENCRYPTED_SESSKEY = 3,
 
45
        PGP_PKT_SECRET_KEY = 5,
 
46
        PGP_PKT_PUBLIC_KEY = 6,
 
47
        PGP_PKT_SECRET_SUBKEY = 7,
 
48
        PGP_PKT_COMPRESSED_DATA = 8,
 
49
        PGP_PKT_SYMENCRYPTED_DATA = 9,
 
50
        PGP_PKT_MARKER = 10,
 
51
        PGP_PKT_LITERAL_DATA = 11,
 
52
        PGP_PKT_TRUST = 12,
 
53
        PGP_PKT_USER_ID = 13,
 
54
        PGP_PKT_PUBLIC_SUBKEY = 14,
 
55
        PGP_PKT_USER_ATTR = 17,
 
56
        PGP_PKT_SYMENCRYPTED_DATA_MDC = 18,
 
57
        PGP_PKT_MDC = 19,
 
58
        PGP_PKT_PRIV_61 = 61            /* occurs in gpg secring */
 
59
};
 
60
 
 
61
enum PGP_PUB_ALGO_TYPE
 
62
{
 
63
        PGP_PUB_RSA_ENCRYPT_SIGN = 1,
 
64
        PGP_PUB_RSA_ENCRYPT = 2,
 
65
        PGP_PUB_RSA_SIGN = 3,
 
66
        PGP_PUB_ELG_ENCRYPT = 16,
 
67
        PGP_PUB_DSA_SIGN = 17
 
68
};
 
69
 
 
70
enum PGP_SYMENC_TYPE
 
71
{
 
72
        PGP_SYM_PLAIN = 0,                      /* ?? */
 
73
        PGP_SYM_IDEA = 1,                       /* obsolete, PGP 2.6 compat */
 
74
        PGP_SYM_DES3 = 2,                       /* must */
 
75
        PGP_SYM_CAST5 = 3,                      /* should */
 
76
        PGP_SYM_BLOWFISH = 4,
 
77
        PGP_SYM_SAFER_SK128 = 5,        /* obsolete */
 
78
        PGP_SYM_DES_SK = 6,                     /* obsolete */
 
79
        PGP_SYM_AES_128 = 7,            /* should */
 
80
        PGP_SYM_AES_192 = 8,
 
81
        PGP_SYM_AES_256 = 9,
 
82
        PGP_SYM_TWOFISH = 10
 
83
};
 
84
 
 
85
enum PGP_COMPR_TYPE
 
86
{
 
87
        PGP_COMPR_NONE = 0,                     /* must */
 
88
        PGP_COMPR_ZIP = 1,                      /* should */
 
89
        PGP_COMPR_ZLIB = 2,
 
90
        PGP_COMPR_BZIP2 = 3
 
91
};
 
92
 
 
93
enum PGP_DIGEST_TYPE
 
94
{
 
95
        PGP_DIGEST_MD5 = 1,                     /* should, deprecated  */
 
96
        PGP_DIGEST_SHA1 = 2,            /* must */
 
97
        PGP_DIGEST_RIPEMD160 = 3,
 
98
        PGP_DIGEST_XSHA = 4,            /* obsolete */
 
99
        PGP_DIGEST_MD2 = 5,                     /* obsolete */
 
100
        PGP_DIGEST_TIGER192 = 6,        /* obsolete */
 
101
        PGP_DIGEST_HAVAL5_160 = 7,      /* obsolete */
 
102
        PGP_DIGEST_SHA256 = 8,
 
103
        PGP_DIGEST_SHA384 = 9,
 
104
        PGP_DIGEST_SHA512 = 10
 
105
};
 
106
 
 
107
#define PGP_MAX_KEY    (256/8)
 
108
#define PGP_MAX_BLOCK  (256/8)
 
109
#define PGP_MAX_DIGEST (512/8)
 
110
#define PGP_S2K_SALT   8
 
111
 
 
112
typedef struct PGP_MPI PGP_MPI;
 
113
typedef struct PGP_PubKey PGP_PubKey;
 
114
typedef struct PGP_Context PGP_Context;
 
115
typedef struct PGP_S2K PGP_S2K;
 
116
 
 
117
struct PGP_S2K
 
118
{
 
119
        uint8           mode;
 
120
        uint8           digest_algo;
 
121
        uint8           salt[8];
 
122
        uint8           iter;
 
123
        /* calculated: */
 
124
        uint8           key[PGP_MAX_KEY];
 
125
        uint8           key_len;
 
126
};
 
127
 
 
128
 
 
129
struct PGP_Context
 
130
{
 
131
        /*
 
132
         * parameters
 
133
         */
 
134
        PGP_S2K         s2k;
 
135
        int                     s2k_mode;
 
136
        int                     s2k_digest_algo;
 
137
        int                     s2k_cipher_algo;
 
138
        int                     cipher_algo;
 
139
        int                     compress_algo;
 
140
        int                     compress_level;
 
141
        int                     disable_mdc;
 
142
        int                     use_sess_key;
 
143
        int                     text_mode;
 
144
        int                     convert_crlf;
 
145
        int                     unicode_mode;
 
146
 
 
147
        /*
 
148
         * internal variables
 
149
         */
 
150
        int                     mdc_checked;
 
151
        int                     corrupt_prefix;
 
152
        int                     in_mdc_pkt;
 
153
        int                     use_mdcbuf_filter;
 
154
        PX_MD      *mdc_ctx;
 
155
 
 
156
        PGP_PubKey *pub_key;            /* ctx owns it */
 
157
        const uint8 *sym_key;           /* ctx does not own it */
 
158
        int                     sym_key_len;
 
159
 
 
160
        /*
 
161
         * read or generated data
 
162
         */
 
163
        uint8           sess_key[PGP_MAX_KEY];
 
164
        unsigned        sess_key_len;
 
165
};
 
166
 
 
167
struct PGP_MPI
 
168
{
 
169
        uint8      *data;
 
170
        int                     bits;
 
171
        int                     bytes;
 
172
};
 
173
 
 
174
struct PGP_PubKey
 
175
{
 
176
        uint8           ver;
 
177
        uint8           time[4];
 
178
        uint8           algo;
 
179
 
 
180
        /* public part */
 
181
        union
 
182
        {
 
183
                struct
 
184
                {
 
185
                        PGP_MPI    *p;
 
186
                        PGP_MPI    *g;
 
187
                        PGP_MPI    *y;
 
188
                }                       elg;
 
189
                struct
 
190
                {
 
191
                        PGP_MPI    *n;
 
192
                        PGP_MPI    *e;
 
193
                }                       rsa;
 
194
                struct
 
195
                {
 
196
                        PGP_MPI    *p;
 
197
                        PGP_MPI    *q;
 
198
                        PGP_MPI    *g;
 
199
                        PGP_MPI    *y;
 
200
                }                       dsa;
 
201
        }                       pub;
 
202
 
 
203
        /* secret part */
 
204
        union
 
205
        {
 
206
                struct
 
207
                {
 
208
                        PGP_MPI    *x;
 
209
                }                       elg;
 
210
                struct
 
211
                {
 
212
                        PGP_MPI    *d;
 
213
                        PGP_MPI    *p;
 
214
                        PGP_MPI    *q;
 
215
                        PGP_MPI    *u;
 
216
                }                       rsa;
 
217
                struct
 
218
                {
 
219
                        PGP_MPI    *x;
 
220
                }                       dsa;
 
221
        }                       sec;
 
222
 
 
223
        uint8           key_id[8];
 
224
        int                     can_encrypt;
 
225
};
 
226
 
 
227
int                     pgp_init(PGP_Context ** ctx);
 
228
int                     pgp_encrypt(PGP_Context * ctx, MBuf * src, MBuf * dst);
 
229
int                     pgp_decrypt(PGP_Context * ctx, MBuf * src, MBuf * dst);
 
230
int                     pgp_free(PGP_Context * ctx);
 
231
 
 
232
int                     pgp_get_digest_code(const char *name);
 
233
int                     pgp_get_cipher_code(const char *name);
 
234
const char *pgp_get_digest_name(int code);
 
235
const char *pgp_get_cipher_name(int code);
 
236
 
 
237
int                     pgp_set_cipher_algo(PGP_Context * ctx, const char *name);
 
238
int                     pgp_set_s2k_mode(PGP_Context * ctx, int type);
 
239
int                     pgp_set_s2k_cipher_algo(PGP_Context * ctx, const char *name);
 
240
int                     pgp_set_s2k_digest_algo(PGP_Context * ctx, const char *name);
 
241
int                     pgp_set_convert_crlf(PGP_Context * ctx, int doit);
 
242
int                     pgp_disable_mdc(PGP_Context * ctx, int disable);
 
243
int                     pgp_set_sess_key(PGP_Context * ctx, int use);
 
244
int                     pgp_set_compress_algo(PGP_Context * ctx, int algo);
 
245
int                     pgp_set_compress_level(PGP_Context * ctx, int level);
 
246
int                     pgp_set_text_mode(PGP_Context * ctx, int mode);
 
247
int                     pgp_set_unicode_mode(PGP_Context * ctx, int mode);
 
248
int                     pgp_get_unicode_mode(PGP_Context * ctx);
 
249
 
 
250
int                     pgp_set_symkey(PGP_Context * ctx, const uint8 *key, int klen);
 
251
int pgp_set_pubkey(PGP_Context * ctx, MBuf * keypkt,
 
252
                           const uint8 *key, int klen, int pubtype);
 
253
 
 
254
int                     pgp_get_keyid(MBuf * pgp_data, char *dst);
 
255
 
 
256
/* internal functions */
 
257
 
 
258
int                     pgp_load_digest(int c, PX_MD ** res);
 
259
int                     pgp_load_cipher(int c, PX_Cipher ** res);
 
260
int                     pgp_get_cipher_key_size(int c);
 
261
int                     pgp_get_cipher_block_size(int c);
 
262
 
 
263
int                     pgp_s2k_fill(PGP_S2K * s2k, int mode, int digest_algo);
 
264
int                     pgp_s2k_read(PullFilter * src, PGP_S2K * s2k);
 
265
int                     pgp_s2k_process(PGP_S2K * s2k, int cipher, const uint8 *key, int klen);
 
266
 
 
267
typedef struct PGP_CFB PGP_CFB;
 
268
int
 
269
pgp_cfb_create(PGP_CFB ** ctx_p, int algo,
 
270
                           const uint8 *key, int key_len, int recync, uint8 *iv);
 
271
void            pgp_cfb_free(PGP_CFB * ctx);
 
272
int                     pgp_cfb_encrypt(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst);
 
273
int                     pgp_cfb_decrypt(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst);
 
274
 
 
275
int                     pgp_armor_encode(const uint8 *src, unsigned len, uint8 *dst);
 
276
int                     pgp_armor_decode(const uint8 *src, unsigned len, uint8 *dst);
 
277
unsigned        pgp_armor_enc_len(unsigned len);
 
278
unsigned        pgp_armor_dec_len(unsigned len);
 
279
 
 
280
int                     pgp_compress_filter(PushFilter ** res, PGP_Context * ctx, PushFilter * dst);
 
281
int                     pgp_decompress_filter(PullFilter ** res, PGP_Context * ctx, PullFilter * src);
 
282
 
 
283
int                     pgp_key_alloc(PGP_PubKey ** pk_p);
 
284
void            pgp_key_free(PGP_PubKey * pk);
 
285
int                     _pgp_read_public_key(PullFilter * pkt, PGP_PubKey ** pk_p);
 
286
 
 
287
int                     pgp_parse_pubenc_sesskey(PGP_Context * ctx, PullFilter * pkt);
 
288
int pgp_create_pkt_reader(PullFilter ** pf_p, PullFilter * src, int len,
 
289
                                          int pkttype, PGP_Context * ctx);
 
290
int pgp_parse_pkt_hdr(PullFilter * src, uint8 *tag, int *len_p,
 
291
                                  int allow_ctx);
 
292
 
 
293
int                     pgp_skip_packet(PullFilter * pkt);
 
294
int                     pgp_expect_packet_end(PullFilter * pkt);
 
295
 
 
296
int                     pgp_write_pubenc_sesskey(PGP_Context * ctx, PushFilter * dst);
 
297
int                     pgp_create_pkt_writer(PushFilter * dst, int tag, PushFilter ** res_p);
 
298
 
 
299
int                     pgp_mpi_alloc(int bits, PGP_MPI ** mpi);
 
300
int                     pgp_mpi_create(uint8 *data, int bits, PGP_MPI ** mpi);
 
301
int                     pgp_mpi_free(PGP_MPI * mpi);
 
302
int                     pgp_mpi_read(PullFilter * src, PGP_MPI ** mpi);
 
303
int                     pgp_mpi_write(PushFilter * dst, PGP_MPI * n);
 
304
int                     pgp_mpi_hash(PX_MD * md, PGP_MPI * n);
 
305
unsigned        pgp_mpi_cksum(unsigned cksum, PGP_MPI * n);
 
306
 
 
307
int pgp_elgamal_encrypt(PGP_PubKey * pk, PGP_MPI * m,
 
308
                                        PGP_MPI ** c1, PGP_MPI ** c2);
 
309
int pgp_elgamal_decrypt(PGP_PubKey * pk, PGP_MPI * c1, PGP_MPI * c2,
 
310
                                        PGP_MPI ** m);
 
311
int                     pgp_rsa_encrypt(PGP_PubKey * pk, PGP_MPI * m, PGP_MPI ** c);
 
312
int                     pgp_rsa_decrypt(PGP_PubKey * pk, PGP_MPI * c, PGP_MPI ** m);
 
313
 
 
314
extern struct PullFilterOps pgp_decrypt_filter;