bytea field, a c function and pgcrypto driving me mad

From: Glyn Astill <glynastill(at)yahoo(dot)co(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: bytea field, a c function and pgcrypto driving me mad
Date: 2008-10-30 17:27:58
Message-ID: 921000.45816.qm@web23602.mail.ird.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi chaps,

I think I'm going to struggle to describe this, but hopefully someone can squint and see where I'm going wrong.

I've got a c function called "ftest", all it does is take some text and prepend "abcdefghijklmnopqr" onto it. I use it to pass a key into pgp_sym_encrypt/decrypt working on a bytea field in a table. The problem is that once the string I pass to "ftest" is longer than 10 characters it stops working when I use it with the bytea column and pgp_sym_decrypt, but it appears to work fine on it's own.

1) The source is here:

http://privatepaste.com/890Bj3FGW0

2) I created a little makefile, as follows:

MODULES = testf
PGXS := $(shell pg_config --pgxs)
include $(PGXS)

3) Then I did make, make install and created the function in the database:

CREATE OR REPLACE FUNCTION
testf( TEXT )
RETURNS
TEXT
AS
'testf.so', 'testf'
LANGUAGE
C
STRICT
IMMUTABLE;

REVOKE ALL ON FUNCTION testf( TEXT ) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION testf( TEXT ) TO admins;

4) I created a table mytest as follows:

CREATE TABLE mytest(
username TEXT PRIMARY KEY,
password BYTEA NOT NULL
);

5) Now with a 10 character string passed to ftest this works:

TEST=# insert into mytest (username,password) values ('short_user', pgp_sym_encrypt('testword', testf('short_user')));
INSERT 0 1
TEST=# select pgp_sym_decrypt(password, testf('short_user')) from mytest where username = 'short_user';
pgp_sym_decrypt
-----------------
testword
(1 row)

6) However if the I make the string longer, the decryption fails:

TEST=# insert into mytest (username,password) values ('longer_user', pgp_sym_encrypt('testword', testf('longer_user')));
INSERT 0 1
TEST=# select pgp_sym_decrypt(password, testf('longer_user')) from mytest where username = 'longer_user';
ERROR: Wrong key or corrupt data

But the C function appears to be working on it's own:

TEST=# select testf('longer_user');
testf
-------------------------------
abcdefghijklmnopqrlonger_user
(1 row)

7) But, if I insert the data into the table without using my function it works:

TEST=# insert into mytest (username,password) values ('longer_user', pgp_sym_encrypt('testword', 'abcdefghijklmnopqrlonger_user'));
INSERT 0 1
TEST=# select pgp_sym_decrypt(password, testf('longer_user')) from mytest where username = 'longer_user';
pgp_sym_decrypt
-----------------
testword
(1 row)

So it appears that my function is only working in conjunction with pgp_sym_encrypt on an insert when the text value I pass into it is less than 10 characters long.

It's driving me nuts, can anyone see what I'm doing wrong?

Thanks
Glyn

Responses

Browse pgsql-general by date

  From Date Subject
Next Message WaGathoni 2008-10-30 17:59:28 Re: Group BY and Chart of Accounts
Previous Message Joshua D. Drake 2008-10-30 17:13:16 Re: Decreasing WAL size effects