summaryrefslogtreecommitdiff
path: root/lib/crypto/gnutls_aead_aes_256_cbc_hmac_sha512.c
blob: 2e37dcd23aae0feed76c86c556da483ff42dc10f (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * Copyright (c) 2021-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 "includes.h"
#include "lib/util/data_blob.h"
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#include "gnutls_helpers.h"

#define SAMR_AES_VERSION_BYTE 0x01
#define SAMR_AES_VERSION_BYTE_LEN 1

static NTSTATUS calculate_enc_key(const DATA_BLOB *cek,
				  const DATA_BLOB *key_salt,
				  uint8_t enc_key[32])
{
	gnutls_mac_algorithm_t hash_algo = GNUTLS_MAC_SHA512;
	size_t hmac_size = gnutls_hmac_get_len(hash_algo);
	uint8_t enc_key_data[hmac_size];
	int rc;

	rc = gnutls_hmac_fast(hash_algo,
			      cek->data,
			      cek->length,
			      key_salt->data,
			      key_salt->length,
			      enc_key_data);
	if (rc < 0) {
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_ENCRYPTION_FAILED);
	}

	/* The key gets truncated to 32 byte */
	memcpy(enc_key, enc_key_data, 32);
	BURN_DATA(enc_key_data);

	return NT_STATUS_OK;
}

static NTSTATUS calculate_mac_key(const DATA_BLOB *cek,
				  const DATA_BLOB *mac_salt,
				  uint8_t mac_key[64])
{
	int rc;

	rc = gnutls_hmac_fast(GNUTLS_MAC_SHA512,
			      cek->data,
			      cek->length,
			      mac_salt->data,
			      mac_salt->length,
			      mac_key);
	if (rc < 0) {
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_ENCRYPTION_FAILED);
	}

	return NT_STATUS_OK;
}

/* This is an implementation of [MS-SAMR] 3.2.2.4 AES Cipher Usage */

