PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgp-encrypt.c File Reference
#include "postgres.h"
#include <time.h>
#include "mbuf.h"
#include "px.h"
#include "pgp.h"
#include "utils/backend_random.h"
Include dependency graph for pgp-encrypt.c:

Go to the source code of this file.

Data Structures

struct  EncStat
 
struct  PktStreamStat
 

Macros

#define MDC_DIGEST_LEN   20
 
#define STREAM_ID   0xE0
 
#define STREAM_BLOCK_SHIFT   14
 
#define ENCBUF   8192
 

Functions

static uint8render_newlen (uint8 *h, int len)
 
static int write_tag_only (PushFilter *dst, int tag)
 
static int write_normal_header (PushFilter *dst, int tag, int len)
 
static int mdc_init (PushFilter *dst, void *init_arg, void **priv_p)
 
static int mdc_write (PushFilter *dst, void *priv, const uint8 *data, int len)
 
static int mdc_flush (PushFilter *dst, void *priv)
 
static void mdc_free (void *priv)
 
static int encrypt_init (PushFilter *next, void *init_arg, void **priv_p)
 
static int encrypt_process (PushFilter *next, void *priv, const uint8 *data, int len)
 
static void encrypt_free (void *priv)
 
static int pkt_stream_init (PushFilter *next, void *init_arg, void **priv_p)
 
static int pkt_stream_process (PushFilter *next, void *priv, const uint8 *data, int len)
 
static int pkt_stream_flush (PushFilter *next, void *priv)
 
static void pkt_stream_free (void *priv)
 
int pgp_create_pkt_writer (PushFilter *dst, int tag, PushFilter **res_p)
 
static int crlf_process (PushFilter *dst, void *priv, const uint8 *data, int len)
 
