PostgreSQL Source Code  git master
pgp-pubenc.c File Reference
#include "postgres.h"
#include "px.h"
#include "pgp.h"
Include dependency graph for pgp-pubenc.c:

Go to the source code of this file.

Functions

static int pad_eme_pkcs1_v15 (uint8 *data, int data_len, int res_len, uint8 **res_p)
 
static int create_secmsg (PGP_Context *ctx, PGP_MPI **msg_p, int full_bytes)
 
static int encrypt_and_write_elgamal (PGP_Context *ctx, PGP_PubKey *pk, PushFilter *pkt)
 
static int encrypt_and_write_rsa (PGP_Context *ctx, PGP_PubKey *pk, PushFilter *pkt)
 
int pgp_write_pubenc_sesskey (PGP_Context *ctx, PushFilter *dst)
 

Function Documentation

◆ create_secmsg()

static int create_secmsg ( PGP_Context ctx,
PGP_MPI **  msg_p,
int  full_bytes 
)
static

Definition at line 88 of file pgp-pubenc.c.

References PGP_Context::cipher_algo, i, pad_eme_pkcs1_v15(), pgp_mpi_create(), px_alloc, px_free, px_memset(), PGP_Context::sess_key, and PGP_Context::sess_key_len.

Referenced by encrypt_and_write_elgamal(), and encrypt_and_write_rsa().

89 {
90  uint8 *secmsg;
91  int res,
92  i;
93  unsigned cksum = 0;
94  int klen = ctx->sess_key_len;
95  uint8 *padded = NULL;
96  PGP_MPI *m = NULL;
97 
98  /* calc checksum */
99  for (i = 0; i < klen; i++)
100  cksum += ctx->sess_key[i];
101 
102  /*
103  * create "secret message"
104  */
105  secmsg = px_alloc(klen + 3);
106  secmsg[0] = ctx->cipher_algo;
107  memcpy(secmsg + 1, ctx->sess_key, klen);
108  secmsg[klen + 1] = (cksum >> 8) & 0xFF;
109  secmsg[klen + 2] = cksum & 0xFF;
110 
111  /*
112  * now create a large integer of it
113  */
114  res = pad_eme_pkcs1_v15(secmsg, klen + 3, full_bytes, &padded);
115  if (res >= 0)
116  {
117  /* first byte will be 0x02 */
118  int full_bits = full_bytes * 8 - 6;
119 
120  res = pgp_mpi_create(padded, full_bits, &m);
121  }
122 
123  if (padded)
124  {
125  px_memset(padded, 0, full_bytes);
126  px_free(padded);
127  }
128  px_memset(secmsg, 0, klen + 3);
129  px_free(secmsg);
130 
131  if (res >= 0)
132  *msg_p = m;
133 
134  return res;
135 }
int cipher_algo
Definition: pgp.h:144
#define px_free(p)
Definition: px.h:46
unsigned char uint8
Definition: c.h:294
static int pad_eme_pkcs1_v15(uint8 *data, int data_len, int res_len, uint8 **res_p)
Definition: pgp-pubenc.c:40
uint8 sess_key[PGP_MAX_KEY]
Definition: pgp.h:171
Definition: pgp.h:179
unsigned sess_key_len
Definition: pgp.h:172
#define px_alloc(s)
Definition: px.h:44
int i
int pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi)
Definition: pgp-mpi.c:56
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134

◆ encrypt_and_write_elgamal()

static int encrypt_and_write_elgamal ( PGP_Context ctx,
PGP_PubKey pk,
PushFilter pkt 
)
static

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

References create_secmsg(), PGP_PubKey::elg, pgp_elgamal_encrypt(), pgp_mpi_free(), pgp_mpi_write(), and PGP_PubKey::pub.

Referenced by pgp_write_pubenc_sesskey().

