HAMMER - Stabilize and refactor volume removal
[dragonfly.git] / lib / libncp / ncpl_crypt.c
blob3f4d8d5c69964e3c2c8f917282395f55af20ae17
1 /*
2 * Routines in this file based on the work of Volker Lendecke,
3 * Adapted for ncplib by Boris Popov
4 * Please note that ncpl_crypt.c file should be indentical to this one
6 * $FreeBSD: src/lib/libncp/ncpl_crypt.c,v 1.1 1999/10/12 11:56:39 bp Exp $
7 * $DragonFly: src/lib/libncp/ncpl_crypt.c,v 1.2 2003/06/17 04:26:50 dillon Exp $
8 */
9 #include <sys/param.h>
10 #include <sys/errno.h>
11 #include <sys/malloc.h>
12 #include <string.h>
14 /*$*********************************************************
16 $* This code has been taken from DDJ 11/93, from an
17 $* article by Pawel Szczerbina.
19 $* Password encryption routines follow.
20 $* Converted to C from Barry Nance's Pascal
21 $* prog published in the March -93 issue of Byte.
23 $* Adapted to be useable for ncpfs by
24 $* Volker Lendecke <lendecke@namu01.gwdg.de> in
25 $* October 1995.
27 $********************************************************* */
31 typedef unsigned char buf32[32];
33 static unsigned char encrypttable[256] = {
34 0x7, 0x8, 0x0, 0x8, 0x6, 0x4, 0xE, 0x4, 0x5, 0xC, 0x1, 0x7, 0xB, 0xF, 0xA, 0x8,
35 0xF, 0x8, 0xC, 0xC, 0x9, 0x4, 0x1, 0xE, 0x4, 0x6, 0x2, 0x4, 0x0, 0xA, 0xB, 0x9,
36 0x2, 0xF, 0xB, 0x1, 0xD, 0x2, 0x1, 0x9, 0x5, 0xE, 0x7, 0x0, 0x0, 0x2, 0x6, 0x6,
37 0x0, 0x7, 0x3, 0x8, 0x2, 0x9, 0x3, 0xF, 0x7, 0xF, 0xC, 0xF, 0x6, 0x4, 0xA, 0x0,
38 0x2, 0x3, 0xA, 0xB, 0xD, 0x8, 0x3, 0xA, 0x1, 0x7, 0xC, 0xF, 0x1, 0x8, 0x9, 0xD,
39 0x9, 0x1, 0x9, 0x4, 0xE, 0x4, 0xC, 0x5, 0x5, 0xC, 0x8, 0xB, 0x2, 0x3,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x50, 0xC0
60 * Create table-based 16-bytes hash from a 32-bytes array
62 static void
63 nw_hash(buf32 temp, unsigned char *target) {
64 short sum;
65 unsigned char b3;
66 int s, b2, i;
68 sum = 0;
70 for (b2 = 0; b2 <= 1; ++b2) {
71 for (s = 0; s <= 31; ++s) {
72 b3 = (temp[s] + sum) ^ (temp[(s + sum) & 31] - encryptkeys[s]);
73 sum += b3;
74 temp[s] = b3;
78 for (i = 0; i <= 15; ++i) {
79 target[i] = encrypttable[temp[2 * i]]
80 | (encrypttable[temp[2 * i + 1]] << 4);
86 * Create a 16-bytes pattern from given buffer based on a four bytes key
88 void
89 nw_keyhash(const u_char *key, const u_char *buf, int buflen, u_char *target) {
90 int b2, d, s;
91 buf32 temp;
93 while (buflen > 0 && buf[buflen - 1] == 0)
94 buflen--;
96 bzero(temp, sizeof(temp));
98 d = 0;
99 while (buflen >= 32) {
100 for (s = 0; s <= 31; ++s)
101 temp[s] ^= buf[d++];
102 buflen -= 32;
104 b2 = d;
105 if (buflen > 0) {
106 for (s = 0; s <= 31; ++s) {
107 if (d + buflen == b2) {
108 temp[s] ^= encryptkeys[s];
109 b2 = d;
110 } else
111 temp[s] ^= buf[b2++];
114 for (s = 0; s <= 31; ++s)
115 temp[s] ^= key[s & 3];
117 nw_hash(temp, target);
121 * Create an 8-bytes pattern from an 8-bytes key and 16-bytes of data
123 void
124 nw_encrypt(const u_char *fra, const u_char *buf, u_char *target) {
125 buf32 k;
126 int s;
128 nw_keyhash(fra, buf, 16, k);
129 nw_keyhash(fra + 4, buf, 16, k + 16);
131 for (s = 0; s < 16; s++)
132 k[s] ^= k[31 - s];
134 for (s = 0; s < 8; s++)
135 *target++ = k[s] ^ k[15 - s];