unsigned char *p24);
        
 static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
-                                   const char *key, char *signature)
+                                   const struct mac_key *key, char *signature)
 {
        struct  MD5Context context;
 
-       if ((cifs_pdu == NULL) || (signature == NULL))
+       if ((cifs_pdu == NULL) || (signature == NULL) || (key == NULL))
                return -EINVAL;
 
        MD5Init(&context);
-       MD5Update(&context, key, CIFS_SESS_KEY_SIZE+16);
+       MD5Update(&context, (char *)&key->data, key->len);
        MD5Update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
+
        MD5Final(signature, &context);
        return 0;
 }
        server->sequence_number++;
        spin_unlock(&GlobalMid_Lock);
 
-       rc = cifs_calculate_signature(cifs_pdu, server->mac_signing_key,
+       rc = cifs_calculate_signature(cifs_pdu, &server->mac_signing_key,
                                      smb_signature);
        if (rc)
                memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
 }
 
 static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
-                               const char *key, char *signature)
+                               const struct mac_key *key, char *signature)
 {
        struct  MD5Context context;
        int i;
 
-       if ((iov == NULL) || (signature == NULL))
+       if ((iov == NULL) || (signature == NULL) || (key == NULL))
                return -EINVAL;
 
        MD5Init(&context);
-       MD5Update(&context, key, CIFS_SESS_KEY_SIZE+16);
+       MD5Update(&context, (char *)&key->data, key->len);
        for (i=0;i<n_vec;i++) {
                if (iov[i].iov_base == NULL) {
                        cERROR(1 ,("null iovec entry"));
        server->sequence_number++;
        spin_unlock(&GlobalMid_Lock);
 
-       rc = cifs_calc_signature2(iov, n_vec, server->mac_signing_key,
+       rc = cifs_calc_signature2(iov, n_vec, &server->mac_signing_key,
                                      smb_signature);
        if (rc)
                memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
        return rc;
 }
 
-int cifs_verify_signature(struct smb_hdr *cifs_pdu, const char *mac_key,
+int cifs_verify_signature(struct smb_hdr *cifs_pdu,
+                         const struct mac_key *mac_key,
                          __u32 expected_sequence_number)
 {
        unsigned int rc;
 }
 
 /* We fill in key by putting in 40 byte array which was allocated by caller */
-int cifs_calculate_mac_key(char * key, const char * rn, const char * password)
+int cifs_calculate_mac_key(struct mac_key *key, const char *rn,
+                          const char *password)
 {
        char temp_key[16];
        if ((key == NULL) || (rn == NULL))
                return -EINVAL;
 
        E_md4hash(password, temp_key);
-       mdfour(key,temp_key,16);
-       memcpy(key+16,rn, CIFS_SESS_KEY_SIZE);
+       mdfour(key->data.ntlm, temp_key, 16);
+       memcpy(key->data.ntlm+16, rn, CIFS_SESS_KEY_SIZE);
+       key->len = 40;
        return 0;
 }
 
        if(ses->domainName == NULL)
                return -EINVAL; /* BB should we use CIFS_LINUX_DOM */
        dom_name_len = strlen(ses->domainName);
-       if(dom_name_len > MAX_USERNAME_SIZE)
+       if (dom_name_len > MAX_USERNAME_SIZE)
                return -EINVAL;
   
        ucase_buf = kmalloc((MAX_USERNAME_SIZE+1), GFP_KERNEL);
-       if(ucase_buf == NULL)
+       if (ucase_buf == NULL)
                return -ENOMEM;
        unicode_buf = kmalloc((MAX_USERNAME_SIZE+1)*4, GFP_KERNEL);
-       if(unicode_buf == NULL) {
+       if (unicode_buf == NULL) {
                kfree(ucase_buf);
                return -ENOMEM;
        }
    
-       for(i=0;i<user_name_len;i++)
+       for (i = 0;i < user_name_len; i++)
                ucase_buf[i] = nls_info->charset2upper[(int)ses->userName[i]];
        ucase_buf[i] = 0;
        user_name_len = cifs_strtoUCS(unicode_buf, ucase_buf, MAX_USERNAME_SIZE*2, nls_info);
        unicode_buf[user_name_len] = 0;
        user_name_len++;
 
-       for(i=0;i<dom_name_len;i++)
+       for (i = 0; i < dom_name_len; i++)
                ucase_buf[i] = nls_info->charset2upper[(int)ses->domainName[i]];
        ucase_buf[i] = 0;
        dom_name_len = cifs_strtoUCS(unicode_buf+user_name_len, ucase_buf, MAX_USERNAME_SIZE*2, nls_info);
 
        unicode_buf[user_name_len + dom_name_len] = 0;
        hmac_md5_update((const unsigned char *) unicode_buf,
-               (user_name_len+dom_name_len)*2,&ctx);
+               (user_name_len+dom_name_len)*2, &ctx);
 
-       hmac_md5_final(ses->server->mac_signing_key,&ctx);
+       hmac_md5_final(ses->server->ntlmv2_hash, &ctx);
        kfree(ucase_buf);
        kfree(unicode_buf);
        return 0;
                if(domain == NULL)
                        goto calc_exit_1;
                len = cifs_strtoUCS(domain, ses->domainName, len, nls_cp);
-               UniStrupr(domain);
+               /* the following line was removed since it didn't work well
+                  with lower cased domain name that passed as an option.
+                  Maybe converting the domain name earlier makes sense */
+               /* UniStrupr(domain); */
 
                hmac_md5_update((char *)domain, 2*len, pctxt);
        
 calc_exit_2:
        /* BB FIXME what about bytes 24 through 40 of the signing key? 
           compare with the NTLM example */
-       hmac_md5_final(ses->server->mac_signing_key, pctxt);
+       hmac_md5_final(ses->server->ntlmv2_hash, pctxt);
 
        return rc;
 }
 {
        int rc;
        struct ntlmv2_resp * buf = (struct ntlmv2_resp *)resp_buf;
+       struct HMACMD5Context context;
 
        buf->blob_signature = cpu_to_le32(0x00000101);
        buf->reserved = 0;
        if(rc)
                cERROR(1,("could not get v2 hash rc %d",rc));
        CalcNTLMv2_response(ses, resp_buf);
+
+       /* now calculate the MAC key for NTLMv2 */
+       hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context);
+       hmac_md5_update(resp_buf, 16, &context);
+       hmac_md5_final(ses->server->mac_signing_key.data.ntlmv2.key, &context);
+
+       memcpy(&ses->server->mac_signing_key.data.ntlmv2.resp, resp_buf,
+              sizeof(struct ntlmv2_resp));
+       ses->server->mac_signing_key.len = 16 + sizeof(struct ntlmv2_resp);
 }
 
 void CalcNTLMv2_response(const struct cifsSesInfo * ses, char * v2_session_response)
        struct HMACMD5Context context;
        /* rest of v2 struct already generated */
        memcpy(v2_session_response + 8, ses->server->cryptKey,8);
-       hmac_md5_init_limK_to_64(ses->server->mac_signing_key, 16, &context);
+       hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context);
 
-       hmac_md5_update(v2_session_response+8, 
+       hmac_md5_update(v2_session_response+8,
                        sizeof(struct ntlmv2_resp) - 8, &context);
 
        hmac_md5_final(v2_session_response,&context);