3 * Various cryptographic stuff for PostgreSQL.
5 * Copyright (c) 2001 Marko Kreen
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * contrib/pgcrypto/pgcrypto.c
58 typedef int (*
PFN) (
const char *
name,
void **res);
65 * Entrypoint of this module.
71 "Sets if builtin crypto functions are enabled.",
72 "\"on\" enables builtin crypto, \"off\" unconditionally disables and \"fips\" "
73 "will disable builtin crypto if OpenSSL is in FIPS mode",
85/* SQL function: hash(bytea, text) returns bytea */
100 /* will give error if fails */
121/* SQL function: hmac(data:bytea, key:bytea, type:text) returns bytea */
138 /* will give error if fails */
164/* SQL function: pg_gen_salt(text) returns text */
178 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
186/* SQL function: pg_gen_salt(text, int4) returns text */
201 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
209/* SQL function: pg_crypt(psw:text, salt:text) returns text */
235 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
236 errmsg(
"crypt(3) returned NULL")));
248/* SQL function: pg_encrypt(bytea, bytea, text) returns bytea */
289 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
297/* SQL function: pg_decrypt(bytea, bytea, text) returns bytea */
333 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
345/* SQL function: pg_encrypt_iv(bytea, bytea, bytea, text) returns bytea */
386 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
399/* SQL function: pg_decrypt_iv(bytea, bytea, bytea, text) returns bytea */
440 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
453/* SQL function: pg_random_bytes(int4) returns bytea */
462 if (len < 1 || len > 1024)
464 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
465 errmsg(
"Length not in range")));
470 /* generate result */
477/* SQL function: gen_random_uuid() returns uuid */
483 /* redirect to built-in function */
498 const char *desc,
int silent)
508 err = provider_lookup(
buf, &res);
512 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
517 return err ? NULL : res;
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void err(int eval, const char *fmt,...)
#define PG_FREE_IF_COPY(ptr, n)
#define PG_GETARG_BYTEA_PP(n)
#define PG_GETARG_TEXT_PP(n)
#define PG_RETURN_BYTEA_P(x)
#define PG_RETURN_TEXT_P(x)
#define PG_GETARG_INT32(n)
#define PG_RETURN_BOOL(x)
void DefineCustomEnumVariable(const char *name, const char *short_desc, const char *long_desc, int *valueAddr, int bootValue, const struct config_enum_entry *options, GucContext context, int flags, GucEnumCheckHook check_hook, GucEnumAssignHook assign_hook, GucShowHook show_hook)
void MarkGUCPrefixReserved(const char *className)
void pfree(void *pointer)
void * palloc0(Size size)
int px_find_digest(const char *name, PX_MD **res)
Datum pg_encrypt_iv(PG_FUNCTION_ARGS)
static const struct config_enum_entry builtin_crypto_options[]
Datum pg_encrypt(PG_FUNCTION_ARGS)
static void * find_provider(text *name, PFN provider_lookup, const char *desc, int silent)
Datum pg_check_fipsmode(PG_FUNCTION_ARGS)
Datum pg_gen_salt_rounds(PG_FUNCTION_ARGS)
Datum pg_decrypt_iv(PG_FUNCTION_ARGS)
PG_MODULE_MAGIC_EXT(.name="pgcrypto",.version=PG_VERSION)
Datum pg_random_uuid(PG_FUNCTION_ARGS)
Datum pg_hmac(PG_FUNCTION_ARGS)
Datum pg_crypt(PG_FUNCTION_ARGS)
int(* PFN)(const char *name, void **res)
Datum pg_random_bytes(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(pg_digest)
Datum pg_gen_salt(PG_FUNCTION_ARGS)
int builtin_crypto_enabled
Datum pg_digest(PG_FUNCTION_ARGS)
Datum pg_decrypt(PG_FUNCTION_ARGS)
bool pg_strong_random(void *buf, size_t len)
int px_gen_salt(const char *salt_type, char *buf, int rounds)
char * px_crypt(const char *psw, const char *salt, char *buf, unsigned len)
int px_find_hmac(const char *name, PX_HMAC **res)
void px_THROW_ERROR(int err)
const char * px_strerror(int err)
int px_find_combo(const char *name, PX_Combo **res)
#define px_md_finish(md, buf)
#define px_combo_init(c, key, klen, iv, ivlen)
#define px_hmac_result_size(hmac)
#define px_hmac_finish(hmac, buf)
#define px_combo_encrypt_len(c, dlen)
#define px_hmac_update(hmac, data, dlen)
#define px_hmac_init(hmac, key, klen)
#define px_combo_encrypt(c, data, dlen, res, rlen)
#define px_hmac_free(hmac)
#define px_combo_decrypt_len(c, dlen)
#define px_combo_decrypt(c, data, dlen, res, rlen)
#define px_md_update(md, data, dlen)
#define px_md_result_size(md)
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Datum gen_random_uuid(PG_FUNCTION_ARGS)
static Size VARSIZE_ANY_EXHDR(const void *PTR)
static char * VARDATA(const void *PTR)
static char * VARDATA_ANY(const void *PTR)
static void SET_VARSIZE(void *PTR, Size len)
text * cstring_to_text_with_len(const char *s, int len)
text * cstring_to_text(const char *s)
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
char * text_to_cstring(const text *t)