PostgreSQL Source Code  git master
hmac_openssl.c File Reference
#include "postgres.h"
#include <openssl/hmac.h>
#include "common/hmac.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 hmac_openssl.c:

Go to the source code of this file.

Data Structures

struct  pg_hmac_ctx
 

Macros

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

Functions

pg_hmac_ctxpg_hmac_create (pg_cryptohash_type type)
 
int pg_hmac_init (pg_hmac_ctx *ctx, const uint8 *key, size_t len)
 
int pg_hmac_update (pg_hmac_ctx *ctx, const uint8 *data, size_t len)
 
int pg_hmac_final (pg_hmac_ctx *ctx, uint8 *dest, size_t len)
 
void pg_hmac_free (pg_hmac_ctx *ctx)
 

Macro Definition Documentation

◆ ALLOC

#define ALLOC (   size)    palloc(size)

Definition at line 45 of file hmac_openssl.c.

Referenced by pg_hmac_create().

◆ FREE

#define FREE (   ptr)    pfree(ptr)

Definition at line 47 of file hmac_openssl.c.

Referenced by pg_hmac_create(), and pg_hmac_free().

Function Documentation

◆ pg_hmac_create()

pg_hmac_ctx* pg_hmac_create ( pg_cryptohash_type  type)

Definition at line 73 of file hmac_openssl.c.

References ALLOC, pg_hmac_ctx::block_size, CurrentResourceOwner, pg_hmac_ctx::digest_size, ereport, errcode(), errmsg(), ERROR, explicit_bzero(), FREE, pg_hmac_ctx::hash, pg_hmac_ctx::hmacctx, MD5_BLOCK_SIZE, MD5_DIGEST_LENGTH, pg_cryptohash_create(), PG_MD5, PG_SHA1, PG_SHA224, PG_SHA224_BLOCK_LENGTH, PG_SHA224_DIGEST_LENGTH, PG_SHA256, PG_SHA256_BLOCK_LENGTH, PG_SHA256_DIGEST_LENGTH, PG_SHA384, PG_SHA384_BLOCK_LENGTH, PG_SHA384_DIGEST_LENGTH, PG_SHA512, PG_SHA512_BLOCK_LENGTH, PG_SHA512_DIGEST_LENGTH, PointerGetDatum, ResourceOwnerEnlargeHMAC(), ResourceOwnerRememberHMAC(), pg_hmac_ctx::resowner, SHA1_BLOCK_SIZE, SHA1_DIGEST_LENGTH, and pg_hmac_ctx::type.

74 {
75  pg_hmac_ctx *ctx;
76 
77  ctx = ALLOC(sizeof(pg_hmac_ctx));
78  if (ctx == NULL)
79  return NULL;
80  memset(ctx, 0, sizeof(pg_hmac_ctx));
81 
82  ctx->type = type;
83 
84  /*
85  * Initialization takes care of assigning the correct type for OpenSSL.
86  */
87 #ifdef HAVE_HMAC_CTX_NEW
88 #ifndef FRONTEND
90 #endif
91  ctx->hmacctx = HMAC_CTX_new();
92 #else
93  ctx->hmacctx = ALLOC(sizeof(HMAC_CTX));
94 #endif
95 
96  if (ctx->hmacctx == NULL)
97  {
98  explicit_bzero(ctx, sizeof(pg_hmac_ctx));
99  FREE(ctx);
100 #ifndef FRONTEND
101  ereport(ERROR,
102  (errcode(ERRCODE_OUT_OF_MEMORY),
103  errmsg("out of memory")));
104 #endif
105  return NULL;
106  }
107 
108 #ifdef HAVE_HMAC_CTX_NEW
109 #ifndef FRONTEND
112 #endif
113 #else
114  memset(ctx->hmacctx, 0, sizeof(HMAC_CTX));
115 #endif /* HAVE_HMAC_CTX_NEW */
116 
117  return ctx;
118 }
#define PointerGetDatum(X)
Definition: postgres.h:600
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
int errcode(int sqlerrcode)
Definition: elog.c:698
#define FREE(ptr)
Definition: hmac_openssl.c:47
void ResourceOwnerEnlargeHMAC(ResourceOwner owner)
Definition: resowner.c:1456
#define ERROR
Definition: elog.h:46
#define ALLOC(size)
Definition: hmac_openssl.c:45
HMAC_CTX * hmacctx
Definition: hmac_openssl.c:58
#define ereport(elevel,...)
Definition: elog.h:157
void ResourceOwnerRememberHMAC(ResourceOwner owner, Datum handle)
Definition: resowner.c:1467
pg_cryptohash_type type
Definition: hmac.c:47
int errmsg(const char *fmt,...)
Definition: elog.c:909
void explicit_bzero(void *buf, size_t len)
ResourceOwner resowner
Definition: hmac_openssl.c:62