139 {
140  int res;
141  PGP_MPI *m = NULL,
142  *c1 = NULL,
143  *c2 = NULL;
144 
145  /* create padded msg */
146  res = create_secmsg(ctx, &m, pk->pub.elg.p->bytes - 1);
147  if (res < 0)
148  goto err;
149 
150  /* encrypt it */
151  res = pgp_elgamal_encrypt(pk, m, &c1, &c2);
152  if (res < 0)
153  goto err;
154 
155  /* write out */
156  res = pgp_mpi_write(pkt, c1);
157  if (res < 0)
158  goto err;
159  res = pgp_mpi_write(pkt, c2);
160 
161 err:
162  pgp_mpi_free(m);
163  pgp_mpi_free(c1);
164  pgp_mpi_free(c2);
165  return res;
166 }
struct PGP_PubKey::@5::@7 elg
int pgp_mpi_write(PushFilter *dst, PGP_MPI *n)
Definition: pgp-mpi.c:105
static int create_secmsg(PGP_Context *ctx, PGP_MPI **msg_p, int full_bytes)
Definition: pgp-pubenc.c:88
int pgp_elgamal_encrypt(PGP_PubKey *pk, PGP_MPI *_m, PGP_MPI **c1_p, PGP_MPI **c2_p)
Definition: pgp.h:179
int pgp_mpi_free(PGP_MPI *mpi)
Definition: pgp-mpi.c:70
union PGP_PubKey::@5 pub

◆ encrypt_and_write_rsa()

static int encrypt_and_write_rsa ( PGP_Context ctx,
PGP_PubKey pk,
PushFilter pkt 
)
static

Definition at line 169 of file pgp-pubenc.c.

References create_secmsg(), pgp_mpi_free(), pgp_mpi_write(), pgp_rsa_encrypt(), PGP_PubKey::pub, and PGP_PubKey::rsa.

Referenced by pgp_write_pubenc_sesskey().

170 {
171  int res;
172  PGP_MPI *m = NULL,
173  *c = NULL;
174 
175  /* create padded msg */
176  res = create_secmsg(ctx, &m, pk->pub.rsa.n->bytes - 1);
177  if (res < 0)
178  goto err;
179 
180  /* encrypt it */
181  res = pgp_rsa_encrypt(pk, m, &c);
182  if (res < 0)
183  goto err;
184 
185  /* write out */
186  res = pgp_mpi_write(pkt, c);
187 
188 err:
189  pgp_mpi_free(m);
190  pgp_mpi_free(c);
191  return res;
192 }
int pgp_mpi_write(PushFilter *dst, PGP_MPI *n)
Definition: pgp-mpi.c:105
static int create_secmsg(PGP_Context *ctx, PGP_MPI **msg_p, int full_bytes)
Definition: pgp-pubenc.c:88
struct PGP_PubKey::@5::@8 rsa
char * c
int pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *_m, PGP_MPI **c_p)
Definition: pgp.h:179
int pgp_mpi_free(PGP_MPI *mpi)
Definition: pgp-mpi.c:70
union PGP_PubKey::@5 pub

◆ pad_eme_pkcs1_v15()

static int pad_eme_pkcs1_v15 ( uint8 data,
int  data_len,
int  res_len,
uint8 **  res_p 
)
static

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

References buf, pg_strong_random(), px_alloc, px_free, px_memset(), PXE_BUG, and PXE_NO_RANDOM.

Referenced by create_secmsg().

