Treat EOF like \n for line-counting purposes in ParseConfigFile,
[PostgreSQL.git] / contrib / pgcrypto / px-hmac.c
blob4883a008e44f41a45c3fd405c521b3def476af5c
1 /*
2 * px-hmac.c
3 * HMAC implementation.
5 * Copyright (c) 2001 Marko Kreen
6 * All rights reserved.
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.
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.
29 * $PostgreSQL$
32 #include "postgres.h"
34 #include "px.h"
36 #define HMAC_IPAD 0x36
37 #define HMAC_OPAD 0x5C
39 static unsigned
40 hmac_result_size(PX_HMAC * h)
42 return px_md_result_size(h->md);
45 static unsigned
46 hmac_block_size(PX_HMAC * h)
48 return px_md_block_size(h->md);
51 static void
52 hmac_init(PX_HMAC * h, const uint8 *key, unsigned klen)
54 unsigned bs,
55 hlen,
57 uint8 *keybuf;
58 PX_MD *md = h->md;
60 bs = px_md_block_size(md);
61 hlen = px_md_result_size(md);
62 keybuf = px_alloc(bs);
63 memset(keybuf, 0, bs);
65 if (klen > bs)
67 px_md_update(md, key, klen);
68 px_md_finish(md, keybuf);
69 px_md_reset(md);
71 else
72 memcpy(keybuf, key, klen);
74 for (i = 0; i < bs; i++)
76 h->p.ipad[i] = keybuf[i] ^ HMAC_IPAD;
77 h->p.opad[i] = keybuf[i] ^ HMAC_OPAD;
80 memset(keybuf, 0, bs);
81 px_free(keybuf);
83 px_md_update(md, h->p.ipad, bs);
86 static void
87 hmac_reset(PX_HMAC * h)
89 PX_MD *md = h->md;
90 unsigned bs = px_md_block_size(md);
92 px_md_reset(md);
93 px_md_update(md, h->p.ipad, bs);
96 static void
97 hmac_update(PX_HMAC * h, const uint8 *data, unsigned dlen)
99 px_md_update(h->md, data, dlen);
102 static void
103 hmac_finish(PX_HMAC * h, uint8 *dst)
105 PX_MD *md = h->md;
106 unsigned bs,
107 hlen;
108 uint8 *buf;
110 bs = px_md_block_size(md);
111 hlen = px_md_result_size(md);
113 buf = px_alloc(hlen);
115 px_md_finish(md, buf);
117 px_md_reset(md);
118 px_md_update(md, h->p.opad, bs);
119 px_md_update(md, buf, hlen);
120 px_md_finish(md, dst);
122 memset(buf, 0, hlen);
123 px_free(buf);
126 static void
127 hmac_free(PX_HMAC * h)
129 unsigned bs;
131 bs = px_md_block_size(h->md);
132 px_md_free(h->md);
134 memset(h->p.ipad, 0, bs);
135 memset(h->p.opad, 0, bs);
136 px_free(h->p.ipad);
137 px_free(h->p.opad);
138 px_free(h);
142 /* PUBLIC FUNCTIONS */
145 px_find_hmac(const char *name, PX_HMAC ** res)
147 int err;
148 PX_MD *md;
149 PX_HMAC *h;
150 unsigned bs;
152 err = px_find_digest(name, &md);
153 if (err)
154 return err;
156 bs = px_md_block_size(md);
157 if (bs < 2)
159 px_md_free(md);
160 return PXE_HASH_UNUSABLE_FOR_HMAC;
163 h = px_alloc(sizeof(*h));
164 h->p.ipad = px_alloc(bs);
165 h->p.opad = px_alloc(bs);
166 h->md = md;
168 h->result_size = hmac_result_size;
169 h->block_size = hmac_block_size;
170 h->reset = hmac_reset;
171 h->update = hmac_update;
172 h->finish = hmac_finish;
173 h->free = hmac_free;
174 h->init = hmac_init;
176 *res = h;
178 return 0;