PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
internal-sha2.c
Go to the documentation of this file.
1 /*
2  * internal.c
3  * Wrapper for builtin functions
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/internal-sha2.c
30  */
31 
32 #include "postgres.h"
33 
34 #include <time.h>
35 
36 #include "common/sha2.h"
37 #include "px.h"
38 
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);
43 
44 /* SHA224 */
45 
46 static unsigned
48 {
50 }
51 
52 static unsigned
54 {
56 }
57 
58 static void
59 int_sha224_update(PX_MD *h, const uint8 *data, unsigned dlen)
60 {
61  pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
62 
63  pg_sha224_update(ctx, data, dlen);
64 }
65 
66 static void
68 {
69  pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
70 
71  pg_sha224_init(ctx);
72 }
73 
74 static void
76 {
77  pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
78 
79  pg_sha224_final(ctx, dst);
80 }
81 
82 static void
84 {
85  pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
86 
87  px_memset(ctx, 0, sizeof(*ctx));
88  px_free(ctx);
89  px_free(h);
90 }
91 
92 /* SHA256 */
93 
94 static unsigned
96 {
98 }
99 
100 static unsigned
102 {
103  return PG_SHA256_BLOCK_LENGTH;
104 }
105 
106 static void
107 int_sha256_update(PX_MD *h, const uint8 *data, unsigned dlen)
108 {
109  pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
110 
111  pg_sha256_update(ctx, data, dlen);
112 }
113 
114 static void
116 {
117  pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
118 
119  pg_sha256_init(ctx);
120 }
121 
122 static void
124 {
125  pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
126 
127  pg_sha256_final(ctx, dst);
128 }
129 
130 static void
132 {
133  pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
134 
135  px_memset(ctx, 0, sizeof(*ctx));
136  px_free(ctx);
137  px_free(h);
138 }
139 
140 /* SHA384 */
141 
142 static unsigned
144 {
146 }
147 
148 static unsigned
150 {
151  return PG_SHA384_BLOCK_LENGTH;
152 }
153 
154 static void
155 int_sha384_update(PX_MD *h, const uint8 *data, unsigned dlen)
156 {
157  pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
158 
159  pg_sha384_update(ctx, data, dlen);
160 }
161 
162 static void
164 {
165  pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
166 
167  pg_sha384_init(ctx);
168 }
169 
170 static void
172 {
173  pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
174 
175  pg_sha384_final(ctx, dst);
176 }
177 
178 static void
180 {
181  pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
182 
183  px_memset(ctx, 0, sizeof(*ctx));
184  px_free(ctx);
185  px_free(h);
186 }
187 
188 /* SHA512 */
189 
190 static unsigned
192 {
194 }
195 
196 static unsigned
198 {
199  return PG_SHA512_BLOCK_LENGTH;
200 }
201 
202 static void
203 int_sha512_update(PX_MD *h, const uint8 *data, unsigned dlen)
204 {
205  pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
206 
207  pg_sha512_update(ctx, data, dlen);
208 }
209 
210 static void
212 {
213  pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
214 
215  pg_sha512_init(ctx);
216 }
217 
218 static void
220 {
221  pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
222 
223  pg_sha512_final(ctx, dst);
224 }
225 
226 static void
228 {
229  pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
230 
231  px_memset(ctx, 0, sizeof(*ctx));
232  px_free(ctx);
233  px_free(h);
234 }
235 
236 /* init functions */
237 
238 void
240 {
241  pg_sha224_ctx *ctx;
242 
243  ctx = px_alloc(sizeof(*ctx));
244  memset(ctx, 0, sizeof(*ctx));
245 
246  md->p.ptr = ctx;
247 
250  md->reset = int_sha224_reset;
253  md->free = int_sha224_free;
254 
255  md->reset(md);
256 }
257 
258 void
260 {
261  pg_sha256_ctx *ctx;
262 
263  ctx = px_alloc(sizeof(*ctx));
264  memset(ctx, 0, sizeof(*ctx));
265 
266  md->p.ptr = ctx;
267 
270  md->reset = int_sha256_reset;
273  md->free = int_sha256_free;
274 
275  md->reset(md);
276 }
277 
278 void
280 {
281  pg_sha384_ctx *ctx;
282 
283  ctx = px_alloc(sizeof(*ctx));
284  memset(ctx, 0, sizeof(*ctx));
285 
286  md->p.ptr = ctx;
287 
290  md->reset = int_sha384_reset;
293  md->free = int_sha384_free;
294 
295  md->reset(md);
296 }
297 
298 void
300 {
301  pg_sha512_ctx *ctx;
302 
303  ctx = px_alloc(sizeof(*ctx));
304  memset(ctx, 0, sizeof(*ctx));
305 
306  md->p.ptr = ctx;
307 
310  md->reset = int_sha512_reset;
313  md->free = int_sha512_free;
314 
315  md->reset(md);
316 }
static void int_sha384_update(PX_MD *h, const uint8 *data, unsigned dlen)
void init_sha256(PX_MD *h)
void * ptr
Definition: px.h:125
unsigned(* result_size)(PX_MD *h)
Definition: px.h:115
#define PG_SHA384_BLOCK_LENGTH
Definition: sha2.h:64
static unsigned int_sha512_len(PX_MD *h)
#define px_free(p)
Definition: px.h:46
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:62
unsigned char uint8
Definition: c.h:266
static void int_sha384_reset(PX_MD *h)
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:68
static void int_sha256_reset(PX_MD *h)
void pg_sha256_init(pg_sha256_ctx *context)
Definition: sha2.c:268
void(* update)(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: px.h:118
void pg_sha384_update(pg_sha384_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:933
void(* free)(PX_MD *h)
Definition: px.h:120
static void int_sha512_reset(PX_MD *h)
static unsigned int_sha384_block_len(PX_MD *h)
#define PG_SHA256_BLOCK_LENGTH
Definition: sha2.h:61
static void int_sha384_finish(PX_MD *h, uint8 *dst)
Definition: px.h:113
static void int_sha224_reset(PX_MD *h)
Definition: internal-sha2.c:67
void pg_sha512_final(pg_sha512_ctx *context, uint8 *digest)
Definition: sha2.c:894
static void int_sha384_free(PX_MD *h)
static unsigned int_sha256_len(PX_MD *h)
Definition: internal-sha2.c:95
#define PG_SHA512_BLOCK_LENGTH
Definition: sha2.h:67
void pg_sha224_final(pg_sha224_ctx *context, uint8 *digest)
Definition: sha2.c:983
void pg_sha512_update(pg_sha512_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:791
static void int_sha512_update(PX_MD *h, const uint8 *data, unsigned dlen)
static unsigned int_sha224_block_len(PX_MD *h)
Definition: internal-sha2.c:53
static unsigned int_sha512_block_len(PX_MD *h)
void(* finish)(PX_MD *h, uint8 *dst)
Definition: px.h:119
static void int_sha224_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal-sha2.c:59
void pg_sha224_init(pg_sha224_ctx *context)
Definition: sha2.c:967
#define PG_SHA224_BLOCK_LENGTH
Definition: sha2.h:58
void pg_sha256_update(pg_sha256_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:465
union px_digest::@13 p
static void int_sha256_finish(PX_MD *h, uint8 *dst)
static void int_sha256_update(PX_MD *h, const uint8 *data, unsigned dlen)
void pg_sha224_update(pg_sha224_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:977
void init_sha224(PX_MD *h)
void pg_sha384_final(pg_sha384_ctx *context, uint8 *digest)
Definition: sha2.c:939
static unsigned int_sha384_len(PX_MD *h)
static unsigned int_sha256_block_len(PX_MD *h)
static unsigned int_sha224_len(PX_MD *h)
Definition: internal-sha2.c:47
void pg_sha384_init(pg_sha384_ctx *context)
Definition: sha2.c:923
void init_sha384(PX_MD *h)
void init_sha512(PX_MD *h)
#define px_alloc(s)
Definition: px.h:44
void pg_sha512_init(pg_sha512_ctx *context)
Definition: sha2.c:594
void pg_sha256_final(pg_sha256_ctx *context, uint8 *digest)
Definition: sha2.c:566
void(* reset)(PX_MD *h)
Definition: px.h:117
static void int_sha224_free(PX_MD *h)
Definition: internal-sha2.c:83
unsigned(* block_size)(PX_MD *h)
Definition: px.h:116
static void int_sha512_finish(PX_MD *h, uint8 *dst)
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:59
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:65
static void int_sha224_finish(PX_MD *h, uint8 *dst)
Definition: internal-sha2.c:75
static void int_sha512_free(PX_MD *h)
static void int_sha256_free(PX_MD *h)