PostgreSQL Source Code  git master
cryptohash_openssl.c File Reference
#include "postgres.h"
#include <openssl/evp.h>
#include "common/cryptohash.h"
#include "common/md5.h"
#include "common/sha1.h"
#include "common/sha2.h"
#include "utils/memutils.h"
#include "utils/resowner.h"
#include "utils/resowner_private.h"
Include dependency graph for cryptohash_openssl.c:

Go to the source code of this file.

Data Structures

struct  pg_cryptohash_ctx
 

Macros

#define ALLOC(size)   MemoryContextAlloc(TopMemoryContext, size)
 
#define FREE(ptr)   pfree(ptr)
 

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)
 

Macro Definition Documentation

◆ ALLOC

#define ALLOC (   size)    MemoryContextAlloc(TopMemoryContext, size)

Definition at line 42 of file cryptohash_openssl.c.

Referenced by pg_cryptohash_create().

◆ FREE

#define FREE (   ptr)    pfree(ptr)

Definition at line 43 of file cryptohash_openssl.c.

Referenced by pg_cryptohash_create(), and pg_cryptohash_free().

Function Documentation

◆ pg_cryptohash_create()

pg_cryptohash_ctx* pg_cryptohash_create ( pg_cryptohash_type  type)

Definition at line 73 of file cryptohash_openssl.c.

References ALLOC, CurrentResourceOwner, ereport, errcode(), errmsg(), ERROR, pg_cryptohash_ctx::evpctx, explicit_bzero(), FREE, PointerGetDatum, ResourceOwnerEnlargeCryptoHash(), ResourceOwnerRememberCryptoHash(), pg_cryptohash_ctx::resowner, and pg_cryptohash_ctx::type.

74 {
75  pg_cryptohash_ctx *ctx;
76 
77  /*
78  * Make sure that the resource owner has space to remember this reference.
79  * This can error out with "out of memory", so do this before any other
80  * allocation to avoid leaking.
81  */
82 #ifndef FRONTEND
84 #endif
85 
86  ctx = ALLOC(sizeof(pg_cryptohash_ctx));
87  if (ctx == NULL)
88  return NULL;
89  memset(ctx, 0, sizeof(pg_cryptohash_ctx));
90  ctx->type = type;
91 
92  /*
93  * Initialization takes care of assigning the correct type for OpenSSL.
94  */
95  ctx->evpctx = EVP_MD_CTX_create();
96 
97  if (ctx->evpctx == NULL)
98  {
99  explicit_bzero(ctx, sizeof(pg_cryptohash_ctx));
100  FREE(ctx);
101 #ifndef FRONTEND
102  ereport(ERROR,
103  (errcode(ERRCODE_OUT_OF_MEMORY),
104  errmsg("out of memory")));
105 #else
106  return NULL;
107 #endif
108  }
109 
110 #ifndef FRONTEND
113  PointerGetDatum(ctx));
114 #endif
115 
116  return ctx;
117 }
#define PointerGetDatum(X)
Definition: postgres.h:600
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
int errcode(int sqlerrcode)
Definition: elog.c:698
pg_cryptohash_type type
Definition: cryptohash.c:46
#define ERROR
Definition: elog.h:46
void ResourceOwnerRememberCryptoHash(ResourceOwner owner, Datum handle)
Definition: resowner.c:1422
void ResourceOwnerEnlargeCryptoHash(ResourceOwner owner)
Definition: resowner.c:1411
ResourceOwner resowner
#define ereport(elevel,...)
Definition: elog.h:157
#define ALLOC(size)
int errmsg(const char *fmt,...)
Definition: elog.c:909
void explicit_bzero(void *buf, size_t len)
#define FREE(ptr)

◆ pg_cryptohash_final()

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

Definition at line 187 of file cryptohash_openssl.c.

References pg_cryptohash_ctx::data, pg_cryptohash_ctx::evpctx, pg_cryptohash_ctx::md5, MD5_DIGEST_LENGTH, 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, status(), and pg_cryptohash_ctx::type.

