PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
px-crypt.c File Reference
#include "postgres.h"
#include "px.h"
#include "px-crypt.h"
#include "utils/backend_random.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

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

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

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

Referenced by pg_crypt().

93 {
94  const struct px_crypt_algo *c;
95 
96  for (c = px_crypt_list; c->id; c++)
97  {
98  if (!c->id_len)
99  break;
100  if (strncmp(salt, c->id, c->id_len) == 0)
101  break;
102  }
103 
104  if (c->crypt == NULL)
105  return NULL;
106 
107  return c->crypt(psw, salt, buf, len);
108 }
static const struct px_crypt_algo px_crypt_list[]
Definition: px-crypt.c:81
char *(* crypt)(const char *psw, const char *salt, char *buf, unsigned len)
Definition: px-crypt.c:76
char * c
static char * buf
Definition: pg_test_fsync.c:66
unsigned id_len
Definition: px-crypt.c:75
#define NULL
Definition: c.h:229
char * id
Definition: px-crypt.c:74
int px_gen_salt ( const char *  salt_type,
char *  buf,
int  rounds 
)

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

References generator::def_rounds, generator::gen, generator::input_len, generator::max_rounds, generator::min_rounds, generator::name, NULL, pg_backend_random(), pg_strcasecmp(), 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().

135 {
136  struct generator *g;
137  char *p;
138  char rbuf[16];
139 
140  for (g = gen_list; g->name; g++)
141  if (pg_strcasecmp(g->name, salt_type) == 0)
142  break;
143 
144  if (g->name == NULL)
145  return PXE_UNKNOWN_SALT_ALGO;
146 
147  if (g->def_rounds)
148  {
149  if (rounds == 0)
150  rounds = g->def_rounds;
151 
152  if (rounds < g->min_rounds || rounds > g->max_rounds)
153  return PXE_BAD_SALT_ROUNDS;
154  }
155 
156  if (!pg_backend_random(rbuf, g->input_len))
157  return PXE_NO_RANDOM;
158 
159  p = g->gen(rounds, rbuf, g->input_len, buf, PX_MAX_SALT_LEN);
160  px_memset(rbuf, 0, sizeof(rbuf));
161 
162  if (p == NULL)
163  return PXE_BAD_SALT_ROUNDS;
164 
165  return strlen(p);
166 }
int input_len
Definition: px-crypt.c:119
int min_rounds
Definition: px-crypt.c:121
#define PXE_NO_RANDOM
Definition: px.h:78
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:75
static struct generator gen_list[]
Definition: px-crypt.c:125
bool pg_backend_random(char *dst, int len)
static char * buf
Definition: pg_test_fsync.c:66
int max_rounds
Definition: px-crypt.c:122
#define NULL
Definition: c.h:229
#define PXE_BAD_SALT_ROUNDS
Definition: px.h:76
int def_rounds
Definition: px-crypt.c:120
char *(* gen)(unsigned long count, const char *input, int size, char *output, int output_size)
Definition: px-crypt.c:117
char * name
Definition: px-crypt.c:116
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
static char* run_crypt_bf ( const char *  psw,
const char *  salt,
char *  buf,
unsigned  len 
)
static

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

References _crypt_blowfish_rn().

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

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

References buf, NULL, and px_crypt_des().

42 {
43  char *res;
44 
45  res = px_crypt_des(psw, salt);
46  if (res == NULL || strlen(res) > len - 1)
47  return NULL;
48  strcpy(buf, res);
49  return buf;
50 }
static char * buf
Definition: pg_test_fsync.c:66
#define NULL
Definition: c.h:229
char * px_crypt_des(const char *key, const char *setting)
Definition: crypt-des.c:654
static char* run_crypt_md5 ( const char *  psw,
const char *  salt,
char *  buf,
unsigned  len 
)
static

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

References px_crypt_md5().

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

Variable Documentation

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
#define NULL
Definition: c.h:229
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 125 of file px-crypt.c.

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:63
static char * run_crypt_des(const char *psw, const char *salt, char *buf, unsigned len)
Definition: px-crypt.c:40
#define NULL
Definition: c.h:229
static char * run_crypt_md5(const char *psw, const char *salt, char *buf, unsigned len)
Definition: px-crypt.c:53

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