00001 #ifndef CRYPTOPP_ESIGN_H
00002 #define CRYPTOPP_ESIGN_H
00003
00004
00005
00006
00007
00008
00009 #include "cryptlib.h"
00010 #include "pubkey.h"
00011 #include "integer.h"
00012 #include "asn.h"
00013 #include "misc.h"
00014
00015 NAMESPACE_BEGIN(CryptoPP)
00016
00017
00018 class ESIGNFunction : public TrapdoorFunction, public ASN1CryptoMaterial<PublicKey>
00019 {
00020 typedef ESIGNFunction ThisClass;
00021
00022 public:
00023 void Initialize(const Integer &n, const Integer &e)
00024 {m_n = n; m_e = e;}
00025
00026
00027 void BERDecode(BufferedTransformation &bt);
00028 void DEREncode(BufferedTransformation &bt) const;
00029
00030
00031 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
00032 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00033 void AssignFrom(const NameValuePairs &source);
00034
00035
00036 Integer ApplyFunction(const Integer &x) const;
00037 Integer PreimageBound() const {return m_n;}
00038 Integer ImageBound() const {return Integer::Power2(GetK());}
00039
00040
00041 const Integer & GetModulus() const {return m_n;}
00042 const Integer & GetPublicExponent() const {return m_e;}
00043
00044 void SetModulus(const Integer &n) {m_n = n;}
00045 void SetPublicExponent(const Integer &e) {m_e = e;}
00046
00047 protected:
00048
00049 unsigned int GetK() const {return SaturatingSubtract(m_n.BitCount()/3, 1U);}
00050
00051 Integer m_n, m_e;
00052 };
00053
00054
00055 class InvertibleESIGNFunction : public ESIGNFunction, public RandomizedTrapdoorFunctionInverse, public PrivateKey
00056 {
00057 typedef InvertibleESIGNFunction ThisClass;
00058
00059 public:
00060 void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q)
00061 {m_n = n; m_e = e; m_p = p; m_q = q;}
00062
00063 void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
00064 {GenerateRandomWithKeySize(rng, modulusBits);}
00065
00066 void BERDecode(BufferedTransformation &bt);
00067 void DEREncode(BufferedTransformation &bt) const;
00068
00069 Integer CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const;
00070
00071
00072 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
00073 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00074 void AssignFrom(const NameValuePairs &source);
00075
00076 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
00077
00078 const Integer& GetPrime1() const {return m_p;}
00079 const Integer& GetPrime2() const {return m_q;}
00080
00081 void SetPrime1(const Integer &p) {m_p = p;}
00082 void SetPrime2(const Integer &q) {m_q = q;}
00083
00084 protected:
00085 Integer m_p, m_q;
00086 };
00087
00088
00089 template <class T>
00090 class EMSA5Pad : public PK_DeterministicSignatureMessageEncodingMethod
00091 {
00092 public:
00093 static const char *StaticAlgorithmName() {return "EMSA5";}
00094
00095 void ComputeMessageRepresentative(RandomNumberGenerator &rng,
00096 const byte *recoverableMessage, size_t recoverableMessageLength,
00097 HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
00098 byte *representative, size_t representativeBitLength) const
00099 {
00100 CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(recoverableMessage), CRYPTOPP_UNUSED(recoverableMessageLength);
00101 CRYPTOPP_UNUSED(messageEmpty), CRYPTOPP_UNUSED(hashIdentifier);
00102 SecByteBlock digest(hash.DigestSize());
00103 hash.Final(digest);
00104 size_t representativeByteLength = BitsToBytes(representativeBitLength);
00105 T mgf;
00106 mgf.GenerateAndMask(hash, representative, representativeByteLength, digest, digest.size(), false);
00107 if (representativeBitLength % 8 != 0)
00108 representative[0] = (byte)Crop(representative[0], representativeBitLength % 8);
00109 }
00110 };
00111
00112
00113 struct P1363_EMSA5 : public SignatureStandard
00114 {
00115 typedef EMSA5Pad<P1363_MGF1> SignatureMessageEncodingMethod;
00116 };
00117
00118 struct ESIGN_Keys
00119 {
00120 static std::string StaticAlgorithmName() {return "ESIGN";}
00121 typedef ESIGNFunction PublicKey;
00122 typedef InvertibleESIGNFunction PrivateKey;
00123 };
00124
00125
00126 template <class H, class STANDARD = P1363_EMSA5>
00127 struct ESIGN : public TF_SS<STANDARD, H, ESIGN_Keys>
00128 {
00129 };
00130
00131 NAMESPACE_END
00132
00133 #endif