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

Referenced by encrypt_init(), and encrypt_process().

◆ MDC_DIGEST_LEN

#define MDC_DIGEST_LEN   20

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

Referenced by mdc_flush().

◆ STREAM_BLOCK_SHIFT

#define STREAM_BLOCK_SHIFT   14

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

Referenced by pkt_stream_init(), and pkt_stream_process().

◆ STREAM_ID

#define STREAM_ID   0xE0

Definition at line 41 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 328 of file pgp-encrypt.c.

References pushf_write().

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

◆ encrypt_free()

static void encrypt_free ( void *  priv)
static

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

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

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

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

◆ encrypt_process()

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

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

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

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

431 {
432  int res;
433  uint8 type = ctx->compress_algo;
434  PushFilter *pkt;
435 
437  if (res < 0)
438  return res;
439 
440  res = pushf_create(&pkt, &pkt_stream_filter, ctx, dst);
441  if (res < 0)
442  return res;
443 
444  res = pushf_write(pkt, &type, 1);
445  if (res >= 0)
446  res = pgp_compress_filter(pf_res, ctx, pkt);
447 
448  if (res < 0)
449  pushf_free(pkt);
450 
451  return res;
452 }
void pushf_free(PushFilter *mp)
Definition: mbuf.c:410
unsigned char uint8
Definition: c.h:357
int pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst)
Definition: pgp-compress.c:316
static int write_tag_only(PushFilter *dst, int tag)
Definition: pgp-encrypt.c:68
int compress_algo
Definition: pgp.h:145
static const PushFilterOps pkt_stream_filter
Definition: pgp-encrypt.c:307
int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)
Definition: mbuf.c:371
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:454

◆ init_encdata_packet()

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

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

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

◆ init_litdata_packet()

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

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

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

◆ init_s2k_key()

static int init_s2k_key ( PGP_Context ctx)
static

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

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

580 {
581  if (ctx->use_sess_key || ctx->pub_key)
582  {
584  if (!pg_strong_random(ctx->sess_key, ctx->sess_key_len))
585  return PXE_NO_RANDOM;
586  }
587  else
588  {
589  ctx->sess_key_len = ctx->s2k.key_len;
590  memcpy(ctx->sess_key, ctx->s2k.key, ctx->s2k.key_len);
591  }
592 
593  return 0;
594 }
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 115 of file pgp-encrypt.c.

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

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

References px_md_free.

136 {
137  PX_MD *md = priv;
138 
139  px_md_free(md);
140 }
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 92 of file pgp-encrypt.c.

References PGP_DIGEST_SHA1, and pgp_load_digest().

93 {
94  int res;
95  PX_MD *md;
96 
97  res = pgp_load_digest(PGP_DIGEST_SHA1, &md);
98  if (res < 0)
99  return res;
100 
101  *priv_p = md;
102  return 0;
103 }
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 106 of file pgp-encrypt.c.

References pushf_write(), and px_md_update.

107 {
108  PX_MD *md = priv;
109 
110  px_md_update(md, data, len);
111  return pushf_write(dst, data, len);
112 }
#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:454

◆ pgp_create_pkt_writer()

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

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

References pushf_create(), and write_tag_only().

Referenced by pgp_write_pubenc_sesskey().

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

◆ pgp_encrypt()

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

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

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

◆ pkt_stream_flush()

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

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

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

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

◆ pkt_stream_free()

static void pkt_stream_free ( void *  priv)
static

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

References px_free, and px_memset().

300 {
301  struct PktStreamStat *st = priv;
302 
303  px_memset(st, 0, sizeof(*st));
304  px_free(st);
305 }
#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 240 of file pgp-encrypt.c.

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

241 {
242  struct PktStreamStat *st;
243 
244  st = px_alloc(sizeof(*st));
245  st->final_done = 0;
246  st->pkt_block = 1 << STREAM_BLOCK_SHIFT;
247  *priv_p = st;
248 
249  return st->pkt_block;
250 }
#define STREAM_BLOCK_SHIFT
Definition: pgp-encrypt.c:42
#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 253 of file pgp-encrypt.c.

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

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

◆ render_newlen()

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

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

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

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

◆ symencrypt_sesskey()

static int symencrypt_sesskey ( PGP_Context ctx,
uint8 dst 
)
static

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

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

References pushf_write(), and render_newlen().

Referenced by write_symenc_sesskey().

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

◆ write_prefix()

static int write_prefix ( PGP_Context ctx,
PushFilter dst 
)
static

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

480 {
481  uint8 prefix[PGP_MAX_BLOCK + 2];
482  int res,
483  bs;
484 
486  if (!pg_strong_random(prefix, bs))
487  return PXE_NO_RANDOM;
488 
489  prefix[bs + 0] = prefix[bs - 2];
490  prefix[bs + 1] = prefix[bs - 1];
491 
492  res = pushf_write(dst, prefix, bs + 2);
493  px_memset(prefix, 0, bs + 2);
494  return res < 0 ? res : 0;
495 }
int cipher_algo
Definition: pgp.h:144
unsigned char uint8
Definition: c.h:357
#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:454
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 522 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().

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

References pushf_write().

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

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

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

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

◆ encrypt_filter

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

Definition at line 225 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:115
static void mdc_free(void *priv)
Definition: pgp-encrypt.c:135
static int mdc_write(PushFilter *dst, void *priv, const uint8 *data, int len)
Definition: pgp-encrypt.c:106
static int mdc_init(PushFilter *dst, void *init_arg, void **priv_p)
Definition: pgp-encrypt.c:92

Definition at line 142 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:253
static void pkt_stream_free(void *priv)
Definition: pgp-encrypt.c:299
static int pkt_stream_init(PushFilter *next, void *init_arg, void **priv_p)
Definition: pgp-encrypt.c:240
static int pkt_stream_flush(PushFilter *next, void *priv)
Definition: pgp-encrypt.c:279

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