Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,6 @@ doc/api/

.idea/
*.iml
.DS_Store
.DS_Store

personalized_test.dart
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add the dependency to your `pubspec.yaml` file:

```yaml
dependencies:
ntlm: ^2.0.1
ntlm: ^2.0.2
```

## Example
Expand Down
6 changes: 1 addition & 5 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
include: package:pedantic/analysis_options.yaml

linter:
rules:
- directives_ordering
include: package:flutter_lints/flutter.yaml
2 changes: 1 addition & 1 deletion lib/ntlm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
/// ```
library ntlm;

export 'package:ntlm/src/http.dart';
export 'package:ntlm/src/hash.dart';
export 'package:ntlm/src/http.dart';
export 'package:ntlm/src/messages/messages.dart';
63 changes: 29 additions & 34 deletions lib/src/des/des.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import 'dart:typed_data';

import 'package:fixnum/fixnum.dart';
import 'package:pointycastle/api.dart';
import 'package:pointycastle/src/impl/base_block_cipher.dart';
import 'package:pointycastle/src/ufixnum.dart';
import 'package:pointycastle/src/registry/registry.dart';
import 'package:fixnum/fixnum.dart';
import 'package:pointycastle/src/ufixnum.dart';

import 'package:ntlm/src/des/des_constants.dart';

