Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ecies.c « cryptography « providers « src - github.com/lavabit/magma.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e5b6b88a5908830ccc64c85611019e2498a3f0a (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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695

/**
 * @file /magma/providers/cryptography/ecies.c
 *
 * @brief	ECIES encryption/decryption functions.
 */

#include "magma.h"

EC_GROUP *ecies_curve_group = NULL;
const EVP_MD *ecies_hmac_evp = NULL;
const EVP_MD *ecies_envelope_evp = NULL;
const EVP_CIPHER *ecies_cipher_evp = NULL;

bool_t deprecated_ecies_start(void) {

	if (!(ecies_cipher_evp = EVP_get_cipherbyname_d(OBJ_nid2sn_d(ECIES_CIPHER))) || !(ecies_hmac_evp = EVP_get_digestbyname_d(OBJ_nid2sn_d(ECIES_HMAC))) ||
			!(ecies_envelope_evp = EVP_get_digestbyname_d(OBJ_nid2sn_d(ECIES_ENVELOPE))) || !(ecies_curve_group	= deprecated_ecies_group(ECIES_CURVE, true))) {
		log_error("Initialization of the default ECIES algorithms failed.");
		return false;
	}

	return true;
}

/**
 * @brief	Destroy all initialized ECIES curve group information.
 * @return	This function returns no value.
 */
void deprecated_ecies_stop(void) {

	EC_GROUP *group;

	if (ecies_curve_group) {
		group = ecies_curve_group;
		ecies_curve_group = NULL;
		EC_GROUP_free_d(group);
	}

	return;
}

/**
 * @brief	Free an ECIES key pair.
 * @return	This function returns no value.
 */
void deprecated_ecies_key_free(EC_KEY *key) {

	EC_KEY_free_d(key);
	return;
}

/**
 * @brief	Allocate a new ECIES key pair from the curve defined by ECIES_CURVE.
 * @see		NID_sect571k1
 * @return	NULL on failure, or a pointer to the newly allocated key pair.
 */
EC_KEY * deprecated_ecies_key_alloc(void) {

	EC_KEY *key = NULL;

	// Create a key and assign the group.
	if (ecies_curve_group) {

		if (!(key = EC_KEY_new_d())) {
			log_info("An error occurred while initializing an empty ECIES key context. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		} else if (EC_KEY_set_group_d(key, ecies_curve_group) != 1) {
			log_info("Unable to assign the default group to our empty key context.. {%s}", ssl_error_string(MEMORYBUF(256), 256));
			EC_KEY_free_d(key);
			key = NULL;
		}

	}

	// If an error occurs above, attempt key creation from scratch.
	if (!key) {

		if (!(key = EC_KEY_new_by_curve_name_d(ECIES_CURVE))) {
			log_info("An error occurred while trying to create a new ECIES key using the default curve. {%s}", ssl_error_string(MEMORYBUF(256), 256));
			return NULL;
		}

		EC_KEY_set_conv_form_d(key, POINT_CONVERSION_COMPRESSED);
	}

	return key;
}

EC_GROUP * deprecated_ecies_group(uint64_t curve, bool_t precompute) {

	EC_GROUP *group;

	if (!(group = EC_GROUP_new_by_curve_name_d(curve))) {
		log_error("An error occurred while trying to create the elliptical group. {%s}",
				ssl_error_string(MEMORYBUF(256), 256));
		return NULL;
	} else if (precompute && EC_GROUP_precompute_mult_d(group, NULL) != 1) {
		log_error("Unable to precompute the required elliptical curve point data. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EC_GROUP_free_d(group);
		return NULL;
	}

	EC_GROUP_set_point_conversion_form_d(group, POINT_CONVERSION_COMPRESSED);

	return group;
}

/**
 * @brief	Generate a random ECIES key pair.
 * @return	NULL on failure, or a new random ECIES key pair on success.
 */
EC_KEY * deprecated_ecies_key_create(void) {

	EC_KEY *key = NULL;

	if (!(key = deprecated_ecies_key_alloc())) {
		log_info("Unable to allocate an empty key context.");
		return NULL;
	}

	// This should generate a random key pair.
	if (EC_KEY_generate_key_d(key) != 1) {
		log_info("An error occurred while trying to generate a random ECIES key pair. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EC_KEY_free_d(key);
		return NULL;
	}

	return key;
}

EC_KEY * deprecated_ecies_key_public(uint64_t format, placer_t data) {

	EC_KEY *key = NULL;
	EC_POINT *point = NULL;

	if (!(key = deprecated_ecies_key_alloc())) {
		log_info("Unable to allocate an empty key context.");
		return NULL;
	}

	// Process a key in binary format.
	if (format & ECIES_PUBLIC_BINARY) {

		// Generate the empty point context we'll be assigning to below.
		if (!(point = EC_POINT_new_d(EC_KEY_get0_group_d(key)))) {
			log_info("An error occurred while allocate the elliptical curve point. {%s}", ssl_error_string(MEMORYBUF(256), 256));
			EC_KEY_free_d(key);
			return NULL;
		}
		else if (EC_POINT_oct2point_d(EC_KEY_get0_group_d(key), point, pl_data_get(data), pl_length_get(data), NULL) != 1) {
			log_info("An error occurred while parsing the binary elliptical curve point data used to represent the public key. {%s}",
					ssl_error_string(MEMORYBUF(256), 256));
			EC_POINT_free_d(point);
			EC_KEY_free_d(key);
			return NULL;
		}

	}
	// Process a key in hex.
	else if (format & ECIES_PUBLIC_HEX) {

		if (!(point = EC_POINT_hex2point_d(EC_KEY_get0_group_d(key), pl_char_get(data), NULL, NULL))) {
			log_info("An error occurred while parsing the binary elliptical curve point data used to represent the public key. {%s}",
					ssl_error_string(MEMORYBUF(256), 256));
			EC_KEY_free_d(key);
			return NULL;
		}

	}
	// Invalid format!
	else {
		log_info("The public key data is using an unrecognized format.");
		EC_POINT_free_d(point);
		EC_KEY_free_d(key);
		return NULL;
	}

	// Assign the point to our empty key instance.
	if (EC_KEY_set_public_key_d(key, point) != 1) {
		log_info("The provided point data does not represent a valid public key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EC_POINT_free_d(point);
		EC_KEY_free_d(key);
		return NULL;
	}

	// The above function call duplicates the point so the local copy is no longer needed.
	EC_POINT_free_d(point);

	// Ensures the provided point is along the active elliptical curve and therefore represents a valid public key.
	if (EC_KEY_check_key_d(key) != 1) {
		log_info("The provided point data does not represent a valid public key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EC_KEY_free_d(key);
		return NULL;
	}

	return key;
}

EC_KEY * deprecated_ecies_key_private(uint64_t format, placer_t data) {

	EC_KEY *key = NULL;
	BIGNUM *number = NULL;

	if (!(key = deprecated_ecies_key_alloc())) {
		log_info("Unable to allocate an empty key context.");
		return NULL;
	}

	// Process a key in binary format.
	if (format & ECIES_PRIVATE_BINARY) {

		if (!(number = BN_bin2bn_d(pl_data_get(data), pl_length_get(data), NULL))) {
			log_info("An error occurred while parsing the binary elliptical curve point data used to represent the private key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
			EC_KEY_free_d(key);
			return NULL;
		}

	}
	// Process a key in hex.
	else if (format & ECIES_PRIVATE_HEX) {

		if (!(BN_hex2bn_d(&number, pl_char_get(data)))) {
			log_info("An error occurred while parsing the binary elliptical curve point data used to represent the private key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
			EC_KEY_free_d(key);
			return NULL;
		}

	}
	// Invalid format!
	else {
		log_info("The private key data is using an unrecognized format.");
		EC_KEY_free_d(key);
		return NULL;
	}

	// Assign the point to our empty key instance.
	if (EC_KEY_set_private_key_d(key, number) != 1) {
		log_info("The provided point data does not represent a valid public key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EC_KEY_free_d(key);
		BN_free_d(number);
		return NULL;
	}

	// The above function call duplicates the point so the local copy is no longer needed.
	BN_free_d(number);

	return key;
}

/**
 * @brief	Return an ECIES public key as a null-terminated hex string.
 * @param	key	the input ECIES key pair.
 * @return	NULL on failure, or the hex-formatted public key as a null-terminated string.
 */
stringer_t * deprecated_ecies_key_public_hex(EC_KEY *key) {

	char *hex;
	const EC_POINT *point;
	const EC_GROUP *group;
	stringer_t *result = NULL;

	if (!(point = EC_KEY_get0_public_key_d(key))) {
		log_info("No public key available. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		return NULL;
	} else if (!(group = EC_KEY_get0_group_d(key))) {
		log_info("No group available. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		return NULL;
	} else if (!(hex = EC_POINT_point2hex_d(group, point, POINT_CONVERSION_COMPRESSED, NULL))) {
		log_info("Unable to serialize the public key into hex. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		return NULL;
	}

	if (!(result = st_import(hex, ns_length_get(hex) + 1))) {
		log_info("Unable to make copy of ECIES public key.");
	}

	OPENSSL_free_d(hex);

	return result;
}

/**
 * @brief	Return an ECIES public key as binary data.
 * @param	key		the input ECIES key pair.
 * @param	olen	a pointer to store the length of the returned key.
 * @return	NULL on failure, or a pointer to the raw public key.
 */
uchr_t * deprecated_ecies_key_public_bin(EC_KEY *key, size_t *olen) {

	uchr_t *result;
	size_t rlen, blen = 512;
	const EC_POINT *point;
	const EC_GROUP *group;

	if (!(point = EC_KEY_get0_public_key_d(key))) {
		log_info("No public key available. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		return NULL;
	} else if (!(group = EC_KEY_get0_group_d(key))) {
		log_info("No group available. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		return NULL;
	} else if (!(result = mm_alloc(blen))) {
		log_info("Error allocating space for ECIES public key.");
		return NULL;
	} else if ((rlen = EC_POINT_point2oct_d(group, point, POINT_CONVERSION_COMPRESSED, result, blen, NULL)) <= 0) {
		log_info("Unable to extract the public key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		mm_free(result);
		return NULL;
	}

	if (olen) {
		*olen = rlen;
	}

	//	char * (*EC_POINT_point2hex_d)(const EC_GROUP *, const EC_POINT *, point_conversion_form_t form, BN_CTX *) __attribute__ ((common)) = NULL;
	//size_t (*EC_POINT_point2oct_d)(const EC_GROUP *, const EC_POINT *, point_conversion_form_t form, unsigned char *buf, size_t len, BN_CTX *ctx) __attribute__ ((common)) = NULL;

	return result;
}

/**
 * @brief	Return an ECIES private key as a hex string.
 * @param	key	the input ECIES key pair.
 * @return	NULL on failure, or the hex-formatted private key as a managed string.
 */
stringer_t * deprecated_ecies_key_private_hex(EC_KEY *key) {

	chr_t *hex;
	const BIGNUM *bn;
	stringer_t *result = NULL;

	if (!(bn = EC_KEY_get0_private_key_d(key))) {
		log_pedantic("No private key available. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		return NULL;
	}
	else if (!(hex = BN_bn2hex_d(bn))) {
		log_pedantic("Unable to serialize the private key into hex. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		return NULL;
	}

	else if (!(result = st_import_opts(MANAGED_T | CONTIGUOUS | SECURE, hex, ns_length_get(hex) + 1))) {
		log_pedantic("Unable to allocate secure buffer for hex string.");
		ns_wipe(hex, ns_length_get(hex));
		OPENSSL_free_d(hex);
		return NULL;
	}

	ns_wipe(hex, ns_length_get(hex));
	OPENSSL_free_d(hex);

	return result;
}

/**
 * @brief	Return an ECIES private key as binary data.
 * @param	key		the input ECIES key pair.
 * @param	olen	a pointer to store the length of the returned key.
 * @return	NULL on failure, or a pointer to the raw private key.
 */
uchr_t * deprecated_ecies_key_private_bin(EC_KEY *key, size_t *olen) {

	const BIGNUM *bn;
	int bn_len;
	uchr_t *result;

	if (!(bn = EC_KEY_get0_private_key_d(key))) {
		log_info("No private key available. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		return NULL;
	} else if (!(bn_len = BN_num_bytes_d(bn))) {
		log_info("Error determining size of ECIES private key.");
		return NULL;
	}

	if (!(result = mm_sec_alloc(bn_len))) {
		log_info("Error allocating space for ECIES private key.");
		return NULL;
	}
	else if (!BN_bn2bin_d(bn, (unsigned char *)result)) {
		log_info("Error retrieving ECIES private key.");
		mm_sec_free(result);
		return NULL;
	}

	if (olen) {
		*olen = bn_len;
	}

	return result;
}

void * deprecated_ecies_envelope_derivation(const void *input, size_t ilen, void *output, size_t *olen) {

	if (EVP_Digest_d(input, ilen, output, (unsigned int *)olen, ecies_envelope_evp, NULL) != 1) {
		return NULL;
	}

	return output;
}

/**
 * @brief	Encrypt a block of data using an ECIES public key.
 * @param	key			the ECIES public key in the specified format.
 * @param	key_type	the encoding type of the ECIES public key (ECIES_PUBLIC_BINARY or ECIES_PUBLIC_HEX).
 * @param	data		a pointer to the block of data to be encrypted.
 * @param	length		the length, in bytes, of the data to be encrypted.
 * @return	NULL on failure, or a pointer to the header of the cryptex object containing the encrypted data on success..
 */
cryptex_t * deprecated_ecies_encrypt(stringer_t *key, ECIES_KEY_TYPE key_type, unsigned char *data, size_t length) {

	void *body;
	HMAC_CTX hmac;
	int body_length;
	cryptex_t *cryptex;
	EVP_CIPHER_CTX cipher;
	unsigned int mac_length;
	EC_KEY *user, *ephemeral;
	size_t envelope_length = 0, block_length = 0, key_length = 0, hexkey_length = 0;
	uchr_t *kbuf;
	unsigned char envelope_key[SHA512_DIGEST_LENGTH], iv[EVP_MAX_IV_LENGTH], block[EVP_MAX_BLOCK_LENGTH];

	// Simple sanity check.
	if (!key || !data || !length) {
		log_info("Invalid parameters passed in.");
		return NULL;
	}
	else if ((key_type != ECIES_PUBLIC_HEX) && (key_type != ECIES_PUBLIC_BINARY)) {
		log_info("Invalid ecies private key type specified!");
		return NULL;
	}
	else if (st_empty_out(key,&kbuf,&hexkey_length)) {
		log_info("Could not read key data.");
		return NULL;
	}

	// Make sure we are generating enough key material for the symmetric ciphers.
	if ((key_length = EVP_CIPHER_key_length_d(EVP_get_cipherbyname_d(OBJ_nid2sn_d(ECIES_CIPHER)))) * 2 > SHA512_DIGEST_LENGTH) {
		log_info("The key derivation method will not produce enough envelope key material for the chosen ciphers. {envelope = %i / required = %zu}", SHA512_DIGEST_LENGTH / 8, (key_length * 2) / 8);
		return NULL;
	}
	// Convert the user's public key from hex into a full EC_KEY structure.
	if (!(user = deprecated_ecies_key_public(key_type, pl_init(kbuf, hexkey_length)))) {
		log_info("Invalid public key provided.");
		return NULL;
	}
	// Create the ephemeral key used specifically for this block of data.
	else if (!(ephemeral = deprecated_ecies_key_create())) {
		log_info("An error occurred while trying to generate the ephemeral key.");
		EC_KEY_free_d(user);
		return NULL;
	}
	// Use the intersection of the provided keys to generate the envelope data used by the ciphers below. The ecies_key_derivation() function uses
	// SHA 512 to ensure we have a sufficient amount of envelope key material and that the material created is sufficiently secure.
	else if (ECDH_compute_key_d(envelope_key, SHA512_DIGEST_LENGTH, EC_KEY_get0_public_key_d(user), ephemeral, deprecated_ecies_envelope_derivation) != SHA512_DIGEST_LENGTH) {
		log_info("An error occurred while trying to compute the envelope key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EC_KEY_free_d(ephemeral);
		EC_KEY_free_d(user);
		return NULL;
	}
	// Determine the envelope and block lengths so we can allocate a buffer for the result.
	else if ((block_length = EVP_CIPHER_block_size_d(EVP_get_cipherbyname_d(OBJ_nid2sn_d(ECIES_CIPHER)))) == 0 || block_length > EVP_MAX_BLOCK_LENGTH || (envelope_length = EC_POINT_point2oct_d(EC_KEY_get0_group_d(ephemeral), EC_KEY_get0_public_key_d(ephemeral),
			POINT_CONVERSION_COMPRESSED, NULL, 0, NULL)) == 0) {
		log_info("Invalid block or envelope length. {block = %zu / envelope = %zu}", block_length, envelope_length);
		EC_KEY_free_d(ephemeral);
		EC_KEY_free_d(user);
		return NULL;
	}
	// We use a conditional to pad the length if the input buffer is not evenly divisible by the block size.
	else if (!(cryptex = deprecated_cryptex_alloc(envelope_length, EVP_MD_size_d(EVP_get_digestbyname_d(OBJ_nid2sn_d(ECIES_HMAC))), length, length + (length % block_length ? (block_length - (length % block_length)) : 0)))) {
		log_info("Unable to allocate a secure_t buffer to hold the encrypted result.");
		EC_KEY_free_d(ephemeral);
		EC_KEY_free_d(user);
		return NULL;
	}
	// Store the public key portion of the ephemeral key.
	else if (EC_POINT_point2oct_d(EC_KEY_get0_group_d(ephemeral), EC_KEY_get0_public_key_d(ephemeral), POINT_CONVERSION_COMPRESSED, deprecated_cryptex_envelope_data(cryptex), envelope_length, NULL) != envelope_length) {
		log_info("An error occurred while trying to record the public portion of the envelope key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EC_KEY_free_d(ephemeral);
		EC_KEY_free_d(user);
		deprecated_cryptex_free(cryptex);
		return NULL;
	}
	// The envelope key has been stored so we no longer need to keep the keys around.
	EC_KEY_free_d(ephemeral);
	EC_KEY_free_d(user);

	// For now we use an empty initialization vector.
	// LOW: Develop a more secure method for selecting and storing the initialization vector.
	memset(iv, 0, EVP_MAX_IV_LENGTH);

	// Setup the cipher context, the body length, and store a pointer to the body buffer location.
	EVP_CIPHER_CTX_init_d(&cipher);
	body = deprecated_cryptex_body_data(cryptex);
	body_length = deprecated_cryptex_body_length(cryptex);

	// Initialize the cipher with the envelope key.
	if (EVP_EncryptInit_ex_d(&cipher, EVP_get_cipherbyname_d(OBJ_nid2sn_d(ECIES_CIPHER)), NULL, envelope_key, iv) != 1 || EVP_CIPHER_CTX_set_padding_d(&cipher, 0) != 1 ||
			EVP_EncryptUpdate_d(&cipher, body, &body_length, data, length - (length % block_length)) != 1) {
		log_info("An error occurred while trying to secure the data using the chosen symmetric cipher. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EVP_CIPHER_CTX_cleanup_d(&cipher);
		deprecated_cryptex_free(cryptex);
		return NULL;
	}
	// Check whether all of the data was encrypted. If they don't match up, we either have a partial block remaining, or an error occurred.
	else if (body_length != length) {

		// Make sure all that remains is a partial block, and their wasn't an error.
		if (length - body_length >= block_length) {
			log_info("Unable to secure the data using the chosen symmetric cipher. {%s}", ssl_error_string(MEMORYBUF(256), 256));
			EVP_CIPHER_CTX_cleanup_d(&cipher);
			deprecated_cryptex_free(cryptex);
			return NULL;
		}

		// Copy the remaining data into our partial block buffer. The memset() call ensures any extra bytes will be zero'ed out.
		memset(block, 0, EVP_MAX_BLOCK_LENGTH);
		memcpy(block, data + body_length, length - body_length);
		// Advance the body pointer to the location of the remaining space, and calculate just how much room is still available.
		body += body_length;

		if ((body_length = deprecated_cryptex_body_length(cryptex) - body_length) < 0) {
			log_info("The symmetric cipher overflowed!");
			EVP_CIPHER_CTX_cleanup_d(&cipher);
			deprecated_cryptex_free(cryptex);
			return NULL;
		}
		// Pass the final partially filled data block into the cipher as a complete block. The padding will be removed during the decryption process.
		else if (EVP_EncryptUpdate_d(&cipher, body, &body_length, block, block_length) != 1) {
			log_info("Unable to secure the data using the chosen symmetric cipher. {%s}", ssl_error_string(MEMORYBUF(256), 256));
			EVP_CIPHER_CTX_cleanup_d(&cipher);
			deprecated_cryptex_free(cryptex);
			return NULL;
		}

	}
	// Advance the pointer, then use pointer arithmetic to calculate how much of the body buffer has been used. The complex logic is needed so that we get
	// the correct status regardless of whether there was a partial data block.
	body += body_length;

	if ((body_length = deprecated_cryptex_body_length(cryptex) - (body - deprecated_cryptex_body_data(cryptex))) < 0) {
		log_info("The symmetric cipher overflowed!");
		EVP_CIPHER_CTX_cleanup_d(&cipher);
		deprecated_cryptex_free(cryptex);
		return NULL;
	} else if (EVP_EncryptFinal_ex_d(&cipher, body, &body_length) != 1) {
		log_info("Unable to secure the data using the chosen symmetric cipher. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EVP_CIPHER_CTX_cleanup_d(&cipher);
		deprecated_cryptex_free(cryptex);
		return NULL;
	}

	EVP_CIPHER_CTX_cleanup_d(&cipher);

	// Generate an authenticated hash which can be used to validate the data during decryption.
	HMAC_CTX_init_d(&hmac);
	mac_length = deprecated_cryptex_hmac_length(cryptex);

	// At the moment we are generating the hash using encrypted data. At some point we may want to validate the original text instead.
	if (HMAC_Init_ex_d(&hmac, envelope_key + key_length, key_length, EVP_get_digestbyname_d(OBJ_nid2sn_d(ECIES_HMAC)), NULL) != 1 || HMAC_Update_d(&hmac, deprecated_cryptex_body_data(cryptex), deprecated_cryptex_body_length(cryptex)) != 1 || HMAC_Final_d(&hmac, deprecated_cryptex_hmac_data(
			cryptex), &mac_length) != 1) {
		log_info("Unable to generate a data authentication code. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		HMAC_CTX_cleanup_d(&hmac);
		deprecated_cryptex_free(cryptex);
		return NULL;
	}

	HMAC_CTX_cleanup_d(&hmac);

	return cryptex;
}

/**
 * @brief	Decrypt a block of data using an ECIES private key.
 * @param	key			the ECIES private key in the specified format.
 * @param	key_type	the encoding type of the ECIES private key (ECIES_PRIVATE_BINARY or ECIES_PRIVATE_HEX).
 * @param	cryptex		a pointer to the head of the cryptex object with the encrypted data.
 * @param	length		a pointer to a size_t variable which will receive the final length of the unencrypted data.
 * @return	NULL on failure, or a pointer to a memory address containing the decrypted data on success..
 */
uchr_t * deprecated_ecies_decrypt(stringer_t *key, ECIES_KEY_TYPE key_type, cryptex_t *cryptex, size_t *length) {

	HMAC_CTX hmac;
	size_t key_length;
	int output_length;
	EVP_CIPHER_CTX cipher;
	EC_KEY *user, *ephemeral;
	uchr_t *kbuf;
	size_t hexkey_length;
	unsigned int mac_length = EVP_MAX_MD_SIZE;
	unsigned char envelope_key[SHA512_DIGEST_LENGTH], iv[EVP_MAX_IV_LENGTH], md[EVP_MAX_MD_SIZE], *block, *output;

	// Simple sanity check.
	if (!key || !cryptex || !length) {
		log_info("Invalid parameters passed in.");
		return NULL;
	}
	else if ((key_type != ECIES_PRIVATE_HEX) && (key_type != ECIES_PRIVATE_BINARY)) {
		log_info("Invalid ecies private key type specified!");
		return NULL;
	}
	else if (st_empty_out(key,&kbuf,&hexkey_length)) {
		log_info("Could not read key data.");
		return NULL;
	}
	// Make sure we are generating enough key material for the symmetric ciphers.
	else if ((key_length = EVP_CIPHER_key_length_d(EVP_get_cipherbyname_d(OBJ_nid2sn_d(ECIES_CIPHER)))) * 2 > SHA512_DIGEST_LENGTH) {
		log_info("The key derivation method will not produce enough envelope key material for the chosen ciphers. {envelope = %i / required = %zu}", SHA512_DIGEST_LENGTH / 8, (key_length * 2) / 8);
		return NULL;
	}
	// Convert the user's public key from hex into a full EC_KEY structure.
	else if (!(user = deprecated_ecies_key_private(key_type, pl_init(kbuf, hexkey_length)))) {
		log_info("Invalid private key provided.");
		return NULL;
	}
	// Create the ephemeral key used specifically for this block of data.
	else if (!(ephemeral = deprecated_ecies_key_public(ECIES_PUBLIC_BINARY, pl_init(deprecated_cryptex_envelope_data(cryptex), deprecated_cryptex_envelope_length(cryptex))))) {
		log_info("An error occurred while trying to recreate the ephemeral key.");
		EC_KEY_free_d(user);
		return NULL;
	}
	// Use the intersection of the provided keys to generate the envelope data used by the ciphers below. The ecies_key_derivation() function uses
	// SHA 512 to ensure we have a sufficient amount of envelope key material and that the material created is sufficiently secure.
	else if (ECDH_compute_key_d(envelope_key, SHA512_DIGEST_LENGTH, EC_KEY_get0_public_key_d(ephemeral), user, deprecated_ecies_envelope_derivation) != SHA512_DIGEST_LENGTH) {
		log_info("An error occurred while trying to compute the envelope key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EC_KEY_free_d(ephemeral);
		EC_KEY_free_d(user);
		return NULL;
	}

	// The envelope key material has been extracted, so we no longer need the user and ephemeral keys.
	EC_KEY_free_d(ephemeral);
	EC_KEY_free_d(user);

	// Use the authenticated hash of the ciphered data to ensure it was not modified after being encrypted.
	HMAC_CTX_init_d(&hmac);

	// At the moment we are generating the hash using encrypted data. At some point we may want to validate the original text instead.
	if (HMAC_Init_ex_d(&hmac, envelope_key + key_length, key_length, EVP_get_digestbyname_d(OBJ_nid2sn_d(ECIES_HMAC)), NULL) != 1 || HMAC_Update_d(&hmac, deprecated_cryptex_body_data(cryptex), deprecated_cryptex_body_length(cryptex)) != 1 || HMAC_Final_d(&hmac, md, &mac_length) != 1) {
		log_info("Unable to generate the authentication code needed for validation. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		HMAC_CTX_cleanup_d(&hmac);
		return NULL;
	}

	HMAC_CTX_cleanup_d(&hmac);

	// We can use the generated hash to ensure the encrypted data was not altered after being encrypted.
	if (mac_length != deprecated_cryptex_hmac_length(cryptex) || memcmp(md, deprecated_cryptex_hmac_data(cryptex), mac_length)) {
		log_info("The authentication code was invalid! The ciphered data has been corrupted!");
		return NULL;
	}

	// Create a buffer to hold the result.
	output_length = deprecated_cryptex_body_length(cryptex);

	if (!(block = output = mm_alloc(output_length + 1))) {
		log_info("An error occurred while trying to allocate memory for the decrypted data.");
		return NULL;
	}

	// For now we use an empty initialization vector. We also clear out the result buffer just to be on the safe side.
	memset(iv, 0, EVP_MAX_IV_LENGTH);
	memset(output, 0, output_length + 1);

	EVP_CIPHER_CTX_init_d(&cipher);

	// Decrypt the data using the chosen symmetric cipher.
	if (EVP_DecryptInit_ex_d(&cipher, EVP_get_cipherbyname_d(OBJ_nid2sn_d(ECIES_CIPHER)), NULL, envelope_key, iv) != 1 || EVP_CIPHER_CTX_set_padding_d(&cipher, 0) != 1 || EVP_DecryptUpdate_d(&cipher, block, &output_length, deprecated_cryptex_body_data(cryptex),
			deprecated_cryptex_body_length(cryptex)) != 1) {
		log_info("Unable to decrypt the data using the chosen symmetric cipher. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EVP_CIPHER_CTX_cleanup_d(&cipher);
		free(output);
		return NULL;
	}

	block += output_length;

	if ((output_length = deprecated_cryptex_body_length(cryptex) - output_length) != 0) {
		log_info("The symmetric cipher failed to properly decrypt the correct amount of data!");
		EVP_CIPHER_CTX_cleanup_d(&cipher);
		free(output);
		return NULL;
	}

	if (EVP_DecryptFinal_ex_d(&cipher, block, &output_length) != 1) {
		log_info("Unable to decrypt the data using the chosen symmetric cipher. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EVP_CIPHER_CTX_cleanup_d(&cipher);
		free(output);
		return NULL;
	}

	EVP_CIPHER_CTX_cleanup_d(&cipher);

	*length = deprecated_cryptex_original_length(cryptex);

	return output;
}