]>
git.saurik.com Git - apple/xnu.git/blob - bsd/crypto/cast128/cast128_cbc.c
e2d2f6c1c6cf053bda591cd656c543d5df78f053
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
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <crypto/cast128/cast128.h>
40 cast128_cbc_process(m0
, skip
, length
, subkey
, iv
, keylen
, mode
)
50 u_int8_t inbuf
[8], outbuf
[8];
54 if (m0
->m_pkthdr
.len
< skip
) {
55 printf("cast128_cbc_process: mbuf length < skip\n");
58 if (m0
->m_pkthdr
.len
< length
) {
59 printf("cast128_cbc_process: mbuf length < encrypt length\n");
62 if (m0
->m_pkthdr
.len
< skip
+ length
) {
63 printf("cast128_cbc_process: "
64 "mbuf length < skip + encrypt length\n");
68 printf("cast128_cbc_process: length is not multiple of 8\n");
75 /* skip over the header */
78 panic("cast128_cbc_process: mbuf chain?\n");
79 if (m
->m_len
<= skip
) {
89 /* copy iv into outbuf for XOR (encrypt) */
93 * encrypt/decrypt packet
99 panic("cast128_cbc_process: mbuf chain?\n");
102 * copy the source into input buffer.
103 * don't update off or m, since we need to use them
106 if (off
+ 8 <= m
->m_len
)
107 bcopy(mtod(m
, u_int8_t
*)+off
, inbuf
, 8);
115 p
= mtod(n
, u_int8_t
*) + noff
;
118 while (in
- inbuf
< 8) {
120 panic("cast128_cbc_process: "
129 } while (n
&& !n
->m_len
);
132 p
= mtod(n
, u_int8_t
*);
138 /* encrypt/decrypt */
140 case CAST128_ENCRYPT
:
142 for (i
= 0; i
< 8; i
++)
143 inbuf
[i
] ^= outbuf
[i
];
147 cast128_encrypt_round12(outbuf
, inbuf
, subkey
);
149 cast128_encrypt_round16(outbuf
, inbuf
, subkey
);
152 case CAST128_DECRYPT
:
155 cast128_decrypt_round12(outbuf
, inbuf
, subkey
);
157 cast128_decrypt_round16(outbuf
, inbuf
, subkey
);
160 for (i
= 0; i
< 8; i
++)
163 /* copy inbuf into iv for next XOR */
169 * copy the output buffer into the result.
170 * need to update off and m.
172 if (off
+ 8 < m
->m_len
) {
173 bcopy(outbuf
, mtod(m
, u_int8_t
*) + off
, 8);
175 } else if (off
+ 8 == m
->m_len
) {
176 bcopy(outbuf
, mtod(m
, u_int8_t
*) + off
, 8);
179 } while (m
&& !m
->m_len
);
188 p
= mtod(n
, u_int8_t
*) + noff
;
191 while (out
- outbuf
< 8) {
193 panic("cast128_cbc_process: "
202 } while (n
&& !n
->m_len
);
205 p
= mtod(n
, u_int8_t
*);