PostgreSQL Source Code  git master
px-hmac.c File Reference
#include "postgres.h"
#include "px.h"
Include dependency graph for px-hmac.c:

Go to the source code of this file.

Macros

#define HMAC_IPAD   0x36
 
#define HMAC_OPAD   0x5C
 

Functions

static unsigned hmac_result_size (PX_HMAC *h)
 
static unsigned hmac_block_size (PX_HMAC *h)
 
static void hmac_init (PX_HMAC *h, const uint8 *key, unsigned klen)
 
static void hmac_reset (PX_HMAC *h)
 
static void hmac_update (PX_HMAC *h, const uint8 *data, unsigned dlen)
 
static void hmac_finish (PX_HMAC *h, uint8 *dst)
 
static void hmac_free (PX_HMAC *h)
 
int px_find_hmac (const char *name, PX_HMAC **res)
 

Macro Definition Documentation

◆ HMAC_IPAD

#define HMAC_IPAD   0x36

Definition at line 36 of file px-hmac.c.

Referenced by hmac_init().

◆ HMAC_OPAD

#define HMAC_OPAD   0x5C

Definition at line 37 of file px-hmac.c.

Referenced by hmac_init().

Function Documentation

◆ hmac_block_size()

static unsigned hmac_block_size ( PX_HMAC h)
static

Definition at line 46 of file px-hmac.c.

References px_hmac::md, and px_md_block_size.

Referenced by px_find_hmac().

47 {
48  return px_md_block_size(h->md);
49 }
#define px_md_block_size(md)
Definition: px.h:191
PX_MD * md
Definition: px.h:130

◆ hmac_finish()

static void hmac_finish ( PX_HMAC h,
uint8 dst 
)
static

Definition at line 100 of file px-hmac.c.

References buf, px_hmac::md, px_hmac::opad, px_hmac::p, palloc(), pfree(), px_md_block_size, px_md_finish, px_md_reset, px_md_result_size, px_md_update, and px_memset().

Referenced by px_find_hmac().

101 {
102  PX_MD *md = h->md;
103  unsigned bs,
104  hlen;
105  uint8 *buf;
106 
107  bs = px_md_block_size(md);
108  hlen = px_md_result_size(md);
109 
110  buf = palloc(hlen);
111 
112  px_md_finish(md, buf);
113 
114  px_md_reset(md);
115  px_md_update(md, h->p.opad, bs);
116  px_md_update(md, buf, hlen);
117  px_md_finish(md, dst);
118 
119  px_memset(buf, 0, hlen);
120  pfree(buf);
121 }
#define px_md_update(md, data, dlen)
Definition: px.h:193
struct px_hmac::@13 p
unsigned char uint8
Definition: c.h:427
#define px_md_finish(md, buf)
Definition: px.h:194
#define px_md_block_size(md)
Definition: px.h:191
#define px_md_reset(md)
Definition: px.h:192
PX_MD * md
Definition: px.h:130
void pfree(void *pointer)
Definition: mcxt.c:1057
#define px_md_result_size(md)
Definition: px.h:190
Definition: px.h:98
static char * buf
Definition: pg_test_fsync.c:68
uint8 * opad
Definition: px.h:135
void * palloc(Size size)
Definition: mcxt.c:950
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127

◆ hmac_free()

static void hmac_free ( PX_HMAC h)
static

Definition at line 124 of file px-hmac.c.

References px_hmac::ipad, px_hmac::md, px_hmac::opad, px_hmac::p, pfree(), px_md_block_size, px_md_free, and px_memset().

Referenced by px_find_hmac().

125 {
126  unsigned bs;
127 
128  bs = px_md_block_size(h->md);
129  px_md_free(h->md);
130 
131  px_memset(h->p.ipad, 0, bs);
132  px_memset(h->p.opad, 0, bs);
133  pfree(h->p.ipad);
134  pfree(h->p.opad);
135  pfree(h);
136 }
struct px_hmac::@13 p
#define px_md_block_size(md)
Definition: px.h:191
PX_MD * md
Definition: px.h:130
void pfree(void *pointer)
Definition: mcxt.c:1057
uint8 * opad
Definition: px.h:135
#define px_md_free(md)
Definition: px.h:195
uint8 * ipad
Definition: px.h:134
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127

◆ hmac_init()

static void hmac_init ( PX_HMAC h,
const uint8 key,
unsigned  klen 
)
static

Definition at line 52 of file px-hmac.c.

References HMAC_IPAD, HMAC_OPAD, i, px_hmac::ipad, px_hmac::md, px_hmac::opad, px_hmac::p, palloc0(), pfree(), px_md_block_size, px_md_finish, px_md_reset, px_md_update, and px_memset().

Referenced by px_find_hmac().

53 {
54  unsigned bs,
55  i;
56  uint8 *keybuf;
57  PX_MD *md = h->md;
58 
59  bs = px_md_block_size(md);
60  keybuf = palloc0(bs);
61 
62  if (klen > bs)
63  {
64  px_md_update(md, key, klen);
65  px_md_finish(md, keybuf);
66  px_md_reset(md);
67  }
68  else
69  memcpy(keybuf, key, klen);
70 
71  for (i = 0; i < bs; i++)
72  {
73  h->p.ipad[i] = keybuf[i] ^ HMAC_IPAD;
74  h->p.opad[i] = keybuf[i] ^ HMAC_OPAD;
75  }
76 
77  px_memset(keybuf, 0, bs);
78  pfree(keybuf);
79 
80  px_md_update(md, h->p.ipad, bs);
81 }
#define HMAC_IPAD
Definition: px-hmac.c:36
#define px_md_update(md, data, dlen)
Definition: px.h:193
struct px_hmac::@13 p
unsigned char uint8
Definition: c.h:427
#define px_md_finish(md, buf)
Definition: px.h:194
#define px_md_block_size(md)
Definition: px.h:191
#define px_md_reset(md)
Definition: px.h:192
PX_MD * md
Definition: px.h:130
void pfree(void *pointer)
Definition: mcxt.c:1057
#define HMAC_OPAD
Definition: px-hmac.c:37
Definition: px.h:98
uint8 * opad
Definition: px.h:135
void * palloc0(Size size)
Definition: mcxt.c:981
uint8 * ipad
Definition: px.h:134
int i
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127

