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