PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

#define HMAC_IPAD   0x36

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

Referenced by hmac_init().

#define HMAC_OPAD   0x5C

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

Referenced by hmac_init().

Function Documentation

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:206
PX_MD * md
Definition: px.h:145
static void hmac_finish ( PX_HMAC h,
uint8 dst 
)
static

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

References buf, px_hmac::md, px_hmac::opad, px_hmac::p, px_alloc, px_free, 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().

102 {
103  PX_MD *md = h->md;
104  unsigned bs,
105  hlen;
106  uint8 *buf;
107 
108  bs = px_md_block_size(md);
109  hlen = px_md_result_size(md);
110 
111  buf = px_alloc(hlen);
112 
113  px_md_finish(md, buf);
114 
115  px_md_reset(md);
116  px_md_update(md, h->p.opad, bs);
117  px_md_update(md, buf, hlen);
118  px_md_finish(md, dst);
119 
120  px_memset(buf, 0, hlen);
121  px_free(buf);
122 }
#define px_free(p)
Definition: px.h:46
#define px_md_update(md, data, dlen)
Definition: px.h:208
struct px_hmac::@14 p
unsigned char uint8
Definition: c.h:263
#define px_md_finish(md, buf)
Definition: px.h:209
#define px_md_block_size(md)
Definition: px.h:206
#define px_md_reset(md)
Definition: px.h:207
PX_MD * md
Definition: px.h:145
#define px_md_result_size(md)
Definition: px.h:205
Definition: px.h:113
static char * buf
Definition: pg_test_fsync.c:65
uint8 * opad
Definition: px.h:150
#define px_alloc(s)
Definition: px.h:44
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
static void hmac_free ( PX_HMAC h)
static

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

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

Referenced by px_find_hmac().

126 {
127  unsigned bs;
128 
129  bs = px_md_block_size(h->md);
130  px_md_free(h->md);
131 
132  px_memset(h->p.ipad, 0, bs);
133  px_memset(h->p.opad, 0, bs);
134  px_free(h->p.ipad);
135  px_free(h->p.opad);
136  px_free(h);
137 }
#define px_free(p)
Definition: px.h:46
struct px_hmac::@14 p
#define px_md_block_size(md)
Definition: px.h:206
PX_MD * md
Definition: px.h:145
uint8 * opad
Definition: px.h:150
#define px_md_free(md)
Definition: px.h:210
uint8 * ipad
Definition: px.h:149
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
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, px_alloc, px_free, 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 = px_alloc(bs);
61  memset(keybuf, 0, bs);
62 
63  if (klen > bs)
64  {
65  px_md_update(md, key, klen);
66  px_md_finish(md, keybuf);
67  px_md_reset(md);
68  }
69  else
70  memcpy(keybuf, key, klen);
71 
72  for (i = 0; i < bs; i++)
73  {
74  h->p.ipad[i] = keybuf[i] ^ HMAC_IPAD;
75  h->p.opad[i] = keybuf[i] ^ HMAC_OPAD;
76  }
77 
78  px_memset(keybuf, 0, bs);
79  px_free(keybuf);
80 
81  px_md_update(md, h->p.ipad, bs);
82 }
#define HMAC_IPAD
Definition: px-hmac.c:36
#define px_free(p)
Definition: px.h:46
#define px_md_update(md, data, dlen)
Definition: px.h:208
struct px_hmac::@14 p
unsigned char uint8
Definition: c.h:263
#define px_md_finish(md, buf)
Definition: px.h:209
#define px_md_block_size(md)
Definition: px.h:206
#define px_md_reset(md)
Definition: px.h:207
PX_MD * md
Definition: px.h:145
#define HMAC_OPAD
Definition: px-hmac.c:37
Definition: px.h:113
uint8 * opad
Definition: px.h:150
#define px_alloc(s)
Definition: px.h:44
uint8 * ipad
Definition: px.h:149
int i
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
static void hmac_reset ( PX_HMAC h)
static

Definition at line 85 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().

86 {
87  PX_MD *md = h->md;
88  unsigned bs = px_md_block_size(md);
89 
90  px_md_reset(md);
91  px_md_update(md, h->p.ipad, bs);
92 }
#define px_md_update(md, data, dlen)
Definition: px.h:208
struct px_hmac::@14 p
#define px_md_block_size(md)
Definition: px.h:206
#define px_md_reset(md)
Definition: px.h:207
PX_MD * md
Definition: px.h:145
Definition: px.h:113
uint8 * ipad
Definition: px.h:149
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:145
#define px_md_result_size(md)
Definition: px.h:205
static void hmac_update ( PX_HMAC h,
const uint8 data,
unsigned  dlen 
)
static

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

References px_hmac::md, and px_md_update.

Referenced by px_find_hmac().

96 {
97  px_md_update(h->md, data, dlen);
98 }
#define px_md_update(md, data, dlen)
Definition: px.h:208
PX_MD * md
Definition: px.h:145
int px_find_hmac ( const char *  name,
PX_HMAC **  res 
)

Definition at line 143 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, px_alloc, 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().

144 {
145  int err;
146  PX_MD *md;
147  PX_HMAC *h;
148  unsigned bs;
149 
150  err = px_find_digest(name, &md);
151  if (err)
152  return err;
153 
154  bs = px_md_block_size(md);
155  if (bs < 2)
156  {
157  px_md_free(md);
159  }
160 
161  h = px_alloc(sizeof(*h));
162  h->p.ipad = px_alloc(bs);
163  h->p.opad = px_alloc(bs);
164  h->md = md;
165 
168  h->reset = hmac_reset;
169  h->update = hmac_update;
170  h->finish = hmac_finish;
171  h->free = hmac_free;
172  h->init = hmac_init;
173 
174  *res = h;
175 
176  return 0;
177 }
#define PXE_HASH_UNUSABLE_FOR_HMAC
Definition: px.h:71
static void hmac_init(PX_HMAC *h, const uint8 *key, unsigned klen)
Definition: px-hmac.c:52
struct px_hmac::@14 p
static void hmac_free(PX_HMAC *h)
Definition: px-hmac.c:125
#define px_md_block_size(md)
Definition: px.h:206
static void hmac_update(PX_HMAC *h, const uint8 *data, unsigned dlen)
Definition: px-hmac.c:95
void(* init)(PX_HMAC *h, const uint8 *key, unsigned klen)
Definition: px.h:143
static void hmac_finish(PX_HMAC *h, uint8 *dst)
Definition: px-hmac.c:101
PX_MD * md
Definition: px.h:145
unsigned(* block_size)(PX_HMAC *h)
Definition: px.h:138
void(* update)(PX_HMAC *h, const uint8 *data, unsigned dlen)
Definition: px.h:140
Definition: px.h:113
uint8 * opad
Definition: px.h:150
static void hmac_reset(PX_HMAC *h)
Definition: px-hmac.c:85
#define px_md_free(md)
Definition: px.h:210
unsigned(* result_size)(PX_HMAC *h)
Definition: px.h:137
void(* finish)(PX_HMAC *h, uint8 *dst)
Definition: px.h:141
static unsigned hmac_result_size(PX_HMAC *h)
Definition: px-hmac.c:40
static unsigned hmac_block_size(PX_HMAC *h)
Definition: px-hmac.c:46
const char * name
Definition: encode.c:521
#define px_alloc(s)
Definition: px.h:44
uint8 * ipad
Definition: px.h:149
Definition: px.h:135
int px_find_digest(const char *name, PX_MD **res)
Definition: internal.c:578
void(* reset)(PX_HMAC *h)
Definition: px.h:139
void(* free)(PX_HMAC *h)
Definition: px.h:142