-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.c
114 lines (93 loc) · 2.99 KB
/
common.c
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
/***************************************************************************
common.c - description
-------------------
begin : Fri Apr 9 2004
copyright : (C) 2004 by Konrad Rosenbaum
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "secshare.h"
#include "sha1.h"
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
byte key[16];
byte hashv[16];
char* key_prints(char*s,byte*h)
{
static const char hex[]="0123456789ABCDEF";
static char sr[64];
int i,j;
if(!s)s=sr;
*s=0;
for(i=j=0;i<16;i++){
s[j++]=hex[h[i]>>4];
s[j++]=hex[h[i]&15];
if(i&1 && i<17)s[j++]=' ';
}
s[j]=0;
return s;
}
void genkey()
{
int i;
for(i=0;i<16;i++)key[i]=randbyte();
}
/*entropy pool*/
static byte*pool=0;
static int ppos=20;
/*This is a rather silly and simple random function.
It should be good enough for IVs and session keys.
Principle: initialize generator with some real near enough random.
Then hash the random before using it: this makes it hard to calculate
back into the original entropy pool.
*/
static void init_seth_rand()
{
struct timeval tv;
static byte pool_[20];
pool=pool_;
if(gettimeofday(&tv,NULL)){
fprintf(stderr,"Unable to seed random generator. Oops, giving up.\n");
exit(1);
}
srand(tv.tv_usec);
}
static void rand_refill()
{
void*rh;
int i,r;
byte*b;
if(pool==NULL)init_seth_rand();
ppos=0;
/*we assume each rand() call will give us 8 random bits (it probably
has more, but you can't have too much random).*/
rh=sha1_new();
/*make the period longer by making the next block dependend on the current one*/
sha1_write(rh,pool,20);
/*add the random to the soup*/
for(i=0;i<20;i++){
r=rand();
sha1_write(rh,(void*)&r,sizeof(r));
}
/*fill the pool*/
b=sha1_get(rh);
memcpy(pool,b,20);
sha1_close(rh);
}
byte randbyte()
{
if(ppos>=20)rand_refill();
return pool[ppos++];
}