PostgreSQL Source Code  git master
cryptohash.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * cryptohash.c
4  * Fallback implementations for cryptographic hash functions.
5  *
6  * This is the set of in-core functions used when there are no other
7  * alternative options like OpenSSL.
8  *
9  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * IDENTIFICATION
13  * src/common/cryptohash.c
14  *
15  *-------------------------------------------------------------------------
16  */
17 
18 #ifndef FRONTEND
19 #include "postgres.h"
20 #else
21 #include "postgres_fe.h"
22 #endif
23 
24 #include <sys/param.h>
25 
26 #include "common/cryptohash.h"
27 #include "md5_int.h"
28 #include "sha1_int.h"
29 #include "sha2_int.h"
30 
31 /*
32  * In backend, use palloc/pfree to ease the error handling. In frontend,
33  * use malloc to be able to return a failure status back to the caller.
34  */
35 #ifndef FRONTEND
36 #define ALLOC(size) palloc(size)
37 #define FREE(ptr) pfree(ptr)
38 #else
39 #define ALLOC(size) malloc(size)
40 #define FREE(ptr) free(ptr)
41 #endif
42 
43 /* Internal pg_cryptohash_ctx structure */
45 {
47 
48  union
49  {
56  } data;
57 };
58 
59 /*
60  * pg_cryptohash_create
61  *
62  * Allocate a hash context. Returns NULL on failure for an OOM. The
63  * backend issues an error, without returning.
64  */
67 {
68  pg_cryptohash_ctx *ctx;
69 
70  /*
71  * Note that this always allocates enough space for the largest hash. A
72  * smaller allocation would be enough for md5, sha224 and sha256, but the
73  * small extra amount of memory does not make it worth complicating this
74  * code.
75  */
76  ctx = ALLOC(sizeof(pg_cryptohash_ctx));
77  if (ctx == NULL)
78  return NULL;
79  memset(ctx, 0, sizeof(pg_cryptohash_ctx));
80  ctx->type = type;
81 
82  return ctx;
83 }
84 
85 /*
86  * pg_cryptohash_init
87  *
88  * Initialize a hash context. Note that this implementation is designed
89  * to never fail, so this always returns 0.
90  */
91 int
93 {
94  if (ctx == NULL)
95  return -1;
96 
97  switch (ctx->type)
98  {
99  case PG_MD5:
100  pg_md5_init(&ctx->data.md5);
101  break;
102  case PG_SHA1:
103  pg_sha1_init(&ctx->data.sha1);
104  break;
105  case PG_SHA224:
106  pg_sha224_init(&ctx->data.sha224);
107  break;
108  case PG_SHA256:
109  pg_sha256_init(&ctx->data.sha256);
110  break;
111  case PG_SHA384:
112  pg_sha384_init(&ctx->data.sha384);
113  break;
114  case PG_SHA512:
115  pg_sha512_init(&ctx->data.sha512);
116  break;
117  }
118 
119  return 0;
120 }
121 
122 /*
123  * pg_cryptohash_update
124  *
125  * Update a hash context. Note that this implementation is designed
126  * to never fail, so this always returns 0 except if the caller has
127  * given a NULL context.
128  */
129 int
131 {
132  if (ctx == NULL)
133  return -1;
134 
135  switch (ctx->type)
136  {
137  case PG_MD5:
138  pg_md5_update(&ctx->data.md5, data, len);
139  break;
140  case PG_SHA1:
141  pg_sha1_update(&ctx->data.sha1, data, len);
142  break;
143  case PG_SHA224:
144  pg_sha224_update(&ctx->data.sha224, data, len);
145  break;
146  case PG_SHA256:
147  pg_sha256_update(&ctx->data.sha256, data, len);
148  break;
149  case PG_SHA384:
150  pg_sha384_update(&ctx->data.sha384, data, len);
151  break;
152  case PG_SHA512:
153  pg_sha512_update(&ctx->data.sha512, data, len);
154  break;
155  }
156 
157  return 0;
158 }
159 
160 /*
161  * pg_cryptohash_final
162  *
163  * Finalize a hash context. Note that this implementation is designed to
164  * never fail, so this always returns 0 except if the destination buffer
165  * is not large enough.
166  */
167 int
169 {
170  if (ctx == NULL)
171  return -1;
172 
173  switch (ctx->type)
174  {
175  case PG_MD5:
176  if (len < MD5_DIGEST_LENGTH)
177  return -1;
178  pg_md5_final(&ctx->data.md5, dest);
179  break;
180  case PG_SHA1:
181  if (len < SHA1_DIGEST_LENGTH)
182  return -1;
183  pg_sha1_final(&ctx->data.sha1, dest);
184  break;
185  case PG_SHA224:
186  if (len < PG_SHA224_DIGEST_LENGTH)
187  return -1;
188  pg_sha224_final(&ctx->data.sha224, dest);
189  break;
190  case PG_SHA256:
191  if (len < PG_SHA256_DIGEST_LENGTH)
192  return -1;
193  pg_sha256_final(&ctx->data.sha256, dest);
194  break;
195  case PG_SHA384:
196  if (len < PG_SHA384_DIGEST_LENGTH)
197  return -1;
198  pg_sha384_final(&ctx->data.sha384, dest);
199  break;
200  case PG_SHA512:
201  if (len < PG_SHA512_DIGEST_LENGTH)
202  return -1;
203  pg_sha512_final(&ctx->data.sha512, dest);
204  break;
205  }
206 
207  return 0;
208 }
209 
210 /*
211  * pg_cryptohash_free
212  *
213  * Free a hash context.
214  */
215 void
217 {
218  if (ctx == NULL)
219  return;
220 
221  explicit_bzero(ctx, sizeof(pg_cryptohash_ctx));
222  FREE(ctx);
223 }
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:168
pg_sha384_ctx sha384
Definition: cryptohash.c:54
pg_sha256_ctx sha256
Definition: cryptohash.c:53
void pg_md5_init(pg_md5_ctx *ctx)
Definition: md5.c:385
#define SHA1_DIGEST_LENGTH
Definition: sha1.h:17
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:23
unsigned char uint8
Definition: c.h:439
#define FREE(ptr)
Definition: cryptohash.c:37
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:29
void pg_sha256_init(pg_sha256_ctx *context)
Definition: sha2.c:279
union pg_cryptohash_ctx::@35 data
void pg_sha384_update(pg_sha384_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:944
pg_cryptohash_type type
Definition: cryptohash.c:46
pg_sha1_ctx sha1
Definition: cryptohash.c:51
#define MD5_DIGEST_LENGTH
Definition: md5.h:20
void pg_sha512_final(pg_sha512_ctx *context, uint8 *digest)
Definition: sha2.c:905
void pg_sha224_final(pg_sha224_ctx *context, uint8 *digest)
Definition: sha2.c:994
void pg_sha512_update(pg_sha512_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:802
void pg_sha1_final(pg_sha1_ctx *ctx, uint8 *dest)
Definition: sha1.c:365
void pg_sha224_init(pg_sha224_ctx *context)
Definition: sha2.c:978
void pg_sha256_update(pg_sha256_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:476
pg_sha224_ctx sha224
Definition: cryptohash.c:52
pg_sha512_ctx sha512
Definition: cryptohash.c:55
void pg_sha224_update(pg_sha224_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:988
pg_md5_ctx md5
Definition: cryptohash.c:50
void pg_sha384_final(pg_sha384_ctx *context, uint8 *digest)
Definition: sha2.c:950
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:66
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:92
void pg_md5_update(pg_md5_ctx *ctx, const uint8 *data, size_t len)
Definition: md5.c:403
#define ALLOC(size)
Definition: cryptohash.c:36
void pg_sha384_init(pg_sha384_ctx *context)
Definition: sha2.c:934
void pg_sha512_init(pg_sha512_ctx *context)
Definition: sha2.c:605
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:130
pg_cryptohash_type
Definition: cryptohash.h:19
void explicit_bzero(void *buf, size_t len)
void pg_sha256_final(pg_sha256_ctx *context, uint8 *digest)
Definition: sha2.c:577
void pg_md5_final(pg_md5_ctx *ctx, uint8 *dest)
Definition: md5.c:435
void pg_sha1_init(pg_sha1_ctx *ctx)
Definition: sha1.c:316
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:216
void pg_sha1_update(pg_sha1_ctx *ctx, const uint8 *data, size_t len)
Definition: sha1.c:332
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:20
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:26