126238Swpaul/* 226238Swpaul * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 326238Swpaul * unrestricted use provided that this legend is included on all tape 426238Swpaul * media and as a part of the software program in whole or part. Users 526238Swpaul * may copy or modify Sun RPC without charge, but are not authorized 626238Swpaul * to license or distribute it to anyone else except as part of a product or 726238Swpaul * program developed by the user or with the express written consent of 826238Swpaul * Sun Microsystems, Inc. 926238Swpaul * 1026238Swpaul * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 1126238Swpaul * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 1226238Swpaul * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 1326238Swpaul * 1426238Swpaul * Sun RPC is provided with no support and without any obligation on the 1526238Swpaul * part of Sun Microsystems, Inc. to assist in its use, correction, 1626238Swpaul * modification or enhancement. 1726238Swpaul * 1826238Swpaul * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 1926238Swpaul * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 2026238Swpaul * OR ANY PART THEREOF. 2126238Swpaul * 2226238Swpaul * In no event will Sun Microsystems, Inc. be liable for any lost revenue 2326238Swpaul * or profits or other special, indirect and consequential damages, even if 2426238Swpaul * Sun has been advised of the possibility of such damages. 2526238Swpaul * 2626238Swpaul * Sun Microsystems, Inc. 2726238Swpaul * 2550 Garcia Avenue 2826238Swpaul * Mountain View, California 94043 2926238Swpaul */ 3095633Smarkm 3126238Swpaul#if !defined(lint) && defined(SCCSIDS) 3227754Scharnier#if 0 3326238Swpaulstatic char sccsid[] = "@(#)generic.c 1.2 91/03/11 Copyr 1986 Sun Micro"; 3426238Swpaul#endif 3527754Scharnier#endif 3626238Swpaul 3726238Swpaul/* 3826238Swpaul * Copyright (C) 1986, Sun Microsystems, Inc. 3926238Swpaul */ 4026238Swpaul 4195633Smarkm#include <sys/cdefs.h> 4295633Smarkm__FBSDID("$FreeBSD$"); 4395633Smarkm 4487204Smarkm#include <sys/file.h> 4595633Smarkm 4687204Smarkm#include <rpc/rpc.h> 4787204Smarkm#include <rpc/key_prot.h> 4895633Smarkm 4987204Smarkm#include <mp.h> 5026238Swpaul#include <stdio.h> 5127754Scharnier#include <stdlib.h> 5226238Swpaul 5387204Smarkm#include "extern.h" 5487204Smarkm 5592921Simpstatic void adjust(char[], char *); 5692921Simpstatic void getseed(char *, int, unsigned char *); 5787204Smarkm 5826238Swpaul/* 5926238Swpaul * Generate a seed 6026238Swpaul */ 6187204Smarkmstatic void 6295633Smarkmgetseed(char *seed, int seedsize, unsigned char *pass) 6326238Swpaul{ 6426238Swpaul int i; 6526238Swpaul 6626238Swpaul for (i = 0; i < seedsize; i++) { 6773563Skris seed[i] = (arc4random() & 0xff) ^ pass[i % 8]; 6826238Swpaul } 6926238Swpaul} 7026238Swpaul 7126238Swpaul/* 7226238Swpaul * Generate a random public/secret key pair 7326238Swpaul */ 7487204Smarkmvoid 7595633Smarkmgenkeys(char *public, char *secret, char *pass) 7626238Swpaul{ 7787204Smarkm unsigned int i; 7826238Swpaul 7926238Swpaul# define BASEBITS (8*sizeof (short) - 1) 8026238Swpaul# define BASE (1 << BASEBITS) 8126238Swpaul 82189092Sed MINT *pk = mp_itom(0); 83189092Sed MINT *sk = mp_itom(0); 8426238Swpaul MINT *tmp; 85189092Sed MINT *base = mp_itom(BASE); 86189092Sed MINT *root = mp_itom(PROOT); 87189092Sed MINT *modulus = mp_xtom(HEXMODULUS); 8826238Swpaul short r; 8926238Swpaul unsigned short seed[KEYSIZE/BASEBITS + 1]; 9026238Swpaul char *xkey; 9126238Swpaul 9226238Swpaul getseed((char *)seed, sizeof (seed), (u_char *)pass); 9326238Swpaul for (i = 0; i < KEYSIZE/BASEBITS + 1; i++) { 9426238Swpaul r = seed[i] % BASE; 95189092Sed tmp = mp_itom(r); 96189092Sed mp_mult(sk, base, sk); 97189092Sed mp_madd(sk, tmp, sk); 98189092Sed mp_mfree(tmp); 9926238Swpaul } 100189092Sed tmp = mp_itom(0); 101189092Sed mp_mdiv(sk, modulus, tmp, sk); 102189092Sed mp_mfree(tmp); 103189092Sed mp_pow(root, sk, modulus, pk); 104189092Sed xkey = mp_mtox(sk); 10526238Swpaul adjust(secret, xkey); 106189092Sed xkey = mp_mtox(pk); 10726238Swpaul adjust(public, xkey); 108189092Sed mp_mfree(sk); 109189092Sed mp_mfree(base); 110189092Sed mp_mfree(pk); 111189092Sed mp_mfree(root); 112189092Sed mp_mfree(modulus); 11326238Swpaul} 11426238Swpaul 11526238Swpaul/* 11626238Swpaul * Adjust the input key so that it is 0-filled on the left 11726238Swpaul */ 11887204Smarkmstatic void 11995633Smarkmadjust(char keyout[HEXKEYBYTES+1], char *keyin) 12026238Swpaul{ 12126238Swpaul char *p; 12226238Swpaul char *s; 12326238Swpaul 12426238Swpaul for (p = keyin; *p; p++) 12526238Swpaul ; 12626238Swpaul for (s = keyout + HEXKEYBYTES; p >= keyin; p--, s--) { 12726238Swpaul *s = *p; 12826238Swpaul } 12926238Swpaul while (s >= keyout) { 13026238Swpaul *s-- = '0'; 13126238Swpaul } 13226238Swpaul} 133