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 "$FreeBSD$"; 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 61 for (i = 0; i < seedsize; i++) { 62 seed[i] = (arc4random() & 0xff) ^ pass[i % 8]; 63 } 64 } 65 66 /* 67 * Generate a random public/secret key pair 68 */ 69 genkeys(public, secret, pass) 70 char *public; 71 char *secret; 72 char *pass; 73 { 74 int i; 75 76 # define BASEBITS (8*sizeof (short) - 1) 77 # define BASE (1 << BASEBITS) 78 79 MINT *pk = itom(0); 80 MINT *sk = itom(0); 81 MINT *tmp; 82 MINT *base = itom(BASE); 83 MINT *root = itom(PROOT); 84 MINT *modulus = xtom(HEXMODULUS); 85 short r; 86 unsigned short seed[KEYSIZE/BASEBITS + 1]; 87 char *xkey; 88 89 getseed((char *)seed, sizeof (seed), (u_char *)pass); 90 for (i = 0; i < KEYSIZE/BASEBITS + 1; i++) { 91 r = seed[i] % BASE; 92 tmp = itom(r); 93 mult(sk, base, sk); 94 madd(sk, tmp, sk); 95 mfree(tmp); 96 } 97 tmp = itom(0); 98 mdiv(sk, modulus, tmp, sk); 99 mfree(tmp); 100 pow(root, sk, modulus, pk); 101 xkey = mtox(sk); 102 adjust(secret, xkey); 103 xkey = mtox(pk); 104 adjust(public, xkey); 105 mfree(sk); 106 mfree(base); 107 mfree(pk); 108 mfree(root); 109 mfree(modulus); 110 } 111 112 /* 113 * Adjust the input key so that it is 0-filled on the left 114 */ 115 static 116 adjust(keyout, keyin) 117 char keyout[HEXKEYBYTES+1]; 118 char *keyin; 119 { 120 char *p; 121 char *s; 122 123 for (p = keyin; *p; p++) 124 ; 125 for (s = keyout + HEXKEYBYTES; p >= keyin; p--, s--) { 126 *s = *p; 127 } 128 while (s >= keyout) { 129 *s-- = '0'; 130 } 131 } 132