PostgreSQL Source Code  git master
scram-common.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * scram-common.h
4  * Declarations for helper functions used for SCRAM authentication
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/common/scram-common.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef SCRAM_COMMON_H
14 #define SCRAM_COMMON_H
15 
16 #include "common/cryptohash.h"
17 #include "common/sha2.h"
18 
19 /* Name of SCRAM mechanisms per IANA */
20 #define SCRAM_SHA_256_NAME "SCRAM-SHA-256"
21 #define SCRAM_SHA_256_PLUS_NAME "SCRAM-SHA-256-PLUS" /* with channel binding */
22 
23 /* Length of SCRAM keys (client and server) */
24 #define SCRAM_KEY_LEN PG_SHA256_DIGEST_LENGTH
25 
26 /* length of HMAC */
27 #define SHA256_HMAC_B PG_SHA256_BLOCK_LENGTH
28 
29 /*
30  * Size of random nonce generated in the authentication exchange. This
31  * is in "raw" number of bytes, the actual nonces sent over the wire are
32  * encoded using only ASCII-printable characters.
33  */
34 #define SCRAM_RAW_NONCE_LEN 18
35 
36 /*
37  * Length of salt when generating new secrets, in bytes. (It will be stored
38  * and sent over the wire encoded in Base64.) 16 bytes is what the example in
39  * RFC 7677 uses.
40  */
41 #define SCRAM_DEFAULT_SALT_LEN 16
42 
43 /*
44  * Default number of iterations when generating secret. Should be at least
45  * 4096 per RFC 7677.
46  */
47 #define SCRAM_DEFAULT_ITERATIONS 4096
48 
49 /*
50  * Context data for HMAC used in SCRAM authentication.
51  */
52 typedef struct
53 {
57 
58 extern int scram_HMAC_init(scram_HMAC_ctx *ctx, const uint8 *key, int keylen);
59 extern int scram_HMAC_update(scram_HMAC_ctx *ctx, const char *str, int slen);
60 extern int scram_HMAC_final(uint8 *result, scram_HMAC_ctx *ctx);
61 
62 extern int scram_SaltedPassword(const char *password, const char *salt,
63  int saltlen, int iterations, uint8 *result);
64 extern int scram_H(const uint8 *str, int len, uint8 *result);
65 extern int scram_ClientKey(const uint8 *salted_password, uint8 *result);
66 extern int scram_ServerKey(const uint8 *salted_password, uint8 *result);
67 
68 extern char *scram_build_secret(const char *salt, int saltlen, int iterations,
69  const char *password);
70 
71 #endif /* SCRAM_COMMON_H */
char * scram_build_secret(const char *salt, int saltlen, int iterations, const char *password)
Definition: scram-common.c:261
unsigned char uint8
Definition: c.h:427
int scram_H(const uint8 *str, int len, uint8 *result)
Definition: scram-common.c:195
pg_cryptohash_ctx * sha256ctx
Definition: scram-common.h:54
int scram_ClientKey(const uint8 *salted_password, uint8 *result)
Definition: scram-common.c:219
static char * password
Definition: streamutil.c:53
int scram_SaltedPassword(const char *password, const char *salt, int saltlen, int iterations, uint8 *result)
Definition: scram-common.c:142
int scram_HMAC_init(scram_HMAC_ctx *ctx, const uint8 *key, int keylen)
Definition: scram-common.c:35
int scram_HMAC_update(scram_HMAC_ctx *ctx, const char *str, int slen)
Definition: scram-common.c:93
int scram_HMAC_final(uint8 *result, scram_HMAC_ctx *ctx)
Definition: scram-common.c:109
int scram_ServerKey(const uint8 *salted_password, uint8 *result)
Definition: scram-common.c:237
#define SHA256_HMAC_B
Definition: scram-common.h:27