summaryrefslogtreecommitdiffstats
path: root/libcli/auth/tests/test_encode_decode.c
blob: 4683edf7ce58b4a9113db3fe7b7571134be17ae7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Unix SMB/CIFS implementation.
 *
 * Copyright (C) 2022 Andreas Schneider <asn@samba.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>

#include "libcli/auth/smbencrypt.c"

/* The following hexdumps are from a Windows Server 2022 time trace */
static const uint8_t plaintext_data[] = {
	0x14, 0x00, 0x50, 0x00, 0x61, 0x00, 0x24, 0x00,
	0x24, 0x00, 0x77, 0x00, 0x30, 0x00, 0x72, 0x00,
	0x64, 0x00, 0x40, 0x00, 0x32, 0x00, 0xc2, 0x34,
	0x7d, 0x21, 0x79, 0x05, 0xef, 0x88, 0xd7, 0x11,
	0xec, 0xe2, 0xce, 0xb5, 0xd4, 0x4d, 0x64, 0x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,
	0xd0, 0xd7, 0x70, 0xad, 0xcd, 0x84, 0xbc, 0x1e,
	0x48, 0xa0, 0x20, 0x14, 0x10, 0xa4, 0xb1, 0x5b,
	0x05, 0x36, 0x9a, 0x6d, 0xb0, 0x10, 0x98, 0xbd,
	0x8d, 0xa2, 0xd1, 0xb2, 0xfa, 0x23, 0x37, 0xeb,
	0xb0, 0x04, 0x53, 0xcb, 0xa1, 0xa9, 0xc4, 0x88,
	0xdd, 0xf9, 0x80, 0xf5, 0xa9, 0xcd, 0x7b, 0xf8,
	0x77, 0x0b, 0x19, 0x84, 0x4c, 0xef, 0x2c, 0x14,
	0xa1, 0xdc, 0x9f, 0x2f, 0x41, 0xd0, 0xd0, 0x33,
	0x29, 0x8a, 0xb9, 0x39, 0x7e, 0xc9, 0x7f, 0xe7,
	0x63, 0x64, 0xa4, 0x7b, 0x4a, 0x6a, 0x33, 0xa7,
	0xaa, 0xf6, 0xca, 0x98, 0xc4, 0x9b, 0x62, 0x5b,
	0xcd, 0x53, 0x82, 0xbf, 0xf0, 0x0b, 0x9c, 0xe7,
	0xb2, 0x44, 0x1b, 0x88, 0x71, 0x61, 0xa1, 0x36,
	0x9e, 0x7a, 0x0a, 0x3c, 0x20, 0xd8, 0xff, 0xa1,
	0x23, 0x66
};

static void torture_encode_pwd_buffer514_from_str(void **state)
{
	uint8_t data[514] = {0};
	bool ok;

	ok = encode_pwd_buffer514_from_str(data, "Pa$$w0rd@2", STR_UNICODE);
	assert_true(ok);

	/* We can only compare the first 22 bytes as the rest is random data */
	assert_memory_equal(data, plaintext_data, 22);
	assert_memory_not_equal(data + 22,
				plaintext_data + 22,
				sizeof(plaintext_data) - 22);
}

static void torture_extract_pwd_blob_from_buffer514(void **state)
{
	DATA_BLOB new_password = {
		.length = 0,
	};
	bool ok;

	ok = extract_pwd_blob_from_buffer514(NULL, plaintext_data, &new_password);
	assert_true(ok);
	assert_int_equal(new_password.length, 20);
	assert_memory_equal(new_password.data,
			    plaintext_data + 2,
			    new_password.length);
	data_blob_free(&new_password);
}

static void torture_decode_pwd_string_from_buffer514(void **state)
{
	DATA_BLOB decoded_password = {
		.length = 0,
	};
	bool ok;

	ok = decode_pwd_string_from_buffer514(NULL,
					   plaintext_data,
				           CH_UTF16,
					   &decoded_password);
	assert_true(ok);
	assert_memory_equal(decoded_password.data, "Pa$$w0rd@2", decoded_password.length);
	data_blob_free(&decoded_password);
}

int main(int argc, char *argv[])
{
	int rc;
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(torture_encode_pwd_buffer514_from_str),
		cmocka_unit_test(torture_extract_pwd_blob_from_buffer514),
		cmocka_unit_test(torture_decode_pwd_string_from_buffer514),
	};

	if (argc == 2) {
		cmocka_set_test_filter(argv[1]);
	}
	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);

	rc = cmocka_run_group_tests(tests, NULL, NULL);

	return rc;
}