186d7f5d3SJohn Marino /* $FreeBSD: src/sys/crypto/des/des_setkey.c,v 1.7 2004/06/14 00:38:16 obrien Exp $ */
286d7f5d3SJohn Marino /* $KAME: des_setkey.c,v 1.7 2001/09/10 04:03:58 itojun Exp $ */
386d7f5d3SJohn Marino
486d7f5d3SJohn Marino /* crypto/des/set_key.c */
586d7f5d3SJohn Marino
686d7f5d3SJohn Marino /* Copyright (C) 1995-1996 Eric Young (eay@mincom.oz.au)
786d7f5d3SJohn Marino * All rights reserved.
886d7f5d3SJohn Marino *
986d7f5d3SJohn Marino * This file is part of an SSL implementation written
1086d7f5d3SJohn Marino * by Eric Young (eay@mincom.oz.au).
1186d7f5d3SJohn Marino * The implementation was written so as to conform with Netscapes SSL
1286d7f5d3SJohn Marino * specification. This library and applications are
1386d7f5d3SJohn Marino * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
1486d7f5d3SJohn Marino * as long as the following conditions are aheared to.
1586d7f5d3SJohn Marino *
1686d7f5d3SJohn Marino * Copyright remains Eric Young's, and as such any Copyright notices in
1786d7f5d3SJohn Marino * the code are not to be removed. If this code is used in a product,
1886d7f5d3SJohn Marino * Eric Young should be given attribution as the author of the parts used.
1986d7f5d3SJohn Marino * This can be in the form of a textual message at program startup or
2086d7f5d3SJohn Marino * in documentation (online or textual) provided with the package.
2186d7f5d3SJohn Marino *
2286d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
2386d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
2486d7f5d3SJohn Marino * are met:
2586d7f5d3SJohn Marino * 1. Redistributions of source code must retain the copyright
2686d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
2786d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
2886d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
2986d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
3086d7f5d3SJohn Marino * 3. All advertising materials mentioning features or use of this software
3186d7f5d3SJohn Marino * must display the following acknowledgement:
3286d7f5d3SJohn Marino * This product includes software developed by Eric Young (eay@mincom.oz.au)
3386d7f5d3SJohn Marino *
3486d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
3586d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3686d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3786d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3886d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3986d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4086d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4186d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4286d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4386d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4486d7f5d3SJohn Marino * SUCH DAMAGE.
4586d7f5d3SJohn Marino *
4686d7f5d3SJohn Marino * The licence and distribution terms for any publically available version or
4786d7f5d3SJohn Marino * derivative of this code cannot be changed. i.e. this code cannot simply be
4886d7f5d3SJohn Marino * copied and put under another distribution licence
4986d7f5d3SJohn Marino * [including the GNU Public Licence.]
5086d7f5d3SJohn Marino */
5186d7f5d3SJohn Marino
5286d7f5d3SJohn Marino /* set_key.c v 1.4 eay 24/9/91
5386d7f5d3SJohn Marino * 1.4 Speed up by 400% :-)
5486d7f5d3SJohn Marino * 1.3 added register declarations.
5586d7f5d3SJohn Marino * 1.2 unrolled make_key_sched a bit more
5686d7f5d3SJohn Marino * 1.1 added norm_expand_bits
5786d7f5d3SJohn Marino * 1.0 First working version
5886d7f5d3SJohn Marino */
5986d7f5d3SJohn Marino #include <sys/param.h>
6086d7f5d3SJohn Marino #include <sys/systm.h>
6186d7f5d3SJohn Marino #include <crypto/des/des_locl.h>
6286d7f5d3SJohn Marino #include <crypto/des/podd.h>
6386d7f5d3SJohn Marino #include <crypto/des/sk.h>
6486d7f5d3SJohn Marino
6586d7f5d3SJohn Marino int des_check_key=0;
6686d7f5d3SJohn Marino
des_set_odd_parity(des_cblock * key)6786d7f5d3SJohn Marino void des_set_odd_parity(des_cblock *key)
6886d7f5d3SJohn Marino {
6986d7f5d3SJohn Marino int i;
7086d7f5d3SJohn Marino
7186d7f5d3SJohn Marino for (i=0; i<DES_KEY_SZ; i++)
7286d7f5d3SJohn Marino (*key)[i]=odd_parity[(*key)[i]];
7386d7f5d3SJohn Marino }
7486d7f5d3SJohn Marino
des_check_key_parity(des_cblock * key)7586d7f5d3SJohn Marino int des_check_key_parity(des_cblock *key)
7686d7f5d3SJohn Marino {
7786d7f5d3SJohn Marino int i;
7886d7f5d3SJohn Marino
7986d7f5d3SJohn Marino for (i=0; i<DES_KEY_SZ; i++)
8086d7f5d3SJohn Marino {
8186d7f5d3SJohn Marino if ((*key)[i] != odd_parity[(*key)[i]])
8286d7f5d3SJohn Marino return(0);
8386d7f5d3SJohn Marino }
8486d7f5d3SJohn Marino return(1);
8586d7f5d3SJohn Marino }
8686d7f5d3SJohn Marino
8786d7f5d3SJohn Marino /* Weak and semi week keys as take from
8886d7f5d3SJohn Marino * %A D.W. Davies
8986d7f5d3SJohn Marino * %A W.L. Price
9086d7f5d3SJohn Marino * %T Security for Computer Networks
9186d7f5d3SJohn Marino * %I John Wiley & Sons
9286d7f5d3SJohn Marino * %D 1984
9386d7f5d3SJohn Marino * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
9486d7f5d3SJohn Marino * (and actual cblock values).
9586d7f5d3SJohn Marino */
9686d7f5d3SJohn Marino #define NUM_WEAK_KEY 16
9786d7f5d3SJohn Marino static des_cblock weak_keys[NUM_WEAK_KEY]={
9886d7f5d3SJohn Marino /* weak keys */
9986d7f5d3SJohn Marino {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
10086d7f5d3SJohn Marino {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
10186d7f5d3SJohn Marino {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
10286d7f5d3SJohn Marino {0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
10386d7f5d3SJohn Marino /* semi-weak keys */
10486d7f5d3SJohn Marino {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
10586d7f5d3SJohn Marino {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
10686d7f5d3SJohn Marino {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
10786d7f5d3SJohn Marino {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
10886d7f5d3SJohn Marino {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
10986d7f5d3SJohn Marino {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
11086d7f5d3SJohn Marino {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
11186d7f5d3SJohn Marino {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
11286d7f5d3SJohn Marino {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
11386d7f5d3SJohn Marino {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
11486d7f5d3SJohn Marino {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
11586d7f5d3SJohn Marino {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
11686d7f5d3SJohn Marino
des_is_weak_key(des_cblock * key)11786d7f5d3SJohn Marino int des_is_weak_key(des_cblock *key)
11886d7f5d3SJohn Marino {
11986d7f5d3SJohn Marino int i;
12086d7f5d3SJohn Marino
12186d7f5d3SJohn Marino for (i=0; i<NUM_WEAK_KEY; i++)
12286d7f5d3SJohn Marino /* Added == 0 to comparison, I obviously don't run
12386d7f5d3SJohn Marino * this section very often :-(, thanks to
12486d7f5d3SJohn Marino * engineering@MorningStar.Com for the fix
12586d7f5d3SJohn Marino * eay 93/06/29
12686d7f5d3SJohn Marino * Another problem, I was comparing only the first 4
12786d7f5d3SJohn Marino * bytes, 97/03/18 */
12886d7f5d3SJohn Marino if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
12986d7f5d3SJohn Marino return(0);
13086d7f5d3SJohn Marino }
13186d7f5d3SJohn Marino
13286d7f5d3SJohn Marino /* NOW DEFINED IN des_local.h
13386d7f5d3SJohn Marino * See ecb_encrypt.c for a pseudo description of these macros.
13486d7f5d3SJohn Marino * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
13586d7f5d3SJohn Marino * (b)^=(t),\
13686d7f5d3SJohn Marino * (a)=((a)^((t)<<(n))))
13786d7f5d3SJohn Marino */
13886d7f5d3SJohn Marino
13986d7f5d3SJohn Marino #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
14086d7f5d3SJohn Marino (a)=(a)^(t)^(t>>(16-(n))))
14186d7f5d3SJohn Marino
des_set_key(des_cblock * key,des_key_schedule schedule)14286d7f5d3SJohn Marino int des_set_key(des_cblock *key, des_key_schedule schedule)
14386d7f5d3SJohn Marino {
14486d7f5d3SJohn Marino if (des_check_key)
14586d7f5d3SJohn Marino {
14686d7f5d3SJohn Marino return des_set_key_checked(key, schedule);
14786d7f5d3SJohn Marino }
14886d7f5d3SJohn Marino else
14986d7f5d3SJohn Marino {
15086d7f5d3SJohn Marino des_set_key_unchecked(key, schedule);
15186d7f5d3SJohn Marino return 0;
15286d7f5d3SJohn Marino }
15386d7f5d3SJohn Marino }
15486d7f5d3SJohn Marino
15586d7f5d3SJohn Marino /* return 0 if key parity is odd (correct),
15686d7f5d3SJohn Marino * return -1 if key parity error,
15786d7f5d3SJohn Marino * return -2 if illegal weak key.
15886d7f5d3SJohn Marino */
des_set_key_checked(des_cblock * key,des_key_schedule schedule)15986d7f5d3SJohn Marino int des_set_key_checked(des_cblock *key, des_key_schedule schedule)
16086d7f5d3SJohn Marino {
16186d7f5d3SJohn Marino if (!des_check_key_parity(key))
16286d7f5d3SJohn Marino return(-1);
16386d7f5d3SJohn Marino if (des_is_weak_key(key))
16486d7f5d3SJohn Marino return(-2);
16586d7f5d3SJohn Marino des_set_key_unchecked(key, schedule);
16686d7f5d3SJohn Marino return 0;
16786d7f5d3SJohn Marino }
16886d7f5d3SJohn Marino
des_set_key_unchecked(des_cblock * key,des_key_schedule schedule)16986d7f5d3SJohn Marino void des_set_key_unchecked(des_cblock *key, des_key_schedule schedule)
17086d7f5d3SJohn Marino {
17186d7f5d3SJohn Marino static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
17286d7f5d3SJohn Marino DES_LONG c,d,t,s,t2;
17386d7f5d3SJohn Marino const unsigned char *in;
17486d7f5d3SJohn Marino DES_LONG *k;
17586d7f5d3SJohn Marino int i;
17686d7f5d3SJohn Marino
17786d7f5d3SJohn Marino k = &schedule->ks.deslong[0];
17886d7f5d3SJohn Marino in = &(*key)[0];
17986d7f5d3SJohn Marino
18086d7f5d3SJohn Marino c2l(in,c);
18186d7f5d3SJohn Marino c2l(in,d);
18286d7f5d3SJohn Marino
18386d7f5d3SJohn Marino /* do PC1 in 47 simple operations :-)
18486d7f5d3SJohn Marino * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
18586d7f5d3SJohn Marino * for the inspiration. :-) */
18686d7f5d3SJohn Marino PERM_OP (d,c,t,4,0x0f0f0f0fL);
18786d7f5d3SJohn Marino HPERM_OP(c,t,-2,0xcccc0000L);
18886d7f5d3SJohn Marino HPERM_OP(d,t,-2,0xcccc0000L);
18986d7f5d3SJohn Marino PERM_OP (d,c,t,1,0x55555555L);
19086d7f5d3SJohn Marino PERM_OP (c,d,t,8,0x00ff00ffL);
19186d7f5d3SJohn Marino PERM_OP (d,c,t,1,0x55555555L);
19286d7f5d3SJohn Marino d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) |
19386d7f5d3SJohn Marino ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
19486d7f5d3SJohn Marino c&=0x0fffffffL;
19586d7f5d3SJohn Marino
19686d7f5d3SJohn Marino for (i=0; i<ITERATIONS; i++)
19786d7f5d3SJohn Marino {
19886d7f5d3SJohn Marino if (shifts2[i])
19986d7f5d3SJohn Marino { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
20086d7f5d3SJohn Marino else
20186d7f5d3SJohn Marino { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
20286d7f5d3SJohn Marino c&=0x0fffffffL;
20386d7f5d3SJohn Marino d&=0x0fffffffL;
20486d7f5d3SJohn Marino /* could be a few less shifts but I am to lazy at this
20586d7f5d3SJohn Marino * point in time to investigate */
20686d7f5d3SJohn Marino s= des_skb[0][ (c )&0x3f ]|
20786d7f5d3SJohn Marino des_skb[1][((c>> 6L)&0x03)|((c>> 7L)&0x3c)]|
20886d7f5d3SJohn Marino des_skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)]|
20986d7f5d3SJohn Marino des_skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) |
21086d7f5d3SJohn Marino ((c>>22L)&0x38)];
21186d7f5d3SJohn Marino t= des_skb[4][ (d )&0x3f ]|
21286d7f5d3SJohn Marino des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
21386d7f5d3SJohn Marino des_skb[6][ (d>>15L)&0x3f ]|
21486d7f5d3SJohn Marino des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
21586d7f5d3SJohn Marino
21686d7f5d3SJohn Marino /* table contained 0213 4657 */
21786d7f5d3SJohn Marino t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
21886d7f5d3SJohn Marino *(k++)=ROTATE(t2,30)&0xffffffffL;
21986d7f5d3SJohn Marino
22086d7f5d3SJohn Marino t2=((s>>16L)|(t&0xffff0000L));
22186d7f5d3SJohn Marino *(k++)=ROTATE(t2,26)&0xffffffffL;
22286d7f5d3SJohn Marino }
22386d7f5d3SJohn Marino }
22486d7f5d3SJohn Marino
des_key_sched(des_cblock * key,des_key_schedule schedule)22586d7f5d3SJohn Marino int des_key_sched(des_cblock *key, des_key_schedule schedule)
22686d7f5d3SJohn Marino {
22786d7f5d3SJohn Marino return(des_set_key(key,schedule));
22886d7f5d3SJohn Marino }
22986d7f5d3SJohn Marino
des_fixup_key_parity(des_cblock * key)23086d7f5d3SJohn Marino void des_fixup_key_parity(des_cblock *key)
23186d7f5d3SJohn Marino {
23286d7f5d3SJohn Marino des_set_odd_parity(key);
23386d7f5d3SJohn Marino }
234