PostgreSQL Source Code  git master
pgp-encrypt.c File Reference
#include "postgres.h"
#include <time.h>
#include "mbuf.h"
#include "px.h"
#include "pgp.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

◆ ENCBUF

#define ENCBUF   8192

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

Referenced by encrypt_init(), and encrypt_process().

◆ MDC_DIGEST_LEN

#define MDC_DIGEST_LEN   20

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

Referenced by mdc_flush().

◆ STREAM_BLOCK_SHIFT

#define STREAM_BLOCK_SHIFT   14

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

Referenced by pkt_stream_init(), and pkt_stream_process().

◆ STREAM_ID

#define STREAM_ID   0xE0

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

Referenced by pkt_stream_process().

Function Documentation

◆ crlf_process()

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

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

References pushf_write().

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

◆ encrypt_free()

static void encrypt_free ( void *  priv)
static

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

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

217 {
218  struct EncStat *st = priv;
219 
220  if (st->ciph)
221  pgp_cfb_free(st->ciph);
222  px_memset(st, 0, sizeof(*st));
223  px_free(st);
224 }
#define px_free(p)
Definition: px.h:46
PGP_CFB * ciph
Definition: pgp-encrypt.c:154
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:126

◆ encrypt_init()

static int encrypt_init ( PushFilter next,
void *  init_arg,
void **  priv_p 
)
static

Definition at line 159 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.

160 {
161  struct EncStat *st;
162  PGP_Context *ctx = init_arg;
163  PGP_CFB *ciph;
164  int resync = 1;
165  int res;
166 
167  /* should we use newer packet format? */
168  if (ctx->disable_mdc == 0)
169  {
170  uint8 ver = 1;
171 
172  resync = 0;
173  res = pushf_write(next, &ver, 1);
174  if (res < 0)
175  return res;
176  }
177  res = pgp_cfb_create(&ciph, ctx->cipher_algo,
178  ctx->sess_key, ctx->sess_key_len, resync, NULL);
179  if (res < 0)
180  return res;
181 
182  st = px_alloc(sizeof(*st));
183  memset(st, 0, sizeof(*st));
184  st->ciph = ciph;
185 
186  *priv_p = st;
187  return ENCBUF;
188 }
int cipher_algo
Definition: pgp.h:144
unsigned char uint8
Definition: c.h:356
int disable_mdc
Definition: pgp.h:147
PGP_CFB * ciph
Definition: pgp-encrypt.c:154
uint8 sess_key[PGP_MAX_KEY]
Definition: pgp.h:171
#define ENCBUF
Definition: pgp-encrypt.c:151
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

◆ encrypt_process()

static int encrypt_process ( PushFilter next,
void *  priv,
const uint8 data,
int  len 
)
static

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

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

192 {
193  int res;
194  struct EncStat *st = priv;
195  int avail = len;
196 
197  while (avail > 0)
198  {
199  int tmplen = avail > ENCBUF ? ENCBUF : avail;
200 
201  res = pgp_cfb_encrypt(st->ciph, data, tmplen, st->buf);
202  if (res < 0)
203  return res;
204 
205  res = pushf_write(next, st->buf, tmplen);
206  if (res < 0)
207  return res;
208 
209  data += tmplen;
210  avail -= tmplen;
211  }
212  return 0;
213 }
PGP_CFB * ciph
Definition: pgp-encrypt.c:154
#define ENCBUF
Definition: pgp-encrypt.c:151
uint8 buf[ENCBUF]
Definition: pgp-encrypt.c:155
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

◆ init_compress()

static int init_compress ( PushFilter **  pf_res,
PGP_Context ctx,
PushFilter dst 
)
static

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

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

Referenced by pgp_encrypt().

432 {
433  int res;
434  uint8 type = ctx->compress_algo;
435  PushFilter *pkt;
436 
438  if (res < 0)
439  return res;
440 
441  res = pushf_create(&pkt, &pkt_stream_filter, ctx, dst);
442  if (res < 0)
443  return res;
444 
445  res = pushf_write(pkt, &type, 1);
446  if (res >= 0)
447  res = pgp_compress_filter(pf_res, ctx, pkt);
448 
449  if (res < 0)
450  pushf_free(pkt);
451 
452  return res;
453 }
void pushf_free(PushFilter *mp)
Definition: mbuf.c:412
unsigned char uint8
Definition: c.h:356
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:69
int compress_algo
Definition: pgp.h:145
static const PushFilterOps pkt_stream_filter
Definition: pgp-encrypt.c:308
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

◆ init_encdata_packet()

static int init_encdata_packet ( PushFilter **  pf_res,
PGP_Context ctx,
PushFilter dst 
)
static

Definition at line 459 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().

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

◆ init_litdata_packet()

static int init_litdata_packet ( PushFilter **  pf_res,
PGP_Context ctx,
PushFilter dst 
)
static

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

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

