diff options
author | ryker <> | 1998-10-05 20:13:14 +0000 |
---|---|---|
committer | ryker <> | 1998-10-05 20:13:14 +0000 |
commit | aeeae06a79815dc190061534d47236cec09f9e32 (patch) | |
tree | 851692b9c2f9c04f077666855641900f19fdb217 /src/lib/libcrypto/des/set_key.c | |
parent | a4f79641824cbf9f60ca9d1168d1fcc46717a82a (diff) | |
download | openbsd-aeeae06a79815dc190061534d47236cec09f9e32.tar.gz openbsd-aeeae06a79815dc190061534d47236cec09f9e32.tar.bz2 openbsd-aeeae06a79815dc190061534d47236cec09f9e32.zip |
Import of SSLeay-0.9.0b with RSA and IDEA stubbed + OpenBSD build
functionality for shared libs.
Note that routines such as sslv2_init and friends that use RSA will
not work due to lack of RSA in this library.
Needs documentation and help from ports for easy upgrade to full
functionality where legally possible.
Diffstat (limited to 'src/lib/libcrypto/des/set_key.c')
-rw-r--r-- | src/lib/libcrypto/des/set_key.c | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/src/lib/libcrypto/des/set_key.c b/src/lib/libcrypto/des/set_key.c new file mode 100644 index 0000000000..c3bcd7ee2b --- /dev/null +++ b/src/lib/libcrypto/des/set_key.c | |||
@@ -0,0 +1,246 @@ | |||
1 | /* crypto/des/set_key.c */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This package is an SSL implementation written | ||
6 | * by Eric Young (eay@cryptsoft.com). | ||
7 | * The implementation was written so as to conform with Netscapes SSL. | ||
8 | * | ||
9 | * This library is free for commercial and non-commercial use as long as | ||
10 | * the following conditions are aheared to. The following conditions | ||
11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
13 | * included with this distribution is covered by the same copyright terms | ||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
15 | * | ||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
17 | * the code are not to be removed. | ||
18 | * If this package is used in a product, Eric Young should be given attribution | ||
19 | * as the author of the parts of the library used. | ||
20 | * This can be in the form of a textual message at program startup or | ||
21 | * in documentation (online or textual) provided with the package. | ||
22 | * | ||
23 | * Redistribution and use in source and binary forms, with or without | ||
24 | * modification, are permitted provided that the following conditions | ||
25 | * are met: | ||
26 | * 1. Redistributions of source code must retain the copyright | ||
27 | * notice, this list of conditions and the following disclaimer. | ||
28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer in the | ||
30 | * documentation and/or other materials provided with the distribution. | ||
31 | * 3. All advertising materials mentioning features or use of this software | ||
32 | * must display the following acknowledgement: | ||
33 | * "This product includes cryptographic software written by | ||
34 | * Eric Young (eay@cryptsoft.com)" | ||
35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
36 | * being used are not cryptographic related :-). | ||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
38 | * the apps directory (application code) you must include an acknowledgement: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
51 | * SUCH DAMAGE. | ||
52 | * | ||
53 | * The licence and distribution terms for any publically available version or | ||
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
55 | * copied and put under another distribution licence | ||
56 | * [including the GNU Public Licence.] | ||
57 | */ | ||
58 | |||
59 | /* set_key.c v 1.4 eay 24/9/91 | ||
60 | * 1.4 Speed up by 400% :-) | ||
61 | * 1.3 added register declarations. | ||
62 | * 1.2 unrolled make_key_sched a bit more | ||
63 | * 1.1 added norm_expand_bits | ||
64 | * 1.0 First working version | ||
65 | */ | ||
66 | #include "des_locl.h" | ||
67 | #include "podd.h" | ||
68 | #include "sk.h" | ||
69 | |||
70 | #ifndef NOPROTO | ||
71 | static int check_parity(des_cblock (*key)); | ||
72 | #else | ||
73 | static int check_parity(); | ||
74 | #endif | ||
75 | |||
76 | int des_check_key=0; | ||
77 | |||
78 | void des_set_odd_parity(key) | ||
79 | des_cblock (*key); | ||
80 | { | ||
81 | int i; | ||
82 | |||
83 | for (i=0; i<DES_KEY_SZ; i++) | ||
84 | (*key)[i]=odd_parity[(*key)[i]]; | ||
85 | } | ||
86 | |||
87 | static int check_parity(key) | ||
88 | des_cblock (*key); | ||
89 | { | ||
90 | int i; | ||
91 | |||
92 | for (i=0; i<DES_KEY_SZ; i++) | ||
93 | { | ||
94 | if ((*key)[i] != odd_parity[(*key)[i]]) | ||
95 | return(0); | ||
96 | } | ||
97 | return(1); | ||
98 | } | ||
99 | |||
100 | /* Weak and semi week keys as take from | ||
101 | * %A D.W. Davies | ||
102 | * %A W.L. Price | ||
103 | * %T Security for Computer Networks | ||
104 | * %I John Wiley & Sons | ||
105 | * %D 1984 | ||
106 | * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference | ||
107 | * (and actual cblock values). | ||
108 | */ | ||
109 | #define NUM_WEAK_KEY 16 | ||
110 | static des_cblock weak_keys[NUM_WEAK_KEY]={ | ||
111 | /* weak keys */ | ||
112 | {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}, | ||
113 | {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE}, | ||
114 | {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F}, | ||
115 | {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0}, | ||
116 | /* semi-weak keys */ | ||
117 | {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE}, | ||
118 | {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01}, | ||
119 | {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1}, | ||
120 | {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E}, | ||
121 | {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1}, | ||
122 | {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01}, | ||
123 | {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE}, | ||
124 | {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E}, | ||
125 | {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E}, | ||
126 | {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01}, | ||
127 | {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE}, | ||
128 | {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}}; | ||
129 | |||
130 | int des_is_weak_key(key) | ||
131 | des_cblock (*key); | ||
132 | { | ||
133 | int i; | ||
134 | |||
135 | for (i=0; i<NUM_WEAK_KEY; i++) | ||
136 | /* Added == 0 to comparision, I obviously don't run | ||
137 | * this section very often :-(, thanks to | ||
138 | * engineering@MorningStar.Com for the fix | ||
139 | * eay 93/06/29 | ||
140 | * Another problem, I was comparing only the first 4 | ||
141 | * bytes, 97/03/18 */ | ||
142 | if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1); | ||
143 | return(0); | ||
144 | } | ||
145 | |||
146 | /* NOW DEFINED IN des_local.h | ||
147 | * See ecb_encrypt.c for a pseudo description of these macros. | ||
148 | * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\ | ||
149 | * (b)^=(t),\ | ||
150 | * (a)=((a)^((t)<<(n)))) | ||
151 | */ | ||
152 | |||
153 | #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\ | ||
154 | (a)=(a)^(t)^(t>>(16-(n)))) | ||
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(key, schedule) | ||
161 | des_cblock (*key); | ||
162 | des_key_schedule schedule; | ||
163 | { | ||
164 | static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0}; | ||
165 | register DES_LONG c,d,t,s,t2; | ||
166 | register unsigned char *in; | ||
167 | register DES_LONG *k; | ||
168 | register int i; | ||
169 | |||
170 | if (des_check_key) | ||
171 | { | ||
172 | if (!check_parity(key)) | ||
173 | return(-1); | ||
174 | |||
175 | if (des_is_weak_key(key)) | ||
176 | return(-2); | ||
177 | } | ||
178 | |||
179 | k=(DES_LONG *)schedule; | ||
180 | in=(unsigned char *)key; | ||
181 | |||
182 | c2l(in,c); | ||
183 | c2l(in,d); | ||
184 | |||
185 | /* do PC1 in 60 simple operations */ | ||
186 | /* PERM_OP(d,c,t,4,0x0f0f0f0fL); | ||
187 | HPERM_OP(c,t,-2, 0xcccc0000L); | ||
188 | HPERM_OP(c,t,-1, 0xaaaa0000L); | ||
189 | HPERM_OP(c,t, 8, 0x00ff0000L); | ||
190 | HPERM_OP(c,t,-1, 0xaaaa0000L); | ||
191 | HPERM_OP(d,t,-8, 0xff000000L); | ||
192 | HPERM_OP(d,t, 8, 0x00ff0000L); | ||
193 | HPERM_OP(d,t, 2, 0x33330000L); | ||
194 | d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L); | ||
195 | d=(d>>8)|((c&0xf0000000L)>>4); | ||
196 | c&=0x0fffffffL; */ | ||
197 | |||
198 | /* I now do it in 47 simple operations :-) | ||
199 | * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov) | ||
200 | * for the inspiration. :-) */ | ||
201 | PERM_OP (d,c,t,4,0x0f0f0f0fL); | ||
202 | HPERM_OP(c,t,-2,0xcccc0000L); | ||
203 | HPERM_OP(d,t,-2,0xcccc0000L); | ||
204 | PERM_OP (d,c,t,1,0x55555555L); | ||
205 | PERM_OP (c,d,t,8,0x00ff00ffL); | ||
206 | PERM_OP (d,c,t,1,0x55555555L); | ||
207 | d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) | | ||
208 | ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L)); | ||
209 | c&=0x0fffffffL; | ||
210 | |||
211 | for (i=0; i<ITERATIONS; i++) | ||
212 | { | ||
213 | if (shifts2[i]) | ||
214 | { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); } | ||
215 | else | ||
216 | { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); } | ||
217 | c&=0x0fffffffL; | ||
218 | d&=0x0fffffffL; | ||
219 | /* could be a few less shifts but I am to lazy at this | ||
220 | * point in time to investigate */ | ||
221 | s= des_skb[0][ (c )&0x3f ]| | ||
222 | des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]| | ||
223 | des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]| | ||
224 | des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) | | ||
225 | ((c>>22L)&0x38)]; | ||
226 | t= des_skb[4][ (d )&0x3f ]| | ||
227 | des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]| | ||
228 | des_skb[6][ (d>>15L)&0x3f ]| | ||
229 | des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)]; | ||
230 | |||
231 | /* table contained 0213 4657 */ | ||
232 | t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL; | ||
233 | *(k++)=ROTATE(t2,30)&0xffffffffL; | ||
234 | |||
235 | t2=((s>>16L)|(t&0xffff0000L)); | ||
236 | *(k++)=ROTATE(t2,26)&0xffffffffL; | ||
237 | } | ||
238 | return(0); | ||
239 | } | ||
240 | |||
241 | int des_key_sched(key, schedule) | ||
242 | des_cblock (*key); | ||
243 | des_key_schedule schedule; | ||
244 | { | ||
245 | return(des_set_key(key,schedule)); | ||
246 | } | ||