41 {
42 #ifdef HAVE_STRONG_RANDOM
43  uint8 *buf,
44  *p;
45  int pad_len = res_len - 2 - data_len;
46 
47  if (pad_len < 8)
48  return PXE_BUG;
49 
50  buf = px_alloc(res_len);
51  buf[0] = 0x02;
52 
53  if (!pg_strong_random((char *) buf + 1, pad_len))
54  {
55  px_free(buf);
56  return PXE_NO_RANDOM;
57  }
58 
59  /* pad must not contain zero bytes */
60  p = buf + 1;
61  while (p < buf + 1 + pad_len)
62  {
63  if (*p == 0)
64  {
65  if (!pg_strong_random((char *) p, 1))
66  {
67  px_memset(buf, 0, res_len);
68  px_free(buf);
69  break;
70  }
71  }
72  if (*p != 0)
73  p++;
74  }
75 
76  buf[pad_len + 1] = 0;
77  memcpy(buf + pad_len + 2, data, data_len);
78  *res_p = buf;
79 
80  return 0;
81 
82 #else
83  return PXE_NO_RANDOM;
84 #endif
85 }
#define px_free(p)
Definition: px.h:46
unsigned char uint8
Definition: c.h:294
bool pg_strong_random(void *buf, size_t len)
#define PXE_NO_RANDOM
Definition: px.h:78
static char * buf
Definition: pg_test_fsync.c:67
#define PXE_BUG
Definition: px.h:73
#define px_alloc(s)
Definition: px.h:44
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134

◆ pgp_write_pubenc_sesskey()

int pgp_write_pubenc_sesskey ( PGP_Context ctx,
PushFilter dst 
)

Definition at line 195 of file pgp-pubenc.c.

References PGP_PubKey::algo, encrypt_and_write_elgamal(), encrypt_and_write_rsa(), PGP_PubKey::key_id, pgp_create_pkt_writer(), PGP_PKT_PUBENCRYPTED_SESSKEY, PGP_PUB_ELG_ENCRYPT, PGP_PUB_RSA_ENCRYPT, PGP_PUB_RSA_ENCRYPT_SIGN, PGP_Context::pub_key, pushf_flush(), pushf_free(), pushf_write(), px_debug(), and PXE_BUG.

Referenced by pgp_encrypt().

196 {
197  int res;
198  PGP_PubKey *pk = ctx->pub_key;
199  uint8 ver = 3;
200  PushFilter *pkt = NULL;
201  uint8 algo;
202 
203  if (pk == NULL)
204  {
205  px_debug("no pubkey?\n");
206  return PXE_BUG;
207  }
208 
209  algo = pk->algo;
210 
211  /*
212  * now write packet
213  */
215  if (res < 0)
216  goto err;
217  res = pushf_write(pkt, &ver, 1);
218  if (res < 0)
219  goto err;
220  res = pushf_write(pkt, pk->key_id, 8);
221  if (res < 0)
222  goto err;
223  res = pushf_write(pkt, &algo, 1);
224  if (res < 0)
225  goto err;
226 
227  switch (algo)
228  {
229  case PGP_PUB_ELG_ENCRYPT:
230  res = encrypt_and_write_elgamal(ctx, pk, pkt);
231  break;
232  case PGP_PUB_RSA_ENCRYPT:
234  res = encrypt_and_write_rsa(ctx, pk, pkt);
235  break;
236  }
237  if (res < 0)
238  goto err;
239 
240  /*
241  * done, signal packet end
242  */
243  res = pushf_flush(pkt);
244 err:
245  if (pkt)
246  pushf_free(pkt);
247 
248  return res;
249 }
void pushf_free(PushFilter *mp)
Definition: mbuf.c:412
int pushf_flush(PushFilter *mp)
Definition: mbuf.c:516
unsigned char uint8
Definition: c.h:294
static int encrypt_and_write_rsa(PGP_Context *ctx, PGP_PubKey *pk, PushFilter *pkt)
Definition: pgp-pubenc.c:169
int pgp_create_pkt_writer(PushFilter *dst, int tag, PushFilter **res_p)
Definition: pgp-encrypt.c:315
static int encrypt_and_write_elgamal(PGP_Context *ctx, PGP_PubKey *pk, PushFilter *pkt)
Definition: pgp-pubenc.c:138
#define PXE_BUG
Definition: px.h:73
void px_debug(const char *fmt,...)
Definition: px.c:160
uint8 key_id[8]
Definition: pgp.h:235
uint8 algo
Definition: pgp.h:190
PGP_PubKey * pub_key
Definition: pgp.h:164
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456