Skip to content

Commit 4fcf8b1

Browse files
committed
- Add Fortuna PRNG to pgcrypto.
- Move openssl random provider to openssl.c and builtin provider to internal.c - Make px_random_bytes use Fortuna, instead of giving error. - Retarget random.c to aquiring system randomness, for initial seeding of Fortuna. There is ATM 2 functions for Windows, reader from /dev/urandom and the regular time()/getpid() silliness. Marko Kreen
1 parent 248eeb8 commit 4fcf8b1

File tree

5 files changed

+284
-77
lines changed

5 files changed

+284
-77
lines changed

contrib/pgcrypto/Makefile

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
#
2-
# $PostgreSQL: pgsql/contrib/pgcrypto/Makefile,v 1.18 2005/07/10 03:52:56 momjian Exp $
2+
# $PostgreSQL: pgsql/contrib/pgcrypto/Makefile,v 1.19 2005/07/10 03:55:28 momjian Exp $
33
#
44

5-
# if you don't have OpenSSL, you can use libc random() or /dev/urandom
6-
INT_CFLAGS = -DRAND_SILLY
7-
#INT_CFLAGS = -DRAND_DEV=\"/dev/urandom\"
8-
9-
INT_SRCS = md5.c sha1.c sha2.c internal.c blf.c rijndael.c
5+
INT_SRCS = md5.c sha1.c sha2.c internal.c blf.c rijndael.c \
6+
fortuna.c random.c
107
INT_TESTS = sha2
118

12-
OSSL_CFLAGS = -DRAND_OPENSSL
139
OSSL_SRCS = openssl.c
1410
OSSL_TESTS = des 3des cast5
1511

1612
CF_SRCS = $(if $(subst no,,$(with_openssl)), $(OSSL_SRCS), $(INT_SRCS))
1713
CF_TESTS = $(if $(subst no,,$(with_openssl)), $(OSSL_TESTS), $(INT_TESTS))
18-
CF_CFLAGS = $(if $(subst no,,$(with_openssl)), $(OSSL_CFLAGS), $(INT_CFLAGS))
14+
CF_CFLAGS =
1915

2016
PG_CPPFLAGS = $(CF_CFLAGS)
2117

22-
SRCS = pgcrypto.c px.c px-hmac.c px-crypt.c misc.c random.c \
18+
SRCS = pgcrypto.c px.c px-hmac.c px-crypt.c misc.c \
2319
crypt-gensalt.c crypt-blowfish.c crypt-des.c \
2420
crypt-md5.c $(CF_SRCS)
2521

contrib/pgcrypto/internal.c

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.17 2005/07/10 03:52:56 momjian Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.18 2005/07/10 03:55:28 momjian Exp $
3030
*/
3131

3232

3333
#include <postgres.h>
34+
#include <time.h>
3435

3536
#include "px.h"
3637

@@ -39,6 +40,13 @@
3940
#include "sha2.h"
4041
#include "blf.h"
4142
#include "rijndael.h"
43+
#include "fortuna.h"
44+
45+
/*
46+
* How often to try to acquire system entropy. (In seconds)
47+
*/
48+
#define SYSTEM_RESEED_FREQ (3*60*60)
49+
4250