static int init_litdata_packet (PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
 
static int init_compress (PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
 
static int init_encdata_packet (PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
 
static int write_prefix (PGP_Context *ctx, PushFilter *dst)
 
static int symencrypt_sesskey (PGP_Context *ctx, uint8 *dst)
 
static int write_symenc_sesskey (PGP_Context *ctx, PushFilter *dst)
 
static int init_s2k_key (PGP_Context *ctx)
 
static int init_sess_key (PGP_Context *ctx)
 
int pgp_encrypt (PGP_Context *ctx, MBuf *src, MBuf *dst)
 

Variables

static const PushFilterOps mdc_filter
 
static const PushFilterOps encrypt_filter
 
static const PushFilterOps pkt_stream_filter
 
static const PushFilterOps crlf_filter
 

Macro Definition Documentation

#define ENCBUF   8192

Definition at line 153 of file pgp-encrypt.c.

Referenced by encrypt_init(), and encrypt_process().

#define MDC_DIGEST_LEN   20

Definition at line 43 of file pgp-encrypt.c.

Referenced by mdc_flush().

#define STREAM_BLOCK_SHIFT   14

Definition at line 45 of file pgp-encrypt.c.

Referenced by pkt_stream_init(), and pkt_stream_process().

#define STREAM_ID   0xE0

Definition at line 44 of file pgp-encrypt.c.

Referenced by pkt_stream_process().

Function Documentation

static int crlf_process ( PushFilter dst,
void *  priv,
const uint8 data,
int  len 
)
static

Definition at line 331 of file pgp-encrypt.c.

References pushf_write().

332 {
333  const uint8 *data_end = data + len;
334  const uint8 *p2,
335  *p1 = data;
336  int line_len;
337  static const uint8 crlf[] = {'\r', '\n'};
338  int res = 0;
339 
340  while (p1 < data_end)
341  {
342  p2 = memchr(p1, '\n', data_end - p1);
343  if (p2 == NULL)
344  p2 = data_end;
345 
346  line_len = p2 - p1;
347 
348  /* write data */
349  res = 0;
350  if (line_len > 0)
351  {
352  res = pushf_write(dst, p1, line_len);
353  if (res < 0)
354  break;
355  p1 += line_len;
356  }
357 
358  /* write crlf */
359  while (p1 < data_end && *p1 == '\n')
360  {
361  res = pushf_write(dst, crlf, 2);
362  if (res < 0)
363  break;
364  p1++;
365  }
366  }
367  return res;
368 }
unsigned char uint8
Definition: c.h:256
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
static void encrypt_free ( void *  priv)
static

Definition at line 218 of file pgp-encrypt.c.

References EncStat::ciph, pgp_cfb_free(), px_free, and px_memset().

219 {
220  struct EncStat *st = priv;
221 
222  if (st->ciph)
223  pgp_cfb_free(st->ciph);
224  px_memset(st, 0, sizeof(*st));
225  px_free(st);
226 }
#define px_free(p)
Definition: px.h:46
PGP_CFB * ciph
Definition: pgp-encrypt.c:156
void pgp_cfb_free(PGP_CFB *ctx)
Definition: pgp-cfb.c:84
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
static int encrypt_init ( PushFilter next,
void *  init_arg,
void **  priv_p 
)
static

Definition at line 161 of file pgp-encrypt.c.

References EncStat::ciph, PGP_Context::cipher_algo, PGP_Context::disable_mdc, ENCBUF, pgp_cfb_create(), pushf_write(), px_alloc, PGP_Context::sess_key, and PGP_Context::sess_key_len.

162 {
163  struct EncStat *st;
164  PGP_Context *ctx = init_arg;
165  PGP_CFB *ciph;
166  int resync = 1;
167  int res;
168 
169  /* should we use newer packet format? */
170  if (ctx->disable_mdc == 0)
171  {
172  uint8 ver = 1;
173 
174  resync = 0;
175  res = pushf_write(next, &ver, 1);
176  if (res < 0)
177  return res;
178  }
179  res = pgp_cfb_create(&ciph, ctx->cipher_algo,
180  ctx->sess_key, ctx->sess_key_len, resync, NULL);
181  if (res < 0)
182  return res;
183 
184  st = px_alloc(sizeof(*st));
185  memset(st, 0, sizeof(*st));
186  st->ciph = ciph;
187 
188  *priv_p = st;
189  return ENCBUF;
190 }
int cipher_algo
Definition: pgp.h:144
unsigned char uint8
Definition: c.h:256
int disable_mdc
Definition: pgp.h:147
PGP_CFB * ciph
Definition: pgp-encrypt.c:156
uint8 sess_key[PGP_MAX_KEY]
Definition: pgp.h:171
#define ENCBUF
Definition: pgp-encrypt.c:153
unsigned sess_key_len
Definition: pgp.h:172
int pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len, int resync, uint8 *iv)
Definition: pgp-cfb.c:52
#define px_alloc(s)
Definition: px.h:44
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
static int encrypt_process ( PushFilter next,
void *  priv,
const uint8 data,
int  len 
)
static

Definition at line 193 of file pgp-encrypt.c.

References EncStat::buf, EncStat::ciph, ENCBUF, pgp_cfb_encrypt(), and pushf_write().

194 {
195  int res;
196  struct EncStat *st = priv;
197  int avail = len;
198 
199  while (avail > 0)
200  {
201  int tmplen = avail > ENCBUF ? ENCBUF : avail;
202 
203  res = pgp_cfb_encrypt(st->ciph, data, tmplen, st->buf);
204  if (res < 0)
205  return res;
206 
207  res = pushf_write(next, st->buf, tmplen);
208  if (res < 0)
209  return res;
210 
211  data += tmplen;
212  avail -= tmplen;
213  }
214  return 0;
215 }
PGP_CFB * ciph
Definition: pgp-encrypt.c:156
#define ENCBUF
Definition: pgp-encrypt.c:153
uint8 buf[ENCBUF]
Definition: pgp-encrypt.c:157
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
int pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:251
static int init_compress ( PushFilter **  pf_res,
PGP_Context ctx,
PushFilter dst 
)
static

Definition at line 433 of file pgp-encrypt.c.

References PGP_Context::compress_algo, pgp_compress_filter(), PGP_PKT_COMPRESSED_DATA, pushf_create(), pushf_free(), pushf_write(), and write_tag_only().

Referenced by pgp_encrypt().

434 {
435  int res;
436  uint8 type = ctx->compress_algo;
437  PushFilter *pkt;
438 
440  if (res < 0)
441  return res;
442 
443  res = pushf_create(&pkt, &pkt_stream_filter, ctx, dst);
444  if (res < 0)
445  return res;
446 
447  res = pushf_write(pkt, &type, 1);
448  if (res >= 0)
449  res = pgp_compress_filter(pf_res, ctx, pkt);
450 
451  if (res < 0)
452  pushf_free(pkt);
453 
454  return res;
455 }
void pushf_free(PushFilter *mp)
Definition: mbuf.c:412
unsigned char uint8
Definition: c.h:256
int pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst)
Definition: pgp-compress.c:317
static int write_tag_only(PushFilter *dst, int tag)
Definition: pgp-encrypt.c:71
int compress_algo
Definition: pgp.h:145
static const PushFilterOps pkt_stream_filter
Definition: pgp-encrypt.c:310
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
Definition: mbuf.c:373
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
static int init_encdata_packet ( PushFilter **  pf_res,
PGP_Context ctx,
PushFilter dst 
)
static

Definition at line 461 of file pgp-encrypt.c.

References PGP_Context::disable_mdc, PGP_PKT_SYMENCRYPTED_DATA, PGP_PKT_SYMENCRYPTED_DATA_MDC, pushf_create(), and write_tag_only().

Referenced by pgp_encrypt().

462 {
463  int res;
464  int tag;
465 
466  if (ctx->disable_mdc)
468  else
470 
471  res = write_tag_only(dst, tag);
472  if (res < 0)
473  return res;
474 
475  return pushf_create(pf_res, &pkt_stream_filter, ctx, dst);
476 }
int disable_mdc
Definition: pgp.h:147
static int write_tag_only(PushFilter *dst, int tag)
Definition: pgp-encrypt.c:71
static const PushFilterOps pkt_stream_filter
Definition: pgp-encrypt.c:310
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
Definition: mbuf.c:373
static int init_litdata_packet ( PushFilter **  pf_res,
PGP_Context ctx,
PushFilter dst 
)
static

Definition at line 378 of file pgp-encrypt.c.

References PGP_PKT_LITERAL_DATA, pushf_create(), pushf_free(), pushf_write(), PGP_Context::text_mode, PGP_Context::unicode_mode, and write_tag_only().

Referenced by pgp_encrypt().

379 {
380  int res;
381  int hdrlen;
382  uint8 hdr[6];
383  uint32 t;
384  PushFilter *pkt;
385  int type;
386 
387  /*
388  * Create header
389  */
390 
391  if (ctx->text_mode)
392  type = ctx->unicode_mode ? 'u' : 't';
393  else
394  type = 'b';
395 
396  /*
397  * Store the creation time into packet. The goal is to have as few known
398  * bytes as possible.
399  */
400  t = (uint32) time(NULL);
401 
402  hdr[0] = type;
403  hdr[1] = 0;
404  hdr[2] = (t >> 24) & 255;
405  hdr[3] = (t >> 16) & 255;
406  hdr[4] = (t >> 8) & 255;
407  hdr[5] = t & 255;
408  hdrlen = 6;
409 
411  if (res < 0)
412  return res;
413 
414  res = pushf_create(&pkt, &pkt_stream_filter, ctx, dst);
415  if (res < 0)
416  return res;
417 
418  res = pushf_write(pkt, hdr, hdrlen);
419  if (res < 0)
420  {
421  pushf_free(pkt);
422  return res;
423  }
424 
425  *pf_res = pkt;
426  return 0;
427 }
void pushf_free(PushFilter *mp)
Definition: mbuf.c:412
int unicode_mode
Definition: pgp.h:151
unsigned char uint8
Definition: c.h:256
static int write_tag_only(PushFilter *dst, int tag)
Definition: pgp-encrypt.c:71
unsigned int uint32
Definition: c.h:258
static const PushFilterOps pkt_stream_filter
Definition: pgp-encrypt.c:310
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
Definition: mbuf.c:373
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
int text_mode
Definition: pgp.h:149
static int init_s2k_key ( PGP_Context ctx)
static

Definition at line 570 of file pgp-encrypt.c.

References PGP_Context::cipher_algo, pgp_s2k_fill(), pgp_s2k_process(), PGP_Context::s2k, PGP_Context::s2k_cipher_algo, PGP_Context::s2k_count, PGP_Context::s2k_digest_algo, PGP_Context::s2k_mode, PGP_Context::sym_key, and PGP_Context::sym_key_len.

Referenced by pgp_encrypt().

571 {
572  int res;
573 
574  if (ctx->s2k_cipher_algo < 0)
575  ctx->s2k_cipher_algo = ctx->cipher_algo;
576 
577  res = pgp_s2k_fill(&ctx->s2k, ctx->s2k_mode, ctx->s2k_digest_algo, ctx->s2k_count);
578  if (res < 0)
579  return res;
580 
581  return pgp_s2k_process(&ctx->s2k, ctx->s2k_cipher_algo,
582  ctx->sym_key, ctx->sym_key_len);
583 }
int sym_key_len
Definition: pgp.h:166
int cipher_algo
Definition: pgp.h:144
const uint8 * sym_key
Definition: pgp.h:165
int pgp_s2k_fill(PGP_S2K *s2k, int mode, int digest_algo, int count)
Definition: pgp-s2k.c:225
int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int key_len)
Definition: pgp-s2k.c:281
int s2k_cipher_algo
Definition: pgp.h:143
int s2k_mode
Definition: pgp.h:140
int s2k_digest_algo
Definition: pgp.h:142
PGP_S2K s2k
Definition: pgp.h:139
int s2k_count
Definition: pgp.h:141
static int init_sess_key ( PGP_Context ctx)
static

