comparison src/lib-ntlm/ntlm-encrypt.c @ 15172:8802322d7257

lib: Generalize hmac to be hash independent
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 30 Aug 2012 00:43:56 +0200
parents d0d7b810646b
children
comparison
equal deleted inserted replaced
15171:fc512eba5207 15172:8802322d7257
9 #include "lib.h" 9 #include "lib.h"
10 #include "buffer.h" 10 #include "buffer.h"
11 #include "compat.h" 11 #include "compat.h"
12 #include "safe-memset.h" 12 #include "safe-memset.h"
13 #include "md4.h" 13 #include "md4.h"
14 #include "hmac-md5.h" 14 #include "md5.h"
15 #include "hmac.h"
15 #include "ntlm.h" 16 #include "ntlm.h"
16 #include "ntlm-des.h" 17 #include "ntlm-des.h"
17 18
18 #include <ctype.h> 19 #include <ctype.h>
19 20
58 59
59 safe_memset(wpwd, 0, len); 60 safe_memset(wpwd, 0, len);
60 } 61 }
61 62
62 static void 63 static void
63 hmac_md5_ucs2le_string_ucase(struct hmac_md5_context *ctx, const char *str) 64 hmac_md5_ucs2le_string_ucase(struct hmac_context *ctx, const char *str)
64 { 65 {
65 size_t len; 66 size_t len;
66 unsigned char *wstr = t_unicode_str(str, 1, &len); 67 unsigned char *wstr = t_unicode_str(str, 1, &len);
67 68
68 hmac_md5_update(ctx, wstr, len); 69 hmac_update(ctx, wstr, len);
69 } 70 }
70 71
71 static void ATTR_NULL(2) 72 static void ATTR_NULL(2)
72 ntlm_v2_hash(const char *user, const char *target, 73 ntlm_v2_hash(const char *user, const char *target,
73 const unsigned char *hash_v1, 74 const unsigned char *hash_v1,
74 unsigned char hash[NTLMSSP_V2_HASH_SIZE]) 75 unsigned char hash[NTLMSSP_V2_HASH_SIZE])
75 { 76 {
76 struct hmac_md5_context ctx; 77 struct hmac_context ctx;
77 78
78 hmac_md5_init(&ctx, hash_v1, NTLMSSP_HASH_SIZE); 79 hmac_init(&ctx, hash_v1, NTLMSSP_HASH_SIZE, &hash_method_md5);
79 hmac_md5_ucs2le_string_ucase(&ctx, user); 80 hmac_md5_ucs2le_string_ucase(&ctx, user);
80 if (target != NULL) 81 if (target != NULL)
81 hmac_md5_ucs2le_string_ucase(&ctx, target); 82 hmac_md5_ucs2le_string_ucase(&ctx, target);
82 hmac_md5_final(&ctx, hash); 83 hmac_final(&ctx, hash);
83 } 84 }
84 85
85 void 86 void
86 ntlmssp_v1_response(const unsigned char *hash, 87 ntlmssp_v1_response(const unsigned char *hash,
87 const unsigned char *challenge, 88 const unsigned char *challenge,
122 const unsigned char *hash_v1, 123 const unsigned char *hash_v1,
123 const unsigned char *challenge, 124 const unsigned char *challenge,
124 const unsigned char *blob, size_t blob_size, 125 const unsigned char *blob, size_t blob_size,
125 unsigned char response[NTLMSSP_V2_RESPONSE_SIZE]) 126 unsigned char response[NTLMSSP_V2_RESPONSE_SIZE])
126 { 127 {
127 struct hmac_md5_context ctx; 128 struct hmac_context ctx;
128 unsigned char hash[NTLMSSP_V2_HASH_SIZE]; 129 unsigned char hash[NTLMSSP_V2_HASH_SIZE];
129 130
130 ntlm_v2_hash(user, target, hash_v1, hash); 131 ntlm_v2_hash(user, target, hash_v1, hash);
131 132
132 hmac_md5_init(&ctx, hash, NTLMSSP_V2_HASH_SIZE); 133 hmac_init(&ctx, hash, NTLMSSP_V2_HASH_SIZE, &hash_method_md5);
133 hmac_md5_update(&ctx, challenge, NTLMSSP_CHALLENGE_SIZE); 134 hmac_update(&ctx, challenge, NTLMSSP_CHALLENGE_SIZE);
134 hmac_md5_update(&ctx, blob, blob_size); 135 hmac_update(&ctx, blob, blob_size);
135 hmac_md5_final(&ctx, response); 136 hmac_final(&ctx, response);
136 137
137 safe_memset(hash, 0, sizeof(hash)); 138 safe_memset(hash, 0, sizeof(hash));
138 } 139 }