1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
// -*-c++-*-
/* $Id: prng.h,v 1.10 2001/04/20 03:14:19 dm Exp $ */
/*
*
* Copyright (C) 1998 David Mazieres (dm@uun.org)
*
* 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, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
#ifndef _PRNG_H_
#define _PRNG_H_ 1
#include "async.h"
#include "sha1.h"
template<unsigned int N> struct sumbuf {
union {
char chars[N * 4];
u_char bytes[N * 4];
u_int32_t words[N];
};
sumbuf () {}
~sumbuf () { bzero (bytes, sizeof (bytes)); }
void set (const u_int32_t dp[N]) { memcpy (bytes, dp, sizeof (bytes)); }
void set (const u_char cp[N * 4]) { memcpy (bytes, cp, sizeof (bytes)); }
// template<size_t M>
// inline void add (const sumbuf<M> *, bool carryin = false);
};
// G++ chokes when this is a member function
template<size_t N, size_t M> inline bool
sumbufadd (sumbuf<N> *dst, const sumbuf<M> *src, bool carry = false)
{
u_int64_t sum = carry;
size_t i;
for (i = 0; i < min<size_t> (N, M); i++) {
dst->words[i] = sum = sum + dst->words[i] + src->words[i];
sum >>= 32;
}
while (i < N && (u_int32_t) (sum)) {
dst->words[i] = sum = sum + dst->words[i];
sum >>= 32;
}
return sum;
}
class prng : public datasink {
static const u_int32_t initdat[];
sumbuf<16> state;
sumbuf<16> input;
u_char *inpos;
u_char *const inlim;
void transform (sumbuf<5> *);
public:
prng ();
virtual ~prng () {}
void seed (const u_char[64]);
void seed_oracle (sha1oracle *);
void update (const void *, size_t);
void getbytes (void *, size_t);
u_int32_t getword () {
u_int32_t ret;
getbytes (&ret, sizeof (ret));
return ret;
}
u_int64_t gethyper () {
u_int64_t ret;
getbytes (&ret, sizeof (ret));
return ret;
}
};
#if 0
// XXX - g++ bug: sumbuf::add must be defined after prng.
template<size_t N> template<size_t M> inline void
sumbuf<N>::add (const sumbuf<M> *sb, bool carryin)
{
u_int64_t sum = carryin;
size_t i;
for (i = 0; i < min (N, M); i++) {
words[i] = sum = sum + words[i] + sb->words[i];
sum >>= 32;
}
while (i < N && (u_int32_t) sum) {
words[i] = sum = sum + words[i];
sum >>= 32;
}
}
#endif
void getclocknoise (datasink *);
void getfdnoise (datasink *, int fd, cbv cb, size_t maxbytes = size_t (-1));
void getfilenoise (datasink *, const char *, cbv, size_t = size_t (-1));
void getprognoise (datasink *, char *const *, cbv);
void getsysnoise (datasink *, cbv);
bool getkbdnoise (size_t, datasink *, cbv);
bool getkbdpwd (str, datasink *, cbs);
bool getkbdline (str, datasink *, cbs, str def = NULL);
#endif /* !_PRNG_H_ */
|