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.

114 lines
3.1 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. // FILE : des3.c //
  3. // DESCRIPTION : Crypto CP interfaces: //
  4. // CPEncrypt //
  5. // CPDecrypt //
  6. // AUTHOR : //
  7. // HISTORY : //
  8. // Sep 13 1996 jeffspel Created //
  9. // //
  10. // Copyright (C) 1993 Microsoft Corporation All Rights Reserved //
  11. /////////////////////////////////////////////////////////////////////////////
  12. //#include <wtypes.h>
  13. #include "precomp.h"
  14. #include "nt_rsa.h"
  15. #include "des3.h"
  16. static BYTE l_ParityTable[] = {0x00,0x01,0x01,0x02,0x01,0x02,0x02,0x03,
  17. 0x01,0x02,0x02,0x03,0x02,0x03,0x03,0x04};
  18. // In des2key.c:
  19. //
  20. // Fill in the DES3Table structs with the decrypt and encrypt
  21. // key expansions.
  22. //
  23. // Assumes that the second parameter points to 2 * DES_BLOCKLEN
  24. // bytes of key.
  25. //
  26. //
  27. void
  28. des2key(
  29. PDES3TABLE pDES3Table,
  30. PBYTE pbKey)
  31. {
  32. deskey(&pDES3Table->keytab1, pbKey);
  33. deskey(&pDES3Table->keytab2, pbKey+DES_KEYSIZE);
  34. CopyMemory(&pDES3Table->keytab3, &pDES3Table->keytab1, DES_TABLESIZE);
  35. }
  36. // In des3key.c:
  37. //
  38. // Fill in the DES3Table structs with the decrypt and encrypt
  39. // key expansions.
  40. //
  41. // Assumes that the second parameter points to 3 * DES_BLOCKLEN
  42. // bytes of key.
  43. //
  44. //
  45. void
  46. des3key(
  47. PDES3TABLE pDES3Table,
  48. PBYTE pbKey)
  49. {
  50. deskey(&pDES3Table->keytab1, pbKey);
  51. deskey(&pDES3Table->keytab2, pbKey+DES_KEYSIZE);
  52. deskey(&pDES3Table->keytab3, pbKey+2*DES_KEYSIZE);
  53. }
  54. //
  55. // Encrypt or decrypt with the key in DES3Table
  56. //
  57. void
  58. des3(
  59. PBYTE pbOut,
  60. PBYTE pbIn,
  61. void *pKey,
  62. int op)
  63. {
  64. PDES3TABLE pDES3Table = (PDES3TABLE)pKey;
  65. BYTE rgbEnc1[DES_BLOCKLEN];
  66. BYTE rgbEnc2[DES_BLOCKLEN];
  67. if (ENCRYPT == op) // encrypt case
  68. {
  69. des(rgbEnc1, pbIn, &pDES3Table->keytab1, ENCRYPT);
  70. des(rgbEnc2, rgbEnc1, &pDES3Table->keytab2, DECRYPT);
  71. memset(rgbEnc1, 0xFF, DES_BLOCKLEN);
  72. des(pbOut, rgbEnc2, &pDES3Table->keytab3, ENCRYPT);
  73. memset(rgbEnc2, 0xFF, DES_BLOCKLEN);
  74. }
  75. else // decrypt case
  76. {
  77. des(rgbEnc1, pbIn, &pDES3Table->keytab3, DECRYPT);
  78. des(rgbEnc2, rgbEnc1, &pDES3Table->keytab2, ENCRYPT);
  79. memset(rgbEnc1, 0xFF, DES_BLOCKLEN);
  80. des(pbOut, rgbEnc2, &pDES3Table->keytab1, DECRYPT);
  81. memset(rgbEnc2, 0xFF, DES_BLOCKLEN);
  82. }
  83. }
  84. //
  85. // set the parity on the DES key to be odd
  86. //
  87. void
  88. desparity(
  89. PBYTE pbKey,
  90. DWORD cbKey)
  91. {
  92. DWORD i;
  93. for (i=0;i<cbKey;i++)
  94. {
  95. if (!((l_ParityTable[pbKey[i]>>4] + l_ParityTable[pbKey[i]&0x0F]) % 2))
  96. pbKey[i] = (BYTE)(pbKey[i] ^ 0x01);
  97. }
  98. }