Home | History | Annotate | Line # | Download | only in libtelnet
pk.c revision 1.1
      1  1.1  christos /*-
      2  1.1  christos  * Copyright (c) 1991, 1993
      3  1.1  christos  *      Dave Safford.  All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Redistribution and use in source and binary forms, with or without
      6  1.1  christos  * modification, are permitted provided that the following conditions
      7  1.1  christos  * are met:
      8  1.1  christos  * 1. Redistributions of source code must retain the above copyright
      9  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     10  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  christos  *    documentation and/or other materials provided with the distribution.
     13  1.1  christos  * 3. Neither the name of the University nor the names of its contributors
     14  1.1  christos  *    may be used to endorse or promote products derived from this software
     15  1.1  christos  *    without specific prior written permission.
     16  1.1  christos  *
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     18  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     21  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.1  christos  * SUCH DAMAGE.
     28  1.1  christos  *
     29  1.1  christos  */
     30  1.1  christos 
     31  1.1  christos #include <sys/cdefs.h>
     32  1.1  christos 
     33  1.1  christos #ifdef notdef
     34  1.1  christos __FBSDID("$FreeBSD: src/contrib/telnet/libtelnet/pk.c,v 1.10 2002/08/22 06:19:07 nsayer Exp $");
     35  1.1  christos #else
     36  1.1  christos __RCSID("$NetBSD: pk.c,v 1.1 2005/02/19 21:55:52 christos Exp $");
     37  1.1  christos #endif
     38  1.1  christos 
     39  1.1  christos /* public key routines */
     40  1.1  christos /* functions:
     41  1.1  christos 	genkeys(char *public, char *secret)
     42  1.1  christos 	common_key(char *secret, char *public, desData *deskey)
     43  1.1  christos         pk_encode(char *in, *out, DesData *deskey);
     44  1.1  christos         pk_decode(char *in, *out, DesData *deskey);
     45  1.1  christos       where
     46  1.1  christos 	char public[HEXKEYBYTES + 1];
     47  1.1  christos 	char secret[HEXKEYBYTES + 1];
     48  1.1  christos  */
     49  1.1  christos 
     50  1.1  christos #include <sys/time.h>
     51  1.1  christos #include <openssl/des.h>
     52  1.1  christos #include <fcntl.h>
     53  1.1  christos #include <stdio.h>
     54  1.1  christos #include <stdlib.h>
     55  1.1  christos #include <string.h>
     56  1.1  christos 
     57  1.1  christos #include "mp.h"
     58  1.1  christos #include "pk.h"
     59  1.1  christos 
     60  1.1  christos static void adjust(char keyout[HEXKEYBYTES+1], char *keyin);
     61  1.1  christos 
     62  1.1  christos /*
     63  1.1  christos  * Choose top 128 bits of the common key to use as our idea key.
     64  1.1  christos  */
     65  1.1  christos static void
     66  1.1  christos extractideakey(MINT *ck, IdeaData *ideakey)
     67  1.1  christos {
     68  1.1  christos         MINT *a;
     69  1.1  christos         MINT *z;
     70  1.1  christos         short r;
     71  1.1  christos         int i;
     72  1.1  christos         short base = (1 << 8);
     73  1.1  christos         char *k;
     74  1.1  christos 
     75  1.1  christos         z = itom(0);
     76  1.1  christos         a = itom(0);
     77  1.1  christos         madd(ck, z, a);
     78  1.1  christos         for (i = 0; i < ((KEYSIZE - 128) / 8); i++) {
     79  1.1  christos                 sdiv(a, base, a, &r);
     80  1.1  christos         }
     81  1.1  christos         k = (char *)ideakey;
     82  1.1  christos         for (i = 0; i < 16; i++) {
     83  1.1  christos                 sdiv(a, base, a, &r);
     84  1.1  christos                 *k++ = r;
     85  1.1  christos         }
     86  1.1  christos 	mfree(z);
     87  1.1  christos         mfree(a);
     88  1.1  christos }
     89  1.1  christos 
     90  1.1  christos /*
     91  1.1  christos  * Choose middle 64 bits of the common key to use as our des key, possibly
     92  1.1  christos  * overwriting the lower order bits by setting parity.
     93  1.1  christos  */
     94  1.1  christos static void
     95  1.1  christos extractdeskey(MINT *ck, DesData *deskey)
     96  1.1  christos {
     97  1.1  christos         MINT *a;
     98  1.1  christos         MINT *z;
     99  1.1  christos         short r;
    100  1.1  christos         int i;
    101  1.1  christos         short base = (1 << 8);
    102  1.1  christos         char *k;
    103  1.1  christos 
    104  1.1  christos         z = itom(0);
    105  1.1  christos         a = itom(0);
    106  1.1  christos         madd(ck, z, a);
    107  1.1  christos         for (i = 0; i < ((KEYSIZE - 64) / 2) / 8; i++) {
    108  1.1  christos                 sdiv(a, base, a, &r);
    109  1.1  christos         }
    110  1.1  christos         k = (char *)deskey;
    111  1.1  christos         for (i = 0; i < 8; i++) {
    112  1.1  christos                 sdiv(a, base, a, &r);
    113  1.1  christos                 *k++ = r;
    114  1.1  christos         }
    115  1.1  christos 	mfree(z);
    116  1.1  christos         mfree(a);
    117  1.1  christos }
    118  1.1  christos 
    119  1.1  christos /*
    120  1.1  christos  * get common key from my secret key and his public key
    121  1.1  christos  */
    122  1.1  christos void
    123  1.1  christos common_key(char *xsecret, char *xpublic, IdeaData *ideakey, DesData *deskey)
    124  1.1  christos {
    125  1.1  christos         MINT *public;
    126  1.1  christos         MINT *secret;
    127  1.1  christos         MINT *common;
    128  1.1  christos 	MINT *modulus = xtom(HEXMODULUS);
    129  1.1  christos 
    130  1.1  christos         public = xtom(xpublic);
    131  1.1  christos         secret = xtom(xsecret);
    132  1.1  christos         common = itom(0);
    133  1.1  christos         pow(public, secret, modulus, common);
    134  1.1  christos         extractdeskey(common, deskey);
    135  1.1  christos         extractideakey(common, ideakey);
    136  1.1  christos 	des_set_odd_parity(deskey);
    137  1.1  christos         mfree(common);
    138  1.1  christos         mfree(secret);
    139  1.1  christos         mfree(public);
    140  1.1  christos 	mfree(modulus);
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos /*
    144  1.1  christos  * Generate a seed
    145  1.1  christos  */
    146  1.1  christos static void
    147  1.1  christos getseed(char *seed, int seedsize)
    148  1.1  christos {
    149  1.1  christos 	int i;
    150  1.1  christos 
    151  1.1  christos 	srandomdev();
    152  1.1  christos 	for (i = 0; i < seedsize; i++) {
    153  1.1  christos 		seed[i] = random() & 0xff;
    154  1.1  christos 	}
    155  1.1  christos }
    156  1.1  christos 
    157  1.1  christos /*
    158  1.1  christos  * Generate a random public/secret key pair
    159  1.1  christos  */
    160  1.1  christos void
    161  1.1  christos genkeys(char *public, char *secret)
    162  1.1  christos {
    163  1.1  christos         size_t i;
    164  1.1  christos 
    165  1.1  christos #       define BASEBITS (8*sizeof(short) - 1)
    166  1.1  christos #       define BASE (1 << BASEBITS)
    167  1.1  christos 
    168  1.1  christos         MINT *pk = itom(0);
    169  1.1  christos         MINT *sk = itom(0);
    170  1.1  christos         MINT *tmp;
    171  1.1  christos         MINT *base = itom(BASE);
    172  1.1  christos         MINT *root = itom(PROOT);
    173  1.1  christos         MINT *modulus = xtom(HEXMODULUS);
    174  1.1  christos         short r;
    175  1.1  christos         unsigned short seed[KEYSIZE/BASEBITS + 1];
    176  1.1  christos         char *xkey;
    177  1.1  christos 
    178  1.1  christos         getseed((char *)seed, sizeof(seed));
    179  1.1  christos         for (i = 0; i < KEYSIZE/BASEBITS + 1; i++) {
    180  1.1  christos                 r = seed[i] % BASE;
    181  1.1  christos                 tmp = itom(r);
    182  1.1  christos                 mult(sk, base, sk);
    183  1.1  christos                 madd(sk, tmp, sk);
    184  1.1  christos                 mfree(tmp);
    185  1.1  christos         }
    186  1.1  christos         tmp = itom(0);
    187  1.1  christos         mdiv(sk, modulus, tmp, sk);
    188  1.1  christos         mfree(tmp);
    189  1.1  christos         pow(root, sk, modulus, pk);
    190  1.1  christos         xkey = mtox(sk);
    191  1.1  christos         adjust(secret, xkey);
    192  1.1  christos         xkey = mtox(pk);
    193  1.1  christos         adjust(public, xkey);
    194  1.1  christos         mfree(sk);
    195  1.1  christos         mfree(base);
    196  1.1  christos         mfree(pk);
    197  1.1  christos         mfree(root);
    198  1.1  christos         mfree(modulus);
    199  1.1  christos }
    200  1.1  christos 
    201  1.1  christos /*
    202  1.1  christos  * Adjust the input key so that it is 0-filled on the left
    203  1.1  christos  */
    204  1.1  christos static void
    205  1.1  christos adjust(char keyout[HEXKEYBYTES+1], char *keyin)
    206  1.1  christos {
    207  1.1  christos         char *p;
    208  1.1  christos         char *s;
    209  1.1  christos 
    210  1.1  christos         for (p = keyin; *p; p++)
    211  1.1  christos                 ;
    212  1.1  christos         for (s = keyout + HEXKEYBYTES; p >= keyin; p--, s--) {
    213  1.1  christos                 *s = *p;
    214  1.1  christos         }
    215  1.1  christos         while (s >= keyout) {
    216  1.1  christos                 *s-- = '0';
    217  1.1  christos         }
    218  1.1  christos }
    219  1.1  christos 
    220  1.1  christos static char hextab[17] = "0123456789ABCDEF";
    221  1.1  christos 
    222  1.1  christos /* given a DES key, cbc encrypt and translate input to terminated hex */
    223  1.1  christos void
    224  1.1  christos pk_encode(char *in, char *out, DesData *key)
    225  1.1  christos {
    226  1.1  christos 	char buf[256];
    227  1.1  christos 	DesData i;
    228  1.1  christos 	des_key_schedule k;
    229  1.1  christos 	int l,op,deslen;
    230  1.1  christos 
    231  1.1  christos 	memset(&i,0,sizeof(i));
    232  1.1  christos 	memset(buf,0,sizeof(buf));
    233  1.1  christos 	deslen = ((strlen(in) + 7)/8)*8;
    234  1.1  christos 	des_key_sched(key, k);
    235  1.1  christos 	des_cbc_encrypt(in,buf,deslen, k,&i,DES_ENCRYPT);
    236  1.1  christos 	for (l=0,op=0;l<deslen;l++) {
    237  1.1  christos 		out[op++] = hextab[(buf[l] & 0xf0) >> 4];
    238  1.1  christos 		out[op++] = hextab[(buf[l] & 0x0f)];
    239  1.1  christos 	}
    240  1.1  christos 	out[op] = '\0';
    241  1.1  christos }
    242  1.1  christos 
    243  1.1  christos /* given a DES key, translate input from hex and decrypt */
    244  1.1  christos void
    245  1.1  christos pk_decode(char *in, char *out, DesData *key)
    246  1.1  christos {
    247  1.1  christos 	char buf[256];
    248  1.1  christos 	DesData i;
    249  1.1  christos 	des_key_schedule k;
    250  1.1  christos 	int n1,n2,op;
    251  1.1  christos 	size_t l;
    252  1.1  christos 
    253  1.1  christos 	memset(&i,0,sizeof(i));
    254  1.1  christos 	memset(buf,0,sizeof(buf));
    255  1.1  christos 	for (l=0,op=0;l<strlen(in)/2;l++,op+=2) {
    256  1.1  christos 		if (in[op] > '9')
    257  1.1  christos 			n1 = in[op] - 'A' + 10;
    258  1.1  christos 		else
    259  1.1  christos 			n1 = in[op] - '0';
    260  1.1  christos 		if (in[op+1] > '9')
    261  1.1  christos 			n2 = in[op+1] - 'A' + 10;
    262  1.1  christos 		else
    263  1.1  christos 			n2 = in[op+1] - '0';
    264  1.1  christos 		buf[l] = n1*16 +n2;
    265  1.1  christos 	}
    266  1.1  christos 	des_key_sched(key, k);
    267  1.1  christos 	des_cbc_encrypt(buf,out,strlen(in)/2, k,&i,DES_DECRYPT);
    268  1.1  christos 	out[strlen(in)/2] = '\0';
    269  1.1  christos }
    270