Thread: bytea field, a c function and pgcrypto driving me mad

bytea field, a c function and pgcrypto driving me mad

From
Glyn Astill
Date:
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




Re: bytea field, a c function and pgcrypto driving me mad

From
Martijn van Oosterhout
Date:
On Thu, Oct 30, 2008 at 05:27:58PM +0000, Glyn Astill wrote:
> 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
itto pass 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

ISTM that in this line:

keying = (text *)palloc( keylen + unamelen );

You forgot to include the length of the header VARHDRSZ.

Have  anice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.

Attachment

Re: bytea field, a c function and pgcrypto driving me mad

From
Glyn Astill
Date:
> ISTM that in this line:
>
> keying = (text *)palloc( keylen + unamelen );
>
> You forgot to include the length of the header VARHDRSZ.
>

Aha, that'd be it, it's been a long day.

Thanks Martijn