2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * Portions of this source code were derived from Berkeley 4.3 BSD
2N/A * under license from the Regents of the University of California.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * generic_key.c
2N/A */
2N/A
2N/A#include <mp.h>
2N/A#include <time.h>
2N/A#include <rpc/rpc.h>
2N/A#include <stdlib.h>
2N/A
2N/A#define BASEBITS (8 * sizeof (char))
2N/A#define BASE (1 << BASEBITS)
2N/A
2N/Aextern void des_setparity(char *);
2N/Aextern void des_setparity_g(des_block *);
2N/A
2N/A/*
2N/A * seed the random generator. Here we use the time of day and a supplied
2N/A * password for generating the seed.
2N/A */
2N/Astatic void
2N/Asetseed(unsigned char *pass)
2N/A{
2N/A int i;
2N/A int rseed;
2N/A struct timeval tv;
2N/A
2N/A (void) gettimeofday(&tv, (struct timezone *)NULL);
2N/A rseed = tv.tv_sec + tv.tv_usec;
2N/A
2N/A for (i = 0; i < 8; i++) {
2N/A rseed ^= (rseed << 8) | pass[i];
2N/A }
2N/A (void) srandom(rseed);
2N/A}
2N/A
2N/A/*
2N/A * Adjust the input key so that it is 0-filled on the left and store
2N/A * the results in key out.
2N/A */
2N/Astatic void
2N/Aadjust(char *keyout, char *keyin, int keylen)
2N/A{
2N/A char *p;
2N/A char *s;
2N/A int hexkeybytes = (keylen+3)/4;
2N/A
2N/A for (p = keyin; *p; p++);
2N/A for (s = keyout + hexkeybytes; p >= keyin; p--, s--) {
2N/A *s = *p;
2N/A }
2N/A while (s >= keyout) {
2N/A *s-- = '0';
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * __generic_gen_dhkeys: Classic Diffie-Hellman key pair generation.
2N/A * Generate a Diffie-Hellman key pair of a given key length using
2N/A * the supplied modulus and root. To calculate the pair we generate
2N/A * a random key of the appropriate key length modulo the modulus.
2N/A * This random key is the private key of the key pair. We now compute
2N/A * the public key as PublicKey = root^PrivateKey % modulus. This routine
2N/A * make use of libmp to do the multiprecision interger arithmetic.
2N/A */
2N/Avoid
2N/A__generic_gen_dhkeys(int keylen, /* Size of keys in bits */
2N/A char *xmodulus, /* The modulus */
2N/A int proot, /* The prime root */
2N/A char *public, /* Public key */
2N/A char *secret, /* Private key */
2N/A char *pass /* password to seed with for private key */)
2N/A{
2N/A int i, len;
2N/A MINT *pk = mp_itom(0); /* Initial public key */
2N/A MINT *sk = mp_itom(0); /* Initial private key */
2N/A MINT *tmp;
2N/A MINT *base = mp_itom(BASE); /* We shift by BASEBITS */
2N/A MINT *root = mp_itom(proot); /* We get the root as a MINT */
2N/A /* Convert the modulus from a hex string to a MINT */
2N/A MINT *modulus = mp_xtom(xmodulus);
2N/A unsigned char seed;
2N/A char *xkey;
2N/A
2N/A /* Seed the random generate */
2N/A setseed((u_char *)pass);
2N/A
2N/A /*
2N/A * We will break up the private key into groups of BASEBITS where
2N/A * BASEBITS is equal to the number of bits in an integer type.
2N/A * Curently, basebits is 8 so the integral type is a character.
2N/A * We will calculate the number of BASEBITS units that we need so
2N/A * that we have at least keylen bits.
2N/A */
2N/A len = ((keylen + BASEBITS - 1) / BASEBITS);
2N/A
2N/A /*
2N/A * Now for each BASEBITS we calculate a new random number.
2N/A * Shift the private key by base bits and then add the
2N/A * generated random number.
2N/A */
2N/A for (i = 0; i < len; i++) {
2N/A /* get a random number */
2N/A seed = random() ^ pass[i % 8];
2N/A /* Convert it to a MINT */
2N/A tmp = mp_itom(seed);
2N/A /* Shift the private key */
2N/A mp_mult(sk, base, sk);
2N/A /* Add in the new low order bits */
2N/A mp_madd(sk, tmp, sk);
2N/A /* Free tmp */
2N/A mp_mfree(tmp);
2N/A }
2N/A
2N/A /* Set timp to 0 */
2N/A tmp = mp_itom(0);
2N/A /* We get the private keys as private key modulo the modulus */
2N/A mp_mdiv(sk, modulus, tmp, sk);
2N/A /* Done with tmp */
2N/A mp_mfree(tmp);
2N/A /* The public key is root^sk % modulus */
2N/A mp_pow(root, sk, modulus, pk);
2N/A /* Convert the private key to a hex string */
2N/A xkey = mp_mtox(sk);
2N/A /* Set leading zeros if necessary and store in secret */
2N/A (void) adjust(secret, xkey, keylen);
2N/A /* Done with xkey */
2N/A free(xkey);
2N/A /* Now set xkey to the hex representation of the public key */
2N/A xkey = mp_mtox(pk);
2N/A /* Set leading zeros and store in public */
2N/A (void) adjust(public, xkey, keylen);
2N/A
2N/A /* Free storage */
2N/A free(xkey);
2N/A
2N/A mp_mfree(sk);
2N/A mp_mfree(base);
2N/A mp_mfree(pk);
2N/A mp_mfree(root);
2N/A mp_mfree(modulus);
2N/A}
2N/A
2N/A/*
2N/A * Given a key extract keynum des keys
2N/A */
2N/Astatic void
2N/Aextractdeskeys(MINT *ck, int keylen, des_block keys[], int keynum)
2N/A{
2N/A MINT *a;
2N/A short r;
2N/A int i;
2N/A short base = (1 << 8);
2N/A char *k;
2N/A /* len is the total number of bits we need for keynum des keys */
2N/A int len = 8 * sizeof (des_block) * keynum;
2N/A extern void _mp_move(MINT *, MINT *);
2N/A
2N/A /* Create a MINT a to hold the common key */
2N/A a = mp_itom(0);
2N/A _mp_move(ck, a);
2N/A
2N/A
2N/A /*
2N/A * Calculate the middle byte in the key. We will simply extract
2N/A * the middle bits of the key for the bits in our DES keys.
2N/A */
2N/A for (i = 0; i < ((keylen - len)/2)/8; i++)
2N/A mp_sdiv(a, base, a, &r); /* Shift the key by one byte */
2N/A
2N/A /*
2N/A * Now take our middle bits referenced by a and shove them
2N/A * into the array of DES keys.
2N/A */
2N/A k = (char *)keys;
2N/A for (i = 0; i < sizeof (des_block) * keynum; i++) {
2N/A mp_sdiv(a, base, a, &r);
2N/A *k++ = r;
2N/A }
2N/A
2N/A /* We're done with a */
2N/A mp_mfree(a);
2N/A
2N/A /* Set the DES parity for each key */
2N/A for (i = 0; i < keynum; i++)
2N/A if (keylen == 192) /* Old broken way for compatibility */
2N/A des_setparity((char *)&keys[i]);
2N/A else
2N/A des_setparity_g(&keys[i]);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * __generic_common_dhkeys: Generate a set of DES keys based on
2N/A * the Diffie-Hellman common key derived from the supplied key pair
2N/A * of the given key length using the passed in modulus. The common key
2N/A * is calculated as:
2N/A *
2N/A * ck = pk ^ sk % modulus
2N/A *
2N/A * We will use the above routine to extract a set of DES keys for the
2N/A * caller.
2N/A */
2N/Avoid
2N/A__generic_common_dhkeys(char *pkey, /* Public key of remote */
2N/A char *skey, /* Our private key */
2N/A int keylen, /* All the keys have this many bits */
2N/A char *xmodulus, /* The modulus */
2N/A des_block keys[], /* DES keys to fill */
2N/A int keynum /* The number of DES keys to create */)
2N/A{
2N/A /* Convert hex string representations to MINTS */
2N/A MINT *pk = mp_xtom(pkey);
2N/A MINT *sk = mp_xtom(skey);
2N/A MINT *modulus = mp_xtom(xmodulus);
2N/A /* Create a MINT for the common key */
2N/A MINT *ck = mp_itom(0);
2N/A
2N/A /* ck = pk ^ sk % modulus */
2N/A mp_pow(pk, sk, modulus, ck);
2N/A
2N/A /* Set the DES keys */
2N/A extractdeskeys(ck, keylen, keys, keynum);
2N/A
2N/A /* Clean up */
2N/A mp_mfree(pk);
2N/A mp_mfree(sk);
2N/A mp_mfree(modulus);
2N/A mp_mfree(ck);
2N/A}