2005-07-10 21:46:29 +08:00
|
|
|
/*
|
|
|
|
* pgp-mpi-openssl.c
|
|
|
|
* OpenPGP MPI functions using OpenSSL BIGNUM code.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Marko Kreen
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2014-05-07 00:12:18 +08:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
2005-07-10 21:46:29 +08:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pgcrypto/pgp-mpi-openssl.c
|
2005-07-10 21:46:29 +08:00
|
|
|
*/
|
2005-07-11 23:07:59 +08:00
|
|
|
#include "postgres.h"
|
2005-07-10 21:46:29 +08:00
|
|
|
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
|
|
|
|
#include "pgp.h"
|
2019-10-23 11:56:22 +08:00
|
|
|
#include "px.h"
|
2005-07-10 21:46:29 +08:00
|
|
|
|
|
|
|
static BIGNUM *
|
2009-06-11 22:49:15 +08:00
|
|
|
mpi_to_bn(PGP_MPI *n)
|
2005-07-10 21:46:29 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
BIGNUM *bn = BN_bin2bn(n->data, n->bytes, NULL);
|
|
|
|
|
2005-07-10 21:46:29 +08:00
|
|
|
if (!bn)
|
|
|
|
return NULL;
|
|
|
|
if (BN_num_bits(bn) != n->bits)
|
|
|
|
{
|
|
|
|
px_debug("mpi_to_bn: bignum conversion failed: mpi=%d, bn=%d",
|
2005-10-15 10:49:52 +08:00
|
|
|
n->bits, BN_num_bits(bn));
|
2005-07-10 21:46:29 +08:00
|
|
|
BN_clear_free(bn);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return bn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PGP_MPI *
|
|
|
|
bn_to_mpi(BIGNUM *bn)
|
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
int res;
|
|
|
|
PGP_MPI *n;
|
2005-07-10 21:46:29 +08:00
|
|
|
|
|
|
|
res = pgp_mpi_alloc(BN_num_bits(bn), &n);
|
|
|
|
if (res < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (BN_num_bytes(bn) != n->bytes)
|
|
|
|
{
|
|
|
|
px_debug("bn_to_mpi: bignum conversion failed: bn=%d, mpi=%d",
|
2005-10-15 10:49:52 +08:00
|
|
|
BN_num_bytes(bn), n->bytes);
|
2005-07-10 21:46:29 +08:00
|
|
|
pgp_mpi_free(n);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
BN_bn2bin(bn, n->data);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-02-06 17:33:58 +08:00
|
|
|
* Decide the number of bits in the random component k
|
2005-07-10 21:46:29 +08:00
|
|
|
*
|
|
|
|
* It should be in the same range as p for signing (which
|
|
|
|
* is deprecated), but can be much smaller for encrypting.
|
|
|
|
*
|
|
|
|
* Until I research it further, I just mimic gpg behaviour.
|
|
|
|
* It has a special mapping table, for values <= 5120,
|
2014-05-07 00:12:18 +08:00
|
|
|
* above that it uses 'arbitrary high number'. Following
|
2017-02-06 17:33:58 +08:00
|
|
|
* algorithm hovers 10-70 bits above gpg values. And for
|
|
|
|
* larger p, it uses gpg's algorithm.
|
2005-07-10 21:46:29 +08:00
|
|
|
*
|
|
|
|
* The point is - if k gets large, encryption will be
|
|
|
|
* really slow. It does not matter for decryption.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
decide_k_bits(int p_bits)
|
|
|
|
{
|
|
|
|
if (p_bits <= 5120)
|
|
|
|
return p_bits / 10 + 160;
|
|
|
|
else
|
|
|
|
return (p_bits / 8 + 200) * 3 / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-06-11 22:49:15 +08:00
|
|
|
pgp_elgamal_encrypt(PGP_PubKey *pk, PGP_MPI *_m,
|
|
|
|
PGP_MPI **c1_p, PGP_MPI **c2_p)
|
2005-07-10 21:46:29 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
int res = PXE_PGP_MATH_FAILED;
|
|
|
|
int k_bits;
|
|
|
|
BIGNUM *m = mpi_to_bn(_m);
|
|
|
|
BIGNUM *p = mpi_to_bn(pk->pub.elg.p);
|
|
|
|
BIGNUM *g = mpi_to_bn(pk->pub.elg.g);
|
|
|
|
BIGNUM *y = mpi_to_bn(pk->pub.elg.y);
|
|
|
|
BIGNUM *k = BN_new();
|
|
|
|
BIGNUM *yk = BN_new();
|
|
|
|
BIGNUM *c1 = BN_new();
|
|
|
|
BIGNUM *c2 = BN_new();
|
|
|
|
BN_CTX *tmp = BN_CTX_new();
|
2005-07-10 21:46:29 +08:00
|
|
|
|
|
|
|
if (!m || !p || !g || !y || !k || !yk || !c1 || !c2 || !tmp)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate k
|
|
|
|
*/
|
|
|
|
k_bits = decide_k_bits(BN_num_bits(p));
|
The large one adds support for RSA keys and reorganizes
the pubkey functions a bit. The actual RSA-specific code
there is tiny, most of the patch consists of reorg of the
pubkey code, as lots of it was written as elgamal-only.
---------------------------------------------------------------------------
The SHLIB section was copy-pasted from somewhere and contains
several unnecessary libs. This cleans it up a bit.
-lcrypt
we don't use system crypt()
-lssl, -lssleay32
no SSL here
-lz in win32 section
already added on previous line
-ldes
The chance anybody has it is pretty low.
And the chance pgcrypto works with it is even lower.
Also trim the win32 section.
---------------------------------------------------------------------------
It is already disabled in Makefile, remove code too.
---------------------------------------------------------------------------
I was bit hasty making the random exponent 'k' a prime. Further researh
shows that Elgamal encryption has no specific needs in respect to k,
any random number is fine.
It is bit different for signing, there it needs to be 'relatively prime'
to p - 1, that means GCD(k, p-1) == 1, which is also a lot lighter than
full primality. As we don't do signing, this can be ignored.
This brings major speedup to Elgamal encryption.
---------------------------------------------------------------------------
o pgp_mpi_free: Accept NULLs
o pgp_mpi_cksum: result should be 16bit
o Remove function name from error messages - to be similar to other
SQL functions, and it does not match anyway the called function
o remove couple junk lines
---------------------------------------------------------------------------
o Support for RSA encryption
o Big reorg to better separate generic and algorithm-specific code.
o Regression tests for RSA.
---------------------------------------------------------------------------
o Tom stuck a CVS id into file. I doubt the usefulness of it,
but if it needs to be in the file then rather at the end.
Also tag it as comment for asciidoc.
o Mention bytea vs. text difference
o Couple clarifications
---------------------------------------------------------------------------
There is a choice whether to update it with pgp functions or
remove it. I decided to remove it, updating is pointless.
I've tried to keep the core of pgcrypto relatively independent
from main PostgreSQL, to make it easy to use externally if needed,
and that is good. Eg. that made development of PGP functions much
nicer.
But I have no plans to release it as generic library, so keeping such
doc
up-to-date is waste of time. If anyone is interested in using it in
other products, he can probably bother to read the source too.
Commented source is another thing - I'll try to make another pass
over code to see if there is anything non-obvious that would need
more comments.
---------------------------------------------------------------------------
Marko Kreen
2005-08-13 10:06:21 +08:00
|
|
|
if (!BN_rand(k, k_bits, 0, 0))
|
2005-07-10 21:46:29 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* c1 = g^k c2 = m * y^k
|
2005-07-10 21:46:29 +08:00
|
|
|
*/
|
|
|
|
if (!BN_mod_exp(c1, g, k, p, tmp))
|
|
|
|
goto err;
|
|
|
|
if (!BN_mod_exp(yk, y, k, p, tmp))
|
|
|
|
goto err;
|
|
|
|
if (!BN_mod_mul(c2, m, yk, p, tmp))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* result */
|
|
|
|
*c1_p = bn_to_mpi(c1);
|
|
|
|
*c2_p = bn_to_mpi(c2);
|
|
|
|
if (*c1_p && *c2_p)
|
|
|
|
res = 0;
|
|
|
|
err:
|
2005-10-15 10:49:52 +08:00
|
|
|
if (tmp)
|
|
|
|
BN_CTX_free(tmp);
|
|
|
|
if (c2)
|
|
|
|
BN_clear_free(c2);
|
|
|
|
if (c1)
|
|
|
|
BN_clear_free(c1);
|
|
|
|
if (yk)
|
|
|
|
BN_clear_free(yk);
|
|
|
|
if (k)
|
|
|
|
BN_clear_free(k);
|
|
|
|
if (y)
|
|
|
|
BN_clear_free(y);
|
|
|
|
if (g)
|
|
|
|
BN_clear_free(g);
|
|
|
|
if (p)
|
|
|
|
BN_clear_free(p);
|
|
|
|
if (m)
|
|
|
|
BN_clear_free(m);
|
2005-07-10 21:46:29 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-06-11 22:49:15 +08:00
|
|
|
pgp_elgamal_decrypt(PGP_PubKey *pk, PGP_MPI *_c1, PGP_MPI *_c2,
|
|
|
|
PGP_MPI **msg_p)
|
2005-07-10 21:46:29 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
int res = PXE_PGP_MATH_FAILED;
|
|
|
|
BIGNUM *c1 = mpi_to_bn(_c1);
|
|
|
|
BIGNUM *c2 = mpi_to_bn(_c2);
|
|
|
|
BIGNUM *p = mpi_to_bn(pk->pub.elg.p);
|
|
|
|
BIGNUM *x = mpi_to_bn(pk->sec.elg.x);
|
|
|
|
BIGNUM *c1x = BN_new();
|
|
|
|
BIGNUM *div = BN_new();
|
|
|
|
BIGNUM *m = BN_new();
|
|
|
|
BN_CTX *tmp = BN_CTX_new();
|
2005-07-10 21:46:29 +08:00
|
|
|
|
|
|
|
if (!c1 || !c2 || !p || !x || !c1x || !div || !m || !tmp)
|
|
|
|
goto err;
|
2005-10-15 10:49:52 +08:00
|
|
|
|
2005-07-10 21:46:29 +08:00
|
|
|
/*
|
|
|
|
* m = c2 / (c1^x)
|
|
|
|
*/
|
|
|
|
if (!BN_mod_exp(c1x, c1, x, p, tmp))
|
|
|
|
goto err;
|
|
|
|
if (!BN_mod_inverse(div, c1x, p, tmp))
|
|
|
|
goto err;
|
|
|
|
if (!BN_mod_mul(m, c2, div, p, tmp))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* result */
|
|
|
|
*msg_p = bn_to_mpi(m);
|
|
|
|
if (*msg_p)
|
|
|
|
res = 0;
|
|
|
|
err:
|
2005-10-15 10:49:52 +08:00
|
|
|
if (tmp)
|
|
|
|
BN_CTX_free(tmp);
|
|
|
|
if (m)
|
|
|
|
BN_clear_free(m);
|
|
|
|
if (div)
|
|
|
|
BN_clear_free(div);
|
|
|
|
if (c1x)
|
|
|
|
BN_clear_free(c1x);
|
|
|
|
if (x)
|
|
|
|
BN_clear_free(x);
|
|
|
|
if (p)
|
|
|
|
BN_clear_free(p);
|
|
|
|
if (c2)
|
|
|
|
BN_clear_free(c2);
|
|
|
|
if (c1)
|
|
|
|
BN_clear_free(c1);
|
2005-07-10 21:46:29 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
The large one adds support for RSA keys and reorganizes
the pubkey functions a bit. The actual RSA-specific code
there is tiny, most of the patch consists of reorg of the
pubkey code, as lots of it was written as elgamal-only.
---------------------------------------------------------------------------
The SHLIB section was copy-pasted from somewhere and contains
several unnecessary libs. This cleans it up a bit.
-lcrypt
we don't use system crypt()
-lssl, -lssleay32
no SSL here
-lz in win32 section
already added on previous line
-ldes
The chance anybody has it is pretty low.
And the chance pgcrypto works with it is even lower.
Also trim the win32 section.
---------------------------------------------------------------------------
It is already disabled in Makefile, remove code too.
---------------------------------------------------------------------------
I was bit hasty making the random exponent 'k' a prime. Further researh
shows that Elgamal encryption has no specific needs in respect to k,
any random number is fine.
It is bit different for signing, there it needs to be 'relatively prime'
to p - 1, that means GCD(k, p-1) == 1, which is also a lot lighter than
full primality. As we don't do signing, this can be ignored.
This brings major speedup to Elgamal encryption.
---------------------------------------------------------------------------
o pgp_mpi_free: Accept NULLs
o pgp_mpi_cksum: result should be 16bit
o Remove function name from error messages - to be similar to other
SQL functions, and it does not match anyway the called function
o remove couple junk lines
---------------------------------------------------------------------------
o Support for RSA encryption
o Big reorg to better separate generic and algorithm-specific code.
o Regression tests for RSA.
---------------------------------------------------------------------------
o Tom stuck a CVS id into file. I doubt the usefulness of it,
but if it needs to be in the file then rather at the end.
Also tag it as comment for asciidoc.
o Mention bytea vs. text difference
o Couple clarifications
---------------------------------------------------------------------------
There is a choice whether to update it with pgp functions or
remove it. I decided to remove it, updating is pointless.
I've tried to keep the core of pgcrypto relatively independent
from main PostgreSQL, to make it easy to use externally if needed,
and that is good. Eg. that made development of PGP functions much
nicer.
But I have no plans to release it as generic library, so keeping such
doc
up-to-date is waste of time. If anyone is interested in using it in
other products, he can probably bother to read the source too.
Commented source is another thing - I'll try to make another pass
over code to see if there is anything non-obvious that would need
more comments.
---------------------------------------------------------------------------
Marko Kreen
2005-08-13 10:06:21 +08:00
|
|
|
int
|
2009-06-11 22:49:15 +08:00
|
|
|
pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *_m, PGP_MPI **c_p)
|
The large one adds support for RSA keys and reorganizes
the pubkey functions a bit. The actual RSA-specific code
there is tiny, most of the patch consists of reorg of the
pubkey code, as lots of it was written as elgamal-only.
---------------------------------------------------------------------------
The SHLIB section was copy-pasted from somewhere and contains
several unnecessary libs. This cleans it up a bit.
-lcrypt
we don't use system crypt()
-lssl, -lssleay32
no SSL here
-lz in win32 section
already added on previous line
-ldes
The chance anybody has it is pretty low.
And the chance pgcrypto works with it is even lower.
Also trim the win32 section.
---------------------------------------------------------------------------
It is already disabled in Makefile, remove code too.
---------------------------------------------------------------------------
I was bit hasty making the random exponent 'k' a prime. Further researh
shows that Elgamal encryption has no specific needs in respect to k,
any random number is fine.
It is bit different for signing, there it needs to be 'relatively prime'
to p - 1, that means GCD(k, p-1) == 1, which is also a lot lighter than
full primality. As we don't do signing, this can be ignored.
This brings major speedup to Elgamal encryption.
---------------------------------------------------------------------------
o pgp_mpi_free: Accept NULLs
o pgp_mpi_cksum: result should be 16bit
o Remove function name from error messages - to be similar to other
SQL functions, and it does not match anyway the called function
o remove couple junk lines
---------------------------------------------------------------------------
o Support for RSA encryption
o Big reorg to better separate generic and algorithm-specific code.
o Regression tests for RSA.
---------------------------------------------------------------------------
o Tom stuck a CVS id into file. I doubt the usefulness of it,
but if it needs to be in the file then rather at the end.
Also tag it as comment for asciidoc.
o Mention bytea vs. text difference
o Couple clarifications
---------------------------------------------------------------------------
There is a choice whether to update it with pgp functions or
remove it. I decided to remove it, updating is pointless.
I've tried to keep the core of pgcrypto relatively independent
from main PostgreSQL, to make it easy to use externally if needed,
and that is good. Eg. that made development of PGP functions much
nicer.
But I have no plans to release it as generic library, so keeping such
doc
up-to-date is waste of time. If anyone is interested in using it in
other products, he can probably bother to read the source too.
Commented source is another thing - I'll try to make another pass
over code to see if there is anything non-obvious that would need
more comments.
---------------------------------------------------------------------------
Marko Kreen
2005-08-13 10:06:21 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
int res = PXE_PGP_MATH_FAILED;
|
|
|
|
BIGNUM *m = mpi_to_bn(_m);
|
|
|
|
BIGNUM *e = mpi_to_bn(pk->pub.rsa.e);
|
|
|
|
BIGNUM *n = mpi_to_bn(pk->pub.rsa.n);
|
|
|
|
BIGNUM *c = BN_new();
|
|
|
|
BN_CTX *tmp = BN_CTX_new();
|
The large one adds support for RSA keys and reorganizes
the pubkey functions a bit. The actual RSA-specific code
there is tiny, most of the patch consists of reorg of the
pubkey code, as lots of it was written as elgamal-only.
---------------------------------------------------------------------------
The SHLIB section was copy-pasted from somewhere and contains
several unnecessary libs. This cleans it up a bit.
-lcrypt
we don't use system crypt()
-lssl, -lssleay32
no SSL here
-lz in win32 section
already added on previous line
-ldes
The chance anybody has it is pretty low.
And the chance pgcrypto works with it is even lower.
Also trim the win32 section.
---------------------------------------------------------------------------
It is already disabled in Makefile, remove code too.
---------------------------------------------------------------------------
I was bit hasty making the random exponent 'k' a prime. Further researh
shows that Elgamal encryption has no specific needs in respect to k,
any random number is fine.
It is bit different for signing, there it needs to be 'relatively prime'
to p - 1, that means GCD(k, p-1) == 1, which is also a lot lighter than
full primality. As we don't do signing, this can be ignored.
This brings major speedup to Elgamal encryption.
---------------------------------------------------------------------------
o pgp_mpi_free: Accept NULLs
o pgp_mpi_cksum: result should be 16bit
o Remove function name from error messages - to be similar to other
SQL functions, and it does not match anyway the called function
o remove couple junk lines
---------------------------------------------------------------------------
o Support for RSA encryption
o Big reorg to better separate generic and algorithm-specific code.
o Regression tests for RSA.
---------------------------------------------------------------------------
o Tom stuck a CVS id into file. I doubt the usefulness of it,
but if it needs to be in the file then rather at the end.
Also tag it as comment for asciidoc.
o Mention bytea vs. text difference
o Couple clarifications
---------------------------------------------------------------------------
There is a choice whether to update it with pgp functions or
remove it. I decided to remove it, updating is pointless.
I've tried to keep the core of pgcrypto relatively independent
from main PostgreSQL, to make it easy to use externally if needed,
and that is good. Eg. that made development of PGP functions much
nicer.
But I have no plans to release it as generic library, so keeping such
doc
up-to-date is waste of time. If anyone is interested in using it in
other products, he can probably bother to read the source too.
Commented source is another thing - I'll try to make another pass
over code to see if there is anything non-obvious that would need
more comments.
---------------------------------------------------------------------------
Marko Kreen
2005-08-13 10:06:21 +08:00
|
|
|
|
|
|
|
if (!m || !e || !n || !c || !tmp)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* c = m ^ e
|
|
|
|
*/
|
|
|
|
if (!BN_mod_exp(c, m, e, n, tmp))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
*c_p = bn_to_mpi(c);
|
|
|
|
if (*c_p)
|
|
|
|
res = 0;
|
|
|
|
err:
|
2005-10-15 10:49:52 +08:00
|
|
|
if (tmp)
|
|
|
|
BN_CTX_free(tmp);
|
|
|
|
if (c)
|
|
|
|
BN_clear_free(c);
|
|
|
|
if (n)
|
|
|
|
BN_clear_free(n);
|
|
|
|
if (e)
|
|
|
|
BN_clear_free(e);
|
|
|
|
if (m)
|
|
|
|
BN_clear_free(m);
|
The large one adds support for RSA keys and reorganizes
the pubkey functions a bit. The actual RSA-specific code
there is tiny, most of the patch consists of reorg of the
pubkey code, as lots of it was written as elgamal-only.
---------------------------------------------------------------------------
The SHLIB section was copy-pasted from somewhere and contains
several unnecessary libs. This cleans it up a bit.
-lcrypt
we don't use system crypt()
-lssl, -lssleay32
no SSL here
-lz in win32 section
already added on previous line
-ldes
The chance anybody has it is pretty low.
And the chance pgcrypto works with it is even lower.
Also trim the win32 section.
---------------------------------------------------------------------------
It is already disabled in Makefile, remove code too.
---------------------------------------------------------------------------
I was bit hasty making the random exponent 'k' a prime. Further researh
shows that Elgamal encryption has no specific needs in respect to k,
any random number is fine.
It is bit different for signing, there it needs to be 'relatively prime'
to p - 1, that means GCD(k, p-1) == 1, which is also a lot lighter than
full primality. As we don't do signing, this can be ignored.
This brings major speedup to Elgamal encryption.
---------------------------------------------------------------------------
o pgp_mpi_free: Accept NULLs
o pgp_mpi_cksum: result should be 16bit
o Remove function name from error messages - to be similar to other
SQL functions, and it does not match anyway the called function
o remove couple junk lines
---------------------------------------------------------------------------
o Support for RSA encryption
o Big reorg to better separate generic and algorithm-specific code.
o Regression tests for RSA.
---------------------------------------------------------------------------
o Tom stuck a CVS id into file. I doubt the usefulness of it,
but if it needs to be in the file then rather at the end.
Also tag it as comment for asciidoc.
o Mention bytea vs. text difference
o Couple clarifications
---------------------------------------------------------------------------
There is a choice whether to update it with pgp functions or
remove it. I decided to remove it, updating is pointless.
I've tried to keep the core of pgcrypto relatively independent
from main PostgreSQL, to make it easy to use externally if needed,
and that is good. Eg. that made development of PGP functions much
nicer.
But I have no plans to release it as generic library, so keeping such
doc
up-to-date is waste of time. If anyone is interested in using it in
other products, he can probably bother to read the source too.
Commented source is another thing - I'll try to make another pass
over code to see if there is anything non-obvious that would need
more comments.
---------------------------------------------------------------------------
Marko Kreen
2005-08-13 10:06:21 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-06-11 22:49:15 +08:00
|
|
|
pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *_c, PGP_MPI **m_p)
|
The large one adds support for RSA keys and reorganizes
the pubkey functions a bit. The actual RSA-specific code
there is tiny, most of the patch consists of reorg of the
pubkey code, as lots of it was written as elgamal-only.
---------------------------------------------------------------------------
The SHLIB section was copy-pasted from somewhere and contains
several unnecessary libs. This cleans it up a bit.
-lcrypt
we don't use system crypt()
-lssl, -lssleay32
no SSL here
-lz in win32 section
already added on previous line
-ldes
The chance anybody has it is pretty low.
And the chance pgcrypto works with it is even lower.
Also trim the win32 section.
---------------------------------------------------------------------------
It is already disabled in Makefile, remove code too.
---------------------------------------------------------------------------
I was bit hasty making the random exponent 'k' a prime. Further researh
shows that Elgamal encryption has no specific needs in respect to k,
any random number is fine.
It is bit different for signing, there it needs to be 'relatively prime'
to p - 1, that means GCD(k, p-1) == 1, which is also a lot lighter than
full primality. As we don't do signing, this can be ignored.
This brings major speedup to Elgamal encryption.
---------------------------------------------------------------------------
o pgp_mpi_free: Accept NULLs
o pgp_mpi_cksum: result should be 16bit
o Remove function name from error messages - to be similar to other
SQL functions, and it does not match anyway the called function
o remove couple junk lines
---------------------------------------------------------------------------
o Support for RSA encryption
o Big reorg to better separate generic and algorithm-specific code.
o Regression tests for RSA.
---------------------------------------------------------------------------
o Tom stuck a CVS id into file. I doubt the usefulness of it,
but if it needs to be in the file then rather at the end.
Also tag it as comment for asciidoc.
o Mention bytea vs. text difference
o Couple clarifications
---------------------------------------------------------------------------
There is a choice whether to update it with pgp functions or
remove it. I decided to remove it, updating is pointless.
I've tried to keep the core of pgcrypto relatively independent
from main PostgreSQL, to make it easy to use externally if needed,
and that is good. Eg. that made development of PGP functions much
nicer.
But I have no plans to release it as generic library, so keeping such
doc
up-to-date is waste of time. If anyone is interested in using it in
other products, he can probably bother to read the source too.
Commented source is another thing - I'll try to make another pass
over code to see if there is anything non-obvious that would need
more comments.
---------------------------------------------------------------------------
Marko Kreen
2005-08-13 10:06:21 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
int res = PXE_PGP_MATH_FAILED;
|
|
|
|
BIGNUM *c = mpi_to_bn(_c);
|
|
|
|
BIGNUM *d = mpi_to_bn(pk->sec.rsa.d);
|
|
|
|
BIGNUM *n = mpi_to_bn(pk->pub.rsa.n);
|
|
|
|
BIGNUM *m = BN_new();
|
|
|
|
BN_CTX *tmp = BN_CTX_new();
|
The large one adds support for RSA keys and reorganizes
the pubkey functions a bit. The actual RSA-specific code
there is tiny, most of the patch consists of reorg of the
pubkey code, as lots of it was written as elgamal-only.
---------------------------------------------------------------------------
The SHLIB section was copy-pasted from somewhere and contains
several unnecessary libs. This cleans it up a bit.
-lcrypt
we don't use system crypt()
-lssl, -lssleay32
no SSL here
-lz in win32 section
already added on previous line
-ldes
The chance anybody has it is pretty low.
And the chance pgcrypto works with it is even lower.
Also trim the win32 section.
---------------------------------------------------------------------------
It is already disabled in Makefile, remove code too.
---------------------------------------------------------------------------
I was bit hasty making the random exponent 'k' a prime. Further researh
shows that Elgamal encryption has no specific needs in respect to k,
any random number is fine.
It is bit different for signing, there it needs to be 'relatively prime'
to p - 1, that means GCD(k, p-1) == 1, which is also a lot lighter than
full primality. As we don't do signing, this can be ignored.
This brings major speedup to Elgamal encryption.
---------------------------------------------------------------------------
o pgp_mpi_free: Accept NULLs
o pgp_mpi_cksum: result should be 16bit
o Remove function name from error messages - to be similar to other
SQL functions, and it does not match anyway the called function
o remove couple junk lines
---------------------------------------------------------------------------
o Support for RSA encryption
o Big reorg to better separate generic and algorithm-specific code.
o Regression tests for RSA.
---------------------------------------------------------------------------
o Tom stuck a CVS id into file. I doubt the usefulness of it,
but if it needs to be in the file then rather at the end.
Also tag it as comment for asciidoc.
o Mention bytea vs. text difference
o Couple clarifications
---------------------------------------------------------------------------
There is a choice whether to update it with pgp functions or
remove it. I decided to remove it, updating is pointless.
I've tried to keep the core of pgcrypto relatively independent
from main PostgreSQL, to make it easy to use externally if needed,
and that is good. Eg. that made development of PGP functions much
nicer.
But I have no plans to release it as generic library, so keeping such
doc
up-to-date is waste of time. If anyone is interested in using it in
other products, he can probably bother to read the source too.
Commented source is another thing - I'll try to make another pass
over code to see if there is anything non-obvious that would need
more comments.
---------------------------------------------------------------------------
Marko Kreen
2005-08-13 10:06:21 +08:00
|
|
|
|
|
|
|
if (!m || !d || !n || !c || !tmp)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* m = c ^ d
|
|
|
|
*/
|
|
|
|
if (!BN_mod_exp(m, c, d, n, tmp))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
*m_p = bn_to_mpi(m);
|
|
|
|
if (*m_p)
|
|
|
|
res = 0;
|
|
|
|
err:
|
2005-10-15 10:49:52 +08:00
|
|
|
if (tmp)
|
|
|
|
BN_CTX_free(tmp);
|
|
|
|
if (m)
|
|
|
|
BN_clear_free(m);
|
|
|
|
if (n)
|
|
|
|
BN_clear_free(n);
|
|
|
|
if (d)
|
|
|
|
BN_clear_free(d);
|
|
|
|
if (c)
|
|
|
|
BN_clear_free(c);
|
The large one adds support for RSA keys and reorganizes
the pubkey functions a bit. The actual RSA-specific code
there is tiny, most of the patch consists of reorg of the
pubkey code, as lots of it was written as elgamal-only.
---------------------------------------------------------------------------
The SHLIB section was copy-pasted from somewhere and contains
several unnecessary libs. This cleans it up a bit.
-lcrypt
we don't use system crypt()
-lssl, -lssleay32
no SSL here
-lz in win32 section
already added on previous line
-ldes
The chance anybody has it is pretty low.
And the chance pgcrypto works with it is even lower.
Also trim the win32 section.
---------------------------------------------------------------------------
It is already disabled in Makefile, remove code too.
---------------------------------------------------------------------------
I was bit hasty making the random exponent 'k' a prime. Further researh
shows that Elgamal encryption has no specific needs in respect to k,
any random number is fine.
It is bit different for signing, there it needs to be 'relatively prime'
to p - 1, that means GCD(k, p-1) == 1, which is also a lot lighter than
full primality. As we don't do signing, this can be ignored.
This brings major speedup to Elgamal encryption.
---------------------------------------------------------------------------
o pgp_mpi_free: Accept NULLs
o pgp_mpi_cksum: result should be 16bit
o Remove function name from error messages - to be similar to other
SQL functions, and it does not match anyway the called function
o remove couple junk lines
---------------------------------------------------------------------------
o Support for RSA encryption
o Big reorg to better separate generic and algorithm-specific code.
o Regression tests for RSA.
---------------------------------------------------------------------------
o Tom stuck a CVS id into file. I doubt the usefulness of it,
but if it needs to be in the file then rather at the end.
Also tag it as comment for asciidoc.
o Mention bytea vs. text difference
o Couple clarifications
---------------------------------------------------------------------------
There is a choice whether to update it with pgp functions or
remove it. I decided to remove it, updating is pointless.
I've tried to keep the core of pgcrypto relatively independent
from main PostgreSQL, to make it easy to use externally if needed,
and that is good. Eg. that made development of PGP functions much
nicer.
But I have no plans to release it as generic library, so keeping such
doc
up-to-date is waste of time. If anyone is interested in using it in
other products, he can probably bother to read the source too.
Commented source is another thing - I'll try to make another pass
over code to see if there is anything non-obvious that would need
more comments.
---------------------------------------------------------------------------
Marko Kreen
2005-08-13 10:06:21 +08:00
|
|
|
return res;
|
|
|
|
}
|