Refactor HMAC implementations

Similarly to the cryptohash implementations, this refactors the existing
HMAC code into a single set of APIs that can be plugged with any crypto
libraries PostgreSQL is built with (only OpenSSL currently).  If there
is no such libraries, a fallback implementation is available.  Those new
APIs are designed similarly to the existing cryptohash layer, so there
is no real new design here, with the same logic around buffer bound
checks and memory handling.

HMAC has a dependency on cryptohashes, so all the cryptohash types
supported by cryptohash{_openssl}.c can be used with HMAC.  This
refactoring is an advantage mainly for SCRAM, that included its own
implementation of HMAC with SHA256 without relying on the existing
crypto libraries even if PostgreSQL was built with their support.

This code has been tested on Windows and Linux, with and without
OpenSSL, across all the versions supported on HEAD from 1.1.1 down to
1.0.1.  I have also checked that the implementations are working fine
using some sample results, a custom extension of my own, and doing
cross-checks across different major versions with SCRAM with the client
and the backend.

Author: Michael Paquier
Reviewed-by: Bruce Momjian
Discussion: https://postgr.es/m/X9m0nkEJEzIPXjeZ@paquier.xyz
This commit is contained in:
Michael Paquier
2021-04-03 17:30:49 +09:00
parent 1d9c5d0ce2
commit e6bdfd9700
18 changed files with 747 additions and 198 deletions

29
src/include/common/hmac.h Normal file
View File

@ -0,0 +1,29 @@
/*-------------------------------------------------------------------------
*
* hmac.h
* Generic headers for HMAC
*
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* src/include/common/hmac.h
*
*-------------------------------------------------------------------------
*/
#ifndef PG_HMAC_H
#define PG_HMAC_H
#include "common/cryptohash.h"
/* opaque context, private to each HMAC implementation */
typedef struct pg_hmac_ctx pg_hmac_ctx;
extern pg_hmac_ctx *pg_hmac_create(pg_cryptohash_type type);
extern int pg_hmac_init(pg_hmac_ctx *ctx, const uint8 *key, size_t len);
extern int pg_hmac_update(pg_hmac_ctx *ctx, const uint8 *data, size_t len);
extern int pg_hmac_final(pg_hmac_ctx *ctx, uint8 *dest, size_t len);
extern void pg_hmac_free(pg_hmac_ctx *ctx);
#endif /* PG_HMAC_H */

View File

@ -18,6 +18,8 @@
/* Size of result generated by MD5 computation */
#define MD5_DIGEST_LENGTH 16
/* Block size for MD5 */
#define MD5_BLOCK_SIZE 64
/* password-related data */
#define MD5_PASSWD_CHARSET "0123456789abcdef"

View File

@ -46,19 +46,6 @@
*/
#define SCRAM_DEFAULT_ITERATIONS 4096
/*
* Context data for HMAC used in SCRAM authentication.
*/
typedef struct
{
pg_cryptohash_ctx *sha256ctx;
uint8 k_opad[SHA256_HMAC_B];
} scram_HMAC_ctx;
extern int scram_HMAC_init(scram_HMAC_ctx *ctx, const uint8 *key, int keylen);
extern int scram_HMAC_update(scram_HMAC_ctx *ctx, const char *str, int slen);
extern int scram_HMAC_final(uint8 *result, scram_HMAC_ctx *ctx);
extern int scram_SaltedPassword(const char *password, const char *salt,
int saltlen, int iterations, uint8 *result);
extern int scram_H(const uint8 *str, int len, uint8 *result);

View File

@ -15,5 +15,7 @@
/* Size of result generated by SHA1 computation */
#define SHA1_DIGEST_LENGTH 20
/* Block size for SHA1 */
#define SHA1_BLOCK_SIZE 64
#endif /* PG_SHA1_H */

View File

@ -268,6 +268,12 @@
/* Define to 1 if you have the `history_truncate_file' function. */
#undef HAVE_HISTORY_TRUNCATE_FILE
/* Define to 1 if you have the `HMAC_CTX_free' function. */
#undef HAVE_HMAC_CTX_FREE
/* Define to 1 if you have the `HMAC_CTX_new' function. */
#undef HAVE_HMAC_CTX_NEW
/* Define to 1 if you have the <ifaddrs.h> header file. */
#undef HAVE_IFADDRS_H

View File

@ -102,4 +102,11 @@ extern void ResourceOwnerRememberCryptoHash(ResourceOwner owner,
extern void ResourceOwnerForgetCryptoHash(ResourceOwner owner,
Datum handle);
/* support for HMAC context management */
extern void ResourceOwnerEnlargeHMAC(ResourceOwner owner);
extern void ResourceOwnerRememberHMAC(ResourceOwner owner,
Datum handle);
extern void ResourceOwnerForgetHMAC(ResourceOwner owner,
Datum handle);
#endif /* RESOWNER_PRIVATE_H */