2 * Copyright (c) 1991, 1993
3 * Dave Safford. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.5 2005/07/18 16:43:26 joerg Exp $
34 /* public key routines */
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);
41 char public[HEXKEYBYTES + 1];
42 char secret[HEXKEYBYTES + 1];
52 #include <openssl/bn.h>
53 #include <openssl/crypto.h>
54 #include <openssl/des.h>
55 #include <openssl/err.h>
59 static void adjust(char keyout[HEXKEYBYTES+1], char *keyin);
62 * Choose top 128 bits of the common key to use as our idea key.
65 extractideakey(BIGNUM *ck, IdeaData *ideakey)
69 BN_ULONG r, base = (1 << 8);
72 if ((z = BN_new()) == NULL)
73 errx(1, "could not create BIGNUM");
75 if ((a = BN_new()) == NULL)
76 errx(1, "could not create BIGNUM");
79 for (i = 0; i < ((KEYSIZE - 128) / 8); i++) {
80 r = BN_div_word(a, base);
83 for (i = 0; i < 16; i++) {
84 r = BN_div_word(a, base);
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.
96 extractdeskey(BIGNUM *ck, DesData *deskey)
100 BN_ULONG r, base = (1 << 8);
103 if ((z = BN_new()) == NULL)
104 errx(1, "could not create BIGNUM");
106 if ((a = BN_new()) == NULL)
107 errx(1, "could not create BIGNUM");
110 for (i = 0; i < ((KEYSIZE - 64) / 2) / 8; i++) {
111 r = BN_div_word(a, base);
114 for (i = 0; i < 8; i++) {
115 r = BN_div_word(a, base);
123 * get common key from my secret key and his public key
126 common_key(char *xsecret, char *xpublic, IdeaData *ideakey, DesData *deskey)
128 BIGNUM *public, *secret, *common, *modulus;
131 if ((ctx = BN_CTX_new()) == NULL)
132 errx(1, "could not create BN_CTX");
134 if (BN_hex2bn(&modulus, HEXMODULUS) == NULL)
135 errx(1, "could not convert modulus");
137 if (BN_hex2bn(&public, xpublic) == NULL)
138 errx(1, "could not convert public");
140 if (BN_hex2bn(&secret, xsecret) == NULL)
141 errx(1, "could not convert secret");
143 if ((common = BN_new()) == NULL)
144 errx(1, "could not create BIGNUM");
146 BN_mod_exp(common, public, secret, modulus, ctx);
147 extractdeskey(common, deskey);
148 extractideakey(common, ideakey);
149 des_set_odd_parity(deskey);
161 getseed(char *seed, int seedsize)
165 for (i = 0; i < seedsize; i++) {
166 seed[i] = arc4random() & 0xff;
175 if ((n = BN_new()) == NULL)
176 errx(1, "could not create BIGNUM: %s",
177 ERR_error_string(ERR_get_error(), 0));
180 BN_add_word(n, (u_long)i);
182 BN_sub_word(n, (u_long)(-i));
187 * Generate a random public/secret key pair
190 genkeys(char *public, char *secret)
192 #define BASEBITS (8*sizeof (short) - 1)
193 #define BASE (short)(1 << BASEBITS)
197 unsigned short seed[KEYSIZE/BASEBITS + 1];
201 BIGNUM *pk, *sk, *tmp, *base, *root, *modulus;
209 if (BN_hex2bn(&modulus, HEXMODULUS) == NULL)
210 errx(1, "could not convert modulus to BIGNUM: %s",
211 ERR_error_string(ERR_get_error(), 0));
213 if ((ctx = BN_CTX_new()) == NULL)
214 errx(1, "could not create BN_CTX: %s",
215 ERR_error_string(ERR_get_error(), 0));
217 getseed((char *)seed, sizeof (seed));
218 for (i = 0; i < KEYSIZE/BASEBITS + 1; i++) {
222 BN_mul(sk, base, sk, ctx);
226 BN_div(tmp, sk, sk, modulus, ctx);
227 BN_mod_exp(pk, root, sk, modulus, ctx);
229 if ((xkey = BN_bn2hex(sk)) == NULL)
230 errx(1, "could convert sk to hex: %s",
231 ERR_error_string(ERR_get_error(), 0));
232 adjust(secret, xkey);
235 if ((xkey = BN_bn2hex(pk)) == NULL)
236 errx(1, "could convert pk to hex: %s",
237 ERR_error_string(ERR_get_error(), 0));
238 adjust(public, xkey);
250 * Adjust the input key so that it is 0-filled on the left
253 adjust(char keyout[HEXKEYBYTES+1], char *keyin)
258 for (p = keyin; *p; p++)
260 for (s = keyout + HEXKEYBYTES; p >= keyin; p--, s--) {
263 while (s >= keyout) {
268 static char hextab[17] = "0123456789ABCDEF";
270 /* given a DES key, cbc encrypt and translate input to terminated hex */
272 pk_encode(char *in, char *out, DesData *key)
279 memset(&i,0,sizeof(i));
280 memset(buf,0,sizeof(buf));
281 deslen = ((strlen(in) + 7)/8)*8;
282 des_key_sched(key, k);
283 des_cbc_encrypt(in,buf,deslen, k,&i,DES_ENCRYPT);
284 for (l=0,op=0;l<deslen;l++) {
285 out[op++] = hextab[(buf[l] & 0xf0) >> 4];
286 out[op++] = hextab[(buf[l] & 0x0f)];
291 /* given a DES key, translate input from hex and decrypt */
293 pk_decode(char *in, char *out, DesData *key)
301 memset(&i,0,sizeof(i));
302 memset(buf,0,sizeof(buf));
303 for (l=0,op=0;l<strlen(in)/2;l++,op+=2) {
305 n1 = in[op] - 'A' + 10;
309 n2 = in[op+1] - 'A' + 10;
314 des_key_sched(key, k);
315 des_cbc_encrypt(buf,out,strlen(in)/2, k,&i,DES_DECRYPT);
316 out[strlen(in)/2] = '\0';