1
0
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:
Bruce Momjian
2005-07-10 03:52:56 +00:00
parent 73a7c322c3
commit 248eeb82f0
2 changed files with 209 additions and 4 deletions

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* 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 "sha1.h"
#include "sha2.h"
#include "blf.h"
#include "rijndael.h"
@ -56,6 +57,9 @@
static void init_md5(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
{
@ -67,6 +71,9 @@ static const struct int_digest
int_digest_list[] = {
{ "md5", init_md5 },
{ "sha1", init_sha1 },
{ "sha256", init_sha256 },
{ "sha384", init_sha384 },
{ "sha512", init_sha512 },
{ NULL, NULL }
};
@ -164,6 +171,146 @@ int_sha1_free(PX_MD * 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 */
static void
@ -204,6 +351,63 @@ init_sha1(PX_MD * 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
*/