◆ pg_hmac_final()

int pg_hmac_final ( pg_hmac_ctx ctx,
uint8 dest,
size_t  len 
)

Definition at line 189 of file hmac_openssl.c.

References ALLOC, pg_hmac_ctx::block_size, pg_hmac_ctx::digest_size, FREE, pg_hmac_ctx::hash, pg_hmac_ctx::hmacctx, pg_hmac_ctx::k_opad, MD5_DIGEST_LENGTH, pg_cryptohash_final(), pg_cryptohash_init(), pg_cryptohash_update(), PG_MD5, PG_SHA1, PG_SHA224, PG_SHA224_DIGEST_LENGTH, PG_SHA256, PG_SHA256_DIGEST_LENGTH, PG_SHA384, PG_SHA384_DIGEST_LENGTH, PG_SHA512, PG_SHA512_DIGEST_LENGTH, SHA1_DIGEST_LENGTH, status(), and pg_hmac_ctx::type.

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

◆ pg_hmac_free()

void pg_hmac_free ( pg_hmac_ctx ctx)

Definition at line 239 of file hmac_openssl.c.

References explicit_bzero(), FREE, pg_hmac_ctx::hash, pg_hmac_ctx::hmacctx, pg_cryptohash_free(), PointerGetDatum, ResourceOwnerForgetHMAC(), and pg_hmac_ctx::resowner.

240 {
241  if (ctx == NULL)
242  return;
243 
244 #ifdef HAVE_HMAC_CTX_FREE
245  HMAC_CTX_free(ctx->hmacctx);
246 #ifndef FRONTEND
248 #endif
249 #else
250  explicit_bzero(ctx->hmacctx, sizeof(HMAC_CTX));
251  FREE(ctx->hmacctx);
252 #endif
253 
254  explicit_bzero(ctx, sizeof(pg_hmac_ctx));
255  FREE(ctx);
256 }
#define PointerGetDatum(X)
Definition: postgres.h:600
#define FREE(ptr)
Definition: hmac_openssl.c:47
HMAC_CTX * hmacctx
Definition: hmac_openssl.c:58
void explicit_bzero(void *buf, size_t len)
void ResourceOwnerForgetHMAC(ResourceOwner owner, Datum handle)
Definition: resowner.c:1476
ResourceOwner resowner
Definition: hmac_openssl.c:62

◆ pg_hmac_init()

int pg_hmac_init ( pg_hmac_ctx ctx,
const uint8 key,
size_t  len 
)

Definition at line 126 of file hmac_openssl.c.

References ALLOC, pg_hmac_ctx::block_size, pg_hmac_ctx::digest_size, FREE, pg_hmac_ctx::hash, HMAC_IPAD, HMAC_OPAD, pg_hmac_ctx::hmacctx, i, pg_hmac_ctx::k_ipad, pg_hmac_ctx::k_opad, pg_cryptohash_create(), pg_cryptohash_final(), pg_cryptohash_free(), pg_cryptohash_init(), pg_cryptohash_update(), PG_MD5, PG_SHA1, PG_SHA224, PG_SHA256, PG_SHA384, PG_SHA512, status(), and pg_hmac_ctx::type.

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

◆ pg_hmac_update()

int pg_hmac_update ( pg_hmac_ctx ctx,
const uint8 data,
size_t  len 
)

Definition at line 168 of file hmac_openssl.c.

References pg_hmac_ctx::hash, pg_hmac_ctx::hmacctx, pg_cryptohash_update(), and status().

169 {
170  int status = 0;
171 
172  if (ctx == NULL)
173  return -1;
174 
175  status = HMAC_Update(ctx->hmacctx, data, len);
176 
177  /* OpenSSL internals return 1 on success, 0 on failure */
178  if (status <= 0)
179  return -1;
180  return 0;
181 }
HMAC_CTX * hmacctx
Definition: hmac_openssl.c:58
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229