PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
px-crypt.c File Reference
#include "postgres.h"
#include "px-crypt.h"
#include "px.h"
Include dependency graph for px-crypt.c:

Go to the source code of this file.

Data Structures

struct  px_crypt_algo
 
struct  generator
 

Functions

static char * run_crypt_des (const char *psw, const char *salt, char *buf, unsigned len)
 
static char * run_crypt_md5 (const char *psw, const char *salt, char *buf, unsigned len)
 
static char * run_crypt_bf (const char *psw, const char *salt, char *buf, unsigned len)
 
static char * run_crypt_sha (const char *psw, const char *salt, char *buf, unsigned len)
 
char * px_crypt (const char *psw, const char *salt, char *buf, unsigned len)
 
int px_gen_salt (const char *salt_type, char *buf, int rounds)
 

Variables

static const struct px_crypt_algo px_crypt_list []
 
static struct generator gen_list []
 

Function Documentation

◆ px_crypt()

char * px_crypt ( const char *  psw,
const char *  salt,
char *  buf,
unsigned  len 
)

Definition at line 102 of file px-crypt.c.

103{
104 const struct px_crypt_algo *c;
105
107
108 for (c = px_crypt_list; c->id; c++)
109 {
110 if (!c->id_len)
111 break;
112 if (strncmp(salt, c->id, c->id_len) == 0)
113 break;
114 }
115
116 if (c->crypt == NULL)
117 return NULL;
118
119 return c->crypt(psw, salt, buf, len);
120}
void CheckBuiltinCryptoMode(void)
Definition: openssl.c:874
const void size_t len
static char * buf
Definition: pg_test_fsync.c:72
char * c
static const struct px_crypt_algo px_crypt_list[]
Definition: px-crypt.c:89
char * id
Definition: px-crypt.c:82

References buf, CheckBuiltinCryptoMode(), px_crypt_algo::id, len, and px_crypt_list.

Referenced by pg_crypt().

◆ px_gen_salt()

int px_gen_salt ( const char *  salt_type,
char *  buf,
int  rounds 
)

Definition at line 156 of file px-crypt.c.

157{
158 struct generator *g;
159 char *p;
160 char rbuf[16];
161
163
164 for (g = gen_list; g->name; g++)
165 if (pg_strcasecmp(g->name, salt_type) == 0)
166 break;
167
168 if (g->name == NULL)
170
171 if (g->def_rounds)
172 {
173 if (rounds == 0)
174 rounds = g->def_rounds;
175
176 if (rounds < g->min_rounds || rounds > g->max_rounds)
177 return PXE_BAD_SALT_ROUNDS;
178 }
179
180 if (!pg_strong_random(rbuf, g->input_len))
181 return PXE_NO_RANDOM;
182
183 p = g->gen(rounds, rbuf, g->input_len, buf, PX_MAX_SALT_LEN);
184 px_memset(rbuf, 0, sizeof(rbuf));
185
186 if (p == NULL)
187 return PXE_BAD_SALT_ROUNDS;
188
189 return strlen(p);
190}
bool pg_strong_random(void *buf, size_t len)
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
static struct generator gen_list[]
Definition: px-crypt.c:137
#define PX_MAX_SALT_LEN
Definition: px-crypt.h:39
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:123
#define PXE_BAD_SALT_ROUNDS
Definition: px.h:61
#define PXE_NO_RANDOM
Definition: px.h:63
#define PXE_UNKNOWN_SALT_ALGO
Definition: px.h:60
int input_len
Definition: px-crypt.c:131
int max_rounds
Definition: px-crypt.c:134
char *(* gen)(unsigned long count, const char *input, int size, char *output, int output_size)
Definition: px-crypt.c:129
int def_rounds
Definition: px-crypt.c:132
char * name
Definition: px-crypt.c:128
int min_rounds
Definition: px-crypt.c:133

References buf, CheckBuiltinCryptoMode(), generator::def_rounds, generator::gen, gen_list, generator::input_len, generator::max_rounds, generator::min_rounds, generator::name, pg_strcasecmp(), pg_strong_random(), PX_MAX_SALT_LEN, px_memset(), PXE_BAD_SALT_ROUNDS, PXE_NO_RANDOM, and PXE_UNKNOWN_SALT_ALGO.

Referenced by pg_gen_salt(), and pg_gen_salt_rounds().

◆ run_crypt_bf()

static char * run_crypt_bf ( const char *  psw,
const char *  salt,
char *  buf,
unsigned  len 
)
static

Definition at line 61 of file px-crypt.c.

63{
64 char *res;
65
66 res = _crypt_blowfish_rn(psw, salt, buf, len);
67 return res;
68}
char * _crypt_blowfish_rn(const char *key, const char *setting, char *output, int size)

