Skip to content

Commit 19b6768

Browse files
author
Neil Conway
committed
pgcrypto update:
Reserve px_get_random_bytes() for strong randomness, add new function px_get_pseudo_random_bytes() for weak randomness and use it in gen_salt(). On openssl case, use RAND_pseudo_bytes() for px_get_pseudo_random_bytes(). Final result is that is user has not configured random souce but kept the 'silly' one, gen_salt() keeps working, but pgp_encrypt() will throw error. Marko Kreen
1 parent 1ea9169 commit 19b6768

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

contrib/pgcrypto/px-crypt.c

Lines changed: 2 additions & 2 deletions
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-crypt.c,v 1.10 2005/03/21 05:19:55 neilc Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/px-crypt.c,v 1.11 2005/03/21 05:22:14 neilc Exp $
3030
*/
3131

3232
#include <postgres.h>
@@ -171,7 +171,7 @@ px_gen_salt(const char *salt_type, char *buf, int rounds)
171171
return PXE_BAD_SALT_ROUNDS;
172172
}
173173

174-
res = px_get_random_bytes(rbuf, g->input_len);
174+
res = px_get_pseudo_random_bytes(rbuf, g->input_len);
175175
if (res < 0)
176176
return res;
177177

contrib/pgcrypto/px.c

Lines changed: 2 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.c,v 1.10 2005/03/21 05:19:55 neilc Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/px.c,v 1.11 2005/03/21 05:22:14 neilc Exp $
3030
*/
3131

3232
#include <postgres.h>
@@ -56,6 +56,7 @@ static const struct error_desc px_err_list[] = {
5656
{PXE_UNKNOWN_SALT_ALGO, "Unknown salt algorithm"},
5757
{PXE_BAD_SALT_ROUNDS, "Incorrect number of rounds"},
5858
{PXE_MCRYPT_INTERNAL, "mcrypt internal error"},
59+
{PXE_NO_RANDOM, "No strong random source"},
5960
{0, NULL},
6061
};
6162

contrib/pgcrypto/px.h

Lines changed: 3 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.11 2005/03/21 05:19:55 neilc Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.12 2005/03/21 05:22:14 neilc Exp $
3030
*/
3131

3232
#ifndef __PX_H
@@ -83,6 +83,7 @@ void px_free(void *p);
8383
#define PXE_UNKNOWN_SALT_ALGO -14
8484
#define PXE_BAD_SALT_ROUNDS -15
8585
#define PXE_MCRYPT_INTERNAL -16
86+
#define PXE_NO_RANDOM -17
8687

8788
typedef struct px_digest PX_MD;
8889
typedef struct px_alias PX_Alias;
@@ -168,6 +169,7 @@ int px_find_cipher(const char *name, PX_Cipher ** res);
168169
int px_find_combo(const char *name, PX_Combo ** res);
169170

170171
int px_get_random_bytes(uint8 *dst, unsigned count);
172+
int px_get_pseudo_random_bytes(uint8 *dst, unsigned count);
171173

172174
const char *px_strerror(int err);
173175

contrib/pgcrypto/random.c

Lines changed: 41 additions & 12 deletions
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/random.c,v 1.9 2005/03/21 05:19:55 neilc Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/random.c,v 1.10 2005/03/21 05:22:14 neilc Exp $
3030
*/
3131

3232

@@ -78,10 +78,16 @@ px_get_random_bytes(uint8 *dst, unsigned count)
7878
return res;
7979
}
8080

81+
int
82+
px_get_pseudo_random_bytes(uint8 *dst, unsigned count)
83+
{
84+
return px_get_random_bytes(dst, count);
85+
}
86+
8187
#elif defined(RAND_SILLY)
8288

8389
int
84-
px_get_random_bytes(uint8 *dst, unsigned count)
90+
px_get_pseudo_random_bytes(uint8 *dst, unsigned count)
8591
{
8692
int i;
8793

@@ -90,6 +96,12 @@ px_get_random_bytes(uint8 *dst, unsigned count)
9096
return i;
9197
}
9298

99+
int
100+
px_get_random_bytes(uint8 *dst, unsigned count)
101+
{
102+
return PXE_NO_RANDOM;
103+
}
104+
93105
#elif defined(RAND_OPENSSL)
94106

95107
#include <openssl/evp.h>
@@ -99,22 +111,24 @@ px_get_random_bytes(uint8 *dst, unsigned count)
99111

100112
static int openssl_random_init = 0;
101113

114+
/*
115+
* OpenSSL random should re-feeded occasionally. From /dev/urandom
116+
* preferably.
117+
*/
118+
static void init_openssl()
119+
{
120+
if (RAND_get_rand_method() == NULL)
121+
RAND_set_rand_method(RAND_SSLeay());
122+
openssl_random_init = 1;
123+
}
124+
102125
int
103126
px_get_random_bytes(uint8 *dst, unsigned count)
104127
{
105128
int res;
106129

107130
if (!openssl_random_init)
108-
{
109-
if (RAND_get_rand_method() == NULL)
110-
RAND_set_rand_method(RAND_SSLeay());
111-
openssl_random_init = 1;
112-
}
113-
114-
/*
115-
* OpenSSL random should re-feeded occasionally. From /dev/urandom
116-
* preferably.
117-
*/
131+
init_openssl();
118132

119133
res = RAND_bytes(dst, count);
120134
if (res == 1)
@@ -123,6 +137,21 @@ px_get_random_bytes(uint8 *dst, unsigned count)
123137
return PXE_OSSL_RAND_ERROR;
124138
}
125139

140+
int
141+
px_get_pseudo_random_bytes(uint8 *dst, unsigned count)
142+
{
143+
int res;
144+
145+
if (!openssl_random_init)
146+
init_openssl();
147+
148+
res = RAND_pseudo_bytes(dst, count);
149+
if (res == 0 || res == 1)
150+
return count;
151+
152+
return PXE_OSSL_RAND_ERROR;
153+
}
154+
126155
#else
127156
#error "Invalid random source"
128157
#endif

0 commit comments

Comments
 (0)