Leaked source code of windows server 2003
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.

151 lines
3.8 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 2002 - 2002
  3. Module Name:
  4. encraption.h
  5. Abstract:
  6. This module contains simple obfuscation algorithm to hide communication
  7. keys.
  8. Author:
  9. Carsten Hansen
  10. Alper Selcuk
  11. --*/
  12. #ifndef _ENCRAPTION_H
  13. #define _ENCRAPTION_H
  14. //=============================================================================
  15. //
  16. // This routine clears the given key. The encryption/decryption method is
  17. // developed by CarstenH.
  18. //
  19. NTSTATUS __forceinline ClearKey(
  20. const BYTE *PrivKey,
  21. BYTE *ClearKey,
  22. ULONG KeySize,
  23. ULONG MagicNumber2)
  24. {
  25. const char * pszName = "Microsoft Corporation";
  26. /* Convert the obfuscated key to clear */
  27. BYTE *clearDRMKPriv;
  28. clearDRMKPriv = (BYTE *) ExAllocatePool(PagedPool, KeySize);
  29. if (NULL == clearDRMKPriv) {
  30. return STATUS_INSUFFICIENT_RESOURCES;
  31. }
  32. /* Permute bytes */
  33. int k = 1;
  34. clearDRMKPriv[0] = PrivKey[0];
  35. do {
  36. int l = k * MagicNumber2 % (KeySize - 1);
  37. clearDRMKPriv[k] = PrivKey[l];
  38. if (l == 1)
  39. break;
  40. k = l;
  41. } while (1);
  42. clearDRMKPriv[KeySize - 1] = PrivKey[KeySize - 1];
  43. /* Swap nibbles */
  44. DWORD * pdw = (DWORD *) clearDRMKPriv;
  45. DWORD * qdw = (DWORD *) (clearDRMKPriv + KeySize);
  46. DWORD granularity = 4;
  47. for (; pdw < qdw; ++ pdw)
  48. {
  49. DWORD temp = 0xF0F0F0F0;
  50. temp &= *pdw;
  51. *pdw ^= temp;
  52. temp ^= (*pdw << granularity);
  53. *pdw |= (*pdw << granularity);
  54. *pdw ^= (temp >> granularity);
  55. }
  56. /* XOR with "Microsoft" */
  57. ULONG len = strlen(pszName);
  58. for (ULONG i = 0, j = 0; i < KeySize; ++i)
  59. {
  60. clearDRMKPriv[i] ^= pszName[j];
  61. ++j;
  62. if (j > len)
  63. j = 0;
  64. }
  65. RtlCopyMemory(ClearKey, clearDRMKPriv, KeySize);
  66. RtlZeroMemory(clearDRMKPriv, KeySize);
  67. ExFreePool(clearDRMKPriv);
  68. return STATUS_SUCCESS;
  69. }
  70. /* Obfuscation algorithm.
  71. {
  72. // XOR with "Microsoft"
  73. int len = strlen(pszName);
  74. for (int i = 0, j = 0; i < sizeof(objDRMKPriv); ++i)
  75. {
  76. objDRMKPriv[i] = DRMKpriv[i] ^ pszName[j];
  77. ++j;
  78. if (j > len)
  79. j = 0;
  80. }
  81. // Swap nibbles
  82. DWORD * pdw = (DWORD *) objDRMKPriv;
  83. DWORD * qdw = (DWORD *) (objDRMKPriv + sizeof(objDRMKPriv));
  84. DWORD granularity = 4;
  85. for (; pdw < qdw; ++ pdw)
  86. {
  87. DWORD temp = 0xF0F0F0F0;
  88. temp &= *pdw;
  89. *pdw ^= temp;
  90. temp ^= (*pdw << granularity);
  91. *pdw |= (*pdw << granularity);
  92. *pdw ^= (temp >> granularity);
  93. }
  94. // Permute bytes
  95. int k = 1;
  96. BYTE temp = objDRMKPriv[k];
  97. do {
  98. int l = k * MAGIC_NUMBER_1 % (sizeof(objDRMKPriv) - 1);
  99. if (l == 1)
  100. break;
  101. objDRMKPriv[k] = objDRMKPriv[l];
  102. k = l;
  103. } while (1);
  104. objDRMKPriv[k] = temp;
  105. }
  106. */
  107. /* Clean Key and Cert
  108. static const BYTE DRMKpriv[20] = {
  109. 0x80, 0x0B, 0x97, 0x30, 0x7A, 0xFB, 0x1B, 0x3B,
  110. 0xB7, 0xB2, 0x0F, 0x44, 0x63, 0xD8, 0xA5, 0x2D,
  111. 0xD5, 0xBC, 0x3D, 0x75};
  112. static const BYTE DRMKCert[104] = {
  113. 0x00, 0x01, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00,
  114. 0x46, 0xB1, 0x18, 0x76, 0x66, 0xBE, 0x91, 0xEC,
  115. 0xBD, 0x06, 0x50, 0x72, 0x1B, 0x8C, 0xD3, 0x55,
  116. 0xD2, 0x1A, 0xB7, 0x60, 0x6C, 0x65, 0xDD, 0xE4,
  117. 0x54, 0xCE, 0xFD, 0xEB, 0x4A, 0x9F, 0x0A, 0x5A,
  118. 0xD1, 0x44, 0xB2, 0x32, 0xB9, 0xA0, 0x84, 0x67,
  119. 0x55, 0xD7, 0xFE, 0x45, 0xD5, 0x16, 0x36, 0x7B,
  120. 0xEC, 0x3C, 0xFF, 0x7D, 0x4C, 0x09, 0x9A, 0x7B,
  121. 0xB4, 0x6C, 0xEF, 0x2B, 0xC5, 0xF8, 0xA3, 0xC4,
  122. 0xE2, 0x57, 0xC5, 0x87, 0xA6, 0x75, 0x85, 0xFE,
  123. 0xE2, 0x34, 0xA3, 0x30, 0xAE, 0x4D, 0xDB, 0x23,
  124. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x06,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
  126. */
  127. #endif