The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netncp/ncp_crypt.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* 
    2  * $FreeBSD$
    3  */
    4 #include <sys/param.h>
    5 #include <sys/systm.h>
    6 #include <netncp/ncp.h>
    7 #include <netncp/ncp_subr.h>
    8 
    9 /*
   10  *  Routines in this file based on work of Volker Lendecke
   11  */
   12 /*$*********************************************************
   13    $*
   14    $* This code has been taken from DDJ 11/93, from an 
   15    $* article by Pawel Szczerbina.
   16    $*
   17    $* Password encryption routines follow.
   18    $* Converted to C from Barry Nance's Pascal
   19    $* prog published in the March -93 issue of Byte.
   20    $*
   21    $* Adapted to be useable for ncpfs by 
   22    $* Volker Lendecke <lendecke@namu01.gwdg.de> in 
   23    $* October 1995. 
   24    $*
   25    $********************************************************* */
   26 
   27 
   28 
   29 typedef unsigned char buf32[32];
   30 
   31 static unsigned char encrypttable[256] = {
   32 0x7, 0x8, 0x0, 0x8, 0x6, 0x4, 0xE, 0x4, 0x5, 0xC, 0x1, 0x7, 0xB, 0xF, 0xA, 0x8,
   33 0x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0x2, 0x5, 0xF, 0x7, 0xA, 0xC, 0xE, 0x5, 0x9, 0x3,
   46 0xE, 0x7, 0x1, 0x2, 0xE, 0x1, 0xF, 0x4, 0xA, 0x6, 0xC, 0x6, 0xF, 0x4, 0x3, 0x0,
   47 0xC, 0x0, 0x3, 0x6, 0xF, 0x8, 0x7, 0xB, 0x2, 0xD, 0xC, 0x6, 0xA, 0xA, 0x8, 0xD
   48 };
   49 
   50 static buf32 encryptkeys = {
   51     0x48, 0x93, 0x46, 0x67, 0x98, 0x3D, 0xE6, 0x8D,
   52     0xB7, 0x10, 0x7A, 0x26, 0x5A, 0xB9, 0xB1, 0x35,
   53     0x6B, 0x0F, 0xD5, 0x70, 0xAE, 0xFB, 0xAD, 0x11,
   54     0xF4, 0x47, 0xDC, 0xA7, 0xEC, 0xCF, 0x50, 0xC0
   55 };
   56 
   57 /*
   58  * Create table-based 16-bytes hash from a 32-bytes array
   59  */
   60 static void
   61 nw_hash(buf32 temp, unsigned char *target) {
   62         short sum;
   63         unsigned char b3;
   64         int s, b2, i;
   65 
   66         sum = 0;
   67 
   68         for (b2 = 0; b2 <= 1; ++b2) {
   69                 for (s = 0; s <= 31; ++s) {
   70                         b3 = (temp[s] + sum) ^ (temp[(s + sum) & 31] - encryptkeys[s]);
   71                         sum += b3;
   72                         temp[s] = b3;
   73                 }
   74         }
   75 
   76         for (i = 0; i <= 15; ++i) {
   77                 target[i] = encrypttable[temp[2 * i]]
   78                     | (encrypttable[temp[2 * i + 1]] << 4);
   79         }
   80 }
   81 
   82 
   83 /*
   84  * Create a 16-bytes pattern from given buffer based on a four bytes key
   85  */
   86 void
   87 nw_keyhash(const u_char *key, const u_char *buf, int buflen, u_char *target) {
   88         int b2, d, s;
   89         buf32 temp;
   90 
   91         while (buflen > 0 && buf[buflen - 1] == 0)
   92                 buflen--;
   93 
   94         bzero(temp, sizeof(temp));
   95 
   96         d = 0;
   97         while (buflen >= 32) {
   98                 for (s = 0; s <= 31; ++s)
   99                         temp[s] ^= buf[d++];
  100                 buflen -= 32;
  101         }
  102         b2 = d;
  103         if (buflen > 0) {
  104                 for (s = 0; s <= 31; ++s) {
  105                         if (d + buflen == b2) {
  106                                 temp[s] ^= encryptkeys[s];
  107                                 b2 = d;
  108                         } else
  109                                 temp[s] ^= buf[b2++];
  110                 }
  111         }
  112         for (s = 0; s <= 31; ++s)
  113                 temp[s] ^= key[s & 3];
  114 
  115         nw_hash(temp, target);
  116 }
  117 
  118 /*
  119  * Create an 8-bytes pattern from an 8-bytes key and 16-bytes of data
  120  */
  121 void
  122 nw_encrypt(const u_char *fra, const u_char *buf, u_char *target) {
  123         buf32 k;
  124         int s;
  125 
  126         nw_keyhash(fra, buf, 16, k);
  127         nw_keyhash(fra + 4, buf, 16, k + 16);
  128 
  129         for (s = 0; s < 16; s++)
  130                 k[s] ^= k[31 - s];
  131 
  132         for (s = 0; s < 8; s++)
  133                 *target++ = k[s] ^ k[15 - s];
  134 }
  135 
  136 #ifdef _KERNEL
  137 /*
  138  * MD4 routine taken from libmd sources
  139  */
  140 typedef u_int32_t       UINT4;
  141 typedef unsigned char *POINTER;
  142 
  143 #define PROTO_LIST(list) list
  144 
  145 static void Decode PROTO_LIST
  146   ((UINT4 *, const unsigned char *, unsigned int));
  147 
  148 /* Constants for MD4Transform routine.
  149  */
  150 #define S11 3
  151 #define S12 7
  152 #define S13 11
  153 #define S14 19
  154 #define S21 3
  155 #define S22 5
  156 #define S23 9
  157 #define S24 13
  158 #define S31 3
  159 #define S32 9
  160 #define S33 11
  161 #define S34 15
  162 
  163 /* F, G and H are basic MD4 functions.
  164  */
  165 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  166 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  167 #define H(x, y, z) ((x) ^ (y) ^ (z))
  168 
  169 /* ROTATE_LEFT rotates x left n bits.
  170  */
  171 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  172 
  173 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
  174 /* Rotation is separate from addition to prevent recomputation */
  175 #define FF(a, b, c, d, x, s) { \
  176     (a) += F ((b), (c), (d)) + (x); \
  177     (a) = ROTATE_LEFT ((a), (s)); \
  178   }
  179 #define GG(a, b, c, d, x, s) { \
  180     (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
  181     (a) = ROTATE_LEFT ((a), (s)); \
  182   }
  183 #define HH(a, b, c, d, x, s) { \
  184     (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
  185     (a) = ROTATE_LEFT ((a), (s)); \
  186   }
  187 
  188 void
  189 ncp_sign(const u_int32_t *state, const char *block, u_int32_t *ostate) {
  190         UINT4 a, b, c, d, x[16];
  191 
  192         Decode (x, block, 64);
  193         a = state[0];
  194         b = state[1];
  195         c = state[2];
  196         d = state[3];
  197         /* Round 1 */
  198         FF (a, b, c, d, x[ 0], S11); /* 1 */
  199         FF (d, a, b, c, x[ 1], S12); /* 2 */
  200         FF (c, d, a, b, x[ 2], S13); /* 3 */
  201         FF (b, c, d, a, x[ 3], S14); /* 4 */
  202         FF (a, b, c, d, x[ 4], S11); /* 5 */
  203         FF (d, a, b, c, x[ 5], S12); /* 6 */
  204         FF (c, d, a, b, x[ 6], S13); /* 7 */
  205         FF (b, c, d, a, x[ 7], S14); /* 8 */
  206         FF (a, b, c, d, x[ 8], S11); /* 9 */
  207         FF (d, a, b, c, x[ 9], S12); /* 10 */
  208         FF (c, d, a, b, x[10], S13); /* 11 */
  209         FF (b, c, d, a, x[11], S14); /* 12 */
  210         FF (a, b, c, d, x[12], S11); /* 13 */
  211         FF (d, a, b, c, x[13], S12); /* 14 */
  212         FF (c, d, a, b, x[14], S13); /* 15 */
  213         FF (b, c, d, a, x[15], S14); /* 16 */
  214 
  215         /* Round 2 */
  216         GG (a, b, c, d, x[ 0], S21); /* 17 */
  217         GG (d, a, b, c, x[ 4], S22); /* 18 */
  218         GG (c, d, a, b, x[ 8], S23); /* 19 */
  219         GG (b, c, d, a, x[12], S24); /* 20 */
  220         GG (a, b, c, d, x[ 1], S21); /* 21 */
  221         GG (d, a, b, c, x[ 5], S22); /* 22 */
  222         GG (c, d, a, b, x[ 9], S23); /* 23 */
  223         GG (b, c, d, a, x[13], S24); /* 24 */
  224         GG (a, b, c, d, x[ 2], S21); /* 25 */
  225         GG (d, a, b, c, x[ 6], S22); /* 26 */
  226         GG (c, d, a, b, x[10], S23); /* 27 */
  227         GG (b, c, d, a, x[14], S24); /* 28 */
  228         GG (a, b, c, d, x[ 3], S21); /* 29 */
  229         GG (d, a, b, c, x[ 7], S22); /* 30 */
  230         GG (c, d, a, b, x[11], S23); /* 31 */
  231         GG (b, c, d, a, x[15], S24); /* 32 */
  232 
  233         /* Round 3 */
  234         HH (a, b, c, d, x[ 0], S31); /* 33 */
  235         HH (d, a, b, c, x[ 8], S32); /* 34 */
  236         HH (c, d, a, b, x[ 4], S33); /* 35 */
  237         HH (b, c, d, a, x[12], S34); /* 36 */
  238         HH (a, b, c, d, x[ 2], S31); /* 37 */
  239         HH (d, a, b, c, x[10], S32); /* 38 */
  240         HH (c, d, a, b, x[ 6], S33); /* 39 */
  241         HH (b, c, d, a, x[14], S34); /* 40 */
  242         HH (a, b, c, d, x[ 1], S31); /* 41 */
  243         HH (d, a, b, c, x[ 9], S32); /* 42 */
  244         HH (c, d, a, b, x[ 5], S33); /* 43 */
  245         HH (b, c, d, a, x[13], S34); /* 44 */
  246         HH (a, b, c, d, x[ 3], S31); /* 45 */
  247         HH (d, a, b, c, x[11], S32); /* 46 */
  248         HH (c, d, a, b, x[ 7], S33); /* 47 */
  249         HH (b, c, d, a, x[15], S34); /* 48 */
  250 
  251         ostate[0] = state[0] + a;
  252         ostate[1] = state[1] + b;
  253         ostate[2] = state[2] + c;
  254         ostate[3] = state[3] + d;
  255 }
  256 
  257 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
  258      a multiple of 4.
  259  */
  260 static void Decode (output, input, len)
  261 
  262 UINT4 *output;
  263 const unsigned char *input;
  264 unsigned int len;
  265 {
  266   unsigned int i, j;
  267 
  268   for (i = 0, j = 0; j < len; i++, j += 4)
  269     output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  270       (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  271 }
  272 
  273 #endif /* _KERNEL */

Cache object: c9931e8b4d73c5f6cb0d99a9bfd2a5ed


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.