Only skip pages marked as clean in the visibility map, if the last 32
[PostgreSQL.git] / contrib / pgcrypto / px-crypt.c
blobb3cb239bfa1baad18f3ec59b1d9a99df8fca387b
1 /*
2 * px-crypt.c
3 * Wrapper for various crypt algorithms.
5 * Copyright (c) 2001 Marko Kreen
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * $PostgreSQL$
32 #include "postgres.h"
34 #include "px.h"
35 #include "px-crypt.h"
38 static char *
39 run_crypt_des(const char *psw, const char *salt,
40 char *buf, unsigned len)
42 char *res;
44 res = px_crypt_des(psw, salt);
45 if (strlen(res) > len - 1)
46 return NULL;
47 strcpy(buf, res);
48 return buf;
51 static char *
52 run_crypt_md5(const char *psw, const char *salt,
53 char *buf, unsigned len)
55 char *res;
57 res = px_crypt_md5(psw, salt, buf, len);
58 return res;
61 static char *
62 run_crypt_bf(const char *psw, const char *salt,
63 char *buf, unsigned len)
65 char *res;
67 res = _crypt_blowfish_rn(psw, salt, buf, len);
68 return res;
71 struct px_crypt_algo
73 char *id;
74 unsigned id_len;
75 char *(*crypt) (const char *psw, const char *salt,
76 char *buf, unsigned len);
79 static const struct px_crypt_algo
80 px_crypt_list[] = {
81 {"$2a$", 4, run_crypt_bf},
82 {"$2$", 3, NULL}, /* N/A */
83 {"$1$", 3, run_crypt_md5},
84 {"_", 1, run_crypt_des},
85 {"", 0, run_crypt_des},
86 {NULL, 0, NULL}
89 char *
90 px_crypt(const char *psw, const char *salt, char *buf, unsigned len)
92 const struct px_crypt_algo *c;
94 for (c = px_crypt_list; c->id; c++)
96 if (!c->id_len)
97 break;
98 if (!strncmp(salt, c->id, c->id_len))
99 break;
102 if (c->crypt == NULL)
103 return NULL;
105 return c->crypt(psw, salt, buf, len);
109 * salt generators
112 struct generator
114 char *name;
115 char *(*gen) (unsigned long count, const char *input, int size,
116 char *output, int output_size);
117 int input_len;
118 int def_rounds;
119 int min_rounds;
120 int max_rounds;
123 static struct generator gen_list[] = {
124 {"des", _crypt_gensalt_traditional_rn, 2, 0, 0, 0},
125 {"md5", _crypt_gensalt_md5_rn, 6, 0, 0, 0},
126 {"xdes", _crypt_gensalt_extended_rn, 3, PX_XDES_ROUNDS, 1, 0xFFFFFF},
127 {"bf", _crypt_gensalt_blowfish_rn, 16, PX_BF_ROUNDS, 4, 31},
128 {NULL, NULL, 0, 0, 0, 0}
132 px_gen_salt(const char *salt_type, char *buf, int rounds)
134 int res;
135 struct generator *g;
136 char *p;
137 char rbuf[16];
139 for (g = gen_list; g->name; g++)
140 if (pg_strcasecmp(g->name, salt_type) == 0)
141 break;
143 if (g->name == NULL)
144 return PXE_UNKNOWN_SALT_ALGO;
146 if (g->def_rounds)
148 if (rounds == 0)
149 rounds = g->def_rounds;
151 if (rounds < g->min_rounds || rounds > g->max_rounds)
152 return PXE_BAD_SALT_ROUNDS;
155 res = px_get_pseudo_random_bytes((uint8 *) rbuf, g->input_len);
156 if (res < 0)
157 return res;
159 p = g->gen(rounds, rbuf, g->input_len, buf, PX_MAX_SALT_LEN);
160 memset(rbuf, 0, sizeof(rbuf));
162 if (p == NULL)
163 return PXE_BAD_SALT_ROUNDS;
165 return strlen(p);