Definition at line 586 of file pgp-encrypt.c.

References PGP_Context::cipher_algo, PGP_S2K::key, PGP_S2K::key_len, pg_strong_random(), pgp_get_cipher_key_size(), PGP_Context::pub_key, PXE_NO_RANDOM, PGP_Context::s2k, PGP_Context::sess_key, PGP_Context::sess_key_len, and PGP_Context::use_sess_key.

Referenced by pgp_encrypt().

587 {
588  if (ctx->use_sess_key || ctx->pub_key)
589  {
590 #ifdef HAVE_STRONG_RANDOM
592  if (!pg_strong_random((char *) ctx->sess_key, ctx->sess_key_len))
593  return PXE_NO_RANDOM;
594 #else
595  return PXE_NO_RANDOM;
596 #endif
597  }
598  else
599  {
600  ctx->sess_key_len = ctx->s2k.key_len;
601  memcpy(ctx->sess_key, ctx->s2k.key, ctx->s2k.key_len);
602  }
603 
604  return 0;
605 }
int cipher_algo
Definition: pgp.h:144
bool pg_strong_random(void *buf, size_t len)
#define PXE_NO_RANDOM
Definition: px.h:78
int use_sess_key
Definition: pgp.h:148
uint8 sess_key[PGP_MAX_KEY]
Definition: pgp.h:171
uint8 key[PGP_MAX_KEY]
Definition: pgp.h:129
int pgp_get_cipher_key_size(int code)
Definition: pgp.c:148
unsigned sess_key_len
Definition: pgp.h:172
uint8 key_len
Definition: pgp.h:130
PGP_PubKey * pub_key
Definition: pgp.h:164
PGP_S2K s2k
Definition: pgp.h:139
static int mdc_flush ( PushFilter dst,
void *  priv 
)
static

