PostgreSQL Source Code: contrib/pgcrypto/pgp-encrypt.c Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

33

35

38#include "px.h"

39

40#define MDC_DIGEST_LEN 20

41#define STREAM_ID 0xE0

42#define STREAM_BLOCK_SHIFT 14

43

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}

66

67static int

69{

70 uint8 hdr = 0xC0 | tag;

71

73}

74

75static int

77{

80

81 *h++ = 0xC0 | tag;

84}

85

86

87

88

89

90

91static int

93{

94 int res;

96

98 if (res < 0)

99 return res;

100

101 *priv_p = md;

102 return 0;

103}

104

105static int

107{

108 PX_MD *md = priv;

109

112}

113

114static int

116{

117 int res;

119 PX_MD *md = priv;

120

121

122

123

124 pkt[0] = 0xD3;

125 pkt[1] = 0x14;

128

131 return res;

132}

133

134static void

136{

137 PX_MD *md = priv;

138

140}

141

144};

145

146

147

148

149

150#define ENCBUF 8192

152{

155};

156

157static int

159{

163 int resync = 1;

164 int res;

165

166

168 {

170

171 resync = 0;

173 if (res < 0)

174 return res;

175 }

178 if (res < 0)

179 return res;

180

181 st = palloc0(sizeof(*st));

183

184 *priv_p = st;

186}

187

188static int

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

200 if (res < 0)

201 return res;

202

204 if (res < 0)

205 return res;

206

207 data += tmplen;

208 avail -= tmplen;

209 }

210 return 0;

211}

212

213static void

215{

216 struct EncStat *st = priv;

217

218 if (st->ciph)

222}

223

226};

227

228

229

230

231

233{

236};

237

238static int

240{

242

243 st = palloc(sizeof(*st));

246 *priv_p = st;

247

249}

250

251static int

253{

254 int res;

258

261

264 else

265 {

268 }

269

271 if (res < 0)

272 return res;

273

275}

276

277static int

279{

280 int res;

284

285

287 {

290 if (res < 0)

291 return res;

293 }

294 return 0;

295}

296

297static void

299{

301

304}

305

308};

309

310int

312{

313 int res;

314

316 if (res < 0)

317 return res;

318

320}

321

322

323

324

325

326static int

328{

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

345 res = 0;

346 if (line_len > 0)

347 {

349 if (res < 0)

350 break;

351 p1 += line_len;

352 }

353

354

355 while (p1 < data_end && *p1 == '\n')

356 {

358 if (res < 0)

359 break;

360 p1++;

361 }

362 }

363 return res;

364}

365

368};

369

370

371

372

373static int

375{

376 int res;

377 int hdrlen;

382

383

384

385

386

389 else

391

392

393

394

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

411 if (res < 0)

412 return res;

413

415 if (res < 0)

416 {

418 return res;

419 }

420

421 *pf_res = pkt;

422 return 0;

423}

424

425

426

427

428static int

430{

431 int res;

434

436 if (res < 0)

437 return res;

438

440 if (res < 0)

441 return res;

442

444 if (res >= 0)

446

447 if (res < 0)

449

450 return res;

451}

452

453

454

455

456static int

458{

459 int res;

460 int tag;

461

464 else

466

468 if (res < 0)

469 return res;

470

472}

473

474

475

476

477static int

479{

481 int res,

482 bs;

483

487

488 prefix[bs + 0] = prefix[bs - 2];

489 prefix[bs + 1] = prefix[bs - 1];

490

493 return res < 0 ? res : 0;

494}

495

496

497

498

499

500static int

502{

503 int res;

506

509 if (res < 0)

510 return res;

511

514

517}

518

519

520static int

522{

524 int pktlen;

525 int res;

527

528 *p++ = 4;

530

534 {

535 memcpy(p, ctx->s2k.salt, 8);

536 p += 8;

537 }

540

542 {

544 if (res < 0)

545 return res;

546 p += res;

547 }

548

549 pktlen = p - pkt;

551 if (res >= 0)

553

555 return res;

556}

557

558

559

560

561static int

563{

564 int res;

565

568

570 if (res < 0)

571 return res;

572

575}

576

577static int

579{

581 {

585 }

586 else

587 {

590 }

591

592 return 0;

593}

594

595

596

597

598int

600{

601 int res;

605 *pf_tmp;

606

607

608

609

612

613

615 if (res < 0)

616 goto out;

617

618

619

620

622 {

624 if (res < 0)

625 goto out;

626 }

627

629 if (res < 0)

630 goto out;

631

632

633

634

637 else

639 if (res < 0)

640 goto out;

641

642

644 if (res < 0)

645 goto out;

646 pf = pf_tmp;

647

648

650 if (res < 0)

651 goto out;

652 pf = pf_tmp;

653

654

656 {

658 if (res < 0)

659 goto out;

660 pf = pf_tmp;

661 }

662

663

665 if (res < 0)

666 goto out;

667

668

670 {

672 if (res < 0)

673 goto out;

674 pf = pf_tmp;

675 }

676

677

679 if (res < 0)

680 goto out;

681 pf = pf_tmp;

682

683

684

686 {

688 if (res < 0)

689 goto out;

690 pf = pf_tmp;

691 }

692

693

694

695

696

699 if (res >= 0)

701out:

703 return res;

704}

int pushf_write(PushFilter *mp, const uint8 *data, int len)

int mbuf_avail(MBuf *mbuf)

int pushf_create(PushFilter **mp_p, const PushFilterOps *op, void *init_arg, PushFilter *next)

int pushf_create_mbuf_writer(PushFilter **res, MBuf *dst)

void pushf_free_all(PushFilter *mp)

int pushf_flush(PushFilter *mp)

int mbuf_grab(MBuf *mbuf, int len, uint8 **data_p)

void pushf_free(PushFilter *mp)

void pfree(void *pointer)

void * palloc0(Size size)

int pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)

void pgp_cfb_free(PGP_CFB *ctx)

int pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len, int resync, uint8 *iv)

int pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst)

static int write_symenc_sesskey(PGP_Context *ctx, PushFilter *dst)

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

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

#define STREAM_BLOCK_SHIFT

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

static void mdc_free(void *priv)

static int write_prefix(PGP_Context *ctx, PushFilter *dst)

static const PushFilterOps crlf_filter

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

static void encrypt_free(void *priv)

static int write_tag_only(PushFilter *dst, int tag)

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

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

static int symencrypt_sesskey(PGP_Context *ctx, uint8 *dst)

static void pkt_stream_free(void *priv)

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

static const PushFilterOps encrypt_filter

static const PushFilterOps pkt_stream_filter

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

static int init_s2k_key(PGP_Context *ctx)

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

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

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

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

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

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

static int init_sess_key(PGP_Context *ctx)

static const PushFilterOps mdc_filter

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)

int pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst)

int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int key_len)

int pgp_s2k_fill(PGP_S2K *s2k, int mode, int digest_algo, int count)

int pgp_get_cipher_block_size(int code)

int pgp_get_cipher_key_size(int code)

int pgp_load_digest(int code, PX_MD **res)

@ PGP_PKT_SYMENCRYPTED_DATA

@ PGP_PKT_COMPRESSED_DATA

@ PGP_PKT_SYMENCRYPTED_SESSKEY

@ PGP_PKT_SYMENCRYPTED_DATA_MDC

bool pg_strong_random(void *buf, size_t len)

void px_memset(void *ptr, int c, size_t len)

#define px_md_finish(md, buf)

#define PXE_ARGUMENT_ERROR

#define px_md_update(md, data, dlen)

uint8 sess_key[PGP_MAX_KEY]