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