Definition at line 118 of file pgp-encrypt.c.

References MDC_DIGEST_LEN, pushf_write(), px_md_finish, px_md_update, and px_memset().

119 {
120  int res;
121  uint8 pkt[2 + MDC_DIGEST_LEN];
122  PX_MD *md = priv;
123 
124  /*
125  * create mdc pkt
126  */
127  pkt[0] = 0xD3;
128  pkt[1] = 0x14; /* MDC_DIGEST_LEN */
129  px_md_update(md, pkt, 2);
130  px_md_finish(md, pkt + 2);
131 
132  res = pushf_write(dst, pkt, 2 + MDC_DIGEST_LEN);
133  px_memset(pkt, 0, 2 + MDC_DIGEST_LEN);
134  return res;
135 }
#define px_md_update(md, data, dlen)
Definition: px.h:208
unsigned char uint8
Definition: c.h:256
#define px_md_finish(md, buf)
Definition: px.h:209
Definition: px.h:113
#define MDC_DIGEST_LEN
Definition: pgp-encrypt.c:43
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
static void mdc_free ( void *  priv)
static

Definition at line 138 of file pgp-encrypt.c.

References px_md_free.

139 {
140  PX_MD *md = priv;
141 
142  px_md_free(md);
143 }
Definition: px.h:113
#define px_md_free(md)
Definition: px.h:210
static int mdc_init ( PushFilter dst,
void *  init_arg,
void **  priv_p 
)
static

Definition at line 95 of file pgp-encrypt.c.

References PGP_DIGEST_SHA1, and pgp_load_digest().

96 {
97  int res;
98  PX_MD *md;
99 
100  res = pgp_load_digest(PGP_DIGEST_SHA1, &md);
101  if (res < 0)
102  return res;
103 
104  *priv_p = md;
105  return 0;
106 }
Definition: px.h:113
int pgp_load_digest(int code, PX_MD **res)
Definition: pgp.c:184
static int mdc_write ( PushFilter dst,
void *  priv,
const uint8 data,
int  len 
)
static

Definition at line 109 of file pgp-encrypt.c.

References pushf_write(), and px_md_update.

110 {
111  PX_MD *md = priv;
112 
113  px_md_update(md, data, len);
114  return pushf_write(dst, data, len);
115 }
#define px_md_update(md, data, dlen)
Definition: px.h:208
Definition: px.h:113
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
int pgp_create_pkt_writer ( PushFilter dst,
int  tag,
PushFilter **  res_p 
)

Definition at line 315 of file pgp-encrypt.c.

References pushf_create(), and write_tag_only().

Referenced by pgp_write_pubenc_sesskey().