References _crypt_blowfish_rn(), buf, and len.

◆ run_crypt_des()

static char * run_crypt_des ( const char *  psw,
const char *  salt,
char *  buf,
unsigned  len 
)
static

Definition at line 38 of file px-crypt.c.

40{
41 char *res;
42
43 res = px_crypt_des(psw, salt);
44 if (res == NULL || strlen(res) > len - 1)
45 return NULL;
46 strcpy(buf, res);
47 return buf;
48}
char * px_crypt_des(const char *key, const char *setting)
Definition: crypt-des.c:651

References buf, len, and px_crypt_des().

◆ run_crypt_md5()

static char * run_crypt_md5 ( const char *  psw,
const char *  salt,
char *  buf,
unsigned  len 
)
static

Definition at line 51 of file px-crypt.c.

53{
54 char *res;
55
56 res = px_crypt_md5(psw, salt, buf, len);
57 return res;
58}
char * px_crypt_md5(const char *pw, const char *salt, char *passwd, unsigned dstlen)
Definition: crypt-md5.c:34

References buf, len, and px_crypt_md5().

◆ run_crypt_sha()

static char * run_crypt_sha ( const char *  psw,
const char *  salt,
char *  buf,
unsigned  len 
)
static

Definition at line 71 of file px-crypt.c.

73{
74 char *res;
75
76 res = px_crypt_shacrypt(psw, salt, buf, len);
77 return res;
78}
char * px_crypt_shacrypt(const char *pw, const char *salt, char *passwd, unsigned dstlen)
Definition: crypt-sha.c:69

References buf, len, and px_crypt_shacrypt().

Variable Documentation

◆ gen_list

struct generator gen_list[]
static
Initial value:
= {
{"des", _crypt_gensalt_traditional_rn, 2, 0, 0, 0},
{"md5", _crypt_gensalt_md5_rn, 6, 0, 0, 0},
{"xdes", _crypt_gensalt_extended_rn, 3, PX_XDES_ROUNDS, 1, 0xFFFFFF},
{
"sha256crypt", _crypt_gensalt_sha256_rn,
},
{
"sha512crypt", _crypt_gensalt_sha512_rn,
},
{NULL, NULL, 0, 0, 0, 0}
}
char * _crypt_gensalt_sha512_rn(unsigned long count, char const *input, int size, char *output, int output_size)
char * _crypt_gensalt_traditional_rn(unsigned long count, const char *input, int size, char *output, int output_size)
Definition: crypt-gensalt.c:25
char * _crypt_gensalt_sha256_rn(unsigned long count, const char *input, int size, char *output, int output_size)
char * _crypt_gensalt_md5_rn(unsigned long count, const char *input, int size, char *output, int output_size)
Definition: crypt-gensalt.c:79
char * _crypt_gensalt_blowfish_rn(unsigned long count, const char *input, int size, char *output, int output_size)
char * _crypt_gensalt_extended_rn(unsigned long count, const char *input, int size, char *output, int output_size)
Definition: crypt-gensalt.c:43
#define PX_SHACRYPT_ROUNDS_MAX
Definition: px-crypt.h:70
#define PX_BF_ROUNDS
Definition: px-crypt.h:46
#define PX_XDES_ROUNDS
Definition: px-crypt.h:43
#define PX_SHACRYPT_ROUNDS_MIN
Definition: px-crypt.h:67
#define PX_SHACRYPT_SALT_MAX_LEN
Definition: px-crypt.h:49
#define PX_SHACRYPT_ROUNDS_DEFAULT
Definition: px-crypt.h:64

Definition at line 137 of file px-crypt.c.

Referenced by px_gen_salt().

◆ px_crypt_list

const struct px_crypt_algo px_crypt_list[]
static
Initial value:
= {
{"$2a$", 4, run_crypt_bf},
{"$2x$", 4, run_crypt_bf},
{"$2$", 3, NULL},
{"$1$", 3, run_crypt_md5},
{"$5$", 3, run_crypt_sha},
{"$6$", 3, run_crypt_sha},
{"_", 1, run_crypt_des},
{"", 0, run_crypt_des},
{NULL, 0, NULL}
}
static char * run_crypt_des(const char *psw, const char *salt, char *buf, unsigned len)
Definition: px-crypt.c:38
static char * run_crypt_bf(const char *psw, const char *salt, char *buf, unsigned len)
Definition: px-crypt.c:61
static char * run_crypt_md5(const char *psw, const char *salt, char *buf, unsigned len)
Definition: px-crypt.c:51
static char * run_crypt_sha(const char *psw, const char *salt, char *buf, unsigned len)
Definition: px-crypt.c:71

Definition at line 89 of file px-crypt.c.

Referenced by px_crypt().