mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
3214563d8e
My normal habit these days, in new code, is to treat int and bool as _almost_ completely separate types. I'm still willing to use C's implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine, no need to spell it out as blob.len != 0), but generally, if a variable is going to be conceptually a boolean, I like to declare it bool and assign to it using 'true' or 'false' rather than 0 or 1. PuTTY is an exception, because it predates the C99 bool, and I've stuck to its existing coding style even when adding new code to it. But it's been annoying me more and more, so now that I've decided C99 bool is an acceptable thing to require from our toolchain in the first place, here's a quite thorough trawl through the source doing 'boolification'. Many variables and function parameters are now typed as bool rather than int; many assignments of 0 or 1 to those variables are now spelled 'true' or 'false'. I managed this thorough conversion with the help of a custom clang plugin that I wrote to trawl the AST and apply heuristics to point out where things might want changing. So I've even managed to do a decent job on parts of the code I haven't looked at in years! To make the plugin's work easier, I pushed platform front ends generally in the direction of using standard 'bool' in preference to platform-specific boolean types like Windows BOOL or GTK's gboolean; I've left the platform booleans in places they _have_ to be for the platform APIs to work right, but variables only used by my own code have been converted wherever I found them. In a few places there are int values that look very like booleans in _most_ of the places they're used, but have a rarely-used third value, or a distinction between different nonzero values that most users don't care about. In these cases, I've _removed_ uses of 'true' and 'false' for the return values, to emphasise that there's something more subtle going on than a simple boolean answer: - the 'multisel' field in dialog.h's list box structure, for which the GTK front end in particular recognises a difference between 1 and 2 but nearly everything else treats as boolean - the 'urgent' parameter to plug_receive, where 1 vs 2 tells you something about the specific location of the urgent pointer, but most clients only care about 0 vs 'something nonzero' - the return value of wc_match, where -1 indicates a syntax error in the wildcard. - the return values from SSH-1 RSA-key loading functions, which use -1 for 'wrong passphrase' and 0 for all other failures (so any caller which already knows it's not loading an _encrypted private_ key can treat them as boolean) - term->esc_query, and the 'query' parameter in toggle_mode in terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h, but can also hold -1 for some other intervening character that we don't support. In a few places there's an integer that I haven't turned into a bool even though it really _can_ only take values 0 or 1 (and, as above, tried to make the call sites consistent in not calling those values true and false), on the grounds that I thought it would make it more confusing to imply that the 0 value was in some sense 'negative' or bad and the 1 positive or good: - the return value of plug_accepting uses the POSIXish convention of 0=success and nonzero=error; I think if I made it bool then I'd also want to reverse its sense, and that's a job for a separate piece of work. - the 'screen' parameter to lineptr() in terminal.c, where 0 and 1 represent the default and alternate screens. There's no obvious reason why one of those should be considered 'true' or 'positive' or 'success' - they're just indices - so I've left it as int. ssh_scp_recv had particularly confusing semantics for its previous int return value: its call sites used '<= 0' to check for error, but it never actually returned a negative number, just 0 or 1. Now the function and its call sites agree that it's a bool. In a couple of places I've renamed variables called 'ret', because I don't like that name any more - it's unclear whether it means the return value (in preparation) for the _containing_ function or the return value received from a subroutine call, and occasionally I've accidentally used the same variable for both and introduced a bug. So where one of those got in my way, I've renamed it to 'toret' or 'retd' (the latter short for 'returned') in line with my usual modern practice, but I haven't done a thorough job of finding all of them. Finally, one amusing side effect of doing this is that I've had to separate quite a few chained assignments. It used to be perfectly fine to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a the 'true' defined by stdbool.h, that idiom provokes a warning from gcc: 'suggest parentheses around assignment used as truth value'!
345 lines
8.9 KiB
C
345 lines
8.9 KiB
C
/*
|
|
* cryptographic random number generator for PuTTY's ssh client
|
|
*/
|
|
|
|
#include "putty.h"
|
|
#include "ssh.h"
|
|
#include <assert.h>
|
|
|
|
/* Collect environmental noise every 5 minutes */
|
|
#define NOISE_REGULAR_INTERVAL (5*60*TICKSPERSEC)
|
|
|
|
void noise_get_heavy(void (*func) (void *, int));
|
|
void noise_get_light(void (*func) (void *, int));
|
|
|
|
/*
|
|
* `pool' itself is a pool of random data which we actually use: we
|
|
* return bytes from `pool', at position `poolpos', until `poolpos'
|
|
* reaches the end of the pool. At this point we generate more
|
|
* random data, by adding noise, stirring well, and resetting
|
|
* `poolpos' to point to just past the beginning of the pool (not
|
|
* _the_ beginning, since otherwise we'd give away the whole
|
|
* contents of our pool, and attackers would just have to guess the
|
|
* next lot of noise).
|
|
*
|
|
* `incomingb' buffers acquired noise data, until it gets full, at
|
|
* which point the acquired noise is SHA'ed into `incoming' and
|
|
* `incomingb' is cleared. The noise in `incoming' is used as part
|
|
* of the noise for each stirring of the pool, in addition to local
|
|
* time, process listings, and other such stuff.
|
|
*/
|
|
|
|
#define HASHINPUT 64 /* 64 bytes SHA input */
|
|
#define HASHSIZE 20 /* 160 bits SHA output */
|
|
#define POOLSIZE 1200 /* size of random pool */
|
|
|
|
struct RandPool {
|
|
unsigned char pool[POOLSIZE];
|
|
int poolpos;
|
|
|
|
unsigned char incoming[HASHSIZE];
|
|
|
|
unsigned char incomingb[HASHINPUT];
|
|
int incomingpos;
|
|
|
|
bool stir_pending;
|
|
};
|
|
|
|
int random_active = 0;
|
|
|
|
#ifdef FUZZING
|
|
/*
|
|
* Special dummy version of the RNG for use when fuzzing.
|
|
*/
|
|
void random_add_noise(void *noise, int length) { }
|
|
void random_add_heavynoise(void *noise, int length) { }
|
|
void random_ref(void) { }
|
|
void random_unref(void) { }
|
|
int random_byte(void)
|
|
{
|
|
return 0x45; /* Chosen by eight fair coin tosses */
|
|
}
|
|
void random_get_savedata(void **data, int *len) { }
|
|
#else /* !FUZZING */
|
|
static struct RandPool pool;
|
|
long next_noise_collection;
|
|
|
|
#ifdef RANDOM_DIAGNOSTICS
|
|
int random_diagnostics = 0;
|
|
#endif
|
|
|
|
static void random_stir(void)
|
|
{
|
|
uint32_t block[HASHINPUT / sizeof(uint32_t)];
|
|
uint32_t digest[HASHSIZE / sizeof(uint32_t)];
|
|
int i, j, k;
|
|
|
|
/*
|
|
* noise_get_light will call random_add_noise, which may call
|
|
* back to here. Prevent recursive stirs.
|
|
*/
|
|
if (pool.stir_pending)
|
|
return;
|
|
pool.stir_pending = true;
|
|
|
|
noise_get_light(random_add_noise);
|
|
|
|
#ifdef RANDOM_DIAGNOSTICS
|
|
{
|
|
int p, q;
|
|
printf("random stir starting\npool:\n");
|
|
for (p = 0; p < POOLSIZE; p += HASHSIZE) {
|
|
printf(" ");
|
|
for (q = 0; q < HASHSIZE; q += 4) {
|
|
printf(" %08x", *(uint32_t *)(pool.pool + p + q));
|
|
}
|
|
printf("\n");
|
|
}
|
|
printf("incoming:\n ");
|
|
for (q = 0; q < HASHSIZE; q += 4) {
|
|
printf(" %08x", *(uint32_t *)(pool.incoming + q));
|
|
}
|
|
printf("\nincomingb:\n ");
|
|
for (q = 0; q < HASHINPUT; q += 4) {
|
|
printf(" %08x", *(uint32_t *)(pool.incomingb + q));
|
|
}
|
|
printf("\n");
|
|
random_diagnostics++;
|
|
}
|
|
#endif
|
|
|
|
SHATransform((uint32_t *) pool.incoming, (uint32_t *) pool.incomingb);
|
|
pool.incomingpos = 0;
|
|
|
|
/*
|
|
* Chunks of this code are blatantly endianness-dependent, but
|
|
* as it's all random bits anyway, WHO CARES?
|
|
*/
|
|
memcpy(digest, pool.incoming, sizeof(digest));
|
|
|
|
/*
|
|
* Make two passes over the pool.
|
|
*/
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
/*
|
|
* We operate SHA in CFB mode, repeatedly adding the same
|
|
* block of data to the digest. But we're also fiddling
|
|
* with the digest-so-far, so this shouldn't be Bad or
|
|
* anything.
|
|
*/
|
|
memcpy(block, pool.pool, sizeof(block));
|
|
|
|
/*
|
|
* Each pass processes the pool backwards in blocks of
|
|
* HASHSIZE, just so that in general we get the output of
|
|
* SHA before the corresponding input, in the hope that
|
|
* things will be that much less predictable that way
|
|
* round, when we subsequently return bytes ...
|
|
*/
|
|
for (j = POOLSIZE; (j -= HASHSIZE) >= 0;) {
|
|
/*
|
|
* XOR the bit of the pool we're processing into the
|
|
* digest.
|
|
*/
|
|
|
|
for (k = 0; k < sizeof(digest) / sizeof(*digest); k++)
|
|
digest[k] ^= ((uint32_t *) (pool.pool + j))[k];
|
|
|
|
/*
|
|
* Munge our unrevealed first block of the pool into
|
|
* it.
|
|
*/
|
|
SHATransform(digest, block);
|
|
|
|
/*
|
|
* Stick the result back into the pool.
|
|
*/
|
|
|
|
for (k = 0; k < sizeof(digest) / sizeof(*digest); k++)
|
|
((uint32_t *) (pool.pool + j))[k] = digest[k];
|
|
}
|
|
|
|
#ifdef RANDOM_DIAGNOSTICS
|
|
if (i == 0) {
|
|
int p, q;
|
|
printf("random stir midpoint\npool:\n");
|
|
for (p = 0; p < POOLSIZE; p += HASHSIZE) {
|
|
printf(" ");
|
|
for (q = 0; q < HASHSIZE; q += 4) {
|
|
printf(" %08x", *(uint32_t *)(pool.pool + p + q));
|
|
}
|
|
printf("\n");
|
|
}
|
|
printf("incoming:\n ");
|
|
for (q = 0; q < HASHSIZE; q += 4) {
|
|
printf(" %08x", *(uint32_t *)(pool.incoming + q));
|
|
}
|
|
printf("\nincomingb:\n ");
|
|
for (q = 0; q < HASHINPUT; q += 4) {
|
|
printf(" %08x", *(uint32_t *)(pool.incomingb + q));
|
|
}
|
|
printf("\n");
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/*
|
|
* Might as well save this value back into `incoming', just so
|
|
* there'll be some extra bizarreness there.
|
|
*/
|
|
SHATransform(digest, block);
|
|
memcpy(pool.incoming, digest, sizeof(digest));
|
|
|
|
pool.poolpos = sizeof(pool.incoming);
|
|
|
|
pool.stir_pending = false;
|
|
|
|
#ifdef RANDOM_DIAGNOSTICS
|
|
{
|
|
int p, q;
|
|
printf("random stir done\npool:\n");
|
|
for (p = 0; p < POOLSIZE; p += HASHSIZE) {
|
|
printf(" ");
|
|
for (q = 0; q < HASHSIZE; q += 4) {
|
|
printf(" %08x", *(uint32_t *)(pool.pool + p + q));
|
|
}
|
|
printf("\n");
|
|
}
|
|
printf("incoming:\n ");
|
|
for (q = 0; q < HASHSIZE; q += 4) {
|
|
printf(" %08x", *(uint32_t *)(pool.incoming + q));
|
|
}
|
|
printf("\nincomingb:\n ");
|
|
for (q = 0; q < HASHINPUT; q += 4) {
|
|
printf(" %08x", *(uint32_t *)(pool.incomingb + q));
|
|
}
|
|
printf("\n");
|
|
random_diagnostics--;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void random_add_noise(void *noise, int length)
|
|
{
|
|
unsigned char *p = noise;
|
|
int i;
|
|
|
|
if (!random_active)
|
|
return;
|
|
|
|
/*
|
|
* This function processes HASHINPUT bytes into only HASHSIZE
|
|
* bytes, so _if_ we were getting incredibly high entropy
|
|
* sources then we would be throwing away valuable stuff.
|
|
*/
|
|
while (length >= (HASHINPUT - pool.incomingpos)) {
|
|
memcpy(pool.incomingb + pool.incomingpos, p,
|
|
HASHINPUT - pool.incomingpos);
|
|
p += HASHINPUT - pool.incomingpos;
|
|
length -= HASHINPUT - pool.incomingpos;
|
|
SHATransform((uint32_t *) pool.incoming, (uint32_t *) pool.incomingb);
|
|
for (i = 0; i < HASHSIZE; i++) {
|
|
pool.pool[pool.poolpos++] ^= pool.incoming[i];
|
|
if (pool.poolpos >= POOLSIZE)
|
|
pool.poolpos = 0;
|
|
}
|
|
if (pool.poolpos < HASHSIZE)
|
|
random_stir();
|
|
|
|
pool.incomingpos = 0;
|
|
}
|
|
|
|
memcpy(pool.incomingb + pool.incomingpos, p, length);
|
|
pool.incomingpos += length;
|
|
}
|
|
|
|
void random_add_heavynoise(void *noise, int length)
|
|
{
|
|
unsigned char *p = noise;
|
|
int i;
|
|
|
|
while (length >= POOLSIZE) {
|
|
for (i = 0; i < POOLSIZE; i++)
|
|
pool.pool[i] ^= *p++;
|
|
random_stir();
|
|
length -= POOLSIZE;
|
|
}
|
|
|
|
for (i = 0; i < length; i++)
|
|
pool.pool[i] ^= *p++;
|
|
random_stir();
|
|
}
|
|
|
|
static void random_add_heavynoise_bitbybit(void *noise, int length)
|
|
{
|
|
unsigned char *p = noise;
|
|
int i;
|
|
|
|
while (length >= POOLSIZE - pool.poolpos) {
|
|
for (i = 0; i < POOLSIZE - pool.poolpos; i++)
|
|
pool.pool[pool.poolpos + i] ^= *p++;
|
|
random_stir();
|
|
length -= POOLSIZE - pool.poolpos;
|
|
pool.poolpos = 0;
|
|
}
|
|
|
|
for (i = 0; i < length; i++)
|
|
pool.pool[i] ^= *p++;
|
|
pool.poolpos = i;
|
|
}
|
|
|
|
static void random_timer(void *ctx, unsigned long now)
|
|
{
|
|
if (random_active > 0 && now == next_noise_collection) {
|
|
noise_regular();
|
|
next_noise_collection =
|
|
schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool);
|
|
}
|
|
}
|
|
|
|
void random_ref(void)
|
|
{
|
|
if (!random_active) {
|
|
memset(&pool, 0, sizeof(pool)); /* just to start with */
|
|
|
|
noise_get_heavy(random_add_heavynoise_bitbybit);
|
|
random_stir();
|
|
|
|
next_noise_collection =
|
|
schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool);
|
|
}
|
|
random_active++;
|
|
}
|
|
|
|
void random_unref(void)
|
|
{
|
|
assert(random_active > 0);
|
|
if (random_active == 1) {
|
|
random_save_seed();
|
|
expire_timer_context(&pool);
|
|
}
|
|
random_active--;
|
|
}
|
|
|
|
int random_byte(void)
|
|
{
|
|
assert(random_active);
|
|
|
|
if (pool.poolpos >= POOLSIZE)
|
|
random_stir();
|
|
|
|
return pool.pool[pool.poolpos++];
|
|
}
|
|
|
|
void random_get_savedata(void **data, int *len)
|
|
{
|
|
void *buf = snewn(POOLSIZE / 2, char);
|
|
random_stir();
|
|
memcpy(buf, pool.pool + pool.poolpos, POOLSIZE / 2);
|
|
*len = POOLSIZE / 2;
|
|
*data = buf;
|
|
random_stir();
|
|
}
|
|
#endif
|