r13466: Make it easier to understand what this function actually does.
[samba.git] / source / libcli / auth / smbencrypt.c
index 3a4a724789f37c1dc60d734651ce4bef10195920..12c5d2dfb6e08994984640aed84210eb2a2cb8e8 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "includes.h"
 #include "system/time.h"
+#include "smb.h"
 #include "auth/ntlmssp/ntlmssp.h"
 #include "lib/crypto/crypto.h"
 #include "pstring.h"
@@ -269,13 +270,13 @@ void SMBsesskeygen_lm_sess_key(const uint8_t lm_hash[16],
        /* Calculate the LM session key (effective length 40 bits,
           but changes with each session) */
        uint8_t p24[24];
-       uint8_t p21[21];
+       uint8_t partial_lm_hash[14];
  
-       memset(p21,'\0',21);
-       memcpy(p21, lm_hash, 8);    
-       memset(p21 + 8, 0xbd, 8);
+       memcpy(partial_lm_hash, lm_hash, 8);    
+       memset(partial_lm_hash + 8, 0xbd, 6);
 
-       E_P24(p21, lm_resp, p24);
+       des_crypt56(p24,   lm_resp, partial_lm_hash,     1);
+       des_crypt56(p24+8, lm_resp, partial_lm_hash + 7, 1);
 
        memcpy(sess_key, p24, 16);
 
@@ -324,7 +325,8 @@ static DATA_BLOB NTLMv2_generate_client_data(TALLOC_CTX *mem_ctx, const DATA_BLO
        return response;
 }
 
-static DATA_BLOB NTLMv2_generate_response(const uint8_t ntlm_v2_hash[16],
+static DATA_BLOB NTLMv2_generate_response(TALLOC_CTX *out_mem_ctx, 
+                                         const uint8_t ntlm_v2_hash[16],
                                          const DATA_BLOB *server_chal,
                                          const DATA_BLOB *names_blob)
 {
@@ -332,7 +334,8 @@ static DATA_BLOB NTLMv2_generate_response(const uint8_t ntlm_v2_hash[16],
        DATA_BLOB ntlmv2_client_data;
        DATA_BLOB final_response;
        
-       TALLOC_CTX *mem_ctx = talloc_init("NTLMv2_generate_response internal context");
+       TALLOC_CTX *mem_ctx = talloc_named(out_mem_ctx, 0, 
+                                          "NTLMv2_generate_response internal context");
 
        if (!mem_ctx) {
                return data_blob(NULL, 0);
@@ -346,7 +349,7 @@ static DATA_BLOB NTLMv2_generate_response(const uint8_t ntlm_v2_hash[16],
        /* Given that data, and the challenge from the server, generate a response */
        SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
        
-       final_response = data_blob(NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
+       final_response = data_blob_talloc(out_mem_ctx, NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
 
        memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
 
@@ -358,12 +361,13 @@ static DATA_BLOB NTLMv2_generate_response(const uint8_t ntlm_v2_hash[16],
        return final_response;
 }
 
-static DATA_BLOB LMv2_generate_response(const uint8_t ntlm_v2_hash[16],
+static DATA_BLOB LMv2_generate_response(TALLOC_CTX *mem_ctx, 
+                                       const uint8_t ntlm_v2_hash[16],
                                        const DATA_BLOB *server_chal)
 {
        uint8_t lmv2_response[16];
-       DATA_BLOB lmv2_client_data = data_blob(NULL, 8);
-       DATA_BLOB final_response = data_blob(NULL, 24);
+       DATA_BLOB lmv2_client_data = data_blob_talloc(mem_ctx, NULL, 8);
+       DATA_BLOB final_response = data_blob_talloc(mem_ctx, NULL,24);
        
        /* LMv2 */
        /* client-supplied random data */
@@ -383,7 +387,8 @@ static DATA_BLOB LMv2_generate_response(const uint8_t ntlm_v2_hash[16],
        return final_response;
 }
 
-BOOL SMBNTLMv2encrypt_hash(const char *user, const char *domain, const uint8_t nt_hash[16],
+BOOL SMBNTLMv2encrypt_hash(TALLOC_CTX *mem_ctx, 
+                          const char *user, const char *domain, const uint8_t nt_hash[16],
                           const DATA_BLOB *server_chal, 
                           const DATA_BLOB *names_blob,
                           DATA_BLOB *lm_response, DATA_BLOB *nt_response, 
@@ -400,10 +405,11 @@ BOOL SMBNTLMv2encrypt_hash(const char *user, const char *domain, const uint8_t n
        }
        
        if (nt_response) {
-               *nt_response = NTLMv2_generate_response(ntlm_v2_hash, server_chal,
+               *nt_response = NTLMv2_generate_response(mem_ctx, 
+                                                       ntlm_v2_hash, server_chal,
                                                        names_blob); 
                if (user_session_key) {
-                       *user_session_key = data_blob(NULL, 16);
+                       *user_session_key = data_blob_talloc(mem_ctx, NULL, 16);
                        
                        /* The NTLMv2 calculations also provide a session key, for signing etc later */
                        /* use only the first 16 bytes of nt_response for session key */
@@ -414,9 +420,10 @@ BOOL SMBNTLMv2encrypt_hash(const char *user, const char *domain, const uint8_t n
        /* LMv2 */
        
        if (lm_response) {
-               *lm_response = LMv2_generate_response(ntlm_v2_hash, server_chal);
+               *lm_response = LMv2_generate_response(mem_ctx, 
+                                                     ntlm_v2_hash, server_chal);
                if (lm_session_key) {
-                       *lm_session_key = data_blob(NULL, 16);
+                       *lm_session_key = data_blob_talloc(mem_ctx, NULL, 16);
                        
                        /* The NTLMv2 calculations also provide a session key, for signing etc later */
                        /* use only the first 16 bytes of lm_response for session key */
@@ -427,7 +434,9 @@ BOOL SMBNTLMv2encrypt_hash(const char *user, const char *domain, const uint8_t n
        return True;
 }
 
-BOOL SMBNTLMv2encrypt(const char *user, const char *domain, const char *password, 
+BOOL SMBNTLMv2encrypt(TALLOC_CTX *mem_ctx, 
+                     const char *user, const char *domain, 
+                     const char *password, 
                      const DATA_BLOB *server_chal, 
                      const DATA_BLOB *names_blob,
                      DATA_BLOB *lm_response, DATA_BLOB *nt_response, 
@@ -436,7 +445,8 @@ BOOL SMBNTLMv2encrypt(const char *user, const char *domain, const char *password
        uint8_t nt_hash[16];
        E_md4hash(password, nt_hash);
 
-       return SMBNTLMv2encrypt_hash(user, domain, nt_hash, server_chal, names_blob,
+       return SMBNTLMv2encrypt_hash(mem_ctx, 
+                                    user, domain, nt_hash, server_chal, names_blob,
                                     lm_response, nt_response, lm_session_key, user_session_key);
 }