It was a bad style to begin with, and now several loops can be clearer.
* pgcrypto.c: Fix function comments
* crypt-gensalt.c, crypt-blowfish.c: stop messing with errno
* openssl.c: use px_free instead pfree
* px.h: make redefining px_alloc/px_realloc/px_free easier
Marko Kreen
#include "px.h"
#include "px-crypt.h"
-#define __set_errno(v)
-
-#ifndef __set_errno
-#define __set_errno(val) errno = (val)
-#endif
-
#ifdef __i386__
#define BF_ASM 0 /* 1 */
#define BF_SCALE 1
int i;
if (size < 7 + 22 + 31 + 1)
- {
- __set_errno(ERANGE);
return NULL;
- }
if (setting[0] != '$' ||
setting[1] != '2' ||
setting[5] < '0' || setting[5] > '9' ||
setting[6] != '$')
{
- __set_errno(EINVAL);
return NULL;
}
if (count < 16 || BF_decode(data.binary.salt, &setting[7], 16))
{
memset(data.binary.salt, 0, sizeof(data.binary.salt));
- __set_errno(EINVAL);
return NULL;
}
BF_swap(data.binary.salt, 4);
#include "px.h"
#include "px-crypt.h"
-#include
-#ifndef __set_errno
-#define __set_errno(val) (errno = (val))
-#endif
-
typedef unsigned int BF_word;
unsigned char _crypt_itoa64[64 + 1] =
{
if (output_size > 0)
output[0] = '\0';
- __set_errno((output_size < 2 + 1) ? ERANGE : EINVAL);
return NULL;
}
{
if (output_size > 0)
output[0] = '\0';
- __set_errno((output_size < 1 + 4 + 4 + 1) ? ERANGE : EINVAL);
return NULL;
}
{
if (output_size > 0)
output[0] = '\0';
- __set_errno((output_size < 3 + 4 + 1) ? ERANGE : EINVAL);
return NULL;
}
{
if (output_size > 0)
output[0] = '\0';
- __set_errno((output_size < 7 + 22 + 1) ? ERANGE : EINVAL);
return NULL;
}
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.14 2004/10/25 02:15:02 tgl Exp $
+ * $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.15 2005/03/21 05:18:45 neilc Exp $
*/
static void init_md5(PX_MD * h);
static void init_sha1(PX_MD * h);
-static struct int_digest
+struct int_digest
{
char *name;
void (*init) (PX_MD * h);
-} int_digest_list[] =
+};
-{
- {
- "md5", init_md5
- },
- {
- "sha1", init_sha1
- },
- {
- NULL, NULL
- }
+static const struct int_digest
+int_digest_list[] = {
+ { "md5", init_md5 },
+ { "sha1", init_sha1 },
+ { NULL, NULL }
};
/* MD5 */
return bf_load(MODE_CBC);
}
-static struct
+struct int_cipher
{
char *name;
PX_Cipher *(*load) (void);
-} int_ciphers[] =
+};
-{
- {
- "bf-cbc", bf_cbc_load
- },
- {
- "bf-ecb", bf_ecb_load
- },
- {
- "aes-128-cbc", rj_128_cbc
- },
- {
- "aes-128-ecb", rj_128_ecb
- },
- {
- NULL, NULL
- }
+static const struct int_cipher
+int_ciphers[] = {
+ { "bf-cbc", bf_cbc_load },
+ { "bf-ecb", bf_ecb_load },
+ { "aes-128-cbc", rj_128_cbc },
+ { "aes-128-ecb", rj_128_ecb },
+ { NULL, NULL }
};
-static PX_Alias int_aliases[] = {
+static const PX_Alias int_aliases[] = {
{"bf", "bf-cbc"},
{"blowfish", "bf-cbc"},
{"aes", "aes-128-cbc"},
int
px_find_digest(const char *name, PX_MD ** res)
{
- struct int_digest *p;
+ const struct int_digest *p;
PX_MD *h;
for (p = int_digest_list; p->name; p++)
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.14 2005/03/12 06:53:54 neilc Exp $
+ * $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.15 2005/03/21 05:18:45 neilc Exp $
*/
ossldata *od = (ossldata *) c->ptr;
memset(od, 0, sizeof(*od));
- pfree(od);
- pfree(c);
+ px_free(od);
+ px_free(c);
}
/* Blowfish */
/*
* Special handlers
*/
-static const struct
+struct ossl_cipher_lookup
{
const char *name;
const struct ossl_cipher *ciph;
-} ossl_cipher_types[] =
+};
-{
- {
- "bf-cbc", &ossl_bf_cbc
- },
- {
- "bf-ecb", &ossl_bf_ecb
- },
- {
- "bf-cfb", &ossl_bf_cfb
- },
- {
- "des-ecb", &ossl_des_ecb
- },
- {
- "des-cbc", &ossl_des_cbc
- },
- {
- "cast5-ecb", &ossl_cast_ecb
- },
- {
- "cast5-cbc", &ossl_cast_cbc
- },
- {
- NULL
- }
+static const struct ossl_cipher_lookup ossl_cipher_types[] = {
+ {"bf-cbc", &ossl_bf_cbc},
+ {"bf-ecb", &ossl_bf_ecb},
+ {"bf-cfb", &ossl_bf_cfb},
+ {"des-ecb", &ossl_des_ecb},
+ {"des-cbc", &ossl_des_cbc},
+ {"cast5-ecb", &ossl_cast_ecb},
+ {"cast5-cbc", &ossl_cast_cbc},
+ {NULL}
};
/* PUBLIC functions */
int
px_find_cipher(const char *name, PX_Cipher ** res)
{
- unsigned i;
- PX_Cipher *c = NULL,
- *csrc;
+ const struct ossl_cipher_lookup *i;
+ PX_Cipher *c = NULL;
ossldata *od;
- const struct ossl_cipher *ossl_ciph = NULL;
name = px_resolve_alias(ossl_aliases, name);
- for (i = 0; ossl_cipher_types[i].name; i++)
- {
- if (!strcmp(ossl_cipher_types[i].name, name))
- {
- ossl_ciph = ossl_cipher_types[i].ciph;
+ for (i = ossl_cipher_types; i->name; i++)
+ if (!strcmp(i->name, name))
break;
- }
- }
- if (ossl_ciph == NULL)
+ if (i->name == NULL)
return -1;
od = px_alloc(sizeof(*od));
memset(od, 0, sizeof(*od));
- od->ciph = ossl_ciph;
-
- csrc = NULL;
+ od->ciph = i->ciph;
c = px_alloc(sizeof(*c));
c->block_size = gen_ossl_block_size;
c->key_size = gen_ossl_key_size;
c->iv_size = gen_ossl_iv_size;
c->free = gen_ossl_free;
- c->init = ossl_ciph->init;
- c->encrypt = ossl_ciph->encrypt;
- c->decrypt = ossl_ciph->decrypt;
+ c->init = od->ciph->init;
+ c->encrypt = od->ciph->encrypt;
+ c->decrypt = od->ciph->decrypt;
c->ptr = od;
*res = c;
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $PostgreSQL: pgsql/contrib/pgcrypto/pgcrypto.c,v 1.16 2004/05/07 00:24:57 tgl Exp $
+ * $PostgreSQL: pgsql/contrib/pgcrypto/pgcrypto.c,v 1.17 2005/03/21 05:18:45 neilc Exp $
*/
#include "postgres.h"
static void *
find_provider(text *name, PFN pf, char *desc, int silent);
-/* SQL function: hash(text, text) returns text */
+/* SQL function: hash(bytea, text) returns bytea */
PG_FUNCTION_INFO_V1(pg_digest);
Datum
PG_RETURN_BOOL(true);
}
-/* SQL function: hmac(data:text, key:text, type:text) */
+/* SQL function: hmac(data:bytea, key:bytea, type:text) returns bytea */
PG_FUNCTION_INFO_V1(pg_hmac);
Datum
PG_RETURN_TEXT_P(res);
}
-/* SQL function: pg_encrypt(text, text, text) returns text */
+/* SQL function: pg_encrypt(bytea, bytea, text) returns bytea */
PG_FUNCTION_INFO_V1(pg_encrypt);
Datum
PG_RETURN_BYTEA_P(res);
}
-/* SQL function: pg_decrypt(text, text, text) returns text */
+/* SQL function: pg_decrypt(bytea, bytea, text) returns bytea */
PG_FUNCTION_INFO_V1(pg_decrypt);
Datum
PG_RETURN_BYTEA_P(res);
}
-/* SQL function: pg_encrypt(text, text, text) returns text */
+/* SQL function: pg_encrypt_iv(bytea, bytea, bytea, text) returns bytea */
PG_FUNCTION_INFO_V1(pg_encrypt_iv);
Datum
PG_RETURN_BYTEA_P(res);
}
-/* SQL function: pg_decrypt_iv(text, text, text) returns text */
+/* SQL function: pg_decrypt_iv(bytea, bytea, bytea, text) returns bytea */
PG_FUNCTION_INFO_V1(pg_decrypt_iv);
Datum
PG_RETURN_BYTEA_P(res);
}
-/* SQL function: pg_decrypt(text, text, text) returns text */
+/* SQL function: pg_cipher_exists(text) returns bool */
PG_FUNCTION_INFO_V1(pg_cipher_exists);
Datum
PG_RETURN_BOOL((c != NULL) ? true : false);
}
-
static void *
find_provider(text *name,
PFN provider_lookup,
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $PostgreSQL: pgsql/contrib/pgcrypto/px-crypt.c,v 1.8 2004/05/07 00:24:57 tgl Exp $
+ * $PostgreSQL: pgsql/contrib/pgcrypto/px-crypt.c,v 1.9 2005/03/21 05:18:45 neilc Exp $
*/
return res;
}
-static struct
+struct px_crypt_algo
{
char *id;
unsigned id_len;
char *(*crypt) (const char *psw, const char *salt,
char *buf, unsigned len);
-} px_crypt_list[] =
+};
-{
- {
- "$2a$", 4, run_crypt_bf
- },
- {
- "$2$", 3, NULL
- }, /* N/A */
- {
- "$1$", 3, run_crypt_md5
- },
- {
- "_", 1, run_crypt_des
- },
- {
- "", 0, run_crypt_des
- },
- {
- NULL, 0, NULL
- }
+static const struct px_crypt_algo
+px_crypt_list[] = {
+ {"$2a$", 4, run_crypt_bf},
+ {"$2$", 3, NULL}, /* N/A */
+ {"$1$", 3, run_crypt_md5},
+ {"_", 1, run_crypt_des},
+ {"", 0, run_crypt_des},
+ {NULL, 0, NULL}
};
char *
px_crypt(const char *psw, const char *salt, char *buf, unsigned len)
{
- int i;
+ const struct px_crypt_algo *c;
- for (i = 0; px_crypt_list[i].id; i++)
+ for (c = px_crypt_list; c->id; c++)
{
- if (!px_crypt_list[i].id_len)
+ if (!c->id_len)
break;
- if (!strncmp(salt, px_crypt_list[i].id, px_crypt_list[i].id_len))
+ if (!strncmp(salt, c->id, c->id_len))
break;
}
- if (px_crypt_list[i].crypt == NULL)
+ if (c->crypt == NULL)
return NULL;
- return px_crypt_list[i].crypt(psw, salt, buf, len);
+ return c->crypt(psw, salt, buf, len);
}
#else /* PX_SYSTEM_CRYPT */
{"md5", _crypt_gensalt_md5_rn, 6, 0, 0, 0},
{"xdes", _crypt_gensalt_extended_rn, 3, PX_XDES_ROUNDS, 1, 0xFFFFFF},
{"bf", _crypt_gensalt_blowfish_rn, 16, PX_BF_ROUNDS, 4, 31},
- {NULL, NULL, 0, 0, 0}
+ {NULL, NULL, 0, 0, 0, 0}
};
unsigned
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.9 2003/11/29 22:39:28 pgsql Exp $
+ * $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.10 2005/03/21 05:18:46 neilc Exp $
*/
#ifndef __PX_H
#endif
-#if 1
+#ifndef PX_OWN_ALLOC
#define px_alloc(s) palloc(s)
-#define px_realloc(p, s) prealloc(p, s)
+#define px_realloc(p, s) repalloc(p, s)
#define px_free(p) pfree(p)
#else
-void *xalloc(size_t s);
-void *xrealloc(void *p, size_t s);
-void xfree(void *p);
+void *px_alloc(size_t s);
+void *px_realloc(void *p, size_t s);
+void px_free(void *p);
-#define px_alloc(s) xalloc(s)
-#define px_realloc(p, s) xrealloc(p, s)
-#define px_free(p) xfree(p)
#endif
/* max len of 'type' parms */