]> git.saurik.com Git - apple/xnu.git/blob - bsd/crypto/des/des_cbc.c
xnu-123.5.tar.gz
[apple/xnu.git] / bsd / crypto / des / des_cbc.c
1 /*
2 * heavily modified by Yoshifumi Nishida <nishida@sfc.wide.ad.jp>.
3 * then, completely rewrote by Jun-ichiro itojun Itoh <itojun@itojun.org>,
4 * 1997.
5 */
6 /* crypto/des/cbc_enc.c */
7 /* Copyright (C) 1995-1996 Eric Young (eay@mincom.oz.au)
8 * All rights reserved.
9 *
10 * This file is part of an SSL implementation written
11 * by Eric Young (eay@mincom.oz.au).
12 * The implementation was written so as to conform with Netscapes SSL
13 * specification. This library and applications are
14 * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
15 * as long as the following conditions are aheared to.
16 *
17 * Copyright remains Eric Young's, and as such any Copyright notices in
18 * the code are not to be removed. If this code is used in a product,
19 * Eric Young should be given attribution as the author of the parts 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 software developed by Eric Young (eay@mincom.oz.au)
34 *
35 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 *
47 * The licence and distribution terms for any publically available version or
48 * derivative of this code cannot be changed. i.e. this code cannot simply be
49 * copied and put under another distribution licence
50 * [including the GNU Public Licence.]
51 */
52
53 #include <crypto/des/des_locl.h>
54
55 #define panic(x) {printf(x); return;}
56
57 void des_cbc_encrypt(m0, skip, length, schedule, ivec, mode)
58 struct mbuf *m0;
59 size_t skip;
60 size_t length;
61 des_key_schedule schedule;
62 des_cblock (*ivec);
63 int mode;
64 {
65 u_int8_t inbuf[8], outbuf[8];
66 struct mbuf *m;
67 size_t off;
68 register DES_LONG tin0, tin1;
69 register DES_LONG tout0, tout1;
70 DES_LONG tin[2];
71 u_int8_t *iv;
72
73 /* sanity checks */
74 if (m0->m_pkthdr.len < skip) {
75 printf("mbuf length < skip\n");
76 return;
77 }
78 if (m0->m_pkthdr.len < length) {
79 printf("mbuf length < encrypt length\n");
80 return;
81 }
82 if (m0->m_pkthdr.len < skip + length) {
83 printf("mbuf length < skip + encrypt length\n");
84 return;
85 }
86 if (length % 8) {
87 printf("length is not multiple of 8\n");
88 return;
89 }
90
91 m = m0;
92 off = 0;
93
94 /* skip over the header */
95 while (skip) {
96 if (!m)
97 panic("mbuf chain?\n");
98 if (m->m_len <= skip) {
99 skip -= m->m_len;
100 m = m->m_next;
101 off = 0;
102 } else {
103 off = skip;
104 skip = 0;
105 }
106 }
107
108 /* initialize */
109 tin0 = tin1 = tout0 = tout1 = 0;
110 tin[0] = tin[1] = 0;
111
112 if (mode == DES_ENCRYPT) {
113 u_int8_t *in, *out;
114
115 iv = (u_int8_t *)ivec;
116 c2l(iv, tout0);
117 c2l(iv, tout1);
118
119 while (0 < length) {
120 if (!m)
121 panic("mbuf chain?\n");
122
123 /*
124 * copy the source into input buffer.
125 * don't update off or m, since we need to use them * later.
126 */
127 if (off + 8 <= m->m_len)
128 bcopy(mtod(m, u_int8_t *) + off, &inbuf[0], 8);
129 else {
130 struct mbuf *n;
131 size_t noff;
132 u_int8_t *p;
133 u_int8_t *in;
134
135 n = m;
136 noff = off;
137 p = mtod(n, u_int8_t *) + noff;
138
139 in = &inbuf[0];
140 while (in - &inbuf[0] < 8) {
141 if (!p)
142 panic("mbuf chain?\n");
143
144 *in++ = *p++;
145 noff++;
146 if (noff < n->m_len)
147 continue;
148 do {
149 n = n->m_next;
150 } while (n && ! n->m_len);
151 noff = 0;
152 if (n)
153 p = mtod(n, u_int8_t *) + noff;
154 else
155 p = NULL;
156 }
157 }
158
159 in = &inbuf[0];
160 out = &outbuf[0];
161 c2l(in, tin0);
162 c2l(in, tin1);
163
164 tin0 ^= tout0; tin[0] = tin0;
165 tin1 ^= tout1; tin[1] = tin1;
166 des_encrypt((DES_LONG *)tin, schedule, DES_ENCRYPT);
167 tout0 = tin[0]; l2c(tout0, out);
168 tout1 = tin[1]; l2c(tout1, out);
169
170 /*
171 * copy the output buffer into the result.
172 * need to update off and m.
173 */
174 if (off + 8 < m->m_len) {
175 bcopy(&outbuf[0], mtod(m, u_int8_t *) + off, 8);
176 off += 8;
177 } else if (off + 8 == m->m_len) {
178 bcopy(&outbuf[0], mtod(m, u_int8_t *) + off, 8);
179 do {
180 m = m->m_next;
181 } while (m && ! m->m_len);
182 off = 0;
183 } else {
184 struct mbuf *n;
185 size_t noff;
186 u_int8_t *p;
187 u_int8_t *out;
188
189 n = m;
190 noff = off;
191 p = mtod(n, u_int8_t *) + noff;
192
193 out = &outbuf[0];
194 while (out - &outbuf[0] < 8) {
195 if (!p)
196 panic("mbuf chain?");
197 *p++ = *out++;
198 noff++;
199 if (noff < n->m_len)
200 continue;
201 do {
202 n = n->m_next;
203 } while (n && ! n->m_len);
204 noff = 0;
205 if (n)
206 p = mtod(n, u_int8_t *) + noff;
207 else
208 p = NULL;
209 }
210
211 m = n;
212 off = noff;
213 }
214
215 length -= 8;
216 }
217 } else if (mode == DES_DECRYPT) {
218 register DES_LONG xor0, xor1;
219 u_int8_t *in, *out;
220
221 xor0 = xor1 = 0;
222 iv = (u_int8_t *)ivec;
223 c2l(iv, xor0);
224 c2l(iv, xor1);
225
226 while (0 < length) {
227 if (!m)
228 panic("mbuf chain?\n");
229
230 /*
231 * copy the source into input buffer.
232 * don't update off or m, since we need to use them * later.
233 */
234 if (off + 8 <= m->m_len)
235 bcopy(mtod(m, u_int8_t *) + off, &inbuf[0], 8);
236 else {
237 struct mbuf *n;
238 size_t noff;
239 u_int8_t *p;
240 u_int8_t *in;
241
242 n = m;
243 noff = off;
244 p = mtod(n, u_int8_t *) + noff;
245
246 in = &inbuf[0];
247 while (in - &inbuf[0] < 8) {
248 if (!p)
249 panic("mbuf chain?\n");
250 *in++ = *p++;
251 noff++;
252 if (noff < n->m_len)
253 continue;
254 do {
255 n = n->m_next;
256 } while (n && ! n->m_len);
257 noff = 0;
258 if (n)
259 p = mtod(n, u_int8_t *) + noff;
260 else
261 p = NULL;
262 }
263 }
264
265 in = &inbuf[0];
266 out = &outbuf[0];
267 c2l(in, tin0); tin[0] = tin0;
268 c2l(in, tin1); tin[1] = tin1;
269 des_encrypt((DES_LONG *)tin, schedule, DES_DECRYPT);
270 tout0 = tin[0] ^ xor0;
271 tout1 = tin[1] ^ xor1;
272 l2c(tout0, out);
273 l2c(tout1, out);
274 xor0 = tin0;
275 xor1 = tin1;
276
277
278 /*
279 * copy the output buffer into the result.
280 * need to update off and m.
281 */
282 if (off + 8 < m->m_len) {
283 bcopy(&outbuf[0], mtod(m, u_int8_t *) + off, 8);
284 off += 8;
285 } else if (off + 8 == m->m_len) {
286 bcopy(&outbuf[0], mtod(m, u_int8_t *) + off, 8);
287 do {
288 m = m->m_next;
289 } while (m && ! m->m_len);
290 off = 0;
291 } else {
292 struct mbuf *n;
293 size_t noff;
294 u_int8_t *p;
295 u_int8_t *out;
296
297 n = m;
298 noff = off;
299 p = mtod(n, u_int8_t *) + noff;
300
301 out = &outbuf[0];
302 while (out - &outbuf[0] < 8) {
303 if (!p)
304 panic("mbuf chain?\n");
305 *p++ = *out++;
306 noff++;
307 if (noff < n->m_len)
308 continue;
309 do {
310 n = n->m_next;
311 } while (n && ! n->m_len);
312 noff = 0;
313 if (n)
314 p = mtod(n, u_int8_t *) + noff;
315 else
316 p = NULL;
317 }
318
319 m = n;
320 off = noff;
321 }
322
323 length -= 8;
324 }
325 }
326 }