Header And Logo

PostgreSQL
| The world's most advanced open source database.

pgp.h

Go to the documentation of this file.
00001 /*
00002  * pgp.h
00003  *    OpenPGP implementation.
00004  *
00005  * Copyright (c) 2005 Marko Kreen
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * contrib/pgcrypto/pgp.h
00030  */
00031 
00032 enum PGP_S2K_TYPE
00033 {
00034     PGP_S2K_SIMPLE = 0,
00035     PGP_S2K_SALTED = 1,
00036     PGP_S2K_ISALTED = 3
00037 };
00038 
00039 enum PGP_PKT_TYPE
00040 {
00041     PGP_PKT_RESERVED = 0,
00042     PGP_PKT_PUBENCRYPTED_SESSKEY = 1,
00043     PGP_PKT_SIGNATURE = 2,
00044     PGP_PKT_SYMENCRYPTED_SESSKEY = 3,
00045     PGP_PKT_SECRET_KEY = 5,
00046     PGP_PKT_PUBLIC_KEY = 6,
00047     PGP_PKT_SECRET_SUBKEY = 7,
00048     PGP_PKT_COMPRESSED_DATA = 8,
00049     PGP_PKT_SYMENCRYPTED_DATA = 9,
00050     PGP_PKT_MARKER = 10,
00051     PGP_PKT_LITERAL_DATA = 11,
00052     PGP_PKT_TRUST = 12,
00053     PGP_PKT_USER_ID = 13,
00054     PGP_PKT_PUBLIC_SUBKEY = 14,
00055     PGP_PKT_USER_ATTR = 17,
00056     PGP_PKT_SYMENCRYPTED_DATA_MDC = 18,
00057     PGP_PKT_MDC = 19,
00058     PGP_PKT_PRIV_61 = 61        /* occurs in gpg secring */
00059 };
00060 
00061 enum PGP_PUB_ALGO_TYPE
00062 {
00063     PGP_PUB_RSA_ENCRYPT_SIGN = 1,
00064     PGP_PUB_RSA_ENCRYPT = 2,
00065     PGP_PUB_RSA_SIGN = 3,
00066     PGP_PUB_ELG_ENCRYPT = 16,
00067     PGP_PUB_DSA_SIGN = 17
00068 };
00069 
00070 enum PGP_SYMENC_TYPE
00071 {
00072     PGP_SYM_PLAIN = 0,          /* ?? */
00073     PGP_SYM_IDEA = 1,           /* obsolete, PGP 2.6 compat */
00074     PGP_SYM_DES3 = 2,           /* must */
00075     PGP_SYM_CAST5 = 3,          /* should */
00076     PGP_SYM_BLOWFISH = 4,
00077     PGP_SYM_SAFER_SK128 = 5,    /* obsolete */
00078     PGP_SYM_DES_SK = 6,         /* obsolete */
00079     PGP_SYM_AES_128 = 7,        /* should */
00080     PGP_SYM_AES_192 = 8,
00081     PGP_SYM_AES_256 = 9,
00082     PGP_SYM_TWOFISH = 10
00083 };
00084 
00085 enum PGP_COMPR_TYPE
00086 {
00087     PGP_COMPR_NONE = 0,         /* must */
00088     PGP_COMPR_ZIP = 1,          /* should */
00089     PGP_COMPR_ZLIB = 2,
00090     PGP_COMPR_BZIP2 = 3
00091 };
00092 
00093 enum PGP_DIGEST_TYPE
00094 {
00095     PGP_DIGEST_MD5 = 1,         /* should, deprecated  */
00096     PGP_DIGEST_SHA1 = 2,        /* must */
00097     PGP_DIGEST_RIPEMD160 = 3,
00098     PGP_DIGEST_XSHA = 4,        /* obsolete */
00099     PGP_DIGEST_MD2 = 5,         /* obsolete */
00100     PGP_DIGEST_TIGER192 = 6,    /* obsolete */
00101     PGP_DIGEST_HAVAL5_160 = 7,  /* obsolete */
00102     PGP_DIGEST_SHA256 = 8,
00103     PGP_DIGEST_SHA384 = 9,
00104     PGP_DIGEST_SHA512 = 10
00105 };
00106 
00107 #define PGP_MAX_KEY    (256/8)
00108 #define PGP_MAX_BLOCK  (256/8)
00109 #define PGP_MAX_DIGEST (512/8)
00110 #define PGP_S2K_SALT   8
00111 
00112 typedef struct PGP_MPI PGP_MPI;
00113 typedef struct PGP_PubKey PGP_PubKey;
00114 typedef struct PGP_Context PGP_Context;
00115 typedef struct PGP_S2K PGP_S2K;
00116 
00117 struct PGP_S2K
00118 {
00119     uint8       mode;
00120     uint8       digest_algo;
00121     uint8       salt[8];
00122     uint8       iter;
00123     /* calculated: */
00124     uint8       key[PGP_MAX_KEY];
00125     uint8       key_len;
00126 };
00127 
00128 
00129 struct PGP_Context
00130 {
00131     /*
00132      * parameters
00133      */
00134     PGP_S2K     s2k;
00135     int         s2k_mode;
00136     int         s2k_digest_algo;
00137     int         s2k_cipher_algo;
00138     int         cipher_algo;
00139     int         compress_algo;
00140     int         compress_level;
00141     int         disable_mdc;
00142     int         use_sess_key;
00143     int         text_mode;
00144     int         convert_crlf;
00145     int         unicode_mode;
00146 
00147     /*
00148      * internal variables
00149      */
00150     int         mdc_checked;
00151     int         corrupt_prefix;
00152     int         in_mdc_pkt;
00153     int         use_mdcbuf_filter;
00154     PX_MD      *mdc_ctx;
00155 
00156     PGP_PubKey *pub_key;        /* ctx owns it */
00157     const uint8 *sym_key;       /* ctx does not own it */
00158     int         sym_key_len;
00159 
00160     /*
00161      * read or generated data
00162      */
00163     uint8       sess_key[PGP_MAX_KEY];
00164     unsigned    sess_key_len;
00165 };
00166 
00167 struct PGP_MPI
00168 {
00169     uint8      *data;
00170     int         bits;
00171     int         bytes;
00172 };
00173 
00174 struct PGP_PubKey
00175 {
00176     uint8       ver;
00177     uint8       time[4];
00178     uint8       algo;
00179 
00180     /* public part */
00181     union
00182     {
00183         struct
00184         {
00185             PGP_MPI    *p;
00186             PGP_MPI    *g;
00187             PGP_MPI    *y;
00188         }           elg;
00189         struct
00190         {
00191             PGP_MPI    *n;
00192             PGP_MPI    *e;
00193         }           rsa;
00194         struct
00195         {
00196             PGP_MPI    *p;
00197             PGP_MPI    *q;
00198             PGP_MPI    *g;
00199             PGP_MPI    *y;
00200         }           dsa;
00201     }           pub;
00202 
00203     /* secret part */
00204     union
00205     {
00206         struct
00207         {
00208             PGP_MPI    *x;
00209         }           elg;
00210         struct
00211         {
00212             PGP_MPI    *d;
00213             PGP_MPI    *p;
00214             PGP_MPI    *q;
00215             PGP_MPI    *u;
00216         }           rsa;
00217         struct
00218         {
00219             PGP_MPI    *x;
00220         }           dsa;
00221     }           sec;
00222 
00223     uint8       key_id[8];
00224     int         can_encrypt;
00225 };
00226 
00227 int         pgp_init(PGP_Context **ctx);
00228 int         pgp_encrypt(PGP_Context *ctx, MBuf *src, MBuf *dst);
00229 int         pgp_decrypt(PGP_Context *ctx, MBuf *src, MBuf *dst);
00230 int         pgp_free(PGP_Context *ctx);
00231 
00232 int         pgp_get_digest_code(const char *name);
00233 int         pgp_get_cipher_code(const char *name);
00234 const char *pgp_get_digest_name(int code);
00235 const char *pgp_get_cipher_name(int code);
00236 
00237 int         pgp_set_cipher_algo(PGP_Context *ctx, const char *name);
00238 int         pgp_set_s2k_mode(PGP_Context *ctx, int type);
00239 int         pgp_set_s2k_cipher_algo(PGP_Context *ctx, const char *name);
00240 int         pgp_set_s2k_digest_algo(PGP_Context *ctx, const char *name);
00241 int         pgp_set_convert_crlf(PGP_Context *ctx, int doit);
00242 int         pgp_disable_mdc(PGP_Context *ctx, int disable);
00243 int         pgp_set_sess_key(PGP_Context *ctx, int use);
00244 int         pgp_set_compress_algo(PGP_Context *ctx, int algo);
00245 int         pgp_set_compress_level(PGP_Context *ctx, int level);
00246 int         pgp_set_text_mode(PGP_Context *ctx, int mode);
00247 int         pgp_set_unicode_mode(PGP_Context *ctx, int mode);
00248 int         pgp_get_unicode_mode(PGP_Context *ctx);
00249 
00250 int         pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int klen);
00251 int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt,
00252                const uint8 *key, int klen, int pubtype);
00253 
00254 int         pgp_get_keyid(MBuf *pgp_data, char *dst);
00255 
00256 /* internal functions */
00257 
00258 int         pgp_load_digest(int c, PX_MD **res);
00259 int         pgp_load_cipher(int c, PX_Cipher **res);
00260 int         pgp_get_cipher_key_size(int c);
00261 int         pgp_get_cipher_block_size(int c);
00262 
00263 int         pgp_s2k_fill(PGP_S2K *s2k, int mode, int digest_algo);
00264 int         pgp_s2k_read(PullFilter *src, PGP_S2K *s2k);
00265 int         pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int klen);
00266 
00267 typedef struct PGP_CFB PGP_CFB;
00268 int
00269 pgp_cfb_create(PGP_CFB **ctx_p, int algo,
00270                const uint8 *key, int key_len, int recync, uint8 *iv);
00271 void        pgp_cfb_free(PGP_CFB *ctx);
00272 int         pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst);
00273 int         pgp_cfb_decrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst);
00274 
00275 int         pgp_armor_encode(const uint8 *src, unsigned len, uint8 *dst);
00276 int         pgp_armor_decode(const uint8 *src, unsigned len, uint8 *dst);
00277 unsigned    pgp_armor_enc_len(unsigned len);
00278 unsigned    pgp_armor_dec_len(unsigned len);
00279 
00280 int         pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst);
00281 int         pgp_decompress_filter(PullFilter **res, PGP_Context *ctx, PullFilter *src);
00282 
00283 int         pgp_key_alloc(PGP_PubKey **pk_p);
00284 void        pgp_key_free(PGP_PubKey *pk);
00285 int         _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p);
00286 
00287 int         pgp_parse_pubenc_sesskey(PGP_Context *ctx, PullFilter *pkt);
00288 int pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len,
00289                       int pkttype, PGP_Context *ctx);
00290 int pgp_parse_pkt_hdr(PullFilter *src, uint8 *tag, int *len_p,
00291                   int allow_ctx);
00292 
00293 int         pgp_skip_packet(PullFilter *pkt);
00294 int         pgp_expect_packet_end(PullFilter *pkt);
00295 
00296 int         pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst);
00297 int         pgp_create_pkt_writer(PushFilter *dst, int tag, PushFilter **res_p);
00298 
00299 int         pgp_mpi_alloc(int bits, PGP_MPI **mpi);
00300 int         pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi);
00301 int         pgp_mpi_free(PGP_MPI *mpi);
00302 int         pgp_mpi_read(PullFilter *src, PGP_MPI **mpi);
00303 int         pgp_mpi_write(PushFilter *dst, PGP_MPI *n);
00304 int         pgp_mpi_hash(PX_MD *md, PGP_MPI *n);
00305 unsigned    pgp_mpi_cksum(unsigned cksum, PGP_MPI *n);
00306 
00307 int pgp_elgamal_encrypt(PGP_PubKey *pk, PGP_MPI *m,
00308                     PGP_MPI **c1, PGP_MPI **c2);
00309 int pgp_elgamal_decrypt(PGP_PubKey *pk, PGP_MPI *c1, PGP_MPI *c2,
00310                     PGP_MPI **m);
00311 int         pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *m, PGP_MPI **c);
00312 int         pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *c, PGP_MPI **m);
00313 
00314 extern struct PullFilterOps pgp_decrypt_filter;