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.

165 lines
7.9 KiB

  1. // pubkey.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "pubkey.h"
  5. NAMESPACE_BEGIN(CryptoPP)
  6. void P1363_MGF1KDF2_Common(HashTransformation &hash, byte *output, size_t outputLength, const byte *input, size_t inputLength, const byte *derivationParams, size_t derivationParamsLength, bool mask, unsigned int counterStart)
  7. {
  8. ArraySink *sink;
  9. HashFilter filter(hash, sink = mask ? new ArrayXorSink(output, outputLength) : new ArraySink(output, outputLength));
  10. word32 counter = counterStart;
  11. while (sink->AvailableSize() > 0)
  12. {
  13. filter.Put(input, inputLength);
  14. filter.PutWord32(counter++);
  15. filter.Put(derivationParams, derivationParamsLength);
  16. filter.MessageEnd();
  17. }
  18. }
  19. bool PK_DeterministicSignatureMessageEncodingMethod::VerifyMessageRepresentative(
  20. HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
  21. byte *representative, size_t representativeBitLength) const
  22. {
  23. SecByteBlock computedRepresentative(BitsToBytes(representativeBitLength));
  24. ComputeMessageRepresentative(NullRNG(), NULL, 0, hash, hashIdentifier, messageEmpty, computedRepresentative, representativeBitLength);
  25. return VerifyBufsEqual(representative, computedRepresentative, computedRepresentative.size());
  26. }
  27. bool PK_RecoverableSignatureMessageEncodingMethod::VerifyMessageRepresentative(
  28. HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
  29. byte *representative, size_t representativeBitLength) const
  30. {
  31. SecByteBlock recoveredMessage(MaxRecoverableLength(representativeBitLength, hashIdentifier.second, hash.DigestSize()));
  32. DecodingResult result = RecoverMessageFromRepresentative(
  33. hash, hashIdentifier, messageEmpty, representative, representativeBitLength, recoveredMessage);
  34. return result.isValidCoding && result.messageLength == 0;
  35. }
  36. void TF_SignerBase::InputRecoverableMessage(PK_MessageAccumulator &messageAccumulator, const byte *recoverableMessage, size_t recoverableMessageLength) const
  37. {
  38. PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);
  39. HashIdentifier id = GetHashIdentifier();
  40. const MessageEncodingInterface &encoding = GetMessageEncodingInterface();
  41. if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(id.second, ma.AccessHash().DigestSize()))
  42. throw PK_SignatureScheme::KeyTooShort();
  43. size_t maxRecoverableLength = encoding.MaxRecoverableLength(MessageRepresentativeBitLength(), GetHashIdentifier().second, ma.AccessHash().DigestSize());
  44. if (maxRecoverableLength == 0)
  45. {throw NotImplemented("TF_SignerBase: this algorithm does not support messsage recovery or the key is too short");}
  46. if (recoverableMessageLength > maxRecoverableLength)
  47. throw InvalidArgument("TF_SignerBase: the recoverable message part is too long for the given key and algorithm");
  48. ma.m_recoverableMessage.Assign(recoverableMessage, recoverableMessageLength);
  49. encoding.ProcessRecoverableMessage(
  50. ma.AccessHash(),
  51. recoverableMessage, recoverableMessageLength,
  52. NULL, 0, ma.m_semisignature);
  53. }
  54. size_t TF_SignerBase::SignAndRestart(RandomNumberGenerator &rng, PK_MessageAccumulator &messageAccumulator, byte *signature, bool restart) const
  55. {
  56. PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);
  57. HashIdentifier id = GetHashIdentifier();
  58. const MessageEncodingInterface &encoding = GetMessageEncodingInterface();
  59. if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(id.second, ma.AccessHash().DigestSize()))
  60. throw PK_SignatureScheme::KeyTooShort();
  61. SecByteBlock representative(MessageRepresentativeLength());
  62. encoding.ComputeMessageRepresentative(rng,
  63. ma.m_recoverableMessage, ma.m_recoverableMessage.size(),
  64. ma.AccessHash(), id, ma.m_empty,
  65. representative, MessageRepresentativeBitLength());
  66. ma.m_empty = true;
  67. Integer r(representative, representative.size());
  68. size_t signatureLength = SignatureLength();
  69. GetTrapdoorFunctionInterface().CalculateRandomizedInverse(rng, r).Encode(signature, signatureLength);
  70. return signatureLength;
  71. }
  72. void TF_VerifierBase::InputSignature(PK_MessageAccumulator &messageAccumulator, const byte *signature, size_t signatureLength) const
  73. {
  74. PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);
  75. HashIdentifier id = GetHashIdentifier();
  76. const MessageEncodingInterface &encoding = GetMessageEncodingInterface();
  77. if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(id.second, ma.AccessHash().DigestSize()))
  78. throw PK_SignatureScheme::KeyTooShort();
  79. ma.m_representative.New(MessageRepresentativeLength());
  80. Integer x = GetTrapdoorFunctionInterface().ApplyFunction(Integer(signature, signatureLength));
  81. if (x.BitCount() > MessageRepresentativeBitLength())
  82. x = Integer::Zero(); // don't return false here to prevent timing attack
  83. x.Encode(ma.m_representative, ma.m_representative.size());
  84. }
  85. bool TF_VerifierBase::VerifyAndRestart(PK_MessageAccumulator &messageAccumulator) const
  86. {
  87. PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);
  88. HashIdentifier id = GetHashIdentifier();
  89. const MessageEncodingInterface &encoding = GetMessageEncodingInterface();
  90. if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(id.second, ma.AccessHash().DigestSize()))
  91. throw PK_SignatureScheme::KeyTooShort();
  92. bool result = encoding.VerifyMessageRepresentative(
  93. ma.AccessHash(), id, ma.m_empty, ma.m_representative, MessageRepresentativeBitLength());
  94. ma.m_empty = true;
  95. return result;
  96. }
  97. DecodingResult TF_VerifierBase::RecoverAndRestart(byte *recoveredMessage, PK_MessageAccumulator &messageAccumulator) const
  98. {
  99. PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);
  100. HashIdentifier id = GetHashIdentifier();
  101. const MessageEncodingInterface &encoding = GetMessageEncodingInterface();
  102. if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(id.second, ma.AccessHash().DigestSize()))
  103. throw PK_SignatureScheme::KeyTooShort();
  104. DecodingResult result = encoding.RecoverMessageFromRepresentative(
  105. ma.AccessHash(), id, ma.m_empty, ma.m_representative, MessageRepresentativeBitLength(), recoveredMessage);
  106. ma.m_empty = true;
  107. return result;
  108. }
  109. DecodingResult TF_DecryptorBase::Decrypt(RandomNumberGenerator &rng, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs &parameters) const
  110. {
  111. if (ciphertextLength != FixedCiphertextLength())
  112. throw InvalidArgument(AlgorithmName() + ": ciphertext length of " + IntToString(ciphertextLength) + " doesn't match the required length of " + IntToString(FixedCiphertextLength()) + " for this key");
  113. SecByteBlock paddedBlock(PaddedBlockByteLength());
  114. Integer x = GetTrapdoorFunctionInterface().CalculateInverse(rng, Integer(ciphertext, ciphertextLength));
  115. if (x.ByteCount() > paddedBlock.size())
  116. x = Integer::Zero(); // don't return false here to prevent timing attack
  117. x.Encode(paddedBlock, paddedBlock.size());
  118. return GetMessageEncodingInterface().Unpad(paddedBlock, PaddedBlockBitLength(), plaintext, parameters);
  119. }
  120. void TF_EncryptorBase::Encrypt(RandomNumberGenerator &rng, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs &parameters) const
  121. {
  122. if (plaintextLength > FixedMaxPlaintextLength())
  123. {
  124. if (FixedMaxPlaintextLength() < 1)
  125. throw InvalidArgument(AlgorithmName() + ": this key is too short to encrypt any messages");
  126. else
  127. throw InvalidArgument(AlgorithmName() + ": message length of " + IntToString(plaintextLength) + " exceeds the maximum of " + IntToString(FixedMaxPlaintextLength()) + " for this public key");
  128. }
  129. SecByteBlock paddedBlock(PaddedBlockByteLength());
  130. GetMessageEncodingInterface().Pad(rng, plaintext, plaintextLength, paddedBlock, PaddedBlockBitLength(), parameters);
  131. GetTrapdoorFunctionInterface().ApplyRandomizedFunction(rng, Integer(paddedBlock, paddedBlock.size())).Encode(ciphertext, FixedCiphertextLength());
  132. }
  133. NAMESPACE_END
  134. #endif