5 #ifndef CRYPTOPP_IMPORTS
9 NAMESPACE_BEGIN(CryptoPP)
11 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)
15 word32 counter = counterStart;
16 while (sink->AvailableSize() > 0)
18 filter.Put(input, inputLength);
19 filter.PutWord32(counter++);
20 filter.Put(derivationParams, derivationParamsLength);
25 bool PK_DeterministicSignatureMessageEncodingMethod::VerifyMessageRepresentative(
27 byte *representative,
size_t representativeBitLength)
const
29 SecByteBlock computedRepresentative(BitsToBytes(representativeBitLength));
30 ComputeMessageRepresentative(
NullRNG(), NULL, 0, hash, hashIdentifier, messageEmpty, computedRepresentative, representativeBitLength);
31 return VerifyBufsEqual(representative, computedRepresentative, computedRepresentative.size());
34 bool PK_RecoverableSignatureMessageEncodingMethod::VerifyMessageRepresentative(
36 byte *representative,
size_t representativeBitLength)
const
38 SecByteBlock recoveredMessage(MaxRecoverableLength(representativeBitLength, hashIdentifier.second, hash.
DigestSize()));
40 hash, hashIdentifier, messageEmpty, representative, representativeBitLength, recoveredMessage);
41 return result.isValidCoding && result.messageLength == 0;
44 void TF_SignerBase::InputRecoverableMessage(
PK_MessageAccumulator &messageAccumulator,
const byte *recoverableMessage,
size_t recoverableMessageLength)
const
47 HashIdentifier
id = GetHashIdentifier();
48 const MessageEncodingInterface &encoding = GetMessageEncodingInterface();
50 if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(
id.second, ma.AccessHash().
DigestSize()))
53 size_t maxRecoverableLength = encoding.MaxRecoverableLength(MessageRepresentativeBitLength(), GetHashIdentifier().second, ma.AccessHash().
DigestSize());
55 if (maxRecoverableLength == 0)
56 {
throw NotImplemented(
"TF_SignerBase: this algorithm does not support messsage recovery or the key is too short");}
57 if (recoverableMessageLength > maxRecoverableLength)
58 throw InvalidArgument(
"TF_SignerBase: the recoverable message part is too long for the given key and algorithm");
60 ma.m_recoverableMessage.
Assign(recoverableMessage, recoverableMessageLength);
61 encoding.ProcessRecoverableMessage(
63 recoverableMessage, recoverableMessageLength,
64 NULL, 0, ma.m_semisignature);
70 HashIdentifier
id = GetHashIdentifier();
73 if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(
id.second, ma.AccessHash().
DigestSize()))
76 SecByteBlock representative(MessageRepresentativeLength());
77 encoding.ComputeMessageRepresentative(rng,
78 ma.m_recoverableMessage, ma.m_recoverableMessage.size(),
79 ma.AccessHash(), id, ma.m_empty,
80 representative, MessageRepresentativeBitLength());
83 Integer r(representative, representative.size());
84 size_t signatureLength = SignatureLength();
85 GetTrapdoorFunctionInterface().CalculateRandomizedInverse(rng, r).
Encode(signature, signatureLength);
86 return signatureLength;
92 HashIdentifier
id = GetHashIdentifier();
95 if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(
id.second, ma.AccessHash().
DigestSize()))
98 ma.m_representative.
New(MessageRepresentativeLength());
99 Integer x = GetTrapdoorFunctionInterface().ApplyFunction(
Integer(signature, signatureLength));
100 if (x.
BitCount() > MessageRepresentativeBitLength())
102 x.
Encode(ma.m_representative, ma.m_representative.size());
108 HashIdentifier
id = GetHashIdentifier();
111 if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(
id.second, ma.AccessHash().
DigestSize()))
114 bool result = encoding.VerifyMessageRepresentative(
115 ma.AccessHash(), id, ma.m_empty, ma.m_representative, MessageRepresentativeBitLength());
123 HashIdentifier
id = GetHashIdentifier();
126 if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(
id.second, ma.AccessHash().
DigestSize()))
129 DecodingResult result = encoding.RecoverMessageFromRepresentative(
130 ma.AccessHash(), id, ma.m_empty, ma.m_representative, MessageRepresentativeBitLength(), recoveredMessage);
137 if (ciphertextLength != FixedCiphertextLength())
138 throw InvalidArgument(
AlgorithmName() +
": ciphertext length of " + IntToString(ciphertextLength) +
" doesn't match the required length of " + IntToString(FixedCiphertextLength()) +
" for this key");
141 Integer x = GetTrapdoorFunctionInterface().CalculateInverse(rng,
Integer(ciphertext, ciphertextLength));
144 x.
Encode(paddedBlock, paddedBlock.size());
145 return GetMessageEncodingInterface().Unpad(paddedBlock, PaddedBlockBitLength(), plaintext, parameters);
150 if (plaintextLength > FixedMaxPlaintextLength())
152 if (FixedMaxPlaintextLength() < 1)
155 throw InvalidArgument(
AlgorithmName() +
": message length of " + IntToString(plaintextLength) +
" exceeds the maximum of " + IntToString(FixedMaxPlaintextLength()) +
" for this public key");
159 GetMessageEncodingInterface().Pad(rng, plaintext, plaintextLength, paddedBlock, PaddedBlockBitLength(), parameters);
160 GetTrapdoorFunctionInterface().ApplyRandomizedFunction(rng,
Integer(paddedBlock, paddedBlock.size())).
Encode(ciphertext, FixedCiphertextLength());
exception thrown when an invalid argument is detected
bool VerifyAndRestart(PK_MessageAccumulator &messageAccumulator) const
check whether messageAccumulator contains a valid signature and message, and restart messageAccumulat...
This file contains helper classes/functions for implementing public key algorithms.
DecodingResult RecoverAndRestart(byte *recoveredMessage, PK_MessageAccumulator &recoveryAccumulator) const
recover a message from its signature
interface for message encoding method for public key signature schemes
a block of memory allocated using A
interface for random number generators
void New(size_type newSize)
change size, without preserving contents
void Encrypt(RandomNumberGenerator &rng, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs ¶meters=g_nullNameValuePairs) const
encrypt a byte string
unsigned int BitCount() const
number of significant bits = floor(log2(abs(*this))) + 1
Copy input to a memory buffer.
used to return decoding results
Xor input to a memory buffer.
exception thrown by a class if a non-implemented method is called
Filter Wrapper for HashTransformation.
key too short exception, may be thrown by any function in this class if the private or public key is ...
size_t SignAndRestart(RandomNumberGenerator &rng, PK_MessageAccumulator &messageAccumulator, byte *signature, bool restart=true) const
sign and restart messageAccumulator
multiple precision integer and basic arithmetics
void Assign(const T *t, size_type len)
set contents and size
virtual std::string AlgorithmName() const
returns name of this algorithm, not universally implemented yet
RandomNumberGenerator & NullRNG()
returns a reference that can be passed to functions that ask for a RNG but doesn't actually use it ...
void Encode(byte *output, size_t outputLen, Signedness=UNSIGNED) const
encode in big-endian format
interface for accumulating messages to be signed or verified
static const Integer & Zero()
avoid calling constructors for these frequently used integers
void InputSignature(PK_MessageAccumulator &messageAccumulator, const byte *signature, size_t signatureLength) const
input signature into a message accumulator
unsigned int ByteCount() const
number of significant bytes = ceiling(BitCount()/8)
interface for retrieving values given their names
DecodingResult Decrypt(RandomNumberGenerator &rng, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs ¶meters=g_nullNameValuePairs) const
decrypt a byte string, and return the length of plaintext