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.

121 lines
4.4 KiB

  1. #ifndef CRYPTOPP_ELGAMAL_H
  2. #define CRYPTOPP_ELGAMAL_H
  3. #include "modexppc.h"
  4. #include "dsa.h"
  5. NAMESPACE_BEGIN(CryptoPP)
  6. class CRYPTOPP_NO_VTABLE ElGamalBase : public DL_KeyAgreementAlgorithm_DH<Integer, NoCofactorMultiplication>,
  7. public DL_KeyDerivationAlgorithm<Integer>,
  8. public DL_SymmetricEncryptionAlgorithm
  9. {
  10. public:
  11. void Derive(const DL_GroupParameters<Integer> &groupParams, byte *derivedKey, size_t derivedLength, const Integer &agreedElement, const Integer &ephemeralPublicKey, const NameValuePairs &derivationParams) const
  12. {
  13. agreedElement.Encode(derivedKey, derivedLength);
  14. }
  15. size_t GetSymmetricKeyLength(size_t plainTextLength) const
  16. {
  17. return GetGroupParameters().GetModulus().ByteCount();
  18. }
  19. size_t GetSymmetricCiphertextLength(size_t plainTextLength) const
  20. {
  21. unsigned int len = GetGroupParameters().GetModulus().ByteCount();
  22. if (plainTextLength <= GetMaxSymmetricPlaintextLength(len))
  23. return len;
  24. else
  25. return 0;
  26. }
  27. size_t GetMaxSymmetricPlaintextLength(size_t cipherTextLength) const
  28. {
  29. unsigned int len = GetGroupParameters().GetModulus().ByteCount();
  30. if (cipherTextLength == len)
  31. return STDMIN(255U, len-3);
  32. else
  33. return 0;
  34. }
  35. void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plainText, size_t plainTextLength, byte *cipherText, const NameValuePairs &parameters) const
  36. {
  37. const Integer &p = GetGroupParameters().GetModulus();
  38. unsigned int modulusLen = p.ByteCount();
  39. SecByteBlock block(modulusLen-1);
  40. rng.GenerateBlock(block, modulusLen-2-plainTextLength);
  41. memcpy(block+modulusLen-2-plainTextLength, plainText, plainTextLength);
  42. block[modulusLen-2] = (byte)plainTextLength;
  43. a_times_b_mod_c(Integer(key, modulusLen), Integer(block, modulusLen-1), p).Encode(cipherText, modulusLen);
  44. }
  45. DecodingResult SymmetricDecrypt(const byte *key, const byte *cipherText, size_t cipherTextLength, byte *plainText, const NameValuePairs &parameters) const
  46. {
  47. const Integer &p = GetGroupParameters().GetModulus();
  48. unsigned int modulusLen = p.ByteCount();
  49. if (cipherTextLength != modulusLen)
  50. return DecodingResult();
  51. Integer m = a_times_b_mod_c(Integer(cipherText, modulusLen), Integer(key, modulusLen).InverseMod(p), p);
  52. m.Encode(plainText, 1);
  53. unsigned int plainTextLength = plainText[0];
  54. if (plainTextLength > GetMaxSymmetricPlaintextLength(modulusLen))
  55. return DecodingResult();
  56. m >>= 8;
  57. m.Encode(plainText, plainTextLength);
  58. return DecodingResult(plainTextLength);
  59. }
  60. virtual const DL_GroupParameters_GFP & GetGroupParameters() const =0;
  61. };
  62. template <class BASE, class SCHEME_OPTIONS, class KEY>
  63. class ElGamalObjectImpl : public DL_ObjectImplBase<BASE, SCHEME_OPTIONS, KEY>, public ElGamalBase
  64. {
  65. public:
  66. size_t FixedMaxPlaintextLength() const {return this->MaxPlaintextLength(FixedCiphertextLength());}
  67. size_t FixedCiphertextLength() const {return this->CiphertextLength(0);}
  68. const DL_GroupParameters_GFP & GetGroupParameters() const {return this->GetKey().GetGroupParameters();}
  69. DecodingResult FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *cipherText, byte *plainText) const
  70. {return Decrypt(rng, cipherText, FixedCiphertextLength(), plainText);}
  71. protected:
  72. const DL_KeyAgreementAlgorithm<Integer> & GetKeyAgreementAlgorithm() const {return *this;}
  73. const DL_KeyDerivationAlgorithm<Integer> & GetKeyDerivationAlgorithm() const {return *this;}
  74. const DL_SymmetricEncryptionAlgorithm & GetSymmetricEncryptionAlgorithm() const {return *this;}
  75. };
  76. struct ElGamalKeys
  77. {
  78. typedef DL_CryptoKeys_GFP::GroupParameters GroupParameters;
  79. typedef DL_PrivateKey_GFP_OldFormat<DL_CryptoKeys_GFP::PrivateKey> PrivateKey;
  80. typedef DL_PublicKey_GFP_OldFormat<DL_CryptoKeys_GFP::PublicKey> PublicKey;
  81. };
  82. //! ElGamal encryption scheme with non-standard padding
  83. struct ElGamal
  84. {
  85. typedef DL_CryptoSchemeOptions<ElGamal, ElGamalKeys, int, int, int> SchemeOptions;
  86. static const char * StaticAlgorithmName() {return "ElgamalEnc/Crypto++Padding";}
  87. typedef SchemeOptions::GroupParameters GroupParameters;
  88. //! implements PK_Encryptor interface
  89. typedef PK_FinalTemplate<ElGamalObjectImpl<DL_EncryptorBase<Integer>, SchemeOptions, SchemeOptions::PublicKey> > Encryptor;
  90. //! implements PK_Decryptor interface
  91. typedef PK_FinalTemplate<ElGamalObjectImpl<DL_DecryptorBase<Integer>, SchemeOptions, SchemeOptions::PrivateKey> > Decryptor;
  92. };
  93. typedef ElGamal::Encryptor ElGamalEncryptor;
  94. typedef ElGamal::Decryptor ElGamalDecryptor;
  95. NAMESPACE_END
  96. #endif