Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# pubkey.cpp

    00001 // pubkey.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #include "pch.h"
    00004
    00005 #ifndef CRYPTOPP_IMPORTS
    00006
    00007 #include "pubkey.h"
    00008
    00009 NAMESPACE_BEGIN(CryptoPP)
    00010
    00011 void TF_DigestSignerBase::SignDigest(RandomNumberGenerator &rng, const byte *digest, unsigned int digestLen, byte *signature) const
    00012 {
    00013         assert(digestLen <= MaxDigestLength());
    00014
    00015         SecByteBlock paddedBlock(PaddedBlockByteLength());
    00016         GetPaddingAlgorithm().Pad(rng, digest, digestLen, paddedBlock, PaddedBlockBitLength());
    00017         GetTrapdoorFunctionInterface().CalculateRandomizedInverse(rng, Integer(paddedBlock, paddedBlock.size())).Encode(signature, DigestSignatureLength());
    00018 }
    00019
    00020 bool TF_DigestVerifierBase::VerifyDigest(const byte *digest, unsigned int digestLen, const byte *signature) const
    00021 {
    00022         SecByteBlock paddedBlock(PaddedBlockByteLength());
    00023         Integer x = GetTrapdoorFunctionInterface().ApplyFunction(Integer(signature, DigestSignatureLength()));
    00024         if (x.ByteCount() > paddedBlock.size())
    00025                 x = Integer::Zero();    // don't return false here to prevent timing attack
    00026         x.Encode(paddedBlock, paddedBlock.size());
    00027         if (GetPaddingAlgorithm().IsReversible())
    00028         {
    00029                 SecByteBlock recoveredDigest(MaxDigestLength());
    00030                 DecodingResult result = GetPaddingAlgorithm().Unpad(paddedBlock, PaddedBlockBitLength(), recoveredDigest);
    00031                 return result == DecodingResult(digestLen) && memcmp(digest, recoveredDigest, digestLen) == 0;
    00032         }
    00033         else
    00034         {
    00035                 SecByteBlock paddedBlock2(PaddedBlockByteLength());
    00036                 GetPaddingAlgorithm().Pad(NullRNG(), digest, digestLen, paddedBlock2, PaddedBlockBitLength());
    00037                 return paddedBlock == paddedBlock2;
    00038         }
    00039 }
    00040
    00041 DecodingResult TF_DecryptorBase::FixedLengthDecrypt(const byte *cipherText, byte *plainText) const
    00042 {
    00043         SecByteBlock paddedBlock(PaddedBlockByteLength());
    00044         Integer x = GetTrapdoorFunctionInterface().CalculateInverse(Integer(cipherText, FixedCiphertextLength()));
    00045         if (x.ByteCount() > paddedBlock.size())
    00046                 x = Integer::Zero();    // don't return false here to prevent timing attack
    00047         x.Encode(paddedBlock, paddedBlock.size());
    00048         return GetPaddingAlgorithm().Unpad(paddedBlock, PaddedBlockBitLength(), plainText);
    00049 }
    00050
    00051 void TF_EncryptorBase::Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText) const
    00052 {
    00053         if (plainTextLength > FixedMaxPlaintextLength())
    00054                 throw InvalidArgument(AlgorithmName() + ": message too long for this public key");
    00055
    00056         SecByteBlock paddedBlock(PaddedBlockByteLength());
    00057         GetPaddingAlgorithm().Pad(rng, plainText, plainTextLength, paddedBlock, PaddedBlockBitLength());
    00058         GetTrapdoorFunctionInterface().ApplyRandomizedFunction(rng, Integer(paddedBlock, paddedBlock.size())).Encode(cipherText, FixedCiphertextLength());
    00059 }
    00060
    00061 NAMESPACE_END
    00062
    00063 #endif