1717#include "ntlm.h"
1818#include "crypt.h"
1919
20+ bool ntlm_crypt_init (ntlm_client * ntlm )
21+ {
22+ const mbedtls_md_info_t * info = mbedtls_md_info_from_type (MBEDTLS_MD_MD5 );
23+
24+ mbedtls_md_init (& ntlm -> crypt_ctx .hmac );
25+
26+ if (mbedtls_md_setup (& ntlm -> crypt_ctx .hmac , info , 1 ) != 0 ) {
27+ ntlm_client_set_errmsg (ntlm , "could not setup mbedtls digest" );
28+ return false;
29+ }
30+
31+ return true;
32+ }
33+
34+
2035bool ntlm_random_bytes (
21- ntlm_client * ntlm ,
2236 unsigned char * out ,
37+ ntlm_client * ntlm ,
2338 size_t len )
2439{
2540 mbedtls_ctr_drbg_context ctr_drbg ;
@@ -51,6 +66,7 @@ bool ntlm_random_bytes(
5166
5267bool ntlm_des_encrypt (
5368 ntlm_des_block * out ,
69+ ntlm_client * ntlm ,
5470 ntlm_des_block * plaintext ,
5571 ntlm_des_block * key )
5672{
@@ -60,8 +76,10 @@ bool ntlm_des_encrypt(
6076 mbedtls_des_init (& ctx );
6177
6278 if (mbedtls_des_setkey_enc (& ctx , * key ) ||
63- mbedtls_des_crypt_ecb (& ctx , * plaintext , * out ))
79+ mbedtls_des_crypt_ecb (& ctx , * plaintext , * out )) {
80+ ntlm_client_set_errmsg (ntlm , "DES encryption failed" );
6481 goto done ;
82+ }
6583
6684 success = true;
6785
@@ -72,11 +90,14 @@ bool ntlm_des_encrypt(
7290
7391bool ntlm_md4_digest (
7492 unsigned char out [CRYPT_MD4_DIGESTSIZE ],
93+ ntlm_client * ntlm ,
7594 const unsigned char * in ,
7695 size_t in_len )
7796{
7897 mbedtls_md4_context ctx ;
7998
99+ NTLM_UNUSED (ntlm );
100+
80101 mbedtls_md4_init (& ctx );
81102 mbedtls_md4_starts (& ctx );
82103 mbedtls_md4_update (& ctx , in , in_len );
@@ -86,60 +107,40 @@ bool ntlm_md4_digest(
86107 return true;
87108}
88109
89- ntlm_hmac_ctx * ntlm_hmac_ctx_init (void )
90- {
91- ntlm_hmac_ctx * ctx ;
92- const mbedtls_md_info_t * info = mbedtls_md_info_from_type (MBEDTLS_MD_MD5 );
93-
94- if ((ctx = calloc (1 , sizeof (ntlm_hmac_ctx ))) == NULL )
95- return NULL ;
96-
97- mbedtls_md_init (& ctx -> mbed );
98-
99- if (mbedtls_md_setup (& ctx -> mbed , info , 1 ) != 0 ) {
100- free (ctx );
101- return false;
102- }
103-
104- return ctx ;
105- }
106-
107- bool ntlm_hmac_ctx_reset (ntlm_hmac_ctx * ctx )
108- {
109- return !mbedtls_md_hmac_reset (& ctx -> mbed );
110- }
111-
112110bool ntlm_hmac_md5_init (
113- ntlm_hmac_ctx * ctx ,
111+ ntlm_client * ntlm ,
114112 const unsigned char * key ,
115113 size_t key_len )
116114{
117- return !mbedtls_md_hmac_starts (& ctx -> mbed , key , key_len );
115+ if (ntlm -> crypt_ctx .hmac_initialized ) {
116+ if (mbedtls_md_hmac_reset (& ntlm -> crypt_ctx .hmac ))
117+ return false;
118+ }
119+
120+ ntlm -> crypt_ctx .hmac_initialized = !mbedtls_md_hmac_starts (& ntlm -> crypt_ctx .hmac , key , key_len );
121+ return ntlm -> crypt_ctx .hmac_initialized ;
118122}
119123
120124bool ntlm_hmac_md5_update (
121- ntlm_hmac_ctx * ctx ,
125+ ntlm_client * ntlm ,
122126 const unsigned char * in ,
123127 size_t in_len )
124128{
125- return !mbedtls_md_hmac_update (& ctx -> mbed , in , in_len );
129+ return !mbedtls_md_hmac_update (& ntlm -> crypt_ctx . hmac , in , in_len );
126130}
127131
128132bool ntlm_hmac_md5_final (
129133 unsigned char * out ,
130134 size_t * out_len ,
131- ntlm_hmac_ctx * ctx )
135+ ntlm_client * ntlm )
132136{
133137 if (* out_len < CRYPT_MD5_DIGESTSIZE )
134138 return false;
135139
136- return !mbedtls_md_hmac_finish (& ctx -> mbed , out );
140+ return !mbedtls_md_hmac_finish (& ntlm -> crypt_ctx . hmac , out );
137141}
138142
139- void ntlm_hmac_ctx_free ( ntlm_hmac_ctx * ctx )
143+ void ntlm_crypt_shutdown ( ntlm_client * ntlm )
140144{
141- if (ctx ) {
142- mbedtls_md_free (& ctx -> mbed );
143- free (ctx );
144- }
145+ mbedtls_md_free (& ntlm -> crypt_ctx .hmac );
145146}
0 commit comments