bool DigestVerifierTemplate<P,F>::VerifyDigest(const byte *digest, unsigned int digestLen, const byte *signature) const { SecByteBlock paddedBlock(PaddedBlockByteLength()); f.ApplyFunction(Integer(signature, DigestSignatureLength())).Encode(paddedBlock, paddedBlock.size); SecByteBlock recoveredDigest(MaxDigestLength()); unsigned int recoveredDigestLen = pad.Unpad(paddedBlock, PaddedBlockBitLength(), recoveredDigest); return digestLen == recoveredDigestLen && memcmp(digest, recoveredDigest, digestLen) == 0; }
void DigestSignerTemplate<P,F>::SignDigest(RandomNumberGenerator &rng, const byte *digest, unsigned int digestLength, byte *signature) const { assert(digestLength <= MaxDigestLength()); SecByteBlock paddedBlock(PaddedBlockByteLength()); pad.Pad(rng, digest, digestLength, paddedBlock, PaddedBlockBitLength()); f.CalculateInverse(Integer(paddedBlock, paddedBlock.size)).Encode(signature, DigestSignatureLength()); }
void EncryptorTemplate<P,F>::Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText) { assert(plainTextLength <= MaxPlainTextLength()); SecByteBlock paddedBlock(PaddedBlockByteLength()); pad.Pad(rng, plainText, plainTextLength, paddedBlock, PaddedBlockBitLength()); f.ApplyFunction(Integer(paddedBlock, paddedBlock.size)).Encode(cipherText, CipherTextLength()); }
DecodingResult TF_DecryptorBase::Decrypt(RandomNumberGenerator &rng, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs ¶meters) const { SecByteBlock paddedBlock(PaddedBlockByteLength()); Integer x = GetTrapdoorFunctionInterface().CalculateInverse(rng, Integer(ciphertext, FixedCiphertextLength())); 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 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()); }
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 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()); }
word32 FixedMaxPlaintextLength() const {return SaturatingSubtract(PaddedBlockBitLength() / 8, 10U); }
word32 PaddedBlockByteLength() const {return BitsToBytes(PaddedBlockBitLength());}
unsigned int DecryptorTemplate<P,F>::Decrypt(const byte *cipherText, byte *plainText) { SecByteBlock paddedBlock(PaddedBlockByteLength()); f.CalculateInverse(Integer(cipherText, CipherTextLength())).Encode(paddedBlock, paddedBlock.size); return pad.Unpad(paddedBlock, PaddedBlockBitLength(), plainText); }
unsigned int CryptoSystemBaseTemplate<P,F>::MaxPlainTextLength() const { return pad.MaxUnpaddedLength(PaddedBlockBitLength()); }