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

Go to the source code of this file.

Functions

static mpz_tmp_new ()
 
static void mp_clear_free (mpz_t *a)
 
static int mp_px_rand (uint32 bits, mpz_t *res)
 
static void mp_modmul (mpz_t *a, mpz_t *b, mpz_t *p, mpz_t *res)
 
static mpz_tmpi_to_bn (PGP_MPI *n)
 
static PGP_MPIbn_to_mpi (mpz_t *bn)
 
static int decide_k_bits (int p_bits)
 
int pgp_elgamal_encrypt (PGP_PubKey *pk, PGP_MPI *_m, PGP_MPI **c1_p, PGP_MPI **c2_p)
 
int pgp_elgamal_decrypt (PGP_PubKey *pk, PGP_MPI *_c1, PGP_MPI *_c2, PGP_MPI **msg_p)
 
int pgp_rsa_encrypt (PGP_PubKey *pk, PGP_MPI *_m, PGP_MPI **c_p)
 
int pgp_rsa_decrypt (PGP_PubKey *pk, PGP_MPI *_c, PGP_MPI **m_p)
 

Function Documentation

◆ bn_to_mpi()

static PGP_MPI* bn_to_mpi ( mpz_t bn)
static

Definition at line 116 of file pgp-mpi-internal.c.

References generate_unaccent_rules::bytes(), PGP_MPI::bytes, PGP_MPI::data, mp_int_count_bits(), mp_int_to_unsigned(), pgp_mpi_alloc(), pgp_mpi_free(), and px_debug().

Referenced by pgp_elgamal_decrypt(), pgp_elgamal_encrypt(), pgp_rsa_decrypt(), and pgp_rsa_encrypt().

117 {
118  int res;
119  PGP_MPI *n;
120  int bytes;
121 
122  res = pgp_mpi_alloc(mp_int_count_bits(bn), &n);
123  if (res < 0)
124  return NULL;
125 
126  bytes = (mp_int_count_bits(bn) + 7) / 8;
127  if (bytes != n->bytes)
128  {
129  px_debug("bn_to_mpi: bignum conversion failed: bn=%d, mpi=%d",
130  bytes, n->bytes);
131  pgp_mpi_free(n);
132  return NULL;
133  }
134  mp_int_to_unsigned(bn, n->data, n->bytes);
135  return n;
136 }
uint8 * data
Definition: pgp.h:181
def bytes(source, encoding='ascii', errors='strict')
mp_result mp_int_to_unsigned(mp_int z, unsigned char *buf, int limit)
Definition: imath.c:2137
mp_result mp_int_count_bits(mp_int z)
Definition: imath.c:2038
int bytes
Definition: pgp.h:183
int pgp_mpi_alloc(int bits, PGP_MPI **mpi)
Definition: pgp-mpi.c:37
Definition: pgp.h:179
int pgp_mpi_free(PGP_MPI *mpi)
Definition: pgp-mpi.c:70
void px_debug(const char *fmt,...)
Definition: px.c:152

◆ decide_k_bits()

static int decide_k_bits ( int  p_bits)
static

Definition at line 154 of file pgp-mpi-internal.c.

Referenced by pgp_elgamal_encrypt().

155 {
156  if (p_bits <= 5120)
157  return p_bits / 10 + 160;
158  else
159  return (p_bits / 8 + 200) * 3 / 2;
160 }

◆ mp_clear_free()

static void mp_clear_free ( mpz_t a)
static

Definition at line 47 of file pgp-mpi-internal.c.

References mp_int_free().

Referenced by mp_modmul(), mpi_to_bn(), pgp_elgamal_decrypt(), pgp_elgamal_encrypt(), pgp_rsa_decrypt(), and pgp_rsa_encrypt().

48 {
49  if (!a)
50  return;
51  /* fixme: no clear? */
52  mp_int_free(a);
53 }
void mp_int_free(mp_int z)
Definition: imath.c:602

◆ mp_modmul()

static void mp_modmul ( mpz_t a,
mpz_t b,
mpz_t p,
mpz_t res 
)
static

Definition at line 87 of file pgp-mpi-internal.c.

References mp_clear_free(), mp_int_mod(), mp_int_mul(), and mp_new().

Referenced by pgp_elgamal_decrypt(), and pgp_elgamal_encrypt().