4351
#ifndef MD5_DIGEST_LENGTH
4452
#define MD5_DIGEST_LENGTH 16
@@ -784,3 +792,58 @@ px_find_cipher(const char *name, PX_Cipher ** res)
784792
*res = c;
785793
return 0;
786794
}
795+
796+
/*
797+
* Randomness provider
798+
*/
799+
800+
/*
801+
* Use libc for all 'public' bytes.
802+
*
803+
* That way we don't expose bytes from Fortuna
804+
* to the public, in case it has some bugs.
805+
*/
806+
int
807+
px_get_pseudo_random_bytes(uint8 *dst, unsigned count)
808+
{
809+
int i;
810+
811+
for (i = 0; i < count; i++)
812+
*dst++ = random();
813+
return i;
814+
}
815+
816+
static time_t seed_time = 0;
817+
static void system_reseed()
818+
{
819+
uint8 buf[1024];
820+
int n;
821+
time_t t;
822+
823+
t = time(NULL);
824+
if (seed_time && (t - seed_time) < SYSTEM_RESEED_FREQ)
825+
return;
826+
827+
n = px_acquire_system_randomness(buf);
828+
if (n > 0)
829+
fortuna_add_entropy(SYSTEM_ENTROPY, buf, n);
830+
831+
seed_time = t;
832+
}
833+
834+
int
835+
px_get_random_bytes(uint8 *dst, unsigned count)
836+
{
837+
system_reseed();
838+
fortuna_get_bytes(count, dst);
839+
return 0;
840+
}
841+
842+
int
843+
px_add_entropy(const uint8 *data, unsigned count)
844+
{
845+
system_reseed();
846+
fortuna_add_entropy(USER_ENTROPY, data, count);
847+
return 0;
848+
}
849+

contrib/pgcrypto/openssl.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.20 2005/07/05 18:15:36 tgl Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.21 2005/07/10 03:55:28 momjian Exp $
3030
*/
3131

3232
#include <postgres.h>
@@ -37,6 +37,9 @@
3737
#include <openssl/blowfish.h>
3838
#include <openssl/cast.h>
3939
#include <openssl/des.h>
40+
#include <openssl/rand.h>
41+
#include <openssl/err.h>
42+
4043

4144
/*
4245
* Does OpenSSL support AES?
@@ -759,3 +762,58 @@ px_find_cipher(const char *name, PX_Cipher ** res)
759762
*res = c;
760763
return 0;
761764
}
765+
766+
767+
static int openssl_random_init = 0;
768+
769+
/*
770+
* OpenSSL random should re-feeded occasionally. From /dev/urandom
771+
* preferably.
772+
*/
773+
static void init_openssl_rand()
774+
{
775+
if (RAND_get_rand_method() == NULL)
776+
RAND_set_rand_method(RAND_SSLeay());
777+
openssl_random_init = 1;
778+
}
779+
780+
int
781+
px_get_random_bytes(uint8 *dst, unsigned count)
782+
{
783+
int res;
784+
785+
if (!openssl_random_init)
786+
init_openssl_rand();
787+
788+
res = RAND_bytes(dst, count);
789+
if (res == 1)
790+
return count;
791+
792+
return PXE_OSSL_RAND_ERROR;
793+
}
794+
795+
int
796+
px_get_pseudo_random_bytes(uint8 *dst, unsigned count)
797+
{
798+
int res;
799+
800+
if (!openssl_random_init)
801+
init_openssl_rand();
802+
803+
res = RAND_pseudo_bytes(dst, count);
804+
if (res == 0 || res == 1)
805+
return count;
806+
807+
return PXE_OSSL_RAND_ERROR;
808+
}
809+
810+
int
811+
px_add_entropy(const uint8 *data, unsigned count)
812+
{
813+
/*
814+
* estimate 0 bits
815+
*/
816+
RAND_add(data, count, 0);
817+
return 0;
818+
}
819+

contrib/pgcrypto/px.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.12 2005/03/21 05:22:14 neilc Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.13 2005/07/10 03:55:28 momjian Exp $
3030
*/
3131

3232
#ifndef __PX_H
@@ -170,6 +170,9 @@ int px_find_combo(const char *name, PX_Combo ** res);
170170

171171
int px_get_random_bytes(uint8 *dst, unsigned count);
172172
int px_get_pseudo_random_bytes(uint8 *dst, unsigned count);
173+
int px_add_entropy(const uint8 *data, unsigned count);
174+
175+
unsigned px_acquire_system_randomness(uint8 *dst);
173176

174177
const char *px_strerror(int err);
175178

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy