00001 #ifndef CRYPTOPP_LUC_H
00002 #define CRYPTOPP_LUC_H
00003
00004
00005
00006
00007 #include "pkcspad.h"
00008 #include "oaep.h"
00009 #include "integer.h"
00010 #include "dh.h"
00011
00012 #include <limits.h>
00013
00014 NAMESPACE_BEGIN(CryptoPP)
00015
00016
00017 class LUCFunction : public TrapdoorFunction, public PublicKey
00018 {
00019 typedef LUCFunction ThisClass;
00020
00021 public:
00022 void Initialize(const Integer &n, const Integer &e)
00023 {m_n = n; m_e = e;}
00024
00025 void BERDecode(BufferedTransformation &bt);
00026 void DEREncode(BufferedTransformation &bt) const;
00027
00028 Integer ApplyFunction(const Integer &x) const;
00029 Integer PreimageBound() const {return m_n;}
00030 Integer ImageBound() const {return m_n;}
00031
00032 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
00033 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00034 void AssignFrom(const NameValuePairs &source);
00035
00036
00037 const Integer & GetModulus() const {return m_n;}
00038 const Integer & GetPublicExponent() const {return m_e;}
00039
00040 void SetModulus(const Integer &n) {m_n = n;}
00041 void SetPublicExponent(const Integer &e) {m_e = e;}
00042
00043 protected:
00044 Integer m_n, m_e;
00045 };
00046
00047
00048 class InvertibleLUCFunction : public LUCFunction, public TrapdoorFunctionInverse, public PrivateKey
00049 {
00050 typedef InvertibleLUCFunction ThisClass;
00051
00052 public:
00053 void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &eStart=17);
00054 void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q, const Integer &u)
00055 {m_n = n; m_e = e; m_p = p; m_q = q; m_u = u;}
00056
00057 void BERDecode(BufferedTransformation &bt);
00058 void DEREncode(BufferedTransformation &bt) const;
00059
00060 Integer CalculateInverse(const Integer &x) const;
00061
00062 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
00063 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00064 void AssignFrom(const NameValuePairs &source);
00065
00066 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
00067
00068
00069 const Integer& GetPrime1() const {return m_p;}
00070 const Integer& GetPrime2() const {return m_q;}
00071 const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
00072
00073 void SetPrime1(const Integer &p) {m_p = p;}
00074 void SetPrime2(const Integer &q) {m_q = q;}
00075 void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
00076
00077 protected:
00078 Integer m_p, m_q, m_u;
00079 };
00080
00081 struct LUC
00082 {
00083 static std::string StaticAlgorithmName() {return "LUC";}
00084 typedef LUCFunction PublicKey;
00085 typedef InvertibleLUCFunction PrivateKey;
00086 };
00087
00088
00089 template <class STANDARD>
00090 struct LUCES : public TF_ES<STANDARD, LUC>
00091 {
00092 };
00093
00094
00095 template <class H, class STANDARD = PKCS1v15>
00096 struct LUCSSA : public TF_SSA<STANDARD, H, LUC>
00097 {
00098 };
00099
00100
00101 typedef LUCES<OAEP<SHA> >::Decryptor LUCES_OAEP_SHA_Decryptor;
00102 typedef LUCES<OAEP<SHA> >::Encryptor LUCES_OAEP_SHA_Encryptor;
00103
00104 typedef LUCSSA<SHA>::Signer LUCSSA_PKCS1v15_SHA_Signer;
00105 typedef LUCSSA<SHA>::Verifier LUCSSA_PKCS1v15_SHA_Verifier;
00106
00107
00108
00109
00110 class DL_GroupPrecomputation_LUC : public DL_GroupPrecomputation<Integer>
00111 {
00112 public:
00113 const AbstractGroup<Element> & GetGroup() const {assert(false); throw 0;}
00114 Element BERDecodeElement(BufferedTransformation &bt) const {return Integer(bt);}
00115 void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {v.DEREncode(bt);}
00116
00117
00118 void SetModulus(const Integer &v) {m_p = v;}
00119 const Integer & GetModulus() const {return m_p;}
00120
00121 private:
00122 Integer m_p;
00123 };
00124
00125
00126 class DL_BasePrecomputation_LUC : public DL_FixedBasePrecomputation<Integer>
00127 {
00128 public:
00129
00130 bool IsInitialized() const {return m_g.NotZero();}
00131 void SetBase(const DL_GroupPrecomputation<Element> &group, const Integer &base) {m_g = base;}
00132 const Integer & GetBase(const DL_GroupPrecomputation<Element> &group) const {return m_g;}
00133 void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage) {}
00134 void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) {}
00135 void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const {}
00136 Integer Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
00137 Integer CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent, const DL_FixedBasePrecomputation<Integer> &pc2, const Integer &exponent2) const
00138 {throw NotImplemented("DL_BasePrecomputation_LUC: CascadeExponentiate not implemented");}
00139
00140 private:
00141 Integer m_g;
00142 };
00143
00144
00145 class DL_GroupParameters_LUC : public DL_GroupParameters_IntegerBasedImpl<DL_GroupPrecomputation_LUC, DL_BasePrecomputation_LUC>
00146 {
00147 public:
00148
00149 bool IsIdentity(const Integer &element) const {return element == Integer::Two();}
00150 void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
00151 Element MultiplyElements(const Element &a, const Element &b) const
00152 {throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");}
00153 Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const
00154 {throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");}
00155
00156
00157 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
00158 {
00159 return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();
00160 }
00161
00162 private:
00163 int GetFieldType() const {return 2;}
00164 };
00165
00166
00167 class DL_GroupParameters_LUC_DefaultSafePrime : public DL_GroupParameters_LUC
00168 {
00169 public:
00170 typedef NoCofactorMultiplication DefaultCofactorOption;
00171
00172 protected:
00173 unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const {return modulusSize-1;}
00174 };
00175
00176
00177 class DL_Algorithm_LUC_HMP : public DL_ElgamalLikeSignatureAlgorithm<Integer>
00178 {
00179 public:
00180 static const char * StaticAlgorithmName() {return "LUC-HMP";}
00181
00182 Integer EncodeDigest(unsigned int modulusBits, const byte *digest, unsigned int digestLen) const
00183 {return DSA_EncodeDigest(modulusBits, digest, digestLen);}
00184
00185 bool Sign(const DL_GroupParameters<Integer> ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const;
00186 bool Verify(const DL_GroupParameters<Integer> ¶ms, const DL_PublicKey<Integer> &publicKey, const Integer &e, const Integer &r, const Integer &s) const;
00187
00188 unsigned int RLen(const DL_GroupParameters<Integer> ¶ms) const
00189 {return params.GetGroupOrder().ByteCount();}
00190 };
00191
00192
00193 struct DL_SignatureKeys_LUC
00194 {
00195 typedef DL_GroupParameters_LUC GroupParameters;
00196 typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
00197 typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
00198 };
00199
00200
00201 template <class H>
00202 struct LUC_HMP : public DL_SSA<DL_SignatureKeys_LUC, DL_Algorithm_LUC_HMP, H>
00203 {
00204 };
00205
00206
00207 struct DL_CryptoKeys_LUC
00208 {
00209 typedef DL_GroupParameters_LUC_DefaultSafePrime GroupParameters;
00210 typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
00211 typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
00212 };
00213
00214
00215 template <class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = true>
00216 struct LUC_IES
00217 : public DL_ES<
00218 DL_CryptoKeys_LUC,
00219 DL_KeyAgreementAlgorithm_DH<Integer, COFACTOR_OPTION>,
00220 DL_KeyDerivationAlgorithm_P1363<Integer, DHAES_MODE, P1363_KDF2<SHA1> >,
00221 DL_EncryptionAlgorithm_Xor<HMAC<SHA1>, DHAES_MODE>,
00222 LUC_IES<> >
00223 {
00224 static std::string StaticAlgorithmName() {return "LUC-IES";}
00225 };
00226
00227
00228
00229
00230 typedef DH_Domain<DL_GroupParameters_LUC_DefaultSafePrime> LUC_DH;
00231
00232 NAMESPACE_END
00233
00234 #endif