◆ hmac_reset()

static void hmac_reset ( PX_HMAC h)
static

Definition at line 84 of file px-hmac.c.

References px_hmac::ipad, px_hmac::md, px_hmac::p, px_md_block_size, px_md_reset, and px_md_update.

Referenced by px_find_hmac().

85 {
86  PX_MD *md = h->md;
87  unsigned bs = px_md_block_size(md);
88 
89  px_md_reset(md);
90  px_md_update(md, h->p.ipad, bs);
91 }
#define px_md_update(md, data, dlen)
Definition: px.h:193
struct px_hmac::@13 p
#define px_md_block_size(md)
Definition: px.h:191
#define px_md_reset(md)
Definition: px.h:192
PX_MD * md
Definition: px.h:130
Definition: px.h:98
uint8 * ipad
Definition: px.h:134

◆ hmac_result_size()

static unsigned hmac_result_size ( PX_HMAC h)
static

Definition at line 40 of file px-hmac.c.

References px_hmac::md, and px_md_result_size.

Referenced by px_find_hmac().

41 {
42  return px_md_result_size(h->md);
43 }
PX_MD * md
Definition: px.h:130
#define px_md_result_size(md)
Definition: px.h:190

◆ hmac_update()

static void hmac_update ( PX_HMAC h,
const uint8 data,
unsigned  dlen 
)
static

Definition at line 94 of file px-hmac.c.

References px_hmac::md, and px_md_update.

Referenced by px_find_hmac().

95 {
96  px_md_update(h->md, data, dlen);
97 }
#define px_md_update(md, data, dlen)
Definition: px.h:193
PX_MD * md
Definition: px.h:130

◆ px_find_hmac()

int px_find_hmac ( const char *  name,
PX_HMAC **  res 
)

Definition at line 142 of file px-hmac.c.

References px_hmac::block_size, px_hmac::finish, px_hmac::free, hmac_block_size(), hmac_finish(), hmac_free(), hmac_init(), hmac_reset(), hmac_result_size(), hmac_update(), px_hmac::init, px_hmac::ipad, px_hmac::md, px_hmac::opad, px_hmac::p, palloc(), px_find_digest(), px_md_block_size, px_md_free, PXE_HASH_UNUSABLE_FOR_HMAC, px_hmac::reset, px_hmac::result_size, and px_hmac::update.

Referenced by pg_hmac().

143 {
144  int err;
145  PX_MD *md;
146  PX_HMAC *h;
147  unsigned bs;
148 
149  err = px_find_digest(name, &md);
150  if (err)
151  return err;
152 
153  bs = px_md_block_size(md);
154  if (bs < 2)
155  {
156  px_md_free(md);
158  }
159 
160  h = palloc(sizeof(*h));
161  h->p.ipad = palloc(bs);
162  h->p.opad = palloc(bs);
163  h->md = md;
164 
167  h->reset = hmac_reset;
168  h->update = hmac_update;
169  h->finish = hmac_finish;
170  h->free = hmac_free;
171  h->init = hmac_init;
172 
173  *res = h;
174 
175  return 0;
176 }
#define PXE_HASH_UNUSABLE_FOR_HMAC
Definition: px.h:55
static void hmac_init(PX_HMAC *h, const uint8 *key, unsigned klen)
Definition: px-hmac.c:52
struct px_hmac::@13 p
void(* free)(PX_HMAC *h)
Definition: px.h:127
static void hmac_free(PX_HMAC *h)
Definition: px-hmac.c:124
unsigned(* result_size)(PX_HMAC *h)
Definition: px.h:122
#define px_md_block_size(md)
Definition: px.h:191
static void hmac_update(PX_HMAC *h, const uint8 *data, unsigned dlen)
Definition: px-hmac.c:94
static void hmac_finish(PX_HMAC *h, uint8 *dst)
Definition: px-hmac.c:100
PX_MD * md
Definition: px.h:130
void(* reset)(PX_HMAC *h)
Definition: px.h:124
Definition: px.h:98
void(* update)(PX_HMAC *h, const uint8 *data, unsigned dlen)
Definition: px.h:125
void(* finish)(PX_HMAC *h, uint8 *dst)
Definition: px.h:126
uint8 * opad
Definition: px.h:135
static void hmac_reset(PX_HMAC *h)
Definition: px-hmac.c:84
#define px_md_free(md)
Definition: px.h:195
static unsigned hmac_result_size(PX_HMAC *h)
Definition: px-hmac.c:40
void(* init)(PX_HMAC *h, const uint8 *key, unsigned klen)
Definition: px.h:128
static unsigned hmac_block_size(PX_HMAC *h)
Definition: px-hmac.c:46
const char * name
Definition: encode.c:561
uint8 * ipad
Definition: px.h:134
void * palloc(Size size)
Definition: mcxt.c:950
unsigned(* block_size)(PX_HMAC *h)
Definition: px.h:123
Definition: px.h:120
int px_find_digest(const char *name, PX_MD **res)
Definition: internal.c:553