randomness.c 2.36 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2
/* randomness.c
 *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Niels Möller's avatar
Niels Möller committed
24 25
 */

26 27
#include <sys/types.h>
#include <time.h>
28
#include <string.h>
29

Niels Möller's avatar
Niels Möller committed
30
#include "randomness.h"
31 32

#include "xalloc.h"
Niels Möller's avatar
Niels Möller committed
33

34 35 36 37 38 39
#define CLASS_DEFINE
#include "randomness.h.x"
#undef CLASS_DEFINE

#include "randomness.c.x"

Niels Möller's avatar
Niels Möller committed
40
/* Random */
41 42 43 44 45 46 47 48 49 50 51
/* CLASS:
   (class
     (name poor_random)
     (super randomness)
     (vars
       (hash object hash_instance)
       (pos simple UINT32)
       (buffer space UINT8)))
*/

static void do_poor_random(struct randomness *r, UINT32 length, UINT8 *dst)
Niels Möller's avatar
Niels Möller committed
52
{
53
  CAST(poor_random, self, r);
Niels Möller's avatar
Niels Möller committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

  while(length)
    {
      UINT32 available = self->hash->hash_size - self->pos;
      UINT32 to_copy;
      
      if (!available)
	{
	  time_t now = time(NULL); /* To avoid cycles */
	  HASH_UPDATE(self->hash, sizeof(now), (UINT8 *) &now);
	  HASH_UPDATE(self->hash, self->hash->hash_size,
		      self->buffer);
	  HASH_DIGEST(self->hash, self->buffer);

	  available = self->hash->hash_size;
	  self->pos = 0;
	}
      to_copy = MIN(available, length);

      memcpy(dst, self->buffer + self->pos, to_copy);
      length -= to_copy;
      dst += to_copy;
      self->pos += to_copy;
    }
}

struct randomness *make_poor_random(struct hash_algorithm *hash,
				    struct lsh_string *init)
{
83
  NEW(poor_random, self);
Niels Möller's avatar
Niels Möller committed
84
  time_t now = time(NULL); /* To avoid cycles */
Niels Möller's avatar
Niels Möller committed
85

Niels Möller's avatar
Niels Möller committed
86 87
  self->super.random = do_poor_random;
  self->hash = MAKE_HASH(hash);
88 89
  self->buffer = lsh_space_alloc(hash->hash_size);
  
Niels Möller's avatar
Niels Möller committed
90 91 92 93 94 95 96 97
  HASH_UPDATE(self->hash, sizeof(now), (UINT8 *) &now);
  HASH_UPDATE(self->hash, init->length, init->data);
  HASH_DIGEST(self->hash, self->buffer);
  
  self->pos = 0;

  return &self->super;
}