Skip to content

Commit 248eeb8

Browse files
committed
This patch adds implementation of SHA2 to pgcrypto.
New hashes: SHA256, SHA384, SHA512. Marko Kreen
1 parent 73a7c32 commit 248eeb8

File tree

2 files changed

+209
-4
lines changed

2 files changed

+209
-4
lines changed

contrib/pgcrypto/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#
2-
# $PostgreSQL: pgsql/contrib/pgcrypto/Makefile,v 1.17 2005/07/06 16:14:42 tgl Exp $
2+
# $PostgreSQL: pgsql/contrib/pgcrypto/Makefile,v 1.18 2005/07/10 03:52:56 momjian Exp $
33
#
44

55
# if you don't have OpenSSL, you can use libc random() or /dev/urandom
66
INT_CFLAGS = -DRAND_SILLY
77
#INT_CFLAGS = -DRAND_DEV=\"/dev/urandom\"
88

9-
INT_SRCS = md5.c sha1.c internal.c blf.c rijndael.c
9+
INT_SRCS = md5.c sha1.c sha2.c internal.c blf.c rijndael.c
10+
INT_TESTS = sha2
1011

1112
OSSL_CFLAGS = -DRAND_OPENSSL
1213
OSSL_SRCS = openssl.c
1314
OSSL_TESTS = des 3des cast5
1415

1516
CF_SRCS = $(if $(subst no,,$(with_openssl)), $(OSSL_SRCS), $(INT_SRCS))
16-
CF_TESTS = $(if $(subst no,,$(with_openssl)), $(OSSL_TESTS))
17+
CF_TESTS = $(if $(subst no,,$(with_openssl)), $(OSSL_TESTS), $(INT_TESTS))
1718
CF_CFLAGS = $(if $(subst no,,$(with_openssl)), $(OSSL_CFLAGS), $(INT_CFLAGS))
1819

1920
PG_CPPFLAGS = $(CF_CFLAGS)

contrib/pgcrypto/internal.c

Lines changed: 205 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.16 2005/03/21 05:19:55 neilc Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.17 2005/07/10 03:52:56 momjian Exp $
3030
*/
3131

3232

@@ -36,6 +36,7 @@
3636

3737
#include "md5.h"
3838
#include "sha1.h"
39+
#include "sha2.h"
3940
#include "blf.h"
4041
#include "rijndael.h"
4142

@@ -56,6 +57,9 @@
5657

5758
static void init_md5(PX_MD * h);
5859
static void init_sha1(PX_MD * h);
60+
static void init_sha256(PX_MD * h);
61+
static void init_sha384(PX_MD * h);
62+
static void init_sha512(PX_MD * h);
5963

6064
struct int_digest
6165
{
@@ -67,6 +71,9 @@ static const struct int_digest
6771
int_digest_list[] = {
6872
{ "md5", init_md5 },
6973
{ "sha1", init_sha1 },
74+
{ "sha256", init_sha256 },
75+
{ "sha384", init_sha384 },
76+
{ "sha512", init_sha512 },
7077
{ NULL, NULL }
7178
};
7279

@@ -164,6 +171,146 @@ int_sha1_free(PX_MD * h)
164171
px_free(h);
165172
}
166173

