]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kgssapi/krb5/kcrypto_des.c
OpenCrypto: Convert sessions to opaque handles instead of integers
[FreeBSD/FreeBSD.git] / sys / kgssapi / krb5 / kcrypto_des.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/kobj.h>
36 #include <sys/malloc.h>
37 #include <sys/md5.h>
38 #include <sys/mutex.h>
39 #include <sys/mbuf.h>
40 #include <crypto/des/des.h>
41 #include <opencrypto/cryptodev.h>
42
43 #include <kgssapi/gssapi.h>
44 #include <kgssapi/gssapi_impl.h>
45
46 #include "kcrypto.h"
47
48 struct des1_state {
49         struct mtx      ds_lock;
50         crypto_session_t ds_session;
51 };
52
53 static void
54 des1_init(struct krb5_key_state *ks)
55 {
56         struct des1_state *ds;
57
58         ds = malloc(sizeof(struct des1_state), M_GSSAPI, M_WAITOK|M_ZERO);
59         mtx_init(&ds->ds_lock, "gss des lock", NULL, MTX_DEF);
60         ks->ks_priv = ds;
61 }
62
63 static void
64 des1_destroy(struct krb5_key_state *ks)
65 {
66         struct des1_state *ds = ks->ks_priv;
67
68         if (ds->ds_session)
69                 crypto_freesession(ds->ds_session);
70         mtx_destroy(&ds->ds_lock);
71         free(ks->ks_priv, M_GSSAPI);
72
73 }
74
75 static void
76 des1_set_key(struct krb5_key_state *ks, const void *in)
77 {
78         void *kp = ks->ks_key;
79         struct des1_state *ds = ks->ks_priv;
80         struct cryptoini cri[1];
81
82         if (kp != in)
83                 bcopy(in, kp, ks->ks_class->ec_keylen);
84
85         if (ds->ds_session)
86                 crypto_freesession(ds->ds_session);
87
88         bzero(cri, sizeof(cri));
89
90         cri[0].cri_alg = CRYPTO_DES_CBC;
91         cri[0].cri_klen = 64;
92         cri[0].cri_mlen = 0;
93         cri[0].cri_key = ks->ks_key;
94         cri[0].cri_next = NULL;
95
96         crypto_newsession(&ds->ds_session, cri,
97             CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
98 }
99
100 static void
101 des1_random_to_key(struct krb5_key_state *ks, const void *in)
102 {
103         uint8_t *outkey = ks->ks_key;
104         const uint8_t *inkey = in;
105
106         /*
107          * Expand 56 bits of random data to 64 bits as follows
108          * (in the example, bit number 1 is the MSB of the 56
109          * bits of random data):
110          *
111          * expanded = 
112          *       1  2  3  4  5  6  7  p
113          *       9 10 11 12 13 14 15  p
114          *      17 18 19 20 21 22 23  p
115          *      25 26 27 28 29 30 31  p
116          *      33 34 35 36 37 38 39  p
117          *      41 42 43 44 45 46 47  p
118          *      49 50 51 52 53 54 55  p
119          *      56 48 40 32 24 16  8  p
120          */
121         outkey[0] = inkey[0];
122         outkey[1] = inkey[1];
123         outkey[2] = inkey[2];
124         outkey[3] = inkey[3];
125         outkey[4] = inkey[4];
126         outkey[5] = inkey[5];
127         outkey[6] = inkey[6];
128         outkey[7] = (((inkey[0] & 1) << 1)
129             | ((inkey[1] & 1) << 2)
130             | ((inkey[2] & 1) << 3)
131             | ((inkey[3] & 1) << 4)
132             | ((inkey[4] & 1) << 5)
133             | ((inkey[5] & 1) << 6)
134             | ((inkey[6] & 1) << 7));
135         des_set_odd_parity((des_cblock *) outkey);
136         if (des_is_weak_key((des_cblock *) outkey))
137                 outkey[7] ^= 0xf0;
138
139         des1_set_key(ks, ks->ks_key);
140 }
141
142 static int
143 des1_crypto_cb(struct cryptop *crp)
144 {
145         int error;
146         struct des1_state *ds = (struct des1_state *) crp->crp_opaque;
147         
148         if (crypto_ses2caps(ds->ds_session) & CRYPTOCAP_F_SYNC)
149                 return (0);
150
151         error = crp->crp_etype;
152         if (error == EAGAIN)
153                 error = crypto_dispatch(crp);
154         mtx_lock(&ds->ds_lock);
155         if (error || (crp->crp_flags & CRYPTO_F_DONE))
156                 wakeup(crp);
157         mtx_unlock(&ds->ds_lock);
158
159         return (0);
160 }
161
162 static void
163 des1_encrypt_1(const struct krb5_key_state *ks, int buftype, void *buf,
164     size_t skip, size_t len, void *ivec, int encdec)
165 {
166         struct des1_state *ds = ks->ks_priv;
167         struct cryptop *crp;
168         struct cryptodesc *crd;
169         int error;
170
171         crp = crypto_getreq(1);
172         crd = crp->crp_desc;
173
174         crd->crd_skip = skip;
175         crd->crd_len = len;
176         crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT | encdec;
177         if (ivec) {
178                 bcopy(ivec, crd->crd_iv, 8);
179         } else {
180                 bzero(crd->crd_iv, 8);
181         }
182         crd->crd_next = NULL;
183         crd->crd_alg = CRYPTO_DES_CBC;
184
185         crp->crp_session = ds->ds_session;
186         crp->crp_flags = buftype | CRYPTO_F_CBIFSYNC;
187         crp->crp_buf = buf;
188         crp->crp_opaque = (void *) ds;
189         crp->crp_callback = des1_crypto_cb;
190
191         error = crypto_dispatch(crp);
192
193         if ((crypto_ses2caps(ds->ds_session) & CRYPTOCAP_F_SYNC) == 0) {
194                 mtx_lock(&ds->ds_lock);
195                 if (!error && !(crp->crp_flags & CRYPTO_F_DONE))
196                         error = msleep(crp, &ds->ds_lock, 0, "gssdes", 0);
197                 mtx_unlock(&ds->ds_lock);
198         }
199
200         crypto_freereq(crp);
201 }
202
203 static void
204 des1_encrypt(const struct krb5_key_state *ks, struct mbuf *inout,
205     size_t skip, size_t len, void *ivec, size_t ivlen)
206 {
207
208         des1_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len, ivec,
209             CRD_F_ENCRYPT);
210 }
211
212 static void
213 des1_decrypt(const struct krb5_key_state *ks, struct mbuf *inout,
214     size_t skip, size_t len, void *ivec, size_t ivlen)
215 {
216
217         des1_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len, ivec, 0);
218 }
219
220 static int
221 MD5Update_int(void *ctx, void *buf, u_int len)
222 {
223
224         MD5Update(ctx, buf, len);
225         return (0);
226 }
227
228 static void
229 des1_checksum(const struct krb5_key_state *ks, int usage,
230     struct mbuf *inout, size_t skip, size_t inlen, size_t outlen)
231 {
232         char hash[16];
233         MD5_CTX md5;
234
235         /*
236          * This checksum is specifically for GSS-API. First take the
237          * MD5 checksum of the message, then calculate the CBC mode
238          * checksum of that MD5 checksum using a zero IV.
239          */
240         MD5Init(&md5);
241         m_apply(inout, skip, inlen, MD5Update_int, &md5);
242         MD5Final(hash, &md5);
243
244         des1_encrypt_1(ks, 0, hash, 0, 16, NULL, CRD_F_ENCRYPT);
245         m_copyback(inout, skip + inlen, outlen, hash + 8);
246 }
247
248 struct krb5_encryption_class krb5_des_encryption_class = {
249         "des-cbc-md5",          /* name */
250         ETYPE_DES_CBC_CRC,      /* etype */
251         0,                      /* flags */
252         8,                      /* blocklen */
253         8,                      /* msgblocklen */
254         8,                      /* checksumlen */
255         56,                     /* keybits */
256         8,                      /* keylen */
257         des1_init,
258         des1_destroy,
259         des1_set_key,
260         des1_random_to_key,
261         des1_encrypt,
262         des1_decrypt,
263         des1_checksum
264 };