33 #include <openssl/bn.h>
41 BIGNUM *bn = BN_bin2bn(n->
data, n->
bytes, NULL);
45 if (BN_num_bits(bn) != n->
bits)
47 px_debug(
"mpi_to_bn: bignum conversion failed: mpi=%d, bn=%d",
48 n->
bits, BN_num_bits(bn));
65 if (BN_num_bytes(bn) != n->
bytes)
67 px_debug(
"bn_to_mpi: bignum conversion failed: bn=%d, mpi=%d",
68 BN_num_bytes(bn), n->
bytes);
72 BN_bn2bin(bn, n->
data);
95 return p_bits / 10 + 160;
97 return (p_bits / 8 + 200) * 3 / 2;
110 BIGNUM *k = BN_new();
111 BIGNUM *yk = BN_new();
112 BIGNUM *c1 = BN_new();
113 BIGNUM *c2 = BN_new();
114 BN_CTX *tmp = BN_CTX_new();
116 if (!m || !p || !g || !
y || !k || !yk || !c1 || !c2 || !tmp)
123 if (!BN_rand(k, k_bits, 0, 0))
129 if (!BN_mod_exp(c1, g, k, p, tmp))
131 if (!BN_mod_exp(yk,
y, k, p, tmp))
133 if (!BN_mod_mul(c2, m, yk, p, tmp))
172 BIGNUM *c1x = BN_new();
173 BIGNUM *div = BN_new();
174 BIGNUM *m = BN_new();
175 BN_CTX *tmp = BN_CTX_new();
177 if (!c1 || !c2 || !p || !
x || !c1x || !div || !m || !tmp)
183 if (!BN_mod_exp(c1x, c1,
x, p, tmp))
185 if (!BN_mod_inverse(div, c1x, p, tmp))
187 if (!BN_mod_mul(m, c2, div, p, tmp))
221 BIGNUM *
c = BN_new();
222 BN_CTX *tmp = BN_CTX_new();
224 if (!m || !
e || !n || !
c || !tmp)
230 if (!BN_mod_exp(
c, m,
e, n, tmp))
257 BIGNUM *m = BN_new();
258 BN_CTX *tmp = BN_CTX_new();
260 if (!m || !d || !n || !
c || !tmp)
266 if (!BN_mod_exp(m,
c, d, n, tmp))
static void PGresult * res
void err(int eval, const char *fmt,...)
int pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *_c, PGP_MPI **m_p)
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)
static int decide_k_bits(int p_bits)
static BIGNUM * mpi_to_bn(PGP_MPI *n)
static PGP_MPI * bn_to_mpi(BIGNUM *bn)
int pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *_m, PGP_MPI **c_p)
int pgp_mpi_alloc(int bits, PGP_MPI **mpi)
int pgp_mpi_free(PGP_MPI *mpi)
void px_debug(const char *fmt,...)
#define PXE_PGP_MATH_FAILED
struct PGP_PubKey::@0::@3 rsa
struct PGP_PubKey::@0::@2 elg