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.

164 lines
5.6 KiB

  1. // MsRsaPriKB.cpp -- MicroSoft RSA Private Key Blob class definition
  2. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  3. // 1999. This computer program includes Confidential, Proprietary
  4. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  5. // use, disclosure, and/or reproduction is prohibited unless authorized
  6. // in writing. All Rights Reserved.
  7. #include "NoWarning.h"
  8. #include "ForceLib.h"
  9. #include <limits>
  10. #include "MsRsaPriKB.h"
  11. using namespace std;
  12. using namespace scu;
  13. using namespace iop;
  14. /////////////////////////// LOCAL/HELPER /////////////////////////////////
  15. auto_ptr<CPrivateKeyBlob>
  16. AsPCciPrivateKeyBlob(MsRsaPrivateKeyBlob const &rmsprivatekeyblob)
  17. {
  18. auto_ptr<CPrivateKeyBlob> appkb =
  19. auto_ptr<CPrivateKeyBlob>(new CPrivateKeyBlob);
  20. appkb->bPLen = static_cast<BYTE>(rmsprivatekeyblob.PrimeLength());
  21. appkb->bQLen = appkb->bPLen;
  22. appkb->bInvQLen = static_cast<BYTE>(rmsprivatekeyblob.CoefficientLength());
  23. appkb->bKsecModQLen = static_cast<BYTE>(rmsprivatekeyblob.ExponentLength());
  24. appkb->bKsecModPLen = appkb->bKsecModQLen;
  25. memcpy(appkb->bP, rmsprivatekeyblob.Prime1(), appkb->bPLen);
  26. memcpy(appkb->bQ, rmsprivatekeyblob.Prime2(), appkb->bQLen);
  27. memcpy(appkb->bInvQ, rmsprivatekeyblob.Coefficient(), appkb->bInvQLen);
  28. memcpy(appkb->bKsecModQ, rmsprivatekeyblob.Exponent2(),
  29. appkb->bKsecModQLen);
  30. memcpy(appkb->bKsecModP, rmsprivatekeyblob.Exponent1(),
  31. appkb->bKsecModPLen);
  32. return appkb;
  33. }
  34. /////////////////////////// PUBLIC /////////////////////////////////
  35. // Types
  36. // C'tors/D'tors
  37. MsRsaPrivateKeyBlob::MsRsaPrivateKeyBlob(ALG_ID algid,
  38. Blob const &rbRawExponent,
  39. Blob const &rbRawModulus,
  40. Blob const &rbPrime1,
  41. Blob const &rbPrime2,
  42. Blob const &rbExponent1,
  43. Blob const &rbExponent2,
  44. Blob const &rbCoefficient,
  45. Blob const &rbPrivateExponent)
  46. : MsRsaPublicKeyBlob(PRIVATEKEYBLOB, algid,
  47. rbRawExponent,
  48. rbRawModulus,
  49. (9 * (rbRawModulus.length() *
  50. sizeof HeaderElementType)) / 16)
  51. {
  52. Append(rbPrime1.data(), rbPrime1.length());
  53. Append(rbPrime2.data(), rbPrime2.length());
  54. Append(rbExponent1.data(), rbExponent1.length());
  55. Append(rbExponent2.data(), rbExponent2.length());
  56. Append(rbCoefficient.data(), rbCoefficient.length());
  57. Append(rbPrivateExponent.data(), rbPrivateExponent.length());
  58. }
  59. MsRsaPrivateKeyBlob::MsRsaPrivateKeyBlob(BYTE const *pbData,
  60. DWORD dwDataLength)
  61. : MsRsaPublicKeyBlob(pbData, dwDataLength)
  62. {}
  63. MsRsaPrivateKeyBlob::~MsRsaPrivateKeyBlob()
  64. {}
  65. // Operators
  66. // Operations
  67. // Access
  68. MsRsaPrivateKeyBlob::ElementValueType const *
  69. MsRsaPrivateKeyBlob::Coefficient() const
  70. {
  71. return Exponent2() + ExponentLength();
  72. }
  73. size_t
  74. MsRsaPrivateKeyBlob::CoefficientLength() const
  75. {
  76. return BitLength() /
  77. (2 * numeric_limits<ElementValueType>::digits);
  78. }
  79. MsRsaPrivateKeyBlob::ElementValueType const *
  80. MsRsaPrivateKeyBlob::Exponent1() const
  81. {
  82. return Prime2() + PrimeLength();
  83. }
  84. MsRsaPrivateKeyBlob::ElementValueType const *
  85. MsRsaPrivateKeyBlob::Exponent2() const
  86. {
  87. return Exponent1() + ExponentLength();
  88. }
  89. size_t
  90. MsRsaPrivateKeyBlob::ExponentLength() const
  91. {
  92. return BitLength() /
  93. (2 * numeric_limits<ElementValueType>::digits);
  94. }
  95. MsRsaPrivateKeyBlob::ElementValueType const *
  96. MsRsaPrivateKeyBlob::Prime1() const
  97. {
  98. return Modulus() + Length();
  99. }
  100. MsRsaPrivateKeyBlob::ElementValueType const *
  101. MsRsaPrivateKeyBlob::Prime2() const
  102. {
  103. return Prime1() + PrimeLength();
  104. }
  105. size_t
  106. MsRsaPrivateKeyBlob::PrimeLength() const
  107. {
  108. return BitLength() / (2 * numeric_limits<ElementValueType>::digits);
  109. }
  110. MsRsaPrivateKeyBlob::ElementValueType const *
  111. MsRsaPrivateKeyBlob::PrivateExponent() const
  112. {
  113. return Coefficient() + CoefficientLength();
  114. }
  115. size_t
  116. MsRsaPrivateKeyBlob::PriExpLength() const
  117. {
  118. return BitLength() / numeric_limits<ElementValueType>::digits;
  119. }
  120. // Predicates
  121. // Static Variables
  122. /////////////////////////// PROTECTED /////////////////////////////////
  123. // C'tors/D'tors
  124. // Operators
  125. // Operations
  126. // Access
  127. // Predicates
  128. // Static Variables
  129. /////////////////////////// PRIVATE /////////////////////////////////
  130. // C'tors/D'tors
  131. // Operators
  132. // Operations
  133. // Access
  134. // Predicates
  135. // Static Variables