mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
This patch adds implementation of SHA2 to pgcrypto.
New hashes: SHA256, SHA384, SHA512. Marko Kreen
This commit is contained in:
@ -1,19 +1,20 @@
|
|||||||
#
|
#
|
||||||
# $PostgreSQL: pgsql/contrib/pgcrypto/Makefile,v 1.17 2005/07/06 16:14:42 tgl Exp $
|
# $PostgreSQL: pgsql/contrib/pgcrypto/Makefile,v 1.18 2005/07/10 03:52:56 momjian Exp $
|
||||||
#
|
#
|
||||||
|
|
||||||
# if you don't have OpenSSL, you can use libc random() or /dev/urandom
|
# if you don't have OpenSSL, you can use libc random() or /dev/urandom
|
||||||
INT_CFLAGS = -DRAND_SILLY
|
INT_CFLAGS = -DRAND_SILLY
|
||||||
#INT_CFLAGS = -DRAND_DEV=\"/dev/urandom\"
|
#INT_CFLAGS = -DRAND_DEV=\"/dev/urandom\"
|
||||||
|
|
||||||
INT_SRCS = md5.c sha1.c internal.c blf.c rijndael.c
|
INT_SRCS = md5.c sha1.c sha2.c internal.c blf.c rijndael.c
|
||||||
|
INT_TESTS = sha2
|
||||||
|
|
||||||
OSSL_CFLAGS = -DRAND_OPENSSL
|
OSSL_CFLAGS = -DRAND_OPENSSL
|
||||||
OSSL_SRCS = openssl.c
|
OSSL_SRCS = openssl.c
|
||||||
OSSL_TESTS = des 3des cast5
|
OSSL_TESTS = des 3des cast5
|
||||||
|
|
||||||
CF_SRCS = $(if $(subst no,,$(with_openssl)), $(OSSL_SRCS), $(INT_SRCS))
|
CF_SRCS = $(if $(subst no,,$(with_openssl)), $(OSSL_SRCS), $(INT_SRCS))
|
||||||
CF_TESTS = $(if $(subst no,,$(with_openssl)), $(OSSL_TESTS))
|
CF_TESTS = $(if $(subst no,,$(with_openssl)), $(OSSL_TESTS), $(INT_TESTS))
|
||||||
CF_CFLAGS = $(if $(subst no,,$(with_openssl)), $(OSSL_CFLAGS), $(INT_CFLAGS))
|
CF_CFLAGS = $(if $(subst no,,$(with_openssl)), $(OSSL_CFLAGS), $(INT_CFLAGS))
|
||||||
|
|
||||||
PG_CPPFLAGS = $(CF_CFLAGS)
|
PG_CPPFLAGS = $(CF_CFLAGS)
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.16 2005/03/21 05:19:55 neilc Exp $
|
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.17 2005/07/10 03:52:56 momjian Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@ -36,6 +36,7 @@
|
|||||||
|
|
||||||
#include "md5.h"
|
#include "md5.h"
|
||||||
#include "sha1.h"
|
#include "sha1.h"
|
||||||
|
#include "sha2.h"
|
||||||
#include "blf.h"
|
#include "blf.h"
|
||||||
#include "rijndael.h"
|
#include "rijndael.h"
|
||||||
|
|
||||||
@ -56,6 +57,9 @@
|
|||||||
|
|
||||||
static void init_md5(PX_MD * h);
|
static void init_md5(PX_MD * h);
|
||||||
static void init_sha1(PX_MD * h);
|
static void init_sha1(PX_MD * h);
|
||||||
|
static void init_sha256(PX_MD * h);
|
||||||
|
static void init_sha384(PX_MD * h);
|
||||||
|
static void init_sha512(PX_MD * h);
|
||||||
|
|
||||||
struct int_digest
|
struct int_digest
|
||||||
{
|
{
|
||||||
@ -67,6 +71,9 @@ static const struct int_digest
|
|||||||
int_digest_list[] = {
|
int_digest_list[] = {
|
||||||
{ "md5", init_md5 },
|
{ "md5", init_md5 },
|
||||||
{ "sha1", init_sha1 },
|
{ "sha1", init_sha1 },
|
||||||
|
{ "sha256", init_sha256 },
|
||||||
|
{ "sha384", init_sha384 },
|
||||||
|
{ "sha512", init_sha512 },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -164,6 +171,146 @@ int_sha1_free(PX_MD * h)
|
|||||||
px_free(h);
|
px_free(h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* SHA256 */
|
||||||
|
|
||||||
|
static unsigned
|
||||||
|
int_sha256_len(PX_MD * h)
|
||||||
|
{
|
||||||
|
return SHA256_DIGEST_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned
|
||||||
|
int_sha256_block_len(PX_MD * h)
|
||||||
|
{
|
||||||
|
return SHA256_BLOCK_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha256_update(PX_MD * h, const uint8 *data, unsigned dlen)
|
||||||
|
{
|
||||||
|
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
SHA256_Update(ctx, data, dlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha256_reset(PX_MD * h)
|
||||||
|
{
|
||||||
|
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
SHA256_Init(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha256_finish(PX_MD * h, uint8 *dst)
|
||||||
|
{
|
||||||
|
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
SHA256_Final(dst, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha256_free(PX_MD * h)
|
||||||
|
{
|
||||||
|
SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
px_free(ctx);
|
||||||
|
px_free(h);
|
||||||
|
}
|
||||||
|
/* SHA384 */
|
||||||
|
|
||||||
|
static unsigned
|
||||||
|
int_sha384_len(PX_MD * h)
|
||||||
|
{
|
||||||
|
return SHA384_DIGEST_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned
|
||||||
|
int_sha384_block_len(PX_MD * h)
|
||||||
|
{
|
||||||
|
return SHA384_BLOCK_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha384_update(PX_MD * h, const uint8 *data, unsigned dlen)
|
||||||
|
{
|
||||||
|
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
SHA384_Update(ctx, data, dlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha384_reset(PX_MD * h)
|
||||||
|
{
|
||||||
|
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
SHA384_Init(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha384_finish(PX_MD * h, uint8 *dst)
|
||||||
|
{
|
||||||
|
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
SHA384_Final(dst, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha384_free(PX_MD * h)
|
||||||
|
{
|
||||||
|
SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
px_free(ctx);
|
||||||
|
px_free(h);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* SHA512 */
|
||||||
|
|
||||||
|
static unsigned
|
||||||
|
int_sha512_len(PX_MD * h)
|
||||||
|
{
|
||||||
|
return SHA512_DIGEST_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned
|
||||||
|
int_sha512_block_len(PX_MD * h)
|
||||||
|
{
|
||||||
|
return SHA512_BLOCK_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha512_update(PX_MD * h, const uint8 *data, unsigned dlen)
|
||||||
|
{
|
||||||
|
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
SHA512_Update(ctx, data, dlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha512_reset(PX_MD * h)
|
||||||
|
{
|
||||||
|
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
SHA512_Init(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha512_finish(PX_MD * h, uint8 *dst)
|
||||||
|
{
|
||||||
|
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
SHA512_Final(dst, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
int_sha512_free(PX_MD * h)
|
||||||
|
{
|
||||||
|
SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
|
||||||
|
|
||||||
|
px_free(ctx);
|
||||||
|
px_free(h);
|
||||||
|
}
|
||||||
|
|
||||||
/* init functions */
|
/* init functions */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -204,6 +351,63 @@ init_sha1(PX_MD * md)
|
|||||||
md->reset(md);
|
md->reset(md);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
init_sha256(PX_MD * md)
|
||||||
|
{
|
||||||
|
SHA256_CTX *ctx;
|
||||||
|
|
||||||
|
ctx = px_alloc(sizeof(*ctx));
|
||||||
|
|
||||||
|
md->p.ptr = ctx;
|
||||||
|
|
||||||
|
md->result_size = int_sha256_len;
|
||||||
|
md->block_size = int_sha256_block_len;
|
||||||
|
md->reset = int_sha256_reset;
|
||||||
|
md->update = int_sha256_update;
|
||||||
|
md->finish = int_sha256_finish;
|
||||||
|
md->free = int_sha256_free;
|
||||||
|
|
||||||
|
md->reset(md);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
init_sha384(PX_MD * md)
|
||||||
|
{
|
||||||
|
SHA384_CTX *ctx;
|
||||||
|
|
||||||
|
ctx = px_alloc(sizeof(*ctx));
|
||||||
|
|
||||||
|
md->p.ptr = ctx;
|
||||||
|
|
||||||
|
md->result_size = int_sha384_len;
|
||||||
|
md->block_size = int_sha384_block_len;
|
||||||
|
md->reset = int_sha384_reset;
|
||||||
|
md->update = int_sha384_update;
|
||||||
|
md->finish = int_sha384_finish;
|
||||||
|
md->free = int_sha384_free;
|
||||||
|
|
||||||
|
md->reset(md);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
init_sha512(PX_MD * md)
|
||||||
|
{
|
||||||
|
SHA512_CTX *ctx;
|
||||||
|
|
||||||
|
ctx = px_alloc(sizeof(*ctx));
|
||||||
|
|
||||||
|
md->p.ptr = ctx;
|
||||||
|
|
||||||
|
md->result_size = int_sha512_len;
|
||||||
|
md->block_size = int_sha512_block_len;
|
||||||
|
md->reset = int_sha512_reset;
|
||||||
|
md->update = int_sha512_update;
|
||||||
|
md->finish = int_sha512_finish;
|
||||||
|
md->free = int_sha512_free;
|
||||||
|
|
||||||
|
md->reset(md);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ciphers generally
|
* ciphers generally
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user