PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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.

◆ MDC_DIGEST_LEN

#define MDC_DIGEST_LEN   20

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

◆ STREAM_BLOCK_SHIFT

#define STREAM_BLOCK_SHIFT   14

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

◆ STREAM_ID

#define STREAM_ID   0xE0

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

Function Documentation

◆ crlf_process()

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

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

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

References data, len, and pushf_write().

◆ encrypt_free()

static void encrypt_free ( void *  priv)
static

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

215{
216 struct EncStat *st = priv;
217
218 if (st->ciph)
219 pgp_cfb_free(st->ciph);
220 px_memset(st, 0, sizeof(*st));
221 pfree(st);
222}
void pfree(void *pointer)
Definition: mcxt.c:1524
void pgp_cfb_free(PGP_CFB *ctx)
Definition: pgp-cfb.c:83
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:123
PGP_CFB * ciph
Definition: pgp-encrypt.c:153

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

◆ encrypt_init()

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

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

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 = palloc0(sizeof(*st));
182 st->ciph = ciph;
183
184 *priv_p = st;
185 return ENCBUF;
186}
static int32 next
Definition: blutils.c:224
void * palloc0(Size size)
Definition: mcxt.c:1347
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 ENCBUF
Definition: pgp-encrypt.c:150
unsigned sess_key_len
Definition: pgp.h:172
int cipher_algo
Definition: pgp.h:144
int disable_mdc
Definition: pgp.h:147
uint8 sess_key[PGP_MAX_KEY]
Definition: pgp.h:171

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

◆ encrypt_process()

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

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

190{
191 int res;
192 struct EncStat *st = priv;
193 int avail = len;
194
195 while (avail > 0)
196 {
197 int tmplen = avail > ENCBUF ? ENCBUF : avail;
198
199 res = pgp_cfb_encrypt(st->ciph, data, tmplen, st->buf);
200 if (res < 0)
201 return res;
202
203 res = pushf_write(next, st->buf, tmplen);
204 if (res < 0)
205 return res;
206
207 data += tmplen;
208 avail -= tmplen;
209 }
210 return 0;
211}
int pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:252
uint8 buf[ENCBUF]
Definition: pgp-encrypt.c:154

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

◆ init_compress()

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

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

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

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

Referenced by pgp_encrypt().

◆ init_encdata_packet()

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

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

458{
459 int res;
460 int tag;
461
462 if (ctx->disable_mdc)
464 else
466
467 res = write_tag_only(dst, tag);
468 if (res < 0)
469 return res;
470
471 return pushf_create(pf_res, &pkt_stream_filter, ctx, dst);
472}
@ PGP_PKT_SYMENCRYPTED_DATA
Definition: pgp.h:54
@ PGP_PKT_SYMENCRYPTED_DATA_MDC
Definition: pgp.h:61

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

Referenced by pgp_encrypt().

◆ init_litdata_packet()

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

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

375{
376 int res;
377 int hdrlen;
378 uint8 hdr[6];
379 uint32 t;
380 PushFilter *pkt;
381 int type;
382
383 /*
384 * Create header
385 */
386
387 if (ctx->text_mode)
388 type = ctx->unicode_mode ? 'u' : 't';
389 else
390 type = 'b';
391
392 /*
393 * Store the creation time into packet. The goal is to have as few known
394 * bytes as possible.
395 */
396 t = (uint32) time(NULL);
397
398 hdr[0] = type;
399 hdr[1] = 0;
400 hdr[2] = (t >> 24) & 255;
401 hdr[3] = (t >> 16) & 255;
402 hdr[4] = (t >> 8) & 255;
403 hdr[5] = t & 255;
404 hdrlen = 6;
405
407 if (res < 0)
408 return res;
409
410 res = pushf_create(&pkt, &pkt_stream_filter, ctx, dst);
411 if (res < 0)
412 return res;
413
414 res = pushf_write(pkt, hdr, hdrlen);
415 if (res < 0)
416 {
417 pushf_free(pkt);
418 return res;
419 }
420
421 *pf_res = pkt;
422 return 0;
423}
uint32_t uint32
Definition: c.h:502
@ PGP_PKT_LITERAL_DATA
Definition: pgp.h:56
int text_mode
Definition: pgp.h:149
int unicode_mode
Definition: pgp.h:151

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

Referenced by pgp_encrypt().

◆ init_s2k_key()

static int init_s2k_key ( PGP_Context ctx)
static

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

