xref: /freebsd/usr.bin/newkey/generic.c (revision ce834215a70ff69e7e222827437116eee2f9ac6f)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user or with the express written consent of
8  * Sun Microsystems, Inc.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)generic.c 1.2 91/03/11 Copyr 1986 Sun Micro";
32 #endif
33 
34 /*
35  * Copyright (C) 1986, Sun Microsystems, Inc.
36  */
37 
38 #include <stdio.h>
39 #include <rpc/rpc.h>
40 #include <sys/file.h>
41 #include <mp.h>
42 #include <rpc/key_prot.h>
43 
44 static int adjust __P(( char[], char * ));
45 /*
46  * Generate a seed
47  */
48 static
49 getseed(seed, seedsize, pass)
50 	char *seed;
51 	int seedsize;
52 	unsigned char *pass;
53 {
54 	int i;
55 	int rseed;
56 	struct timeval tv;
57 
58 	(void)gettimeofday(&tv, (struct timezone *)NULL);
59 	rseed = tv.tv_sec + tv.tv_usec;
60 	for (i = 0; i < 8; i++) {
61 		rseed ^= (rseed << 8) | pass[i];
62 	}
63 	srand(rseed);
64 
65 	for (i = 0; i < seedsize; i++) {
66 		seed[i] = (rand() & 0xff) ^ pass[i % 8];
67 	}
68 }
69 
70 /*
71  * Generate a random public/secret key pair
72  */
73 genkeys(public, secret, pass)
74 	char *public;
75 	char *secret;
76 	char *pass;
77 {
78 	int i;
79 
80 #   define BASEBITS (8*sizeof (short) - 1)
81 #	define BASE		(1 << BASEBITS)
82 
83 	MINT *pk = itom(0);
84 	MINT *sk = itom(0);
85 	MINT *tmp;
86 	MINT *base = itom(BASE);
87 	MINT *root = itom(PROOT);
88 	MINT *modulus = xtom(HEXMODULUS);
89 	short r;
90 	unsigned short seed[KEYSIZE/BASEBITS + 1];
91 	char *xkey;
92 
93 	getseed((char *)seed, sizeof (seed), (u_char *)pass);
94 	for (i = 0; i < KEYSIZE/BASEBITS + 1; i++) {
95 		r = seed[i] % BASE;
96 		tmp = itom(r);
97 		mult(sk, base, sk);
98 		madd(sk, tmp, sk);
99 		mfree(tmp);
100 	}
101 	tmp = itom(0);
102 	mdiv(sk, modulus, tmp, sk);
103 	mfree(tmp);
104 	pow(root, sk, modulus, pk);
105 	xkey = mtox(sk);
106 	adjust(secret, xkey);
107 	xkey = mtox(pk);
108 	adjust(public, xkey);
109 	mfree(sk);
110 	mfree(base);
111 	mfree(pk);
112 	mfree(root);
113 	mfree(modulus);
114 }
115 
116 /*
117  * Adjust the input key so that it is 0-filled on the left
118  */
119 static
120 adjust(keyout, keyin)
121 	char keyout[HEXKEYBYTES+1];
122 	char *keyin;
123 {
124 	char *p;
125 	char *s;
126 
127 	for (p = keyin; *p; p++)
128 		;
129 	for (s = keyout + HEXKEYBYTES; p >= keyin; p--, s--) {
130 		*s = *p;
131 	}
132 	while (s >= keyout) {
133 		*s-- = '0';
134 	}
135 }
136