88 {
89  mpz_t *tmp = mp_new();
90 
91  mp_int_mul(a, b, tmp);
92  mp_int_mod(tmp, p, res);
93  mp_clear_free(tmp);
94 }
mp_result mp_int_mul(mp_int a, mp_int b, mp_int c)
Definition: imath.c:857
Definition: imath.h:52
mp_result mp_int_mod(mp_int a, mp_int m, mp_int c)
Definition: imath.c:1122
static mpz_t * mp_new()
static void mp_clear_free(mpz_t *a)

◆ mp_new()

static mpz_t* mp_new ( )
static

Definition at line 38 of file pgp-mpi-internal.c.

References mp_int_alloc(), and mp_int_init_size().

Referenced by mp_modmul(), mpi_to_bn(), pgp_elgamal_decrypt(), pgp_elgamal_encrypt(), pgp_rsa_decrypt(), and pgp_rsa_encrypt().

39 {
40  mpz_t *mp = mp_int_alloc();
41 
42  mp_int_init_size(mp, 256);
43  return mp;
44 }
Definition: imath.h:52
mp_result mp_int_init_size(mp_int z, mp_size prec)
Definition: imath.c:490
mp_int mp_int_alloc(void)
Definition: imath.c:479

◆ mp_px_rand()

static int mp_px_rand ( uint32  bits,
mpz_t res 
)
static

Definition at line 57 of file pgp-mpi-internal.c.

References buf, generate_unaccent_rules::bytes(), mp_int_read_unsigned(), pg_strong_random(), px_alloc, px_free, and PXE_NO_RANDOM.

Referenced by pgp_elgamal_encrypt().

58 {
59  unsigned bytes = (bits + 7) / 8;
60  int last_bits = bits & 7;
61  uint8 *buf;
62 
63  buf = px_alloc(bytes);
64  if (!pg_strong_random(buf, bytes))
65  {
66  px_free(buf);
67  return PXE_NO_RANDOM;
68  }
69 
70  /* clear unnecessary bits and set last bit to one */
71  if (last_bits)
72  {
73  buf[0] >>= 8 - last_bits;
74  buf[0] |= 1 << (last_bits - 1);
75  }
76  else
77  buf[0] |= 1 << 7;
78 
79  mp_int_read_unsigned(res, buf, bytes);
80 
81  px_free(buf);
82 
83  return 0;
84 }
mp_result mp_int_read_unsigned(mp_int z, unsigned char *buf, int len)
Definition: imath.c:2147
def bytes(source, encoding='ascii', errors='strict')
#define px_free(p)
Definition: px.h:46
unsigned char uint8
Definition: c.h:357
#define PXE_NO_RANDOM
Definition: px.h:75
static char * buf
Definition: pg_test_fsync.c:67
bool pg_strong_random(void *buf, size_t len)
#define px_alloc(s)
Definition: px.h:44

◆ mpi_to_bn()

static mpz_t* mpi_to_bn ( PGP_MPI n)
static

Definition at line 97 of file pgp-mpi-internal.c.

References PGP_MPI::bits, PGP_MPI::bytes, PGP_MPI::data, mp_clear_free(), mp_int_count_bits(), mp_int_read_unsigned(), mp_new(), and px_debug().

Referenced by pgp_elgamal_decrypt(), pgp_elgamal_encrypt(), pgp_rsa_decrypt(), and pgp_rsa_encrypt().

98 {
99  mpz_t *bn = mp_new();
100 
101  mp_int_read_unsigned(bn, n->data, n->bytes);
102 
103  if (!bn)
104  return NULL;
105  if (mp_int_count_bits(bn) != n->bits)
106  {
107  px_debug("mpi_to_bn: bignum conversion failed: mpi=%d, bn=%d",
108  n->bits, mp_int_count_bits(bn));
109  mp_clear_free(bn);
110  return NULL;
111  }
112  return bn;
113 }
mp_result mp_int_read_unsigned(mp_int z, unsigned char *buf, int len)
Definition: imath.c:2147
uint8 * data
Definition: pgp.h:181
Definition: imath.h:52
mp_result mp_int_count_bits(mp_int z)
Definition: imath.c:2038
int bytes
Definition: pgp.h:183
static mpz_t * mp_new()
void px_debug(const char *fmt,...)
Definition: px.c:152
static void mp_clear_free(mpz_t *a)
int bits
Definition: pgp.h:182

