[PATCH] pgcrypto: pgp_encrypt

From: Marko Kreen <marko(at)l-t(dot)ee>
To: pgsql-patches(at)postgresql(dot)org
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: [PATCH] pgcrypto: pgp_encrypt
Date: 2005-06-22 23:27:57
Message-ID: 20050622232757.GA31711@l-t.ee
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-patches

Finally, here is pgp_encrypt()/pgp_decrypt() - implementation
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 support.

--
marko

PS. openssl/3des regression test fails, I'll send fix once
I understand why its happening.

Attachment Content-Type Size
pgp.diff.gz application/octet-stream 27.1 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Denis Lussier 2005-06-22 23:44:58 PL/pgSQL Debugger Support
Previous Message Tom Lane 2005-06-22 21:23:29 Re: pg_terminate_backend idea

Browse pgsql-patches by date

  From Date Subject
Next Message Denis Lussier 2005-06-22 23:44:58 PL/pgSQL Debugger Support
Previous Message Chris Campbell 2005-06-22 21:08:17 Re: Problem with dblink regression test