7 #ifndef CRYPTOPP_GFPCRYPT_H 8 #define CRYPTOPP_GFPCRYPT_H 12 #if CRYPTOPP_MSC_VERSION 13 # pragma warning(push) 14 # pragma warning(disable: 4189) 59 {SetModulusAndSubgroupGenerator(p, g); SetSubgroupOrder(ComputeGroupOrder(p)/2);}
65 void Initialize(
const Integer &p,
const Integer &q,
const Integer &
g)
66 {SetModulusAndSubgroupGenerator(p, g); SetSubgroupOrder(q);}
75 bool GetVoidValue(
const char *
name,
const std::type_info &valueType,
void *pValue)
const;
86 void EncodeElement(
bool reversible,
const Element &element,
byte *encoded)
const;
87 unsigned int GetEncodedElementSize(
bool reversible)
const;
89 Integer DecodeElement(
const byte *encoded,
bool checkForGroupMembership)
const;
92 Integer GetMaxExponent()
const;
95 OID GetAlgorithmID()
const;
97 virtual const Integer & GetModulus()
const =0;
98 virtual void SetModulusAndSubgroupGenerator(
const Integer &p,
const Integer &
g) =0;
101 {m_q = q; ParametersChanged();}
105 {
return modulus-(GetFieldType() == 1 ? 1 : -1);}
108 virtual int GetFieldType()
const =0;
109 virtual unsigned int GetDefaultSubgroupOrderSize(
unsigned int modulusSize)
const;
119 template <
class GROUP_PRECOMP,
class BASE_PRECOMP = DL_FixedBasePrecomputationImpl<
typename GROUP_PRECOMP::Element> >
125 typedef typename GROUP_PRECOMP::Element
Element;
131 {
return GetValueHelper<DL_GroupParameters_IntegerBased>(
this,
name, valueType, pValue).Assignable();}
134 {AssignFromHelper<DL_GroupParameters_IntegerBased>(
this,
source);}
145 {this->m_groupPrecomputation.SetModulus(p); this->m_gpc.SetBase(this->
GetGroupPrecomputation(), g); this->ParametersChanged();}
165 void SimultaneousExponentiate(
Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const;
170 return GetValueHelper<DL_GroupParameters_IntegerBased>(
this,
name, valueType, pValue).Assignable();
210 s = (kInv * (x*r +
e)) % q;
217 if (r>=q || r<1 || s>=q || s<1)
235 template <
class T,
class H>
251 static const byte zero = 0, one = 1;
258 BH = bits2octets(BH, q);
266 std::fill(V.
begin(), V.
begin()+H::DIGESTSIZE, one);
270 std::fill(K.begin(), K.begin()+H::DIGESTSIZE, zero);
273 m_hmac.SetKey(K, K.size());
274 m_hmac.Update(V, V.
size());
275 m_hmac.Update(&zero, 1);
276 m_hmac.Update(BX, BX.
size());
277 m_hmac.Update(BH, BH.size());
278 m_hmac.TruncatedFinal(K, K.size());
281 m_hmac.SetKey(K, K.size());
282 m_hmac.Update(V, V.
size());
283 m_hmac.TruncatedFinal(V, V.
size());
286 m_hmac.SetKey(K, K.size());
287 m_hmac.Update(V, V.
size());
288 m_hmac.Update(&one, 1);
289 m_hmac.Update(BX, BX.
size());
290 m_hmac.Update(BH, BH.size());
291 m_hmac.TruncatedFinal(K, K.size());
294 m_hmac.SetKey(K, K.size());
295 m_hmac.Update(V, V.
size());
296 m_hmac.TruncatedFinal(V, V.
size());
307 m_hmac.Update(V, V.
size());
308 m_hmac.TruncatedFinal(V, V.
size());
315 k = bits2int(temp, qlen);
320 m_hmac.Update(V, V.
size());
321 m_hmac.Update(&zero, 1);
322 m_hmac.TruncatedFinal(K, K.size());
324 m_hmac.SetKey(K, K.size());
325 m_hmac.Update(V, V.
size());
326 m_hmac.TruncatedFinal(V, V.
size());
336 inline unsigned int BitCount(
const byte* buffer,
size_t size)
const 338 unsigned int idx = 0;
339 while (idx < size && buffer[idx] == 0) { idx++; }
347 size_t blen = bits.
size()*8;
362 if (block.size() == rlen)
367 if (block.size() > rlen)
369 size_t offset = block.size() - rlen;
370 memcpy(t, block + offset, rlen);
374 size_t offset = rlen - block.size();
375 memset(t,
'\x00', offset);
376 memcpy(t + offset, block, rlen - offset);
388 return int2octets(b1.IsNegative() ? b2 : b1, q.
ByteCount());
415 s = (k * r -
e) * x % q;
422 if (r>=q || r<1 || s>=q || s<1)
462 if (r>=q || r<1 || s>=q)
484 {this->AccessGroupParameters().Initialize(params); this->SetPublicElement(y);}
491 {this->AccessGroupParameters().Initialize(p, g); this->SetPublicElement(y);}
499 {this->AccessGroupParameters().Initialize(p, q, g); this->SetPublicElement(y);}
503 {this->SetPublicElement(
Integer(bt));}
505 {this->GetPublicElement().DEREncode(bt);}
524 {this->GenerateRandomWithKeySize(rng, modulusBits);}
534 {this->GenerateRandom(rng,
MakeParameters(
"Modulus", p)(
"SubgroupGenerator", g));}
545 {this->GenerateRandom(rng,
MakeParameters(
"Modulus", p)(
"SubgroupOrder", q)(
"SubgroupGenerator", g));}
551 {this->AccessGroupParameters().Initialize(params); this->SetPrivateExponent(x);}
558 {this->AccessGroupParameters().Initialize(p, g); this->SetPrivateExponent(x);}
566 {this->AccessGroupParameters().Initialize(p, q, g); this->SetPrivateExponent(x);}
592 template <
class BASE>
607 this->AccessGroupParameters().Initialize(v1, v1/2, v2);
608 this->SetPublicElement(v3);
613 this->AccessGroupParameters().Initialize(v1, v2, v3);
614 this->SetPublicElement(v4);
623 this->GetGroupParameters().GetModulus().DEREncode(seq);
624 if (this->GetGroupParameters().GetCofactor() != 2)
625 this->GetGroupParameters().GetSubgroupOrder().DEREncode(seq);
626 this->GetGroupParameters().GetGenerator().DEREncode(seq);
627 this->GetPublicElement().DEREncode(seq);
637 template <
class BASE>
653 this->AccessGroupParameters().Initialize(v1, v1/2, v2);
654 this->SetPrivateExponent(v4 % (v1/2));
659 this->AccessGroupParameters().Initialize(v1, v2, v3);
660 this->SetPrivateExponent(v5);
669 this->GetGroupParameters().GetModulus().DEREncode(seq);
670 if (this->GetGroupParameters().GetCofactor() != 2)
671 this->GetGroupParameters().GetSubgroupOrder().DEREncode(seq);
672 this->GetGroupParameters().GetGenerator().DEREncode(seq);
673 this->GetGroupParameters().ExponentiateBase(this->GetPrivateExponent()).DEREncode(seq);
674 this->GetPrivateExponent().DEREncode(seq);
686 DL_SignatureKeys_GFP,
687 DL_Algorithm_GDSA<Integer>,
688 DL_SignatureMessageEncodingMethod_DSA,
699 DL_SignatureKeys_GFP,
700 DL_Algorithm_NR<Integer>,
701 DL_SignatureMessageEncodingMethod_NR,
722 {
return pbits >= MIN_PRIME_LENGTH && pbits <= MAX_PRIME_LENGTH && pbits % PRIME_LENGTH_MULTIPLE == 0;}
724 enum {MIN_PRIME_LENGTH = 1024, MAX_PRIME_LENGTH = 3072, PRIME_LENGTH_MULTIPLE = 1024};
748 DL_Algorithm_GDSA<Integer>,
749 DL_SignatureMessageEncodingMethod_DSA,
764 DL_SignatureKeys_GFP,
765 DL_Algorithm_DSA_RFC6979<Integer, H>,
766 DL_SignatureMessageEncodingMethod_DSA,
794 template <
class MAC,
bool DHAES_MODE,
bool LABEL_OCTETS=false>
802 {
return plaintextLength +
static_cast<size_t>(MAC::DIGESTSIZE);}
804 {
return plaintextLength +
static_cast<size_t>(MAC::DIGESTSIZE);}
810 const byte *cipherKey = NULL, *macKey = NULL;
814 cipherKey = key + MAC::DEFAULT_KEYLENGTH;
819 macKey = key + plaintextLength;
823 parameters.
GetValue(Name::EncodingParameters(), encodingParameters);
826 xorbuf(ciphertext, plaintext, cipherKey, plaintextLength);
829 mac.Update(ciphertext, plaintextLength);
830 mac.Update(encodingParameters.
begin(), encodingParameters.
size());
837 mac.Final(ciphertext + plaintextLength);
841 size_t plaintextLength = GetMaxSymmetricPlaintextLength(ciphertextLength);
842 const byte *cipherKey, *macKey;
846 cipherKey = key + MAC::DEFAULT_KEYLENGTH;
851 macKey = key + plaintextLength;
855 parameters.
GetValue(Name::EncodingParameters(), encodingParameters);
858 mac.Update(ciphertext, plaintextLength);
859 mac.Update(encodingParameters.
begin(), encodingParameters.
size());
866 if (!mac.Verify(ciphertext + plaintextLength))
870 xorbuf(plaintext, ciphertext, cipherKey, plaintextLength);
877 template <
class T,
bool DHAES_MODE,
class KDF>
890 params.
EncodeElement(
true, ephemeralPublicKey, agreedSecret);
900 parameters.
GetValue(Name::KeyDerivationParameters(), derivationParameters);
901 KDF::DeriveKey(derivedKey, derivedLength, agreedSecret, agreedSecret.
size(), derivationParameters.
begin(), derivationParameters.
size());
939 template <
class HASH = SHA1,
class COFACTOR_OPTION = NoCofactorMultiplication,
bool DHAES_MODE = true,
bool LABEL_OCTETS=false>
943 DL_KeyAgreementAlgorithm_DH<Integer, COFACTOR_OPTION>,
944 DL_KeyDerivationAlgorithm_P1363<Integer, DHAES_MODE, P1363_KDF2<HASH> >,
945 DL_EncryptionAlgorithm_Xor<HMAC<HASH>, DHAES_MODE, LABEL_OCTETS>,
953 #if CRYPTOPP_MSC_VERSION 954 # pragma warning(pop) Used to pass byte array input as part of a NameValuePairs object.
void Initialize(RandomNumberGenerator &rng, const Integer &p, const Integer &q, const Integer &g)
Create a private key.
static std::string CRYPTOPP_API StaticAlgorithmName()
bool IsIdentity(const Integer &element) const
Determines if an element is an identity.
Discrete Log Integrated Encryption Scheme.
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Classes for working with NameValuePairs.
Utility functions for the Crypto++ library.
#define CRYPTOPP_STATIC_CONSTEXPR
DL_PrivateKey_GFP< GroupParameters > PrivateKey
void SetModulusAndSubgroupGenerator(const Integer &p, const Integer &g)
static bool CRYPTOPP_API IsValidPrimeLength(unsigned int pbits)
size_t GetMaxSymmetricPlaintextLength(size_t ciphertextLength) const
void Initialize(const DL_GroupParameters_IntegerBased ¶ms, const Integer &y)
Initialize a public key over GF(p)
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
DL_GroupParameters_GFP GroupParameters
DL_PublicKey_GFP< DL_GroupParameters_DSA > PublicKey
Integer GetGroupOrder() const
Retrieves the order of the group.
size_t BitsToBytes(size_t bitCount)
Returns the number of 8-bit bytes or octets required for the specified number of bits.
Interface for deterministic signers.
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
Create a private key.
virtual Element CascadeExponentiateBaseAndPublicElement(const Integer &baseExp, const Integer &publicExp) const
void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock=NULL)
Access a block of memory.
size_t GetSymmetricCiphertextLength(size_t plaintextLength) const
void Initialize(const Integer &p, const Integer &g)
Initialize a group parameters over integers.
#define NAMESPACE_BEGIN(x)
Interface for Discrete Log (DL) group parameters.
#define CRYPTOPP_DLL_TEMPLATE_CLASS
P1363 based XOR Encryption Method.
Converts an enumeration to a type suitable for use as a template parameter.
const Integer & GetModulus() const
SecByteBlock bits2octets(const SecByteBlock &in, const Integer &q) const
void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs ¶meters) const
Abstract base classes that provide a uniform interface to this library.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
bool IsDeterministic() const
SecByteBlock int2octets(const Integer &val, size_t rlen) const
virtual Integer ConvertElementToInteger(const Element &element) const =0
Converts an element to an Integer.
bool ParameterSupported(const char *name) const
size_type size() const
Provides the count of elements in the SecBlock.
void SetSubgroupOrder(const Integer &q)
Interface for key derivation algorithms used in DL cryptosystems.
Classes for automatic resource management.
size_t size() const
Length of the memory block.
Library configuration file.
bool FastSubgroupCheckAvailable() const
Interface for random number generators.
void Initialize(RandomNumberGenerator &rng, unsigned int pbits)
Create a group parameters over integers.
virtual ~DL_EncryptionAlgorithm_Xor()
size_t MinEncodedSize(Signedness sign=UNSIGNED) const
Minimum number of bytes to encode this integer.
void New(size_type newSize)
Change size without preserving contents.
bool operator!=(const DL_GroupParameters_IntegerBasedImpl< GROUP_PRECOMP, BASE_PRECOMP > &rhs) const
virtual ~DL_PublicKey_GFP()
DL_PublicKey_GFP< GroupParameters > PublicKey
Discrete Log (DL) encryption scheme.
virtual ~DL_Algorithm_DSA_RFC6979()
const byte * begin() const
Pointer to the first byte in the memory block.
static const Integer &CRYPTOPP_API One()
Integer representing 1.
bool Verify(const DL_GroupParameters< T > ¶ms, const DL_PublicKey< T > &publicKey, const Integer &e, const Integer &r, const Integer &s) const
Interface for Discrete Log (DL) public keys.
CRYPTOPP_STATIC_CONSTEXPR const char *CRYPTOPP_API StaticAlgorithmName()
virtual const Element & GetSubgroupGenerator() const
Retrieves the subgroup generator.
bool operator==(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
void Initialize(const Integer &p, const Integer &q, const Integer &g)
Initialize a group parameters over integers.
Discrete Log (DL) signature scheme.
void Initialize(RandomNumberGenerator &rng, const Integer &p, const Integer &g)
Create a private key.
Integer ConvertElementToInteger(const Element &element) const
Converts an element to an Integer.
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
DL_FixedBasePrecomputation< Element > & AccessBasePrecomputation()
Retrieves the group precomputation.
Returns a decoding results.
bool ParameterSupported(const char *name) const
virtual ~DL_Algorithm_GDSA_ISO15946()
DSA2< SHA1 > DSA
DSA with SHA-1, typedef'd for backwards compatibility.
Classes for HMAC message authentication codes.
bool GetValue(const char *name, T &value) const
Get a named value.
bool Verify(const DL_GroupParameters< T > ¶ms, const DL_PublicKey< T > &publicKey, const Integer &e, const Integer &r, const Integer &s) const
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Interface for Elgamal-like signature algorithms.
void Initialize(const Integer &p, const Integer &g, const Integer &y)
Initialize a public key over GF(p)
DL_GroupParameters_IntegerBasedImpl< GROUP_PRECOMP, BASE_PRECOMP > ThisClass
Discrete Log (DL) signing/verification keys in GF(p) groups.
DL_GroupParameters_GFP_DefaultSafePrime GroupParameters
DSA deterministic signature scheme.
Discrete Log (DL) encryption/decryption keys in GF(p) groups.
const Integer & GetSubgroupOrder() const
Retrieves the subgroup order.
void Sign(const DL_GroupParameters< T > ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
Multiple precision integer with arithmetic operations.
CRYPTOPP_STATIC_CONSTEXPR const char *CRYPTOPP_API StaticAlgorithmName()
T1 SaturatingSubtract(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 0.
Integer-based GroupParameters default implementation.
size_t GetSymmetricKeyLength(size_t plaintextLength) const
GROUP_PRECOMP::Element Element
void Sign(const DL_GroupParameters< T > ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
unsigned long long word64
virtual ~DL_KeyDerivationAlgorithm_P1363()
const DL_GroupPrecomputation< Element > & GetGroupPrecomputation() const
Retrieves the group precomputation.
const DL_FixedBasePrecomputation< Element > & GetBasePrecomputation() const
Retrieves the group precomputation.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
static std::string CRYPTOPP_API StaticAlgorithmName()
Classes and functions for working with ANS.1 objects.
Classes for SHA-1 and SHA-2 family of message digests.
void Initialize(const Integer &p, const Integer &q, const Integer &g, const Integer &x)
Initialize a private key over GF(p)
void Initialize(const DL_GroupParameters_IntegerBased ¶ms)
Initialize a group parameters over integers.
#define CRYPTOPP_NO_VTABLE
Discrete Log (DL) public key in GF(p) groups.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
virtual ~DL_GroupParameters_IntegerBased()
bool Verify(const DL_GroupParameters< T > ¶ms, const DL_PublicKey< T > &publicKey, const Integer &e, const Integer &r, const Integer &s) const
Integer ComputeGroupOrder(const Integer &modulus) const
DSA signature algorithm based on RFC 6979.
uint8_t const size_t const size
void * memcpy(void *a, const void *b, size_t c)
Discrete Log (DL) private key in GF(p) groups.
#define CRYPTOPP_UNUSED(x)
static std::string CRYPTOPP_API StaticAlgorithmName()
void Initialize(const DL_GroupParameters_IntegerBased ¶ms, const Integer &x)
Initialize a private key over GF(p)
Integer GenerateRandom(const Integer &x, const Integer &q, const Integer &e) const
virtual ~DL_GroupParameters_DSA()
virtual void EncodeElement(bool reversible, const Element &element, byte *encoded) const =0
Encodes the element.
Integer InverseMod(const Integer &n) const
calculate multiplicative inverse of *this mod n
virtual unsigned int GetEncodedElementSize(bool reversible) const =0
Retrieves the encoded element's size.
virtual ~DL_Algorithm_GDSA()
void Derive(const DL_GroupParameters< T > ¶ms, byte *derivedKey, size_t derivedLength, const T &agreedElement, const T &ephemeralPublicKey, const NameValuePairs ¶meters) const
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Multiple precision integer with arithmetic operations.
void DEREncodePublicKey(BufferedTransformation &bt) const
encode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header ...
void Sign(const DL_GroupParameters< T > ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
virtual ~DL_GroupParameters_GFP()
std::vector< char * > parameters
virtual ~DL_GroupParameters_GFP_DefaultSafePrime()
CRYPTOPP_STATIC_CONSTEXPR const char *CRYPTOPP_API StaticAlgorithmName()
const Integer & GetGenerator() const
DL_PublicKey_GFP< GroupParameters > PublicKey
Interface for symmetric encryption algorithms used in DL cryptosystems.
Base implementation of Discrete Log (DL) group parameters.
DL_PrivateKey_WithSignaturePairwiseConsistencyTest< DL_PrivateKey_GFP< DL_GroupParameters_DSA >, DSA2< SHA > > PrivateKey
void BERDecodePublicKey(BufferedTransformation &bt, bool, size_t)
decode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header ...
virtual const Integer & GetModulus() const =0
DL_PrivateKey_GFP< GroupParameters > PrivateKey
unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const
DL_GroupParameters_IntegerBased ThisClass
static std::string CRYPTOPP_API StaticAlgorithmNamePrefix()
virtual Element ExponentiateBase(const Integer &exponent) const
Retrieves the subgroup generator.
German Digital Signature Algorithm.
void Initialize(const Integer &p, const Integer &q, const Integer &g, const Integer &y)
Initialize a public key over GF(p)
Integer-based GroupParameters specialization.
CRYPTOPP_STATIC_CONSTEXPR const char *CRYPTOPP_API StaticAlgorithmName()
bool IsProbabilistic() const
Encode and decode ASN.1 objects with additional information.
void Initialize(const Integer &p, const Integer &g, const Integer &x)
Initialize a private key over GF(p)
DecodingResult SymmetricDecrypt(const byte *key, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs ¶meters) const
NoCofactorMultiplication DefaultCofactorOption
Integer bits2int(const SecByteBlock &bits, size_t qlen) const
bool operator==(const DL_GroupParameters_IntegerBasedImpl< GROUP_PRECOMP, BASE_PRECOMP > &rhs) const
virtual ~DL_GroupParameters_IntegerBasedImpl()
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
unsigned int ByteCount() const
Determines the number of bytes required to represent the Integer.
virtual ~DL_PrivateKey_GFP()
Interface for retrieving values given their names.
virtual ~DL_Algorithm_NR()
virtual const Integer & GetSubgroupOrder() const =0
Retrieves the subgroup order.