ms_funcs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * WPA Supplicant / shared MSCHAPV2 helper functions / RFC 2433 / RFC 2759
  3. * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "sha1.h"
  17. #include "ms_funcs.h"
  18. #include "crypto.h"
  19. #include "rc4.h"
  20. /**
  21. * challenge_hash - ChallengeHash() - RFC 2759, Sect. 8.2
  22. * @peer_challenge: 16-octet PeerChallenge (IN)
  23. * @auth_challenge: 16-octet AuthenticatorChallenge (IN)
  24. * @username: 0-to-256-char UserName (IN)
  25. * @username_len: Length of username
  26. * @challenge: 8-octet Challenge (OUT)
  27. */
  28. static void challenge_hash(const u8 *peer_challenge, const u8 *auth_challenge,
  29. const u8 *username, size_t username_len,
  30. u8 *challenge)
  31. {
  32. u8 hash[SHA1_MAC_LEN];
  33. const unsigned char *addr[3];
  34. size_t len[3];
  35. addr[0] = peer_challenge;
  36. len[0] = 16;
  37. addr[1] = auth_challenge;
  38. len[1] = 16;
  39. addr[2] = username;
  40. len[2] = username_len;
  41. sha1_vector(3, addr, len, hash);
  42. os_memcpy(challenge, hash, 8);
  43. }
  44. /**
  45. * nt_password_hash - NtPasswordHash() - RFC 2759, Sect. 8.3
  46. * @password: 0-to-256-unicode-char Password (IN; ASCII)
  47. * @password_len: Length of password
  48. * @password_hash: 16-octet PasswordHash (OUT)
  49. */
  50. void nt_password_hash(const u8 *password, size_t password_len,
  51. u8 *password_hash)
  52. {
  53. u8 buf[512], *pos;
  54. size_t i, len;
  55. if (password_len > 256)
  56. password_len = 256;
  57. /* Convert password into unicode */
  58. for (i = 0; i < password_len; i++) {
  59. buf[2 * i] = password[i];
  60. buf[2 * i + 1] = 0;
  61. }
  62. len = password_len * 2;
  63. pos = buf;
  64. md4_vector(1, (const u8 **) &pos, &len, password_hash);
  65. }
  66. /**
  67. * hash_nt_password_hash - HashNtPasswordHash() - RFC 2759, Sect. 8.4
  68. * @password_hash: 16-octet PasswordHash (IN)
  69. * @password_hash_hash: 16-octet PasswordHashHash (OUT)
  70. */
  71. void hash_nt_password_hash(const u8 *password_hash, u8 *password_hash_hash)
  72. {
  73. size_t len = 16;
  74. md4_vector(1, &password_hash, &len, password_hash_hash);
  75. }
  76. /**
  77. * challenge_response - ChallengeResponse() - RFC 2759, Sect. 8.5
  78. * @challenge: 8-octet Challenge (IN)
  79. * @password_hash: 16-octet PasswordHash (IN)
  80. * @response: 24-octet Response (OUT)
  81. */
  82. void challenge_response(const u8 *challenge, const u8 *password_hash,
  83. u8 *response)
  84. {
  85. u8 zpwd[7];
  86. des_encrypt(challenge, password_hash, response);
  87. des_encrypt(challenge, password_hash + 7, response + 8);
  88. zpwd[0] = password_hash[14];
  89. zpwd[1] = password_hash[15];
  90. os_memset(zpwd + 2, 0, 5);
  91. des_encrypt(challenge, zpwd, response + 16);
  92. }
  93. /**
  94. * generate_nt_response - GenerateNTResponse() - RFC 2759, Sect. 8.1
  95. * @auth_challenge: 16-octet AuthenticatorChallenge (IN)
  96. * @peer_challenge: 16-octet PeerChallenge (IN)
  97. * @username: 0-to-256-char UserName (IN)
  98. * @username_len: Length of username
  99. * @password: 0-to-256-unicode-char Password (IN; ASCII)
  100. * @password_len: Length of password
  101. * @response: 24-octet Response (OUT)
  102. */
  103. void generate_nt_response(const u8 *auth_challenge, const u8 *peer_challenge,
  104. const u8 *username, size_t username_len,
  105. const u8 *password, size_t password_len,
  106. u8 *response)
  107. {
  108. u8 challenge[8];
  109. u8 password_hash[16];
  110. challenge_hash(peer_challenge, auth_challenge, username, username_len,
  111. challenge);
  112. nt_password_hash(password, password_len, password_hash);
  113. challenge_response(challenge, password_hash, response);
  114. }
  115. /**
  116. * generate_nt_response_pwhash - GenerateNTResponse() - RFC 2759, Sect. 8.1
  117. * @auth_challenge: 16-octet AuthenticatorChallenge (IN)
  118. * @peer_challenge: 16-octet PeerChallenge (IN)
  119. * @username: 0-to-256-char UserName (IN)
  120. * @username_len: Length of username
  121. * @password_hash: 16-octet PasswordHash (IN)
  122. * @response: 24-octet Response (OUT)
  123. */
  124. void generate_nt_response_pwhash(const u8 *auth_challenge,
  125. const u8 *peer_challenge,
  126. const u8 *username, size_t username_len,
  127. const u8 *password_hash,
  128. u8 *response)
  129. {
  130. u8 challenge[8];
  131. challenge_hash(peer_challenge, auth_challenge, username, username_len,
  132. challenge);
  133. challenge_response(challenge, password_hash, response);
  134. }
  135. /**
  136. * generate_authenticator_response_pwhash - GenerateAuthenticatorResponse() - RFC 2759, Sect. 8.7
  137. * @password_hash: 16-octet PasswordHash (IN)
  138. * @nt_response: 24-octet NT-Response (IN)
  139. * @peer_challenge: 16-octet PeerChallenge (IN)
  140. * @auth_challenge: 16-octet AuthenticatorChallenge (IN)
  141. * @username: 0-to-256-char UserName (IN)
  142. * @username_len: Length of username
  143. * @response: 20-octet AuthenticatorResponse (OUT) (note: this value is usually
  144. * encoded as a 42-octet ASCII string (S=<hexdump of response>)
  145. */
  146. void generate_authenticator_response_pwhash(
  147. const u8 *password_hash,
  148. const u8 *peer_challenge, const u8 *auth_challenge,
  149. const u8 *username, size_t username_len,
  150. const u8 *nt_response, u8 *response)
  151. {
  152. static const u8 magic1[39] = {
  153. 0x4D, 0x61, 0x67, 0x69, 0x63, 0x20, 0x73, 0x65, 0x72, 0x76,
  154. 0x65, 0x72, 0x20, 0x74, 0x6F, 0x20, 0x63, 0x6C, 0x69, 0x65,
  155. 0x6E, 0x74, 0x20, 0x73, 0x69, 0x67, 0x6E, 0x69, 0x6E, 0x67,
  156. 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x74
  157. };
  158. static const u8 magic2[41] = {
  159. 0x50, 0x61, 0x64, 0x20, 0x74, 0x6F, 0x20, 0x6D, 0x61, 0x6B,
  160. 0x65, 0x20, 0x69, 0x74, 0x20, 0x64, 0x6F, 0x20, 0x6D, 0x6F,
  161. 0x72, 0x65, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x6F, 0x6E,
  162. 0x65, 0x20, 0x69, 0x74, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F,
  163. 0x6E
  164. };
  165. u8 password_hash_hash[16], challenge[8];
  166. const unsigned char *addr1[3];
  167. const size_t len1[3] = { 16, 24, sizeof(magic1) };
  168. const unsigned char *addr2[3];
  169. const size_t len2[3] = { SHA1_MAC_LEN, 8, sizeof(magic2) };
  170. addr1[0] = password_hash_hash;
  171. addr1[1] = nt_response;
  172. addr1[2] = magic1;
  173. addr2[0] = response;
  174. addr2[1] = challenge;
  175. addr2[2] = magic2;
  176. hash_nt_password_hash(password_hash, password_hash_hash);
  177. sha1_vector(3, addr1, len1, response);
  178. challenge_hash(peer_challenge, auth_challenge, username, username_len,
  179. challenge);
  180. sha1_vector(3, addr2, len2, response);
  181. }
  182. /**
  183. * generate_authenticator_response - GenerateAuthenticatorResponse() - RFC 2759, Sect. 8.7
  184. * @password: 0-to-256-unicode-char Password (IN; ASCII)
  185. * @password_len: Length of password
  186. * @nt_response: 24-octet NT-Response (IN)
  187. * @peer_challenge: 16-octet PeerChallenge (IN)
  188. * @auth_challenge: 16-octet AuthenticatorChallenge (IN)
  189. * @username: 0-to-256-char UserName (IN)
  190. * @username_len: Length of username
  191. * @response: 20-octet AuthenticatorResponse (OUT) (note: this value is usually
  192. * encoded as a 42-octet ASCII string (S=<hexdump of response>)
  193. */
  194. void generate_authenticator_response(const u8 *password, size_t password_len,
  195. const u8 *peer_challenge,
  196. const u8 *auth_challenge,
  197. const u8 *username, size_t username_len,
  198. const u8 *nt_response, u8 *response)
  199. {
  200. u8 password_hash[16];
  201. nt_password_hash(password, password_len, password_hash);
  202. generate_authenticator_response_pwhash(password_hash,
  203. peer_challenge, auth_challenge,
  204. username, username_len,
  205. nt_response, response);
  206. }
  207. /**
  208. * nt_challenge_response - NtChallengeResponse() - RFC 2433, Sect. A.5
  209. * @challenge: 8-octet Challenge (IN)
  210. * @password: 0-to-256-unicode-char Password (IN; ASCII)
  211. * @password_len: Length of password
  212. * @response: 24-octet Response (OUT)
  213. */
  214. void nt_challenge_response(const u8 *challenge, const u8 *password,
  215. size_t password_len, u8 *response)
  216. {
  217. u8 password_hash[16];
  218. nt_password_hash(password, password_len, password_hash);
  219. challenge_response(challenge, password_hash, response);
  220. }
  221. /**
  222. * get_master_key - GetMasterKey() - RFC 3079, Sect. 3.4
  223. * @password_hash_hash: 16-octet PasswordHashHash (IN)
  224. * @nt_response: 24-octet NTResponse (IN)
  225. * @master_key: 16-octet MasterKey (OUT)
  226. */
  227. void get_master_key(const u8 *password_hash_hash, const u8 *nt_response,
  228. u8 *master_key)
  229. {
  230. static const u8 magic1[27] = {
  231. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
  232. 0x68, 0x65, 0x20, 0x4d, 0x50, 0x50, 0x45, 0x20, 0x4d,
  233. 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x4b, 0x65, 0x79
  234. };
  235. const unsigned char *addr[3];
  236. const size_t len[3] = { 16, 24, sizeof(magic1) };
  237. u8 hash[SHA1_MAC_LEN];
  238. addr[0] = password_hash_hash;
  239. addr[1] = nt_response;
  240. addr[2] = magic1;
  241. sha1_vector(3, addr, len, hash);
  242. os_memcpy(master_key, hash, 16);
  243. }
  244. /**
  245. * get_asymetric_start_key - GetAsymetricStartKey() - RFC 3079, Sect. 3.4
  246. * @master_key: 16-octet MasterKey (IN)
  247. * @session_key: 8-to-16 octet SessionKey (OUT)
  248. * @session_key_len: SessionKeyLength (Length of session_key) (IN)
  249. * @is_send: IsSend (IN, BOOLEAN)
  250. * @is_server: IsServer (IN, BOOLEAN)
  251. */
  252. void get_asymetric_start_key(const u8 *master_key, u8 *session_key,
  253. size_t session_key_len, int is_send,
  254. int is_server)
  255. {
  256. static const u8 magic2[84] = {
  257. 0x4f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x69,
  258. 0x65, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x64, 0x65, 0x2c, 0x20,
  259. 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
  260. 0x65, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x20, 0x6b, 0x65, 0x79,
  261. 0x3b, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73,
  262. 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73, 0x69, 0x64, 0x65,
  263. 0x2c, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
  264. 0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,
  265. 0x6b, 0x65, 0x79, 0x2e
  266. };
  267. static const u8 magic3[84] = {
  268. 0x4f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x69,
  269. 0x65, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x64, 0x65, 0x2c, 0x20,
  270. 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
  271. 0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,
  272. 0x6b, 0x65, 0x79, 0x3b, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68,
  273. 0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73,
  274. 0x69, 0x64, 0x65, 0x2c, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73,
  275. 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x20,
  276. 0x6b, 0x65, 0x79, 0x2e
  277. };
  278. static const u8 shs_pad1[40] = {
  279. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  280. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  281. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  282. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  283. };
  284. static const u8 shs_pad2[40] = {
  285. 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
  286. 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
  287. 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
  288. 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2
  289. };
  290. u8 digest[SHA1_MAC_LEN];
  291. const unsigned char *addr[4];
  292. const size_t len[4] = { 16, 40, 84, 40 };
  293. addr[0] = master_key;
  294. addr[1] = shs_pad1;
  295. if (is_send) {
  296. addr[2] = is_server ? magic3 : magic2;
  297. } else {
  298. addr[2] = is_server ? magic2 : magic3;
  299. }
  300. addr[3] = shs_pad2;
  301. sha1_vector(4, addr, len, digest);
  302. if (session_key_len > SHA1_MAC_LEN)
  303. session_key_len = SHA1_MAC_LEN;
  304. os_memcpy(session_key, digest, session_key_len);
  305. }
  306. #define PWBLOCK_LEN 516
  307. /**
  308. * encrypt_pw_block_with_password_hash - EncryptPwBlockWithPasswordHash() - RFC 2759, Sect. 8.10
  309. * @password: 0-to-256-unicode-char Password (IN; ASCII)
  310. * @password_len: Length of password
  311. * @password_hash: 16-octet PasswordHash (IN)
  312. * @pw_block: 516-byte PwBlock (OUT)
  313. * Returns: 0 on success, -1 on failure
  314. */
  315. int encrypt_pw_block_with_password_hash(
  316. const u8 *password, size_t password_len,
  317. const u8 *password_hash, u8 *pw_block)
  318. {
  319. size_t i, offset;
  320. u8 *pos;
  321. if (password_len > 256)
  322. return -1;
  323. os_memset(pw_block, 0, PWBLOCK_LEN);
  324. offset = (256 - password_len) * 2;
  325. if (os_get_random(pw_block, offset) < 0)
  326. return -1;
  327. for (i = 0; i < password_len; i++)
  328. pw_block[offset + i * 2] = password[i];
  329. /*
  330. * PasswordLength is 4 octets, but since the maximum password length is
  331. * 256, only first two (in little endian byte order) can be non-zero.
  332. */
  333. pos = &pw_block[2 * 256];
  334. WPA_PUT_LE16(pos, password_len * 2);
  335. rc4(pw_block, PWBLOCK_LEN, password_hash, 16);
  336. return 0;
  337. }
  338. /**
  339. * new_password_encrypted_with_old_nt_password_hash - NewPasswordEncryptedWithOldNtPasswordHash() - RFC 2759, Sect. 8.9
  340. * @new_password: 0-to-256-unicode-char NewPassword (IN; ASCII)
  341. * @new_password_len: Length of new_password
  342. * @old_password: 0-to-256-unicode-char OldPassword (IN; ASCII)
  343. * @old_password_len: Length of old_password
  344. * @encrypted_pw_block: 516-octet EncryptedPwBlock (OUT)
  345. * Returns: 0 on success, -1 on failure
  346. */
  347. int new_password_encrypted_with_old_nt_password_hash(
  348. const u8 *new_password, size_t new_password_len,
  349. const u8 *old_password, size_t old_password_len,
  350. u8 *encrypted_pw_block)
  351. {
  352. u8 password_hash[16];
  353. nt_password_hash(old_password, old_password_len, password_hash);
  354. if (encrypt_pw_block_with_password_hash(new_password, new_password_len,
  355. password_hash,
  356. encrypted_pw_block))
  357. return -1;
  358. return 0;
  359. }
  360. /**
  361. * nt_password_hash_encrypted_with_block - NtPasswordHashEncryptedWithBlock() - RFC 2759, Sect 8.13
  362. * @password_hash: 16-octer PasswordHash (IN)
  363. * @block: 16-octet Block (IN)
  364. * @cypher: 16-octer Cypher (OUT)
  365. */
  366. void nt_password_hash_encrypted_with_block(const u8 *password_hash,
  367. const u8 *block, u8 *cypher)
  368. {
  369. des_encrypt(password_hash, block, cypher);
  370. des_encrypt(password_hash + 8, block + 7, cypher + 8);
  371. }
  372. /**
  373. * old_nt_password_hash_encrypted_with_new_nt_password_hash - OldNtPasswordHashEncryptedWithNewNtPasswordHash() - RFC 2759, Sect. 8.12
  374. * @new_password: 0-to-256-unicode-char NewPassword (IN; ASCII)
  375. * @new_password_len: Length of new_password
  376. * @old_password: 0-to-256-unicode-char OldPassword (IN; ASCII)
  377. * @old_password_len: Length of old_password
  378. * @encrypted_password_hash: 16-octet EncryptedPasswordHash (OUT)
  379. */
  380. void old_nt_password_hash_encrypted_with_new_nt_password_hash(
  381. const u8 *new_password, size_t new_password_len,
  382. const u8 *old_password, size_t old_password_len,
  383. u8 *encrypted_password_hash)
  384. {
  385. u8 old_password_hash[16], new_password_hash[16];
  386. nt_password_hash(old_password, old_password_len, old_password_hash);
  387. nt_password_hash(new_password, new_password_len, new_password_hash);
  388. nt_password_hash_encrypted_with_block(old_password_hash,
  389. new_password_hash,
  390. encrypted_password_hash);
  391. }