📄 gfpcrypt.h
字号:
#ifndef CRYPTOPP_GFPCRYPT_H#define CRYPTOPP_GFPCRYPT_H/** \file Implementation of schemes based on DL over GF(p)*/#include "pubkey.h"#include "modexppc.h"#include "sha.h"#include "algparam.h"#include "asn.h"#include "smartptr.h"#include "hmac.h"#include <limits.h>NAMESPACE_BEGIN(CryptoPP)CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters<Integer>;//! _class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE DL_GroupParameters_IntegerBased : public ASN1CryptoMaterial<DL_GroupParameters<Integer> >{ typedef DL_GroupParameters_IntegerBased ThisClass; public: void Initialize(const DL_GroupParameters_IntegerBased ¶ms) {Initialize(params.GetModulus(), params.GetSubgroupOrder(), params.GetSubgroupGenerator());} void Initialize(RandomNumberGenerator &rng, unsigned int pbits) {GenerateRandom(rng, MakeParameters("ModulusSize", (int)pbits));} void Initialize(const Integer &p, const Integer &g) {SetModulusAndSubgroupGenerator(p, g); SetSubgroupOrder(ComputeGroupOrder(p)/2);} void Initialize(const Integer &p, const Integer &q, const Integer &g) {SetModulusAndSubgroupGenerator(p, g); SetSubgroupOrder(q);} // ASN1Object interface void BERDecode(BufferedTransformation &bt); void DEREncode(BufferedTransformation &bt) const; // GeneratibleCryptoMaterial interface /*! parameters: (ModulusSize, SubgroupOrderSize (optional)) */ void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg); bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const; void AssignFrom(const NameValuePairs &source); // DL_GroupParameters const Integer & GetSubgroupOrder() const {return m_q;} Integer GetGroupOrder() const {return GetFieldType() == 1 ? GetModulus()-Integer::One() : GetModulus()+Integer::One();} bool ValidateGroup(RandomNumberGenerator &rng, unsigned int level) const; bool ValidateElement(unsigned int level, const Integer &element, const DL_FixedBasePrecomputation<Integer> *precomp) const; bool FastSubgroupCheckAvailable() const {return GetCofactor() == 2;} void EncodeElement(bool reversible, const Element &element, byte *encoded) const {element.Encode(encoded, GetModulus().ByteCount());} unsigned int GetEncodedElementSize(bool reversible) const {return GetModulus().ByteCount();} Integer DecodeElement(const byte *encoded, bool checkForGroupMembership) const; Integer ConvertElementToInteger(const Element &element) const {return element;} Integer GetMaxExponent() const; static std::string CRYPTOPP_API StaticAlgorithmNamePrefix() {return "";} OID GetAlgorithmID() const; virtual const Integer & GetModulus() const =0; virtual void SetModulusAndSubgroupGenerator(const Integer &p, const Integer &g) =0; void SetSubgroupOrder(const Integer &q) {m_q = q; ParametersChanged();}protected: Integer ComputeGroupOrder(const Integer &modulus) const {return modulus-(GetFieldType() == 1 ? 1 : -1);} // GF(p) = 1, GF(p^2) = 2 virtual int GetFieldType() const =0; virtual unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const;private: Integer m_q;};//! _template <class GROUP_PRECOMP, class BASE_PRECOMP = DL_FixedBasePrecomputationImpl<CPP_TYPENAME GROUP_PRECOMP::Element> >class CRYPTOPP_NO_VTABLE DL_GroupParameters_IntegerBasedImpl : public DL_GroupParametersImpl<GROUP_PRECOMP, BASE_PRECOMP, DL_GroupParameters_IntegerBased>{ typedef DL_GroupParameters_IntegerBasedImpl<GROUP_PRECOMP, BASE_PRECOMP> ThisClass;public: typedef typename GROUP_PRECOMP::Element Element; // GeneratibleCryptoMaterial interface bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const {return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();} void AssignFrom(const NameValuePairs &source) {AssignFromHelper<DL_GroupParameters_IntegerBased>(this, source);} // DL_GroupParameters const DL_FixedBasePrecomputation<Element> & GetBasePrecomputation() const {return this->m_gpc;} DL_FixedBasePrecomputation<Element> & AccessBasePrecomputation() {return this->m_gpc;} // IntegerGroupParameters const Integer & GetModulus() const {return this->m_groupPrecomputation.GetModulus();} const Integer & GetGenerator() const {return this->m_gpc.GetBase(this->GetGroupPrecomputation());} void SetModulusAndSubgroupGenerator(const Integer &p, const Integer &g) // these have to be set together {this->m_groupPrecomputation.SetModulus(p); this->m_gpc.SetBase(this->GetGroupPrecomputation(), g); this->ParametersChanged();} // non-inherited bool operator==(const DL_GroupParameters_IntegerBasedImpl<GROUP_PRECOMP, BASE_PRECOMP> &rhs) const {return GetModulus() == rhs.GetModulus() && GetGenerator() == rhs.GetGenerator() && this->GetSubgroupOrder() == rhs.GetSubgroupOrder();} bool operator!=(const DL_GroupParameters_IntegerBasedImpl<GROUP_PRECOMP, BASE_PRECOMP> &rhs) const {return !operator==(rhs);}};CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters_IntegerBasedImpl<ModExpPrecomputation>;//! GF(p) group parametersclass CRYPTOPP_DLL DL_GroupParameters_GFP : public DL_GroupParameters_IntegerBasedImpl<ModExpPrecomputation>{public: // DL_GroupParameters bool IsIdentity(const Integer &element) const {return element == Integer::One();} void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const; // NameValuePairs interface bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const { return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable(); } // used by MQV Element MultiplyElements(const Element &a, const Element &b) const; Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const;protected: int GetFieldType() const {return 1;}};//! GF(p) group parameters that default to same primesclass CRYPTOPP_DLL DL_GroupParameters_GFP_DefaultSafePrime : public DL_GroupParameters_GFP{public: typedef NoCofactorMultiplication DefaultCofactorOption;protected: unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const {return modulusSize-1;}};//! GDSA algorithmtemplate <class T>class DL_Algorithm_GDSA : public DL_ElgamalLikeSignatureAlgorithm<T>{public: static const char * CRYPTOPP_API StaticAlgorithmName() {return "DSA-1363";} void Sign(const DL_GroupParameters<T> ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const { const Integer &q = params.GetSubgroupOrder(); r %= q; Integer kInv = k.InverseMod(q); s = (kInv * (x*r + e)) % q; assert(!!r && !!s); } bool Verify(const DL_GroupParameters<T> ¶ms, const DL_PublicKey<T> &publicKey, const Integer &e, const Integer &r, const Integer &s) const { const Integer &q = params.GetSubgroupOrder(); if (r>=q || r<1 || s>=q || s<1) return false; Integer w = s.InverseMod(q); Integer u1 = (e * w) % q; Integer u2 = (r * w) % q; // verify r == (g^u1 * y^u2 mod p) mod q return r == params.ConvertElementToInteger(publicKey.CascadeExponentiateBaseAndPublicElement(u1, u2)) % q; }};CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_GDSA<Integer>;//! NR algorithmtemplate <class T>class DL_Algorithm_NR : public DL_ElgamalLikeSignatureAlgorithm<T>{public: static const char * CRYPTOPP_API StaticAlgorithmName() {return "NR";} void Sign(const DL_GroupParameters<T> ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const { const Integer &q = params.GetSubgroupOrder(); r = (r + e) % q; s = (k - x*r) % q; assert(!!r); } bool Verify(const DL_GroupParameters<T> ¶ms, const DL_PublicKey<T> &publicKey, const Integer &e, const Integer &r, const Integer &s) const { const Integer &q = params.GetSubgroupOrder(); if (r>=q || r<1 || s>=q) return false; // check r == (m_g^s * m_y^r + m) mod m_q return r == (params.ConvertElementToInteger(publicKey.CascadeExponentiateBaseAndPublicElement(s, r)) + e) % q; }};/*! DSA public key format is defined in 7.3.3 of RFC 2459. The private key format is defined in 12.9 of PKCS #11 v2.10. */template <class GP>class DL_PublicKey_GFP : public DL_PublicKeyImpl<GP>{public: void Initialize(const DL_GroupParameters_IntegerBased ¶ms, const Integer &y) {this->AccessGroupParameters().Initialize(params); this->SetPublicElement(y);} void Initialize(const Integer &p, const Integer &g, const Integer &y) {this->AccessGroupParameters().Initialize(p, g); this->SetPublicElement(y);} void Initialize(const Integer &p, const Integer &q, const Integer &g, const Integer &y) {this->AccessGroupParameters().Initialize(p, q, g); this->SetPublicElement(y);} // X509PublicKey void BERDecodePublicKey(BufferedTransformation &bt, bool, size_t) {this->SetPublicElement(Integer(bt));} void DEREncodePublicKey(BufferedTransformation &bt) const {this->GetPublicElement().DEREncode(bt);}};//! DL private key (in GF(p) groups)template <class GP>class DL_PrivateKey_GFP : public DL_PrivateKeyImpl<GP>{public: void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits) {this->GenerateRandomWithKeySize(rng, modulusBits);} void Initialize(RandomNumberGenerator &rng, const Integer &p, const Integer &g) {this->GenerateRandom(rng, MakeParameters("Modulus", p)("SubgroupGenerator", g));} void Initialize(RandomNumberGenerator &rng, const Integer &p, const Integer &q, const Integer &g) {this->GenerateRandom(rng, MakeParameters("Modulus", p)("SubgroupOrder", q)("SubgroupGenerator", g));} void Initialize(const DL_GroupParameters_IntegerBased ¶ms, const Integer &x) {this->AccessGroupParameters().Initialize(params); this->SetPrivateExponent(x);} void Initialize(const Integer &p, const Integer &g, const Integer &x) {this->AccessGroupParameters().Initialize(p, g); this->SetPrivateExponent(x);} void Initialize(const Integer &p, const Integer &q, const Integer &g, const Integer &x) {this->AccessGroupParameters().Initialize(p, q, g); this->SetPrivateExponent(x);}};//! DL signing/verification keys (in GF(p) groups)struct DL_SignatureKeys_GFP{ typedef DL_GroupParameters_GFP GroupParameters; typedef DL_PublicKey_GFP<GroupParameters> PublicKey; typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;};//! DL encryption/decryption keys (in GF(p) groups)struct DL_CryptoKeys_GFP{ typedef DL_GroupParameters_GFP_DefaultSafePrime GroupParameters; typedef DL_PublicKey_GFP<GroupParameters> PublicKey; typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;};//! provided for backwards compatibility, this class uses the old non-standard Crypto++ key formattemplate <class BASE>class DL_PublicKey_GFP_OldFormat : public BASE{public: void BERDecode(BufferedTransformation &bt) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -