]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/crypto/des/des_setkey.c
sqlite3: Vendor import of sqlite3 3.43.1
[FreeBSD/FreeBSD.git] / sys / crypto / des / des_setkey.c
1 /*      $KAME: des_setkey.c,v 1.7 2001/09/10 04:03:58 itojun Exp $      */
2
3 /* crypto/des/set_key.c */
4
5 /* Copyright (C) 1995-1996 Eric Young (eay@mincom.oz.au)
6  * All rights reserved.
7  *
8  * This file is part of an SSL implementation written
9  * by Eric Young (eay@mincom.oz.au).
10  * The implementation was written so as to conform with Netscapes SSL
11  * specification.  This library and applications are
12  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
13  * as long as the following conditions are aheared to.
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.  If this code is used in a product,
17  * Eric Young should be given attribution as the author of the parts used.
18  * This can be in the form of a textual message at program startup or
19  * in documentation (online or textual) provided with the package.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  * 1. Redistributions of source code must retain the copyright
25  *    notice, this list of conditions and the following disclaimer.
26  * 2. Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in the
28  *    documentation and/or other materials provided with the distribution.
29  * 3. All advertising materials mentioning features or use of this software
30  *    must display the following acknowledgement:
31  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
32  *
33  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  *
45  * The licence and distribution terms for any publically available version or
46  * derivative of this code cannot be changed.  i.e. this code cannot simply be
47  * copied and put under another distribution licence
48  * [including the GNU Public Licence.]
49  */
50
51 /* set_key.c v 1.4 eay 24/9/91
52  * 1.4 Speed up by 400% :-)
53  * 1.3 added register declarations.
54  * 1.2 unrolled make_key_sched a bit more
55  * 1.1 added norm_expand_bits
56  * 1.0 First working version
57  */
58
59 #include <sys/cdefs.h>
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <crypto/des/des_locl.h>
63 #include <crypto/des/podd.h>
64 #include <crypto/des/sk.h>
65
66 int des_check_key=0;
67
68 void des_set_odd_parity(unsigned char *key)
69 {
70         int i;
71
72         for (i=0; i<DES_KEY_SZ; i++)
73                 key[i]=odd_parity[key[i]];
74 }
75
76 int des_check_key_parity(const unsigned char *key)
77 {
78         int i;
79
80         for (i=0; i<DES_KEY_SZ; i++)
81                 {
82                 if (key[i] != odd_parity[key[i]])
83                         return(0);
84                 }
85         return(1);
86 }
87
88 /* Weak and semi week keys as take from
89  * %A D.W. Davies
90  * %A W.L. Price
91  * %T Security for Computer Networks
92  * %I John Wiley & Sons
93  * %D 1984
94  * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
95  * (and actual cblock values).
96  */
97 #define NUM_WEAK_KEY    16
98 static des_cblock weak_keys[NUM_WEAK_KEY]={
99         /* weak keys */
100         {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
101         {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
102         {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
103         {0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
104         /* semi-weak keys */
105         {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
106         {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
107         {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
108         {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
109         {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
110         {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
111         {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
112         {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
113         {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
114         {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
115         {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
116         {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
117
118 int des_is_weak_key(const unsigned char *key)
119 {
120         int i;
121
122         for (i=0; i<NUM_WEAK_KEY; i++)
123                 /* Added == 0 to comparison, I obviously don't run
124                  * this section very often :-(, thanks to
125                  * engineering@MorningStar.Com for the fix
126                  * eay 93/06/29
127                  * Another problem, I was comparing only the first 4
128                  * bytes, 97/03/18 */
129                 if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
130         return(0);
131 }
132
133 /* NOW DEFINED IN des_local.h
134  * See ecb_encrypt.c for a pseudo description of these macros. 
135  * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
136  *      (b)^=(t),\
137  *      (a)=((a)^((t)<<(n))))
138  */
139
140 #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
141         (a)=(a)^(t)^(t>>(16-(n))))
142
143 int des_set_key(const unsigned char *key, des_key_schedule schedule)
144 {
145         if (des_check_key)
146         {
147                 return des_set_key_checked(key, schedule);
148         }
149         else
150         {
151                 des_set_key_unchecked(key, schedule);
152                 return 0;
153         }
154 }
155
156 /* return 0 if key parity is odd (correct),
157  * return -1 if key parity error,
158  * return -2 if illegal weak key.
159  */
160 int des_set_key_checked(const unsigned char *key, des_key_schedule schedule)
161 {
162         if (!des_check_key_parity(key))
163                 return(-1);
164         if (des_is_weak_key(key))
165                 return(-2);
166         des_set_key_unchecked(key, schedule);
167         return 0;
168 }
169
170 void des_set_key_unchecked(const unsigned char *key, des_key_schedule schedule)
171 {
172         static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
173         DES_LONG c,d,t,s,t2;
174         const unsigned char *in;
175         DES_LONG *k;
176         int i;
177
178         k = &schedule->ks.deslong[0];
179         in = key;
180
181         c2l(in,c);
182         c2l(in,d);
183
184         /* do PC1 in 47 simple operations :-)
185          * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
186          * for the inspiration. :-) */
187         PERM_OP (d,c,t,4,0x0f0f0f0fL);
188         HPERM_OP(c,t,-2,0xcccc0000L);
189         HPERM_OP(d,t,-2,0xcccc0000L);
190         PERM_OP (d,c,t,1,0x55555555L);
191         PERM_OP (c,d,t,8,0x00ff00ffL);
192         PERM_OP (d,c,t,1,0x55555555L);
193         d=      (((d&0x000000ffL)<<16L)| (d&0x0000ff00L)     |
194                  ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
195         c&=0x0fffffffL;
196
197         for (i=0; i<ITERATIONS; i++)
198         {
199                 if (shifts2[i])
200                         { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
201                 else
202                         { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
203                 c&=0x0fffffffL;
204                 d&=0x0fffffffL;
205                 /* could be a few less shifts but I am to lazy at this
206                 * point in time to investigate */
207                 s=      des_skb[0][ (c    )&0x3f                ]|
208                         des_skb[1][((c>> 6L)&0x03)|((c>> 7L)&0x3c)]|
209                         des_skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)]|
210                         des_skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) |
211                                                         ((c>>22L)&0x38)];
212                 t=      des_skb[4][ (d    )&0x3f                ]|
213                         des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
214                         des_skb[6][ (d>>15L)&0x3f                ]|
215                         des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
216
217                 /* table contained 0213 4657 */
218                 t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
219                 *(k++)=ROTATE(t2,30)&0xffffffffL;
220
221                 t2=((s>>16L)|(t&0xffff0000L));
222                 *(k++)=ROTATE(t2,26)&0xffffffffL;
223         }
224 }
225
226 int des_key_sched(const unsigned char *key, des_key_schedule schedule)
227 {
228         return(des_set_key(key,schedule));
229 }
230
231 void des_fixup_key_parity(unsigned char *key)
232 {
233         des_set_odd_parity(key);
234 }