PostgreSQL Source Code  git master
px-crypt.c File Reference
#include "postgres.h"
#include "px.h"
#include "px-crypt.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)
 
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 91 of file px-crypt.c.

References px_crypt_algo::crypt, px_crypt_algo::id, and px_crypt_algo::id_len.

Referenced by pg_crypt().

92 {
93  const struct px_crypt_algo *c;
94 
95  for (c = px_crypt_list; c->id; c++)
96  {
97  if (!c->id_len)
98  break;
99  if (strncmp(salt, c->id, c->id_len) == 0)
100  break;
101  }
102 
103  if (c->crypt == NULL)
104  return NULL;
105 
106  return c->crypt(psw, salt, buf, len);
107 }
static const struct px_crypt_algo px_crypt_list[]
Definition: px-crypt.c:80
char * c
static char * buf
Definition: pg_test_fsync.c:68
char *(* crypt)(const char *psw, const char *salt, char *buf, unsigned len)
Definition: px-crypt.c:75
unsigned id_len
Definition: px-crypt.c:74
char * id
Definition: px-crypt.c:73

◆ px_gen_salt()

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

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

References generator::def_rounds, generator::gen, 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().

134 {
135  struct generator *g;
136  char *p;
137  char rbuf[16];
138 
139  for (g = gen_list; g->name; g++)
140  if (pg_strcasecmp(g->name, salt_type) == 0)
141  break;
142 
143  if (g->name == NULL)
144  return PXE_UNKNOWN_SALT_ALGO;
145 
146  if (g->def_rounds)
147  {
148  if (rounds == 0)
149  rounds = g->def_rounds;
150 
151  if (rounds < g->min_rounds || rounds > g->max_rounds)
152  return PXE_BAD_SALT_ROUNDS;
153  }
154 
155  if (!pg_strong_random(rbuf, g->input_len))
156  return PXE_NO_RANDOM;
157 
158  p = g->gen(rounds, rbuf, g->input_len, buf, PX_MAX_SALT_LEN);
159  px_memset(rbuf, 0, sizeof(rbuf));
160 
161  if (p == NULL)
162  return PXE_BAD_SALT_ROUNDS;
163 
164  return strlen(p);
165 }
int input_len
Definition: px-crypt.c:118
char *(* gen)(unsigned long count, const char *input, int size, char *output, int output_size)
Definition: px-crypt.c:116
int min_rounds
Definition: px-crypt.c:120
#define PXE_NO_RANDOM
Definition: px.h:75
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define PX_MAX_SALT_LEN
Definition: px-crypt.h:39
#define PXE_UNKNOWN_SALT_ALGO
Definition: px.h:72
static struct generator gen_list[]
Definition: px-crypt.c:124
static char * buf
Definition: pg_test_fsync.c:68
int max_rounds
Definition: px-crypt.c:121
bool pg_strong_random(void *buf, size_t len)
#define PXE_BAD_SALT_ROUNDS
Definition: px.h:73
int def_rounds
Definition: px-crypt.c:119
char * name
Definition: px-crypt.c:115
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:126

◆ run_crypt_bf()

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

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

References _crypt_blowfish_rn().

64 {
65  char *res;
66 
67  res = _crypt_blowfish_rn(psw, salt, buf, len);
68  return res;
69 }
char * _crypt_blowfish_rn(const char *key, const char *setting, char *output, int size)
static char * buf
Definition: pg_test_fsync.c:68

◆ run_crypt_des()

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

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

References buf, and px_crypt_des().

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

◆ run_crypt_md5()

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

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

References px_crypt_md5().

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

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},
{NULL, NULL, 0, 0, 0, 0}
}
#define PX_BF_ROUNDS
Definition: px-crypt.h:46
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_traditional_rn(unsigned long count, const char *input, int size, char *output, int output_size)
Definition: crypt-gensalt.c:25
char * _crypt_gensalt_blowfish_rn(unsigned long count, const char *input, int size, char *output, int output_size)
#define PX_XDES_ROUNDS
Definition: px-crypt.h:43
char * _crypt_gensalt_extended_rn(unsigned long count, const char *input, int size, char *output, int output_size)
Definition: crypt-gensalt.c:43

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

◆ 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},
{"_", 1, run_crypt_des},
{"", 0, run_crypt_des},
{NULL, 0, NULL}
}
static char * run_crypt_bf(const char *psw, const char *salt, char *buf, unsigned len)
Definition: px-crypt.c:62
static char * run_crypt_des(const char *psw, const char *salt, char *buf, unsigned len)
Definition: px-crypt.c:39
static char * run_crypt_md5(const char *psw, const char *salt, char *buf, unsigned len)
Definition: px-crypt.c:52

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