316 {
317  int res;
318 
319  res = write_tag_only(dst, tag);
320  if (res < 0)
321  return res;
322 
323  return pushf_create(res_p, &pkt_stream_filter, NULL, dst);
324 }
static int write_tag_only(PushFilter *dst, int tag)
Definition: pgp-encrypt.c:71
static const PushFilterOps pkt_stream_filter
Definition: pgp-encrypt.c:310
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
Definition: mbuf.c:373
int pgp_encrypt ( PGP_Context ctx,
MBuf src,
MBuf dst 
)

Definition at line 611 of file pgp-encrypt.c.

References buf, PGP_Context::compress_algo, PGP_Context::compress_level, PGP_Context::convert_crlf, PGP_Context::disable_mdc, init_compress(), init_encdata_packet(), init_litdata_packet(), init_s2k_key(), init_sess_key(), mbuf_avail(), mbuf_grab(), pgp_write_pubenc_sesskey(), PGP_Context::pub_key, pushf_create(), pushf_create_mbuf_writer(), pushf_flush(), pushf_free_all(), pushf_write(), PXE_ARGUMENT_ERROR, PGP_Context::sym_key, PGP_Context::text_mode, write_prefix(), and write_symenc_sesskey().

Referenced by encrypt_internal().

612 {
613  int res;
614  int len;
615  uint8 *buf;
616  PushFilter *pf,
617  *pf_tmp;
618 
619  /*
620  * do we have any key
621  */
622  if (!ctx->sym_key && !ctx->pub_key)
623  return PXE_ARGUMENT_ERROR;
624 
625  /* MBuf writer */
626  res = pushf_create_mbuf_writer(&pf, dst);
627  if (res < 0)
628  goto out;
629 
630  /*
631  * initialize symkey
632  */
633  if (ctx->sym_key)
634  {
635  res = init_s2k_key(ctx);
636  if (res < 0)
637  goto out;
638  }
639 
640  res = init_sess_key(ctx);
641  if (res < 0)
642  goto out;
643 
644  /*
645  * write keypkt
646  */
647  if (ctx->pub_key)
648  res = pgp_write_pubenc_sesskey(ctx, pf);
649  else
650  res = write_symenc_sesskey(ctx, pf);
651  if (res < 0)
652  goto out;
653 
654  /* encrypted data pkt */
655  res = init_encdata_packet(&pf_tmp, ctx, pf);
656  if (res < 0)
657  goto out;
658  pf = pf_tmp;
659 
660  /* encrypter */
661  res = pushf_create(&pf_tmp, &encrypt_filter, ctx, pf);
662  if (res < 0)
663  goto out;
664  pf = pf_tmp;
665 
666  /* hasher */
667  if (ctx->disable_mdc == 0)
668  {
669  res = pushf_create(&pf_tmp, &mdc_filter, ctx, pf);
670  if (res < 0)
671  goto out;
672  pf = pf_tmp;
673  }
674 
675  /* prefix */
676  res = write_prefix(ctx, pf);
677  if (res < 0)
678  goto out;
679 
680  /* compressor */
681  if (ctx->compress_algo > 0 && ctx->compress_level > 0)
682  {
683  res = init_compress(&pf_tmp, ctx, pf);
684  if (res < 0)
685  goto out;
686  pf = pf_tmp;
687  }
688 
689  /* data streamer */
690  res = init_litdata_packet(&pf_tmp, ctx, pf);
691  if (res < 0)
692  goto out;
693  pf = pf_tmp;
694 
695 
696  /* text conversion? */
697  if (ctx->text_mode && ctx->convert_crlf)
698  {
699  res = pushf_create(&pf_tmp, &crlf_filter, ctx, pf);
700  if (res < 0)
701  goto out;
702  pf = pf_tmp;
703  }
704 
705  /*
706  * chain complete
707  */
708 
709  len = mbuf_grab(src, mbuf_avail(src), &buf);
710  res = pushf_write(pf, buf, len);
711  if (res >= 0)
712  res = pushf_flush(pf);
713 out:
714  pushf_free_all(pf);
715  return res;
716 }
int pushf_create_mbuf_writer(PushFilter **res, MBuf *dst)
Definition: mbuf.c:561
#define PXE_ARGUMENT_ERROR
Definition: px.h:74
static int init_s2k_key(PGP_Context *ctx)
Definition: pgp-encrypt.c:570
static const PushFilterOps encrypt_filter
Definition: pgp-encrypt.c:228
int pushf_flush(PushFilter *mp)
Definition: mbuf.c:516
static const PushFilterOps mdc_filter
Definition: pgp-encrypt.c:145
int pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst)
Definition: pgp-pubenc.c:195
const uint8 * sym_key
Definition: pgp.h:165
unsigned char uint8
Definition: c.h:256
int compress_level
Definition: pgp.h:146
static int init_encdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
Definition: pgp-encrypt.c:461
int disable_mdc
Definition: pgp.h:147
int mbuf_grab(MBuf *mbuf, int len, uint8 **data_p)
Definition: mbuf.c:157
static char * buf
Definition: pg_test_fsync.c:67
int compress_algo
Definition: pgp.h:145
void pushf_free_all(PushFilter *mp)
Definition: mbuf.c:428
int mbuf_avail(MBuf *mbuf)
Definition: mbuf.c:50
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
Definition: mbuf.c:373
static int init_litdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
Definition: pgp-encrypt.c:378
static int init_compress(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
Definition: pgp-encrypt.c:433
static int init_sess_key(PGP_Context *ctx)
Definition: pgp-encrypt.c:586
int convert_crlf
Definition: pgp.h:150
static int write_symenc_sesskey(PGP_Context *ctx, PushFilter *dst)
Definition: pgp-encrypt.c:529
PGP_PubKey * pub_key
Definition: pgp.h:164
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
int text_mode
Definition: pgp.h:149
static int write_prefix(PGP_Context *ctx, PushFilter *dst)
Definition: pgp-encrypt.c:482
static const PushFilterOps crlf_filter
Definition: pgp-encrypt.c:370
static int pkt_stream_flush ( PushFilter next,
void *  priv 
)
static

Definition at line 282 of file pgp-encrypt.c.

References PktStreamStat::final_done, pushf_write(), and render_newlen().

283 {
284  int res;
285  uint8 hdr[8];
286  uint8 *h = hdr;
287  struct PktStreamStat *st = priv;
288 
289  /* stream MUST end with normal packet. */
290  if (!st->final_done)
291  {
292  h = render_newlen(h, 0);
293  res = pushf_write(next, hdr, h - hdr);
294  if (res < 0)
295  return res;
296  st->final_done = 1;
297  }
298  return 0;
299 }
static uint8 * render_newlen(uint8 *h, int len)
Definition: pgp-encrypt.c:48
unsigned char uint8
Definition: c.h:256
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
static void pkt_stream_free ( void *  priv)
static

Definition at line 302 of file pgp-encrypt.c.

References px_free, and px_memset().

303 {
304  struct PktStreamStat *st = priv;
305 
306  px_memset(st, 0, sizeof(*st));
307  px_free(st);
308 }
#define px_free(p)
Definition: px.h:46
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
static int pkt_stream_init ( PushFilter next,
void *  init_arg,
void **  priv_p 
)
static

Definition at line 243 of file pgp-encrypt.c.

References PktStreamStat::final_done, PktStreamStat::pkt_block, px_alloc, and STREAM_BLOCK_SHIFT.

244 {
245  struct PktStreamStat *st;
246 
247  st = px_alloc(sizeof(*st));
248  st->final_done = 0;
249  st->pkt_block = 1 << STREAM_BLOCK_SHIFT;
250  *priv_p = st;
251 
252  return st->pkt_block;
253 }
#define STREAM_BLOCK_SHIFT
Definition: pgp-encrypt.c:45
#define px_alloc(s)
Definition: px.h:44
static int pkt_stream_process ( PushFilter next,
void *  priv,
const uint8 data,
int  len 
)
static

Definition at line 256 of file pgp-encrypt.c.

References PktStreamStat::final_done, PktStreamStat::pkt_block, pushf_write(), PXE_BUG, render_newlen(), STREAM_BLOCK_SHIFT, and STREAM_ID.

257 {
258  int res;
259  uint8 hdr[8];
260  uint8 *h = hdr;
261  struct PktStreamStat *st = priv;
262 
263  if (st->final_done)
264  return PXE_BUG;
265 
266  if (len == st->pkt_block)
267  *h++ = STREAM_ID | STREAM_BLOCK_SHIFT;
268  else
269  {
270  h = render_newlen(h, len);
271  st->final_done = 1;
272  }
273 
274  res = pushf_write(next, hdr, h - hdr);
275  if (res < 0)
276  return res;
277 
278  return pushf_write(next, data, len);
279 }
static uint8 * render_newlen(uint8 *h, int len)
Definition: pgp-encrypt.c:48
unsigned char uint8
Definition: c.h:256
#define PXE_BUG
Definition: px.h:73
#define STREAM_BLOCK_SHIFT
Definition: pgp-encrypt.c:45
#define STREAM_ID
Definition: pgp-encrypt.c:44
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
static uint8* render_newlen ( uint8 h,
int  len 
)
static

Definition at line 48 of file pgp-encrypt.c.

Referenced by pkt_stream_flush(), pkt_stream_process(), and write_normal_header().

49 {
50  if (len <= 191)
51  {
52  *h++ = len & 255;
53  }
54  else if (len > 191 && len <= 8383)
55  {
56  *h++ = ((len - 192) >> 8) + 192;
57  *h++ = (len - 192) & 255;
58  }
59  else
60  {
61  *h++ = 255;
62  *h++ = (len >> 24) & 255;
63  *h++ = (len >> 16) & 255;
64  *h++ = (len >> 8) & 255;
65  *h++ = len & 255;
66  }
67  return h;
68 }
static int symencrypt_sesskey ( PGP_Context ctx,
uint8 dst 
)
static

Definition at line 509 of file pgp-encrypt.c.

References PGP_Context::cipher_algo, PGP_S2K::key, PGP_S2K::key_len, pgp_cfb_create(), pgp_cfb_encrypt(), pgp_cfb_free(), PGP_Context::s2k, PGP_Context::s2k_cipher_algo, PGP_Context::sess_key, and PGP_Context::sess_key_len.

Referenced by write_symenc_sesskey().

510 {
511  int res;
512  PGP_CFB *cfb;
513  uint8 algo = ctx->cipher_algo;
514 
515  res = pgp_cfb_create(&cfb, ctx->s2k_cipher_algo,
516  ctx->s2k.key, ctx->s2k.key_len, 0, NULL);
517  if (res < 0)
518  return res;
519 
520  pgp_cfb_encrypt(cfb, &algo, 1, dst);
521  pgp_cfb_encrypt(cfb, ctx->sess_key, ctx->sess_key_len, dst + 1);
522 
523  pgp_cfb_free(cfb);
524  return ctx->sess_key_len + 1;
525 }
int cipher_algo
Definition: pgp.h:144
unsigned char uint8
Definition: c.h:256
uint8 sess_key[PGP_MAX_KEY]
Definition: pgp.h:171
uint8 key[PGP_MAX_KEY]
Definition: pgp.h:129
void pgp_cfb_free(PGP_CFB *ctx)
Definition: pgp-cfb.c:84
unsigned sess_key_len
Definition: pgp.h:172
int s2k_cipher_algo
Definition: pgp.h:143
uint8 key_len
Definition: pgp.h:130
int pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len, int resync, uint8 *iv)
Definition: pgp-cfb.c:52
PGP_S2K s2k
Definition: pgp.h:139
int pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:251
static int write_normal_header ( PushFilter dst,
int  tag,
int  len 
)
static

