2002-10-04 17:31:41 +00:00
// pubkey.cpp - written and placed in the public domain by Wei Dai
# include "pch.h"
2003-07-04 00:17:37 +00:00
# ifndef CRYPTOPP_IMPORTS
2002-10-04 17:31:41 +00:00
# include "pubkey.h"
NAMESPACE_BEGIN ( CryptoPP )
2005-07-12 04:23:32 +00:00
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 )
2002-10-04 17:31:41 +00:00
{
2003-03-20 01:24:12 +00:00
ArraySink * sink ;
HashFilter filter ( hash , sink = mask ? new ArrayXorSink ( output , outputLength ) : new ArraySink ( output , outputLength ) ) ;
word32 counter = counterStart ;
while ( sink - > AvailableSize ( ) > 0 )
{
filter . Put ( input , inputLength ) ;
filter . PutWord32 ( counter + + ) ;
2003-07-16 01:53:45 +00:00
filter . Put ( derivationParams , derivationParamsLength ) ;
2003-03-20 01:24:12 +00:00
filter . MessageEnd ( ) ;
}
}
2002-10-04 17:31:41 +00:00
2003-03-20 01:24:12 +00:00
bool PK_DeterministicSignatureMessageEncodingMethod : : VerifyMessageRepresentative (
HashTransformation & hash , HashIdentifier hashIdentifier , bool messageEmpty ,
2005-07-12 04:23:32 +00:00
byte * representative , size_t representativeBitLength ) const
2003-03-20 01:24:12 +00:00
{
SecByteBlock computedRepresentative ( BitsToBytes ( representativeBitLength ) ) ;
ComputeMessageRepresentative ( NullRNG ( ) , NULL , 0 , hash , hashIdentifier , messageEmpty , computedRepresentative , representativeBitLength ) ;
2009-03-02 02:39:17 +00:00
return VerifyBufsEqual ( representative , computedRepresentative , computedRepresentative . size ( ) ) ;
2002-10-04 17:31:41 +00:00
}
2003-03-20 01:24:12 +00:00
bool PK_RecoverableSignatureMessageEncodingMethod : : VerifyMessageRepresentative (
HashTransformation & hash , HashIdentifier hashIdentifier , bool messageEmpty ,
2005-07-12 04:23:32 +00:00
byte * representative , size_t representativeBitLength ) const
2002-10-04 17:31:41 +00:00
{
2003-03-20 01:24:12 +00:00
SecByteBlock recoveredMessage ( MaxRecoverableLength ( representativeBitLength , hashIdentifier . second , hash . DigestSize ( ) ) ) ;
DecodingResult result = RecoverMessageFromRepresentative (
hash , hashIdentifier , messageEmpty , representative , representativeBitLength , recoveredMessage ) ;
return result . isValidCoding & & result . messageLength = = 0 ;
}
2005-07-12 04:23:32 +00:00
void TF_SignerBase : : InputRecoverableMessage ( PK_MessageAccumulator & messageAccumulator , const byte * recoverableMessage , size_t recoverableMessageLength ) const
2003-03-20 01:24:12 +00:00
{
PK_MessageAccumulatorBase & ma = static_cast < PK_MessageAccumulatorBase & > ( messageAccumulator ) ;
2005-01-20 04:19:35 +00:00
HashIdentifier id = GetHashIdentifier ( ) ;
const MessageEncodingInterface & encoding = GetMessageEncodingInterface ( ) ;
if ( MessageRepresentativeBitLength ( ) < encoding . MinRepresentativeBitLength ( id . second , ma . AccessHash ( ) . DigestSize ( ) ) )
throw PK_SignatureScheme : : KeyTooShort ( ) ;
2005-07-12 04:23:32 +00:00
size_t maxRecoverableLength = encoding . MaxRecoverableLength ( MessageRepresentativeBitLength ( ) , GetHashIdentifier ( ) . second , ma . AccessHash ( ) . DigestSize ( ) ) ;
2003-03-20 01:24:12 +00:00
if ( maxRecoverableLength = = 0 )
{ throw NotImplemented ( " TF_SignerBase: this algorithm does not support messsage recovery or the key is too short " ) ; }
if ( recoverableMessageLength > maxRecoverableLength )
throw InvalidArgument ( " TF_SignerBase: the recoverable message part is too long for the given key and algorithm " ) ;
ma . m_recoverableMessage . Assign ( recoverableMessage , recoverableMessageLength ) ;
2005-01-20 04:19:35 +00:00
encoding . ProcessRecoverableMessage (
2003-03-20 01:24:12 +00:00
ma . AccessHash ( ) ,
recoverableMessage , recoverableMessageLength ,
NULL , 0 , ma . m_semisignature ) ;
}
2005-07-12 04:23:32 +00:00
size_t TF_SignerBase : : SignAndRestart ( RandomNumberGenerator & rng , PK_MessageAccumulator & messageAccumulator , byte * signature , bool restart ) const
2003-03-20 01:24:12 +00:00
{
PK_MessageAccumulatorBase & ma = static_cast < PK_MessageAccumulatorBase & > ( messageAccumulator ) ;
2005-01-20 04:19:35 +00:00
HashIdentifier id = GetHashIdentifier ( ) ;
const MessageEncodingInterface & encoding = GetMessageEncodingInterface ( ) ;
if ( MessageRepresentativeBitLength ( ) < encoding . MinRepresentativeBitLength ( id . second , ma . AccessHash ( ) . DigestSize ( ) ) )
throw PK_SignatureScheme : : KeyTooShort ( ) ;
2003-03-20 01:24:12 +00:00
SecByteBlock representative ( MessageRepresentativeLength ( ) ) ;
2005-01-20 04:19:35 +00:00
encoding . ComputeMessageRepresentative ( rng ,
2003-03-20 01:24:12 +00:00
ma . m_recoverableMessage , ma . m_recoverableMessage . size ( ) ,
2005-01-20 04:19:35 +00:00
ma . AccessHash ( ) , id , ma . m_empty ,
2003-03-20 01:24:12 +00:00
representative , MessageRepresentativeBitLength ( ) ) ;
ma . m_empty = true ;
Integer r ( representative , representative . size ( ) ) ;
2005-07-12 04:23:32 +00:00
size_t signatureLength = SignatureLength ( ) ;
2003-03-20 01:24:12 +00:00
GetTrapdoorFunctionInterface ( ) . CalculateRandomizedInverse ( rng , r ) . Encode ( signature , signatureLength ) ;
return signatureLength ;
}
2005-07-12 04:23:32 +00:00
void TF_VerifierBase : : InputSignature ( PK_MessageAccumulator & messageAccumulator , const byte * signature , size_t signatureLength ) const
2003-03-20 01:24:12 +00:00
{
PK_MessageAccumulatorBase & ma = static_cast < PK_MessageAccumulatorBase & > ( messageAccumulator ) ;
2005-01-20 04:19:35 +00:00
HashIdentifier id = GetHashIdentifier ( ) ;
const MessageEncodingInterface & encoding = GetMessageEncodingInterface ( ) ;
if ( MessageRepresentativeBitLength ( ) < encoding . MinRepresentativeBitLength ( id . second , ma . AccessHash ( ) . DigestSize ( ) ) )
throw PK_SignatureScheme : : KeyTooShort ( ) ;
2003-03-20 01:24:12 +00:00
ma . m_representative . New ( MessageRepresentativeLength ( ) ) ;
Integer x = GetTrapdoorFunctionInterface ( ) . ApplyFunction ( Integer ( signature , signatureLength ) ) ;
if ( x . BitCount ( ) > MessageRepresentativeBitLength ( ) )
2002-10-04 17:31:41 +00:00
x = Integer : : Zero ( ) ; // don't return false here to prevent timing attack
2003-03-20 01:24:12 +00:00
x . Encode ( ma . m_representative , ma . m_representative . size ( ) ) ;
}
bool TF_VerifierBase : : VerifyAndRestart ( PK_MessageAccumulator & messageAccumulator ) const
{
PK_MessageAccumulatorBase & ma = static_cast < PK_MessageAccumulatorBase & > ( messageAccumulator ) ;
2005-01-20 04:19:35 +00:00
HashIdentifier id = GetHashIdentifier ( ) ;
const MessageEncodingInterface & encoding = GetMessageEncodingInterface ( ) ;
if ( MessageRepresentativeBitLength ( ) < encoding . MinRepresentativeBitLength ( id . second , ma . AccessHash ( ) . DigestSize ( ) ) )
throw PK_SignatureScheme : : KeyTooShort ( ) ;
bool result = encoding . VerifyMessageRepresentative (
ma . AccessHash ( ) , id , ma . m_empty , ma . m_representative , MessageRepresentativeBitLength ( ) ) ;
2003-03-20 01:24:12 +00:00
ma . m_empty = true ;
return result ;
}
DecodingResult TF_VerifierBase : : RecoverAndRestart ( byte * recoveredMessage , PK_MessageAccumulator & messageAccumulator ) const
{
PK_MessageAccumulatorBase & ma = static_cast < PK_MessageAccumulatorBase & > ( messageAccumulator ) ;
2005-01-20 04:19:35 +00:00
HashIdentifier id = GetHashIdentifier ( ) ;
const MessageEncodingInterface & encoding = GetMessageEncodingInterface ( ) ;
if ( MessageRepresentativeBitLength ( ) < encoding . MinRepresentativeBitLength ( id . second , ma . AccessHash ( ) . DigestSize ( ) ) )
throw PK_SignatureScheme : : KeyTooShort ( ) ;
DecodingResult result = encoding . RecoverMessageFromRepresentative (
ma . AccessHash ( ) , id , ma . m_empty , ma . m_representative , MessageRepresentativeBitLength ( ) , recoveredMessage ) ;
2003-03-20 01:24:12 +00:00
ma . m_empty = true ;
return result ;
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
DecodingResult TF_DecryptorBase : : Decrypt ( RandomNumberGenerator & rng , const byte * ciphertext , size_t ciphertextLength , byte * plaintext , const NameValuePairs & parameters ) const
2002-10-04 17:31:41 +00:00
{
2010-06-18 01:18:22 +00:00
if ( ciphertextLength ! = FixedCiphertextLength ( ) )
throw InvalidArgument ( AlgorithmName ( ) + " : ciphertext length of " + IntToString ( ciphertextLength ) + " doesn't match the required length of " + IntToString ( FixedCiphertextLength ( ) ) + " for this key " ) ;
2002-10-04 17:31:41 +00:00
SecByteBlock paddedBlock ( PaddedBlockByteLength ( ) ) ;
2010-06-18 01:18:22 +00:00
Integer x = GetTrapdoorFunctionInterface ( ) . CalculateInverse ( rng , Integer ( ciphertext , ciphertextLength ) ) ;
2002-10-04 17:31:41 +00:00
if ( x . ByteCount ( ) > paddedBlock . size ( ) )
x = Integer : : Zero ( ) ; // don't return false here to prevent timing attack
x . Encode ( paddedBlock , paddedBlock . size ( ) ) ;
2003-07-16 01:53:45 +00:00
return GetMessageEncodingInterface ( ) . Unpad ( paddedBlock , PaddedBlockBitLength ( ) , plaintext , parameters ) ;
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
void TF_EncryptorBase : : Encrypt ( RandomNumberGenerator & rng , const byte * plaintext , size_t plaintextLength , byte * ciphertext , const NameValuePairs & parameters ) const
2002-10-04 17:31:41 +00:00
{
2003-07-16 01:53:45 +00:00
if ( plaintextLength > FixedMaxPlaintextLength ( ) )
2009-03-02 02:39:17 +00:00
{
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 " ) ;
}
2002-10-04 17:31:41 +00:00
SecByteBlock paddedBlock ( PaddedBlockByteLength ( ) ) ;
2003-07-16 01:53:45 +00:00
GetMessageEncodingInterface ( ) . Pad ( rng , plaintext , plaintextLength , paddedBlock , PaddedBlockBitLength ( ) , parameters ) ;
GetTrapdoorFunctionInterface ( ) . ApplyRandomizedFunction ( rng , Integer ( paddedBlock , paddedBlock . size ( ) ) ) . Encode ( ciphertext , FixedCiphertextLength ( ) ) ;
2002-10-04 17:31:41 +00:00
}
NAMESPACE_END
2003-07-04 00:17:37 +00:00
# endif