|
| 1 | +package ncp |
| 2 | + |
| 3 | +// nwcrypt.go is the NetWare bindery password-encryption used by the encrypted login |
| 4 | +// handshake a NetWare 3.x/4.x server requires (a cleartext Login To File Server is |
| 5 | +// refused by a default-configured real server). The flow is: Get Login Key draws an |
| 6 | +// 8-byte challenge from the server; Get Bindery Object ID resolves the user name to its |
| 7 | +// 4-byte object ID; then the password is shuffle()d with the object ID into a 16-byte |
| 8 | +// digest, and nw_encrypt() folds the challenge key into it to produce the 8-byte response |
| 9 | +// carried by Login Object (Encrypted). |
| 10 | +// |
| 11 | +// ATTRIBUTION (CLAUDE.md #7): the shuffle / nw_encrypt algorithm is the one published in |
| 12 | +// Dr. Dobb's Journal 11/93 "Undocumented Corner" by Pawel Szczerbina (itself converted |
| 13 | +// from Barry Nance's Pascal in Byte 3/93), and adapted for the free NCP filesystem by |
| 14 | +// Volker Lendecke in ncpfs (lib/nwcrypt.c, GPL). This is a faithful Go port of that |
| 15 | +// code — the tables and step structure are preserved exactly so it interoperates with a |
| 16 | +// real NetWare server and with mars_nwe. Only the surface (Go slices, names) differs. |
| 17 | + |
| 18 | +// encryptTable is the 256-entry nibble substitution table (ncpfs encrypttable). |
| 19 | +var encryptTable = [256]byte{ |
| 20 | + 0x7, 0x8, 0x0, 0x8, 0x6, 0x4, 0xE, 0x4, 0x5, 0xC, 0x1, 0x7, 0xB, 0xF, 0xA, 0x8, |
| 21 | + 0xF, 0x8, 0xC, 0xC, 0x9, 0x4, 0x1, 0xE, 0x4, 0x6, 0x2, 0x4, 0x0, 0xA, 0xB, 0x9, |
| 22 | + 0x2, 0xF, 0xB, 0x1, 0xD, 0x2, 0x1, 0x9, 0x5, 0xE, 0x7, 0x0, 0x0, 0x2, 0x6, 0x6, |
| 23 | + 0x0, 0x7, 0x3, 0x8, 0x2, 0x9, 0x3, 0xF, 0x7, 0xF, 0xC, 0xF, 0x6, 0x4, 0xA, 0x0, |
| 24 | + 0x2, 0x3, 0xA, 0xB, 0xD, 0x8, 0x3, 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 37 | + |
| 38 | +// encryptKeys is the 32-byte key vector mixed into the shuffle (ncpfs encryptkeys). |
| 39 | +var encryptKeys = [32]byte{ |
| 40 | + 0x48, 0x93, 0x46, 0x67, 0x98, 0x3D, 0xE6, 0x8D, |
| 41 | + 0xB7, 0x10, 0x7A, 0x26, 0x5A, 0xB9, 0xB1, 0x35, |
| 42 | + 0x6B, 0x0F, 0xD5, 0x70, 0xAE, 0xFB, 0xAD, 0x11, |
| 43 | + 0xF4, 0x47, 0xDC, 0xA7, 0xEC, 0xCF, 0x50, 0xC0, |
| 44 | +} |
| 45 | + |
| 46 | +// shuffle1 mixes the 32-byte temp buffer and folds it to a 16-byte target (ncpfs |
| 47 | +// shuffle1): two mixing passes over temp, then a nibble-substitution to target. |
| 48 | +func shuffle1(temp *[32]byte, target *[16]byte) { |
| 49 | + var b4 int16 |
| 50 | + for b2 := 0; b2 <= 1; b2++ { |
| 51 | + for s := 0; s <= 31; s++ { |
| 52 | + b3 := byte((int(temp[s]) + int(b4)) ^ (int(temp[(s+int(b4))&31]) - int(encryptKeys[s]))) |
| 53 | + b4 += int16(b3) |
| 54 | + temp[s] = b3 |
| 55 | + } |
| 56 | + } |
| 57 | + for i := 0; i <= 15; i++ { |
| 58 | + target[i] = encryptTable[temp[2*i]] | (encryptTable[temp[2*i+1]] << 4) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// shuffle hashes password bytes buf keyed by the 4-byte lon (the login object ID in |
| 63 | +// network byte order) into a 16-byte target (ncpfs shuffle). Trailing NUL bytes of buf |
| 64 | +// are dropped first, matching ncpfs. |
| 65 | +func shuffle(lon [4]byte, buf []byte, target *[16]byte) { |
| 66 | + buflen := len(buf) |
| 67 | + for buflen > 0 && buf[buflen-1] == 0 { |
| 68 | + buflen-- |
| 69 | + } |
| 70 | + |
| 71 | + var temp [32]byte |
| 72 | + d := 0 |
| 73 | + for buflen >= 32 { |
| 74 | + for s := 0; s <= 31; s++ { |
| 75 | + temp[s] ^= buf[d] |
| 76 | + d++ |
| 77 | + } |
| 78 | + buflen -= 32 |
| 79 | + } |
| 80 | + b2 := d |
| 81 | + if buflen > 0 { |
| 82 | + for s := 0; s <= 31; s++ { |
| 83 | + if d+buflen == b2 { |
| 84 | + b2 = d |
| 85 | + temp[s] ^= encryptKeys[s] |
| 86 | + } else { |
| 87 | + temp[s] ^= buf[b2] |
| 88 | + b2++ |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + for s := 0; s <= 31; s++ { |
| 93 | + temp[s] ^= lon[s&3] |
| 94 | + } |
| 95 | + shuffle1(&temp, target) |
| 96 | +} |
| 97 | + |
| 98 | +// nwEncrypt folds the 8-byte server login key fra into the 16-byte shuffled password buf |
| 99 | +// to produce the 8-byte response til carried by the encrypted login (ncpfs nw_encrypt). |
| 100 | +// The two 4-byte halves of the login key each shuffle buf into one 16-byte half of k, |
| 101 | +// which is then folded twice down to the 8-byte result. |
| 102 | +func nwEncrypt(fra [8]byte, buf [16]byte, til *[8]byte) { |
| 103 | + var a, b [16]byte |
| 104 | + var fra0, fra4 [4]byte |
| 105 | + copy(fra0[:], fra[0:4]) |
| 106 | + copy(fra4[:], fra[4:8]) |
| 107 | + shuffle(fra0, buf[:], &a) |
| 108 | + shuffle(fra4, buf[:], &b) |
| 109 | + |
| 110 | + var k [32]byte |
| 111 | + copy(k[0:16], a[:]) |
| 112 | + copy(k[16:32], b[:]) |
| 113 | + for s := 0; s <= 15; s++ { |
| 114 | + k[s] ^= k[31-s] |
| 115 | + } |
| 116 | + for s := 0; s <= 7; s++ { |
| 117 | + til[s] = k[s] ^ k[15-s] |
| 118 | + } |
| 119 | +} |
0 commit comments