◆ pgp_elgamal_decrypt()

int pgp_elgamal_decrypt ( PGP_PubKey pk,
PGP_MPI _c1,
PGP_MPI _c2,
PGP_MPI **  msg_p 
)

Definition at line 213 of file pgp-mpi-internal.c.

Referenced by decrypt_elgamal().

215 {
216  int res = PXE_PGP_MATH_FAILED;
217  mpz_t *c1 = mpi_to_bn(_c1);
218  mpz_t *c2 = mpi_to_bn(_c2);
219  mpz_t *p = mpi_to_bn(pk->pub.elg.p);
220  mpz_t *x = mpi_to_bn(pk->sec.elg.x);
221  mpz_t *c1x = mp_new();
222  mpz_t *div = mp_new();
223  mpz_t *m = mp_new();
224 
225  if (!c1 || !c2 || !p || !x || !c1x || !div || !m)
226  goto err;
227 
228  /*
229  * m = c2 / (c1^x)
230  */
231  mp_int_exptmod(c1, x, p, c1x);
232  mp_int_invmod(c1x, p, div);
233  mp_modmul(c2, div, p, m);
234 
235  /* result */
236  *msg_p = bn_to_mpi(m);
237  if (*msg_p)
238  res = 0;
239 err:
240  mp_clear_free(m);
241  mp_clear_free(div);
242  mp_clear_free(c1x);
243  mp_clear_free(x);
244  mp_clear_free(p);
245  mp_clear_free(c2);
246  mp_clear_free(c1);
247  return res;
248 }
mp_result mp_int_invmod(mp_int a, mp_int m, mp_int c)
Definition: imath.c:1474
struct PGP_PubKey::@5::@7 elg
Definition: imath.h:52
static mpz_t * mpi_to_bn(PGP_MPI *n)
static mpz_t * mp_new()
mp_result mp_int_exptmod(mp_int a, mp_int b, mp_int m, mp_int c)
Definition: imath.c:1369
static PGP_MPI * bn_to_mpi(mpz_t *bn)
union PGP_PubKey::@6 sec
static void mp_modmul(mpz_t *a, mpz_t *b, mpz_t *p, mpz_t *res)
union PGP_PubKey::@5 pub
#define PXE_PGP_MATH_FAILED
Definition: px.h:87
static void mp_clear_free(mpz_t *a)

◆ pgp_elgamal_encrypt()

int pgp_elgamal_encrypt ( PGP_PubKey pk,
PGP_MPI _m,
PGP_MPI **  c1_p,
PGP_MPI **  c2_p 
)

Definition at line 163 of file pgp-mpi-internal.c.

Referenced by encrypt_and_write_elgamal().

165 {
166  int res = PXE_PGP_MATH_FAILED;
167  int k_bits;
168  mpz_t *m = mpi_to_bn(_m);
169  mpz_t *p = mpi_to_bn(pk->pub.elg.p);
170  mpz_t *g = mpi_to_bn(pk->pub.elg.g);
171  mpz_t *y = mpi_to_bn(pk->pub.elg.y);
172  mpz_t *k = mp_new();
173  mpz_t *yk = mp_new();
174  mpz_t *c1 = mp_new();
175  mpz_t *c2 = mp_new();
176 
177  if (!m || !p || !g || !y || !k || !yk || !c1 || !c2)
178  goto err;
179 
180  /*
181  * generate k
182  */
183  k_bits = decide_k_bits(mp_int_count_bits(p));
184  res = mp_px_rand(k_bits, k);
185  if (res < 0)
186  return res;
187 
188  /*
189  * c1 = g^k c2 = m * y^k
190  */
191  mp_int_exptmod(g, k, p, c1);
192  mp_int_exptmod(y, k, p, yk);
193  mp_modmul(m, yk, p, c2);
194 
195  /* result */
196  *c1_p = bn_to_mpi(c1);
197  *c2_p = bn_to_mpi(c2);
198  if (*c1_p && *c2_p)
199  res = 0;
200 err:
201  mp_clear_free(c2);
202  mp_clear_free(c1);
203  mp_clear_free(yk);
204  mp_clear_free(k);
205  mp_clear_free(y);
206  mp_clear_free(g);
207  mp_clear_free(p);
208  mp_clear_free(m);
209  return res;
210 }
struct PGP_PubKey::@5::@7 elg
Definition: imath.h:52
mp_result mp_int_count_bits(mp_int z)
Definition: imath.c:2038
static mpz_t * mpi_to_bn(PGP_MPI *n)
static mpz_t * mp_new()
mp_result mp_int_exptmod(mp_int a, mp_int b, mp_int m, mp_int c)
Definition: imath.c:1369
static int decide_k_bits(int p_bits)
static PGP_MPI * bn_to_mpi(mpz_t *bn)
static int mp_px_rand(uint32 bits, mpz_t *res)
static void mp_modmul(mpz_t *a, mpz_t *b, mpz_t *p, mpz_t *res)
union PGP_PubKey::@5 pub
#define PXE_PGP_MATH_FAILED
Definition: px.h:87
static void mp_clear_free(mpz_t *a)

