Fabcoin Core  0.16.2
P2P Digital Currency
pubkey.cpp
Go to the documentation of this file.
1 // pubkey.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "config.h"
5 
6 #ifndef CRYPTOPP_IMPORTS
7 
8 #include "pubkey.h"
9 #include "integer.h"
10 #include "filters.h"
11 
13 
14 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 {
16  ArraySink *sink;
17  HashFilter filter(hash, sink = mask ? new ArrayXorSink(output, outputLength) : new ArraySink(output, outputLength));
18  word32 counter = counterStart;
19  while (sink->AvailableSize() > 0)
20  {
21  filter.Put(input, inputLength);
22  filter.PutWord32(counter++);
23  filter.Put(derivationParams, derivationParamsLength);
24  filter.MessageEnd();
25  }
26 }
27 
29  HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
30  byte *representative, size_t representativeBitLength) const
31 {
32  SecByteBlock computedRepresentative(BitsToBytes(representativeBitLength));
33  ComputeMessageRepresentative(NullRNG(), NULL, 0, hash, hashIdentifier, messageEmpty, computedRepresentative, representativeBitLength);
34  return VerifyBufsEqual(representative, computedRepresentative, computedRepresentative.size());
35 }
36 
38  HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
39  byte *representative, size_t representativeBitLength) const
40 {
41  SecByteBlock recoveredMessage(MaxRecoverableLength(representativeBitLength, hashIdentifier.second, hash.DigestSize()));
43  hash, hashIdentifier, messageEmpty, representative, representativeBitLength, recoveredMessage);
44  return result.isValidCoding && result.messageLength == 0;
45 }
46 
47 void TF_SignerBase::InputRecoverableMessage(PK_MessageAccumulator &messageAccumulator, const byte *recoverableMessage, size_t recoverableMessageLength) const
48 {
49  PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);
50  HashIdentifier id = GetHashIdentifier();
51  const MessageEncodingInterface &encoding = GetMessageEncodingInterface();
52 
53  if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(id.second, ma.AccessHash().DigestSize()))
55 
56  size_t maxRecoverableLength = encoding.MaxRecoverableLength(MessageRepresentativeBitLength(), GetHashIdentifier().second, ma.AccessHash().DigestSize());
57 
58  if (maxRecoverableLength == 0)
59  {throw NotImplemented("TF_SignerBase: this algorithm does not support message recovery or the key is too short");}
60  if (recoverableMessageLength > maxRecoverableLength)
61  throw InvalidArgument("TF_SignerBase: the recoverable message part is too long for the given key and algorithm");
62 
63  ma.m_recoverableMessage.Assign(recoverableMessage, recoverableMessageLength);
65  ma.AccessHash(),
66  recoverableMessage, recoverableMessageLength,
67  NULL, 0, ma.m_semisignature);
68 }
69 
70 size_t TF_SignerBase::SignAndRestart(RandomNumberGenerator &rng, PK_MessageAccumulator &messageAccumulator, byte *signature, bool restart) const
71 {
72  CRYPTOPP_UNUSED(restart);
73 
74  PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);
75  HashIdentifier id = GetHashIdentifier();
76  const MessageEncodingInterface &encoding = GetMessageEncodingInterface();
77 
78  if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(id.second, ma.AccessHash().DigestSize()))
80 
81  SecByteBlock representative(MessageRepresentativeLength());
82  encoding.ComputeMessageRepresentative(rng,
84  ma.AccessHash(), id, ma.m_empty,
85  representative, MessageRepresentativeBitLength());
86  ma.m_empty = true;
87 
88  Integer r(representative, representative.size());
89  size_t signatureLength = SignatureLength();
90  GetTrapdoorFunctionInterface().CalculateRandomizedInverse(rng, r).Encode(signature, signatureLength);
91  return signatureLength;
92 }
93 
94 void TF_VerifierBase::InputSignature(PK_MessageAccumulator &messageAccumulator, const byte *signature, size_t signatureLength) const
95 {
96  PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);
97  HashIdentifier id = GetHashIdentifier();
98  const MessageEncodingInterface &encoding = GetMessageEncodingInterface();
99 
100  if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(id.second, ma.AccessHash().DigestSize()))
102 
103  ma.m_representative.New(MessageRepresentativeLength());
104  Integer x = GetTrapdoorFunctionInterface().ApplyFunction(Integer(signature, signatureLength));
105  if (x.BitCount() > MessageRepresentativeBitLength())
106  x = Integer::Zero(); // don't return false here to prevent timing attack
108 }
109 
111 {
112  PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);
113  HashIdentifier id = GetHashIdentifier();
114  const MessageEncodingInterface &encoding = GetMessageEncodingInterface();
115 
116  if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(id.second, ma.AccessHash().DigestSize()))
118 
119  bool result = encoding.VerifyMessageRepresentative(
120  ma.AccessHash(), id, ma.m_empty, ma.m_representative, MessageRepresentativeBitLength());
121  ma.m_empty = true;
122  return result;
123 }
124 
126 {
127  PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);
128  HashIdentifier id = GetHashIdentifier();
129  const MessageEncodingInterface &encoding = GetMessageEncodingInterface();
130 
131  if (MessageRepresentativeBitLength() < encoding.MinRepresentativeBitLength(id.second, ma.AccessHash().DigestSize()))
133 
135  ma.AccessHash(), id, ma.m_empty, ma.m_representative, MessageRepresentativeBitLength(), recoveredMessage);
136  ma.m_empty = true;
137  return result;
138 }
139 
140 DecodingResult TF_DecryptorBase::Decrypt(RandomNumberGenerator &rng, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs &parameters) const
141 {
142  if (ciphertextLength != FixedCiphertextLength())
143  throw InvalidArgument(AlgorithmName() + ": ciphertext length of " + IntToString(ciphertextLength) + " doesn't match the required length of " + IntToString(FixedCiphertextLength()) + " for this key");
144 
145  SecByteBlock paddedBlock(PaddedBlockByteLength());
146  Integer x = GetTrapdoorFunctionInterface().CalculateInverse(rng, Integer(ciphertext, ciphertextLength));
147  if (x.ByteCount() > paddedBlock.size())
148  x = Integer::Zero(); // don't return false here to prevent timing attack
149  x.Encode(paddedBlock, paddedBlock.size());
150  return GetMessageEncodingInterface().Unpad(paddedBlock, PaddedBlockBitLength(), plaintext, parameters);
151 }
152 
153 void TF_EncryptorBase::Encrypt(RandomNumberGenerator &rng, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs &parameters) const
154 {
155  if (plaintextLength > FixedMaxPlaintextLength())
156  {
157  if (FixedMaxPlaintextLength() < 1)
158  throw InvalidArgument(AlgorithmName() + ": this key is too short to encrypt any messages");
159  else
160  throw InvalidArgument(AlgorithmName() + ": message length of " + IntToString(plaintextLength) + " exceeds the maximum of " + IntToString(FixedMaxPlaintextLength()) + " for this public key");
161  }
162 
163  SecByteBlock paddedBlock(PaddedBlockByteLength());
164  GetMessageEncodingInterface().Pad(rng, plaintext, plaintextLength, paddedBlock, PaddedBlockBitLength(), parameters);
165  GetTrapdoorFunctionInterface().ApplyRandomizedFunction(rng, Integer(paddedBlock, paddedBlock.size())).Encode(ciphertext, FixedCiphertextLength());
166 }
167 
169 
170 #endif
An invalid argument was detected.
Definition: cryptlib.h:184
bool VerifyAndRestart(PK_MessageAccumulator &messageAccumulator) const
Check whether messageAccumulator contains a valid signature and message, and restart messageAccumulat...
Definition: pubkey.cpp:110
uint8_t byte
Definition: Common.h:57
void InputRecoverableMessage(PK_MessageAccumulator &messageAccumulator, const byte *recoverableMessage, size_t recoverableMessageLength) const
Input a recoverable message to an accumulator.
Definition: pubkey.cpp:47
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
Definition: integer.cpp:3369
virtual bool VerifyMessageRepresentative(HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, byte *representative, size_t representativeBitLength) const =0
size_t BitsToBytes(size_t bitCount)
Returns the number of 8-bit bytes or octets required for the specified number of bits.
Definition: misc.h:749
virtual DecodingResult RecoverMessageFromRepresentative(HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, byte *representative, size_t representativeBitLength, byte *recoveredMessage) const
Definition: pubkey.h:370
DecodingResult RecoverAndRestart(byte *recoveredMessage, PK_MessageAccumulator &recoveryAccumulator) const
Recover a message from its signature.
Definition: pubkey.cpp:125
virtual size_t MinRepresentativeBitLength(size_t hashIdentifierLength, size_t digestLength) const
Definition: pubkey.h:327
SecByteBlock m_recoverableMessage
Definition: pubkey.h:467
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
virtual void ProcessRecoverableMessage(HashTransformation &hash, const byte *recoverableMessage, size_t recoverableMessageLength, const byte *presignature, size_t presignatureLength, SecByteBlock &semisignature) const
Definition: pubkey.h:350
Interface for message encoding method for public key signature schemes.
Definition: pubkey.h:322
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:524
virtual void ComputeMessageRepresentative(RandomNumberGenerator &rng, const byte *recoverableMessage, size_t recoverableMessageLength, HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, byte *representative, size_t representativeBitLength) const =0
Library configuration file.
Interface for random number generators.
Definition: cryptlib.h:1188
size_t messageLength
Recovered message length if isValidCoding is true, undefined otherwise.
Definition: cryptlib.h:261
void New(size_type newSize)
Change size without preserving contents.
Definition: secblock.h:647
virtual size_t MaxRecoverableLength(size_t representativeBitLength, size_t hashIdentifierLength, size_t digestLength) const
Definition: pubkey.h:329
void Encrypt(RandomNumberGenerator &rng, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs &parameters=g_nullNameValuePairs) const
Encrypt a byte string.
Definition: pubkey.cpp:153
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
Definition: integer.cpp:3305
bool MessageEnd(int propagation=-1, bool blocking=true)
Signals the end of messages to the object.
Definition: cryptlib.h:1434
Copy input to a memory buffer.
Definition: filters.h:1101
Returns a decoding results.
Definition: cryptlib.h:238
#define x(i)
Xor input to a memory buffer.
Definition: filters.h:1138
A method was called which was not implemented.
Definition: cryptlib.h:205
Filter wrapper for HashTransformation.
Definition: filters.h:521
Exception throw when the private or public key is too short to sign or verify.
Definition: cryptlib.h:2448
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1376
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)
Definition: pubkey.cpp:14
size_t SignAndRestart(RandomNumberGenerator &rng, PK_MessageAccumulator &messageAccumulator, byte *signature, bool restart=true) const
Sign and restart messageAccumulator.
Definition: pubkey.cpp:70
Interface for message encoding method for public key signature schemes.
Definition: pubkey.h:454
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
Definition: secblock.h:544
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
size_t AvailableSize()
Provides the size remaining in the Sink.
Definition: filters.h:1120
virtual HashTransformation & AccessHash()=0
Implementation of BufferedTransformation&#39;s attachment interface.
Interface for accumulating messages to be signed or verified.
Definition: cryptlib.h:2510
size_t PutWord32(word32 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true)
Input a 32-bit word for processing.
Definition: cryptlib.cpp:719
bool VerifyMessageRepresentative(HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, byte *representative, size_t representativeBitLength) const
Definition: pubkey.cpp:37
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
RandomNumberGenerator & NullRNG()
Random Number Generator that does not produce random numbers.
Definition: cryptlib.cpp:402
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:930
SecByteBlock m_representative
Definition: pubkey.h:467
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:539
Multiple precision integer with arithmetic operations.
static const Integer &CRYPTOPP_API Zero()
Integer representing 0.
Definition: integer.cpp:3027
#define NAMESPACE_END
Definition: config.h:201
std::vector< char * > parameters
Definition: boostTest.cpp:46
bool isValidCoding
Flag to indicate the decoding is valid.
Definition: cryptlib.h:259
std::pair< const byte *, unsigned int > HashIdentifier
Definition: pubkey.h:314
bool VerifyMessageRepresentative(HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, byte *representative, size_t representativeBitLength) const
Definition: pubkey.cpp:28
unsigned int word32
Definition: config.h:231
void InputSignature(PK_MessageAccumulator &messageAccumulator, const byte *signature, size_t signatureLength) const
Input signature into a message accumulator.
Definition: pubkey.cpp:94
SecByteBlock m_semisignature
Definition: pubkey.h:467
unsigned int ByteCount() const
Determines the number of bytes required to represent the Integer.
Definition: integer.cpp:3296
bool VerifyBufsEqual(const byte *buf, const byte *mask, size_t count)
Performs a near constant-time comparison of two equally sized buffers.
Definition: misc.cpp:96
Interface for retrieving values given their names.
Definition: cryptlib.h:279
DecodingResult Decrypt(RandomNumberGenerator &rng, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs &parameters=g_nullNameValuePairs) const
Decrypt a byte string.
Definition: pubkey.cpp:140