Referenced by pgp_encrypt().

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

◆ init_s2k_key()

static int init_s2k_key ( PGP_Context ctx)
static

Definition at line 564 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().

565 {
566  int res;
567 
568  if (ctx->s2k_cipher_algo < 0)
569  ctx->s2k_cipher_algo = ctx->cipher_algo;
570 
571  res = pgp_s2k_fill(&ctx->s2k, ctx->s2k_mode, ctx->s2k_digest_algo, ctx->s2k_count);
572  if (res < 0)
573  return res;
574 
575  return pgp_s2k_process(&ctx->s2k, ctx->s2k_cipher_algo,
576  ctx->sym_key, ctx->sym_key_len);
577 }
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:224
int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int key_len)
Definition: pgp-s2k.c:280
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

◆ init_sess_key()

static int init_sess_key ( PGP_Context ctx)
static

Definition at line 580 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().

581 {
582  if (ctx->use_sess_key || ctx->pub_key)
583  {
585  if (!pg_strong_random(ctx->sess_key, ctx->sess_key_len))
586  return PXE_NO_RANDOM;
587  }
588  else
589  {
590  ctx->sess_key_len = ctx->s2k.key_len;
591  memcpy(ctx->sess_key, ctx->s2k.key, ctx->s2k.key_len);
592  }
593 
594  return 0;
595 }
int cipher_algo
Definition: pgp.h:144
#define PXE_NO_RANDOM
Definition: px.h:75
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:147
bool pg_strong_random(void *buf, size_t len)
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

◆ mdc_flush()

static int mdc_flush ( PushFilter dst,
void *  priv 
)
static

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

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

117 {
118  int res;
119  uint8 pkt[2 + MDC_DIGEST_LEN];
120  PX_MD *md = priv;
121 
122  /*
123  * create mdc pkt
124  */
125  pkt[0] = 0xD3;
126  pkt[1] = 0x14; /* MDC_DIGEST_LEN */
127  px_md_update(md, pkt, 2);
128  px_md_finish(md, pkt + 2);
129 
130  res = pushf_write(dst, pkt, 2 + MDC_DIGEST_LEN);
131  px_memset(pkt, 0, 2 + MDC_DIGEST_LEN);
132  return res;
133 }
#define px_md_update(md, data, dlen)
Definition: px.h:205
unsigned char uint8
Definition: c.h:356
#define px_md_finish(md, buf)
Definition: px.h:206
Definition: px.h:110
#define MDC_DIGEST_LEN
Definition: pgp-encrypt.c:41
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:126

◆ mdc_free()

static void mdc_free ( void *  priv)
static

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

References px_md_free.

137 {
138  PX_MD *md = priv;
139 
140  px_md_free(md);
141 }
Definition: px.h:110
#define px_md_free(md)
Definition: px.h:207

◆ mdc_init()

static int mdc_init ( PushFilter dst,
void *  init_arg,
void **  priv_p 
)
static

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

References PGP_DIGEST_SHA1, and pgp_load_digest().

94 {
95  int res;
96  PX_MD *md;
97 
98  res = pgp_load_digest(PGP_DIGEST_SHA1, &md);
99  if (res < 0)
100  return res;
101 
102  *priv_p = md;
103  return 0;
104 }
Definition: px.h:110
int pgp_load_digest(int code, PX_MD **res)
Definition: pgp.c:183

◆ mdc_write()

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

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

References pushf_write(), and px_md_update.

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

◆ pgp_create_pkt_writer()

int pgp_create_pkt_writer ( PushFilter dst,
int  tag,
PushFilter **  res_p 
)

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

References pushf_create(), and write_tag_only().

Referenced by pgp_write_pubenc_sesskey().

314 {
315  int res;
316 
317  res = write_tag_only(dst, tag);
318  if (res < 0)
319  return res;
320 
321  return pushf_create(res_p, &pkt_stream_filter, NULL, dst);
322 }
static int write_tag_only(PushFilter *dst, int tag)
Definition: pgp-encrypt.c:69
static const PushFilterOps pkt_stream_filter
Definition: pgp-encrypt.c:308
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
Definition: mbuf.c:373

◆ pgp_encrypt()

int pgp_encrypt ( PGP_Context ctx,
MBuf src,
MBuf dst 
)

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

References EncStat::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().

