#include "postgres.h"
#include <ctype.h>
#include "parser/scansup.h"
#include "utils/builtins.h"
#include "px.h"
#include "px-crypt.h"
#include "pgcrypto.h"
Go to the source code of this file.
Typedefs | |
typedef int(* | PFN )(const char *name, void **res) |
Functions | |
static void * | find_provider (text *name, PFN pf, char *desc, int silent) |
PG_FUNCTION_INFO_V1 (pg_digest) | |
Datum | pg_digest (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (pg_hmac) | |
Datum | pg_hmac (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (pg_gen_salt) | |
Datum | pg_gen_salt (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (pg_gen_salt_rounds) | |
Datum | pg_gen_salt_rounds (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (pg_crypt) | |
Datum | pg_crypt (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (pg_encrypt) | |
Datum | pg_encrypt (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (pg_decrypt) | |
Datum | pg_decrypt (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (pg_encrypt_iv) | |
Datum | pg_encrypt_iv (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (pg_decrypt_iv) | |
Datum | pg_decrypt_iv (PG_FUNCTION_ARGS) |
PG_FUNCTION_INFO_V1 (pg_random_bytes) | |
Datum | pg_random_bytes (PG_FUNCTION_ARGS) |
Variables | |
PG_MODULE_MAGIC |
Definition at line 47 of file pgcrypto.c.
Definition at line 447 of file pgcrypto.c.
References buf, downcase_truncate_identifier(), ereport, errcode(), errmsg(), ERROR, NULL, pfree(), px_strerror(), VARDATA, VARHDRSZ, and VARSIZE.
Referenced by pg_decrypt(), pg_decrypt_iv(), pg_digest(), pg_encrypt(), pg_encrypt_iv(), and pg_hmac().
{ void *res; char *buf; int err; buf = downcase_truncate_identifier(VARDATA(name), VARSIZE(name) - VARHDRSZ, false); err = provider_lookup(buf, &res); if (err && !silent) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Cannot use \"%s\": %s", buf, px_strerror(err)))); pfree(buf); return err ? NULL : res; }
Datum pg_crypt | ( | PG_FUNCTION_ARGS | ) |
Definition at line 178 of file pgcrypto.c.
References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, NULL, palloc0(), pfree(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, px_crypt(), PX_MAX_CRYPT, and text_to_cstring().
{ text *arg0 = PG_GETARG_TEXT_PP(0); text *arg1 = PG_GETARG_TEXT_PP(1); char *buf0, *buf1, *cres, *resbuf; text *res; buf0 = text_to_cstring(arg0); buf1 = text_to_cstring(arg1); resbuf = palloc0(PX_MAX_CRYPT); cres = px_crypt(buf0, buf1, resbuf, PX_MAX_CRYPT); pfree(buf0); pfree(buf1); if (cres == NULL) ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("crypt(3) returned NULL"))); res = cstring_to_text(cres); pfree(resbuf); PG_FREE_IF_COPY(arg0, 0); PG_FREE_IF_COPY(arg1, 1); PG_RETURN_TEXT_P(res); }
Datum pg_decrypt | ( | PG_FUNCTION_ARGS | ) |
Definition at line 266 of file pgcrypto.c.
References ereport, errcode(), errmsg(), ERROR, find_provider(), NULL, palloc(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_P, PG_GETARG_TEXT_P, PG_RETURN_BYTEA_P, px_combo_decrypt, px_combo_decrypt_len, px_combo_free, px_combo_init, px_find_combo(), px_strerror(), SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.
{ int err; bytea *data, *key, *res; text *type; PX_Combo *c; unsigned dlen, klen, rlen; type = PG_GETARG_TEXT_P(2); c = find_provider(type, (PFN) px_find_combo, "Cipher", 0); 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, (uint8 *) VARDATA(key), klen, NULL, 0); if (!err) err = px_combo_decrypt(c, (uint8 *) VARDATA(data), dlen, (uint8 *) VARDATA(res), &rlen); px_combo_free(c); if (err) ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("decrypt error: %s", px_strerror(err)))); SET_VARSIZE(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); }
Datum pg_decrypt_iv | ( | PG_FUNCTION_ARGS | ) |
Definition at line 368 of file pgcrypto.c.
References ereport, errcode(), errmsg(), ERROR, find_provider(), palloc(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_P, PG_GETARG_TEXT_P, PG_RETURN_BYTEA_P, px_combo_decrypt, px_combo_decrypt_len, px_combo_free, px_combo_init, px_find_combo(), px_strerror(), SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.
{ int err; bytea *data, *key, *iv, *res; text *type; PX_Combo *c; unsigned dlen, klen, rlen, ivlen; type = PG_GETARG_TEXT_P(3); c = find_provider(type, (PFN) px_find_combo, "Cipher", 0); 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, (uint8 *) VARDATA(key), klen, (uint8 *) VARDATA(iv), ivlen); if (!err) err = px_combo_decrypt(c, (uint8 *) VARDATA(data), dlen, (uint8 *) VARDATA(res), &rlen); px_combo_free(c); if (err) ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("decrypt_iv error: %s", px_strerror(err)))); SET_VARSIZE(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); }
Datum pg_digest | ( | PG_FUNCTION_ARGS | ) |
Definition at line 54 of file pgcrypto.c.
References arg, find_provider(), name, palloc(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_P, PG_GETARG_TEXT_P, PG_RETURN_BYTEA_P, px_find_digest(), px_md_finish, px_md_free, px_md_result_size, px_md_update, SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.
{ bytea *arg; text *name; unsigned len, hlen; PX_MD *md; bytea *res; name = PG_GETARG_TEXT_P(1); /* will give error if fails */ md = find_provider(name, (PFN) px_find_digest, "Digest", 0); hlen = px_md_result_size(md); res = (text *) palloc(hlen + VARHDRSZ); SET_VARSIZE(res, hlen + VARHDRSZ); arg = PG_GETARG_BYTEA_P(0); len = VARSIZE(arg) - VARHDRSZ; px_md_update(md, (uint8 *) VARDATA(arg), len); px_md_finish(md, (uint8 *) VARDATA(res)); px_md_free(md); PG_FREE_IF_COPY(arg, 0); PG_FREE_IF_COPY(name, 1); PG_RETURN_BYTEA_P(res); }
Datum pg_encrypt | ( | PG_FUNCTION_ARGS | ) |
Definition at line 217 of file pgcrypto.c.
References ereport, errcode(), errmsg(), ERROR, find_provider(), NULL, palloc(), pfree(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_P, PG_GETARG_TEXT_P, PG_RETURN_BYTEA_P, px_combo_encrypt, px_combo_encrypt_len, px_combo_free, px_combo_init, px_find_combo(), px_strerror(), SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.
{ int err; bytea *data, *key, *res; text *type; PX_Combo *c; unsigned dlen, klen, rlen; type = PG_GETARG_TEXT_P(2); c = find_provider(type, (PFN) px_find_combo, "Cipher", 0); 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, (uint8 *) VARDATA(key), klen, NULL, 0); if (!err) err = px_combo_encrypt(c, (uint8 *) VARDATA(data), dlen, (uint8 *) 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); if (err) { pfree(res); ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("encrypt error: %s", px_strerror(err)))); } SET_VARSIZE(res, VARHDRSZ + rlen); PG_RETURN_BYTEA_P(res); }
Datum pg_encrypt_iv | ( | PG_FUNCTION_ARGS | ) |
Definition at line 314 of file pgcrypto.c.
References ereport, errcode(), errmsg(), ERROR, find_provider(), palloc(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_P, PG_GETARG_TEXT_P, PG_RETURN_BYTEA_P, px_combo_encrypt, px_combo_encrypt_len, px_combo_free, px_combo_init, px_find_combo(), px_strerror(), SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.
{ int err; bytea *data, *key, *iv, *res; text *type; PX_Combo *c; unsigned dlen, klen, ivlen, rlen; type = PG_GETARG_TEXT_P(3); c = find_provider(type, (PFN) px_find_combo, "Cipher", 0); 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, (uint8 *) VARDATA(key), klen, (uint8 *) VARDATA(iv), ivlen); if (!err) err = px_combo_encrypt(c, (uint8 *) VARDATA(data), dlen, (uint8 *) VARDATA(res), &rlen); px_combo_free(c); if (err) ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("encrypt_iv error: %s", px_strerror(err)))); SET_VARSIZE(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); }
PG_FUNCTION_INFO_V1 | ( | pg_hmac | ) |
PG_FUNCTION_INFO_V1 | ( | pg_encrypt_iv | ) |
PG_FUNCTION_INFO_V1 | ( | pg_gen_salt | ) |
PG_FUNCTION_INFO_V1 | ( | pg_decrypt_iv | ) |
PG_FUNCTION_INFO_V1 | ( | pg_crypt | ) |
PG_FUNCTION_INFO_V1 | ( | pg_digest | ) |
PG_FUNCTION_INFO_V1 | ( | pg_random_bytes | ) |
PG_FUNCTION_INFO_V1 | ( | pg_gen_salt_rounds | ) |
PG_FUNCTION_INFO_V1 | ( | pg_decrypt | ) |
PG_FUNCTION_INFO_V1 | ( | pg_encrypt | ) |
Datum pg_gen_salt | ( | PG_FUNCTION_ARGS | ) |
Definition at line 133 of file pgcrypto.c.
References buf, cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, px_gen_salt(), PX_MAX_SALT_LEN, px_strerror(), and text_to_cstring_buffer().
{ text *arg0 = PG_GETARG_TEXT_PP(0); int len; char buf[PX_MAX_SALT_LEN + 1]; text_to_cstring_buffer(arg0, buf, sizeof(buf)); len = px_gen_salt(buf, buf, 0); if (len < 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("gen_salt: %s", px_strerror(len)))); PG_FREE_IF_COPY(arg0, 0); PG_RETURN_TEXT_P(cstring_to_text_with_len(buf, len)); }
Datum pg_gen_salt_rounds | ( | PG_FUNCTION_ARGS | ) |
Definition at line 155 of file pgcrypto.c.
References buf, cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, PG_FREE_IF_COPY, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, px_gen_salt(), PX_MAX_SALT_LEN, px_strerror(), and text_to_cstring_buffer().
{ text *arg0 = PG_GETARG_TEXT_PP(0); int rounds = PG_GETARG_INT32(1); int len; char buf[PX_MAX_SALT_LEN + 1]; text_to_cstring_buffer(arg0, buf, sizeof(buf)); len = px_gen_salt(buf, buf, rounds); if (len < 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("gen_salt: %s", px_strerror(len)))); PG_FREE_IF_COPY(arg0, 0); PG_RETURN_TEXT_P(cstring_to_text_with_len(buf, len)); }
Datum pg_hmac | ( | PG_FUNCTION_ARGS | ) |
Definition at line 90 of file pgcrypto.c.
References arg, find_provider(), name, palloc(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_P, PG_GETARG_TEXT_P, PG_RETURN_BYTEA_P, px_find_hmac(), px_hmac_finish, px_hmac_free, px_hmac_init, px_hmac_result_size, px_hmac_update, SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.
{ bytea *arg; bytea *key; text *name; unsigned len, hlen, klen; PX_HMAC *h; bytea *res; 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); SET_VARSIZE(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, (uint8 *) VARDATA(key), klen); px_hmac_update(h, (uint8 *) VARDATA(arg), len); px_hmac_finish(h, (uint8 *) 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); }
Datum pg_random_bytes | ( | PG_FUNCTION_ARGS | ) |
Definition at line 422 of file pgcrypto.c.
References ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_INT32, PG_RETURN_BYTEA_P, px_get_random_bytes(), px_strerror(), SET_VARSIZE, VARDATA, and VARHDRSZ.
{ int err; int len = PG_GETARG_INT32(0); bytea *res; if (len < 1 || len > 1024) ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("Length not in range"))); res = palloc(VARHDRSZ + len); SET_VARSIZE(res, VARHDRSZ + len); /* generate result */ err = px_get_random_bytes((uint8 *) VARDATA(res), len); if (err < 0) ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("Random generator error: %s", px_strerror(err)))); PG_RETURN_BYTEA_P(res); }
Definition at line 43 of file pgcrypto.c.