bytea field, a c function and pgcrypto driving me mad - Mailing list pgsql-general

From Glyn Astill
Subject bytea field, a c function and pgcrypto driving me mad
Date
Msg-id 921000.45816.qm@web23602.mail.ird.yahoo.com
Whole thread Raw
Responses Re: bytea field, a c function and pgcrypto driving me mad  (Martijn van Oosterhout <kleptog@svana.org>)
List 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
topass a key into pgp_sym_encrypt/decrypt working on a bytea field in a table. The problem is that once the string I
passto "ftest" is longer than 10 characters it stops working when I use it with the bytea column and pgp_sym_decrypt,
butit 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
passinto it is less than 10 characters long. 

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

Thanks
Glyn




pgsql-general by date:

Previous
From: "Joshua D. Drake"
Date:
Subject: Re: Decreasing WAL size effects
Next
From: WaGathoni
Date:
Subject: Re: Group BY and Chart of Accounts