602 {
603  int res;
604  int len;
605  uint8 *buf;
606  PushFilter *pf,
607  *pf_tmp;
608 
609  /*
610  * do we have any key
611  */
612  if (!ctx->sym_key && !ctx->pub_key)
613  return PXE_ARGUMENT_ERROR;
614 
615  /* MBuf writer */
616  res = pushf_create_mbuf_writer(&pf, dst);
617  if (res < 0)
618  goto out;
619 
620  /*
621  * initialize sym_key
622  */
623  if (ctx->sym_key)
624  {
625  res = init_s2k_key(ctx);
626  if (res < 0)
627  goto out;
628  }
629 
630  res = init_sess_key(ctx);
631  if (res < 0)
632  goto out;
633 
634  /*
635  * write keypkt
636  */
637  if (ctx->pub_key)
638  res = pgp_write_pubenc_sesskey(ctx, pf);
639  else
640  res = write_symenc_sesskey(ctx, pf);
641  if (res < 0)
642  goto out;
643 
644  /* encrypted data pkt */
645  res = init_encdata_packet(&pf_tmp, ctx, pf);
646  if (res < 0)
647  goto out;
648  pf = pf_tmp;
649 
650  /* encrypter */
651  res = pushf_create(&pf_tmp, &encrypt_filter, ctx, pf);
652  if (res < 0)
653  goto out;
654  pf = pf_tmp;
655 
656  /* hasher */
657  if (ctx->disable_mdc == 0)
658  {
659  res = pushf_create(&pf_tmp, &mdc_filter, ctx, pf);
660  if (res < 0)
661  goto out;
662  pf = pf_tmp;
663  }
664 
665  /* prefix */
666  res = write_prefix(ctx, pf);
667  if (res < 0)
668  goto out;
669 
670  /* compressor */
671  if (ctx->compress_algo > 0 && ctx->compress_level > 0)
672  {
673  res = init_compress(&pf_tmp, ctx, pf);
674  if (res < 0)
675  goto out;
676  pf = pf_tmp;
677  }
678 
679  /* data streamer */
680  res = init_litdata_packet(&pf_tmp, ctx, pf);
681  if (res < 0)
682  goto out;
683  pf = pf_tmp;
684 
685 
686  /* text conversion? */
687  if (ctx->text_mode && ctx->convert_crlf)
688  {
689  res = pushf_create(&pf_tmp, &crlf_filter, ctx, pf);
690  if (res < 0)
691  goto out;
692  pf = pf_tmp;
693  }
694 
695  /*
696  * chain complete
697  */
698 
699  len = mbuf_grab(src, mbuf_avail(src), &buf);
700  res = pushf_write(pf, buf, len);
701  if (res >= 0)
702  res = pushf_flush(pf);
703 out:
704  pushf_free_all(pf);
705  return res;
706 }
int pushf_create_mbuf_writer(PushFilter **res, MBuf *dst)
Definition: mbuf.c:561
#define PXE_ARGUMENT_ERROR
Definition: px.h:71
static int init_s2k_key(PGP_Context *ctx)
Definition: pgp-encrypt.c:564
static const PushFilterOps encrypt_filter
Definition: pgp-encrypt.c:226
int pushf_flush(PushFilter *mp)
Definition: mbuf.c:516
static const PushFilterOps mdc_filter
Definition: pgp-encrypt.c:143
int pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst)
Definition: pgp-pubenc.c:190
const uint8 * sym_key
Definition: pgp.h:165
unsigned char uint8
Definition: c.h:356
int compress_level
Definition: pgp.h:146
static int init_encdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
Definition: pgp-encrypt.c:459
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:68
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:376
static int init_compress(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
Definition: pgp-encrypt.c:431
static int init_sess_key(PGP_Context *ctx)
Definition: pgp-encrypt.c:580
int convert_crlf
Definition: pgp.h:150
static int write_symenc_sesskey(PGP_Context *ctx, PushFilter *dst)
Definition: pgp-encrypt.c:523
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:480
static const PushFilterOps crlf_filter
Definition: pgp-encrypt.c:368

◆ pkt_stream_flush()

static int pkt_stream_flush ( PushFilter next,
void *  priv 
)
static

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

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

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

◆ pkt_stream_free()

static void pkt_stream_free ( void *  priv)
static

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

References px_free, and px_memset().

301 {
302  struct PktStreamStat *st = priv;
303 
304  px_memset(st, 0, sizeof(*st));
305  px_free(st);
306 }
#define px_free(p)
Definition: px.h:46
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:126

◆ pkt_stream_init()

static int pkt_stream_init ( PushFilter next,
void *  init_arg,
void **  priv_p 
)
static

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

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

242 {
243  struct PktStreamStat *st;
244 
245  st = px_alloc(sizeof(*st));
246  st->final_done = 0;
247  st->pkt_block = 1 << STREAM_BLOCK_SHIFT;
248  *priv_p = st;
249 
250  return st->pkt_block;
251 }
#define STREAM_BLOCK_SHIFT
Definition: pgp-encrypt.c:43
#define px_alloc(s)
Definition: px.h:44

◆ pkt_stream_process()

static int pkt_stream_process ( PushFilter next,
void *  priv,
const uint8 data,
int  len 
)
static

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

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

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

◆ render_newlen()

static uint8* render_newlen ( uint8 h,
int  len 
)
static

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

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

47 {
48  if (len <= 191)
49  {
50  *h++ = len & 255;
51  }
52  else if (len > 191 && len <= 8383)
53  {
54  *h++ = ((len - 192) >> 8) + 192;
55  *h++ = (len - 192) & 255;
56  }
57  else
58  {
59  *h++ = 255;
60  *h++ = (len >> 24) & 255;
61  *h++ = (len >> 16) & 255;
62  *h++ = (len >> 8) & 255;
63  *h++ = len & 255;
64  }
65  return h;
66 }

◆ symencrypt_sesskey()

static int symencrypt_sesskey ( PGP_Context ctx,
uint8 dst 
)
static

Definition at line 503 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().

504 {
505  int res;
506  PGP_CFB *cfb;
507  uint8 algo = ctx->cipher_algo;
508 
509  res = pgp_cfb_create(&cfb, ctx->s2k_cipher_algo,
510  ctx->s2k.key, ctx->s2k.key_len, 0, NULL);
511  if (res < 0)
512  return res;
513 
514  pgp_cfb_encrypt(cfb, &algo, 1, dst);
515  pgp_cfb_encrypt(cfb, ctx->sess_key, ctx->sess_key_len, dst + 1);
516 
517  pgp_cfb_free(cfb);
518  return ctx->sess_key_len + 1;
519 }
int cipher_algo
Definition: pgp.h:144
unsigned char uint8
Definition: c.h:356
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

◆ write_normal_header()

static int write_normal_header ( PushFilter dst,
int  tag,
int  len 
)
static

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

References pushf_write(), and render_newlen().

Referenced by write_symenc_sesskey().

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

◆ write_prefix()

static int write_prefix ( PGP_Context ctx,
PushFilter dst 
)
static

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

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

Referenced by pgp_encrypt().

481 {
482  uint8 prefix[PGP_MAX_BLOCK + 2];
483  int res,
484  bs;
485 
487  if (!pg_strong_random(prefix, bs))
488  return PXE_NO_RANDOM;
489 
490  prefix[bs + 0] = prefix[bs - 2];
491  prefix[bs + 1] = prefix[bs - 1];
492 
493  res = pushf_write(dst, prefix, bs + 2);
494  px_memset(prefix, 0, bs + 2);
495  return res < 0 ? res : 0;
496 }
int cipher_algo
Definition: pgp.h:144
unsigned char uint8
Definition: c.h:356
#define PXE_NO_RANDOM
Definition: px.h:75
#define PGP_MAX_BLOCK
Definition: pgp.h:113
bool pg_strong_random(void *buf, size_t len)
int pgp_get_cipher_block_size(int code)
Definition: pgp.c:157
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:126

◆ write_symenc_sesskey()

static int write_symenc_sesskey ( PGP_Context ctx,
PushFilter dst 
)
static

Definition at line 523 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().

524 {
525  uint8 pkt[256];
526  int pktlen;
527  int res;
528  uint8 *p = pkt;
529 
530  *p++ = 4; /* 5.3 - version number */
531  *p++ = ctx->s2k_cipher_algo;
532 
533  *p++ = ctx->s2k.mode;
534  *p++ = ctx->s2k.digest_algo;
535  if (ctx->s2k.mode > 0)
536  {
537  memcpy(p, ctx->s2k.salt, 8);
538  p += 8;
539  }
540  if (ctx->s2k.mode == 3)
541  *p++ = ctx->s2k.iter;
542 
543  if (ctx->use_sess_key)
544  {
545  res = symencrypt_sesskey(ctx, p);
546  if (res < 0)
547  return res;
548  p += res;
549  }
550 
551  pktlen = p - pkt;
553  if (res >= 0)
554  res = pushf_write(dst, pkt, pktlen);
555 
556  px_memset(pkt, 0, pktlen);
557  return res;
558 }
uint8 mode
Definition: pgp.h:124
static int symencrypt_sesskey(PGP_Context *ctx, uint8 *dst)
Definition: pgp-encrypt.c:503
unsigned char uint8
Definition: c.h:356
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:77
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:126

◆ write_tag_only()

static int write_tag_only ( PushFilter dst,
int  tag 
)
static

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

References pushf_write().

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

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

Variable Documentation

◆ crlf_filter

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:329

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

◆ encrypt_filter

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

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

◆ mdc_filter

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

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

◆ pkt_stream_filter

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:254
static void pkt_stream_free(void *priv)
Definition: pgp-encrypt.c:300
static int pkt_stream_init(PushFilter *next, void *init_arg, void **priv_p)
Definition: pgp-encrypt.c:241
static int pkt_stream_flush(PushFilter *next, void *priv)
Definition: pgp-encrypt.c:280

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