1 /*
2  * Copyright (c) Edward Thomson.  All rights reserved.
3  *
4  * This file is part of ntlmclient, distributed under the MIT license.
5  * For full terms and copyright information, and for third-party
6  * copyright information, see the included LICENSE.txt file.
7  */
8 
9 #ifndef PRIVATE_CRYPT_OPENSSL_H__
10 #define PRIVATE_CRYPT_OPENSSL_H__
11 
12 #ifndef CRYPT_OPENSSL_DYNAMIC
13 # include <openssl/des.h>
14 # include <openssl/hmac.h>
15 #endif
16 
17 /* OpenSSL 1.1.0 uses opaque structs, we'll reuse these. */
18 #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L
19 # define HMAC_CTX struct hmac_ctx_st
20 #endif
21 
22 #ifdef CRYPT_OPENSSL_DYNAMIC
23 typedef unsigned char DES_cblock[8];
24 typedef unsigned char const_DES_cblock[8];
25 
26 typedef unsigned long DES_LONG;
27 
28 typedef struct DES_ks {
29     union {
30         DES_cblock cblock;
31         DES_LONG deslong[2];
32     } ks[16];
33 } DES_key_schedule;
34 
35 #define DES_ENCRYPT 1
36 
37 typedef void EVP_MD;
38 typedef void ENGINE;
39 typedef void EVP_PKEY_CTX;
40 
41 #define HMAC_MAX_MD_CBLOCK 128
42 
43 typedef struct env_md_ctx_st EVP_MD_CTX;
44 struct env_md_ctx_st {
45     const EVP_MD *digest;
46     ENGINE *engine;
47     unsigned long flags;
48     void *md_data;
49     EVP_PKEY_CTX *pctx;
50     int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
51 };
52 
53 typedef struct hmac_ctx_st {
54     const EVP_MD *md;
55     EVP_MD_CTX md_ctx;
56     EVP_MD_CTX i_ctx;
57     EVP_MD_CTX o_ctx;
58     unsigned int key_length;
59     unsigned char key[HMAC_MAX_MD_CBLOCK];
60 } HMAC_CTX;
61 #endif
62 
63 struct ntlm_crypt_ctx {
64 	HMAC_CTX *hmac;
65 
66 	void *openssl_handle;
67 
68 	void (*des_ecb_encrypt_fn)(const_DES_cblock *input, DES_cblock *output, DES_key_schedule *ks, int enc);
69 	int (*des_set_key_fn)(const_DES_cblock *key, DES_key_schedule *schedule);
70 
71 	unsigned long (*err_get_error_fn)(void);
72 	const char *(*err_lib_error_string_fn)(unsigned long e);
73 
74 	const EVP_MD *(*evp_md5_fn)(void);
75 
76 	HMAC_CTX *(*hmac_ctx_new_fn)(void);
77 	int (*hmac_ctx_reset_fn)(HMAC_CTX *ctx);
78 	void (*hmac_ctx_free_fn)(HMAC_CTX *ctx);
79 	void (*hmac_ctx_cleanup_fn)(HMAC_CTX *ctx);
80 
81 	int (*hmac_init_ex_fn)(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md, ENGINE *impl);
82 	int (*hmac_update_fn)(HMAC_CTX *ctx, const unsigned char *data, size_t len);
83 	int (*hmac_final_fn)(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);
84 
85 	unsigned char *(*md4_fn)(const unsigned char *d, size_t n, unsigned char *md);
86 
87 	int (*rand_bytes_fn)(unsigned char *buf, int num);
88 };
89 
90 #endif /* PRIVATE_CRYPT_OPENSSL_H__ */
91