1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*
* px-hmac.c
* HMAC implementation.
*
* Copyright (c) 2001 Marko Kreen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* contrib/pgcrypto/px-hmac.c
*/
#include "postgres.h"
#include "px.h"
#define HMAC_IPAD 0x36
#define HMAC_OPAD 0x5C
static unsigned
hmac_result_size(PX_HMAC *h)
{
return px_md_result_size(h->md);
}
static unsigned
hmac_block_size(PX_HMAC *h)
{
return px_md_block_size(h->md);
}
static void
hmac_init(PX_HMAC *h, const uint8 *key, unsigned klen)
{
unsigned bs,
i;
uint8 *keybuf;
PX_MD *md = h->md;
bs = px_md_block_size(md);
keybuf = palloc0(bs);
if (klen > bs)
{
px_md_update(md, key, klen);
px_md_finish(md, keybuf);
px_md_reset(md);
}
else
memcpy(keybuf, key, klen);
for (i = 0; i < bs; i++)
{
h->p.ipad[i] = keybuf[i] ^ HMAC_IPAD;
h->p.opad[i] = keybuf[i] ^ HMAC_OPAD;
}
px_memset(keybuf, 0, bs);
pfree(keybuf);
px_md_update(md, h->p.ipad, bs);
}
static void
hmac_reset(PX_HMAC *h)
{
PX_MD *md = h->md;
unsigned bs = px_md_block_size(md);
px_md_reset(md);
px_md_update(md, h->p.ipad, bs);
}
static void
hmac_update(PX_HMAC *h, const uint8 *data, unsigned dlen)
{
px_md_update(h->md, data, dlen);
}
static void
hmac_finish(PX_HMAC *h, uint8 *dst)
{
PX_MD *md = h->md;
unsigned bs,
hlen;
uint8 *buf;
bs = px_md_block_size(md);
hlen = px_md_result_size(md);
buf = palloc(hlen);
px_md_finish(md, buf);
px_md_reset(md);
px_md_update(md, h->p.opad, bs);
px_md_update(md, buf, hlen);
px_md_finish(md, dst);
px_memset(buf, 0, hlen);
pfree(buf);
}
static void
hmac_free(PX_HMAC *h)
{
unsigned bs;
bs = px_md_block_size(h->md);
px_md_free(h->md);
px_memset(h->p.ipad, 0, bs);
px_memset(h->p.opad, 0, bs);
pfree(h->p.ipad);
pfree(h->p.opad);
pfree(h);
}
/* PUBLIC FUNCTIONS */
int
px_find_hmac(const char *name, PX_HMAC **res)
{
int err;
PX_MD *md;
PX_HMAC *h;
unsigned bs;
err = px_find_digest(name, &md);
if (err)
return err;
bs = px_md_block_size(md);
if (bs < 2)
{
px_md_free(md);
return PXE_HASH_UNUSABLE_FOR_HMAC;
}
h = palloc(sizeof(*h));
h->p.ipad = palloc(bs);
h->p.opad = palloc(bs);
h->md = md;
h->result_size = hmac_result_size;
h->block_size = hmac_block_size;
h->reset = hmac_reset;
h->update = hmac_update;
h->finish = hmac_finish;
h->free = hmac_free;
h->init = hmac_init;
*res = h;
return 0;
}
|