Team Fortress 2 Source Code as on 22/4/2020
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.

129 lines
4.5 KiB

  1. // pkcspad.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_PKCSPAD_CPP // SunCC workaround: compiler could cause this file to be included twice
  4. #define CRYPTOPP_PKCSPAD_CPP
  5. #include "pkcspad.h"
  6. #include "misc.h"
  7. #include <assert.h>
  8. NAMESPACE_BEGIN(CryptoPP)
  9. // more in dll.cpp
  10. template<> const byte PKCS_DigestDecoration<Weak1::MD2>::decoration[] = {0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x02,0x05,0x00,0x04,0x10};
  11. template<> const unsigned int PKCS_DigestDecoration<Weak1::MD2>::length = sizeof(PKCS_DigestDecoration<Weak1::MD2>::decoration);
  12. template<> const byte PKCS_DigestDecoration<Weak1::MD5>::decoration[] = {0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x04,0x10};
  13. template<> const unsigned int PKCS_DigestDecoration<Weak1::MD5>::length = sizeof(PKCS_DigestDecoration<Weak1::MD5>::decoration);
  14. template<> const byte PKCS_DigestDecoration<RIPEMD160>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x24,0x03,0x02,0x01,0x05,0x00,0x04,0x14};
  15. template<> const unsigned int PKCS_DigestDecoration<RIPEMD160>::length = sizeof(PKCS_DigestDecoration<RIPEMD160>::decoration);
  16. template<> const byte PKCS_DigestDecoration<Tiger>::decoration[] = {0x30,0x29,0x30,0x0D,0x06,0x09,0x2B,0x06,0x01,0x04,0x01,0xDA,0x47,0x0C,0x02,0x05,0x00,0x04,0x18};
  17. template<> const unsigned int PKCS_DigestDecoration<Tiger>::length = sizeof(PKCS_DigestDecoration<Tiger>::decoration);
  18. size_t PKCS_EncryptionPaddingScheme::MaxUnpaddedLength(size_t paddedLength) const
  19. {
  20. return SaturatingSubtract(paddedLength/8, 10U);
  21. }
  22. void PKCS_EncryptionPaddingScheme::Pad(RandomNumberGenerator& rng, const byte *input, size_t inputLen, byte *pkcsBlock, size_t pkcsBlockLen, const NameValuePairs& parameters) const
  23. {
  24. CRYPTOPP_UNUSED(parameters);
  25. assert (inputLen <= MaxUnpaddedLength(pkcsBlockLen)); // this should be checked by caller
  26. // convert from bit length to byte length
  27. if (pkcsBlockLen % 8 != 0)
  28. {
  29. pkcsBlock[0] = 0;
  30. pkcsBlock++;
  31. }
  32. pkcsBlockLen /= 8;
  33. pkcsBlock[0] = 2; // block type 2
  34. // pad with non-zero random bytes
  35. for (unsigned i = 1; i < pkcsBlockLen-inputLen-1; i++)
  36. pkcsBlock[i] = (byte)rng.GenerateWord32(1, 0xff);
  37. pkcsBlock[pkcsBlockLen-inputLen-1] = 0; // separator
  38. memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);
  39. }
  40. DecodingResult PKCS_EncryptionPaddingScheme::Unpad(const byte *pkcsBlock, size_t pkcsBlockLen, byte *output, const NameValuePairs& parameters) const
  41. {
  42. CRYPTOPP_UNUSED(parameters);
  43. bool invalid = false;
  44. size_t maxOutputLen = MaxUnpaddedLength(pkcsBlockLen);
  45. // convert from bit length to byte length
  46. if (pkcsBlockLen % 8 != 0)
  47. {
  48. invalid = (pkcsBlock[0] != 0) || invalid;
  49. pkcsBlock++;
  50. }
  51. pkcsBlockLen /= 8;
  52. // Require block type 2.
  53. invalid = (pkcsBlock[0] != 2) || invalid;
  54. // skip past the padding until we find the separator
  55. size_t i=1;
  56. while (i<pkcsBlockLen && pkcsBlock[i++]) { // null body
  57. }
  58. assert(i==pkcsBlockLen || pkcsBlock[i-1]==0);
  59. size_t outputLen = pkcsBlockLen - i;
  60. invalid = (outputLen > maxOutputLen) || invalid;
  61. if (invalid)
  62. return DecodingResult();
  63. memcpy (output, pkcsBlock+i, outputLen);
  64. return DecodingResult(outputLen);
  65. }
  66. // ********************************************************
  67. #ifndef CRYPTOPP_IMPORTS
  68. void PKCS1v15_SignatureMessageEncodingMethod::ComputeMessageRepresentative(RandomNumberGenerator &rng,
  69. const byte *recoverableMessage, size_t recoverableMessageLength,
  70. HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
  71. byte *representative, size_t representativeBitLength) const
  72. {
  73. CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(recoverableMessage), CRYPTOPP_UNUSED(recoverableMessageLength);
  74. CRYPTOPP_UNUSED(messageEmpty), CRYPTOPP_UNUSED(hashIdentifier);
  75. assert(representativeBitLength >= MinRepresentativeBitLength(hashIdentifier.second, hash.DigestSize()));
  76. size_t pkcsBlockLen = representativeBitLength;
  77. // convert from bit length to byte length
  78. if (pkcsBlockLen % 8 != 0)
  79. {
  80. representative[0] = 0;
  81. representative++;
  82. }
  83. pkcsBlockLen /= 8;
  84. representative[0] = 1; // block type 1
  85. unsigned int digestSize = hash.DigestSize();
  86. byte *pPadding = representative + 1;
  87. byte *pDigest = representative + pkcsBlockLen - digestSize;
  88. byte *pHashId = pDigest - hashIdentifier.second;
  89. byte *pSeparator = pHashId - 1;
  90. // pad with 0xff
  91. memset(pPadding, 0xff, pSeparator-pPadding);
  92. *pSeparator = 0;
  93. memcpy(pHashId, hashIdentifier.first, hashIdentifier.second);
  94. hash.Final(pDigest);
  95. }
  96. #endif
  97. NAMESPACE_END
  98. #endif