~ubuntu-branches/ubuntu/hardy/postgresql-8.4/hardy-backports

« back to all changes in this revision

Viewing changes to contrib/pgcrypto/internal-sha2.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * $PostgreSQL$
 
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
 
47
int_sha224_len(PX_MD * h)
 
48
{
 
49
        return SHA224_DIGEST_LENGTH;
 
50
}
 
51
 
 
52
static unsigned
 
53
int_sha224_block_len(PX_MD * h)
 
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
 
67
int_sha224_reset(PX_MD * h)
 
68
{
 
69
        SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
 
70
 
 
71
        SHA224_Init(ctx);
 
72
}
 
73
 
 
74
static void
 
75
int_sha224_finish(PX_MD * h, uint8 *dst)
 
76
{
 
77
        SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
 
78
 
 
79
        SHA224_Final(dst, ctx);
 
80
}
 
81
 
 
82
static void
 
83
int_sha224_free(PX_MD * h)
 
84
{
 
85
        SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
 
86
 
 
87
        memset(ctx, 0, sizeof(*ctx));
 
88
        px_free(ctx);
 
89
        px_free(h);
 
90
}
 
91
 
 
92
/* SHA256 */
 
93
 
 
94
static unsigned
 
95
int_sha256_len(PX_MD * h)
 
96
{
 
97
        return SHA256_DIGEST_LENGTH;
 
98
}
 
99
 
 
100
static unsigned
 
101
int_sha256_block_len(PX_MD * h)
 
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
 
115
int_sha256_reset(PX_MD * h)
 
116
{
 
117
        SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
 
118
 
 
119
        SHA256_Init(ctx);
 
120
}
 
121
 
 
122
static void
 
123
int_sha256_finish(PX_MD * h, uint8 *dst)
 
124
{
 
125
        SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
 
126
 
 
127
        SHA256_Final(dst, ctx);
 
128
}
 
129
 
 
130
static void
 
131
int_sha256_free(PX_MD * h)
 
132
{
 
133
        SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
 
134
 
 
135
        memset(ctx, 0, sizeof(*ctx));
 
136
        px_free(ctx);
 
137
        px_free(h);
 
138
}
 
139
 
 
140
/* SHA384 */
 
141
 
 
142
static unsigned
 
143
int_sha384_len(PX_MD * h)
 
144
{
 
145
        return SHA384_DIGEST_LENGTH;
 
146
}
 
147
 
 
148
static unsigned
 
149
int_sha384_block_len(PX_MD * h)
 
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
 
163
int_sha384_reset(PX_MD * h)
 
164
{
 
165
        SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
 
166
 
 
167
        SHA384_Init(ctx);
 
168
}
 
169
 
 
170
static void
 
171
int_sha384_finish(PX_MD * h, uint8 *dst)
 
172
{
 
173
        SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
 
174
 
 
175
        SHA384_Final(dst, ctx);
 
176
}
 
177
 
 
178
static void
 
179
int_sha384_free(PX_MD * h)
 
180
{
 
181
        SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
 
182
 
 
183
        memset(ctx, 0, sizeof(*ctx));
 
184
        px_free(ctx);
 
185
        px_free(h);
 
186
}
 
187
 
 
188
/* SHA512 */
 
189
 
 
190
static unsigned
 
191
int_sha512_len(PX_MD * h)
 
192
{
 
193
        return SHA512_DIGEST_LENGTH;
 
194
}
 
195
 
 
196
static unsigned
 
197
int_sha512_block_len(PX_MD * h)
 
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
 
211
int_sha512_reset(PX_MD * h)
 
212
{
 
213
        SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
 
214
 
 
215
        SHA512_Init(ctx);
 
216
}
 
217
 
 
218
static void
 
219
int_sha512_finish(PX_MD * h, uint8 *dst)
 
220
{
 
221
        SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
 
222
 
 
223
        SHA512_Final(dst, ctx);
 
224
}
 
225
 
 
226
static void
 
227
int_sha512_free(PX_MD * h)
 
228
{
 
229
        SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
 
230
 
 
231
        memset(ctx, 0, sizeof(*ctx));
 
232
        px_free(ctx);
 
233
        px_free(h);
 
234
}
 
235
 
 
236
/* init functions */
 
237
 
 
238
void
 
239
init_sha224(PX_MD * md)
 
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
 
 
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;
 
254
 
 
255
        md->reset(md);
 
256
}
 
257
 
 
258
void
 
259
init_sha256(PX_MD * md)
 
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
 
 
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;
 
274
 
 
275
        md->reset(md);
 
276
}
 
277
 
 
278
void
 
279
init_sha384(PX_MD * md)
 
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
 
 
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;
 
294
 
 
295
        md->reset(md);
 
296
}
 
297
 
 
298
void
 
299
init_sha512(PX_MD * md)
 
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
 
 
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;
 
314
 
 
315
        md->reset(md);
 
316
}