File indexing completed on 2025-01-18 09:55:02
0001
0002
0003
0004
0005
0006
0007 #ifndef CRYPTOPP_ESIGN_H
0008 #define CRYPTOPP_ESIGN_H
0009
0010 #include "cryptlib.h"
0011 #include "pubkey.h"
0012 #include "integer.h"
0013 #include "asn.h"
0014 #include "misc.h"
0015
0016 NAMESPACE_BEGIN(CryptoPP)
0017
0018
0019
0020 class ESIGNFunction : public TrapdoorFunction, public ASN1CryptoMaterial<PublicKey>
0021 {
0022 typedef ESIGNFunction ThisClass;
0023
0024 public:
0025
0026
0027
0028
0029 void Initialize(const Integer &n, const Integer &e)
0030 {m_n = n; m_e = e;}
0031
0032
0033 void BERDecode(BufferedTransformation &bt);
0034 void DEREncode(BufferedTransformation &bt) const;
0035
0036
0037 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0038 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0039 void AssignFrom(const NameValuePairs &source);
0040
0041
0042 Integer ApplyFunction(const Integer &x) const;
0043 Integer PreimageBound() const {return m_n;}
0044 Integer ImageBound() const {return Integer::Power2(GetK());}
0045
0046
0047 const Integer & GetModulus() const {return m_n;}
0048 const Integer & GetPublicExponent() const {return m_e;}
0049
0050 void SetModulus(const Integer &n) {m_n = n;}
0051 void SetPublicExponent(const Integer &e) {m_e = e;}
0052
0053 protected:
0054
0055 unsigned int GetK() const {return SaturatingSubtract(m_n.BitCount()/3, 1U);}
0056
0057 Integer m_n, m_e;
0058 };
0059
0060
0061
0062 class InvertibleESIGNFunction : public ESIGNFunction, public RandomizedTrapdoorFunctionInverse, public PrivateKey
0063 {
0064 typedef InvertibleESIGNFunction ThisClass;
0065
0066 public:
0067
0068
0069
0070
0071
0072
0073
0074 void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q)
0075 {m_n = n; m_e = e; m_p = p; m_q = q;}
0076
0077
0078
0079
0080
0081
0082
0083 void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
0084 {GenerateRandomWithKeySize(rng, modulusBits);}
0085
0086
0087 void Save(BufferedTransformation &bt) const
0088 {BEREncode(bt);}
0089
0090
0091 void Load(BufferedTransformation &bt)
0092 {BERDecode(bt);}
0093
0094 void BERDecode(BufferedTransformation &bt);
0095 void DEREncode(BufferedTransformation &bt) const;
0096
0097 Integer CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const;
0098
0099
0100 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0101 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0102 void AssignFrom(const NameValuePairs &source);
0103
0104 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
0105
0106 const Integer& GetPrime1() const {return m_p;}
0107 const Integer& GetPrime2() const {return m_q;}
0108
0109 void SetPrime1(const Integer &p) {m_p = p;}
0110 void SetPrime2(const Integer &q) {m_q = q;}
0111
0112 protected:
0113 Integer m_p, m_q;
0114 };
0115
0116
0117
0118
0119 template <class T>
0120 class EMSA5Pad : public PK_DeterministicSignatureMessageEncodingMethod
0121 {
0122 public:
0123 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "EMSA5";}
0124
0125 void ComputeMessageRepresentative(RandomNumberGenerator &rng,
0126 const byte *recoverableMessage, size_t recoverableMessageLength,
0127 HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
0128 byte *representative, size_t representativeBitLength) const
0129 {
0130 CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(recoverableMessage), CRYPTOPP_UNUSED(recoverableMessageLength);
0131 CRYPTOPP_UNUSED(messageEmpty), CRYPTOPP_UNUSED(hashIdentifier);
0132 SecByteBlock digest(hash.DigestSize());
0133 hash.Final(digest);
0134 size_t representativeByteLength = BitsToBytes(representativeBitLength);
0135 T mgf;
0136 mgf.GenerateAndMask(hash, representative, representativeByteLength, digest, digest.size(), false);
0137 if (representativeBitLength % 8 != 0)
0138 representative[0] = (byte)Crop(representative[0], representativeBitLength % 8);
0139 }
0140 };
0141
0142
0143
0144 struct P1363_EMSA5 : public SignatureStandard
0145 {
0146 typedef EMSA5Pad<P1363_MGF1> SignatureMessageEncodingMethod;
0147 };
0148
0149
0150
0151 struct ESIGN_Keys
0152 {
0153 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "ESIGN";}
0154 typedef ESIGNFunction PublicKey;
0155 typedef InvertibleESIGNFunction PrivateKey;
0156 };
0157
0158
0159
0160
0161
0162 template <class H, class STANDARD = P1363_EMSA5>
0163 struct ESIGN : public TF_SS<ESIGN_Keys, STANDARD, H>
0164 {
0165 };
0166
0167 NAMESPACE_END
0168
0169 #endif