Definition at line 79 of file pgp-encrypt.c.

References pushf_write(), and render_newlen().

Referenced by write_symenc_sesskey().

80 {
81  uint8 hdr[8];
82  uint8 *h = hdr;
83 
84  *h++ = 0xC0 | tag;
85  h = render_newlen(h, len);
86  return pushf_write(dst, hdr, h - hdr);
87 }
static uint8 * render_newlen(uint8 *h, int len)
Definition: pgp-encrypt.c:48
unsigned char uint8
Definition: c.h:256
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
static int write_prefix ( PGP_Context ctx,
PushFilter dst 
)
static

Definition at line 482 of file pgp-encrypt.c.

References PGP_Context::cipher_algo, pg_backend_random(), pgp_get_cipher_block_size(), PGP_MAX_BLOCK, pushf_write(), px_memset(), and PXE_NO_RANDOM.

Referenced by pgp_encrypt().

483 {
484 #ifdef HAVE_STRONG_RANDOM
485  uint8 prefix[PGP_MAX_BLOCK + 2];
486  int res,
487  bs;
488 
490  if (!pg_backend_random((char *) prefix, bs))
491  return PXE_NO_RANDOM;
492 
493  prefix[bs + 0] = prefix[bs - 2];
494  prefix[bs + 1] = prefix[bs - 1];
495 
496  res = pushf_write(dst, prefix, bs + 2);
497  px_memset(prefix, 0, bs + 2);
498  return res < 0 ? res : 0;
499 #else
500  return PXE_NO_RANDOM;
501 #endif
502 }
int cipher_algo
Definition: pgp.h:144
unsigned char uint8
Definition: c.h:256
#define PXE_NO_RANDOM
Definition: px.h:78
#define PGP_MAX_BLOCK
Definition: pgp.h:113
bool pg_backend_random(char *dst, int len)
int pgp_get_cipher_block_size(int code)
Definition: pgp.c:158
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
static int write_symenc_sesskey ( PGP_Context ctx,
PushFilter dst 
)
static

Definition at line 529 of file pgp-encrypt.c.

References PGP_S2K::digest_algo, PGP_S2K::iter, PGP_S2K::mode, PGP_PKT_SYMENCRYPTED_SESSKEY, pushf_write(), px_memset(), PGP_Context::s2k, PGP_Context::s2k_cipher_algo, PGP_S2K::salt, symencrypt_sesskey(), PGP_Context::use_sess_key, and write_normal_header().

Referenced by pgp_encrypt().

530 {
531  uint8 pkt[256];
532  int pktlen;
533  int res;
534  uint8 *p = pkt;
535 
536  *p++ = 4; /* 5.3 - version number */
537  *p++ = ctx->s2k_cipher_algo;
538 
539  *p++ = ctx->s2k.mode;
540  *p++ = ctx->s2k.digest_algo;
541  if (ctx->s2k.mode > 0)
542  {
543  memcpy(p, ctx->s2k.salt, 8);
544  p += 8;
545  }
546  if (ctx->s2k.mode == 3)
547  *p++ = ctx->s2k.iter;
548 
549  if (ctx->use_sess_key)
550  {
551  res = symencrypt_sesskey(ctx, p);
552  if (res < 0)
553  return res;
554  p += res;
555  }
556 
557  pktlen = p - pkt;
559  if (res >= 0)
560  res = pushf_write(dst, pkt, pktlen);
561 
562  px_memset(pkt, 0, pktlen);
563  return res;
564 }
uint8 mode
Definition: pgp.h:124
static int symencrypt_sesskey(PGP_Context *ctx, uint8 *dst)
Definition: pgp-encrypt.c:509
unsigned char uint8
Definition: c.h:256
uint8 digest_algo
Definition: pgp.h:125
int use_sess_key
Definition: pgp.h:148
uint8 salt[8]
Definition: pgp.h:126
static int write_normal_header(PushFilter *dst, int tag, int len)
Definition: pgp-encrypt.c:79
uint8 iter
Definition: pgp.h:127
int s2k_cipher_algo
Definition: pgp.h:143
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
PGP_S2K s2k
Definition: pgp.h:139
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
static int write_tag_only ( PushFilter dst,
int  tag 
)
static

