📄 cryptlib.cpp
字号:
ArraySink arraySink(outString, getMax); return (size_t)TransferTo(arraySink, getMax); }}size_t BufferedTransformation::Peek(byte &outByte) const{ if (AttachedTransformation()) return AttachedTransformation()->Peek(outByte); else return Peek(&outByte, 1);}size_t BufferedTransformation::Peek(byte *outString, size_t peekMax) const{ if (AttachedTransformation()) return AttachedTransformation()->Peek(outString, peekMax); else { ArraySink arraySink(outString, peekMax); return (size_t)CopyTo(arraySink, peekMax); }}lword BufferedTransformation::Skip(lword skipMax){ if (AttachedTransformation()) return AttachedTransformation()->Skip(skipMax); else return TransferTo(TheBitBucket(), skipMax);}lword BufferedTransformation::TotalBytesRetrievable() const{ if (AttachedTransformation()) return AttachedTransformation()->TotalBytesRetrievable(); else return MaxRetrievable();}unsigned int BufferedTransformation::NumberOfMessages() const{ if (AttachedTransformation()) return AttachedTransformation()->NumberOfMessages(); else return CopyMessagesTo(TheBitBucket());}bool BufferedTransformation::AnyMessages() const{ if (AttachedTransformation()) return AttachedTransformation()->AnyMessages(); else return NumberOfMessages() != 0;}bool BufferedTransformation::GetNextMessage(){ if (AttachedTransformation()) return AttachedTransformation()->GetNextMessage(); else { assert(!AnyMessages()); return false; }}unsigned int BufferedTransformation::SkipMessages(unsigned int count){ if (AttachedTransformation()) return AttachedTransformation()->SkipMessages(count); else return TransferMessagesTo(TheBitBucket(), count);}size_t BufferedTransformation::TransferMessagesTo2(BufferedTransformation &target, unsigned int &messageCount, const std::string &channel, bool blocking){ if (AttachedTransformation()) return AttachedTransformation()->TransferMessagesTo2(target, messageCount, channel, blocking); else { unsigned int maxMessages = messageCount; for (messageCount=0; messageCount < maxMessages && AnyMessages(); messageCount++) { size_t blockedBytes; lword transferredBytes; while (AnyRetrievable()) { transferredBytes = LWORD_MAX; blockedBytes = TransferTo2(target, transferredBytes, channel, blocking); if (blockedBytes > 0) return blockedBytes; } if (target.ChannelMessageEnd(channel, GetAutoSignalPropagation(), blocking)) return 1; bool result = GetNextMessage(); assert(result); } return 0; }}unsigned int BufferedTransformation::CopyMessagesTo(BufferedTransformation &target, unsigned int count, const std::string &channel) const{ if (AttachedTransformation()) return AttachedTransformation()->CopyMessagesTo(target, count, channel); else return 0;}void BufferedTransformation::SkipAll(){ if (AttachedTransformation()) AttachedTransformation()->SkipAll(); else { while (SkipMessages()) {} while (Skip()) {} }}size_t BufferedTransformation::TransferAllTo2(BufferedTransformation &target, const std::string &channel, bool blocking){ if (AttachedTransformation()) return AttachedTransformation()->TransferAllTo2(target, channel, blocking); else { assert(!NumberOfMessageSeries()); unsigned int messageCount; do { messageCount = UINT_MAX; size_t blockedBytes = TransferMessagesTo2(target, messageCount, channel, blocking); if (blockedBytes) return blockedBytes; } while (messageCount != 0); lword byteCount; do { byteCount = ULONG_MAX; size_t blockedBytes = TransferTo2(target, byteCount, channel, blocking); if (blockedBytes) return blockedBytes; } while (byteCount != 0); return 0; }}void BufferedTransformation::CopyAllTo(BufferedTransformation &target, const std::string &channel) const{ if (AttachedTransformation()) AttachedTransformation()->CopyAllTo(target, channel); else { assert(!NumberOfMessageSeries()); while (CopyMessagesTo(target, UINT_MAX, channel)) {} }}void BufferedTransformation::SetRetrievalChannel(const std::string &channel){ if (AttachedTransformation()) AttachedTransformation()->SetRetrievalChannel(channel);}size_t BufferedTransformation::ChannelPutWord16(const std::string &channel, word16 value, ByteOrder order, bool blocking){ PutWord(false, order, m_buf, value); return ChannelPut(channel, m_buf, 2, blocking);}size_t BufferedTransformation::ChannelPutWord32(const std::string &channel, word32 value, ByteOrder order, bool blocking){ PutWord(false, order, m_buf, value); return ChannelPut(channel, m_buf, 4, blocking);}size_t BufferedTransformation::PutWord16(word16 value, ByteOrder order, bool blocking){ return ChannelPutWord16(DEFAULT_CHANNEL, value, order, blocking);}size_t BufferedTransformation::PutWord32(word32 value, ByteOrder order, bool blocking){ return ChannelPutWord32(DEFAULT_CHANNEL, value, order, blocking);}size_t BufferedTransformation::PeekWord16(word16 &value, ByteOrder order) const{ byte buf[2] = {0, 0}; size_t len = Peek(buf, 2); if (order) value = (buf[0] << 8) | buf[1]; else value = (buf[1] << 8) | buf[0]; return len;}size_t BufferedTransformation::PeekWord32(word32 &value, ByteOrder order) const{ byte buf[4] = {0, 0, 0, 0}; size_t len = Peek(buf, 4); if (order) value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf [3]; else value = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf [0]; return len;}size_t BufferedTransformation::GetWord16(word16 &value, ByteOrder order){ return (size_t)Skip(PeekWord16(value, order));}size_t BufferedTransformation::GetWord32(word32 &value, ByteOrder order){ return (size_t)Skip(PeekWord32(value, order));}void BufferedTransformation::Attach(BufferedTransformation *newOut){ if (AttachedTransformation() && AttachedTransformation()->Attachable()) AttachedTransformation()->Attach(newOut); else Detach(newOut);}void GeneratableCryptoMaterial::GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize){ GenerateRandom(rng, MakeParameters("KeySize", (int)keySize));}class PK_DefaultEncryptionFilter : public Unflushable<Filter>{public: PK_DefaultEncryptionFilter(RandomNumberGenerator &rng, const PK_Encryptor &encryptor, BufferedTransformation *attachment, const NameValuePairs ¶meters) : m_rng(rng), m_encryptor(encryptor), m_parameters(parameters) { Detach(attachment); } size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking) { FILTER_BEGIN; m_plaintextQueue.Put(inString, length); if (messageEnd) { { size_t plaintextLength; if (!SafeConvert(m_plaintextQueue.CurrentSize(), plaintextLength)) throw InvalidArgument("PK_DefaultEncryptionFilter: plaintext too long"); size_t ciphertextLength = m_encryptor.CiphertextLength(plaintextLength); SecByteBlock plaintext(plaintextLength); m_plaintextQueue.Get(plaintext, plaintextLength); m_ciphertext.resize(ciphertextLength); m_encryptor.Encrypt(m_rng, plaintext, plaintextLength, m_ciphertext, m_parameters); } FILTER_OUTPUT(1, m_ciphertext, m_ciphertext.size(), messageEnd); } FILTER_END_NO_MESSAGE_END; } RandomNumberGenerator &m_rng; const PK_Encryptor &m_encryptor; const NameValuePairs &m_parameters; ByteQueue m_plaintextQueue; SecByteBlock m_ciphertext;};BufferedTransformation * PK_Encryptor::CreateEncryptionFilter(RandomNumberGenerator &rng, BufferedTransformation *attachment, const NameValuePairs ¶meters) const{ return new PK_DefaultEncryptionFilter(rng, *this, attachment, parameters);}class PK_DefaultDecryptionFilter : public Unflushable<Filter>{public: PK_DefaultDecryptionFilter(RandomNumberGenerator &rng, const PK_Decryptor &decryptor, BufferedTransformation *attachment, const NameValuePairs ¶meters) : m_rng(rng), m_decryptor(decryptor), m_parameters(parameters) { Detach(attachment); } size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking) { FILTER_BEGIN; m_ciphertextQueue.Put(inString, length); if (messageEnd) { { size_t ciphertextLength; if (!SafeConvert(m_ciphertextQueue.CurrentSize(), ciphertextLength)) throw InvalidArgument("PK_DefaultDecryptionFilter: ciphertext too long"); size_t maxPlaintextLength = m_decryptor.MaxPlaintextLength(ciphertextLength); SecByteBlock ciphertext(ciphertextLength); m_ciphertextQueue.Get(ciphertext, ciphertextLength); m_plaintext.resize(maxPlaintextLength); m_result = m_decryptor.Decrypt(m_rng, ciphertext, ciphertextLength, m_plaintext, m_parameters); if (!m_result.isValidCoding) throw InvalidCiphertext(m_decryptor.AlgorithmName() + ": invalid ciphertext"); } FILTER_OUTPUT(1, m_plaintext, m_result.messageLength, messageEnd); } FILTER_END_NO_MESSAGE_END; } RandomNumberGenerator &m_rng; const PK_Decryptor &m_decryptor; const NameValuePairs &m_parameters; ByteQueue m_ciphertextQueue; SecByteBlock m_plaintext; DecodingResult m_result;};BufferedTransformation * PK_Decryptor::CreateDecryptionFilter(RandomNumberGenerator &rng, BufferedTransformation *attachment, const NameValuePairs ¶meters) const{ return new PK_DefaultDecryptionFilter(rng, *this, attachment, parameters);}size_t PK_Signer::Sign(RandomNumberGenerator &rng, PK_MessageAccumulator *messageAccumulator, byte *signature) const{ std::auto_ptr<PK_MessageAccumulator> m(messageAccumulator); return SignAndRestart(rng, *m, signature, false);}size_t PK_Signer::SignMessage(RandomNumberGenerator &rng, const byte *message, size_t messageLen, byte *signature) const{ std::auto_ptr<PK_MessageAccumulator> m(NewSignatureAccumulator(rng)); m->Update(message, messageLen); return SignAndRestart(rng, *m, signature, false);}size_t PK_Signer::SignMessageWithRecovery(RandomNumberGenerator &rng, const byte *recoverableMessage, size_t recoverableMessageLength, const byte *nonrecoverableMessage, size_t nonrecoverableMessageLength, byte *signature) const{ std::auto_ptr<PK_MessageAccumulator> m(NewSignatureAccumulator(rng)); InputRecoverableMessage(*m, recoverableMessage, recoverableMessageLength); m->Update(nonrecoverableMessage, nonrecoverableMessageLength); return SignAndRestart(rng, *m, signature, false);}bool PK_Verifier::Verify(PK_MessageAccumulator *messageAccumulator) const{ std::auto_ptr<PK_MessageAccumulator> m(messageAccumulator); return VerifyAndRestart(*m);}bool PK_Verifier::VerifyMessage(const byte *message, size_t messageLen, const byte *signature, size_t signatureLength) const{ std::auto_ptr<PK_MessageAccumulator> m(NewVerificationAccumulator()); InputSignature(*m, signature, signatureLength); m->Update(message, messageLen); return VerifyAndRestart(*m);}DecodingResult PK_Verifier::Recover(byte *recoveredMessage, PK_MessageAccumulator *messageAccumulator) const{ std::auto_ptr<PK_MessageAccumulator> m(messageAccumulator); return RecoverAndRestart(recoveredMessage, *m);}DecodingResult PK_Verifier::RecoverMessage(byte *recoveredMessage, const byte *nonrecoverableMessage, size_t nonrecoverableMessageLength, const byte *signature, size_t signatureLength) const{ std::auto_ptr<PK_MessageAccumulator> m(NewVerificationAccumulator()); InputSignature(*m, signature, signatureLength); m->Update(nonrecoverableMessage, nonrecoverableMessageLength); return RecoverAndRestart(recoveredMessage, *m);}void SimpleKeyAgreementDomain::GenerateKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const{ GeneratePrivateKey(rng, privateKey); GeneratePublicKey(rng, privateKey, publicKey);}void AuthenticatedKeyAgreementDomain::GenerateStaticKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const{ GenerateStaticPrivateKey(rng, privateKey); GenerateStaticPublicKey(rng, privateKey, publicKey);}void AuthenticatedKeyAgreementDomain::GenerateEphemeralKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const{ GenerateEphemeralPrivateKey(rng, privateKey); GenerateEphemeralPublicKey(rng, privateKey, publicKey);}NAMESPACE_END#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -