2 Unix SMB/CIFS implementation.
4 code to encrypt/decrypt data using the user session key
6 Copyright (C) Andrew Tridgell 2004
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
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/auth/libcli_auth.h"
27 encrypt or decrypt a blob of data using the user session key
28 as used in lsa_SetSecret
30 before calling, the out blob must be initialised to be the same size
33 void sess_crypt_blob(DATA_BLOB
*out
, const DATA_BLOB
*in
, const DATA_BLOB
*session_key
,
41 uint8_t bin
[8], bout
[8], key
[7];
44 memcpy(bin
, &in
->data
[i
], MIN(8, in
->length
-i
));
46 if (k
+ 7 > session_key
->length
) {
47 k
= (session_key
->length
- k
);
49 memcpy(key
, &session_key
->data
[k
], 7);
51 des_crypt56(bout
, bin
, key
, forward
?1:0);
53 memcpy(&out
->data
[i
], bout
, MIN(8, in
->length
-i
));
59 a convenient wrapper around sess_crypt_blob() for strings, using the LSA convention
61 note that we round the length to a multiple of 8. This seems to be needed for
62 compatibility with windows
64 caller should free using data_blob_free()
66 DATA_BLOB
sess_encrypt_string(const char *str
, const DATA_BLOB
*session_key
)
69 int slen
= strlen(str
);
70 int dlen
= (slen
+7) & ~7;
72 src
= data_blob(NULL
, 8+dlen
);
74 return data_blob(NULL
, 0);
77 ret
= data_blob(NULL
, 8+dlen
);
80 return data_blob(NULL
, 0);
83 SIVAL(src
.data
, 0, slen
);
84 SIVAL(src
.data
, 4, 1);
85 memset(src
.data
+8, 0, dlen
);
86 memcpy(src
.data
+8, str
, slen
);
88 sess_crypt_blob(&ret
, &src
, session_key
, True
);
96 a convenient wrapper around sess_crypt_blob() for strings, using the LSA convention
98 caller should free the returned string
100 char *sess_decrypt_string(DATA_BLOB
*blob
, const DATA_BLOB
*session_key
)
106 if (blob
->length
< 8) {
110 out
= data_blob(NULL
, blob
->length
);
115 sess_crypt_blob(&out
, blob
, session_key
, False
);
117 if (IVAL(out
.data
, 4) != 1) {
118 DEBUG(0,("Unexpected revision number %d in session crypted string\n",
123 slen
= IVAL(out
.data
, 0);
124 if (slen
> blob
->length
- 8) {
125 DEBUG(0,("Invalid crypt length %d\n", slen
));
129 ret
= strndup((const char *)(out
.data
+8), slen
);
131 data_blob_free(&out
);
137 a convenient wrapper around sess_crypt_blob() for DATA_BLOBs, using the LSA convention
139 note that we round the length to a multiple of 8. This seems to be needed for
140 compatibility with windows
142 caller should free using data_blob_free()
144 DATA_BLOB
sess_encrypt_blob(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob_in
, const DATA_BLOB
*session_key
)
147 int dlen
= (blob_in
->length
+7) & ~7;
149 src
= data_blob_talloc(mem_ctx
, NULL
, 8+dlen
);
151 return data_blob(NULL
, 0);
154 ret
= data_blob_talloc(mem_ctx
, NULL
, 8+dlen
);
156 data_blob_free(&src
);
157 return data_blob(NULL
, 0);
160 SIVAL(src
.data
, 0, blob_in
->length
);
161 SIVAL(src
.data
, 4, 1);
162 memset(src
.data
+8, 0, dlen
);
163 memcpy(src
.data
+8, blob_in
->data
, blob_in
->length
);
165 sess_crypt_blob(&ret
, &src
, session_key
, True
);
167 data_blob_free(&src
);
173 Decrypt a DATA_BLOB using the LSA convention
175 NTSTATUS
sess_decrypt_blob(TALLOC_CTX
*mem_ctx
, const DATA_BLOB
*blob
, const DATA_BLOB
*session_key
,
181 if (blob
->length
< 8) {
182 DEBUG(0, ("Unexpected length %d in session crypted secret (BLOB)\n",
184 return NT_STATUS_INVALID_PARAMETER
;
187 out
= data_blob_talloc(mem_ctx
, NULL
, blob
->length
);
189 return NT_STATUS_NO_MEMORY
;
192 sess_crypt_blob(&out
, blob
, session_key
, False
);
194 if (IVAL(out
.data
, 4) != 1) {
195 DEBUG(2,("Unexpected revision number %d in session crypted secret (BLOB)\n",
197 return NT_STATUS_UNKNOWN_REVISION
;
200 slen
= IVAL(out
.data
, 0);
201 if (slen
> blob
->length
- 8) {
202 DEBUG(0,("Invalid crypt length %d in session crypted secret (BLOB)\n", slen
));
203 return NT_STATUS_WRONG_PASSWORD
;
206 *ret
= data_blob_talloc(mem_ctx
, out
.data
+8, slen
);
207 if (slen
&& !ret
->data
) {
208 return NT_STATUS_NO_MEMORY
;
211 data_blob_free(&out
);