]> andersk Git - moira.git/blob - util/gdss/lib/crypto/crypto_util.c
initial import of gdss from the Athena source tree
[moira.git] / util / gdss / lib / crypto / crypto_util.c
1 /*
2  * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
3  * ALL RIGHTS RESERVED
4  *
5  * "Digital Equipment Corporation authorizes the reproduction,
6  * distribution and modification of this software subject to the following
7  * restrictions:
8  * 
9  * 1.  Any partial or whole copy of this software, or any modification
10  * thereof, must include this copyright notice in its entirety.
11  *
12  * 2.  This software is supplied "as is" with no warranty of any kind,
13  * expressed or implied, for any purpose, including any warranty of fitness 
14  * or merchantibility.  DIGITAL assumes no responsibility for the use or
15  * reliability of this software, nor promises to provide any form of 
16  * support for it on any basis.
17  *
18  * 3.  Distribution of this software is authorized only if no profit or
19  * remuneration of any kind is received in exchange for such distribution.
20  * 
21  * 4.  This software produces public key authentication certificates
22  * bearing an expiration date established by DIGITAL and RSA Data
23  * Security, Inc.  It may cease to generate certificates after the expiration
24  * date.  Any modification of this software that changes or defeats
25  * the expiration date or its effect is unauthorized.
26  * 
27  * 5.  Software that will renew or extend the expiration date of
28  * authentication certificates produced by this software may be obtained
29  * from RSA Data Security, Inc., 10 Twin Dolphin Drive, Redwood City, CA
30  * 94065, (415)595-8782, or from DIGITAL"
31  *
32  */
33
34 #include <stdio.h>
35 #ifndef ultrix
36 #include <sys/time.h>
37 #else
38 #include <time.h>
39 #endif
40 #include <sys/types.h>
41 #include "random.h"
42 #include "BigNum.h"
43 #include "BigRSA.h"
44 #include "bigkeygen.h"
45
46 /*
47  * InitDelegationKey.  Generate a new rsa key of desired bit length.
48  *                     If random number generator is not yet seeded,
49  *                     initialize with uncertainty from the principal
50  *                     prime.
51  */
52 int InitDelegationKey(principal, delegation, bitlen)
53 RSAKeyStorage *principal, *delegation;
54 int bitlen;
55 {
56     RNGState rng;
57     read_rng_state (&rng);
58     if ((rng.count)==0) initialize_rng_state (principal->p,32);
59     return(newRSAKey (delegation, bitlen));
60 }
61
62 /*
63  * Private2Public.  Remove private key stuff in a key storage block.
64  */
65 int Private2Public (key)
66 RSAKeyStorage *key;
67 {
68        memset(key->p,0,PRIVATE_KEY_SIZE-PUBLIC_KEY_SIZE);
69        key->pl=key->ql=key->dpl=key->dql=0;
70 }
71
72
73 /* 
74  * initrandom - interactive random number initializer
75  *
76  * The following routine attempts to initialize the random number generator
77  * with a reasonable amount of uncertainty.  This is provided mostly by
78  * the user, with some additional time inputs from the system.
79  */
80 int initrandom()
81
82 unsigned char bar[16];
83 int seed ;
84 char buffer[256], sphinx_ans[80];
85 char *ptr=buffer, *env=NULL;
86 int i;
87 long tick = clock();
88 struct timeval tv ;
89 struct timezone tz ;
90 int thischar , lastchar ;
91
92         memset(ptr,0,256);
93
94         gettimeofday(&tv, &tz);
95         memcpy(ptr,&tv,sizeof(struct timeval));
96         ptr += sizeof(struct timeval);
97 /*
98         printf("\nRiddle of the Sphinx :\n\n");
99         printf("What creature is it, that in the morning goes on four feet,\n");
100         printf("at noon on two, and at night on three?\n\n");
101         fflush(stdout);
102         scanf("%s", sphinx_ans); gets(ptr);
103         if ((strcasecmp(sphinx_ans, "man") == 0) || (strcasecmp(sphinx_ans, "woman") == 0)) {
104           printf("\nCorrect!\n\n");
105         } else {
106           printf("\nWrong!  Please brush up on your Egyptian mythology.\n\n");
107         }
108 */
109         tick = clock();
110         memcpy(ptr,&tick,sizeof(long));
111         ptr += sizeof(long);
112         memcpy(ptr,&ptr,sizeof(char *));
113         ptr+=sizeof(char *);
114         gettimeofday(&tv, &tz);
115         memcpy(ptr,&tv,sizeof(struct timeval));
116         ptr += sizeof(struct timeval);
117  
118         seed = sizeof(buffer) - (ptr - buffer) - sizeof(struct timeval);
119
120         printf("\nSome 'uncertainty' is needed to initialize  the  random");
121         printf("\nnumber generator to generate your long term key. Please");
122         printf("\nenter up to %d characters of text.  The quality of your key", seed);
123         printf("\ndepends upon how 'uncertain' this input is.  When you");
124         printf("\nthink you have entered enough text, enter two successive");
125         printf("\ncarriage returns.");
126         printf("\n\n");
127         fflush(stdout);
128
129         for( i = thischar = lastchar = 0;
130           (thischar=getchar())!= EOF,((thischar != '\n')||(lastchar != '\n')); i++) {
131               lastchar = thischar;
132               ptr[ i % seed ] += (unsigned char) thischar ;
133         } 
134
135         printf("\nThank you very much.\n");
136
137         gettimeofday(&tv, &tz);
138         memcpy(ptr,&tv,sizeof(struct timeval));
139
140         initialize_rng_state (buffer, sizeof(buffer));
141
142 }
143
This page took 1.128115 seconds and 5 git commands to generate.