mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-21 17:33:24 +02:00
Two new routines to allocate a hash context and to free it are created,
as these become necessary for the goal behind this refactoring: switch
the all cryptohash implementations for OpenSSL to use EVP (for FIPS and
also because upstream does not recommend the use of low-level cryptohash
functions for 20 years). Note that OpenSSL hides the internals of
cryptohash contexts since 1.1.0, so it is necessary to leave the
allocation to OpenSSL itself, explaining the need for those two new
routines. This part is going to require more work to properly track
hash contexts with resource owners, but this not introduced here.
Still, this refactoring makes the move possible.
This reduces the number of routines for all SHA2 implementations from
twelve (SHA{224,256,386,512} with init, update and final calls) to five
(create, free, init, update and final calls) by incorporating the hash
type directly into the hash context data.
The new cryptohash routines are moved to a new file, called cryptohash.c
for the fallback implementations, with SHA2 specifics becoming a part
internal to src/common/. OpenSSL specifics are part of
cryptohash_openssl.c. This infrastructure is usable for more hash
types, like MD5 or HMAC.
Any code paths using the internal SHA2 routines are adapted to report
correctly errors, which are most of the changes of this commit. The
zones mostly impacted are checksum manifests, libpq and SCRAM.
Note that e21cbb4
was a first attempt to switch SHA2 to EVP, but it
lacked the refactoring needed for libpq, as done here.
This patch has been tested on Linux and Windows, with and without
OpenSSL, and down to 1.0.1, the oldest version supported on HEAD.
Author: Michael Paquier
Reviewed-by: Daniel Gustafsson
Discussion: https://postgr.es/m/20200924025314.GE7405@paquier.xyz
207 lines
4.4 KiB
C
207 lines
4.4 KiB
C
/*
|
|
* internal.c
|
|
* Wrapper for builtin functions
|
|
*
|
|
* Copyright (c) 2001 Marko Kreen
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*
|
|
* contrib/pgcrypto/internal-sha2.c
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
#include <time.h>
|
|
|
|
#include "common/cryptohash.h"
|
|
#include "common/sha2.h"
|
|
#include "px.h"
|
|
|
|
void init_sha224(PX_MD *h);
|
|
void init_sha256(PX_MD *h);
|
|
void init_sha384(PX_MD *h);
|
|
void init_sha512(PX_MD *h);
|
|
|
|
/* SHA224 */
|
|
static unsigned
|
|
int_sha224_len(PX_MD *h)
|
|
{
|
|
return PG_SHA224_DIGEST_LENGTH;
|
|
}
|
|
|
|
static unsigned
|
|
int_sha224_block_len(PX_MD *h)
|
|
{
|
|
return PG_SHA224_BLOCK_LENGTH;
|
|
}
|
|
|
|
/* SHA256 */
|
|
static unsigned
|
|
int_sha256_len(PX_MD *h)
|
|
{
|
|
return PG_SHA256_DIGEST_LENGTH;
|
|
}
|
|
|
|
static unsigned
|
|
int_sha256_block_len(PX_MD *h)
|
|
{
|
|
return PG_SHA256_BLOCK_LENGTH;
|
|
}
|
|
|
|
/* SHA384 */
|
|
static unsigned
|
|
int_sha384_len(PX_MD *h)
|
|
{
|
|
return PG_SHA384_DIGEST_LENGTH;
|
|
}
|
|
|
|
static unsigned
|
|
int_sha384_block_len(PX_MD *h)
|
|
{
|
|
return PG_SHA384_BLOCK_LENGTH;
|
|
}
|
|
|
|
/* SHA512 */
|
|
static unsigned
|
|
int_sha512_len(PX_MD *h)
|
|
{
|
|
return PG_SHA512_DIGEST_LENGTH;
|
|
}
|
|
|
|
static unsigned
|
|
int_sha512_block_len(PX_MD *h)
|
|
{
|
|
return PG_SHA512_BLOCK_LENGTH;
|
|
}
|
|
|
|
/* Generic interface for all SHA2 methods */
|
|
static void
|
|
int_sha2_update(PX_MD *h, const uint8 *data, unsigned dlen)
|
|
{
|
|
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
|
|
|
|
if (pg_cryptohash_update(ctx, data, dlen) < 0)
|
|
elog(ERROR, "could not update %s context", "SHA2");
|
|
}
|
|
|
|
static void
|
|
int_sha2_reset(PX_MD *h)
|
|
{
|
|
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
|
|
|
|
if (pg_cryptohash_init(ctx) < 0)
|
|
elog(ERROR, "could not initialize %s context", "SHA2");
|
|
}
|
|
|
|
static void
|
|
int_sha2_finish(PX_MD *h, uint8 *dst)
|
|
{
|
|
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
|
|
|
|
if (pg_cryptohash_final(ctx, dst) < 0)
|
|
elog(ERROR, "could not finalize %s context", "SHA2");
|
|
}
|
|
|
|
static void
|
|
int_sha2_free(PX_MD *h)
|
|
{
|
|
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
|
|
|
|
pg_cryptohash_free(ctx);
|
|
pfree(h);
|
|
}
|
|
|
|
/* init functions */
|
|
|
|
void
|
|
init_sha224(PX_MD *md)
|
|
{
|
|
pg_cryptohash_ctx *ctx;
|
|
|
|
ctx = pg_cryptohash_create(PG_SHA224);
|
|
md->p.ptr = ctx;
|
|
|
|
md->result_size = int_sha224_len;
|
|
md->block_size = int_sha224_block_len;
|
|
md->reset = int_sha2_reset;
|
|
md->update = int_sha2_update;
|
|
md->finish = int_sha2_finish;
|
|
md->free = int_sha2_free;
|
|
|
|
md->reset(md);
|
|
}
|
|
|
|
void
|
|
init_sha256(PX_MD *md)
|
|
{
|
|
pg_cryptohash_ctx *ctx;
|
|
|
|
ctx = pg_cryptohash_create(PG_SHA256);
|
|
md->p.ptr = ctx;
|
|
|
|
md->result_size = int_sha256_len;
|
|
md->block_size = int_sha256_block_len;
|
|
md->reset = int_sha2_reset;
|
|
md->update = int_sha2_update;
|
|
md->finish = int_sha2_finish;
|
|
md->free = int_sha2_free;
|
|
|
|
md->reset(md);
|
|
}
|
|
|
|
void
|
|
init_sha384(PX_MD *md)
|
|
{
|
|
pg_cryptohash_ctx *ctx;
|
|
|
|
ctx = pg_cryptohash_create(PG_SHA384);
|
|
md->p.ptr = ctx;
|
|
|
|
md->result_size = int_sha384_len;
|
|
md->block_size = int_sha384_block_len;
|
|
md->reset = int_sha2_reset;
|
|
md->update = int_sha2_update;
|
|
md->finish = int_sha2_finish;
|
|
md->free = int_sha2_free;
|
|
|
|
md->reset(md);
|
|
}
|
|
|
|
void
|
|
init_sha512(PX_MD *md)
|
|
{
|
|
pg_cryptohash_ctx *ctx;
|
|
|
|
ctx = pg_cryptohash_create(PG_SHA512);
|
|
md->p.ptr = ctx;
|
|
|
|
md->result_size = int_sha512_len;
|
|
md->block_size = int_sha512_block_len;
|
|
md->reset = int_sha2_reset;
|
|
md->update = int_sha2_update;
|
|
md->finish = int_sha2_finish;
|
|
md->free = int_sha2_free;
|
|
|
|
md->reset(md);
|
|
}
|