PostgreSQL Source Code  git master
cryptohash.c File Reference
#include "postgres.h"
#include <sys/param.h>
#include "common/cryptohash.h"
#include "md5_int.h"
#include "sha1_int.h"
#include "sha2_int.h"
Include dependency graph for cryptohash.c:

Go to the source code of this file.

Data Structures

struct  pg_cryptohash_ctx
 

Macros

#define ALLOC(size)   palloc(size)
 
#define FREE(ptr)   pfree(ptr)
 

Typedefs

typedef enum pg_cryptohash_errno pg_cryptohash_errno
 

Enumerations

enum  pg_cryptohash_errno {
  PG_CRYPTOHASH_ERROR_NONE = 0 , PG_CRYPTOHASH_ERROR_DEST_LEN , PG_CRYPTOHASH_ERROR_NONE = 0 , PG_CRYPTOHASH_ERROR_DEST_LEN ,
  PG_CRYPTOHASH_ERROR_OPENSSL
}
 

Functions

pg_cryptohash_ctxpg_cryptohash_create (pg_cryptohash_type type)
 
int pg_cryptohash_init (pg_cryptohash_ctx *ctx)
 
int pg_cryptohash_update (pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
 
int pg_cryptohash_final (pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
 
void pg_cryptohash_free (pg_cryptohash_ctx *ctx)
 
const char * pg_cryptohash_error (pg_cryptohash_ctx *ctx)
 

Macro Definition Documentation

◆ ALLOC

#define ALLOC (   size)    palloc(size)

Definition at line 36 of file cryptohash.c.

◆ FREE

#define FREE (   ptr)    pfree(ptr)

Definition at line 37 of file cryptohash.c.

Typedef Documentation

◆ pg_cryptohash_errno

Enumeration Type Documentation

◆ pg_cryptohash_errno

Enumerator
PG_CRYPTOHASH_ERROR_NONE 
PG_CRYPTOHASH_ERROR_DEST_LEN 
PG_CRYPTOHASH_ERROR_NONE 
PG_CRYPTOHASH_ERROR_DEST_LEN 
PG_CRYPTOHASH_ERROR_OPENSSL 

Definition at line 44 of file cryptohash.c.

45 {
pg_cryptohash_errno
Definition: cryptohash.c:45
@ PG_CRYPTOHASH_ERROR_DEST_LEN
Definition: cryptohash.c:47
@ PG_CRYPTOHASH_ERROR_NONE
Definition: cryptohash.c:46

Function Documentation

◆ pg_cryptohash_create()

pg_cryptohash_ctx* pg_cryptohash_create ( pg_cryptohash_type  type)

Definition at line 74 of file cryptohash.c.

75 {
76  pg_cryptohash_ctx *ctx;
77 
78  /*
79  * Note that this always allocates enough space for the largest hash. A
80  * smaller allocation would be enough for md5, sha224 and sha256, but the
81  * small extra amount of memory does not make it worth complicating this
82  * code.
83  */
84  ctx = ALLOC(sizeof(pg_cryptohash_ctx));
85  if (ctx == NULL)
86  return NULL;
87 
88  memset(ctx, 0, sizeof(pg_cryptohash_ctx));
89  ctx->type = type;
91  return ctx;
92 }
#define ALLOC(size)
Definition: cryptohash.c:36
pg_cryptohash_errno error
Definition: cryptohash.c:54
pg_cryptohash_type type
Definition: cryptohash.c:53
const char * type

References ALLOC, pg_cryptohash_ctx::error, PG_CRYPTOHASH_ERROR_NONE, type, and pg_cryptohash_ctx::type.

Referenced by cryptohash_internal(), InitializeBackupManifest(), json_parse_manifest_incremental_init(), pg_checksum_init(), pg_hmac_create(), pg_hmac_init(), pg_md5_binary(), pg_md5_hash(), scram_H(), scram_mock_salt(), uuid_generate_internal(), and verify_manifest_checksum().

◆ pg_cryptohash_error()

const char* pg_cryptohash_error ( pg_cryptohash_ctx ctx)

Definition at line 254 of file cryptohash.c.

255 {
256  /*
257  * This implementation would never fail because of an out-of-memory error,
258  * except when creating the context.
259  */
260  if (ctx == NULL)
261  return _("out of memory");
262 
263  switch (ctx->error)
264  {
266  return _("success");
268  return _("destination buffer too small");
269  }
270 
271  Assert(false);
272  return _("success");
273 }
#define Assert(condition)
Definition: c.h:858
#define _(x)
Definition: elog.c:90

References _, Assert, pg_cryptohash_ctx::error, PG_CRYPTOHASH_ERROR_DEST_LEN, and PG_CRYPTOHASH_ERROR_NONE.

Referenced by AppendStringToManifest(), cryptohash_internal(), InitializeBackupManifest(), pg_hmac_final(), pg_hmac_init(), pg_hmac_update(), pg_md5_binary(), pg_md5_hash(), scram_H(), SendBackupManifest(), and uuid_generate_internal().

◆ pg_cryptohash_final()

int pg_cryptohash_final ( pg_cryptohash_ctx ctx,
uint8 dest,
size_t  len 
)

Definition at line 172 of file cryptohash.c.

173 {
174  if (ctx == NULL)
175  return -1;
176 
177  switch (ctx->type)
178  {
179  case PG_MD5:
180  if (len < MD5_DIGEST_LENGTH)
181  {
183  return -1;
184  }
185  pg_md5_final(&ctx->data.md5, dest);
186  break;
187  case PG_SHA1:
188  if (len < SHA1_DIGEST_LENGTH)
189  {
191  return -1;
192  }
193  pg_sha1_final(&ctx->data.sha1, dest);
194  break;
195  case PG_SHA224:
197  {
199  return -1;
200  }
202  break;
203  case PG_SHA256:
205  {
207  return -1;
208  }
210  break;
211  case PG_SHA384:
213  {
215  return -1;
216  }
218  break;
219  case PG_SHA512:
221  {
223  return -1;
224  }
226  break;
227  }
228 
229  return 0;
230 }
@ 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
void pg_md5_final(pg_md5_ctx *ctx, uint8 *dest)
Definition: md5.c:435
#define MD5_DIGEST_LENGTH
Definition: md5.h:20
const void size_t len
void pg_sha1_final(pg_sha1_ctx *ctx, uint8 *dest)
Definition: sha1.c:365
#define SHA1_DIGEST_LENGTH
Definition: sha1.h:17
void pg_sha512_final(pg_sha512_ctx *context, uint8 *digest)
Definition: sha2.c:905
void pg_sha256_final(pg_sha256_ctx *context, uint8 *digest)
Definition: sha2.c:577
void pg_sha224_final(pg_sha224_ctx *context, uint8 *digest)
Definition: sha2.c:994
void pg_sha384_final(pg_sha384_ctx *context, uint8 *digest)
Definition: sha2.c:950
#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
union pg_cryptohash_ctx::@40 data
pg_md5_ctx md5
Definition: cryptohash.c:58
pg_sha384_ctx sha384
Definition: cryptohash.c:62
pg_sha1_ctx sha1
Definition: cryptohash.c:59
pg_sha256_ctx sha256
Definition: cryptohash.c:61
pg_sha512_ctx sha512
Definition: cryptohash.c:63
pg_sha224_ctx sha224
Definition: cryptohash.c:60

References pg_cryptohash_ctx::data, generate_unaccent_rules::dest, pg_cryptohash_ctx::error, len, pg_cryptohash_ctx::md5, MD5_DIGEST_LENGTH, PG_CRYPTOHASH_ERROR_DEST_LEN, PG_MD5, pg_md5_final(), PG_SHA1, pg_sha1_final(), PG_SHA224, PG_SHA224_DIGEST_LENGTH, pg_sha224_final(), PG_SHA256, PG_SHA256_DIGEST_LENGTH, pg_sha256_final(), PG_SHA384, PG_SHA384_DIGEST_LENGTH, pg_sha384_final(), PG_SHA512, PG_SHA512_DIGEST_LENGTH, pg_sha512_final(), pg_cryptohash_ctx::sha1, SHA1_DIGEST_LENGTH, pg_cryptohash_ctx::sha224, pg_cryptohash_ctx::sha256, pg_cryptohash_ctx::sha384, pg_cryptohash_ctx::sha512, and pg_cryptohash_ctx::type.

Referenced by cryptohash_internal(), pg_checksum_final(), pg_hmac_final(), pg_hmac_init(), pg_md5_binary(), pg_md5_hash(), scram_H(), scram_mock_salt(), SendBackupManifest(), uuid_generate_internal(), and verify_manifest_checksum().

◆ pg_cryptohash_free()

void pg_cryptohash_free ( pg_cryptohash_ctx ctx)

Definition at line 238 of file cryptohash.c.

239 {
240  if (ctx == NULL)
241  return;
242 
243  explicit_bzero(ctx, sizeof(pg_cryptohash_ctx));
244  FREE(ctx);
245 }
#define FREE(ptr)
Definition: cryptohash.c:37
void explicit_bzero(void *buf, size_t len)

References explicit_bzero(), and FREE.

Referenced by cryptohash_internal(), FreeBackupManifest(), pg_checksum_final(), pg_checksum_init(), pg_hmac_free(), pg_hmac_init(), pg_md5_binary(), pg_md5_hash(), scram_H(), scram_mock_salt(), uuid_generate_internal(), and verify_manifest_checksum().

◆ pg_cryptohash_init()

int pg_cryptohash_init ( pg_cryptohash_ctx ctx)

Definition at line 100 of file cryptohash.c.

101 {
102  if (ctx == NULL)
103  return -1;
104 
105  switch (ctx->type)
106  {
107  case PG_MD5:
108  pg_md5_init(&ctx->data.md5);
109  break;
110  case PG_SHA1:
111  pg_sha1_init(&ctx->data.sha1);
112  break;
113  case PG_SHA224:
114  pg_sha224_init(&ctx->data.sha224);
115  break;
116  case PG_SHA256:
117  pg_sha256_init(&ctx->data.sha256);
118  break;
119  case PG_SHA384:
120  pg_sha384_init(&ctx->data.sha384);
121  break;
122  case PG_SHA512:
123  pg_sha512_init(&ctx->data.sha512);
124  break;
125  }
126 
127  return 0;
128 }
void pg_md5_init(pg_md5_ctx *ctx)
Definition: md5.c:385
void pg_sha1_init(pg_sha1_ctx *ctx)
Definition: sha1.c:316
void pg_sha384_init(pg_sha384_ctx *context)
Definition: sha2.c:934
void pg_sha256_init(pg_sha256_ctx *context)
Definition: sha2.c:279
void pg_sha512_init(pg_sha512_ctx *context)
Definition: sha2.c:605
void pg_sha224_init(pg_sha224_ctx *context)
Definition: sha2.c:978

References pg_cryptohash_ctx::data, pg_cryptohash_ctx::md5, PG_MD5, pg_md5_init(), PG_SHA1, pg_sha1_init(), PG_SHA224, pg_sha224_init(), PG_SHA256, pg_sha256_init(), PG_SHA384, pg_sha384_init(), PG_SHA512, pg_sha512_init(), pg_cryptohash_ctx::sha1, pg_cryptohash_ctx::sha224, pg_cryptohash_ctx::sha256, pg_cryptohash_ctx::sha384, pg_cryptohash_ctx::sha512, and pg_cryptohash_ctx::type.

Referenced by cryptohash_internal(), InitializeBackupManifest(), json_parse_manifest_incremental_init(), pg_checksum_init(), pg_hmac_final(), pg_hmac_init(), pg_md5_binary(), pg_md5_hash(), scram_H(), scram_mock_salt(), uuid_generate_internal(), and verify_manifest_checksum().

◆ pg_cryptohash_update()

int pg_cryptohash_update ( pg_cryptohash_ctx ctx,
const uint8 data,
size_t  len 
)

Definition at line 136 of file cryptohash.c.

137 {
138  if (ctx == NULL)
139  return -1;
140 
141  switch (ctx->type)
142  {
143  case PG_MD5:
144  pg_md5_update(&ctx->data.md5, data, len);
145  break;
146  case PG_SHA1:
147  pg_sha1_update(&ctx->data.sha1, data, len);
148  break;
149  case PG_SHA224:
151  break;
152  case PG_SHA256:
154  break;
155  case PG_SHA384:
157  break;
158  case PG_SHA512:
160  break;
161  }
162 
163  return 0;
164 }
void pg_md5_update(pg_md5_ctx *ctx, const uint8 *data, size_t len)
Definition: md5.c:403
const void * data
void pg_sha1_update(pg_sha1_ctx *ctx, const uint8 *data, size_t len)
Definition: sha1.c:332
void pg_sha512_update(pg_sha512_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:802
void pg_sha224_update(pg_sha224_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:988
void pg_sha384_update(pg_sha384_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:944
void pg_sha256_update(pg_sha256_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:476

References pg_cryptohash_ctx::data, data, len, pg_cryptohash_ctx::md5, PG_MD5, pg_md5_update(), PG_SHA1, pg_sha1_update(), PG_SHA224, pg_sha224_update(), PG_SHA256, pg_sha256_update(), PG_SHA384, pg_sha384_update(), PG_SHA512, pg_sha512_update(), pg_cryptohash_ctx::sha1, pg_cryptohash_ctx::sha224, pg_cryptohash_ctx::sha256, pg_cryptohash_ctx::sha384, pg_cryptohash_ctx::sha512, and pg_cryptohash_ctx::type.

Referenced by AppendStringToManifest(), cryptohash_internal(), json_parse_manifest_incremental_chunk(), pg_checksum_update(), pg_hmac_final(), pg_hmac_init(), pg_hmac_update(), pg_md5_binary(), pg_md5_hash(), scram_H(), scram_mock_salt(), uuid_generate_internal(), and verify_manifest_checksum().