Fix compile on IA64 by noting that this should be the integer, not a pointer
[Samba.git] / source / libsmb / ntlmssp_sign.c
blob8f6bd0c6914010fb62d65ec3e93ea6d1c48f6da7
1 /*
2 * Unix SMB/CIFS implementation.
3 * Version 3.0
4 * NTLMSSP Signing routines
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-2001
6 * Copyright (C) Andrew Bartlett 2003
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "includes.h"
25 #define CLI_SIGN "session key to client-to-server signing key magic constant"
26 #define CLI_SEAL "session key to client-to-server sealing key magic constant"
27 #define SRV_SIGN "session key to server-to-client signing key magic constant"
28 #define SRV_SEAL "session key to server-to-client sealing key magic constant"
30 static void NTLMSSPcalc_ap( unsigned char *hash, unsigned char *data, int len)
32 unsigned char index_i = hash[256];
33 unsigned char index_j = hash[257];
34 int ind;
36 for (ind = 0; ind < len; ind++)
38 unsigned char tc;
39 unsigned char t;
41 index_i++;
42 index_j += hash[index_i];
44 tc = hash[index_i];
45 hash[index_i] = hash[index_j];
46 hash[index_j] = tc;
48 t = hash[index_i] + hash[index_j];
49 data[ind] = data[ind] ^ hash[t];
52 hash[256] = index_i;
53 hash[257] = index_j;
56 static void calc_hash(unsigned char *hash, const char *k2, int k2l)
58 unsigned char j = 0;
59 int ind;
61 for (ind = 0; ind < 256; ind++)
63 hash[ind] = (unsigned char)ind;
66 for (ind = 0; ind < 256; ind++)
68 unsigned char tc;
70 j += (hash[ind] + k2[ind%k2l]);
72 tc = hash[ind];
73 hash[ind] = hash[j];
74 hash[j] = tc;
77 hash[256] = 0;
78 hash[257] = 0;
81 static void calc_ntlmv2_hash(unsigned char hash[16], char digest[16],
82 const char encrypted_response[16],
83 const char *constant)
85 struct MD5Context ctx3;
87 MD5Init(&ctx3);
88 MD5Update(&ctx3, encrypted_response, 5);
89 MD5Update(&ctx3, constant, strlen(constant));
90 MD5Final(digest, &ctx3);
92 calc_hash(hash, digest, 16);
95 static NTSTATUS ntlmssp_make_packet_signiture(NTLMSSP_CLIENT_STATE *ntlmssp_state,
96 const uchar *data, size_t length,
97 DATA_BLOB *sig)
99 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
100 HMACMD5Context ctx;
101 char seq_num[4];
102 uchar digest[16];
103 SIVAL(seq_num, 0, ntlmssp_state->ntlmssp_seq_num);
105 hmac_md5_init_limK_to_64(ntlmssp_state->cli_sign_const, 16, &ctx);
106 hmac_md5_update(seq_num, 4, &ctx);
107 hmac_md5_update(data, length, &ctx);
108 hmac_md5_final(digest, &ctx);
110 if (!msrpc_gen(sig, "Bd", digest, sizeof(digest), ntlmssp_state->ntlmssp_seq_num)) {
111 return NT_STATUS_NO_MEMORY;
114 NTLMSSPcalc_ap(ntlmssp_state->cli_seal_hash, sig->data, sig->length);
115 } else {
116 uint32 crc;
117 crc = crc32_calc_buffer(data, length);
118 if (!msrpc_gen(sig, "ddd", 0, crc, ntlmssp_state->ntlmssp_seq_num)) {
119 return NT_STATUS_NO_MEMORY;
122 NTLMSSPcalc_ap(ntlmssp_state->ntlmssp_hash, sig->data, sig->length);
124 return NT_STATUS_OK;
127 NTSTATUS ntlmssp_client_sign_packet(NTLMSSP_CLIENT_STATE *ntlmssp_state,
128 const uchar *data, size_t length,
129 DATA_BLOB *sig)
131 ntlmssp_state->ntlmssp_seq_num++;
132 return ntlmssp_make_packet_signiture(ntlmssp_state, data, length, sig);
136 * Check the signature of an incoming packet
137 * @note caller *must* check that the signature is the size it expects
141 NTSTATUS ntlmssp_client_check_packet(NTLMSSP_CLIENT_STATE *ntlmssp_state,
142 const uchar *data, size_t length,
143 const DATA_BLOB *sig)
145 DATA_BLOB local_sig;
146 NTSTATUS nt_status;
148 if (sig->length < 8) {
149 DEBUG(0, ("NTLMSSP packet check failed due to short signiture (%u bytes)!\n",
150 sig->length));
153 nt_status = ntlmssp_make_packet_signiture(ntlmssp_state, data,
154 length, &local_sig);
156 if (!NT_STATUS_IS_OK(nt_status)) {
157 DEBUG(0, ("NTLMSSP packet check failed with %s\n", nt_errstr(nt_status)));
158 return nt_status;
161 if (memcmp(sig->data, local_sig.data, MIN(sig->length, local_sig.length)) == 0) {
162 return NT_STATUS_OK;
163 } else {
164 DEBUG(0, ("NTLMSSP packet check failed due to invalid signiture!\n"));
165 return NT_STATUS_ACCESS_DENIED;
170 Initialise the state for NTLMSSP signing.
172 NTSTATUS ntlmssp_client_sign_init(NTLMSSP_CLIENT_STATE *ntlmssp_state)
174 unsigned char p24[24];
175 unsigned char lm_hash[16];
177 if (!ntlmssp_state->lm_resp.data) {
178 /* can't sign or check signitures yet */
179 return NT_STATUS_UNSUCCESSFUL;
182 E_deshash(ntlmssp_state->password, lm_hash);
184 NTLMSSPOWFencrypt(lm_hash, ntlmssp_state->lm_resp.data, p24);
186 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
188 calc_ntlmv2_hash(ntlmssp_state->cli_sign_hash, ntlmssp_state->cli_sign_const, p24, CLI_SIGN);
189 calc_ntlmv2_hash(ntlmssp_state->cli_seal_hash, ntlmssp_state->cli_seal_const, p24, CLI_SEAL);
190 calc_ntlmv2_hash(ntlmssp_state->srv_sign_hash, ntlmssp_state->srv_sign_const, p24, SRV_SIGN);
191 calc_ntlmv2_hash(ntlmssp_state->srv_seal_hash, ntlmssp_state->srv_seal_const, p24, SRV_SEAL);
193 else
195 char k2[8];
196 memcpy(k2, p24, 5);
197 k2[5] = 0xe5;
198 k2[6] = 0x38;
199 k2[7] = 0xb0;
201 calc_hash(ntlmssp_state->ntlmssp_hash, k2, 8);
204 ntlmssp_state->ntlmssp_seq_num = 0;
206 ZERO_STRUCT(lm_hash);
207 return NT_STATUS_OK;