185909Simp/* 285909Simp * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3122116Sbde * unrestricted use provided that this legend is included on all tape 4122116Sbde * media and as a part of the software program in whole or part. Users 5122116Sbde * may copy or modify Sun RPC without charge, but are not authorized 6266349Simp * to license or distribute it to anyone else except as part of a product or 7266349Simp * program developed by the user or with the express written consent of 8266349Simp * Sun Microsystems, Inc. 9266349Simp * 10266349Simp * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 11266349Simp * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 12266349Simp * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 13266349Simp * 14266349Simp * Sun RPC is provided with no support and without any obligation on the 15266349Simp * part of Sun Microsystems, Inc. to assist in its use, correction, 16266349Simp * modification or enhancement. 17266349Simp * 18266349Simp * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 19240468Sbrooks * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 20265785Simp * OR ANY PART THEREOF. 21160440Sobrien * 22300795Sbdrewery * In no event will Sun Microsystems, Inc. be liable for any lost revenue 23300795Sbdrewery * or profits or other special, indirect and consequential damages, even if 24300795Sbdrewery * Sun has been advised of the possibility of such damages. 25300795Sbdrewery * 26300795Sbdrewery * Sun Microsystems, Inc. 27330420Sbdrewery * 2550 Garcia Avenue 28330420Sbdrewery * Mountain View, California 94043 29330420Sbdrewery */ 3085909Simp 3185909Simp#if !defined(lint) && defined(SCCSIDS) 3285909Simp#if 0 3385909Simpstatic char sccsid[] = "@(#)generic.c 1.2 91/03/11 Copyr 1986 Sun Micro"; 34175888Simp#endif 35175888Simp#endif 3685909Simp 37281627Semaste/* 3885909Simp * Copyright (C) 1986, Sun Microsystems, Inc. 3991512Sobrien */ 40240451Snp 41116341Smarkm#include <sys/cdefs.h> 4285909Simp__FBSDID("$FreeBSD$"); 4385909Simp 4485909Simp#include <sys/file.h> 4585909Simp 46220863Sdim#include <rpc/rpc.h> 47140606Sobrien#include <rpc/key_prot.h> 48187103Sgnn 49220863Sdim#include <mp.h> 50224882Snwhitehorn#include <stdio.h> 51224882Snwhitehorn#include <stdlib.h> 52224882Snwhitehorn 53140606Sobrien#include "extern.h" 54220863Sdim 55224882Snwhitehornstatic void adjust(char[], char *); 56220863Sdimstatic void getseed(char *, int, unsigned char *); 57265832Simp 58265832Simp/* 59265832Simp * Generate a seed 60127204Sobrien */ 61228868Sdimstatic void 62228868Sdimgetseed(char *seed, int seedsize, unsigned char *pass) 63140606Sobrien{ 64220863Sdim int i; 65220863Sdim 66124834Sru for (i = 0; i < seedsize; i++) { 67124834Sru seed[i] = (arc4random() & 0xff) ^ pass[i % 8]; 6885909Simp } 6985909Simp} 7085909Simp 71126890Strhodes/* 7285909Simp * Generate a random public/secret key pair 73192901Sthompsa */ 74126890Strhodesvoid 75282207Simpgenkeys(char *public, char *secret, char *pass) 76150966Sglebius{ 77257735Simp unsigned int i; 78257735Simp 79257735Simp# define BASEBITS (8*sizeof (short) - 1) 80257735Simp# define BASE (1 << BASEBITS) 81257735Simp 82278913Sglebius MINT *pk = mp_itom(0); 83265832Simp MINT *sk = mp_itom(0); 84265832Simp MINT *tmp; 85257735Simp MINT *base = mp_itom(BASE); 86265832Simp MINT *root = mp_itom(PROOT); 87210311Sjmallett MINT *modulus = mp_xtom(HEXMODULUS); 88352023Simp short r; 89352023Simp unsigned short seed[KEYSIZE/BASEBITS + 1]; 90352023Simp char *xkey; 91352023Simp 92352023Simp getseed((char *)seed, sizeof (seed), (u_char *)pass); 9385909Simp for (i = 0; i < KEYSIZE/BASEBITS + 1; i++) { 9485909Simp r = seed[i] % BASE; 95276770Simp tmp = mp_itom(r); 9685909Simp mp_mult(sk, base, sk); 9799923Sbde mp_madd(sk, tmp, sk); 98242715Sdim mp_mfree(tmp); 99265832Simp } 10099932Sbde tmp = mp_itom(0); 10199932Sbde mp_mdiv(sk, modulus, tmp, sk); 102242717Sdim mp_mfree(tmp); 103265832Simp mp_pow(root, sk, modulus, pk); 104242717Sdim xkey = mp_mtox(sk); 105242717Sdim adjust(secret, xkey); 10699932Sbde xkey = mp_mtox(pk); 107242717Sdim adjust(public, xkey); 10899923Sbde mp_mfree(sk); 10999932Sbde mp_mfree(base); 11085909Simp mp_mfree(pk); 11191002Speter mp_mfree(root); 11285909Simp mp_mfree(modulus); 11385909Simp} 11485909Simp 11585909Simp/* 116331752Semaste * Adjust the input key so that it is 0-filled on the left 117331730Semaste */ 118331730Semastestatic void 119331730Semasteadjust(char keyout[HEXKEYBYTES+1], char *keyin) 120116341Smarkm{ 121116341Smarkm char *p; 122116341Smarkm char *s; 12391002Speter 124290526Sbdrewery for (p = keyin; *p; p++) 12591002Speter ; 126105489Smux for (s = keyout + HEXKEYBYTES; p >= keyin; p--, s--) { 12785909Simp *s = *p; 128105462Smux } 129105462Smux while (s >= keyout) { 13085909Simp *s-- = '0'; 131239956Sjhb } 132239957Sjhb} 133316469Semaste