188 {
189  int status = 0;
190 
191  if (ctx == NULL)
192  return -1;
193 
194  switch (ctx->type)
195  {
196  case PG_MD5:
197  if (len < MD5_DIGEST_LENGTH)
198  return -1;
199  break;
200  case PG_SHA1:
201  if (len < SHA1_DIGEST_LENGTH)
202  return -1;
203  break;
204  case PG_SHA224:
205  if (len < PG_SHA224_DIGEST_LENGTH)
206  return -1;
207  break;
208  case PG_SHA256:
209  if (len < PG_SHA256_DIGEST_LENGTH)
210  return -1;
211  break;
212  case PG_SHA384:
213  if (len < PG_SHA384_DIGEST_LENGTH)
214  return -1;
215  break;
216  case PG_SHA512:
217  if (len < PG_SHA512_DIGEST_LENGTH)
218  return -1;
219  break;
220  }
221 
222  status = EVP_DigestFinal_ex(ctx->evpctx, dest, 0);
223 
224  /* OpenSSL internals return 1 on success, 0 on failure */
225  if (status <= 0)
226  return -1;
227  return 0;
228 }
#define SHA1_DIGEST_LENGTH
Definition: sha1.h:17
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:22
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:26
pg_cryptohash_type type
Definition: cryptohash.c:46
#define MD5_DIGEST_LENGTH
Definition: md5.h:20
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:20
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:24

◆ pg_cryptohash_free()

void pg_cryptohash_free ( pg_cryptohash_ctx ctx)

Definition at line 236 of file cryptohash_openssl.c.

References pg_cryptohash_ctx::evpctx, explicit_bzero(), FREE, PointerGetDatum, ResourceOwnerForgetCryptoHash(), and pg_cryptohash_ctx::resowner.

237 {
238  if (ctx == NULL)
239  return;
240 
241  EVP_MD_CTX_destroy(ctx->evpctx);
242 
243 #ifndef FRONTEND
245  PointerGetDatum(ctx));
246 #endif
247 
248  explicit_bzero(ctx, sizeof(pg_cryptohash_ctx));
249  FREE(ctx);
250 }
#define PointerGetDatum(X)
Definition: postgres.h:600
ResourceOwner resowner
void ResourceOwnerForgetCryptoHash(ResourceOwner owner, Datum handle)
Definition: resowner.c:1431
void explicit_bzero(void *buf, size_t len)
#define FREE(ptr)

◆ pg_cryptohash_init()

int pg_cryptohash_init ( pg_cryptohash_ctx ctx)

Definition at line 125 of file cryptohash_openssl.c.

References pg_cryptohash_ctx::data, pg_cryptohash_ctx::evpctx, 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, status(), and pg_cryptohash_ctx::type.

126 {
127  int status = 0;
128 
129  if (ctx == NULL)
130  return -1;
131 
132  switch (ctx->type)
133  {
134  case PG_MD5:
135  status = EVP_DigestInit_ex(ctx->evpctx, EVP_md5(), NULL);
136  break;
137  case PG_SHA1:
138  status = EVP_DigestInit_ex(ctx->evpctx, EVP_sha1(), NULL);
139  break;
140  case PG_SHA224:
141  status = EVP_DigestInit_ex(ctx->evpctx, EVP_sha224(), NULL);
142  break;
143  case PG_SHA256:
144  status = EVP_DigestInit_ex(ctx->evpctx, EVP_sha256(), NULL);
145  break;
146  case PG_SHA384:
147  status = EVP_DigestInit_ex(ctx->evpctx, EVP_sha384(), NULL);
148  break;
149  case PG_SHA512:
150  status = EVP_DigestInit_ex(ctx->evpctx, EVP_sha512(), NULL);
151  break;
152  }
153 
154  /* OpenSSL internals return 1 on success, 0 on failure */
155  if (status <= 0)
156  return -1;
157  return 0;
158 }
pg_cryptohash_type type
Definition: cryptohash.c:46
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ pg_cryptohash_update()

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

Definition at line 166 of file cryptohash_openssl.c.

References pg_cryptohash_ctx::data, pg_cryptohash_ctx::evpctx, 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, status(), and pg_cryptohash_ctx::type.

167 {
168  int status = 0;
169 
170  if (ctx == NULL)
171  return -1;
172 
173  status = EVP_DigestUpdate(ctx->evpctx, data, len);
174 
175  /* OpenSSL internals return 1 on success, 0 on failure */
176  if (status <= 0)
177  return -1;
178  return 0;
179 }
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227