Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.0 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. owf.c
  5. Abstract:
  6. Implentation of the one-way-functions used to implement password hashing.
  7. CalculateLmOwfPassword
  8. Author:
  9. David Chalmers (Davidc) 10-21-91
  10. David Arnold (DavidAr) 12-15-93 (Adapted for WfW RPC SSP)
  11. Revision History:
  12. --*/
  13. #ifdef BLDR_KERNEL_RUNTIME
  14. #include <bootdefs.h>
  15. #endif
  16. #include <ntlmsspi.h>
  17. #include <descrypt.h>
  18. #include <crypt.h>
  19. #include <md4.h>
  20. #include <string.h>
  21. BOOL
  22. CalculateLmOwfPassword(
  23. IN PLM_PASSWORD LmPassword,
  24. OUT PLM_OWF_PASSWORD LmOwfPassword
  25. )
  26. /*++
  27. Routine Description:
  28. Takes the passed LmPassword and performs a one-way-function on it.
  29. The current implementation does this by using the password as a key
  30. to encrypt a known block of text.
  31. Arguments:
  32. LmPassword - The password to perform the one-way-function on.
  33. LmOwfPassword - The hashed password is returned here
  34. Return Values:
  35. TRUE - The function was completed successfully. The hashed
  36. password is in LmOwfPassword.
  37. FALSE - Something failed. The LmOwfPassword is undefined.
  38. --*/
  39. {
  40. char StdEncrPwd[] = "KGS!@#$%";
  41. BLOCK_KEY Key[2];
  42. PCHAR pKey;
  43. // Copy the password into our key buffer and zero pad to fill the 2 keys
  44. pKey = (PCHAR)(&Key[0]);
  45. while (*LmPassword && (pKey < (PCHAR)(&Key[2]))) {
  46. *pKey++ = *LmPassword++;
  47. }
  48. while (pKey < (PCHAR)(&Key[2])) {
  49. *pKey++ = 0;
  50. }
  51. // Use the keys to encrypt the standard text
  52. if (DES_ECB_LM(ENCR_KEY,
  53. (unsigned char *)&Key[0],
  54. (unsigned char *)StdEncrPwd,
  55. (unsigned char *)&LmOwfPassword->data[0]
  56. ) != CRYPT_OK) {
  57. return (FALSE);
  58. }
  59. if (DES_ECB_LM(ENCR_KEY,
  60. (unsigned char *)&Key[1],
  61. (unsigned char *)StdEncrPwd,
  62. (unsigned char *)&LmOwfPassword->data[1]
  63. ) != CRYPT_OK) {
  64. return (FALSE);
  65. }
  66. //
  67. // clear our copy of the cleartext password
  68. //
  69. pKey = (PCHAR)(&Key[0]);
  70. while (pKey < (PCHAR)(&Key[2])) {
  71. *pKey++ = 0;
  72. }
  73. return(TRUE);
  74. }
  75. BOOL
  76. CalculateNtOwfPassword(
  77. IN PNT_PASSWORD NtPassword,
  78. OUT PNT_OWF_PASSWORD NtOwfPassword
  79. )
  80. /*++
  81. Routine Description:
  82. Takes the passed NtPassword and performs a one-way-function on it.
  83. Uses the RSA MD4 function
  84. Arguments:
  85. NtPassword - The password to perform the one-way-function on.
  86. NtOwfPassword - The hashed password is returned here
  87. Return Values:
  88. STATUS_SUCCESS - The function was completed successfully. The hashed
  89. password is in NtOwfPassword.
  90. --*/
  91. {
  92. MD4_CTX MD4_Context;
  93. MD4Init(&MD4_Context);
  94. MD4Update(&MD4_Context, (PCHAR)NtPassword->Buffer, NtPassword->Length);
  95. MD4Final(&MD4_Context);
  96. if (sizeof(*NtOwfPassword) != sizeof(MD4_Context.digest)) {
  97. return(FALSE);
  98. }
  99. memcpy((PVOID)NtOwfPassword, (PVOID)MD4_Context.digest, sizeof(*NtOwfPassword));
  100. return(TRUE);
  101. }