]>
git.saurik.com Git - apple/xnu.git/blob - bsd/crypto/des/des_3cbc.c
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * based on sys/crypto/des/des_cbc.c, rewrote by Tomomi Suzuki
32 #include <crypto/des/des_locl.h>
35 void des_3cbc_process(m0
, skip
, length
, schedule
, ivec
, mode
)
39 des_key_schedule
*schedule
;
43 u_int8_t inbuf
[8], outbuf
[8];
47 DES_LONG tout0
, tout1
;
49 DES_LONG xor0
= 0, xor1
= 0;
54 if (m0
->m_pkthdr
.len
< skip
) {
55 printf("des_3cbc_process: mbuf length < skip\n");
58 if (m0
->m_pkthdr
.len
< length
) {
59 printf("des_3cbc_process: mbuf length < encrypt length\n");
62 if (m0
->m_pkthdr
.len
< skip
+ length
) {
63 printf("des_3cbc_process: mbuf length < "
64 "skip + encrypt length\n");
68 printf("des_3cbc_process: length(%lu) is not multiple of 8\n",
76 /* skip over the header */
79 panic("des_3cbc_process: mbuf chain?\n");
80 if (m
->m_len
<= skip
) {
91 tin0
= tin1
= tout0
= tout1
= 0;
96 iv
= (u_int8_t
*)ivec
;
102 iv
= (u_int8_t
*)ivec
;
109 * encrypt/decrypt packet
113 panic("des_3cbc_process: mbuf chain?\n");
116 * copy the source into input buffer.
117 * don't update off or m, since we need to use them
120 if (off
+ 8 <= m
->m_len
)
121 bcopy(mtod(m
, u_int8_t
*) + off
, &inbuf
[0], 8);
130 p
= mtod(n
, u_int8_t
*) + noff
;
133 while (in
- &inbuf
[0] < 8) {
135 panic("des_3cbc_process: "
144 } while (n
&& !n
->m_len
);
147 p
= mtod(n
, u_int8_t
*) + noff
;
153 /* encrypt/decrypt */
162 tin0
^= tout0
; tin
[0] = tin0
;
163 tin1
^= tout1
; tin
[1] = tin1
;
165 des_encrypt((DES_LONG
*)tin
, schedule
[0], DES_ENCRYPT
);
166 des_encrypt((DES_LONG
*)tin
, schedule
[1], DES_DECRYPT
);
167 des_encrypt((DES_LONG
*)tin
, schedule
[2], DES_ENCRYPT
);
169 tout0
= tin
[0]; l2c(tout0
, out
);
170 tout1
= tin
[1]; l2c(tout1
, out
);
175 c2l(in
, tin0
); tin
[0] = tin0
;
176 c2l(in
, tin1
); tin
[1] = tin1
;
178 des_encrypt((DES_LONG
*)tin
, schedule
[2], DES_DECRYPT
);
179 des_encrypt((DES_LONG
*)tin
, schedule
[1], DES_ENCRYPT
);
180 des_encrypt((DES_LONG
*)tin
, schedule
[0], DES_DECRYPT
);
183 tout0
= tin
[0] ^ xor0
;
184 tout1
= tin
[1] ^ xor1
;
195 * copy the output buffer int the result.
196 * need to update off and m.
198 if (off
+ 8 < m
->m_len
) {
199 bcopy(&outbuf
[0], mtod(m
, u_int8_t
*) + off
, 8);
201 } else if (off
+ 8 == m
->m_len
) {
202 bcopy(&outbuf
[0], mtod(m
, u_int8_t
*) + off
, 8);
205 } while (m
&& !m
->m_len
);
215 p
= mtod(n
, u_int8_t
*) + noff
;
218 while (out
- &outbuf
[0] < 8) {
220 panic("des_3cbc_process: "
229 } while (n
&& !n
->m_len
);
232 p
= mtod(n
, u_int8_t
*) + noff
;