Definition at line 71 of file pgp-encrypt.c.

References pushf_write().

Referenced by init_compress(), init_encdata_packet(), init_litdata_packet(), and pgp_create_pkt_writer().

72 {
73  uint8 hdr = 0xC0 | tag;
74 
75  return pushf_write(dst, &hdr, 1);
76 }
unsigned char uint8
Definition: c.h:256
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456

Variable Documentation

const PushFilterOps crlf_filter
static
Initial value:
= {
NULL, crlf_process, NULL, NULL
}
static int crlf_process(PushFilter *dst, void *priv, const uint8 *data, int len)
Definition: pgp-encrypt.c:331

Definition at line 370 of file pgp-encrypt.c.

const PushFilterOps encrypt_filter
static
Initial value:
= {
}
static void encrypt_free(void *priv)
Definition: pgp-encrypt.c:218
static int encrypt_init(PushFilter *next, void *init_arg, void **priv_p)
Definition: pgp-encrypt.c:161
static int encrypt_process(PushFilter *next, void *priv, const uint8 *data, int len)
Definition: pgp-encrypt.c:193

Definition at line 228 of file pgp-encrypt.c.

const PushFilterOps mdc_filter
static
Initial value:
= {
}
static int mdc_flush(PushFilter *dst, void *priv)
Definition: pgp-encrypt.c:118
static void mdc_free(void *priv)
Definition: pgp-encrypt.c:138
static int mdc_write(PushFilter *dst, void *priv, const uint8 *data, int len)
Definition: pgp-encrypt.c:109
static int mdc_init(PushFilter *dst, void *init_arg, void **priv_p)
Definition: pgp-encrypt.c:95

Definition at line 145 of file pgp-encrypt.c.

const PushFilterOps pkt_stream_filter
static
Initial value:
= {
}
static int pkt_stream_process(PushFilter *next, void *priv, const uint8 *data, int len)
Definition: pgp-encrypt.c:256
static void pkt_stream_free(void *priv)
Definition: pgp-encrypt.c:302
static int pkt_stream_init(PushFilter *next, void *init_arg, void **priv_p)
Definition: pgp-encrypt.c:243
static int pkt_stream_flush(PushFilter *next, void *priv)
Definition: pgp-encrypt.c:282

Definition at line 310 of file pgp-encrypt.c.