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 "px.h"
37 #include "sha2.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 {
49  return SHA224_DIGEST_LENGTH;
50 }
51 
52 static unsigned
54 {
55  return SHA224_BLOCK_LENGTH;
56 }
57 
58 static void
59 int_sha224_update(PX_MD *h, const uint8 *data, unsigned dlen)
60 {
61  SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
62 
63  SHA224_Update(ctx, data, dlen);
64 }
65 
66 static void
68 {
69  SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
70 
71  SHA224_Init(ctx);
72 }
73 
74 static void
76 {
77  SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
78 
79  SHA224_Final(dst, ctx);
80 }
81 
82 static void
84 {
85  SHA224_CTX *ctx = (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 {
97  return SHA256_DIGEST_LENGTH;
98 }
99 
100 static unsigned
102 {
103  return SHA256_BLOCK_LENGTH;
104 }
105 
106 static void
107 int_sha256_update(PX_MD *h, const uint8 *data, unsigned dlen)
108 {
109  SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
110 
111  SHA256_Update(ctx, data, dlen);
112 }
113 
114 static void
116 {
117  SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
118 
119  SHA256_Init(ctx);
120 }
121 
122 static void
124 {
125  SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
126 
127  SHA256_Final(dst, ctx);
128 }
129 
130 static void
132 {
133  SHA256_CTX *ctx = (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 {
145  return SHA384_DIGEST_LENGTH;
146 }
147 
148 static unsigned
150 {
151  return SHA384_BLOCK_LENGTH;
152 }
153 
154 static void
155 int_sha384_update(PX_MD *h, const uint8 *data, unsigned dlen)
156 {
157  SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
158 
159  SHA384_Update(ctx, data, dlen);
160 }
161 
162 static void
164 {
165  SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
166 
167  SHA384_Init(ctx);
168 }
169 
170 static void
172 {
173  SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
174 
175  SHA384_Final(dst, ctx);
176 }
177 
178 static void
180 {
181  SHA384_CTX *ctx = (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 {
193  return SHA512_DIGEST_LENGTH;
194 }
195 
196 static unsigned
198 {
199  return SHA512_BLOCK_LENGTH;
200 }
201 
202 static void
203 int_sha512_update(PX_MD *h, const uint8 *data, unsigned dlen)
204 {
205  SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
206 
207  SHA512_Update(ctx, data, dlen);
208 }
209 
210 static void
212 {
213  SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
214 
215  SHA512_Init(ctx);
216 }
217 
218 static void
220 {
221  SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
222 
223  SHA512_Final(dst, ctx);
224 }
225 
226 static void
228 {
229  SHA512_CTX *ctx = (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  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  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  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  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 }
#define SHA384_Final
Definition: sha2.h:47
static void int_sha384_update(PX_MD *h, const uint8 *data, unsigned dlen)
void init_sha256(PX_MD *h)
void * ptr
Definition: px.h:126
unsigned(* result_size)(PX_MD *h)
Definition: px.h:116
#define SHA384_BLOCK_LENGTH
Definition: sha2.h:59
static unsigned int_sha512_len(PX_MD *h)
#define px_free(p)
Definition: px.h:47
unsigned char uint8
Definition: c.h:263
#define SHA256_BLOCK_LENGTH
Definition: sha2.h:56
static void int_sha384_reset(PX_MD *h)
#define SHA512_Update
Definition: sha2.h:49
void SHA224_Final(uint8 digest[], SHA224_CTX *context)
Definition: sha2.c:969
#define SHA256_Final
Definition: sha2.h:44
static void int_sha256_reset(PX_MD *h)
void(* update)(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: px.h:119
#define SHA512_Final
Definition: sha2.h:50
void(* free)(PX_MD *h)
Definition: px.h:121
static void int_sha512_reset(PX_MD *h)
static unsigned int_sha384_block_len(PX_MD *h)
static void int_sha384_finish(PX_MD *h, uint8 *dst)
Definition: px.h:114
static void int_sha224_reset(PX_MD *h)
Definition: internal-sha2.c:67
static void int_sha384_free(PX_MD *h)
#define SHA384_Update
Definition: sha2.h:46
static unsigned int_sha256_len(PX_MD *h)
Definition: internal-sha2.c:95
#define SHA224_BLOCK_LENGTH
Definition: sha2.h:53
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
#define SHA384_DIGEST_LENGTH
Definition: sha2.h:60
static unsigned int_sha512_block_len(PX_MD *h)
void(* finish)(PX_MD *h, uint8 *dst)
Definition: px.h:120
void SHA224_Init(SHA224_CTX *context)
Definition: sha2.c:953
#define SHA256_Init
Definition: sha2.h:42
static void int_sha224_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal-sha2.c:59
#define SHA384_Init
Definition: sha2.h:45
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 init_sha224(PX_MD *h)
#define SHA256_DIGEST_LENGTH
Definition: sha2.h:57
static unsigned int_sha384_len(PX_MD *h)
static unsigned int_sha256_block_len(PX_MD *h)
#define SHA512_Init
Definition: sha2.h:48
static unsigned int_sha224_len(PX_MD *h)
Definition: internal-sha2.c:47
void init_sha384(PX_MD *h)
void init_sha512(PX_MD *h)
void SHA224_Update(SHA224_CTX *context, const uint8 *data, size_t len)
Definition: sha2.c:963
#define px_alloc(s)
Definition: px.h:45
#define SHA256_Update
Definition: sha2.h:43
void(* reset)(PX_MD *h)
Definition: px.h:118
static void int_sha224_free(PX_MD *h)
Definition: internal-sha2.c:83
#define SHA512_DIGEST_LENGTH
Definition: sha2.h:63
#define SHA512_BLOCK_LENGTH
Definition: sha2.h:62
unsigned(* block_size)(PX_MD *h)
Definition: px.h:117
static void int_sha512_finish(PX_MD *h, uint8 *dst)
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
#define SHA224_DIGEST_LENGTH
Definition: sha2.h:54
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)