generic_key.c revision 2
0N/A * The contents of this file are subject to the terms of the 0N/A * Common Development and Distribution License, Version 1.0 only 0N/A * (the "License"). You may not use this file except in compliance 0N/A * See the License for the specific language governing permissions 0N/A * and limitations under the License. 0N/A * When distributing Covered Code, include this CDDL HEADER in each 0N/A * If applicable, add the following below this CDDL HEADER, with the 0N/A * fields enclosed by brackets "[]" replaced with your own identifying 0N/A * information: Portions Copyright [yyyy] [name of copyright owner] 0N/A * Copyright 1997 Sun Microsystems, Inc. All rights reserved. 0N/A * Use is subject to license terms. 0N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 0N/A/* All Rights Reserved */ 0N/A * Portions of this source code were derived from Berkeley 4.3 BSD 0N/A * under license from the Regents of the University of California. 0N/A#
pragma ident "%Z%%M% %I% %E% SMI" 0N/A * seed the random generator. Here we use the time of day and a supplied 0N/A * password for generating the seed. 0N/A for (i = 0; i <
8; i++) {
0N/A * Adjust the input key so that it is 0-filled on the left and store 0N/A * the results in key out. 0N/A * __generic_gen_dhkeys: Classic Diffie-Hellman key pair generation. 0N/A * Generate a Diffie-Hellman key pair of a given key length using 0N/A * the supplied modulus and root. To calculate the pair we generate 0N/A * a random key of the appropriate key length modulo the modulus. 0N/A * This random key is the private key of the key pair. We now compute 0N/A * the public key as PublicKey = root^PrivateKey % modulus. This routine 0N/A * make use of libmp to do the multiprecision interger arithmetic. 0N/A char *
pass /* password to seed with for private key */)
0N/A /* Convert the modulus from a hex string to a MINT */ 0N/A /* Seed the random generate */ 0N/A * We will break up the private key into groups of BASEBITS where 0N/A * BASEBITS is equal to the number of bits in an integer type. 0N/A * Curently, basebits is 8 so the integral type is a character. 0N/A * We will calculate the number of BASEBITS units that we need so 0N/A * that we have at least keylen bits. 0N/A * Now for each BASEBITS we calculate a new random number. 0N/A * Shift the private key by base bits and then add the 0N/A * generated random number. 0N/A /* get a random number */ 0N/A /* Convert it to a MINT */ 0N/A /* Shift the private key */ 0N/A /* Add in the new low order bits */ 710N/A /* We get the private keys as private key modulo the modulus */ 0N/A /* The public key is root^sk % modulus */ 0N/A /* Convert the private key to a hex string */ 0N/A /* Set leading zeros if necessary and store in secret */ 0N/A /* Done with xkey */ 0N/A /* Now set xkey to the hex representation of the public key */ 0N/A /* Set leading zeros and store in public */ 0N/A * Given a key extract keynum des keys 0N/A /* len is the total number of bits we need for keynum des keys */ 0N/A /* Create a MINT a to hold the common key */ 0N/A * Calculate the middle byte in the key. We will simply extract 0N/A * the middle bits of the key for the bits in our DES keys. 0N/A * Now take our middle bits referenced by a and shove them 0N/A * into the array of DES keys. 431N/A /* We're done with a */ 431N/A /* Set the DES parity for each key */ 431N/A if (
keylen ==
192)
/* Old broken way for compatibility */ 431N/A * __generic_common_dhkeys: Generate a set of DES keys based on 431N/A * the Diffie-Hellman common key derived from the supplied key pair 431N/A * of the given key length using the passed in modulus. The common key 431N/A * ck = pk ^ sk % modulus 431N/A * We will use the above routine to extract a set of DES keys for the 0N/A int keynum /* The number of DES keys to create */)
0N/A /* Convert hex string representations to MINTS */ 0N/A /* Create a MINT for the common key */ 0N/A /* ck = pk ^ sk % modulus */ 0N/A /* Set the DES keys */