void AuthenticatedSymmetricCipherBase::ProcessData(byte *outString, const byte *inString, size_t length) { m_totalMessageLength += length; if (m_state >= State_IVSet && m_totalMessageLength > MaxMessageLength()) throw InvalidArgument(AlgorithmName() + ": message length exceeds maximum"); reswitch: switch (m_state) { case State_Start: case State_KeySet: throw BadState(AlgorithmName(), "ProcessData", "setting key and IV"); case State_AuthFooter: throw BadState(AlgorithmName(), "ProcessData was called after footer input has started"); case State_IVSet: AuthenticateLastHeaderBlock(); m_bufferedDataLength = 0; m_state = AuthenticationIsOnPlaintext()==IsForwardTransformation() ? State_AuthUntransformed : State_AuthTransformed; goto reswitch; case State_AuthUntransformed: AuthenticateData(inString, length); AccessSymmetricCipher().ProcessData(outString, inString, length); break; case State_AuthTransformed: AccessSymmetricCipher().ProcessData(outString, inString, length); AuthenticateData(outString, length); break; default: CRYPTOPP_ASSERT(false); } }
void TF_EncryptorBase::Encrypt(RandomNumberGenerator &rng, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs ¶meters) const { if (plaintextLength > FixedMaxPlaintextLength()) { if (FixedMaxPlaintextLength() < 1) throw InvalidArgument(AlgorithmName() + ": this key is too short to encrypt any messages"); else throw InvalidArgument(AlgorithmName() + ": message length of " + IntToString(plaintextLength) + " exceeds the maximum of " + IntToString(FixedMaxPlaintextLength()) + " for this public key"); } SecByteBlock paddedBlock(PaddedBlockByteLength()); GetMessageEncodingInterface().Pad(rng, plaintext, plaintextLength, paddedBlock, PaddedBlockBitLength(), parameters); GetTrapdoorFunctionInterface().ApplyRandomizedFunction(rng, Integer(paddedBlock, paddedBlock.size())).Encode(ciphertext, FixedCiphertextLength()); }
void CCM_Base::SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs ¶ms) { BlockCipher &blockCipher = AccessBlockCipher(); blockCipher.SetKey(userKey, keylength, params); if (blockCipher.BlockSize() != REQUIRED_BLOCKSIZE) throw InvalidArgument(AlgorithmName() + ": block size of underlying block cipher is not 16"); m_digestSize = params.GetIntValueWithDefault(Name::DigestSize(), DefaultDigestSize()); if (m_digestSize % 2 > 0 || m_digestSize < 4 || m_digestSize > 16) throw InvalidArgument(AlgorithmName() + ": DigestSize must be 4, 6, 8, 10, 12, 14, or 16"); m_buffer.Grow(2*REQUIRED_BLOCKSIZE); m_L = 8; }
bool BufferedTransformation::ChannelMessageSeriesEnd(const std::string &channel, int propagation, bool blocking) { if (channel.empty()) return MessageSeriesEnd(propagation, blocking); else throw NoChannelSupport(AlgorithmName()); }
bool BufferedTransformation::ChannelFlush(const std::string &channel, bool completeFlush, int propagation, bool blocking) { if (channel.empty()) return Flush(completeFlush, propagation, blocking); else throw NoChannelSupport(AlgorithmName()); }
size_t BufferedTransformation::ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) { if (channel.empty()) return Put2(begin, length, messageEnd, blocking); else throw NoChannelSupport(AlgorithmName()); }
byte * BufferedTransformation::ChannelCreatePutSpace(const std::string &channel, size_t &size) { if (channel.empty()) return CreatePutSpace(size); else throw NoChannelSupport(AlgorithmName()); }
void AuthenticatedSymmetricCipherBase::Update(const byte *input, size_t length) { if (length == 0) return; switch (m_state) { case State_Start: case State_KeySet: throw BadState(AlgorithmName(), "Update", "setting key and IV"); case State_IVSet: AuthenticateData(input, length); m_totalHeaderLength += length; break; case State_AuthUntransformed: case State_AuthTransformed: AuthenticateLastConfidentialBlock(); m_bufferedDataLength = 0; m_state = State_AuthFooter; // fall through case State_AuthFooter: AuthenticateData(input, length); m_totalFooterLength += length; break; default: CRYPTOPP_ASSERT(false); } }
void TF_EncryptorBase::Encrypt(RandomNumberGenerator &rng, const byte *plaintext, unsigned int plaintextLength, byte *ciphertext, const NameValuePairs ¶meters) const { if (plaintextLength > FixedMaxPlaintextLength()) throw InvalidArgument(AlgorithmName() + ": message too long for this public key"); SecByteBlock paddedBlock(PaddedBlockByteLength()); GetMessageEncodingInterface().Pad(rng, plaintext, plaintextLength, paddedBlock, PaddedBlockBitLength(), parameters); GetTrapdoorFunctionInterface().ApplyRandomizedFunction(rng, Integer(paddedBlock, paddedBlock.size())).Encode(ciphertext, FixedCiphertextLength()); }
void StreamTransformation::ProcessLastBlock(byte *outString, const byte *inString, size_t length) { CRYPTOPP_ASSERT(MinLastBlockSize() == 0); // this function should be overridden otherwise if (length == MandatoryBlockSize()) ProcessData(outString, inString, length); else if (length != 0) throw NotImplemented(AlgorithmName() + ": this object doesn't support a special last block"); }
void AuthenticatedSymmetricCipherBase::TruncatedFinal(byte *mac, size_t macSize) { if (m_totalHeaderLength > MaxHeaderLength()) throw InvalidArgument(AlgorithmName() + ": header length of " + IntToString(m_totalHeaderLength) + " exceeds the maximum of " + IntToString(MaxHeaderLength())); if (m_totalFooterLength > MaxFooterLength()) { if (MaxFooterLength() == 0) throw InvalidArgument(AlgorithmName() + ": additional authenticated data (AAD) cannot be input after data to be encrypted or decrypted"); else throw InvalidArgument(AlgorithmName() + ": footer length of " + IntToString(m_totalFooterLength) + " exceeds the maximum of " + IntToString(MaxFooterLength())); } switch (m_state) { case State_Start: case State_KeySet: throw BadState(AlgorithmName(), "TruncatedFinal", "setting key and IV"); case State_IVSet: AuthenticateLastHeaderBlock(); m_bufferedDataLength = 0; // fall through case State_AuthUntransformed: case State_AuthTransformed: AuthenticateLastConfidentialBlock(); m_bufferedDataLength = 0; // fall through case State_AuthFooter: AuthenticateLastFooterBlock(mac, macSize); m_bufferedDataLength = 0; break; default: CRYPTOPP_ASSERT(false); } m_state = State_KeySet; }
void AuthenticatedSymmetricCipherBase::Resynchronize(const byte *iv, int length) { if (m_state < State_KeySet) throw BadState(AlgorithmName(), "Resynchronize", "key is set"); m_bufferedDataLength = 0; m_totalHeaderLength = m_totalMessageLength = m_totalFooterLength = 0; m_state = State_KeySet; Resync(iv, this->ThrowIfInvalidIVLength(length)); m_state = State_IVSet; }
DecodingResult TF_DecryptorBase::Decrypt(RandomNumberGenerator &rng, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs ¶meters) const { if (ciphertextLength != FixedCiphertextLength()) throw InvalidArgument(AlgorithmName() + ": ciphertext length of " + IntToString(ciphertextLength) + " doesn't match the required length of " + IntToString(FixedCiphertextLength()) + " for this key"); SecByteBlock paddedBlock(PaddedBlockByteLength()); Integer x = GetTrapdoorFunctionInterface().CalculateInverse(rng, Integer(ciphertext, ciphertextLength)); if (x.ByteCount() > paddedBlock.size()) x = Integer::Zero(); // don't return false here to prevent timing attack x.Encode(paddedBlock, paddedBlock.size()); return GetMessageEncodingInterface().Unpad(paddedBlock, PaddedBlockBitLength(), plaintext, parameters); }
void CCM_Base::AuthenticateLastHeaderBlock() { byte *cbcBuffer = CBC_Buffer(); const BlockCipher &cipher = GetBlockCipher(); if (m_aadLength != m_totalHeaderLength) throw InvalidArgument(AlgorithmName() + ": header length doesn't match that given in SpecifyDataLengths"); if (m_bufferedDataLength > 0) { xorbuf(cbcBuffer, m_buffer, m_bufferedDataLength); cipher.ProcessBlock(cbcBuffer); m_bufferedDataLength = 0; } }
void CCM_Base::UncheckedSpecifyDataLengths(lword headerLength, lword messageLength, lword /*footerLength*/) { if (m_state != State_IVSet) throw BadState(AlgorithmName(), "SpecifyDataLengths", "or after State_IVSet"); m_aadLength = headerLength; m_messageLength = messageLength; byte *cbcBuffer = CBC_Buffer(); const BlockCipher &cipher = GetBlockCipher(); cbcBuffer[0] = byte(64*(headerLength>0) + 8*((m_digestSize-2)/2) + (m_L-1)); // flag PutWord<word64>(true, BIG_ENDIAN_ORDER, cbcBuffer+REQUIRED_BLOCKSIZE-8, m_messageLength); memcpy(cbcBuffer+1, m_buffer+1, REQUIRED_BLOCKSIZE-1-m_L); cipher.ProcessBlock(cbcBuffer); if (headerLength>0) { CRYPTOPP_ASSERT(m_bufferedDataLength == 0); if (headerLength < ((1<<16) - (1<<8))) { PutWord<word16>(true, BIG_ENDIAN_ORDER, m_buffer, (word16)headerLength); m_bufferedDataLength = 2; } else if (headerLength < (W64LIT(1)<<32)) { m_buffer[0] = 0xff; m_buffer[1] = 0xfe; PutWord<word32>(false, BIG_ENDIAN_ORDER, m_buffer+2, (word32)headerLength); m_bufferedDataLength = 6; } else { m_buffer[0] = 0xff; m_buffer[1] = 0xff; PutWord<word64>(false, BIG_ENDIAN_ORDER, m_buffer+2, headerLength); m_bufferedDataLength = 10; } } }
Base::Result Processor::createResult(int user_id) { return Base::Result(AlgorithmName(),user_id, penalty_judge_->OveralPenalty(), 0 /*TODO*/); }