class DESEngine extends BaseBlockCipher {
static final FactoryConfig FACTORY_CONFIG =
StaticFactoryConfig(BlockCipher, 'DES', () => DESEngine());
static final FactoryConfig factoryConfig = StaticFactoryConfig(BlockCipher, 'DES', () => DESEngine());

static const _BLOCK_SIZE = 8;
static const _blockSize = 8;

List<Int32>? _workingKey;

@override
String get algorithmName => 'DES';

@override
int get blockSize => _BLOCK_SIZE;
int get blockSize => _blockSize;

@override
void reset() {}
Expand All @@ -38,17 +39,17 @@ class DESEngine extends BaseBlockCipher {
throw StateError('DES engine not initialised');
}

if ((inpOff + _BLOCK_SIZE) > inp.length) {
if ((inpOff + _blockSize) > inp.length) {
throw ArgumentError('input buffer too short');
}

if ((outOff + _BLOCK_SIZE) > out.length) {
if ((outOff + _blockSize) > out.length) {
throw ArgumentError('output buffer too short');
}

_desFunc(_workingKey!, inp, inpOff, out, outOff);

return _BLOCK_SIZE;
return _blockSize;
}

List<Int32> _generateWorkingKey(bool encrypting, Uint8List key) {
Expand All @@ -59,9 +60,7 @@ class DESEngine extends BaseBlockCipher {
for (var j = 0; j < 56; j++) {
var l = pc1[j];

pc1m[j] =
((key[l.shiftRightUnsigned(3).toInt()] & bytebit[(l & 7).toInt()]) !=
0);
pc1m[j] = ((key[l.shiftRightUnsigned(3).toInt()] & bytebit[(l & 7).toInt()]) != 0);
}

for (IntX i = Int32.ZERO; i < 16; i++) {
Expand Down Expand Up @@ -120,17 +119,13 @@ class DESEngine extends BaseBlockCipher {
((i2 & 0x00fc0000).shiftRightUnsigned(10)) |
((i2 & 0x00000fc0).shiftRightUnsigned(6));

newKey[i + 1] = ((i1 & 0x0003f000) << 12) |
((i1 & 0x0000003f) << 16) |
((i2 & 0x0003f000).shiftRightUnsigned(4)) |
(i2 & 0x0000003f);
newKey[i + 1] = ((i1 & 0x0003f000) << 12) | ((i1 & 0x0000003f) << 16) | ((i2 & 0x0003f000).shiftRightUnsigned(4)) | (i2 & 0x0000003f);
}

return newKey;
}

void _desFunc(
List<Int32> wKey, Uint8List inp, int inOff, Uint8List out, int outOff) {
void _desFunc(List<Int32> wKey, Uint8List inp, int inOff, Uint8List out, int outOff) {
Int32 work, right, left;

left = Int32(unpack32(inp, inOff, Endian.big));
Expand Down Expand Up @@ -160,27 +155,27 @@ class DESEngine extends BaseBlockCipher {
work = (right << 28) | (right.shiftRightUnsigned(4));
work ^= wKey[round * 4 + 0];

fval = SP7[(work & 0x3f).toInt()];
fval |= SP5[((work.shiftRightUnsigned(8)) & 0x3f).toInt()];
fval |= SP3[((work.shiftRightUnsigned(16)) & 0x3f).toInt()];
fval |= SP1[((work.shiftRightUnsigned(24)) & 0x3f).toInt()];
fval = sp7[(work & 0x3f).toInt()];
fval |= sp5[((work.shiftRightUnsigned(8)) & 0x3f).toInt()];
fval |= sp3[((work.shiftRightUnsigned(16)) & 0x3f).toInt()];
fval |= sp1[((work.shiftRightUnsigned(24)) & 0x3f).toInt()];
work = right ^ wKey[(round * 4 + 1)];
fval |= SP8[(work & 0x3f).toInt()];
fval |= SP6[((work.shiftRightUnsigned(8)) & 0x3f).toInt()];
fval |= SP4[((work.shiftRightUnsigned(16)) & 0x3f).toInt()];
fval |= SP2[((work.shiftRightUnsigned(24)) & 0x3f).toInt()];
fval |= sp8[(work & 0x3f).toInt()];
fval |= sp6[((work.shiftRightUnsigned(8)) & 0x3f).toInt()];
fval |= sp4[((work.shiftRightUnsigned(16)) & 0x3f).toInt()];
fval |= sp2[((work.shiftRightUnsigned(24)) & 0x3f).toInt()];
left ^= fval;
work = (left << 28) | (left.shiftRightUnsigned(4));
work ^= wKey[(round * 4 + 2)];
fval = SP7[(work & 0x3f).toInt()];
fval |= SP5[((work.shiftRightUnsigned(8)) & 0x3f).toInt()];
fval |= SP3[((work.shiftRightUnsigned(16)) & 0x3f).toInt()];
fval |= SP1[((work.shiftRightUnsigned(24)) & 0x3f).toInt()];
fval = sp7[(work & 0x3f).toInt()];
fval |= sp5[((work.shiftRightUnsigned(8)) & 0x3f).toInt()];
fval |= sp3[((work.shiftRightUnsigned(16)) & 0x3f).toInt()];
fval |= sp1[((work.shiftRightUnsigned(24)) & 0x3f).toInt()];
work = left ^ wKey[(round * 4 + 3)];
fval |= SP8[(work & 0x3f).toInt()];
fval |= SP6[((work.shiftRightUnsigned(8)) & 0x3f).toInt()];
fval |= SP4[((work.shiftRightUnsigned(16)) & 0x3f).toInt()];
fval |= SP2[((work.shiftRightUnsigned(24)) & 0x3f).toInt()];
fval |= sp8[(work & 0x3f).toInt()];
fval |= sp6[((work.shiftRightUnsigned(8)) & 0x3f).toInt()];
fval |= sp4[((work.shiftRightUnsigned(16)) & 0x3f).toInt()];
fval |= sp2[((work.shiftRightUnsigned(24)) & 0x3f).toInt()];
right ^= fval;
}

Expand Down
20 changes: 9 additions & 11 deletions lib/src/des/des_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ final pc1 = [
3
].map((i) => Int32(i)).toList();

final totrot = [1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28]
.map((i) => Int32(i))
.toList();
final totrot = [1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28].map((i) => Int32(i)).toList();

final pc2 = [
13,
Expand Down Expand Up @@ -143,7 +141,7 @@ final pc2 = [
31
].map((i) => Int32(i)).toList();

final SP1 = [
final sp1 = [
0x01010400,
0x00000000,
0x00010000,
Expand Down Expand Up @@ -210,7 +208,7 @@ final SP1 = [
0x01010004
].map((i) => Int32(i)).toList();

final SP2 = [
final sp2 = [
0x80108020,
0x80008000,
0x00008000,
Expand Down Expand Up @@ -277,7 +275,7 @@ final SP2 = [
0x00108000
].map((i) => Int32(i)).toList();

final SP3 = [
final sp3 = [
0x00000208,
0x08020200,
0x00000000,
Expand Down Expand Up @@ -344,7 +342,7 @@ final SP3 = [
0x00020200
].map((i) => Int32(i)).toList();

final SP4 = [
final sp4 = [
0x00802001,
0x00002081,
0x00002081,
Expand Down Expand Up @@ -411,7 +409,7 @@ final SP4 = [
0x00802080
].map((i) => Int32(i)).toList();

final SP5 = [
final sp5 = [
0x00000100,
0x02080100,
0x02080000,
Expand Down Expand Up @@ -478,7 +476,7 @@ final SP5 = [
0x40000100
].map((i) => Int32(i)).toList();

final SP6 = [
final sp6 = [
0x20000010,
0x20400000,
0x00004000,
Expand Down Expand Up @@ -545,7 +543,7 @@ final SP6 = [
0x20004010
].map((i) => Int32(i)).toList();

final SP7 = [
final sp7 = [
0x00200000,
0x04200002,
0x04000802,
Expand Down Expand Up @@ -612,7 +610,7 @@ final SP7 = [
0x00200002
].map((i) => Int32(i)).toList();

final SP8 = [
final sp8 = [
0x10001040,
0x00001000,
0x00040000,
Expand Down
104 changes: 52 additions & 52 deletions lib/src/messages/common/flags.dart
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
const NTLM_NegotiateUnicode = 0x00000001;
const NTLM_NegotiateOEM = 0x00000002;
const NTLM_RequestTarget = 0x00000004;
const NTLM_Unknown9 = 0x00000008;
const NTLM_NegotiateSign = 0x00000010;
const NTLM_NegotiateSeal = 0x00000020;
const NTLM_NegotiateDatagram = 0x00000040;
const NTLM_NegotiateLanManagerKey = 0x00000080;
const NTLM_Unknown8 = 0x00000100;
const NTLM_NegotiateNTLM = 0x00000200;
const NTLM_NegotiateNTOnly = 0x00000400;
const NTLM_Anonymous = 0x00000800;
const NTLM_NegotiateOemDomainSupplied = 0x00001000;
const NTLM_NegotiateOemWorkstationSupplied = 0x00002000;
const NTLM_Unknown6 = 0x00004000;
const NTLM_NegotiateAlwaysSign = 0x00008000;
const NTLM_TargetTypeDomain = 0x00010000;
const NTLM_TargetTypeServer = 0x00020000;
const NTLM_TargetTypeShare = 0x00040000;
const NTLM_NegotiateExtendedSecurity = 0x00080000;
const NTLM_NegotiateIdentify = 0x00100000;
const NTLM_Unknown5 = 0x00200000;
const NTLM_RequestNonNTSessionKey = 0x00400000;
const NTLM_NegotiateTargetInfo = 0x00800000;
const NTLM_Unknown4 = 0x01000000;
const NTLM_NegotiateVersion = 0x02000000;
const NTLM_Unknown3 = 0x04000000;
const NTLM_Unknown2 = 0x08000000;
const NTLM_Unknown1 = 0x10000000;
const NTLM_Negotiate128 = 0x20000000;
const NTLM_NegotiateKeyExchange = 0x40000000;
const NTLM_Negotiate56 = 0x80000000;
const ntlmNegotiateUnicode = 0x00000001;
const ntlmNegotiateOEM = 0x00000002;
const ntlmRequestTarget = 0x00000004;
const ntlmUnknown9 = 0x00000008;
const ntlmNegotiateSign = 0x00000010;
const ntlmNegotiateSeal = 0x00000020;
const ntlmNegotiateDatagram = 0x00000040;
const ntlmNegotiateLanManagerKey = 0x00000080;
const ntlmUnknown8 = 0x00000100;
const ntlmNegotiateNTLM = 0x00000200;
const ntlmNegotiateNTOnly = 0x00000400;
const ntlmAnonymous = 0x00000800;
const ntlmNegotiateOemDomainSupplied = 0x00001000;
const ntlmNegotiateOemWorkstationSupplied = 0x00002000;
const ntlmUnknown6 = 0x00004000;
const ntlmNegotiateAlwaysSign = 0x00008000;
const ntlmTargetTypeDomain = 0x00010000;
const ntlmTargetTypeServer = 0x00020000;
const ntlmTargetTypeShare = 0x00040000;
const ntlmNegotiateExtendedSecurity = 0x00080000;
const ntlmNegotiateIdentify = 0x00100000;
const ntlmUnknown5 = 0x00200000;
const ntlmRequestNonNTSessionKey = 0x00400000;
const ntlmNegotiateTargetInfo = 0x00800000;
const ntlmUnknown4 = 0x01000000;
const ntlmNegotiateVersion = 0x02000000;
const ntlmUnknown3 = 0x04000000;
const ntlmUnknown2 = 0x08000000;
const ntlmUnknown1 = 0x10000000;
const ntlmNegotiate128 = 0x20000000;
const ntlmNegotiateKeyExchange = 0x40000000;
const ntlmNegotiate56 = 0x80000000;

const NTLM_TYPE1_FLAGS = NTLM_NegotiateUnicode +
NTLM_NegotiateOEM +
NTLM_RequestTarget +
NTLM_NegotiateNTLM +
NTLM_NegotiateOemDomainSupplied +
NTLM_NegotiateOemWorkstationSupplied +
NTLM_NegotiateAlwaysSign +
NTLM_NegotiateExtendedSecurity +
NTLM_NegotiateVersion +
NTLM_Negotiate128 +
NTLM_Negotiate56;
const ntlmType1Flags = ntlmNegotiateUnicode +
ntlmNegotiateOEM +
ntlmRequestTarget +
ntlmNegotiateNTLM +
ntlmNegotiateOemDomainSupplied +
ntlmNegotiateOemWorkstationSupplied +
ntlmNegotiateAlwaysSign +
ntlmNegotiateExtendedSecurity +
ntlmNegotiateVersion +
ntlmNegotiate128 +
ntlmNegotiate56;

const NTLM_TYPE2_FLAGS = NTLM_NegotiateUnicode +
NTLM_RequestTarget +
NTLM_NegotiateNTLM +
NTLM_NegotiateAlwaysSign +
NTLM_NegotiateExtendedSecurity +
NTLM_NegotiateTargetInfo +
NTLM_NegotiateVersion +
NTLM_Negotiate128 +
NTLM_Negotiate56;
const ntlmType2Flags = ntlmNegotiateUnicode +
ntlmRequestTarget +
ntlmNegotiateNTLM +
ntlmNegotiateAlwaysSign +
ntlmNegotiateExtendedSecurity +
ntlmNegotiateTargetInfo +
ntlmNegotiateVersion +
ntlmNegotiate128 +
ntlmNegotiate56;
7 changes: 4 additions & 3 deletions lib/src/messages/common/utils.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'dart:convert';
import 'dart:math' as math;
import 'dart:typed_data';

import 'package:ntlm/src/des/des.dart';
import 'package:ntlm/src/messages/type2.dart';
import 'package:pointycastle/api.dart';
import 'package:pointycastle/block/modes/ecb.dart';
import 'package:pointycastle/digests/md4.dart';
import 'package:pointycastle/digests/md5.dart';
import 'package:pointycastle/block/modes/ecb.dart';
import 'package:pointycastle/macs/hmac.dart';
import 'package:ntlm/src/des/des.dart';
import 'package:ntlm/src/messages/type2.dart';

void write(ByteData buf, Uint8List data, int offset, int length) {
for (var i = 0; i < length; i++) {
Expand Down
Loading