#include "postgres.h"
#include <time.h>
#include "px.h"
#include "md5.h"
#include "sha1.h"
#include "blf.h"
#include "rijndael.h"
#include "fortuna.h"
Go to the source code of this file.
Data Structures | |
struct | int_digest |
struct | int_ctx |
struct | int_cipher |
Defines | |
#define | SYSTEM_RESEED_MIN (20*60) |
#define | SYSTEM_RESEED_CHECK_TIME (10*60) |
#define | SYSTEM_RESEED_CHANCE (4) |
#define | SYSTEM_RESEED_MAX (12*60*60) |
#define | MD5_DIGEST_LENGTH 16 |
#define | SHA1_DIGEST_LENGTH SHA1_RESULTLEN |
#define | SHA1_BLOCK_SIZE 64 |
#define | MD5_BLOCK_SIZE 64 |
#define | INT_MAX_KEY (512/8) |
#define | INT_MAX_IV (128/8) |
#define | MODE_ECB 0 |
#define | MODE_CBC 1 |
Functions | |
static void | init_md5 (PX_MD *h) |
static void | init_sha1 (PX_MD *h) |
void | init_sha224 (PX_MD *h) |
void | init_sha256 (PX_MD *h) |
void | init_sha384 (PX_MD *h) |
void | init_sha512 (PX_MD *h) |
static unsigned | int_md5_len (PX_MD *h) |
static unsigned | int_md5_block_len (PX_MD *h) |
static void | int_md5_update (PX_MD *h, const uint8 *data, unsigned dlen) |
static void | int_md5_reset (PX_MD *h) |
static void | int_md5_finish (PX_MD *h, uint8 *dst) |
static void | int_md5_free (PX_MD *h) |
static unsigned | int_sha1_len (PX_MD *h) |
static unsigned | int_sha1_block_len (PX_MD *h) |
static void | int_sha1_update (PX_MD *h, const uint8 *data, unsigned dlen) |
static void | int_sha1_reset (PX_MD *h) |
static void | int_sha1_finish (PX_MD *h, uint8 *dst) |
static void | int_sha1_free (PX_MD *h) |
static void | intctx_free (PX_Cipher *c) |
static unsigned | rj_block_size (PX_Cipher *c) |
static unsigned | rj_key_size (PX_Cipher *c) |
static unsigned | rj_iv_size (PX_Cipher *c) |
static int | rj_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv) |
static int | rj_real_init (struct int_ctx *cx, int dir) |
static int | rj_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res) |
static int | rj_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res) |
static PX_Cipher * | rj_load (int mode) |
static unsigned | bf_block_size (PX_Cipher *c) |
static unsigned | bf_key_size (PX_Cipher *c) |
static unsigned | bf_iv_size (PX_Cipher *c) |
static int | bf_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv) |
static int | bf_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res) |
static int | bf_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res) |
static PX_Cipher * | bf_load (int mode) |
static PX_Cipher * | rj_128_ecb (void) |
static PX_Cipher * | rj_128_cbc (void) |
static PX_Cipher * | bf_ecb_load (void) |
static PX_Cipher * | bf_cbc_load (void) |
int | px_find_digest (const char *name, PX_MD **res) |
int | px_find_cipher (const char *name, PX_Cipher **res) |
int | px_get_pseudo_random_bytes (uint8 *dst, unsigned count) |
static void | system_reseed (void) |
int | px_get_random_bytes (uint8 *dst, unsigned count) |
int | px_add_entropy (const uint8 *data, unsigned count) |
Variables | |
static struct int_digest | int_digest_list [] |
static struct int_cipher | int_ciphers [] |
static const PX_Alias | int_aliases [] |
static time_t | seed_time = 0 |
static time_t | check_time = 0 |
#define INT_MAX_IV (128/8) |
Definition at line 245 of file internal.c.
#define INT_MAX_KEY (512/8) |
Definition at line 244 of file internal.c.
#define MD5_BLOCK_SIZE 64 |
Definition at line 75 of file internal.c.
#define MD5_DIGEST_LENGTH 16 |
Definition at line 63 of file internal.c.
#define MODE_CBC 1 |
Definition at line 279 of file internal.c.
Referenced by bf_cbc_load(), bf_decrypt(), bf_encrypt(), rj_128_cbc(), rj_decrypt(), and rj_encrypt().
#define MODE_ECB 0 |
Definition at line 278 of file internal.c.
Referenced by bf_decrypt(), bf_ecb_load(), bf_encrypt(), and rj_128_ecb().
#define SHA1_BLOCK_SIZE 64 |
Definition at line 74 of file internal.c.
#define SHA1_DIGEST_LENGTH SHA1_RESULTLEN |
Definition at line 68 of file internal.c.
#define SYSTEM_RESEED_CHANCE (4) |
Definition at line 54 of file internal.c.
#define SYSTEM_RESEED_CHECK_TIME (10*60) |
Definition at line 50 of file internal.c.
Referenced by system_reseed().
#define SYSTEM_RESEED_MAX (12*60*60) |
Definition at line 59 of file internal.c.
Referenced by system_reseed().
#define SYSTEM_RESEED_MIN (20*60) |
Definition at line 46 of file internal.c.
Referenced by system_reseed().
static unsigned bf_block_size | ( | PX_Cipher * | c | ) | [static] |
Definition at line 420 of file internal.c.
{
return 8;
}
static PX_Cipher* bf_cbc_load | ( | void | ) | [static] |
Definition at line 475 of file internal.c.
References int_ctx::bf, blowfish_decrypt_cbc(), blowfish_decrypt_ecb(), int_ctx::ctx, cx(), int_ctx::mode, MODE_CBC, MODE_ECB, and px_cipher::ptr.
{ struct int_ctx *cx = (struct int_ctx *) c->ptr; BlowfishContext *bfctx = &cx->ctx.bf; if (dlen == 0) return 0; if (dlen & 7) return PXE_NOTBLOCKSIZE; memcpy(res, data, dlen); switch (cx->mode) { case MODE_ECB: blowfish_decrypt_ecb(res, dlen, bfctx); break; case MODE_CBC: blowfish_decrypt_cbc(res, dlen, bfctx); break; } return 0; }
static PX_Cipher* bf_ecb_load | ( | void | ) | [static] |
Definition at line 450 of file internal.c.
References int_ctx::bf, blowfish_encrypt_cbc(), blowfish_encrypt_ecb(), int_ctx::ctx, cx(), int_ctx::mode, MODE_CBC, MODE_ECB, and px_cipher::ptr.
{ struct int_ctx *cx = (struct int_ctx *) c->ptr; BlowfishContext *bfctx = &cx->ctx.bf; if (dlen == 0) return 0; if (dlen & 7) return PXE_NOTBLOCKSIZE; memcpy(res, data, dlen); switch (cx->mode) { case MODE_ECB: blowfish_encrypt_ecb(res, dlen, bfctx); break; case MODE_CBC: blowfish_encrypt_cbc(res, dlen, bfctx); break; } return 0; }
Definition at line 438 of file internal.c.
References int_ctx::bf, blowfish_setiv(), blowfish_setkey(), int_ctx::ctx, cx(), and px_cipher::ptr.
static unsigned bf_iv_size | ( | PX_Cipher * | c | ) | [static] |
Definition at line 432 of file internal.c.
{
return 8;
}
static unsigned bf_key_size | ( | PX_Cipher * | c | ) | [static] |
Definition at line 426 of file internal.c.
{
return 448 / 8;
}
static PX_Cipher* bf_load | ( | int | mode | ) | [static] |
Definition at line 500 of file internal.c.
References px_cipher::block_size, px_cipher::decrypt, px_cipher::encrypt, px_cipher::free, px_cipher::init, px_cipher::iv_size, px_cipher::key_size, int_ctx::mode, px_cipher::ptr, and px_alloc.
Referenced by bf_cbc_load(), and bf_ecb_load().
{ PX_Cipher *c; struct int_ctx *cx; c = px_alloc(sizeof *c); memset(c, 0, sizeof *c); c->block_size = bf_block_size; c->key_size = bf_key_size; c->iv_size = bf_iv_size; c->init = bf_init; c->encrypt = bf_encrypt; c->decrypt = bf_decrypt; c->free = intctx_free; cx = px_alloc(sizeof *cx); memset(cx, 0, sizeof *cx); cx->mode = mode; c->ptr = cx; return c; }
static void init_md5 | ( | PX_MD * | h | ) | [static] |
Definition at line 201 of file internal.c.
References px_digest::block_size, px_digest::finish, px_digest::free, MD5_CTX, px_digest::p, px_digest::ptr, px_alloc, px_digest::reset, px_digest::result_size, and px_digest::update.
static void init_sha1 | ( | PX_MD * | h | ) | [static] |
Definition at line 221 of file internal.c.
References px_digest::block_size, px_digest::finish, px_digest::free, px_digest::p, px_digest::ptr, px_alloc, px_digest::reset, px_digest::result_size, and px_digest::update.
void init_sha224 | ( | PX_MD * | h | ) |
Definition at line 239 of file internal-sha2.c.
References px_digest::block_size, px_digest::finish, px_digest::free, px_digest::p, px_digest::ptr, px_alloc, px_digest::reset, px_digest::result_size, and px_digest::update.
{ SHA224_CTX *ctx; ctx = px_alloc(sizeof(*ctx)); memset(ctx, 0, sizeof(*ctx)); md->p.ptr = ctx; md->result_size = int_sha224_len; md->block_size = int_sha224_block_len; md->reset = int_sha224_reset; md->update = int_sha224_update; md->finish = int_sha224_finish; md->free = int_sha224_free; md->reset(md); }
void init_sha256 | ( | PX_MD * | h | ) |
Definition at line 259 of file internal-sha2.c.
References px_digest::block_size, px_digest::finish, px_digest::free, px_digest::p, px_digest::ptr, px_alloc, px_digest::reset, px_digest::result_size, and px_digest::update.
{ SHA256_CTX *ctx; ctx = px_alloc(sizeof(*ctx)); memset(ctx, 0, sizeof(*ctx)); md->p.ptr = ctx; md->result_size = int_sha256_len; md->block_size = int_sha256_block_len; md->reset = int_sha256_reset; md->update = int_sha256_update; md->finish = int_sha256_finish; md->free = int_sha256_free; md->reset(md); }
void init_sha384 | ( | PX_MD * | h | ) |
Definition at line 279 of file internal-sha2.c.
References px_digest::block_size, px_digest::finish, px_digest::free, px_digest::p, px_digest::ptr, px_alloc, px_digest::reset, px_digest::result_size, and px_digest::update.
{ SHA384_CTX *ctx; ctx = px_alloc(sizeof(*ctx)); memset(ctx, 0, sizeof(*ctx)); md->p.ptr = ctx; md->result_size = int_sha384_len; md->block_size = int_sha384_block_len; md->reset = int_sha384_reset; md->update = int_sha384_update; md->finish = int_sha384_finish; md->free = int_sha384_free; md->reset(md); }
void init_sha512 | ( | PX_MD * | h | ) |
Definition at line 299 of file internal-sha2.c.
References px_digest::block_size, px_digest::finish, px_digest::free, px_digest::p, px_digest::ptr, px_alloc, px_digest::reset, px_digest::result_size, and px_digest::update.
{ SHA512_CTX *ctx; ctx = px_alloc(sizeof(*ctx)); memset(ctx, 0, sizeof(*ctx)); md->p.ptr = ctx; md->result_size = int_sha512_len; md->block_size = int_sha512_block_len; md->reset = int_sha512_reset; md->update = int_sha512_update; md->finish = int_sha512_finish; md->free = int_sha512_free; md->reset(md); }
static unsigned int_md5_block_len | ( | PX_MD * | h | ) | [static] |
Definition at line 111 of file internal.c.
{
return MD5_BLOCK_SIZE;
}
Definition at line 133 of file internal.c.
References MD5_CTX, MD5Final, px_digest::p, and px_digest::ptr.
static void int_md5_free | ( | PX_MD * | h | ) | [static] |
Definition at line 141 of file internal.c.
References MD5_CTX, px_digest::p, px_digest::ptr, and px_free.
static unsigned int_md5_len | ( | PX_MD * | h | ) | [static] |
Definition at line 105 of file internal.c.
{
return MD5_DIGEST_LENGTH;
}
static void int_md5_reset | ( | PX_MD * | h | ) | [static] |
Definition at line 125 of file internal.c.
References MD5_CTX, MD5Init, px_digest::p, and px_digest::ptr.
Definition at line 117 of file internal.c.
References MD5_CTX, MD5Update, px_digest::p, and px_digest::ptr.
static unsigned int_sha1_block_len | ( | PX_MD * | h | ) | [static] |
Definition at line 159 of file internal.c.
{
return SHA1_BLOCK_SIZE;
}
Definition at line 181 of file internal.c.
References px_digest::p, px_digest::ptr, and SHA1Final.
static void int_sha1_free | ( | PX_MD * | h | ) | [static] |
Definition at line 189 of file internal.c.
References px_digest::p, px_digest::ptr, and px_free.
static unsigned int_sha1_len | ( | PX_MD * | h | ) | [static] |
Definition at line 153 of file internal.c.
{
return SHA1_DIGEST_LENGTH;
}
static void int_sha1_reset | ( | PX_MD * | h | ) | [static] |
Definition at line 173 of file internal.c.
References px_digest::p, px_digest::ptr, and SHA1Init.
Definition at line 165 of file internal.c.
References px_digest::p, px_digest::ptr, and SHA1Update.
static void intctx_free | ( | PX_Cipher * | c | ) | [static] |
int px_add_entropy | ( | const uint8 * | data, | |
unsigned | count | |||
) |
Definition at line 683 of file internal.c.
Referenced by add_block_entropy().
{ system_reseed(); fortuna_add_entropy(data, count); return 0; }
int px_find_cipher | ( | const char * | name, | |
PX_Cipher ** | res | |||
) |
Definition at line 598 of file internal.c.
Referenced by pgp_load_cipher(), and px_find_combo().
{ int i; PX_Cipher *c = NULL; name = px_resolve_alias(int_aliases, name); for (i = 0; int_ciphers[i].name; i++) if (strcmp(int_ciphers[i].name, name) == 0) { c = int_ciphers[i].load(); break; } if (c == NULL) return PXE_NO_CIPHER; *res = c; return 0; }
int px_find_digest | ( | const char * | name, | |
PX_MD ** | res | |||
) |
Definition at line 579 of file internal.c.
Referenced by add_entropy(), pg_digest(), pgp_load_digest(), px_crypt_md5(), px_find_hmac(), and try_unix_std().
{ const struct int_digest *p; PX_MD *h; for (p = int_digest_list; p->name; p++) if (pg_strcasecmp(p->name, name) == 0) { h = px_alloc(sizeof(*h)); p->init(h); *res = h; return 0; } return PXE_NO_HASH; }
int px_get_pseudo_random_bytes | ( | uint8 * | dst, | |
unsigned | count | |||
) |
Definition at line 627 of file internal.c.
Referenced by pgp_s2k_fill(), and px_gen_salt().
{ return px_get_random_bytes(dst, count); }
int px_get_random_bytes | ( | uint8 * | dst, | |
unsigned | count | |||
) |
Definition at line 675 of file internal.c.
Referenced by add_entropy(), init_sess_key(), mp_px_rand(), pad_eme_pkcs1_v15(), pg_random_bytes(), px_get_pseudo_random_bytes(), system_reseed(), and write_prefix().
{ system_reseed(); fortuna_get_bytes(count, dst); return 0; }
static PX_Cipher* rj_128_cbc | ( | void | ) | [static] |
static PX_Cipher* rj_128_ecb | ( | void | ) | [static] |
static unsigned rj_block_size | ( | PX_Cipher * | c | ) | [static] |
Definition at line 282 of file internal.c.
{
return 128 / 8;
}
Definition at line 359 of file internal.c.
References aes_cbc_decrypt(), aes_ecb_decrypt(), int_ctx::ctx, cx(), int_ctx::is_init, int_ctx::iv, int_ctx::mode, MODE_CBC, px_cipher::ptr, PXE_CIPHER_INIT, int_ctx::rj, and rj_real_init().
{ struct int_ctx *cx = (struct int_ctx *) c->ptr; if (!cx->is_init) if (rj_real_init(cx, 0)) return PXE_CIPHER_INIT; if (dlen == 0) return 0; if (dlen & 15) return PXE_NOTBLOCKSIZE; memcpy(res, data, dlen); if (cx->mode == MODE_CBC) { aes_cbc_decrypt(&cx->ctx.rj, cx->iv, res, dlen); memcpy(cx->iv, data + dlen - 16, 16); } else aes_ecb_decrypt(&cx->ctx.rj, res, dlen); return 0; }
Definition at line 329 of file internal.c.
References aes_cbc_encrypt(), aes_ecb_encrypt(), int_ctx::ctx, cx(), int_ctx::is_init, int_ctx::iv, int_ctx::mode, MODE_CBC, px_cipher::ptr, PXE_CIPHER_INIT, int_ctx::rj, and rj_real_init().
{ struct int_ctx *cx = (struct int_ctx *) c->ptr; if (!cx->is_init) { if (rj_real_init(cx, 1)) return PXE_CIPHER_INIT; } if (dlen == 0) return 0; if (dlen & 15) return PXE_NOTBLOCKSIZE; memcpy(res, data, dlen); if (cx->mode == MODE_CBC) { aes_cbc_encrypt(&cx->ctx.rj, cx->iv, res, dlen); memcpy(cx->iv, res + dlen - 16, 16); } else aes_ecb_encrypt(&cx->ctx.rj, res, dlen); return 0; }
Definition at line 300 of file internal.c.
References cx(), int_ctx::iv, int_ctx::keybuf, int_ctx::keylen, and px_cipher::ptr.
static unsigned rj_iv_size | ( | PX_Cipher * | c | ) | [static] |
Definition at line 294 of file internal.c.
{
return 128 / 8;
}
static unsigned rj_key_size | ( | PX_Cipher * | c | ) | [static] |
Definition at line 288 of file internal.c.
{
return 256 / 8;
}
static PX_Cipher* rj_load | ( | int | mode | ) | [static] |
Definition at line 391 of file internal.c.
References px_cipher::block_size, px_cipher::decrypt, px_cipher::encrypt, px_cipher::free, px_cipher::init, px_cipher::iv_size, px_cipher::key_size, int_ctx::mode, px_cipher::ptr, and px_alloc.
Referenced by rj_128_cbc(), and rj_128_ecb().
{ PX_Cipher *c; struct int_ctx *cx; c = px_alloc(sizeof *c); memset(c, 0, sizeof *c); c->block_size = rj_block_size; c->key_size = rj_key_size; c->iv_size = rj_iv_size; c->init = rj_init; c->encrypt = rj_encrypt; c->decrypt = rj_decrypt; c->free = intctx_free; cx = px_alloc(sizeof *cx); memset(cx, 0, sizeof *cx); cx->mode = mode; c->ptr = cx; return c; }
static int rj_real_init | ( | struct int_ctx * | cx, | |
int | dir | |||
) | [static] |
Definition at line 322 of file internal.c.
References aes_set_key(), int_ctx::ctx, int_ctx::keybuf, int_ctx::keylen, and int_ctx::rj.
Referenced by rj_decrypt(), and rj_encrypt().
{ aes_set_key(&cx->ctx.rj, cx->keybuf, cx->keylen * 8, dir); return 0; }
static void system_reseed | ( | void | ) | [static] |
Definition at line 636 of file internal.c.
References buf, check_time, fortuna_add_entropy(), NULL, px_acquire_system_randomness(), px_get_random_bytes(), seed_time, skip(), SYSTEM_RESEED_CHECK_TIME, SYSTEM_RESEED_MAX, and SYSTEM_RESEED_MIN.
Referenced by px_add_entropy(), and px_get_random_bytes().
{ uint8 buf[1024]; int n; time_t t; int skip = 1; t = time(NULL); if (seed_time == 0) skip = 0; else if ((t - seed_time) < SYSTEM_RESEED_MIN) skip = 1; else if ((t - seed_time) > SYSTEM_RESEED_MAX) skip = 0; else if (check_time == 0 || (t - check_time) > SYSTEM_RESEED_CHECK_TIME) { check_time = t; /* roll dice */ px_get_random_bytes(buf, 1); skip = buf[0] >= SYSTEM_RESEED_CHANCE; } /* clear 1 byte */ memset(buf, 0, sizeof(buf)); if (skip) return; n = px_acquire_system_randomness(buf); if (n > 0) fortuna_add_entropy(buf, n); seed_time = t; memset(buf, 0, sizeof(buf)); }
time_t check_time = 0 [static] |
Definition at line 633 of file internal.c.
Referenced by system_reseed().
const PX_Alias int_aliases[] [static] |
{ {"bf", "bf-cbc"}, {"blowfish", "bf-cbc"}, {"aes", "aes-128-cbc"}, {"aes-ecb", "aes-128-ecb"}, {"aes-cbc", "aes-128-cbc"}, {"aes-128", "aes-128-cbc"}, {"rijndael", "aes-128-cbc"}, {"rijndael-128", "aes-128-cbc"}, {NULL, NULL} }
Definition at line 564 of file internal.c.
struct int_cipher int_ciphers[] [static] |
{ {"bf-cbc", bf_cbc_load}, {"bf-ecb", bf_ecb_load}, {"aes-128-cbc", rj_128_cbc}, {"aes-128-ecb", rj_128_ecb}, {NULL, NULL} }
Definition at line 556 of file internal.c.
struct int_digest int_digest_list[] [static] |
{ {"md5", init_md5}, {"sha1", init_sha1}, {"sha224", init_sha224}, {"sha256", init_sha256}, {"sha384", init_sha384}, {"sha512", init_sha512}, {NULL, NULL} }
Definition at line 92 of file internal.c.
time_t seed_time = 0 [static] |
Definition at line 632 of file internal.c.
Referenced by system_reseed().