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