2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3 * Authors: Doug Rabson <dfr@rabson.org>
4 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/malloc.h>
37 #include <crypto/rc4/rc4.h>
39 #include <kgssapi/gssapi.h>
40 #include <kgssapi/gssapi_impl.h>
45 arcfour_init(struct krb5_key_state *ks)
52 arcfour_destroy(struct krb5_key_state *ks)
58 arcfour_set_key(struct krb5_key_state *ks, const void *in)
60 void *kp = ks->ks_key;
67 arcfour_random_to_key(struct krb5_key_state *ks, const void *in)
70 arcfour_set_key(ks, in);
74 arcfour_hmac(uint8_t *key, uint8_t *data, size_t datalen,
81 for (i = 0; i < 16; i++)
82 buf[i] = key[i] ^ 0x36;
87 MD5Update(&md5, buf, 64);
88 MD5Update(&md5, data, datalen);
89 MD5Final(result, &md5);
91 for (i = 0; i < 16; i++)
92 buf[i] = key[i] ^ 0x5c;
97 MD5Update(&md5, buf, 64);
98 MD5Update(&md5, result, 16);
99 MD5Final(result, &md5);
103 arcfour_derive_key(const struct krb5_key_state *ks, uint32_t usage,
108 t[0] = (usage >> 24);
109 t[1] = (usage >> 16);
112 if (ks->ks_class->ec_type == ETYPE_ARCFOUR_HMAC_MD5_56) {
113 uint8_t L40[14] = "fortybits";
114 bcopy(t, L40 + 10, 4);
115 arcfour_hmac(ks->ks_key, L40, 14, newkey);
116 memset(newkey + 7, 0xab, 9);
118 arcfour_hmac(ks->ks_key, t, 4, newkey);
123 rc4_crypt_int(void *rs, void *buf, u_int len)
126 rc4_crypt(rs, buf, buf, len);
131 arcfour_encrypt(const struct krb5_key_state *ks, struct mbuf *inout,
132 size_t skip, size_t len, void *ivec, size_t ivlen)
137 arcfour_derive_key(ks, 0, newkey);
140 * If we have an IV, then generate a new key from it using HMAC.
144 arcfour_hmac(newkey, ivec, ivlen, kk);
145 rc4_init(&rs, kk, 16);
147 rc4_init(&rs, newkey, 16);
150 m_apply(inout, skip, len, rc4_crypt_int, &rs);
154 MD5Update_int(void *ctx, void *buf, u_int len)
157 MD5Update(ctx, buf, len);
162 arcfour_checksum(const struct krb5_key_state *ks, int usage,
163 struct mbuf *inout, size_t skip, size_t inlen, size_t outlen)
168 uint8_t sgn_cksum[16];
170 arcfour_hmac(ks->ks_key, "signaturekey", 13, Ksign);
178 MD5Update(&md5, t, 4);
179 m_apply(inout, skip, inlen, MD5Update_int, &md5);
180 MD5Final(sgn_cksum, &md5);
182 arcfour_hmac(Ksign, sgn_cksum, 16, sgn_cksum);
183 m_copyback(inout, skip + inlen, outlen, sgn_cksum);
186 struct krb5_encryption_class krb5_arcfour_encryption_class = {
187 "arcfour-hmac-md5", /* name */
188 ETYPE_ARCFOUR_HMAC_MD5, /* etype */
198 arcfour_random_to_key,
204 struct krb5_encryption_class krb5_arcfour_56_encryption_class = {
205 "arcfour-hmac-md5-56", /* name */
206 ETYPE_ARCFOUR_HMAC_MD5_56, /* etype */
216 arcfour_random_to_key,