563{
564 int res;
565
566 if (ctx->s2k_cipher_algo < 0)
567 ctx->s2k_cipher_algo = ctx->cipher_algo;
568
569 res = pgp_s2k_fill(&ctx->s2k, ctx->s2k_mode, ctx->s2k_digest_algo, ctx->s2k_count);
570 if (res < 0)
571 return res;
572
573 return pgp_s2k_process(&ctx->s2k, ctx->s2k_cipher_algo,
574 ctx->sym_key, ctx->sym_key_len);
575}
int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int key_len)
Definition: pgp-s2k.c:279
int pgp_s2k_fill(PGP_S2K *s2k, int mode, int digest_algo, int count)
Definition: pgp-s2k.c:223
int s2k_mode
Definition: pgp.h:140
PGP_S2K s2k
Definition: pgp.h:139
int s2k_cipher_algo
Definition: pgp.h:143
int s2k_count
Definition: pgp.h:141
int sym_key_len
Definition: pgp.h:166
int s2k_digest_algo
Definition: pgp.h:142
const uint8 * sym_key
Definition: pgp.h:165

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().

◆ init_sess_key()

static int init_sess_key ( PGP_Context ctx)
static

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

579{
580 if (ctx->use_sess_key || ctx->pub_key)
581 {
583 if (!pg_strong_random(ctx->sess_key, ctx->sess_key_len))
584 return PXE_NO_RANDOM;
585 }
586 else
587 {
588 ctx->sess_key_len = ctx->s2k.key_len;
589 memcpy(ctx->sess_key, ctx->s2k.key, ctx->s2k.key_len);
590 }
591
592 return 0;
593}
int pgp_get_cipher_key_size(int code)
Definition: pgp.c:137
bool pg_strong_random(void *buf, size_t len)
#define PXE_NO_RANDOM
Definition: px.h:63
PGP_PubKey * pub_key
Definition: pgp.h:164
int use_sess_key
Definition: pgp.h:148
uint8 key_len
Definition: pgp.h:130
uint8 key[PGP_MAX_KEY]
Definition: pgp.h:129

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().

◆ mdc_flush()

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

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

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 MDC_DIGEST_LEN
Definition: pgp-encrypt.c:40
#define px_md_finish(md, buf)
Definition: px.h:206
#define px_md_update(md, data, dlen)
Definition: px.h:205
Definition: px.h:108

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

◆ mdc_free()

static void mdc_free ( void *  priv)
static

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

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

References px_md_free.

◆ mdc_init()

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

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

93{
94 int res;
95 PX_MD *md;
96
98 if (res < 0)
99 return res;
100
101 *priv_p = md;
102 return 0;
103}
int pgp_load_digest(int code, PX_MD **res)
Definition: pgp.c:173
@ PGP_DIGEST_SHA1
Definition: pgp.h:101

References PGP_DIGEST_SHA1, and pgp_load_digest().

◆ 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.

107{
108 PX_MD *md = priv;
109
110 px_md_update(md, data, len);
111 return pushf_write(dst, data, len);
112}

References data, len, pushf_write(), and px_md_update.

◆ pgp_create_pkt_writer()

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

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

312{
313 int res;
314
315 res = write_tag_only(dst, tag);
316 if (res < 0)
317 return res;
318
319 return pushf_create(res_p, &pkt_stream_filter, NULL, dst);
320}

References pkt_stream_filter, pushf_create(), and write_tag_only().

Referenced by pgp_write_pubenc_sesskey().

◆ pgp_encrypt()

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

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

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

References buf, PGP_Context::compress_algo, PGP_Context::compress_level, PGP_Context::convert_crlf, crlf_filter, PGP_Context::disable_mdc, encrypt_filter, init_compress(), init_encdata_packet(), init_litdata_packet(), init_s2k_key(), init_sess_key(), len, mbuf_avail(), mbuf_grab(), mdc_filter, 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().

◆ pkt_stream_flush()

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

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

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

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

◆ pkt_stream_free()

static void pkt_stream_free ( void *  priv)
static

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

299{
300 struct PktStreamStat *st = priv;
301
302 px_memset(st, 0, sizeof(*st));
303 pfree(st);
304}

References pfree(), and px_memset().

◆ pkt_stream_init()

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

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

240{
241 struct PktStreamStat *st;
242
243 st = palloc(sizeof(*st));
244 st->final_done = 0;
245 st->pkt_block = 1 << STREAM_BLOCK_SHIFT;
246 *priv_p = st;
247
248 return st->pkt_block;
249}
void * palloc(Size size)
Definition: mcxt.c:1317
#define STREAM_BLOCK_SHIFT
Definition: pgp-encrypt.c:42

References PktStreamStat::final_done, palloc(), PktStreamStat::pkt_block, and STREAM_BLOCK_SHIFT.

◆ pkt_stream_process()

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

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