NTSTATUS
samba_gnutls_aead_aes_256_cbc_hmac_sha512_encrypt(TALLOC_CTX *mem_ctx,
						  const DATA_BLOB *plaintext,
						  const DATA_BLOB *cek,
						  const DATA_BLOB *key_salt,
						  const DATA_BLOB *mac_salt,
						  const DATA_BLOB *iv,
						  DATA_BLOB *pciphertext,
						  uint8_t pauth_tag[64])
{
	gnutls_hmac_hd_t hmac_hnd = NULL;
	gnutls_mac_algorithm_t hmac_algo = GNUTLS_MAC_SHA512;
	size_t hmac_size = gnutls_hmac_get_len(hmac_algo);
	gnutls_cipher_hd_t cipher_hnd = NULL;
	gnutls_cipher_algorithm_t cipher_algo = GNUTLS_CIPHER_AES_256_CBC;
	uint32_t aes_block_size = gnutls_cipher_get_block_size(cipher_algo);
	gnutls_datum_t iv_datum = {
		.data = iv->data,
		.size = iv->length,
	};
	uint8_t enc_key_data[32] = {0};
	gnutls_datum_t enc_key = {
		.data = enc_key_data,
		.size = sizeof(enc_key_data),
	};
	uint8_t *cipher_text = NULL;
	size_t cipher_text_len = 0;
	uint8_t mac_key_data[64] = {0};
	gnutls_datum_t mac_key = {
		.data = mac_key_data,
		.size = sizeof(mac_key_data),
	};
	uint8_t version_byte = SAMR_AES_VERSION_BYTE;
	uint8_t version_byte_len = SAMR_AES_VERSION_BYTE_LEN;
	uint8_t auth_data[hmac_size];
	DATA_BLOB padded_plaintext;
	size_t padding;
	NTSTATUS status;
	int rc;

	/*
	 * We don't want to overflow 'pauth_tag', which is 64 bytes in
	 * size.
	 */
	SMB_ASSERT(hmac_size == 64);

	if (plaintext->length == 0 || cek->length == 0 ||
	    key_salt->length == 0 || mac_salt->length == 0 || iv->length == 0) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	/*
	 * PKCS#7 padding
	 *
	 * TODO: Use gnutls_cipher_encrypt3()
	 */

	if (plaintext->length + aes_block_size < plaintext->length) {
		return NT_STATUS_INVALID_BUFFER_SIZE;
	}

	padded_plaintext.length =
		aes_block_size * (plaintext->length / aes_block_size) +
		aes_block_size;

	padding = padded_plaintext.length - plaintext->length;

	padded_plaintext =
		data_blob_talloc(mem_ctx, NULL, padded_plaintext.length);
	if (padded_plaintext.data == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	/* Allocate buffer for cipher text */
	cipher_text_len = padded_plaintext.length;
	cipher_text = talloc_size(mem_ctx, cipher_text_len);
	if (cipher_text == NULL) {
		data_blob_free(&padded_plaintext);
		return NT_STATUS_NO_MEMORY;
	}

	memcpy(padded_plaintext.data, plaintext->data, plaintext->length);
	memset(padded_plaintext.data + plaintext->length, padding, padding);

	status = calculate_enc_key(cek, key_salt, enc_key_data);
	if (!NT_STATUS_IS_OK(status)) {
		data_blob_clear_free(&padded_plaintext);
		return status;
	}

	/* Encrypt plaintext */
	rc = gnutls_cipher_init(&cipher_hnd, cipher_algo, &enc_key, &iv_datum);
	if (rc < 0) {
		data_blob_clear_free(&padded_plaintext);
		BURN_DATA(enc_key_data);
		TALLOC_FREE(cipher_text);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_ENCRYPTION_FAILED);
	}

	rc = gnutls_cipher_encrypt2(cipher_hnd,
				    padded_plaintext.data,
				    padded_plaintext.length,
				    cipher_text,
				    cipher_text_len);
	gnutls_cipher_deinit(cipher_hnd);
	data_blob_clear_free(&padded_plaintext);
	BURN_DATA(enc_key_data);
	if (rc < 0) {
		TALLOC_FREE(cipher_text);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_ENCRYPTION_FAILED);
	}

	/* Calculate mac key */
	status = calculate_mac_key(cek, mac_salt, mac_key_data);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(cipher_text);
		return status;
	}

	/* Generate auth tag */
	rc = gnutls_hmac_init(&hmac_hnd, hmac_algo, mac_key.data, mac_key.size);
	BURN_DATA(mac_key_data);
	if (rc < 0) {
		TALLOC_FREE(cipher_text);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_ENCRYPTION_FAILED);
	}

	rc = gnutls_hmac(hmac_hnd, &version_byte, sizeof(uint8_t));
	if (rc < 0) {
		TALLOC_FREE(cipher_text);
		gnutls_hmac_deinit(hmac_hnd, NULL);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_ENCRYPTION_FAILED);
	}

	rc = gnutls_hmac(hmac_hnd, iv->data, iv->length);
	if (rc < 0) {
		TALLOC_FREE(cipher_text);
		gnutls_hmac_deinit(hmac_hnd, NULL);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_ENCRYPTION_FAILED);
	}

	rc = gnutls_hmac(hmac_hnd, cipher_text, cipher_text_len);
	if (rc < 0) {
		TALLOC_FREE(cipher_text);
		gnutls_hmac_deinit(hmac_hnd, NULL);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_ENCRYPTION_FAILED);
	}

	rc = gnutls_hmac(hmac_hnd, &version_byte_len, sizeof(uint8_t));
	if (rc < 0) {
		TALLOC_FREE(cipher_text);
		gnutls_hmac_deinit(hmac_hnd, NULL);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_ENCRYPTION_FAILED);
	}
	gnutls_hmac_deinit(hmac_hnd, auth_data);

	if (pciphertext != NULL) {
		pciphertext->length = cipher_text_len;
		pciphertext->data = cipher_text;
	}
	(void)memcpy(pauth_tag, auth_data, hmac_size);

	return NT_STATUS_OK;
}

