]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kgssapi/krb5/kcrypto_arcfour.c
Merge clang trunk r338150, and resolve conflicts.
[FreeBSD/FreeBSD.git] / sys / kgssapi / krb5 / kcrypto_arcfour.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
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>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/md5.h>
37 #include <sys/kobj.h>
38 #include <sys/mbuf.h>
39 #include <crypto/rc4/rc4.h>
40
41 #include <kgssapi/gssapi.h>
42 #include <kgssapi/gssapi_impl.h>
43
44 #include "kcrypto.h"
45
46 static void
47 arcfour_init(struct krb5_key_state *ks)
48 {
49
50         ks->ks_priv = NULL;
51 }
52
53 static void
54 arcfour_destroy(struct krb5_key_state *ks)
55 {
56
57 }
58
59 static void
60 arcfour_set_key(struct krb5_key_state *ks, const void *in)
61 {
62         void *kp = ks->ks_key;
63
64         if (kp != in)
65                 bcopy(in, kp, 16);
66 }
67
68 static void
69 arcfour_random_to_key(struct krb5_key_state *ks, const void *in)
70 {
71
72         arcfour_set_key(ks, in);
73 }
74
75 static void
76 arcfour_hmac(uint8_t *key, uint8_t *data, size_t datalen,
77         uint8_t *result)
78 {
79         uint8_t buf[64];
80         MD5_CTX md5;
81         int i;
82
83         for (i = 0; i < 16; i++)
84                 buf[i] = key[i] ^ 0x36;
85         for (; i < 64; i++)
86                 buf[i] = 0x36;
87
88         MD5Init(&md5);
89         MD5Update(&md5, buf, 64);
90         MD5Update(&md5, data, datalen);
91         MD5Final(result, &md5);
92
93         for (i = 0; i < 16; i++)
94                 buf[i] = key[i] ^ 0x5c;
95         for (; i < 64; i++)
96                 buf[i] = 0x5c;
97         
98         MD5Init(&md5);
99         MD5Update(&md5, buf, 64);
100         MD5Update(&md5, result, 16);
101         MD5Final(result, &md5);
102 }
103
104 static void
105 arcfour_derive_key(const struct krb5_key_state *ks, uint32_t usage,
106     uint8_t *newkey)
107 {
108         uint8_t t[4];
109
110         t[0] = (usage >> 24);
111         t[1] = (usage >> 16);
112         t[2] = (usage >> 8);
113         t[3] = (usage >> 0);
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);
119         } else {
120                 arcfour_hmac(ks->ks_key, t, 4, newkey);
121         }
122 }
123
124 static int
125 rc4_crypt_int(void *rs, void *buf, u_int len)
126 {
127
128         rc4_crypt(rs, buf, buf, len);
129         return (0);
130 }
131
132 static void
133 arcfour_encrypt(const struct krb5_key_state *ks, struct mbuf *inout,
134     size_t skip, size_t len, void *ivec, size_t ivlen)
135 {
136         struct rc4_state rs;
137         uint8_t newkey[16];
138
139         arcfour_derive_key(ks, 0, newkey);
140
141         /*
142          * If we have an IV, then generate a new key from it using HMAC.
143          */
144         if (ivec) {
145                 uint8_t kk[16];
146                 arcfour_hmac(newkey, ivec, ivlen, kk);
147                 rc4_init(&rs, kk, 16);
148         } else {
149                 rc4_init(&rs, newkey, 16);
150         }
151
152         m_apply(inout, skip, len, rc4_crypt_int, &rs);
153 }
154
155 static int
156 MD5Update_int(void *ctx, void *buf, u_int len)
157 {
158
159         MD5Update(ctx, buf, len);
160         return (0);
161 }
162
163 static void
164 arcfour_checksum(const struct krb5_key_state *ks, int usage,
165     struct mbuf *inout, size_t skip, size_t inlen, size_t outlen)
166 {
167         MD5_CTX md5;
168         uint8_t Ksign[16];
169         uint8_t t[4];
170         uint8_t sgn_cksum[16];
171
172         arcfour_hmac(ks->ks_key, "signaturekey", 13, Ksign);
173
174         t[0] = usage >> 0;
175         t[1] = usage >> 8;
176         t[2] = usage >> 16;
177         t[3] = usage >> 24;
178
179         MD5Init(&md5);
180         MD5Update(&md5, t, 4);
181         m_apply(inout, skip, inlen, MD5Update_int, &md5);
182         MD5Final(sgn_cksum, &md5);
183         
184         arcfour_hmac(Ksign, sgn_cksum, 16, sgn_cksum);
185         m_copyback(inout, skip + inlen, outlen, sgn_cksum);
186 }
187
188 struct krb5_encryption_class krb5_arcfour_encryption_class = {
189         "arcfour-hmac-md5",     /* name */
190         ETYPE_ARCFOUR_HMAC_MD5, /* etype */
191         0,                      /* flags */
192         1,                      /* blocklen */
193         1,                      /* msgblocklen */
194         8,                      /* checksumlen */
195         128,                    /* keybits */
196         16,                     /* keylen */
197         arcfour_init,
198         arcfour_destroy,
199         arcfour_set_key,
200         arcfour_random_to_key,
201         arcfour_encrypt,
202         arcfour_encrypt,
203         arcfour_checksum
204 };
205
206 struct krb5_encryption_class krb5_arcfour_56_encryption_class = {
207         "arcfour-hmac-md5-56",     /* name */
208         ETYPE_ARCFOUR_HMAC_MD5_56, /* etype */
209         0,                         /* flags */
210         1,                      /* blocklen */
211         1,                      /* msgblocklen */
212         8,                      /* checksumlen */
213         128,                    /* keybits */
214         16,                     /* keylen */
215         arcfour_init,
216         arcfour_destroy,
217         arcfour_set_key,
218         arcfour_random_to_key,
219         arcfour_encrypt,
220         arcfour_encrypt,
221         arcfour_checksum
222 };