diff options
author | Michael Paquier <michael@paquier.xyz> | 2021-04-03 17:30:49 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2021-04-03 17:30:49 +0900 |
commit | e6bdfd9700ebfc7df811c97c2fc46d7e94e329a2 (patch) | |
tree | 2fb0dad9a0ba47b6ba0a6da9780b77a65dc7dffa /src/include | |
parent | 1d9c5d0ce2dcac05850401cf266a9df10a68de49 (diff) | |
download | postgresql-e6bdfd9700ebfc7df811c97c2fc46d7e94e329a2.tar.gz postgresql-e6bdfd9700ebfc7df811c97c2fc46d7e94e329a2.zip |
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
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/common/hmac.h | 29 | ||||
-rw-r--r-- | src/include/common/md5.h | 2 | ||||
-rw-r--r-- | src/include/common/scram-common.h | 13 | ||||
-rw-r--r-- | src/include/common/sha1.h | 2 | ||||
-rw-r--r-- | src/include/pg_config.h.in | 6 | ||||
-rw-r--r-- | src/include/utils/resowner_private.h | 7 |
6 files changed, 46 insertions, 13 deletions
diff --git a/src/include/common/hmac.h b/src/include/common/hmac.h new file mode 100644 index 00000000000..ea0343a9da7 --- /dev/null +++ b/src/include/common/hmac.h @@ -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 */ diff --git a/src/include/common/md5.h b/src/include/common/md5.h index 6d100f5cfc2..62a31e6ed4e 100644 --- a/src/include/common/md5.h +++ b/src/include/common/md5.h @@ -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" diff --git a/src/include/common/scram-common.h b/src/include/common/scram-common.h index 9d684b41e8e..5777ce9fe33 100644 --- a/src/include/common/scram-common.h +++ b/src/include/common/scram-common.h @@ -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); diff --git a/src/include/common/sha1.h b/src/include/common/sha1.h index a61bc47deda..b1ee36f8eaf 100644 --- a/src/include/common/sha1.h +++ b/src/include/common/sha1.h @@ -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 */ diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index 5e2255a2f53..783b8fc1ba7 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -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 diff --git a/src/include/utils/resowner_private.h b/src/include/utils/resowner_private.h index c480a1a24be..6dafc87e28c 100644 --- a/src/include/utils/resowner_private.h +++ b/src/include/utils/resowner_private.h @@ -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 */ |