]>
git.saurik.com Git - apple/xnu.git/blob - bsd/crypto/rc5/rc5_cbc.c
c3d094d2499bc8a233040814fc60186882349727
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/rc5/rc5.h>
36 rc5_cbc_process(m0
, skip
, length
, e_key
, iv
, mode
)
44 u_int8_t inbuf
[8], outbuf
[8];
49 if (m0
->m_pkthdr
.len
< skip
) {
50 printf("rc5_cbc_process: mbuf length < skip\n");
53 if (m0
->m_pkthdr
.len
< length
) {
54 printf("rc5_cbc_process: mbuf length < encrypt length\n");
57 if (m0
->m_pkthdr
.len
< skip
+ length
) {
58 printf("rc5_cbc_process: mbuf length < "
59 "skip + encrypt length\n");
63 printf("rc5_cbc_process: length(%lu)is not multipleof 8\n",
71 /* skip over the header */
74 panic("rc5_cbc_process: mbuf chain?\n");
75 if (m
->m_len
<= skip
) {
85 /* copy iv into outbuf for XOR (encrypt) */
89 * encrypt/decrypt packet
95 panic("rc5_cbc_process: mbuf chain?\n");
98 * copy the source into input buffer.
99 * don't update off or m, since we need to use them
102 if (off
+ 8 <= m
->m_len
)
103 bcopy(mtod(m
, u_int8_t
*) + off
, &inbuf
[0], 8);
112 p
= mtod(n
, u_int8_t
*) + noff
;
115 while (in
- &inbuf
[0] < 8) {
117 panic("rc5_cbc_process: "
126 } while (n
&& !n
->m_len
);
129 p
= mtod(n
, u_int8_t
*) + noff
;
135 /* encrypt/decrypt */
139 for (i
= 0; i
< 8; i
++)
140 inbuf
[i
] ^= outbuf
[i
];
143 rc5_encrypt_round16(outbuf
, inbuf
, e_key
);
148 rc5_decrypt_round16(outbuf
, inbuf
, e_key
);
151 for (i
= 0; i
< 8; i
++)
154 /* copy inbuf into iv for next XOR */
160 * copy the output buffer into the result.
161 * need to update off and m.
163 if (off
+ 8 < m
->m_len
) {
164 bcopy(&outbuf
[0], mtod(m
, u_int8_t
*) + off
, 8);
166 } else if (off
+ 8 == m
->m_len
) {
167 bcopy(&outbuf
[0], mtod(m
, u_int8_t
*) + off
, 8);
170 } while (m
&& !m
->m_len
);
180 p
= mtod(n
, u_int8_t
*) + noff
;
183 while (out
- &outbuf
[0] < 8) {
185 panic("rc5_cbc_process: "
194 } while (n
&& !n
->m_len
);
197 p
= mtod(n
, u_int8_t
*) + noff
;