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());