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
31 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 /* public key routines */
37 genkeys(char *public, char *secret)
38 common_key(char *secret, char *public, desData *deskey)
39 pk_encode(char *in, *out, DesData *deskey);
40 pk_decode(char *in, *out, DesData *deskey);
42 char public[HEXKEYBYTES + 1];
43 char secret[HEXKEYBYTES + 1];
47 #include <openssl/des.h>
56 static void adjust(char keyout[HEXKEYBYTES+1], char *keyin);
59 * Choose top 128 bits of the common key to use as our idea key.
62 extractideakey(MINT *ck, IdeaData *ideakey)
68 short base = (1 << 8);
74 for (i = 0; i < ((KEYSIZE - 128) / 8); i++) {
75 mp_sdiv(a, base, a, &r);
78 for (i = 0; i < 16; i++) {
79 mp_sdiv(a, base, a, &r);
87 * Choose middle 64 bits of the common key to use as our des key, possibly
88 * overwriting the lower order bits by setting parity.
91 extractdeskey(MINT *ck, DesData *deskey)
97 short base = (1 << 8);
103 for (i = 0; i < ((KEYSIZE - 64) / 2) / 8; i++) {
104 mp_sdiv(a, base, a, &r);
107 for (i = 0; i < 8; i++) {
108 mp_sdiv(a, base, a, &r);
116 * get common key from my secret key and his public key
119 common_key(char *xsecret, char *xpublic, IdeaData *ideakey, DesData *deskey)
124 MINT *modulus = mp_xtom(HEXMODULUS);
126 public = mp_xtom(xpublic);
127 secret = mp_xtom(xsecret);
129 mp_pow(public, secret, modulus, common);
130 extractdeskey(common, deskey);
131 extractideakey(common, ideakey);
132 DES_set_odd_parity(deskey);
143 getseed(char *seed, int seedsize)
145 arc4random_buf(seed, seedsize);
149 * Generate a random public/secret key pair
152 genkeys(char *public, char *secret)
156 # define BASEBITS (8*sizeof(short) - 1)
157 # define BASE (1 << BASEBITS)
159 MINT *pk = mp_itom(0);
160 MINT *sk = mp_itom(0);
162 MINT *base = mp_itom((short)BASE);
163 MINT *root = mp_itom(PROOT);
164 MINT *modulus = mp_xtom(HEXMODULUS);
166 unsigned short seed[KEYSIZE/BASEBITS + 1];
169 getseed((char *)seed, sizeof(seed));
170 for (i = 0; i < KEYSIZE/BASEBITS + 1; i++) {
173 mp_mult(sk, base, sk);
174 mp_madd(sk, tmp, sk);
178 mp_mdiv(sk, modulus, tmp, sk);
180 mp_pow(root, sk, modulus, pk);
182 adjust(secret, xkey);
184 adjust(public, xkey);
193 * Adjust the input key so that it is 0-filled on the left
196 adjust(char keyout[HEXKEYBYTES+1], char *keyin)
201 for (p = keyin; *p; p++)
203 for (s = keyout + HEXKEYBYTES; p >= keyin; p--, s--) {
206 while (s >= keyout) {
211 static char hextab[17] = "0123456789ABCDEF";
213 /* given a DES key, cbc encrypt and translate input to terminated hex */
215 pk_encode(char *in, char *out, DesData *key)
222 memset(&i,0,sizeof(i));
223 memset(buf,0,sizeof(buf));
224 deslen = ((strlen(in) + 7)/8)*8;
225 DES_key_sched(key, &k);
226 DES_cbc_encrypt(in, buf, deslen, &k, &i, DES_ENCRYPT);
227 for (l=0,op=0;l<deslen;l++) {
228 out[op++] = hextab[(buf[l] & 0xf0) >> 4];
229 out[op++] = hextab[(buf[l] & 0x0f)];
234 /* given a DES key, translate input from hex and decrypt */
236 pk_decode(char *in, char *out, DesData *key)
244 memset(&i,0,sizeof(i));
245 memset(buf,0,sizeof(buf));
246 for (l=0,op=0;l<strlen(in)/2;l++,op+=2) {
248 n1 = in[op] - 'A' + 10;
252 n2 = in[op+1] - 'A' + 10;
257 DES_key_sched(key, &k);
258 DES_cbc_encrypt(buf, out, strlen(in) / 2, &k, &i, DES_DECRYPT);
259 out[strlen(in)/2] = '\0';