PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgp-mpi-internal.c
Go to the documentation of this file.
1 /*
2  * pgp-mpi-internal.c
3  * OpenPGP MPI functions.
4  *
5  * Copyright (c) 2005 Marko Kreen
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * contrib/pgcrypto/pgp-mpi-internal.c
30  */
31 #include "postgres.h"
32 
33 #include "imath.h"
34 
35 #include "px.h"
36 #include "pgp.h"
37 
38 static mpz_t *
40 {
41  mpz_t *mp = mp_int_alloc();
42 
43  mp_int_init_size(mp, 256);
44  return mp;
45 }
46 
47 static void
49 {
50  if (!a)
51  return;
52  /* fixme: no clear? */
53  mp_int_free(a);
54 }
55 
56 
57 static int
58 mp_px_rand(uint32 bits, mpz_t *res)
59 {
60  int err;
61  unsigned bytes = (bits + 7) / 8;
62  int last_bits = bits & 7;
63  uint8 *buf;
64 
65  buf = px_alloc(bytes);
66  err = px_get_random_bytes(buf, bytes);
67  if (err < 0)
68  {
69  px_free(buf);
70  return err;
71  }
72 
73  /* clear unnecessary bits and set last bit to one */
74  if (last_bits)
75  {
76  buf[0] >>= 8 - last_bits;
77  buf[0] |= 1 << (last_bits - 1);
78  }
79  else
80  buf[0] |= 1 << 7;
81 
82  mp_int_read_unsigned(res, buf, bytes);
83 
84  px_free(buf);
85 
86  return 0;
87 }
88 
89 static void
90 mp_modmul(mpz_t *a, mpz_t *b, mpz_t *p, mpz_t *res)
91 {
92  mpz_t *tmp = mp_new();
93 
94  mp_int_mul(a, b, tmp);
95  mp_int_mod(tmp, p, res);
96  mp_clear_free(tmp);
97 }
98 
99 static mpz_t *
101 {
102  mpz_t *bn = mp_new();
103 
104  mp_int_read_unsigned(bn, n->data, n->bytes);
105 
106  if (!bn)
107  return NULL;
108  if (mp_int_count_bits(bn) != n->bits)
109  {
110  px_debug("mpi_to_bn: bignum conversion failed: mpi=%d, bn=%d",
111  n->bits, mp_int_count_bits(bn));
112  mp_clear_free(bn);
113  return NULL;
114  }
115  return bn;
116 }
117 
118 static PGP_MPI *
120 {
121  int res;
122  PGP_MPI *n;
123  int bytes;
124 
125  res = pgp_mpi_alloc(mp_int_count_bits(bn), &n);
126  if (res < 0)
127  return NULL;
128 
129  bytes = (mp_int_count_bits(bn) + 7) / 8;
130  if (bytes != n->bytes)
131  {
132  px_debug("bn_to_mpi: bignum conversion failed: bn=%d, mpi=%d",
133  bytes, n->bytes);
134  pgp_mpi_free(n);
135  return NULL;
136  }
137  mp_int_to_unsigned(bn, n->data, n->bytes);
138  return n;
139 }
140 
141 /*
142  * Decide the number of bits in the random componont k
143  *
144  * It should be in the same range as p for signing (which
145  * is deprecated), but can be much smaller for encrypting.
146  *
147  * Until I research it further, I just mimic gpg behaviour.
148  * It has a special mapping table, for values <= 5120,
149  * above that it uses 'arbitrary high number'. Following
150  * algorihm hovers 10-70 bits above gpg values. And for
151  * larger p, it uses gpg's algorihm.
152  *
153  * The point is - if k gets large, encryption will be
154  * really slow. It does not matter for decryption.
155  */
156 static int
157 decide_k_bits(int p_bits)
158 {
159  if (p_bits <= 5120)
160  return p_bits / 10 + 160;
161  else
162  return (p_bits / 8 + 200) * 3 / 2;
163 }
164 
165 int
167  PGP_MPI **c1_p, PGP_MPI **c2_p)
168 {
169  int res = PXE_PGP_MATH_FAILED;
170  int k_bits;
171  mpz_t *m = mpi_to_bn(_m);
172  mpz_t *p = mpi_to_bn(pk->pub.elg.p);
173  mpz_t *g = mpi_to_bn(pk->pub.elg.g);
174  mpz_t *y = mpi_to_bn(pk->pub.elg.y);
175  mpz_t *k = mp_new();
176  mpz_t *yk = mp_new();
177  mpz_t *c1 = mp_new();
178  mpz_t *c2 = mp_new();
179 
180  if (!m || !p || !g || !y || !k || !yk || !c1 || !c2)
181  goto err;
182 
183  /*
184  * generate k
185  */
186  k_bits = decide_k_bits(mp_int_count_bits(p));
187  res = mp_px_rand(k_bits, k);
188  if (res < 0)
189  return res;
190 
191  /*
192  * c1 = g^k c2 = m * y^k
193  */
194  mp_int_exptmod(g, k, p, c1);
195  mp_int_exptmod(y, k, p, yk);
196  mp_modmul(m, yk, p, c2);
197 
198  /* result */
199  *c1_p = bn_to_mpi(c1);
200  *c2_p = bn_to_mpi(c2);
201  if (*c1_p && *c2_p)
202  res = 0;
203 err:
204  mp_clear_free(c2);
205  mp_clear_free(c1);
206  mp_clear_free(yk);
207  mp_clear_free(k);
208  mp_clear_free(y);
209  mp_clear_free(g);
210  mp_clear_free(p);
211  mp_clear_free(m);
212  return res;
213 }
214 
215 int
217  PGP_MPI **msg_p)
218 {
219  int res = PXE_PGP_MATH_FAILED;
220  mpz_t *c1 = mpi_to_bn(_c1);
221  mpz_t *c2 = mpi_to_bn(_c2);
222  mpz_t *p = mpi_to_bn(pk->pub.elg.p);
223  mpz_t *x = mpi_to_bn(pk->sec.elg.x);
224  mpz_t *c1x = mp_new();
225  mpz_t *div = mp_new();
226  mpz_t *m = mp_new();
227 
228  if (!c1 || !c2 || !p || !x || !c1x || !div || !m)
229  goto err;
230 
231  /*
232  * m = c2 / (c1^x)
233  */
234  mp_int_exptmod(c1, x, p, c1x);
235  mp_int_invmod(c1x, p, div);
236  mp_modmul(c2, div, p, m);
237 
238  /* result */
239  *msg_p = bn_to_mpi(m);
240  if (*msg_p)
241  res = 0;
242 err:
243  mp_clear_free(m);
244  mp_clear_free(div);
245  mp_clear_free(c1x);
246  mp_clear_free(x);
247  mp_clear_free(p);
248  mp_clear_free(c2);
249  mp_clear_free(c1);
250  return res;
251 }
252 
253 int
255 {
256  int res = PXE_PGP_MATH_FAILED;
257  mpz_t *m = mpi_to_bn(_m);
258  mpz_t *e = mpi_to_bn(pk->pub.rsa.e);
259  mpz_t *n = mpi_to_bn(pk->pub.rsa.n);
260  mpz_t *c = mp_new();
261 
262  if (!m || !e || !n || !c)
263  goto err;
264 
265  /*
266  * c = m ^ e
267  */
268  mp_int_exptmod(m, e, n, c);
269 
270  *c_p = bn_to_mpi(c);
271  if (*c_p)
272  res = 0;
273 err:
274  mp_clear_free(c);
275  mp_clear_free(n);
276  mp_clear_free(e);
277  mp_clear_free(m);
278  return res;
279 }
280 
281 int
283 {
284  int res = PXE_PGP_MATH_FAILED;
285  mpz_t *c = mpi_to_bn(_c);
286  mpz_t *d = mpi_to_bn(pk->sec.rsa.d);
287  mpz_t *n = mpi_to_bn(pk->pub.rsa.n);
288  mpz_t *m = mp_new();
289 
290  if (!m || !d || !n || !c)
291  goto err;
292 
293  /*
294  * m = c ^ d
295  */
296  mp_int_exptmod(c, d, n, m);
297 
298  *m_p = bn_to_mpi(m);
299  if (*m_p)
300  res = 0;
301 err:
302  mp_clear_free(m);
303  mp_clear_free(n);
304  mp_clear_free(d);
305  mp_clear_free(c);
306  return res;
307 }
mp_result mp_int_mul(mp_int a, mp_int b, mp_int c)
Definition: imath.c:794
union PGP_PubKey::@9 pub
mp_result mp_int_invmod(mp_int a, mp_int m, mp_int c)
Definition: imath.c:1481
struct PGP_PubKey::@9::@12 rsa
mp_result mp_int_read_unsigned(mp_int z, unsigned char *buf, int len)
Definition: imath.c:2210
uint8 * data
Definition: pgp.h:181
struct PGP_PubKey::@9::@11 elg
#define px_free(p)
Definition: px.h:47
unsigned char uint8
Definition: c.h:263
mp_result mp_int_to_unsigned(mp_int z, unsigned char *buf, int limit)
Definition: imath.c:2196
mp_result mp_int_mod(mp_int a, mp_int m, mp_int c)
Definition: imath.c:1080
mp_result mp_int_count_bits(mp_int z)
Definition: imath.c:2073
int bytes
Definition: pgp.h:183
static mpz_t * mpi_to_bn(PGP_MPI *n)
int pgp_elgamal_encrypt(PGP_PubKey *pk, PGP_MPI *_m, PGP_MPI **c1_p, PGP_MPI **c2_p)
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:1336
int pgp_mpi_alloc(int bits, PGP_MPI **mpi)
Definition: pgp-mpi.c:37
void mp_int_free(mp_int z)
Definition: imath.c:495
char * c
int px_get_random_bytes(uint8 *dst, unsigned count)
Definition: internal.c:675
static char * buf
Definition: pg_test_fsync.c:65
int pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *_m, PGP_MPI **c_p)
unsigned int uint32
Definition: c.h:265
Definition: pgp.h:179
mp_result mp_int_init_size(mp_int z, mp_size prec)
Definition: imath.c:389
int pgp_mpi_free(PGP_MPI *mpi)
Definition: pgp-mpi.c:70
static int decide_k_bits(int p_bits)
static PGP_MPI * bn_to_mpi(mpz_t *bn)
#define NULL
Definition: c.h:226
void px_debug(const char *fmt,...)
Definition: px.c:132
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)
int pgp_elgamal_decrypt(PGP_PubKey *pk, PGP_MPI *_c1, PGP_MPI *_c2, PGP_MPI **msg_p)
#define PXE_PGP_MATH_FAILED
Definition: px.h:92
#define px_alloc(s)
Definition: px.h:45
e
Definition: preproc-init.c:82
int pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *_c, PGP_MPI **m_p)
mp_int mp_int_alloc(void)
Definition: imath.c:371
Definition: imath.h:57
static void mp_clear_free(mpz_t *a)
union PGP_PubKey::@10 sec
int bits
Definition: pgp.h:182