PostgreSQL Source Code  git master
cryptohashes.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * cryptohashes.c
4  * Cryptographic hash functions
5  *
6  * Portions Copyright (c) 2018-2019, PostgreSQL Global Development Group
7  *
8  *
9  * IDENTIFICATION
10  * src/backend/utils/adt/cryptohashes.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "common/md5.h"
17 #include "common/sha2.h"
18 #include "utils/builtins.h"
19 
20 
21 /*
22  * MD5
23  */
24 
25 /* MD5 produces a 16 byte (128 bit) hash; double it for hex */
26 #define MD5_HASH_LEN 32
27 
28 /*
29  * Create an MD5 hash of a text value and return it as hex string.
30  */
31 Datum
33 {
34  text *in_text = PG_GETARG_TEXT_PP(0);
35  size_t len;
36  char hexsum[MD5_HASH_LEN + 1];
37 
38  /* Calculate the length of the buffer using varlena metadata */
39  len = VARSIZE_ANY_EXHDR(in_text);
40 
41  /* get the hash result */
42  if (pg_md5_hash(VARDATA_ANY(in_text), len, hexsum) == false)
43  ereport(ERROR,
44  (errcode(ERRCODE_OUT_OF_MEMORY),
45  errmsg("out of memory")));
46 
47  /* convert to text and return it */
49 }
50 
51 /*
52  * Create an MD5 hash of a bytea value and return it as a hex string.
53  */
54 Datum
56 {
57  bytea *in = PG_GETARG_BYTEA_PP(0);
58  size_t len;
59  char hexsum[MD5_HASH_LEN + 1];
60 
61  len = VARSIZE_ANY_EXHDR(in);
62  if (pg_md5_hash(VARDATA_ANY(in), len, hexsum) == false)
63  ereport(ERROR,
64  (errcode(ERRCODE_OUT_OF_MEMORY),
65  errmsg("out of memory")));
66 
68 }
69 
70 
71 /*
72  * SHA-2 variants
73  */
74 
75 Datum
77 {
78  bytea *in = PG_GETARG_BYTEA_PP(0);
79  const uint8 *data;
80  size_t len;
81  pg_sha224_ctx ctx;
82  unsigned char buf[PG_SHA224_DIGEST_LENGTH];
83  bytea *result;
84 
85  len = VARSIZE_ANY_EXHDR(in);
86  data = (unsigned char *) VARDATA_ANY(in);
87 
88  pg_sha224_init(&ctx);
89  pg_sha224_update(&ctx, data, len);
90  pg_sha224_final(&ctx, buf);
91 
92  result = palloc(sizeof(buf) + VARHDRSZ);
93  SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
94  memcpy(VARDATA(result), buf, sizeof(buf));
95 
96  PG_RETURN_BYTEA_P(result);
97 }
98 
99 Datum
101 {
102  bytea *in = PG_GETARG_BYTEA_PP(0);
103  const uint8 *data;
104  size_t len;
105  pg_sha256_ctx ctx;
106  unsigned char buf[PG_SHA256_DIGEST_LENGTH];
107  bytea *result;
108 
109  len = VARSIZE_ANY_EXHDR(in);
110  data = (unsigned char *) VARDATA_ANY(in);
111 
112  pg_sha256_init(&ctx);
113  pg_sha256_update(&ctx, data, len);
114  pg_sha256_final(&ctx, buf);
115 
116  result = palloc(sizeof(buf) + VARHDRSZ);
117  SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
118  memcpy(VARDATA(result), buf, sizeof(buf));
119 
120  PG_RETURN_BYTEA_P(result);
121 }
122 
123 Datum
125 {
126  bytea *in = PG_GETARG_BYTEA_PP(0);
127  const uint8 *data;
128  size_t len;
129  pg_sha384_ctx ctx;
130  unsigned char buf[PG_SHA384_DIGEST_LENGTH];
131  bytea *result;
132 
133  len = VARSIZE_ANY_EXHDR(in);
134  data = (unsigned char *) VARDATA_ANY(in);
135 
136  pg_sha384_init(&ctx);
137  pg_sha384_update(&ctx, data, len);
138  pg_sha384_final(&ctx, buf);
139 
140  result = palloc(sizeof(buf) + VARHDRSZ);
141  SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
142  memcpy(VARDATA(result), buf, sizeof(buf));
143 
144  PG_RETURN_BYTEA_P(result);
145 }
146 
147 Datum
149 {
150  bytea *in = PG_GETARG_BYTEA_PP(0);
151  const uint8 *data;
152  size_t len;
153  pg_sha512_ctx ctx;
154  unsigned char buf[PG_SHA512_DIGEST_LENGTH];
155  bytea *result;
156 
157  len = VARSIZE_ANY_EXHDR(in);
158  data = (unsigned char *) VARDATA_ANY(in);
159 
160  pg_sha512_init(&ctx);
161  pg_sha512_update(&ctx, data, len);
162  pg_sha512_final(&ctx, buf);
163 
164  result = palloc(sizeof(buf) + VARHDRSZ);
165  SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
166  memcpy(VARDATA(result), buf, sizeof(buf));
167 
168  PG_RETURN_BYTEA_P(result);
169 }
Datum sha224_bytea(PG_FUNCTION_ARGS)
Definition: cryptohashes.c:76
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
Datum md5_bytea(PG_FUNCTION_ARGS)
Definition: cryptohashes.c:55
#define VARHDRSZ
Definition: c.h:555
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:62
unsigned char uint8
Definition: c.h:356
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:68
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
void pg_sha256_init(pg_sha256_ctx *context)
Definition: sha2.c:268
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
void pg_sha384_update(pg_sha384_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:933
Datum md5_text(PG_FUNCTION_ARGS)
Definition: cryptohashes.c:32
#define ERROR
Definition: elog.h:43
void pg_sha512_final(pg_sha512_ctx *context, uint8 *digest)
Definition: sha2.c:894
static char * buf
Definition: pg_test_fsync.c:68
void pg_sha224_final(pg_sha224_ctx *context, uint8 *digest)
Definition: sha2.c:983
void pg_sha512_update(pg_sha512_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:791
#define MD5_HASH_LEN
Definition: cryptohashes.c:26
#define ereport(elevel, rest)
Definition: elog.h:141
void pg_sha224_init(pg_sha224_ctx *context)
Definition: sha2.c:967
void pg_sha256_update(pg_sha256_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:465
uintptr_t Datum
Definition: postgres.h:367
void pg_sha224_update(pg_sha224_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:977
Datum sha256_bytea(PG_FUNCTION_ARGS)
Definition: cryptohashes.c:100
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
void pg_sha384_final(pg_sha384_ctx *context, uint8 *digest)
Definition: sha2.c:939
text * cstring_to_text(const char *s)
Definition: varlena.c:171
bool pg_md5_hash(const void *buff, size_t len, char *hexsum)
Definition: md5.c:293
Datum sha384_bytea(PG_FUNCTION_ARGS)
Definition: cryptohashes.c:124
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:302
void pg_sha384_init(pg_sha384_ctx *context)
Definition: sha2.c:923
void pg_sha512_init(pg_sha512_ctx *context)
Definition: sha2.c:594
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
void pg_sha256_final(pg_sha256_ctx *context, uint8 *digest)
Definition: sha2.c:566
Datum sha512_bytea(PG_FUNCTION_ARGS)
Definition: cryptohashes.c:148
Definition: c.h:549
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:59
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:65