PostgreSQL Source Code  git master
cryptohashfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * cryptohashfuncs.c
4  * Cryptographic hash functions
5  *
6  * Portions Copyright (c) 2018-2022, PostgreSQL Global Development Group
7  *
8  *
9  * IDENTIFICATION
10  * src/backend/utils/adt/cryptohashfuncs.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "common/cryptohash.h"
17 #include "common/md5.h"
18 #include "common/sha2.h"
19 #include "utils/builtins.h"
20 
21 
22 /*
23  * MD5
24  */
25 
26 /* MD5 produces a 16 byte (128 bit) hash; double it for hex */
27 #define MD5_HASH_LEN 32
28 
29 /*
30  * Create an MD5 hash of a text value and return it as hex string.
31  */
32 Datum
34 {
35  text *in_text = PG_GETARG_TEXT_PP(0);
36  size_t len;
37  char hexsum[MD5_HASH_LEN + 1];
38  const char *errstr = NULL;
39 
40  /* Calculate the length of the buffer using varlena metadata */
41  len = VARSIZE_ANY_EXHDR(in_text);
42 
43  /* get the hash result */
44  if (pg_md5_hash(VARDATA_ANY(in_text), len, hexsum, &errstr) == false)
45  ereport(ERROR,
46  (errcode(ERRCODE_INTERNAL_ERROR),
47  errmsg("could not compute %s hash: %s", "MD5",
48  errstr)));
49 
50  /* convert to text and return it */
52 }
53 
54 /*
55  * Create an MD5 hash of a bytea value and return it as a hex string.
56  */
57 Datum
59 {
60  bytea *in = PG_GETARG_BYTEA_PP(0);
61  size_t len;
62  char hexsum[MD5_HASH_LEN + 1];
63  const char *errstr = NULL;
64 
65  len = VARSIZE_ANY_EXHDR(in);
66  if (pg_md5_hash(VARDATA_ANY(in), len, hexsum, &errstr) == false)
67  ereport(ERROR,
68  (errcode(ERRCODE_INTERNAL_ERROR),
69  errmsg("could not compute %s hash: %s", "MD5",
70  errstr)));
71 
73 }
74 
75 /*
76  * Internal routine to compute a cryptohash with the given bytea input.
77  */
78 static inline bytea *
80 {
81  const uint8 *data;
82  const char *typestr = NULL;
83  int digest_len = 0;
84  size_t len;
85  pg_cryptohash_ctx *ctx;
86  bytea *result;
87 
88  switch (type)
89  {
90  case PG_SHA224:
91  typestr = "SHA224";
92  digest_len = PG_SHA224_DIGEST_LENGTH;
93  break;
94  case PG_SHA256:
95  typestr = "SHA256";
96  digest_len = PG_SHA256_DIGEST_LENGTH;
97  break;
98  case PG_SHA384:
99  typestr = "SHA384";
100  digest_len = PG_SHA384_DIGEST_LENGTH;
101  break;
102  case PG_SHA512:
103  typestr = "SHA512";
104  digest_len = PG_SHA512_DIGEST_LENGTH;
105  break;
106  case PG_MD5:
107  case PG_SHA1:
108  elog(ERROR, "unsupported cryptohash type %d", type);
109  break;
110  }
111 
112  result = palloc0(digest_len + VARHDRSZ);
113  len = VARSIZE_ANY_EXHDR(input);
114  data = (unsigned char *) VARDATA_ANY(input);
115 
116  ctx = pg_cryptohash_create(type);
117  if (pg_cryptohash_init(ctx) < 0)
118  elog(ERROR, "could not initialize %s context: %s", typestr,
119  pg_cryptohash_error(ctx));
120  if (pg_cryptohash_update(ctx, data, len) < 0)
121  elog(ERROR, "could not update %s context: %s", typestr,
122  pg_cryptohash_error(ctx));
123  if (pg_cryptohash_final(ctx, (unsigned char *) VARDATA(result),
124  digest_len) < 0)
125  elog(ERROR, "could not finalize %s context: %s", typestr,
126  pg_cryptohash_error(ctx));
127  pg_cryptohash_free(ctx);
128 
129  SET_VARSIZE(result, digest_len + VARHDRSZ);
130 
131  return result;
132 }
133 
134 /*
135  * SHA-2 variants
136  */
137 
138 Datum
140 {
142 
143  PG_RETURN_BYTEA_P(result);
144 }
145 
146 Datum
148 {
150 
151  PG_RETURN_BYTEA_P(result);
152 }
153 
154 Datum
156 {
158 
159  PG_RETURN_BYTEA_P(result);
160 }
161 
162 Datum
164 {
166 
167  PG_RETURN_BYTEA_P(result);
168 }
#define VARHDRSZ
Definition: c.h:627
unsigned char uint8
Definition: c.h:439
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:139
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:101
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:243
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:74
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:177
const char * pg_cryptohash_error(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:259
pg_cryptohash_type
Definition: cryptohash.h:20
@ PG_SHA512
Definition: cryptohash.h:26
@ PG_SHA224
Definition: cryptohash.h:23
@ PG_SHA384
Definition: cryptohash.h:25
@ PG_SHA1
Definition: cryptohash.h:22
@ PG_SHA256
Definition: cryptohash.h:24
@ PG_MD5
Definition: cryptohash.h:21
Datum md5_bytea(PG_FUNCTION_ARGS)
Datum sha384_bytea(PG_FUNCTION_ARGS)
Datum sha256_bytea(PG_FUNCTION_ARGS)
#define MD5_HASH_LEN
Datum sha512_bytea(PG_FUNCTION_ARGS)
Datum sha224_bytea(PG_FUNCTION_ARGS)
Datum md5_text(PG_FUNCTION_ARGS)
static bytea * cryptohash_internal(pg_cryptohash_type type, bytea *input)
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define ereport(elevel,...)
Definition: elog.h:143
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
void * palloc0(Size size)
Definition: mcxt.c:1099
bool pg_md5_hash(const void *buff, size_t len, char *hexsum, const char **errstr)
Definition: md5_common.c:73
const void size_t len
const void * data
uintptr_t Datum
Definition: postgres.h:411
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:23
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:26
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:29
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:20
Definition: c.h:622
text * cstring_to_text(const char *s)
Definition: varlena.c:188