Counter Strike : Global Offensive Source Code
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.

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