2000-10-31 16:11:28 +03:00
|
|
|
/*
|
|
|
|
* pgcrypto.c
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
* Various cryptographic stuff for PostgreSQL.
|
2001-03-22 07:01:46 +03:00
|
|
|
*
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
* Copyright (c) 2001 Marko Kreen
|
2000-10-31 16:11:28 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2001-03-22 07:01:46 +03:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2000-10-31 16:11:28 +03:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
2001-03-22 07:01:46 +03:00
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2000-10-31 16:11:28 +03:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2001-03-22 07:01:46 +03:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
2000-10-31 16:11:28 +03:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2005-03-21 08:18:46 +03:00
|
|
|
* $PostgreSQL: pgsql/contrib/pgcrypto/pgcrypto.c,v 1.17 2005/03/21 05:18:45 neilc Exp $
|
2000-10-31 16:11:28 +03:00
|
|
|
*/
|
|
|
|
|
2004-05-07 04:24:59 +04:00
|
|
|
#include "postgres.h"
|
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
#include <ctype.h>
|
2000-10-31 16:11:28 +03:00
|
|
|
|
2004-05-07 04:24:59 +04:00
|
|
|
#include "fmgr.h"
|
|
|
|
#include "parser/scansup.h"
|
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
#include "px.h"
|
|
|
|
#include "px-crypt.h"
|
2000-10-31 16:11:28 +03:00
|
|
|
#include "pgcrypto.h"
|
|
|
|
|
|
|
|
/* private stuff */
|
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
typedef int (*PFN) (const char *name, void **res);
|
|
|
|
static void *
|
2001-10-25 09:50:21 +04:00
|
|
|
find_provider(text *name, PFN pf, char *desc, int silent);
|
2000-10-31 16:11:28 +03:00
|
|
|
|
2005-03-21 08:18:46 +03:00
|
|
|
/* SQL function: hash(bytea, text) returns bytea */
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_FUNCTION_INFO_V1(pg_digest);
|
2000-11-20 23:36:57 +03:00
|
|
|
|
2000-10-31 16:11:28 +03:00
|
|
|
Datum
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
pg_digest(PG_FUNCTION_ARGS)
|
2000-10-31 16:11:28 +03:00
|
|
|
{
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
bytea *arg;
|
2001-03-22 07:01:46 +03:00
|
|
|
text *name;
|
2001-11-20 18:50:53 +03:00
|
|
|
unsigned len,
|
2001-03-22 07:01:46 +03:00
|
|
|
hlen;
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PX_MD *md;
|
|
|
|
bytea *res;
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2000-10-31 16:11:28 +03:00
|
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
|
|
|
|
PG_RETURN_NULL();
|
2001-03-22 07:01:46 +03:00
|
|
|
|
|
|
|
name = PG_GETARG_TEXT_P(1);
|
2000-10-31 16:11:28 +03:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
/* will give error if fails */
|
|
|
|
md = find_provider(name, (PFN) px_find_digest, "Digest", 0);
|
|
|
|
|
|
|
|
hlen = px_md_result_size(md);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
|
|
|
res = (text *) palloc(hlen + VARHDRSZ);
|
2001-01-24 06:46:16 +03:00
|
|
|
VARATT_SIZEP(res) = hlen + VARHDRSZ;
|
2001-03-22 07:01:46 +03:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
arg = PG_GETARG_BYTEA_P(0);
|
2000-10-31 16:11:28 +03:00
|
|
|
len = VARSIZE(arg) - VARHDRSZ;
|
2001-03-22 07:01:46 +03:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
px_md_update(md, VARDATA(arg), len);
|
|
|
|
px_md_finish(md, VARDATA(res));
|
|
|
|
px_md_free(md);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2000-10-31 16:11:28 +03:00
|
|
|
PG_FREE_IF_COPY(arg, 0);
|
2001-02-06 21:05:13 +03:00
|
|
|
PG_FREE_IF_COPY(name, 1);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_RETURN_BYTEA_P(res);
|
2000-10-31 16:11:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if given hash exists */
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_FUNCTION_INFO_V1(pg_digest_exists);
|
2000-11-20 23:36:57 +03:00
|
|
|
|
2000-10-31 16:11:28 +03:00
|
|
|
Datum
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
pg_digest_exists(PG_FUNCTION_ARGS)
|
2000-10-31 16:11:28 +03:00
|
|
|
{
|
2001-03-22 07:01:46 +03:00
|
|
|
text *name;
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PX_MD *res;
|
2000-10-31 16:11:28 +03:00
|
|
|
|
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_NULL();
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2000-10-31 16:11:28 +03:00
|
|
|
name = PG_GETARG_TEXT_P(0);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
res = find_provider(name, (PFN) px_find_digest, "Digest", 1);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2001-01-09 19:07:14 +03:00
|
|
|
PG_FREE_IF_COPY(name, 0);
|
2000-10-31 16:11:28 +03:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
if (res == NULL)
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
|
|
|
|
res->free(res);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
}
|
|
|
|
|
2005-03-21 08:18:46 +03:00
|
|
|
/* SQL function: hmac(data:bytea, key:bytea, type:text) returns bytea */
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_FUNCTION_INFO_V1(pg_hmac);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_hmac(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
bytea *arg;
|
|
|
|
bytea *key;
|
|
|
|
text *name;
|
2001-11-20 18:50:53 +03:00
|
|
|
unsigned len,
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
hlen,
|
|
|
|
klen;
|
|
|
|
PX_HMAC *h;
|
|
|
|
bytea *res;
|
|
|
|
|
|
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
name = PG_GETARG_TEXT_P(2);
|
|
|
|
|
|
|
|
/* will give error if fails */
|
|
|
|
h = find_provider(name, (PFN) px_find_hmac, "HMAC", 0);
|
|
|
|
|
|
|
|
hlen = px_hmac_result_size(h);
|
|
|
|
|
|
|
|
res = (text *) palloc(hlen + VARHDRSZ);
|
|
|
|
VARATT_SIZEP(res) = hlen + VARHDRSZ;
|
|
|
|
|
|
|
|
arg = PG_GETARG_BYTEA_P(0);
|
|
|
|
key = PG_GETARG_BYTEA_P(1);
|
|
|
|
len = VARSIZE(arg) - VARHDRSZ;
|
|
|
|
klen = VARSIZE(key) - VARHDRSZ;
|
|
|
|
|
|
|
|
px_hmac_init(h, VARDATA(key), klen);
|
|
|
|
px_hmac_update(h, VARDATA(arg), len);
|
|
|
|
px_hmac_finish(h, VARDATA(res));
|
|
|
|
px_hmac_free(h);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(arg, 0);
|
|
|
|
PG_FREE_IF_COPY(key, 1);
|
|
|
|
PG_FREE_IF_COPY(name, 2);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if given hmac type exists */
|
|
|
|
PG_FUNCTION_INFO_V1(pg_hmac_exists);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_hmac_exists(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *name;
|
|
|
|
PX_HMAC *h;
|
|
|
|
|
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
name = PG_GETARG_TEXT_P(0);
|
|
|
|
|
|
|
|
h = find_provider(name, (PFN) px_find_hmac, "HMAC", 1);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(name, 0);
|
|
|
|
|
|
|
|
if (h != NULL)
|
|
|
|
{
|
|
|
|
px_hmac_free(h);
|
2000-10-31 16:11:28 +03:00
|
|
|
PG_RETURN_BOOL(true);
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
}
|
2000-10-31 16:11:28 +03:00
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
|
|
|
|
/* SQL function: pg_gen_salt(text) returns text */
|
|
|
|
PG_FUNCTION_INFO_V1(pg_gen_salt);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_gen_salt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *arg0;
|
2001-11-20 18:50:53 +03:00
|
|
|
unsigned len;
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
text *res;
|
|
|
|
char buf[PX_MAX_SALT_LEN + 1];
|
|
|
|
|
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
arg0 = PG_GETARG_TEXT_P(0);
|
|
|
|
|
|
|
|
len = VARSIZE(arg0) - VARHDRSZ;
|
|
|
|
len = len > PX_MAX_SALT_LEN ? PX_MAX_SALT_LEN : len;
|
|
|
|
memcpy(buf, VARDATA(arg0), len);
|
|
|
|
buf[len] = 0;
|
2001-09-23 08:12:44 +04:00
|
|
|
len = px_gen_salt(buf, buf, 0);
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
if (len == 0)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("no such crypt algorithm")));
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
|
|
|
|
res = (text *) palloc(len + VARHDRSZ);
|
|
|
|
VARATT_SIZEP(res) = len + VARHDRSZ;
|
|
|
|
memcpy(VARDATA(res), buf, len);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(arg0, 0);
|
|
|
|
|
2001-09-23 08:12:44 +04:00
|
|
|
PG_RETURN_TEXT_P(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SQL function: pg_gen_salt(text, int4) returns text */
|
|
|
|
PG_FUNCTION_INFO_V1(pg_gen_salt_rounds);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_gen_salt_rounds(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *arg0;
|
|
|
|
int rounds;
|
2001-11-20 18:50:53 +03:00
|
|
|
unsigned len;
|
2001-09-23 08:12:44 +04:00
|
|
|
text *res;
|
|
|
|
char buf[PX_MAX_SALT_LEN + 1];
|
|
|
|
|
|
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
arg0 = PG_GETARG_TEXT_P(0);
|
|
|
|
rounds = PG_GETARG_INT32(1);
|
|
|
|
|
|
|
|
len = VARSIZE(arg0) - VARHDRSZ;
|
|
|
|
len = len > PX_MAX_SALT_LEN ? PX_MAX_SALT_LEN : len;
|
|
|
|
memcpy(buf, VARDATA(arg0), len);
|
|
|
|
buf[len] = 0;
|
|
|
|
len = px_gen_salt(buf, buf, rounds);
|
|
|
|
if (len == 0)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2003-08-04 04:43:34 +04:00
|
|
|
errmsg("no such crypt algorithm or bad number of rounds")));
|
2001-09-23 08:12:44 +04:00
|
|
|
|
|
|
|
res = (text *) palloc(len + VARHDRSZ);
|
|
|
|
VARATT_SIZEP(res) = len + VARHDRSZ;
|
|
|
|
memcpy(VARDATA(res), buf, len);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(arg0, 0);
|
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_RETURN_TEXT_P(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SQL function: pg_crypt(psw:text, salt:text) returns text */
|
|
|
|
PG_FUNCTION_INFO_V1(pg_crypt);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_crypt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *arg0;
|
|
|
|
text *arg1;
|
2001-11-20 18:50:53 +03:00
|
|
|
unsigned len0,
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
len1,
|
|
|
|
clen;
|
|
|
|
char *buf0,
|
|
|
|
*buf1,
|
|
|
|
*cres,
|
|
|
|
*resbuf;
|
|
|
|
text *res;
|
|
|
|
|
|
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
arg0 = PG_GETARG_TEXT_P(0);
|
|
|
|
arg1 = PG_GETARG_TEXT_P(1);
|
|
|
|
len0 = VARSIZE(arg0) - VARHDRSZ;
|
|
|
|
len1 = VARSIZE(arg1) - VARHDRSZ;
|
|
|
|
|
|
|
|
buf0 = palloc(len0 + 1);
|
|
|
|
buf1 = palloc(len1 + 1);
|
|
|
|
|
|
|
|
memcpy(buf0, VARDATA(arg0), len0);
|
|
|
|
memcpy(buf1, VARDATA(arg1), len1);
|
|
|
|
|
|
|
|
buf0[len0] = '\0';
|
|
|
|
buf1[len1] = '\0';
|
|
|
|
|
|
|
|
resbuf = palloc(PX_MAX_CRYPT);
|
|
|
|
|
|
|
|
memset(resbuf, 0, PX_MAX_CRYPT);
|
|
|
|
|
|
|
|
cres = px_crypt(buf0, buf1, resbuf, PX_MAX_CRYPT);
|
|
|
|
|
|
|
|
pfree(buf0);
|
|
|
|
pfree(buf1);
|
|
|
|
|
|
|
|
if (cres == NULL)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
|
|
|
errmsg("crypt(3) returned NULL")));
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
|
|
|
|
clen = strlen(cres);
|
|
|
|
|
|
|
|
res = (text *) palloc(clen + VARHDRSZ);
|
|
|
|
VARATT_SIZEP(res) = clen + VARHDRSZ;
|
|
|
|
memcpy(VARDATA(res), cres, clen);
|
|
|
|
pfree(resbuf);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(arg0, 0);
|
|
|
|
PG_FREE_IF_COPY(arg1, 1);
|
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(res);
|
|
|
|
}
|
|
|
|
|
2005-03-21 08:18:46 +03:00
|
|
|
/* SQL function: pg_encrypt(bytea, bytea, text) returns bytea */
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_FUNCTION_INFO_V1(pg_encrypt);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_encrypt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 09:50:21 +04:00
|
|
|
int err;
|
|
|
|
bytea *data,
|
|
|
|
*key,
|
|
|
|
*res;
|
|
|
|
text *type;
|
|
|
|
PX_Combo *c;
|
2001-11-20 18:50:53 +03:00
|
|
|
unsigned dlen,
|
2001-10-25 09:50:21 +04:00
|
|
|
klen,
|
|
|
|
rlen;
|
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
|
|
|
|
PG_RETURN_NULL();
|
2001-10-25 09:50:21 +04:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
type = PG_GETARG_TEXT_P(2);
|
2001-10-25 09:50:21 +04:00
|
|
|
c = find_provider(type, (PFN) px_find_combo, "Cipher", 0);
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
|
|
|
|
data = PG_GETARG_BYTEA_P(0);
|
|
|
|
key = PG_GETARG_BYTEA_P(1);
|
|
|
|
dlen = VARSIZE(data) - VARHDRSZ;
|
|
|
|
klen = VARSIZE(key) - VARHDRSZ;
|
|
|
|
|
|
|
|
rlen = px_combo_encrypt_len(c, dlen);
|
|
|
|
res = palloc(VARHDRSZ + rlen);
|
|
|
|
|
|
|
|
err = px_combo_init(c, VARDATA(key), klen, NULL, 0);
|
|
|
|
if (!err)
|
|
|
|
err = px_combo_encrypt(c, VARDATA(data), dlen, VARDATA(res), &rlen);
|
|
|
|
px_combo_free(c);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(data, 0);
|
|
|
|
PG_FREE_IF_COPY(key, 1);
|
|
|
|
PG_FREE_IF_COPY(type, 2);
|
2001-10-25 09:50:21 +04:00
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
pfree(res);
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
|
|
|
errmsg("encrypt error: %d", err)));
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VARATT_SIZEP(res) = VARHDRSZ + rlen;
|
|
|
|
PG_RETURN_BYTEA_P(res);
|
|
|
|
}
|
|
|
|
|
2005-03-21 08:18:46 +03:00
|
|
|
/* SQL function: pg_decrypt(bytea, bytea, text) returns bytea */
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_FUNCTION_INFO_V1(pg_decrypt);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_decrypt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 09:50:21 +04:00
|
|
|
int err;
|
|
|
|
bytea *data,
|
|
|
|
*key,
|
|
|
|
*res;
|
|
|
|
text *type;
|
|
|
|
PX_Combo *c;
|
2001-11-20 18:50:53 +03:00
|
|
|
unsigned dlen,
|
2001-10-25 09:50:21 +04:00
|
|
|
klen,
|
|
|
|
rlen;
|
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
|
|
|
|
PG_RETURN_NULL();
|
2001-10-25 09:50:21 +04:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
type = PG_GETARG_TEXT_P(2);
|
2001-10-25 09:50:21 +04:00
|
|
|
c = find_provider(type, (PFN) px_find_combo, "Cipher", 0);
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
|
|
|
|
data = PG_GETARG_BYTEA_P(0);
|
|
|
|
key = PG_GETARG_BYTEA_P(1);
|
|
|
|
dlen = VARSIZE(data) - VARHDRSZ;
|
|
|
|
klen = VARSIZE(key) - VARHDRSZ;
|
|
|
|
|
|
|
|
rlen = px_combo_decrypt_len(c, dlen);
|
|
|
|
res = palloc(VARHDRSZ + rlen);
|
|
|
|
|
|
|
|
err = px_combo_init(c, VARDATA(key), klen, NULL, 0);
|
|
|
|
if (!err)
|
|
|
|
err = px_combo_decrypt(c, VARDATA(data), dlen, VARDATA(res), &rlen);
|
|
|
|
|
|
|
|
px_combo_free(c);
|
|
|
|
|
|
|
|
if (err)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
|
|
|
errmsg("decrypt error: %d", err)));
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
|
|
|
|
VARATT_SIZEP(res) = VARHDRSZ + rlen;
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(data, 0);
|
|
|
|
PG_FREE_IF_COPY(key, 1);
|
|
|
|
PG_FREE_IF_COPY(type, 2);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(res);
|
|
|
|
}
|
|
|
|
|
2005-03-21 08:18:46 +03:00
|
|
|
/* SQL function: pg_encrypt_iv(bytea, bytea, bytea, text) returns bytea */
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_FUNCTION_INFO_V1(pg_encrypt_iv);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_encrypt_iv(PG_FUNCTION_ARGS)
|
2000-10-31 16:11:28 +03:00
|
|
|
{
|
2001-10-25 09:50:21 +04:00
|
|
|
int err;
|
|
|
|
bytea *data,
|
|
|
|
*key,
|
|
|
|
*iv,
|
|
|
|
*res;
|
|
|
|
text *type;
|
|
|
|
PX_Combo *c;
|
2001-11-20 18:50:53 +03:00
|
|
|
unsigned dlen,
|
2001-10-25 09:50:21 +04:00
|
|
|
klen,
|
|
|
|
ivlen,
|
|
|
|
rlen;
|
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1)
|
2001-10-25 09:50:21 +04:00
|
|
|
|| PG_ARGISNULL(2) || PG_ARGISNULL(3))
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_RETURN_NULL();
|
2001-10-25 09:50:21 +04:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
type = PG_GETARG_TEXT_P(3);
|
2001-10-25 09:50:21 +04:00
|
|
|
c = find_provider(type, (PFN) px_find_combo, "Cipher", 0);
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
|
|
|
|
data = PG_GETARG_BYTEA_P(0);
|
|
|
|
key = PG_GETARG_BYTEA_P(1);
|
|
|
|
iv = PG_GETARG_BYTEA_P(2);
|
|
|
|
dlen = VARSIZE(data) - VARHDRSZ;
|
|
|
|
klen = VARSIZE(key) - VARHDRSZ;
|
|
|
|
ivlen = VARSIZE(iv) - VARHDRSZ;
|
|
|
|
|
|
|
|
rlen = px_combo_encrypt_len(c, dlen);
|
|
|
|
res = palloc(VARHDRSZ + rlen);
|
|
|
|
|
|
|
|
err = px_combo_init(c, VARDATA(key), klen, VARDATA(iv), ivlen);
|
|
|
|
if (!err)
|
|
|
|
px_combo_encrypt(c, VARDATA(data), dlen, VARDATA(res), &rlen);
|
|
|
|
|
|
|
|
px_combo_free(c);
|
|
|
|
|
|
|
|
if (err)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
|
|
|
errmsg("encrypt_iv error: %d", err)));
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
|
|
|
|
VARATT_SIZEP(res) = VARHDRSZ + rlen;
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(data, 0);
|
|
|
|
PG_FREE_IF_COPY(key, 1);
|
|
|
|
PG_FREE_IF_COPY(iv, 2);
|
|
|
|
PG_FREE_IF_COPY(type, 3);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(res);
|
|
|
|
}
|
|
|
|
|
2005-03-21 08:18:46 +03:00
|
|
|
/* SQL function: pg_decrypt_iv(bytea, bytea, bytea, text) returns bytea */
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_FUNCTION_INFO_V1(pg_decrypt_iv);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_decrypt_iv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 09:50:21 +04:00
|
|
|
int err;
|
|
|
|
bytea *data,
|
|
|
|
*key,
|
|
|
|
*iv,
|
|
|
|
*res;
|
|
|
|
text *type;
|
|
|
|
PX_Combo *c;
|
2001-11-20 18:50:53 +03:00
|
|
|
unsigned dlen,
|
2001-10-25 09:50:21 +04:00
|
|
|
klen,
|
|
|
|
rlen,
|
|
|
|
ivlen;
|
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1)
|
2001-10-25 09:50:21 +04:00
|
|
|
|| PG_ARGISNULL(2) || PG_ARGISNULL(3))
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_RETURN_NULL();
|
2001-10-25 09:50:21 +04:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
type = PG_GETARG_TEXT_P(3);
|
2001-10-25 09:50:21 +04:00
|
|
|
c = find_provider(type, (PFN) px_find_combo, "Cipher", 0);
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
|
|
|
|
data = PG_GETARG_BYTEA_P(0);
|
|
|
|
key = PG_GETARG_BYTEA_P(1);
|
|
|
|
iv = PG_GETARG_BYTEA_P(2);
|
|
|
|
dlen = VARSIZE(data) - VARHDRSZ;
|
|
|
|
klen = VARSIZE(key) - VARHDRSZ;
|
|
|
|
ivlen = VARSIZE(iv) - VARHDRSZ;
|
|
|
|
|
|
|
|
rlen = px_combo_decrypt_len(c, dlen);
|
|
|
|
res = palloc(VARHDRSZ + rlen);
|
|
|
|
|
|
|
|
err = px_combo_init(c, VARDATA(key), klen, VARDATA(iv), ivlen);
|
|
|
|
if (!err)
|
|
|
|
px_combo_decrypt(c, VARDATA(data), dlen, VARDATA(res), &rlen);
|
|
|
|
|
|
|
|
px_combo_free(c);
|
|
|
|
|
|
|
|
if (err)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
|
|
|
errmsg("decrypt_iv error: %d", err)));
|
2001-10-25 09:50:21 +04:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
VARATT_SIZEP(res) = VARHDRSZ + rlen;
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(data, 0);
|
|
|
|
PG_FREE_IF_COPY(key, 1);
|
|
|
|
PG_FREE_IF_COPY(iv, 2);
|
|
|
|
PG_FREE_IF_COPY(type, 3);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(res);
|
|
|
|
}
|
|
|
|
|
2005-03-21 08:18:46 +03:00
|
|
|
/* SQL function: pg_cipher_exists(text) returns bool */
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PG_FUNCTION_INFO_V1(pg_cipher_exists);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_cipher_exists(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 09:50:21 +04:00
|
|
|
text *arg;
|
|
|
|
PX_Combo *c;
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
|
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
arg = PG_GETARG_TEXT_P(0);
|
|
|
|
|
2001-10-25 09:50:21 +04:00
|
|
|
c = find_provider(arg, (PFN) px_find_combo, "Cipher", 1);
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
if (c != NULL)
|
|
|
|
px_combo_free(c);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL((c != NULL) ? true : false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
2001-10-25 09:50:21 +04:00
|
|
|
find_provider(text *name,
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
PFN provider_lookup,
|
|
|
|
char *desc, int silent)
|
|
|
|
{
|
|
|
|
void *res;
|
2004-05-07 04:24:59 +04:00
|
|
|
char *buf;
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
int err;
|
2001-03-22 07:01:46 +03:00
|
|
|
|
2004-05-07 04:24:59 +04:00
|
|
|
buf = downcase_truncate_identifier(VARDATA(name),
|
|
|
|
VARSIZE(name) - VARHDRSZ,
|
|
|
|
false);
|
2001-03-22 07:01:46 +03:00
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
err = provider_lookup(buf, &res);
|
|
|
|
|
|
|
|
if (err && !silent)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("%s type does not exist: \"%s\"", desc, buf)));
|
2000-10-31 16:11:28 +03:00
|
|
|
|
2004-05-07 04:24:59 +04:00
|
|
|
pfree(buf);
|
|
|
|
|
/contrib/pgcrypto:
* remove support for encode() as it is in main tree now
* remove krb5.c
* new 'PX library' architecture
* remove BSD license from my code to let the general
PostgreSQL one to apply
* md5, sha1: ANSIfy, use const where appropriate
* various other formatting and clarity changes
* hmac()
* UN*X-like crypt() - system or internal crypt
* Internal crypt: DES, Extended DES, MD5, Blowfish
crypt-des.c, crypt-md5.c from FreeBSD
crypt-blowfish.c from Solar Designer
* gen_salt() for crypt() - Blowfish, MD5, DES, Extended DES
* encrypt(), decrypt(), encrypt_iv(), decrypt_iv()
* Cipher support in mhash.c, openssl.c
* internal: Blowfish, Rijndael-128 ciphers
* blf.[ch], rijndael.[ch] from OpenBSD
* there will be generated file rijndael-tbl.inc.
Marko Kreen
2001-08-21 04:42:41 +04:00
|
|
|
return err ? NULL : res;
|
2000-10-31 16:11:28 +03:00
|
|
|
}
|