1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Major pgcrypto changes:

of password-based encryption from RFC2440 (OpenPGP).

The goal of this code is to be more featureful encryption solution
than current encrypt(), which only functionality is running cipher
over data.

Compared to encrypt(), pgp_encrypt() does following:

* It uses the equvialent of random Inital Vector to get cipher
  into random state before it processes user data
* Stores SHA-1 of the data into result so any modification
  will be detected.
* Remembers if data was text or binary - thus it can decrypt
  to/from text data.  This was a major nuisance for encrypt().
* Stores info about used algorithms with result, so user needs
  not remember them - more user friendly!
* Uses String2Key algorithms (similar to crypt()) with random salt
  to generate full-length binary key to be used for encrypting.
* Uses standard format for data - you can feed it to GnuPG, if needed.

Optional features (off by default):

* Can use separate session key - user data will be encrypted
  with totally random key, which will be encrypted with S2K
  generated key and attached to result.
* Data compression with zlib.
* Can convert between CRLF<->LF line-endings - to get fully
  RFC2440-compliant behaviour.  This is off by default as
  pgcrypto does not know the line-endings of user data.

Interface is simple:


    pgp_encrypt(data text, key text) returns bytea
    pgp_decrypt(data text, key text) returns text
    pgp_encrypt_bytea(data bytea, key text) returns bytea
    pgp_decrypt_bytea(data bytea, key text) returns bytea

To change parameters (cipher, compression, mdc):

    pgp_encrypt(data text, key text, parms text) returns bytea
    pgp_decrypt(data text, key text, parms text) returns text
    pgp_encrypt_bytea(data bytea, key text, parms text) returns bytea
    pgp_decrypt_bytea(data bytea, key text, parms text) returns bytea

Parameter names I lifted from gpg:

   pgp_encrypt('message', 'key', 'compress-algo=1,cipher-algo=aes256')

For text data, pgp_encrypt simply encrypts the PostgreSQL internal data.

This maps to RFC2440 data type 't' - 'extenally specified encoding'.
But this may cause problems if data is dumped and reloaded into database
which as different internal encoding.  My next goal is to implement data
type 'u' - which means data is in UTF-8 encoding by converting internal
encoding to UTF-8 and back.  And there wont be any compatibility
problems with current code, I think its ok to submit this without UTF-8
encoding by converting internal encoding to UTF-8 and back.  And there
wont be any compatibility problems with current code, I think its ok to
submit this without UTF-8 support.


Here is v4 of PGP encrypt.  This depends on previously sent
Fortuna-patch, as it uses the px_add_entropy function.

- New function: pgp_key_id() for finding key id's.
- Add SHA1 of user data and key into RNG pools.  We need to get
  randomness from somewhere, and it is in user best interests
  to contribute.
- Regenerate pgp-armor test for SQL_ASCII database.
- Cleanup the key handling so that the pubkey support is less
  hackish.

Marko Kreen
This commit is contained in:
Bruce Momjian
2005-07-10 03:57:55 +00:00
parent 4fcf8b11ff
commit 73e2431817
5 changed files with 408 additions and 17 deletions

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.13 2005/07/10 03:55:28 momjian Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.14 2005/07/10 03:57:55 momjian Exp $
*/
#ifndef __PX_H
@ -42,19 +42,20 @@
#error BYTE_ORDER must be defined as LITTLE_ENDIAN or BIG_ENDIAN
#endif
/* keep debug messages? */
#define PX_DEBUG
/* a way to disable palloc
* - useful if compiled into standalone
*/
#ifndef PX_OWN_ALLOC
#define px_alloc(s) palloc(s)
#define px_realloc(p, s) repalloc(p, s)
#define px_free(p) pfree(p)
#else
void *px_alloc(size_t s);
void *px_realloc(void *p, size_t s);
void px_free(void *p);
#endif
/* max len of 'type' parms */
@ -85,6 +86,34 @@ void px_free(void *p);
#define PXE_MCRYPT_INTERNAL -16
#define PXE_NO_RANDOM -17
#define PXE_MBUF_SHORT_READ -50
#define PXE_PGP_CORRUPT_DATA -100
#define PXE_PGP_CORRUPT_ARMOR -101
#define PXE_PGP_UNSUPPORTED_COMPR -102
#define PXE_PGP_UNSUPPORTED_CIPHER -103
#define PXE_PGP_UNSUPPORTED_HASH -104
#define PXE_PGP_COMPRESSION_ERROR -105
#define PXE_PGP_NOT_TEXT -106
#define PXE_PGP_UNEXPECTED_PKT -107
#define PXE_PGP_NO_BIGNUM -108
#define PXE_PGP_MATH_FAILED -109
#define PXE_PGP_SHORT_ELGAMAL_KEY -110
#define PXE_PGP_RSA_UNSUPPORTED -111
#define PXE_PGP_UNKNOWN_PUBALGO -112
#define PXE_PGP_WRONG_KEYID -113
#define PXE_PGP_MULTIPLE_KEYS -114
#define PXE_PGP_EXPECT_PUBLIC_KEY -115
#define PXE_PGP_EXPECT_SECRET_KEY -116
#define PXE_PGP_NOT_V4_KEYPKT -117
#define PXE_PGP_KEYPKT_CORRUPT -118
#define PXE_PGP_NO_USABLE_KEY -119
#define PXE_PGP_NEED_SECRET_PSW -120
#define PXE_PGP_BAD_S2K_MODE -121
#define PXE_PGP_UNSUPPORTED_PUBALGO -122
#define PXE_PGP_MULTIPLE_SUBKEYS -123
typedef struct px_digest PX_MD;
typedef struct px_alias PX_Alias;
typedef struct px_hmac PX_HMAC;
@ -103,7 +132,7 @@ struct px_digest
union
{
unsigned code;
const void *ptr;
void *ptr;
} p;
};
@ -178,6 +207,13 @@ const char *px_strerror(int err);
const char *px_resolve_alias(const PX_Alias * aliases, const char *name);
void px_set_debug_handler(void (*handler)(const char *));
#ifdef PX_DEBUG
void px_debug(const char *fmt, ...);
#else
#define px_debug(...)
#endif
#define px_md_result_size(md) (md)->result_size(md)
#define px_md_block_size(md) (md)->block_size(md)
#define px_md_reset(md) (md)->reset(md)