Fix UNLISTEN to fall out quickly if the current backend has never executed
[PostgreSQL.git] / contrib / pgcrypto / internal-sha2.c
blobc54f9605fdb6991ab1be9208ed09f0895f3ec8ae
1 /*
2 * internal.c
3 * Wrapper for builtin functions
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 <time.h>
36 #include "px.h"
37 #include "sha2.h"
39 void init_sha224(PX_MD * h);
40 void init_sha256(PX_MD * h);
41 void init_sha384(PX_MD * h);
42 void init_sha512(PX_MD * h);
44 /* SHA224 */
46 static unsigned
47 int_sha224_len(PX_MD * h)
49 return SHA224_DIGEST_LENGTH;
52 static unsigned
53 int_sha224_block_len(PX_MD * h)
55 return SHA224_BLOCK_LENGTH;
58 static void
59 int_sha224_update(PX_MD * h, const uint8 *data, unsigned dlen)
61 SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
63 SHA224_Update(ctx, data, dlen);
66 static void
67 int_sha224_reset(PX_MD * h)
69 SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
71 SHA224_Init(ctx);
74 static void
75 int_sha224_finish(PX_MD * h, uint8 *dst)
77 SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
79 SHA224_Final(dst, ctx);
82 static void
83 int_sha224_free(PX_MD * h)
85 SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
87 memset(ctx, 0, sizeof(*ctx));
88 px_free(ctx);
89 px_free(h);
92 /* SHA256 */
94 static unsigned
95 int_sha256_len(PX_MD * h)
97 return SHA256_DIGEST_LENGTH;
100 static unsigned
101 int_sha256_block_len(PX_MD * h)
103 return SHA256_BLOCK_LENGTH;
106 static void
107 int_sha256_update(PX_MD * h, const uint8 *data, unsigned dlen)
109 SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
111 SHA256_Update(ctx, data, dlen);
114 static void
115 int_sha256_reset(PX_MD * h)
117 SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
119 SHA256_Init(ctx);
122 static void
123 int_sha256_finish(PX_MD * h, uint8 *dst)
125 SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
127 SHA256_Final(dst, ctx);
130 static void
131 int_sha256_free(PX_MD * h)
133 SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
135 memset(ctx, 0, sizeof(*ctx));
136 px_free(ctx);
137 px_free(h);
140 /* SHA384 */
142 static unsigned
143 int_sha384_len(PX_MD * h)
145 return SHA384_DIGEST_LENGTH;
148 static unsigned
149 int_sha384_block_len(PX_MD * h)
151 return SHA384_BLOCK_LENGTH;
154 static void
155 int_sha384_update(PX_MD * h, const uint8 *data, unsigned dlen)
157 SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
159 SHA384_Update(ctx, data, dlen);
162 static void
163 int_sha384_reset(PX_MD * h)
165 SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
167 SHA384_Init(ctx);
170 static void
171 int_sha384_finish(PX_MD * h, uint8 *dst)
173 SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
175 SHA384_Final(dst, ctx);
178 static void
179 int_sha384_free(PX_MD * h)
181 SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
183 memset(ctx, 0, sizeof(*ctx));
184 px_free(ctx);
185 px_free(h);
188 /* SHA512 */
190 static unsigned
191 int_sha512_len(PX_MD * h)
193 return SHA512_DIGEST_LENGTH;
196 static unsigned
197 int_sha512_block_len(PX_MD * h)
199 return SHA512_BLOCK_LENGTH;
202 static void
203 int_sha512_update(PX_MD * h, const uint8 *data, unsigned dlen)
205 SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
207 SHA512_Update(ctx, data, dlen);
210 static void
211 int_sha512_reset(PX_MD * h)
213 SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
215 SHA512_Init(ctx);
218 static void
219 int_sha512_finish(PX_MD * h, uint8 *dst)
221 SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
223 SHA512_Final(dst, ctx);
226 static void
227 int_sha512_free(PX_MD * h)
229 SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
231 memset(ctx, 0, sizeof(*ctx));
232 px_free(ctx);
233 px_free(h);
236 /* init functions */
238 void
239 init_sha224(PX_MD * md)
241 SHA224_CTX *ctx;
243 ctx = px_alloc(sizeof(*ctx));
244 memset(ctx, 0, sizeof(*ctx));
246 md->p.ptr = ctx;
248 md->result_size = int_sha224_len;
249 md->block_size = int_sha224_block_len;
250 md->reset = int_sha224_reset;
251 md->update = int_sha224_update;
252 md->finish = int_sha224_finish;
253 md->free = int_sha224_free;
255 md->reset(md);
258 void
259 init_sha256(PX_MD * md)
261 SHA256_CTX *ctx;
263 ctx = px_alloc(sizeof(*ctx));
264 memset(ctx, 0, sizeof(*ctx));
266 md->p.ptr = ctx;
268 md->result_size = int_sha256_len;
269 md->block_size = int_sha256_block_len;
270 md->reset = int_sha256_reset;
271 md->update = int_sha256_update;
272 md->finish = int_sha256_finish;
273 md->free = int_sha256_free;
275 md->reset(md);
278 void
279 init_sha384(PX_MD * md)
281 SHA384_CTX *ctx;
283 ctx = px_alloc(sizeof(*ctx));
284 memset(ctx, 0, sizeof(*ctx));
286 md->p.ptr = ctx;
288 md->result_size = int_sha384_len;
289 md->block_size = int_sha384_block_len;
290 md->reset = int_sha384_reset;
291 md->update = int_sha384_update;
292 md->finish = int_sha384_finish;
293 md->free = int_sha384_free;
295 md->reset(md);
298 void
299 init_sha512(PX_MD * md)
301 SHA512_CTX *ctx;
303 ctx = px_alloc(sizeof(*ctx));
304 memset(ctx, 0, sizeof(*ctx));
306 md->p.ptr = ctx;
308 md->result_size = int_sha512_len;
309 md->block_size = int_sha512_block_len;
310 md->reset = int_sha512_reset;
311 md->update = int_sha512_update;
312 md->finish = int_sha512_finish;
313 md->free = int_sha512_free;
315 md->reset(md);