253{
254 int res;
255 uint8 hdr[8];
256 uint8 *h = hdr;
257 struct PktStreamStat *st = priv;
258
259 if (st->final_done)
260 return PXE_BUG;
261
262 if (len == st->pkt_block)
264 else
265 {
266 h = render_newlen(h, len);
267 st->final_done = 1;
268 }
269
270 res = pushf_write(next, hdr, h - hdr);
271 if (res < 0)
272 return res;
273
274 return pushf_write(next, data, len);
275}
#define STREAM_ID
Definition: pgp-encrypt.c:41
#define PXE_BUG
Definition: px.h:58

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

◆ render_newlen()

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

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

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}

References len.

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

◆ symencrypt_sesskey()

static int symencrypt_sesskey ( PGP_Context ctx,
uint8 dst 
)
static

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

502{
503 int res;
504 PGP_CFB *cfb;
505 uint8 algo = ctx->cipher_algo;
506
507 res = pgp_cfb_create(&cfb, ctx->s2k_cipher_algo,
508 ctx->s2k.key, ctx->s2k.key_len, 0, NULL);
509 if (res < 0)
510 return res;
511
512 pgp_cfb_encrypt(cfb, &algo, 1, dst);
513 pgp_cfb_encrypt(cfb, ctx->sess_key, ctx->sess_key_len, dst + 1);
514
515 pgp_cfb_free(cfb);
516 return ctx->sess_key_len + 1;
517}

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().

◆ write_normal_header()

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

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

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}

References len, pushf_write(), and render_newlen().

Referenced by write_symenc_sesskey().

◆ write_prefix()

static int write_prefix ( PGP_Context ctx,
PushFilter dst 
)
static

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

479{
480 uint8 prefix[PGP_MAX_BLOCK + 2];
481 int res,
482 bs;
483
485 if (!pg_strong_random(prefix, bs))
486 return PXE_NO_RANDOM;
487
488 prefix[bs + 0] = prefix[bs - 2];
489 prefix[bs + 1] = prefix[bs - 1];
490
491 res = pushf_write(dst, prefix, bs + 2);
492 px_memset(prefix, 0, bs + 2);
493 return res < 0 ? res : 0;
494}
int pgp_get_cipher_block_size(int code)
Definition: pgp.c:147
#define PGP_MAX_BLOCK
Definition: pgp.h:113

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().

◆ write_symenc_sesskey()

static int write_symenc_sesskey ( PGP_Context ctx,
PushFilter dst 
)
static

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

522{
523 uint8 pkt[256];
524 int pktlen;
525 int res;
526 uint8 *p = pkt;
527
528 *p++ = 4; /* 5.3 - version number */
529 *p++ = ctx->s2k_cipher_algo;
530
531 *p++ = ctx->s2k.mode;
532 *p++ = ctx->s2k.digest_algo;
533 if (ctx->s2k.mode > 0)
534 {
535 memcpy(p, ctx->s2k.salt, 8);
536 p += 8;
537 }
538 if (ctx->s2k.mode == 3)
539 *p++ = ctx->s2k.iter;
540
541 if (ctx->use_sess_key)
542 {
543 res = symencrypt_sesskey(ctx, p);
544 if (res < 0)
545 return res;
546 p += res;
547 }
548
549 pktlen = p - pkt;
551 if (res >= 0)
552 res = pushf_write(dst, pkt, pktlen);
553
554 px_memset(pkt, 0, pktlen);
555 return res;
556}
static int write_normal_header(PushFilter *dst, int tag, int len)
Definition: pgp-encrypt.c:76
static int symencrypt_sesskey(PGP_Context *ctx, uint8 *dst)
Definition: pgp-encrypt.c:501
@ PGP_PKT_SYMENCRYPTED_SESSKEY
Definition: pgp.h:49
uint8 digest_algo
Definition: pgp.h:125
uint8 mode
Definition: pgp.h:124
uint8 iter
Definition: pgp.h:127
uint8 salt[8]
Definition: pgp.h:126

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().

◆ write_tag_only()

static int write_tag_only ( PushFilter dst,
int  tag 
)
static

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

69{
70 uint8 hdr = 0xC0 | tag;
71
72 return pushf_write(dst, &hdr, 1);
73}

References pushf_write().

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

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

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

Referenced by pgp_encrypt().

◆ encrypt_filter

const PushFilterOps encrypt_filter
static
Initial value:
= {
}
static void encrypt_free(void *priv)
Definition: pgp-encrypt.c:214
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:189

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

Referenced by pgp_encrypt().

◆ mdc_filter

const PushFilterOps mdc_filter
static
Initial value:
= {
}
static int mdc_init(PushFilter *dst, void *init_arg, void **priv_p)
Definition: pgp-encrypt.c:92
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_flush(PushFilter *dst, void *priv)
Definition: pgp-encrypt.c:115

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

Referenced by pgp_encrypt().

◆ pkt_stream_filter

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

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

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