◆ pgp_rsa_decrypt()

int pgp_rsa_decrypt ( PGP_PubKey pk,
PGP_MPI _c,
PGP_MPI **  m_p 
)

Definition at line 279 of file pgp-mpi-internal.c.

Referenced by decrypt_rsa().

280 {
281  int res = PXE_PGP_MATH_FAILED;
282  mpz_t *c = mpi_to_bn(_c);
283  mpz_t *d = mpi_to_bn(pk->sec.rsa.d);
284  mpz_t *n = mpi_to_bn(pk->pub.rsa.n);
285  mpz_t *m = mp_new();
286 
287  if (!m || !d || !n || !c)
288  goto err;
289 
290  /*
291  * m = c ^ d
292  */
293  mp_int_exptmod(c, d, n, m);
294 
295  *m_p = bn_to_mpi(m);
296  if (*m_p)
297  res = 0;
298 err:
299  mp_clear_free(m);
300  mp_clear_free(n);
301  mp_clear_free(d);
302  mp_clear_free(c);
303  return res;
304 }
Definition: imath.h:52
static mpz_t * mpi_to_bn(PGP_MPI *n)
struct PGP_PubKey::@5::@8 rsa
static mpz_t * mp_new()
mp_result mp_int_exptmod(mp_int a, mp_int b, mp_int m, mp_int c)
Definition: imath.c:1369
char * c
static PGP_MPI * bn_to_mpi(mpz_t *bn)
union PGP_PubKey::@6 sec
union PGP_PubKey::@5 pub
#define PXE_PGP_MATH_FAILED
Definition: px.h:87
static void mp_clear_free(mpz_t *a)

◆ pgp_rsa_encrypt()

int pgp_rsa_encrypt ( PGP_PubKey pk,
PGP_MPI _m,
PGP_MPI **  c_p 
)

Definition at line 251 of file pgp-mpi-internal.c.

Referenced by encrypt_and_write_rsa().

252 {
253  int res = PXE_PGP_MATH_FAILED;
254  mpz_t *m = mpi_to_bn(_m);
255  mpz_t *e = mpi_to_bn(pk->pub.rsa.e);
256  mpz_t *n = mpi_to_bn(pk->pub.rsa.n);
257  mpz_t *c = mp_new();
258 
259  if (!m || !e || !n || !c)
260  goto err;
261 
262  /*
263  * c = m ^ e
264  */
265  mp_int_exptmod(m, e, n, c);
266 
267  *c_p = bn_to_mpi(c);
268  if (*c_p)
269  res = 0;
270 err:
271  mp_clear_free(c);
272  mp_clear_free(n);
273  mp_clear_free(e);
274  mp_clear_free(m);
275  return res;
276 }
Definition: imath.h:52
static mpz_t * mpi_to_bn(PGP_MPI *n)
struct PGP_PubKey::@5::@8 rsa
static mpz_t * mp_new()
mp_result mp_int_exptmod(mp_int a, mp_int b, mp_int m, mp_int c)
Definition: imath.c:1369
char * c
static PGP_MPI * bn_to_mpi(mpz_t *bn)
union PGP_PubKey::@5 pub
#define PXE_PGP_MATH_FAILED
Definition: px.h:87
e
Definition: preproc-init.c:82
static void mp_clear_free(mpz_t *a)