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.

163 lines
4.8 KiB

  1. // EncodedMsg.cpp -- Encoded Message implementation
  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 <scuOsExc.h>
  10. #include "EncodedMsg.h"
  11. /////////////////////////// HELPER /////////////////////////////////
  12. namespace
  13. {
  14. typedef Blob::value_type Marker;
  15. enum
  16. {
  17. mSeparator = '\x00',
  18. mPrivatePad = '\xff'
  19. };
  20. enum
  21. {
  22. // Require three markers: one block type + two separators
  23. cRequiredMarkersLength = 3 * sizeof Marker
  24. };
  25. enum
  26. {
  27. mAlternatePrivateBlockType = '\x00',
  28. mPrivateBlockType = '\x01',
  29. mPublicBlockType = '\x02'
  30. };
  31. enum
  32. {
  33. cMinimumPadding = 8
  34. };
  35. Blob::size_type
  36. PadLength(Blob::size_type cMessageLength,
  37. Blob::size_type cIntendedEncodingLength)
  38. {
  39. // Require three markers: one block type + two separators
  40. Blob::size_type cLength = cIntendedEncodingLength;
  41. // Calculate pad length, guarding against underflow.
  42. if (cLength < cMessageLength)
  43. return 0;
  44. cLength -= cMessageLength;
  45. if (cLength < cRequiredMarkersLength)
  46. return 0;
  47. cLength -= cRequiredMarkersLength;
  48. return cLength;
  49. }
  50. }
  51. /////////////////////////// PUBLIC /////////////////////////////////
  52. // Types
  53. // C'tors/D'tors
  54. EncodedMessage::EncodedMessage(Blob const &rMessage,
  55. RsaKey::Type ktOperation,
  56. Blob::size_type cIntendedEncodingLength)
  57. : m_blob()
  58. {
  59. // Precondition:
  60. // if (!RsaKey::IsValidModulusLength(cIntendedEncodingLength))
  61. // throw scu::OsException(ERROR_INTERNAL_ERROR);
  62. // Precondition: Message must be small enough to encode
  63. if (!IsMessageLengthValid(rMessage.length(), cIntendedEncodingLength))
  64. throw scu::OsException(ERROR_INTERNAL_ERROR);
  65. m_blob.reserve(cIntendedEncodingLength);
  66. // Ensure the encoded message when converted to an integer is
  67. // less than the modulus by leading with a separator
  68. m_blob += mSeparator;
  69. Marker const mBlockType = (ktOperation == RsaKey::ktPrivate) ?
  70. mPrivateBlockType : mPublicBlockType;
  71. m_blob += mBlockType;
  72. Blob::size_type cPadLength = PadLength(rMessage.length(),
  73. cIntendedEncodingLength);
  74. Pad(ktOperation, cPadLength);
  75. // Mark beginning of message
  76. m_blob += mSeparator;
  77. m_blob += rMessage;
  78. }
  79. EncodedMessage::~EncodedMessage()
  80. {}
  81. // Operators
  82. // Operations
  83. // Access
  84. Blob
  85. EncodedMessage::Value() const
  86. {
  87. return m_blob;
  88. }
  89. // Predicates
  90. bool
  91. EncodedMessage::IsMessageLengthValid(Blob::size_type cMessageLength,
  92. Blob::size_type cIntendedEncodingLength)
  93. {
  94. return PadLength(cMessageLength, cIntendedEncodingLength) >=
  95. cMinimumPadding;
  96. }
  97. // Static Variables
  98. /////////////////////////// PROTECTED /////////////////////////////////
  99. // C'tors/D'tors
  100. // Operators
  101. // Operations
  102. // Access
  103. // Predicates
  104. // Static Variables
  105. /////////////////////////// PRIVATE /////////////////////////////////
  106. // Types
  107. // C'tors/D'tors
  108. // Operators
  109. // Operations
  110. void
  111. EncodedMessage::Pad(RsaKey::Type ktOperation,
  112. Blob::size_type cRequiredPadLength)
  113. {
  114. if (RsaKey::ktPrivate == ktOperation)
  115. m_blob.append(cRequiredPadLength, mPrivatePad);
  116. else // TO DO: Support random pad
  117. throw scu::OsException(NTE_FAIL);
  118. }
  119. // Access
  120. // Predicates
  121. // Static Variables