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.

127 lines
3.3 KiB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // File: pkcrypto.h
  4. //
  5. // Microsoft Digital Rights Management
  6. // Copyright (C) 1998-1999 Microsoft Corporation, All Rights Reserved
  7. //
  8. // Description:
  9. // public key crypto library
  10. //
  11. // Author: marcuspe
  12. //
  13. //-----------------------------------------------------------------------------
  14. #ifndef __DRMPKCRYPTO_H__
  15. #define __DRMPKCRYPTO_H__
  16. #include <wtypes.h>
  17. #define LNGQDW 5
  18. /*
  19. typedef struct {
  20. DWORD y[2*LNGQDW];
  21. } PUBKEY;
  22. typedef struct {
  23. DWORD x[LNGQDW];
  24. } PRIVKEY;
  25. */
  26. #define PK_ENC_PUBLIC_KEY_LEN (2 * LNGQDW * sizeof(DWORD))
  27. #define PK_ENC_PRIVATE_KEY_LEN ( LNGQDW * sizeof(DWORD))
  28. #define PK_ENC_PLAINTEXT_LEN ((LNGQDW-1) * sizeof(DWORD))
  29. #define PK_ENC_CIPHERTEXT_LEN (4 * LNGQDW * sizeof(DWORD))
  30. #define PK_ENC_SIGNATURE_LEN (2 * LNGQDW * sizeof(DWORD))
  31. //////////////////////////////////////////////////////////////////////
  32. struct PUBKEY
  33. {
  34. BYTE y[ PK_ENC_PUBLIC_KEY_LEN ];
  35. };
  36. //////////////////////////////////////////////////////////////////////
  37. static inline int operator == ( const PUBKEY& a, const PUBKEY& b )
  38. {
  39. return (memcmp( a.y, b.y, sizeof(a.y) ) == 0);
  40. }
  41. //////////////////////////////////////////////////////////////////////
  42. struct PRIVKEY
  43. {
  44. BYTE x[ PK_ENC_PRIVATE_KEY_LEN ];
  45. };
  46. #if 0
  47. #include <iostream.h>
  48. #include <iomanip.h>
  49. static inline ostream& operator << ( ostream& out, const PUBKEY& oPublKey )
  50. {
  51. for (int i = 0; i < sizeof(oPublKey.y); i++)
  52. {
  53. out << " " << setfill('0') << setw(2) << hex << oPublKey.y[i];
  54. }
  55. return out;
  56. }
  57. static inline ostream& operator << ( ostream& out, const PRIVKEY& oPrivKey )
  58. {
  59. for (int i = 0; i < sizeof(oPrivKey.x); i++)
  60. {
  61. out << " " << setfill('0') << setw(2) << hex << oPrivKey.x[i];
  62. }
  63. return out;
  64. }
  65. #endif
  66. //////////////////////////////////////////////////////////////////////
  67. //
  68. //
  69. //
  70. class CDRMPKCrypto {
  71. private:
  72. char *pkd;
  73. public:
  74. CDRMPKCrypto();
  75. ~CDRMPKCrypto();
  76. HRESULT PKinit();
  77. HRESULT PKencrypt( PUBKEY *pk, BYTE *in, BYTE *out );
  78. HRESULT PKdecrypt( PRIVKEY *pk, BYTE *in, BYTE *out );
  79. HRESULT PKsign( PRIVKEY *privkey, BYTE *buffer, DWORD lbuf, BYTE *sign );
  80. BOOL PKverify( PUBKEY *pubkey, BYTE *buffer, DWORD lbuf, BYTE *sign );
  81. HRESULT PKGenKeyPair( PUBKEY *pPub, PRIVKEY *pPriv );
  82. HRESULT PKEncryptLarge( PUBKEY *pk, BYTE *in, DWORD dwLenIn, BYTE *out, DWORD symm_key_len, DWORD symm_alg );
  83. HRESULT PKDecryptLarge( PRIVKEY *pk, BYTE *in, DWORD dwLenIn, BYTE *out );
  84. };
  85. // #include "contcrpt.h"
  86. #define PKSYMM_KEY_LEN_DRMV2 7
  87. #define PKSYMM_ALG_TYPE_RC4 1
  88. // These are provided for backwards compatibility.
  89. // It can be more efficient to use the member functions in CDRMPKCrypto,
  90. // because construction of CDRMPKCrypto objects is relatively expensive.
  91. // in terms of computation.
  92. inline HRESULT PKEncryptLarge( PUBKEY *pk, BYTE *in, DWORD dwLenIn, BYTE *out, DWORD symm_key_len, DWORD symm_alg )
  93. {
  94. CDRMPKCrypto oPkc;
  95. return oPkc.PKEncryptLarge( pk, in, dwLenIn, out, symm_key_len, symm_alg );
  96. }
  97. inline HRESULT PKDecryptLarge( PRIVKEY *pk, BYTE *in, DWORD dwLenIn, BYTE *out )
  98. {
  99. CDRMPKCrypto oPkc;
  100. return oPkc.PKDecryptLarge( pk, in, dwLenIn, out );
  101. }
  102. #endif