2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/malloc.h>
39 #include <crypto/rc4/rc4.h>
41 #include <kgssapi/gssapi.h>
42 #include <kgssapi/gssapi_impl.h>
47 arcfour_init(struct krb5_key_state *ks)
54 arcfour_destroy(struct krb5_key_state *ks)
60 arcfour_set_key(struct krb5_key_state *ks, const void *in)
62 void *kp = ks->ks_key;
69 arcfour_random_to_key(struct krb5_key_state *ks, const void *in)
72 arcfour_set_key(ks, in);
76 arcfour_hmac(uint8_t *key, uint8_t *data, size_t datalen,
83 for (i = 0; i < 16; i++)
84 buf[i] = key[i] ^ 0x36;
89 MD5Update(&md5, buf, 64);
90 MD5Update(&md5, data, datalen);
91 MD5Final(result, &md5);
93 for (i = 0; i < 16; i++)
94 buf[i] = key[i] ^ 0x5c;
99 MD5Update(&md5, buf, 64);
100 MD5Update(&md5, result, 16);
101 MD5Final(result, &md5);
105 arcfour_derive_key(const struct krb5_key_state *ks, uint32_t usage,
110 t[0] = (usage >> 24);
111 t[1] = (usage >> 16);
114 if (ks->ks_class->ec_type == ETYPE_ARCFOUR_HMAC_MD5_56) {
115 uint8_t L40[14] = "fortybits";
116 bcopy(t, L40 + 10, 4);
117 arcfour_hmac(ks->ks_key, L40, 14, newkey);
118 memset(newkey + 7, 0xab, 9);
120 arcfour_hmac(ks->ks_key, t, 4, newkey);
125 rc4_crypt_int(void *rs, void *buf, u_int len)
128 rc4_crypt(rs, buf, buf, len);
133 arcfour_encrypt(const struct krb5_key_state *ks, struct mbuf *inout,
134 size_t skip, size_t len, void *ivec, size_t ivlen)
139 arcfour_derive_key(ks, 0, newkey);
142 * If we have an IV, then generate a new key from it using HMAC.
146 arcfour_hmac(newkey, ivec, ivlen, kk);
147 rc4_init(&rs, kk, 16);
149 rc4_init(&rs, newkey, 16);
152 m_apply(inout, skip, len, rc4_crypt_int, &rs);
156 MD5Update_int(void *ctx, void *buf, u_int len)
159 MD5Update(ctx, buf, len);
164 arcfour_checksum(const struct krb5_key_state *ks, int usage,
165 struct mbuf *inout, size_t skip, size_t inlen, size_t outlen)
170 uint8_t sgn_cksum[16];
172 arcfour_hmac(ks->ks_key, "signaturekey", 13, Ksign);
180 MD5Update(&md5, t, 4);
181 m_apply(inout, skip, inlen, MD5Update_int, &md5);
182 MD5Final(sgn_cksum, &md5);
184 arcfour_hmac(Ksign, sgn_cksum, 16, sgn_cksum);
185 m_copyback(inout, skip + inlen, outlen, sgn_cksum);
188 struct krb5_encryption_class krb5_arcfour_encryption_class = {
189 "arcfour-hmac-md5", /* name */
190 ETYPE_ARCFOUR_HMAC_MD5, /* etype */
200 arcfour_random_to_key,
206 struct krb5_encryption_class krb5_arcfour_56_encryption_class = {
207 "arcfour-hmac-md5-56", /* name */
208 ETYPE_ARCFOUR_HMAC_MD5_56, /* etype */
218 arcfour_random_to_key,