xref: /freebsd/usr.bin/newkey/generic.c (revision 380a989b3223d455375b4fae70fd0b9bdd43bafb)
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 #if 0
32 static char sccsid[] = "@(#)generic.c 1.2 91/03/11 Copyr 1986 Sun Micro";
33 #endif
34 static const char rcsid[] =
35 	"$Id$";
36 #endif
37 
38 /*
39  * Copyright (C) 1986, Sun Microsystems, Inc.
40  */
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <rpc/rpc.h>
45 #include <sys/file.h>
46 #include <mp.h>
47 #include <rpc/key_prot.h>
48 
49 static int adjust __P(( char[], char * ));
50 /*
51  * Generate a seed
52  */
53 static
54 getseed(seed, seedsize, pass)
55 	char *seed;
56 	int seedsize;
57 	unsigned char *pass;
58 {
59 	int i;
60 	int rseed;
61 	struct timeval tv;
62 
63 	(void)gettimeofday(&tv, (struct timezone *)NULL);
64 	rseed = tv.tv_sec + tv.tv_usec;
65 	for (i = 0; i < 8; i++) {
66 		rseed ^= (rseed << 8) | pass[i];
67 	}
68 	srand(rseed);
69 
70 	for (i = 0; i < seedsize; i++) {
71 		seed[i] = (rand() & 0xff) ^ pass[i % 8];
72 	}
73 }
74 
75 /*
76  * Generate a random public/secret key pair
77  */
78 genkeys(public, secret, pass)
79 	char *public;
80 	char *secret;
81 	char *pass;
82 {
83 	int i;
84 
85 #   define BASEBITS (8*sizeof (short) - 1)
86 #	define BASE		(1 << BASEBITS)
87 
88 	MINT *pk = itom(0);
89 	MINT *sk = itom(0);
90 	MINT *tmp;
91 	MINT *base = itom(BASE);
92 	MINT *root = itom(PROOT);
93 	MINT *modulus = xtom(HEXMODULUS);
94 	short r;
95 	unsigned short seed[KEYSIZE/BASEBITS + 1];
96 	char *xkey;
97 
98 	getseed((char *)seed, sizeof (seed), (u_char *)pass);
99 	for (i = 0; i < KEYSIZE/BASEBITS + 1; i++) {
100 		r = seed[i] % BASE;
101 		tmp = itom(r);
102 		mult(sk, base, sk);
103 		madd(sk, tmp, sk);
104 		mfree(tmp);
105 	}
106 	tmp = itom(0);
107 	mdiv(sk, modulus, tmp, sk);
108 	mfree(tmp);
109 	pow(root, sk, modulus, pk);
110 	xkey = mtox(sk);
111 	adjust(secret, xkey);
112 	xkey = mtox(pk);
113 	adjust(public, xkey);
114 	mfree(sk);
115 	mfree(base);
116 	mfree(pk);
117 	mfree(root);
118 	mfree(modulus);
119 }
120 
121 /*
122  * Adjust the input key so that it is 0-filled on the left
123  */
124 static
125 adjust(keyout, keyin)
126 	char keyout[HEXKEYBYTES+1];
127 	char *keyin;
128 {
129 	char *p;
130 	char *s;
131 
132 	for (p = keyin; *p; p++)
133 		;
134 	for (s = keyout + HEXKEYBYTES; p >= keyin; p--, s--) {
135 		*s = *p;
136 	}
137 	while (s >= keyout) {
138 		*s-- = '0';
139 	}
140 }
141