2 * heavily modified by Yoshifumi Nishida <nishida@sfc.wide.ad.jp>.
3 * then, completely rewrote by Jun-ichiro itojun Itoh <itojun@itojun.org>,
6 /* crypto/des/cbc_enc.c */
7 /* Copyright (C) 1995-1996 Eric Young (eay@mincom.oz.au)
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.
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)
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
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.]
53 #include <crypto/des/des_locl.h>
55 #define panic(x) {printf(x); return;}
57 void des_cbc_encrypt(m0
, skip
, length
, schedule
, ivec
, mode
)
61 des_key_schedule schedule
;
65 u_int8_t inbuf
[8], outbuf
[8];
68 register DES_LONG tin0
, tin1
;
69 register DES_LONG tout0
, tout1
;
74 if (m0
->m_pkthdr
.len
< skip
) {
75 printf("mbuf length < skip\n");
78 if (m0
->m_pkthdr
.len
< length
) {
79 printf("mbuf length < encrypt length\n");
82 if (m0
->m_pkthdr
.len
< skip
+ length
) {
83 printf("mbuf length < skip + encrypt length\n");
87 printf("length is not multiple of 8\n");
94 /* skip over the header */
97 panic("mbuf chain?\n");
98 if (m
->m_len
<= skip
) {
109 tin0
= tin1
= tout0
= tout1
= 0;
112 if (mode
== DES_ENCRYPT
) {
115 iv
= (u_int8_t
*)ivec
;
121 panic("mbuf chain?\n");
124 * copy the source into input buffer.
125 * don't update off or m, since we need to use them * later.
127 if (off
+ 8 <= m
->m_len
)
128 bcopy(mtod(m
, u_int8_t
*) + off
, &inbuf
[0], 8);
137 p
= mtod(n
, u_int8_t
*) + noff
;
140 while (in
- &inbuf
[0] < 8) {
142 panic("mbuf chain?\n");
150 } while (n
&& ! n
->m_len
);
153 p
= mtod(n
, u_int8_t
*) + noff
;
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
);
171 * copy the output buffer into the result.
172 * need to update off and m.
174 if (off
+ 8 < m
->m_len
) {
175 bcopy(&outbuf
[0], mtod(m
, u_int8_t
*) + off
, 8);
177 } else if (off
+ 8 == m
->m_len
) {
178 bcopy(&outbuf
[0], mtod(m
, u_int8_t
*) + off
, 8);
181 } while (m
&& ! m
->m_len
);
191 p
= mtod(n
, u_int8_t
*) + noff
;
194 while (out
- &outbuf
[0] < 8) {
196 panic("mbuf chain?");
203 } while (n
&& ! n
->m_len
);
206 p
= mtod(n
, u_int8_t
*) + noff
;
217 } else if (mode
== DES_DECRYPT
) {
218 register DES_LONG xor0
, xor1
;
222 iv
= (u_int8_t
*)ivec
;
228 panic("mbuf chain?\n");
231 * copy the source into input buffer.
232 * don't update off or m, since we need to use them * later.
234 if (off
+ 8 <= m
->m_len
)
235 bcopy(mtod(m
, u_int8_t
*) + off
, &inbuf
[0], 8);
244 p
= mtod(n
, u_int8_t
*) + noff
;
247 while (in
- &inbuf
[0] < 8) {
249 panic("mbuf chain?\n");
256 } while (n
&& ! n
->m_len
);
259 p
= mtod(n
, u_int8_t
*) + noff
;
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
;
279 * copy the output buffer into the result.
280 * need to update off and m.
282 if (off
+ 8 < m
->m_len
) {
283 bcopy(&outbuf
[0], mtod(m
, u_int8_t
*) + off
, 8);
285 } else if (off
+ 8 == m
->m_len
) {
286 bcopy(&outbuf
[0], mtod(m
, u_int8_t
*) + off
, 8);
289 } while (m
&& ! m
->m_len
);
299 p
= mtod(n
, u_int8_t
*) + noff
;
302 while (out
- &outbuf
[0] < 8) {
304 panic("mbuf chain?\n");
311 } while (n
&& ! n
->m_len
);
314 p
= mtod(n
, u_int8_t
*) + noff
;