NTSTATUS
samba_gnutls_aead_aes_256_cbc_hmac_sha512_decrypt(TALLOC_CTX *mem_ctx,
						  const DATA_BLOB *ciphertext,
						  const DATA_BLOB *cdk,
						  const DATA_BLOB *key_salt,
						  const DATA_BLOB *mac_salt,
						  const DATA_BLOB *iv,
						  const uint8_t auth_tag[64],
						  DATA_BLOB *pplaintext)
{
	gnutls_hmac_hd_t hmac_hnd = NULL;
	gnutls_mac_algorithm_t hash_algo = GNUTLS_MAC_SHA512;
	size_t hmac_size = gnutls_hmac_get_len(hash_algo);
	uint8_t dec_key_data[32];
	uint8_t mac_key_data[64];
	gnutls_datum_t mac_key = {
		.data = mac_key_data,
		.size = sizeof(mac_key_data),
	};
	gnutls_cipher_hd_t cipher_hnd = NULL;
	gnutls_cipher_algorithm_t cipher_algo = GNUTLS_CIPHER_AES_256_CBC;
	gnutls_datum_t dec_key = {
		.data = dec_key_data,
		.size = sizeof(dec_key_data),
	};
	gnutls_datum_t iv_datum = {
		.data = iv->data,
		.size = iv->length,
	};
	uint8_t version_byte = SAMR_AES_VERSION_BYTE;
	uint8_t version_byte_len = SAMR_AES_VERSION_BYTE_LEN;
	uint8_t auth_data[hmac_size];
	uint8_t padding;
	size_t i;
	NTSTATUS status;
	bool equal;
	int rc;

	if (cdk->length == 0 || ciphertext->length == 0 ||
	    key_salt->length == 0 || mac_salt->length == 0 || iv->length == 0 ||
	    pplaintext == NULL) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	/* Calculate mac key */
	status = calculate_mac_key(cdk, mac_salt, mac_key_data);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	rc = gnutls_hmac_init(&hmac_hnd, hash_algo, mac_key.data, mac_key.size);
	BURN_DATA(mac_key_data);
	if (rc < 0) {
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_DECRYPTION_FAILED);
	}

	rc = gnutls_hmac(hmac_hnd, &version_byte, sizeof(uint8_t));
	if (rc < 0) {
		gnutls_hmac_deinit(hmac_hnd, NULL);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_DECRYPTION_FAILED);
	}

	rc = gnutls_hmac(hmac_hnd, iv->data, iv->length);
	if (rc < 0) {
		gnutls_hmac_deinit(hmac_hnd, NULL);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_DECRYPTION_FAILED);
	}

	rc = gnutls_hmac(hmac_hnd, ciphertext->data, ciphertext->length);
	if (rc < 0) {
		gnutls_hmac_deinit(hmac_hnd, NULL);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_DECRYPTION_FAILED);
	}

	rc = gnutls_hmac(hmac_hnd, &version_byte_len, sizeof(uint8_t));
	if (rc < 0) {
		gnutls_hmac_deinit(hmac_hnd, NULL);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_DECRYPTION_FAILED);
	}
	gnutls_hmac_deinit(hmac_hnd, auth_data);

	equal = mem_equal_const_time(auth_data, auth_tag, sizeof(auth_data));
	if (!equal) {
		return NT_STATUS_DECRYPTION_FAILED;
	}

	*pplaintext = data_blob_talloc_zero(mem_ctx, ciphertext->length);
	if (pplaintext->data == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	/* Calculate decryption key */
	status = calculate_enc_key(cdk, key_salt, dec_key_data);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	rc = gnutls_cipher_init(&cipher_hnd, cipher_algo, &dec_key, &iv_datum);
	BURN_DATA(dec_key_data);
	if (rc < 0) {
		data_blob_free(pplaintext);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_DECRYPTION_FAILED);
	}

	rc = gnutls_cipher_decrypt2(cipher_hnd,
				    ciphertext->data,
				    ciphertext->length,
				    pplaintext->data,
				    pplaintext->length);
	gnutls_cipher_deinit(cipher_hnd);
	if (rc < 0) {
		data_blob_clear_free(pplaintext);
		return gnutls_error_to_ntstatus(rc,
						NT_STATUS_DECRYPTION_FAILED);
	}

	/*
	 * PKCS#7 padding
	 *
	 * TODO: Use gnutls_cipher_decrypt3()
	 */

	/*
	 * The plaintext is always padded.
	 *
	 * We already checked for ciphertext->length == 0 above and the
	 * pplaintext->length is equal to ciphertext->length here. We need to
	 * remove the padding from the plaintext size.
	 */
	padding = pplaintext->data[pplaintext->length - 1];
	if (padding == 0 || padding > 16) {
		data_blob_clear_free(pplaintext);
		return NT_STATUS_DECRYPTION_FAILED;
	}

	for (i = pplaintext->length - padding; i < pplaintext->length; i++) {
		if (pplaintext->data[i] != padding) {
			data_blob_clear_free(pplaintext);
			return NT_STATUS_DECRYPTION_FAILED;
		}
	}

	pplaintext->length -= padding;

	return NT_STATUS_OK;
}