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.

97 lines
3.0 KiB

  1. // oaep.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "oaep.h"
  5. #include <functional>
  6. NAMESPACE_BEGIN(CryptoPP)
  7. // ********************************************************
  8. size_t OAEP_Base::MaxUnpaddedLength(size_t paddedLength) const
  9. {
  10. return SaturatingSubtract(paddedLength/8, 1+2*DigestSize());
  11. }
  12. void OAEP_Base::Pad(RandomNumberGenerator &rng, const byte *input, size_t inputLength, byte *oaepBlock, size_t oaepBlockLen, const NameValuePairs &parameters) const
  13. {
  14. assert (inputLength <= MaxUnpaddedLength(oaepBlockLen));
  15. // convert from bit length to byte length
  16. if (oaepBlockLen % 8 != 0)
  17. {
  18. oaepBlock[0] = 0;
  19. oaepBlock++;
  20. }
  21. oaepBlockLen /= 8;
  22. std::auto_ptr<HashTransformation> pHash(NewHash());
  23. const size_t hLen = pHash->DigestSize();
  24. const size_t seedLen = hLen, dbLen = oaepBlockLen-seedLen;
  25. byte *const maskedSeed = oaepBlock;
  26. byte *const maskedDB = oaepBlock+seedLen;
  27. ConstByteArrayParameter encodingParameters;
  28. parameters.GetValue(Name::EncodingParameters(), encodingParameters);
  29. // DB = pHash || 00 ... || 01 || M
  30. pHash->CalculateDigest(maskedDB, encodingParameters.begin(), encodingParameters.size());
  31. memset(maskedDB+hLen, 0, dbLen-hLen-inputLength-1);
  32. maskedDB[dbLen-inputLength-1] = 0x01;
  33. memcpy(maskedDB+dbLen-inputLength, input, inputLength);
  34. rng.GenerateBlock(maskedSeed, seedLen);
  35. std::auto_ptr<MaskGeneratingFunction> pMGF(NewMGF());
  36. pMGF->GenerateAndMask(*pHash, maskedDB, dbLen, maskedSeed, seedLen);
  37. pMGF->GenerateAndMask(*pHash, maskedSeed, seedLen, maskedDB, dbLen);
  38. }
  39. DecodingResult OAEP_Base::Unpad(const byte *oaepBlock, size_t oaepBlockLen, byte *output, const NameValuePairs &parameters) const
  40. {
  41. bool invalid = false;
  42. // convert from bit length to byte length
  43. if (oaepBlockLen % 8 != 0)
  44. {
  45. invalid = (oaepBlock[0] != 0) || invalid;
  46. oaepBlock++;
  47. }
  48. oaepBlockLen /= 8;
  49. std::auto_ptr<HashTransformation> pHash(NewHash());
  50. const size_t hLen = pHash->DigestSize();
  51. const size_t seedLen = hLen, dbLen = oaepBlockLen-seedLen;
  52. invalid = (oaepBlockLen < 2*hLen+1) || invalid;
  53. SecByteBlock t(oaepBlock, oaepBlockLen);
  54. byte *const maskedSeed = t;
  55. byte *const maskedDB = t+seedLen;
  56. std::auto_ptr<MaskGeneratingFunction> pMGF(NewMGF());
  57. pMGF->GenerateAndMask(*pHash, maskedSeed, seedLen, maskedDB, dbLen);
  58. pMGF->GenerateAndMask(*pHash, maskedDB, dbLen, maskedSeed, seedLen);
  59. ConstByteArrayParameter encodingParameters;
  60. parameters.GetValue(Name::EncodingParameters(), encodingParameters);
  61. // DB = pHash' || 00 ... || 01 || M
  62. byte *M = std::find(maskedDB+hLen, maskedDB+dbLen, 0x01);
  63. invalid = (M == maskedDB+dbLen) || invalid;
  64. invalid = (std::find_if(maskedDB+hLen, M, std::bind2nd(std::not_equal_to<byte>(), 0)) != M) || invalid;
  65. invalid = !pHash->VerifyDigest(maskedDB, encodingParameters.begin(), encodingParameters.size()) || invalid;
  66. if (invalid)
  67. return DecodingResult();
  68. M++;
  69. memcpy(output, M, maskedDB+dbLen-M);
  70. return DecodingResult(maskedDB+dbLen-M);
  71. }
  72. NAMESPACE_END
  73. #endif