2005-07-10 21:46:29 +08:00
|
|
|
/*
|
|
|
|
* pgp-s2k.c
|
|
|
|
* OpenPGP string2key functions.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Marko Kreen
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2014-05-07 00:12:18 +08:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
2005-07-10 21:46:29 +08:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pgcrypto/pgp-s2k.c
|
2005-07-10 21:46:29 +08:00
|
|
|
*/
|
|
|
|
|
2005-07-11 23:07:59 +08:00
|
|
|
#include "postgres.h"
|
2005-07-10 21:46:29 +08:00
|
|
|
|
|
|
|
#include "pgp.h"
|
2019-10-23 11:56:22 +08:00
|
|
|
#include "px.h"
|
Replace PostmasterRandom() with a stronger source, second attempt.
This adds a new routine, pg_strong_random() for generating random bytes,
for use in both frontend and backend. At the moment, it's only used in
the backend, but the upcoming SCRAM authentication patches need strong
random numbers in libpq as well.
pg_strong_random() is based on, and replaces, the existing implementation
in pgcrypto. It can acquire strong random numbers from a number of sources,
depending on what's available:
- OpenSSL RAND_bytes(), if built with OpenSSL
- On Windows, the native cryptographic functions are used
- /dev/urandom
Unlike the current pgcrypto function, the source is chosen by configure.
That makes it easier to test different implementations, and ensures that
we don't accidentally fall back to a less secure implementation, if the
primary source fails. All of those methods are quite reliable, it would be
pretty surprising for them to fail, so we'd rather find out by failing
hard.
If no strong random source is available, we fall back to using erand48(),
seeded from current timestamp, like PostmasterRandom() was. That isn't
cryptographically secure, but allows us to still work on platforms that
don't have any of the above stronger sources. Because it's not very secure,
the built-in implementation is only used if explicitly requested with
--disable-strong-random.
This replaces the more complicated Fortuna algorithm we used to have in
pgcrypto, which is unfortunate, but all modern platforms have /dev/urandom,
so it doesn't seem worth the maintenance effort to keep that. pgcrypto
functions that require strong random numbers will be disabled with
--disable-strong-random.
Original patch by Magnus Hagander, tons of further work by Michael Paquier
and me.
Discussion: https://www.postgresql.org/message-id/CAB7nPqRy3krN8quR9XujMVVHYtXJ0_60nqgVc6oUk8ygyVkZsA@mail.gmail.com
Discussion: https://www.postgresql.org/message-id/CAB7nPqRWkNYRRPJA7-cF+LfroYV10pvjdz6GNvxk-Eee9FypKA@mail.gmail.com
2016-12-05 19:42:59 +08:00
|
|
|
|
2005-07-10 21:46:29 +08:00
|
|
|
static int
|
2009-06-11 22:49:15 +08:00
|
|
|
calc_s2k_simple(PGP_S2K *s2k, PX_MD *md, const uint8 *key,
|
2005-07-10 21:46:29 +08:00
|
|
|
unsigned key_len)
|
|
|
|
{
|
2011-04-12 03:28:45 +08:00
|
|
|
unsigned md_rlen;
|
2005-07-10 21:46:29 +08:00
|
|
|
uint8 buf[PGP_MAX_DIGEST];
|
|
|
|
unsigned preload;
|
|
|
|
unsigned remain;
|
|
|
|
uint8 *dst = s2k->key;
|
|
|
|
|
|
|
|
md_rlen = px_md_result_size(md);
|
|
|
|
|
|
|
|
remain = s2k->key_len;
|
|
|
|
preload = 0;
|
|
|
|
while (remain > 0)
|
|
|
|
{
|
|
|
|
px_md_reset(md);
|
|
|
|
|
|
|
|
if (preload)
|
|
|
|
{
|
|
|
|
memset(buf, 0, preload);
|
|
|
|
px_md_update(md, buf, preload);
|
|
|
|
}
|
|
|
|
preload++;
|
|
|
|
|
|
|
|
px_md_update(md, key, key_len);
|
|
|
|
px_md_finish(md, buf);
|
|
|
|
|
|
|
|
if (remain > md_rlen)
|
|
|
|
{
|
|
|
|
memcpy(dst, buf, md_rlen);
|
|
|
|
dst += md_rlen;
|
|
|
|
remain -= md_rlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(dst, buf, remain);
|
|
|
|
remain = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-04-18 00:37:53 +08:00
|
|
|
px_memset(buf, 0, sizeof(buf));
|
2005-07-10 21:46:29 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-06-11 22:49:15 +08:00
|
|
|
calc_s2k_salted(PGP_S2K *s2k, PX_MD *md, const uint8 *key, unsigned key_len)
|
2005-07-10 21:46:29 +08:00
|
|
|
{
|
2011-04-12 03:28:45 +08:00
|
|
|
unsigned md_rlen;
|
2005-07-10 21:46:29 +08:00
|
|
|
uint8 buf[PGP_MAX_DIGEST];
|
|
|
|
unsigned preload = 0;
|
|
|
|
uint8 *dst;
|
|
|
|
unsigned remain;
|
|
|
|
|
|
|
|
md_rlen = px_md_result_size(md);
|
|
|
|
|
|
|
|
dst = s2k->key;
|
|
|
|
remain = s2k->key_len;
|
|
|
|
while (remain > 0)
|
|
|
|
{
|
|
|
|
px_md_reset(md);
|
|
|
|
|
|
|
|
if (preload > 0)
|
|
|
|
{
|
|
|
|
memset(buf, 0, preload);
|
|
|
|
px_md_update(md, buf, preload);
|
|
|
|
}
|
|
|
|
preload++;
|
|
|
|
|
|
|
|
px_md_update(md, s2k->salt, PGP_S2K_SALT);
|
|
|
|
px_md_update(md, key, key_len);
|
|
|
|
px_md_finish(md, buf);
|
|
|
|
|
|
|
|
if (remain > md_rlen)
|
|
|
|
{
|
|
|
|
memcpy(dst, buf, md_rlen);
|
|
|
|
remain -= md_rlen;
|
|
|
|
dst += md_rlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(dst, buf, remain);
|
|
|
|
remain = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-04-18 00:37:53 +08:00
|
|
|
px_memset(buf, 0, sizeof(buf));
|
2005-07-10 21:46:29 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-06-11 22:49:15 +08:00
|
|
|
calc_s2k_iter_salted(PGP_S2K *s2k, PX_MD *md, const uint8 *key,
|
2005-10-15 10:49:52 +08:00
|
|
|
unsigned key_len)
|
2005-07-10 21:46:29 +08:00
|
|
|
{
|
2011-04-12 03:28:45 +08:00
|
|
|
unsigned md_rlen;
|
2005-07-10 21:46:29 +08:00
|
|
|
uint8 buf[PGP_MAX_DIGEST];
|
|
|
|
uint8 *dst;
|
|
|
|
unsigned preload = 0;
|
|
|
|
unsigned remain,
|
|
|
|
c,
|
|
|
|
curcnt,
|
|
|
|
count;
|
|
|
|
|
2016-03-10 01:31:07 +08:00
|
|
|
count = s2k_decode_count(s2k->iter);
|
2005-07-10 21:46:29 +08:00
|
|
|
|
|
|
|
md_rlen = px_md_result_size(md);
|
|
|
|
|
|
|
|
remain = s2k->key_len;
|
|
|
|
dst = s2k->key;
|
|
|
|
while (remain > 0)
|
|
|
|
{
|
|
|
|
px_md_reset(md);
|
|
|
|
|
|
|
|
if (preload)
|
|
|
|
{
|
|
|
|
memset(buf, 0, preload);
|
|
|
|
px_md_update(md, buf, preload);
|
|
|
|
}
|
|
|
|
preload++;
|
|
|
|
|
|
|
|
px_md_update(md, s2k->salt, PGP_S2K_SALT);
|
|
|
|
px_md_update(md, key, key_len);
|
|
|
|
curcnt = PGP_S2K_SALT + key_len;
|
|
|
|
|
|
|
|
while (curcnt < count)
|
|
|
|
{
|
|
|
|
if (curcnt + PGP_S2K_SALT < count)
|
|
|
|
c = PGP_S2K_SALT;
|
|
|
|
else
|
|
|
|
c = count - curcnt;
|
|
|
|
px_md_update(md, s2k->salt, c);
|
|
|
|
curcnt += c;
|
|
|
|
|
|
|
|
if (curcnt + key_len < count)
|
|
|
|
c = key_len;
|
|
|
|
else if (curcnt < count)
|
|
|
|
c = count - curcnt;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
px_md_update(md, key, c);
|
|
|
|
curcnt += c;
|
|
|
|
}
|
|
|
|
px_md_finish(md, buf);
|
|
|
|
|
|
|
|
if (remain > md_rlen)
|
|
|
|
{
|
|
|
|
memcpy(dst, buf, md_rlen);
|
|
|
|
remain -= md_rlen;
|
|
|
|
dst += md_rlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(dst, buf, remain);
|
|
|
|
remain = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-04-18 00:37:53 +08:00
|
|
|
px_memset(buf, 0, sizeof(buf));
|
2005-07-10 21:46:29 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-03-10 01:31:07 +08:00
|
|
|
* Decide PGP_S2K_ISALTED iteration count (in OpenPGP one-byte representation)
|
2005-10-15 10:49:52 +08:00
|
|
|
*
|
2005-07-10 21:46:29 +08:00
|
|
|
* Too small: weak
|
|
|
|
* Too big: slow
|
|
|
|
* gpg defaults to 96 => 65536 iters
|
2016-03-10 01:31:07 +08:00
|
|
|
*
|
|
|
|
* For our default (count=-1) we let it float a bit: 96 + 32 => between 65536
|
|
|
|
* and 262144 iterations.
|
|
|
|
*
|
|
|
|
* Otherwise, find the smallest number which provides at least the specified
|
|
|
|
* iteration count.
|
2005-07-10 21:46:29 +08:00
|
|
|
*/
|
2016-03-10 01:31:07 +08:00
|
|
|
static uint8
|
|
|
|
decide_s2k_iter(unsigned rand_byte, int count)
|
2005-07-10 21:46:29 +08:00
|
|
|
{
|
2016-03-10 01:31:07 +08:00
|
|
|
int iter;
|
|
|
|
|
|
|
|
if (count == -1)
|
|
|
|
return 96 + (rand_byte & 0x1F);
|
|
|
|
/* this is a bit brute-force, but should be quick enough */
|
|
|
|
for (iter = 0; iter <= 255; iter++)
|
|
|
|
if (s2k_decode_count(iter) >= count)
|
|
|
|
return iter;
|
|
|
|
return 255;
|
2005-07-10 21:46:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-03-10 01:31:07 +08:00
|
|
|
pgp_s2k_fill(PGP_S2K *s2k, int mode, int digest_algo, int count)
|
2005-07-10 21:46:29 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
int res = 0;
|
|
|
|
uint8 tmp;
|
2005-07-10 21:46:29 +08:00
|
|
|
|
|
|
|
s2k->mode = mode;
|
|
|
|
s2k->digest_algo = digest_algo;
|
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
switch (s2k->mode)
|
|
|
|
{
|
2016-03-10 01:31:07 +08:00
|
|
|
case PGP_S2K_SIMPLE:
|
2005-07-10 21:46:29 +08:00
|
|
|
break;
|
2016-03-10 01:31:07 +08:00
|
|
|
case PGP_S2K_SALTED:
|
2019-01-01 19:05:51 +08:00
|
|
|
if (!pg_strong_random(s2k->salt, PGP_S2K_SALT))
|
Replace PostmasterRandom() with a stronger source, second attempt.
This adds a new routine, pg_strong_random() for generating random bytes,
for use in both frontend and backend. At the moment, it's only used in
the backend, but the upcoming SCRAM authentication patches need strong
random numbers in libpq as well.
pg_strong_random() is based on, and replaces, the existing implementation
in pgcrypto. It can acquire strong random numbers from a number of sources,
depending on what's available:
- OpenSSL RAND_bytes(), if built with OpenSSL
- On Windows, the native cryptographic functions are used
- /dev/urandom
Unlike the current pgcrypto function, the source is chosen by configure.
That makes it easier to test different implementations, and ensures that
we don't accidentally fall back to a less secure implementation, if the
primary source fails. All of those methods are quite reliable, it would be
pretty surprising for them to fail, so we'd rather find out by failing
hard.
If no strong random source is available, we fall back to using erand48(),
seeded from current timestamp, like PostmasterRandom() was. That isn't
cryptographically secure, but allows us to still work on platforms that
don't have any of the above stronger sources. Because it's not very secure,
the built-in implementation is only used if explicitly requested with
--disable-strong-random.
This replaces the more complicated Fortuna algorithm we used to have in
pgcrypto, which is unfortunate, but all modern platforms have /dev/urandom,
so it doesn't seem worth the maintenance effort to keep that. pgcrypto
functions that require strong random numbers will be disabled with
--disable-strong-random.
Original patch by Magnus Hagander, tons of further work by Michael Paquier
and me.
Discussion: https://www.postgresql.org/message-id/CAB7nPqRy3krN8quR9XujMVVHYtXJ0_60nqgVc6oUk8ygyVkZsA@mail.gmail.com
Discussion: https://www.postgresql.org/message-id/CAB7nPqRWkNYRRPJA7-cF+LfroYV10pvjdz6GNvxk-Eee9FypKA@mail.gmail.com
2016-12-05 19:42:59 +08:00
|
|
|
return PXE_NO_RANDOM;
|
2005-07-10 21:46:29 +08:00
|
|
|
break;
|
2016-03-10 01:31:07 +08:00
|
|
|
case PGP_S2K_ISALTED:
|
2019-01-01 19:05:51 +08:00
|
|
|
if (!pg_strong_random(s2k->salt, PGP_S2K_SALT))
|
Replace PostmasterRandom() with a stronger source, second attempt.
This adds a new routine, pg_strong_random() for generating random bytes,
for use in both frontend and backend. At the moment, it's only used in
the backend, but the upcoming SCRAM authentication patches need strong
random numbers in libpq as well.
pg_strong_random() is based on, and replaces, the existing implementation
in pgcrypto. It can acquire strong random numbers from a number of sources,
depending on what's available:
- OpenSSL RAND_bytes(), if built with OpenSSL
- On Windows, the native cryptographic functions are used
- /dev/urandom
Unlike the current pgcrypto function, the source is chosen by configure.
That makes it easier to test different implementations, and ensures that
we don't accidentally fall back to a less secure implementation, if the
primary source fails. All of those methods are quite reliable, it would be
pretty surprising for them to fail, so we'd rather find out by failing
hard.
If no strong random source is available, we fall back to using erand48(),
seeded from current timestamp, like PostmasterRandom() was. That isn't
cryptographically secure, but allows us to still work on platforms that
don't have any of the above stronger sources. Because it's not very secure,
the built-in implementation is only used if explicitly requested with
--disable-strong-random.
This replaces the more complicated Fortuna algorithm we used to have in
pgcrypto, which is unfortunate, but all modern platforms have /dev/urandom,
so it doesn't seem worth the maintenance effort to keep that. pgcrypto
functions that require strong random numbers will be disabled with
--disable-strong-random.
Original patch by Magnus Hagander, tons of further work by Michael Paquier
and me.
Discussion: https://www.postgresql.org/message-id/CAB7nPqRy3krN8quR9XujMVVHYtXJ0_60nqgVc6oUk8ygyVkZsA@mail.gmail.com
Discussion: https://www.postgresql.org/message-id/CAB7nPqRWkNYRRPJA7-cF+LfroYV10pvjdz6GNvxk-Eee9FypKA@mail.gmail.com
2016-12-05 19:42:59 +08:00
|
|
|
return PXE_NO_RANDOM;
|
2019-01-01 19:05:51 +08:00
|
|
|
if (!pg_strong_random(&tmp, 1))
|
Replace PostmasterRandom() with a stronger source, second attempt.
This adds a new routine, pg_strong_random() for generating random bytes,
for use in both frontend and backend. At the moment, it's only used in
the backend, but the upcoming SCRAM authentication patches need strong
random numbers in libpq as well.
pg_strong_random() is based on, and replaces, the existing implementation
in pgcrypto. It can acquire strong random numbers from a number of sources,
depending on what's available:
- OpenSSL RAND_bytes(), if built with OpenSSL
- On Windows, the native cryptographic functions are used
- /dev/urandom
Unlike the current pgcrypto function, the source is chosen by configure.
That makes it easier to test different implementations, and ensures that
we don't accidentally fall back to a less secure implementation, if the
primary source fails. All of those methods are quite reliable, it would be
pretty surprising for them to fail, so we'd rather find out by failing
hard.
If no strong random source is available, we fall back to using erand48(),
seeded from current timestamp, like PostmasterRandom() was. That isn't
cryptographically secure, but allows us to still work on platforms that
don't have any of the above stronger sources. Because it's not very secure,
the built-in implementation is only used if explicitly requested with
--disable-strong-random.
This replaces the more complicated Fortuna algorithm we used to have in
pgcrypto, which is unfortunate, but all modern platforms have /dev/urandom,
so it doesn't seem worth the maintenance effort to keep that. pgcrypto
functions that require strong random numbers will be disabled with
--disable-strong-random.
Original patch by Magnus Hagander, tons of further work by Michael Paquier
and me.
Discussion: https://www.postgresql.org/message-id/CAB7nPqRy3krN8quR9XujMVVHYtXJ0_60nqgVc6oUk8ygyVkZsA@mail.gmail.com
Discussion: https://www.postgresql.org/message-id/CAB7nPqRWkNYRRPJA7-cF+LfroYV10pvjdz6GNvxk-Eee9FypKA@mail.gmail.com
2016-12-05 19:42:59 +08:00
|
|
|
return PXE_NO_RANDOM;
|
2016-03-10 01:31:07 +08:00
|
|
|
s2k->iter = decide_s2k_iter(tmp, count);
|
2005-07-10 21:46:29 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = PXE_PGP_BAD_S2K_MODE;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-06-11 22:49:15 +08:00
|
|
|
pgp_s2k_read(PullFilter *src, PGP_S2K *s2k)
|
2005-07-10 21:46:29 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
int res = 0;
|
2005-07-10 21:46:29 +08:00
|
|
|
|
|
|
|
GETBYTE(src, s2k->mode);
|
|
|
|
GETBYTE(src, s2k->digest_algo);
|
2005-10-15 10:49:52 +08:00
|
|
|
switch (s2k->mode)
|
|
|
|
{
|
2005-07-10 21:46:29 +08:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
res = pullf_read_fixed(src, 8, s2k->salt);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
res = pullf_read_fixed(src, 8, s2k->salt);
|
|
|
|
if (res < 0)
|
|
|
|
break;
|
|
|
|
GETBYTE(src, s2k->iter);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = PXE_PGP_BAD_S2K_MODE;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
int
|
2009-06-11 22:49:15 +08:00
|
|
|
pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int key_len)
|
2005-07-10 21:46:29 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
int res;
|
|
|
|
PX_MD *md;
|
2005-07-10 21:46:29 +08:00
|
|
|
|
|
|
|
s2k->key_len = pgp_get_cipher_key_size(cipher);
|
|
|
|
if (s2k->key_len <= 0)
|
|
|
|
return PXE_PGP_UNSUPPORTED_CIPHER;
|
|
|
|
|
|
|
|
res = pgp_load_digest(s2k->digest_algo, &md);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
switch (s2k->mode)
|
|
|
|
{
|
2005-07-10 21:46:29 +08:00
|
|
|
case 0:
|
|
|
|
res = calc_s2k_simple(s2k, md, key, key_len);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
res = calc_s2k_salted(s2k, md, key, key_len);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
res = calc_s2k_iter_salted(s2k, md, key, key_len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = PXE_PGP_BAD_S2K_MODE;
|
|
|
|
}
|
|
|
|
px_md_free(md);
|
|
|
|
return res;
|
|
|
|
}
|