|
| 1 | +#ifndef AUTH_DES_H |
| 2 | +#define AUTH_DES_H |
| 3 | +/* ========================================================================== ** |
| 4 | + * |
| 5 | + * DES.h |
| 6 | + * |
| 7 | + * Copyright: |
| 8 | + * Copyright (C) 2003, 2004 by Christopher R. Hertel |
| 9 | + * |
| 10 | + |
| 11 | + * |
| 12 | + * $Id: des.h,v 1.1 2006-12-19 14:21:54 wntrmute Exp $ |
| 13 | + * |
| 14 | + * -------------------------------------------------------------------------- ** |
| 15 | + * |
| 16 | + * Description: |
| 17 | + * |
| 18 | + * Implements DES encryption, but not decryption. |
| 19 | + * DES is used to create LM password hashes and both LM and NTLM Responses. |
| 20 | + * |
| 21 | + * -------------------------------------------------------------------------- ** |
| 22 | + * |
| 23 | + * License: |
| 24 | + * |
| 25 | + * This library is free software; you can redistribute it and/or |
| 26 | + * modify it under the terms of the GNU Lesser General Public |
| 27 | + * License as published by the Free Software Foundation; either |
| 28 | + * version 2.1 of the License, or (at your option) any later version. |
| 29 | + * |
| 30 | + * This library is distributed in the hope that it will be useful, |
| 31 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 32 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 33 | + * Lesser General Public License for more details. |
| 34 | + * |
| 35 | + * You should have received a copy of the GNU Lesser General Public |
| 36 | + * License along with this library; if not, write to the Free Software |
| 37 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 38 | + * |
| 39 | + * -------------------------------------------------------------------------- ** |
| 40 | + * |
| 41 | + * Notes: |
| 42 | + * |
| 43 | + * This implementation was created by studying many existing examples |
| 44 | + * found in Open Source, in the public domain, and in various documentation. |
| 45 | + * The SMB protocol makes minimal use of the DES function, so this is a |
| 46 | + * minimal implementation. That which is not required has been removed. |
| 47 | + * |
| 48 | + * The SMB protocol uses the DES algorithm as a hash function, not an |
| 49 | + * encryption function. The auth_DEShash() implemented here is a one-way |
| 50 | + * function. The reverse is not implemented in this module. Also, there |
| 51 | + * is no attempt at making this either fast or efficient. There is no |
| 52 | + * need, as the auth)DEShash() function is used for generating the LM |
| 53 | + * Response from a 7-byte key and an 8-byte challenge. It is not intended |
| 54 | + * for use in encrypting large blocks of data or data streams. |
| 55 | + * |
| 56 | + * As stated above, this implementation is based on studying existing work |
| 57 | + * in the public domain or under Open Source (specifically LGPL) license. |
| 58 | + * The code, however, is written from scratch. Obviously, I make no claim |
| 59 | + * with regard to those earlier works (except to claim that I am grateful |
| 60 | + * to the previous implementors whose work I studied). See the list of |
| 61 | + * references below for resources I used. |
| 62 | + * |
| 63 | + * References: |
| 64 | + * I read through the libmcrypt code to see how they put the pieces |
| 65 | + * together. See: http://mcrypt.hellug.gr/ |
| 66 | + * Libmcrypt is available under the terms of the LGPL. |
| 67 | + * |
| 68 | + * The libmcrypt implementation includes the following credits: |
| 69 | + * written 12 Dec 1986 by Phil Karn, KA9Q; large sections adapted |
| 70 | + * from the 1977 public-domain program by Jim Gillogly |
| 71 | + * Modified for additional speed - 6 December 1988 Phil Karn |
| 72 | + * Modified for parameterized key schedules - Jan 1991 Phil Karn |
| 73 | + * modified in order to use the libmcrypt API by Nikos Mavroyanopoulos |
| 74 | + * All modifications are placed under the license of libmcrypt. |
| 75 | + * |
| 76 | + * See also Phil Karn's privacy and security page: |
| 77 | + * http://www.ka9q.net/privacy.html |
| 78 | + * |
| 79 | + * I relied heavily upon: |
| 80 | + * Applied Cryptography, Second Edition: |
| 81 | + * Protocols, Algorithms, and Source Code in C |
| 82 | + * by Bruce Schneier. ISBN 0-471-11709-9, John Wiley & Sons, Inc., 1996 |
| 83 | + * Particularly Chapter 12. |
| 84 | + * |
| 85 | + * Here's one more DES resource, which I found quite helpful (aside from |
| 86 | + * the Clinton jokes): |
| 87 | + * http://www.aci.net/kalliste/des.htm |
| 88 | + * |
| 89 | + * Finally, the use of DES in SMB is covered in: |
| 90 | + * Implementing CIFS - the Common Internet File System |
| 91 | + * by your truly. ISBN 0-13-047116-X, Prentice Hall PTR., August 2003 |
| 92 | + * Section 15.3, in particular. |
| 93 | + * (Online at: http://ubiqx.org/cifs/SMB.html#SMB.8.3) |
| 94 | + * |
| 95 | + * ========================================================================== ** |
| 96 | + */ |
| 97 | + |
| 98 | +//#include "auth_common.h" |
| 99 | +#include <stdio.h> |
| 100 | +typedef unsigned char uchar; |
| 101 | +typedef unsigned char uint8_t; |
| 102 | + |
| 103 | +/* -------------------------------------------------------------------------- ** |
| 104 | + * Functions: |
| 105 | + */ |
| 106 | + |
| 107 | +uchar *auth_DESkey8to7( uchar *dst, const uchar *key ); |
| 108 | + /* ------------------------------------------------------------------------ ** |
| 109 | + * Compress an 8-byte DES key to its 7-byte form. |
| 110 | + * |
| 111 | + * Input: dst - Pointer to a memory location (minimum 7 bytes) to accept |
| 112 | + * the compressed key. |
| 113 | + * key - Pointer to an 8-byte DES key. See the notes below. |
| 114 | + * |
| 115 | + * Output: A pointer to the compressed key (same as <dst>) or NULL if |
| 116 | + * either <src> or <dst> were NULL. |
| 117 | + * |
| 118 | + * Notes: There are no checks done to ensure that <dst> and <key> point |
| 119 | + * to sufficient space. Please be carefull. |
| 120 | + * |
| 121 | + * The two pointers, <dst> and <key> may point to the same |
| 122 | + * memory location. Internally, a temporary buffer is used and |
| 123 | + * the results are copied back to <dst>. |
| 124 | + * |
| 125 | + * The DES algorithm uses 8 byte keys by definition. The first |
| 126 | + * step in the algorithm, however, involves removing every eigth |
| 127 | + * bit to produce a 56-bit key (seven bytes). SMB authentication |
| 128 | + * skips this step and uses 7-byte keys. The <auth_DEShash()> |
| 129 | + * algorithm in this module expects 7-byte keys. This function |
| 130 | + * is used to convert an 8-byte DES key into a 7-byte SMB DES key. |
| 131 | + * |
| 132 | + * ------------------------------------------------------------------------ ** |
| 133 | + */ |
| 134 | + |
| 135 | + |
| 136 | +uchar *auth_DEShash( uchar *dst, const uchar *key, const uchar *src ); |
| 137 | + /* ------------------------------------------------------------------------ ** |
| 138 | + * DES encryption of the input data using the input key. |
| 139 | + * |
| 140 | + * Input: dst - Destination buffer. It *must* be at least eight bytes |
| 141 | + * in length, to receive the encrypted result. |
| 142 | + * key - Encryption key. Exactly seven bytes will be used. |
| 143 | + * If your key is shorter, ensure that you pad it to seven |
| 144 | + * bytes. |
| 145 | + * src - Source data to be encrypted. Exactly eight bytes will |
| 146 | + * be used. If your source data is shorter, ensure that |
| 147 | + * you pad it to eight bytes. |
| 148 | + * |
| 149 | + * Output: A pointer to the encrpyted data (same as <dst>). |
| 150 | + * |
| 151 | + * Notes: In SMB, the DES function is used as a hashing function rather |
| 152 | + * than an encryption/decryption tool. When used for generating |
| 153 | + * the LM hash the <src> input is the known value "KGS!@#$%" and |
| 154 | + * the key is derived from the password entered by the user. |
| 155 | + * When used to generate the LM or NTLM response, the <key> is |
| 156 | + * derived from the LM or NTLM hash, and the challenge is used |
| 157 | + * as the <src> input. |
| 158 | + * See: http://ubiqx.org/cifs/SMB.html#SMB.8.3 |
| 159 | + * |
| 160 | + * - This function is called "DEShash" rather than just "DES" |
| 161 | + * because it is only used for creating LM hashes and the |
| 162 | + * LM/NTLM responses. For all practical purposes, however, it |
| 163 | + * is a full DES encryption implementation. |
| 164 | + * |
| 165 | + * - This DES implementation does not need to be fast, nor is a |
| 166 | + * DES decryption function needed. The goal is to keep the |
| 167 | + * code small, simple, and well documented. |
| 168 | + * |
| 169 | + * - The input values are copied and refiddled within the module |
| 170 | + * and the result is not written to <dst> until the very last |
| 171 | + * step, so it's okay if <dst> points to the same memory as |
| 172 | + * <key> or <src>. |
| 173 | + * |
| 174 | + * ------------------------------------------------------------------------ ** |
| 175 | + */ |
| 176 | + |
| 177 | + |
| 178 | +/* ========================================================================== */ |
| 179 | +#endif /* AUTH_DES_H */ |
0 commit comments