[BACK]Return to smb_crypt.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netsmb

Annotation of src/sys/netsmb/smb_crypt.c, Revision 1.8

1.8     ! christos    1: /*     $NetBSD: smb_crypt.c,v 1.6.2.1 2005/11/10 14:11:55 skrll Exp $  */
1.1       deberg      2:
                      3: /*
1.3       deberg      4:  * Copyright (c) 2000-2001, Boris Popov
1.1       deberg      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *    This product includes software developed by Boris Popov.
                     18:  * 4. Neither the name of the author nor the names of any co-contributors
                     19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
1.3       deberg     33:  *
                     34:  * FreeBSD: src/sys/netsmb/smb_crypt.c,v 1.3 2001/08/21 08:07:18 bp Exp
1.1       deberg     35:  */
1.6       jdolecek   36:
                     37: #include <sys/cdefs.h>
1.8     ! christos   38: __KERNEL_RCSID(0, "$NetBSD: smb_crypt.c,v 1.6.2.1 2005/11/10 14:11:55 skrll Exp $");
1.6       jdolecek   39:
1.1       deberg     40: #include <sys/param.h>
                     41: #include <sys/malloc.h>
                     42: #include <sys/kernel.h>
                     43: #include <sys/systm.h>
                     44: #include <sys/conf.h>
                     45: #include <sys/proc.h>
                     46: #include <sys/fcntl.h>
                     47: #include <sys/socket.h>
                     48: #include <sys/socketvar.h>
1.3       deberg     49: #include <sys/sysctl.h>
1.1       deberg     50:
1.3       deberg     51: #include <sys/md4.h>
1.1       deberg     52:
                     53: #include <netsmb/smb.h>
                     54: #include <netsmb/smb_conn.h>
                     55: #include <netsmb/smb_subr.h>
                     56: #include <netsmb/smb_dev.h>
1.3       deberg     57:
1.5       jdolecek   58: /* always enable */
                     59: #define NETSMBCRYPTO
                     60:
1.3       deberg     61: #ifdef NETSMBCRYPTO
1.1       deberg     62:
                     63: #include <crypto/des/des.h>
                     64:
1.5       jdolecek   65: static const u_char N8[] = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
1.1       deberg     66:
                     67:
                     68: static void
1.5       jdolecek   69: smb_E(const u_char *key, const u_char *data, u_char *dest)
1.1       deberg     70: {
1.3       deberg     71:        des_key_schedule *ksp;
1.1       deberg     72:        u_char kk[8];
                     73:
                     74:        kk[0] = key[0] & 0xfe;
                     75:        kk[1] = key[0] << 7 | (key[1] >> 1 & 0xfe);
                     76:        kk[2] = key[1] << 6 | (key[2] >> 2 & 0xfe);
                     77:        kk[3] = key[2] << 5 | (key[3] >> 3 & 0xfe);
                     78:        kk[4] = key[3] << 4 | (key[4] >> 4 & 0xfe);
                     79:        kk[5] = key[4] << 3 | (key[5] >> 5 & 0xfe);
                     80:        kk[6] = key[5] << 2 | (key[6] >> 6 & 0xfe);
                     81:        kk[7] = key[6] << 1;
1.3       deberg     82:        ksp = malloc(sizeof(des_key_schedule), M_SMBTEMP, M_WAITOK);
                     83:        des_set_key((des_cblock *)kk, *ksp);
1.7       christos   84:        /* XXXUNCONST */
                     85:        des_ecb_encrypt(__UNCONST(data), (des_cblock *)dest, *ksp, 1);
1.3       deberg     86:        free(ksp, M_SMBTEMP);
1.1       deberg     87: }
                     88: #endif
                     89:
                     90:
                     91: int
1.3       deberg     92: smb_encrypt(const u_char *apwd, u_char *C8, u_char *RN)
1.1       deberg     93: {
1.3       deberg     94: #ifdef NETSMBCRYPTO
                     95:        u_char *p, *P14, *S21;
1.1       deberg     96:
1.3       deberg     97:        p = malloc(14 + 21, M_SMBTEMP, M_WAITOK);
                     98:        bzero(p, 14 + 21);
                     99:        P14 = p;
                    100:        S21 = p + 14;
1.1       deberg    101:        bcopy(apwd, P14, min(14, strlen(apwd)));
                    102:        /*
                    103:         * S21 = concat(Ex(P14, N8), zeros(5));
                    104:         */
                    105:        smb_E(P14, N8, S21);
                    106:        smb_E(P14 + 7, N8, S21 + 8);
                    107:
                    108:        smb_E(S21, C8, RN);
                    109:        smb_E(S21 + 7, C8, RN + 8);
                    110:        smb_E(S21 + 14, C8, RN + 16);
1.3       deberg    111:        free(p, M_SMBTEMP);
1.1       deberg    112:        return 0;
                    113: #else
                    114:        SMBERROR("password encryption is not available\n");
                    115:        bzero(RN, 24);
                    116:        return EAUTH;
                    117: #endif
                    118: }
                    119:
                    120: int
1.3       deberg    121: smb_ntencrypt(const u_char *apwd, u_char *C8, u_char *RN)
1.1       deberg    122: {
1.3       deberg    123: #ifdef NETSMBCRYPTO
1.1       deberg    124:        u_char S21[21];
                    125:        u_int16_t *unipwd;
1.3       deberg    126:        MD4_CTX *ctxp;
1.1       deberg    127:        int len;
                    128:
                    129:        len = strlen(apwd);
1.3       deberg    130:        unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
1.1       deberg    131:        /*
                    132:         * S21 = concat(MD4(U(apwd)), zeros(5));
                    133:         */
                    134:        smb_strtouni(unipwd, apwd);
1.3       deberg    135:        ctxp = malloc(sizeof(MD4_CTX), M_SMBTEMP, M_WAITOK);
                    136:        MD4Init(ctxp);
                    137:        MD4Update(ctxp, (u_char*)unipwd, len * sizeof(u_int16_t));
1.1       deberg    138:        free(unipwd, M_SMBTEMP);
                    139:        bzero(S21, 21);
1.3       deberg    140:        MD4Final(S21, ctxp);
                    141:        free(ctxp, M_SMBTEMP);
1.1       deberg    142:
                    143:        smb_E(S21, C8, RN);
                    144:        smb_E(S21 + 7, C8, RN + 8);
                    145:        smb_E(S21 + 14, C8, RN + 16);
                    146:        return 0;
                    147: #else
                    148:        SMBERROR("password encryption is not available\n");
                    149:        bzero(RN, 24);
                    150:        return EAUTH;
                    151: #endif
                    152: }
                    153:

CVSweb <webmaster@jp.NetBSD.org>