PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
px-hmac.c
Go to the documentation of this file.
1 /*
2  * px-hmac.c
3  * HMAC implementation.
4  *
5  * Copyright (c) 2001 Marko Kreen
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * contrib/pgcrypto/px-hmac.c
30  */
31 
32 #include "postgres.h"
33 
34 #include "px.h"
35 
36 #define HMAC_IPAD 0x36
37 #define HMAC_OPAD 0x5C
38 
39 static unsigned
41 {
42  return px_md_result_size(h->md);
43 }
44 
45 static unsigned
47 {
48  return px_md_block_size(h->md);
49 }
50 
51 static void
52 hmac_init(PX_HMAC *h, const uint8 *key, unsigned klen)
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 }
83 
84 static void
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 }
93 
94 static void
95 hmac_update(PX_HMAC *h, const uint8 *data, unsigned dlen)
96 {
97  px_md_update(h->md, data, dlen);
98 }
99 
100 static void
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 }
123 
124 static void
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 }
138 
139 
140 /* PUBLIC FUNCTIONS */
141 
142 int
143 px_find_hmac(const char *name, PX_HMAC **res)
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
#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:266
static void hmac_free(PX_HMAC *h)
Definition: px-hmac.c:125
#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
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
#define px_md_result_size(md)
Definition: px.h:205
void(* update)(PX_HMAC *h, const uint8 *data, unsigned dlen)
Definition: px.h:140
#define HMAC_OPAD
Definition: px-hmac.c:37
Definition: px.h:113
static char * buf
Definition: pg_test_fsync.c:66
int px_find_hmac(const char *name, PX_HMAC **res)
Definition: px-hmac.c:143
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
int i
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 px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
void(* free)(PX_HMAC *h)
Definition: px.h:142