Deleted Added
full compact
des_setkey.c (91671) des_setkey.c (130443)
1/* $FreeBSD: head/sys/crypto/des/des_setkey.c 91671 2002-03-05 09:19:02Z ume $ */
2/* $KAME: des_setkey.c,v 1.7 2001/09/10 04:03:58 itojun Exp $ */
3
4/* crypto/des/set_key.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 */
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__FBSDID("$FreeBSD: head/sys/crypto/des/des_setkey.c 130443 2004-06-14 00:38:54Z obrien $");
61
58#include <sys/param.h>
59#include <sys/systm.h>
60#include <crypto/des/des_locl.h>
61#include <crypto/des/podd.h>
62#include <crypto/des/sk.h>
63
64int des_check_key=0;
65
66void des_set_odd_parity(des_cblock *key)
67{
68 int i;
69
70 for (i=0; i<DES_KEY_SZ; i++)
71 (*key)[i]=odd_parity[(*key)[i]];
72}
73
74int des_check_key_parity(des_cblock *key)
75{
76 int i;
77
78 for (i=0; i<DES_KEY_SZ; i++)
79 {
80 if ((*key)[i] != odd_parity[(*key)[i]])
81 return(0);
82 }
83 return(1);
84}
85
86/* Weak and semi week keys as take from
87 * %A D.W. Davies
88 * %A W.L. Price
89 * %T Security for Computer Networks
90 * %I John Wiley & Sons
91 * %D 1984
92 * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
93 * (and actual cblock values).
94 */
95#define NUM_WEAK_KEY 16
96static des_cblock weak_keys[NUM_WEAK_KEY]={
97 /* weak keys */
98 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
99 {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
100 {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
101 {0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
102 /* semi-weak keys */
103 {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
104 {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
105 {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
106 {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
107 {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
108 {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
109 {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
110 {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
111 {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
112 {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
113 {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
114 {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
115
116int des_is_weak_key(des_cblock *key)
117{
118 int i;
119
120 for (i=0; i<NUM_WEAK_KEY; i++)
121 /* Added == 0 to comparison, I obviously don't run
122 * this section very often :-(, thanks to
123 * engineering@MorningStar.Com for the fix
124 * eay 93/06/29
125 * Another problem, I was comparing only the first 4
126 * bytes, 97/03/18 */
127 if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
128 return(0);
129}
130
131/* NOW DEFINED IN des_local.h
132 * See ecb_encrypt.c for a pseudo description of these macros.
133 * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
134 * (b)^=(t),\
135 * (a)=((a)^((t)<<(n))))
136 */
137
138#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
139 (a)=(a)^(t)^(t>>(16-(n))))
140
141int des_set_key(des_cblock *key, des_key_schedule schedule)
142{
143 if (des_check_key)
144 {
145 return des_set_key_checked(key, schedule);
146 }
147 else
148 {
149 des_set_key_unchecked(key, schedule);
150 return 0;
151 }
152}
153
154/* return 0 if key parity is odd (correct),
155 * return -1 if key parity error,
156 * return -2 if illegal weak key.
157 */
158int des_set_key_checked(des_cblock *key, des_key_schedule schedule)
159{
160 if (!des_check_key_parity(key))
161 return(-1);
162 if (des_is_weak_key(key))
163 return(-2);
164 des_set_key_unchecked(key, schedule);
165 return 0;
166}
167
168void des_set_key_unchecked(des_cblock *key, des_key_schedule schedule)
169{
170 static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
171 register DES_LONG c,d,t,s,t2;
172 register const unsigned char *in;
173 register DES_LONG *k;
174 register int i;
175
176 k = &schedule->ks.deslong[0];
177 in = &(*key)[0];
178
179 c2l(in,c);
180 c2l(in,d);
181
182 /* do PC1 in 47 simple operations :-)
183 * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
184 * for the inspiration. :-) */
185 PERM_OP (d,c,t,4,0x0f0f0f0fL);
186 HPERM_OP(c,t,-2,0xcccc0000L);
187 HPERM_OP(d,t,-2,0xcccc0000L);
188 PERM_OP (d,c,t,1,0x55555555L);
189 PERM_OP (c,d,t,8,0x00ff00ffL);
190 PERM_OP (d,c,t,1,0x55555555L);
191 d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) |
192 ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
193 c&=0x0fffffffL;
194
195 for (i=0; i<ITERATIONS; i++)
196 {
197 if (shifts2[i])
198 { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
199 else
200 { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
201 c&=0x0fffffffL;
202 d&=0x0fffffffL;
203 /* could be a few less shifts but I am to lazy at this
204 * point in time to investigate */
205 s= des_skb[0][ (c )&0x3f ]|
206 des_skb[1][((c>> 6L)&0x03)|((c>> 7L)&0x3c)]|
207 des_skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)]|
208 des_skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) |
209 ((c>>22L)&0x38)];
210 t= des_skb[4][ (d )&0x3f ]|
211 des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
212 des_skb[6][ (d>>15L)&0x3f ]|
213 des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
214
215 /* table contained 0213 4657 */
216 t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
217 *(k++)=ROTATE(t2,30)&0xffffffffL;
218
219 t2=((s>>16L)|(t&0xffff0000L));
220 *(k++)=ROTATE(t2,26)&0xffffffffL;
221 }
222}
223
224int des_key_sched(des_cblock *key, des_key_schedule schedule)
225{
226 return(des_set_key(key,schedule));
227}
228
229void des_fixup_key_parity(des_cblock *key)
230{
231 des_set_odd_parity(key);
232}
62#include <sys/param.h>
63#include <sys/systm.h>
64#include <crypto/des/des_locl.h>
65#include <crypto/des/podd.h>
66#include <crypto/des/sk.h>
67
68int des_check_key=0;
69
70void des_set_odd_parity(des_cblock *key)
71{
72 int i;
73
74 for (i=0; i<DES_KEY_SZ; i++)
75 (*key)[i]=odd_parity[(*key)[i]];
76}
77
78int des_check_key_parity(des_cblock *key)
79{
80 int i;
81
82 for (i=0; i<DES_KEY_SZ; i++)
83 {
84 if ((*key)[i] != odd_parity[(*key)[i]])
85 return(0);
86 }
87 return(1);
88}
89
90/* Weak and semi week keys as take from
91 * %A D.W. Davies
92 * %A W.L. Price
93 * %T Security for Computer Networks
94 * %I John Wiley & Sons
95 * %D 1984
96 * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
97 * (and actual cblock values).
98 */
99#define NUM_WEAK_KEY 16
100static des_cblock weak_keys[NUM_WEAK_KEY]={
101 /* weak keys */
102 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
103 {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
104 {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
105 {0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
106 /* semi-weak keys */
107 {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
108 {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
109 {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
110 {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
111 {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
112 {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
113 {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
114 {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
115 {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
116 {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
117 {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
118 {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
119
120int des_is_weak_key(des_cblock *key)
121{
122 int i;
123
124 for (i=0; i<NUM_WEAK_KEY; i++)
125 /* Added == 0 to comparison, I obviously don't run
126 * this section very often :-(, thanks to
127 * engineering@MorningStar.Com for the fix
128 * eay 93/06/29
129 * Another problem, I was comparing only the first 4
130 * bytes, 97/03/18 */
131 if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
132 return(0);
133}
134
135/* NOW DEFINED IN des_local.h
136 * See ecb_encrypt.c for a pseudo description of these macros.
137 * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
138 * (b)^=(t),\
139 * (a)=((a)^((t)<<(n))))
140 */
141
142#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
143 (a)=(a)^(t)^(t>>(16-(n))))
144
145int des_set_key(des_cblock *key, des_key_schedule schedule)
146{
147 if (des_check_key)
148 {
149 return des_set_key_checked(key, schedule);
150 }
151 else
152 {
153 des_set_key_unchecked(key, schedule);
154 return 0;
155 }
156}
157
158/* return 0 if key parity is odd (correct),
159 * return -1 if key parity error,
160 * return -2 if illegal weak key.
161 */
162int des_set_key_checked(des_cblock *key, des_key_schedule schedule)
163{
164 if (!des_check_key_parity(key))
165 return(-1);
166 if (des_is_weak_key(key))
167 return(-2);
168 des_set_key_unchecked(key, schedule);
169 return 0;
170}
171
172void des_set_key_unchecked(des_cblock *key, des_key_schedule schedule)
173{
174 static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
175 register DES_LONG c,d,t,s,t2;
176 register const unsigned char *in;
177 register DES_LONG *k;
178 register int i;
179
180 k = &schedule->ks.deslong[0];
181 in = &(*key)[0];
182
183 c2l(in,c);
184 c2l(in,d);
185
186 /* do PC1 in 47 simple operations :-)
187 * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
188 * for the inspiration. :-) */
189 PERM_OP (d,c,t,4,0x0f0f0f0fL);
190 HPERM_OP(c,t,-2,0xcccc0000L);
191 HPERM_OP(d,t,-2,0xcccc0000L);
192 PERM_OP (d,c,t,1,0x55555555L);
193 PERM_OP (c,d,t,8,0x00ff00ffL);
194 PERM_OP (d,c,t,1,0x55555555L);
195 d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) |
196 ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
197 c&=0x0fffffffL;
198
199 for (i=0; i<ITERATIONS; i++)
200 {
201 if (shifts2[i])
202 { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
203 else
204 { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
205 c&=0x0fffffffL;
206 d&=0x0fffffffL;
207 /* could be a few less shifts but I am to lazy at this
208 * point in time to investigate */
209 s= des_skb[0][ (c )&0x3f ]|
210 des_skb[1][((c>> 6L)&0x03)|((c>> 7L)&0x3c)]|
211 des_skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)]|
212 des_skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) |
213 ((c>>22L)&0x38)];
214 t= des_skb[4][ (d )&0x3f ]|
215 des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
216 des_skb[6][ (d>>15L)&0x3f ]|
217 des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
218
219 /* table contained 0213 4657 */
220 t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
221 *(k++)=ROTATE(t2,30)&0xffffffffL;
222
223 t2=((s>>16L)|(t&0xffff0000L));
224 *(k++)=ROTATE(t2,26)&0xffffffffL;
225 }
226}
227
228int des_key_sched(des_cblock *key, des_key_schedule schedule)
229{
230 return(des_set_key(key,schedule));
231}
232
233void des_fixup_key_parity(des_cblock *key)
234{
235 des_set_odd_parity(key);
236}