174+
/* SHA256 */
175+
176+
static unsigned
177+
int_sha256_len(PX_MD * h)
178+
{
179+
return SHA256_DIGEST_LENGTH;
180+
}
181+
182+
static unsigned
183+
int_sha256_block_len(PX_MD * h)
184+
{
185+
return SHA256_BLOCK_LENGTH;
186+
}
187+
188+
static void
189+
int_sha256_update(PX_MD * h, const uint8 *data, unsigned dlen)
190+
{
191+
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
192+
193+
SHA256_Update(ctx, data, dlen);
194+
}
195+
196+
static void
197+
int_sha256_reset(PX_MD * h)
198+
{
199+
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
200+
201+
SHA256_Init(ctx);
202+
}
203+
204+
static void
205+
int_sha256_finish(PX_MD * h, uint8 *dst)
206+
{
207+
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
208+
209+
SHA256_Final(dst, ctx);
210+
}
211+
212+
static void
213+
int_sha256_free(PX_MD * h)
214+
{
215+
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
216+
217+
px_free(ctx);
218+
px_free(h);
219+
}
220+
/* SHA384 */
221+
222+
static unsigned
223+
int_sha384_len(PX_MD * h)
224+
{
225+
return SHA384_DIGEST_LENGTH;
226+
}
227+
228+
static unsigned
229+
int_sha384_block_len(PX_MD * h)
230+
{
231+
return SHA384_BLOCK_LENGTH;
232+
}
233+
234+
static void
235+
int_sha384_update(PX_MD * h, const uint8 *data, unsigned dlen)
236+
{
237+
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
238+
239+
SHA384_Update(ctx, data, dlen);
240+
}
241+
242+
static void
243+
int_sha384_reset(PX_MD * h)
244+
{
245+
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
246+
247+
SHA384_Init(ctx);
248+
}
249+
250+
static void
251+
int_sha384_finish(PX_MD * h, uint8 *dst)
252+
{
253+
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
254+
255+
SHA384_Final(dst, ctx);
256+
}
257+
258+
static void
259+
int_sha384_free(PX_MD * h)
260+
{
261+
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
262+
263+
px_free(ctx);
264+
px_free(h);
265+
}
266+
267+
/* SHA512 */
268+
269+
static unsigned
270+
int_sha512_len(PX_MD * h)
271+
{
272+
return SHA512_DIGEST_LENGTH;
273+
}
274+
275+
static unsigned
276+
int_sha512_block_len(PX_MD * h)
277+
{
278+
return SHA512_BLOCK_LENGTH;
279+
}
280+
281+
static void
282+
int_sha512_update(PX_MD * h, const uint8 *data, unsigned dlen)
283+
{
284+
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
285+
286+
SHA512_Update(ctx, data, dlen);
287+
}
288+
289+
static void
290+
int_sha512_reset(PX_MD * h)
291+
{
292+
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
293+
294+
SHA512_Init(ctx);
295+
}
296+
297+
static void
298+
int_sha512_finish(PX_MD * h, uint8 *dst)
299+
{
300+
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
301+
302+
SHA512_Final(dst, ctx);
303+
}
304+
305+
static void
306+
int_sha512_free(PX_MD * h)
307+
{
308+
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
309+
310+
px_free(ctx);
311+
px_free(h);
312+
}
313+
167314
/* init functions */
168315

169316
static void
@@ -204,6 +351,63 @@ init_sha1(PX_MD * md)
204351
md->reset(md);
205352
}
206353

354+
static void
355+
init_sha256(PX_MD * md)
356+
{
357+
SHA256_CTX *ctx;
358+
359+
ctx = px_alloc(sizeof(*ctx));
360+
361+
md->p.ptr = ctx;
362+
363+
md->result_size = int_sha256_len;
364+
md->block_size = int_sha256_block_len;
365+
md->reset = int_sha256_reset;
366+
md->update = int_sha256_update;
367+
md->finish = int_sha256_finish;
368+
md->free = int_sha256_free;
369+
370+
md->reset(md);
371+
}
372+
373+
static void
374+
init_sha384(PX_MD * md)
375+
{
376+
SHA384_CTX *ctx;
377+
378+
ctx = px_alloc(sizeof(*ctx));
379+
380+
md->p.ptr = ctx;
381+
382+
md->result_size = int_sha384_len;
383+
md->block_size = int_sha384_block_len;
384+
md->reset = int_sha384_reset;
385+
md->update = int_sha384_update;
386+
md->finish = int_sha384_finish;
387+
md->free = int_sha384_free;
388+
389+
md->reset(md);
390+
}
391+
392+
static void
393+
init_sha512(PX_MD * md)
394+
{
395+
SHA512_CTX *ctx;
396+
397+
ctx = px_alloc(sizeof(*ctx));
398+
399+
md->p.ptr = ctx;
400+
401+
md->result_size = int_sha512_len;
402+
md->block_size = int_sha512_block_len;
403+
md->reset = int_sha512_reset;
404+
md->update = int_sha512_update;
405+
md->finish = int_sha512_finish;
406+
md->free = int_sha512_free;
407+
408+
md->reset(md);
409+
}
410+
207411
/*
208412
* ciphers generally
209413
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy