File indexing completed on 2025-01-18 09:55:04
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef CRYPTOPP_LUC_H
0011 #define CRYPTOPP_LUC_H
0012
0013 #include "cryptlib.h"
0014 #include "gfpcrypt.h"
0015 #include "integer.h"
0016 #include "algebra.h"
0017 #include "secblock.h"
0018
0019 #if CRYPTOPP_MSC_VERSION
0020 # pragma warning(push)
0021 # pragma warning(disable: 4127 4189)
0022 #endif
0023
0024 #include "pkcspad.h"
0025 #include "integer.h"
0026 #include "oaep.h"
0027 #include "dh.h"
0028
0029 #include <limits.h>
0030
0031 NAMESPACE_BEGIN(CryptoPP)
0032
0033
0034
0035
0036
0037
0038 class LUCFunction : public TrapdoorFunction, public PublicKey
0039 {
0040 typedef LUCFunction ThisClass;
0041
0042 public:
0043 virtual ~LUCFunction() {}
0044
0045
0046
0047
0048 void Initialize(const Integer &n, const Integer &e)
0049 {m_n = n; m_e = e;}
0050
0051 void BERDecode(BufferedTransformation &bt);
0052 void DEREncode(BufferedTransformation &bt) const;
0053
0054 Integer ApplyFunction(const Integer &x) const;
0055 Integer PreimageBound() const {return m_n;}
0056 Integer ImageBound() const {return m_n;}
0057
0058 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0059 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0060 void AssignFrom(const NameValuePairs &source);
0061
0062
0063 const Integer & GetModulus() const {return m_n;}
0064 const Integer & GetPublicExponent() const {return m_e;}
0065
0066 void SetModulus(const Integer &n) {m_n = n;}
0067 void SetPublicExponent(const Integer &e) {m_e = e;}
0068
0069 protected:
0070 Integer m_n, m_e;
0071 };
0072
0073
0074
0075
0076
0077
0078 class InvertibleLUCFunction : public LUCFunction, public TrapdoorFunctionInverse, public PrivateKey
0079 {
0080 typedef InvertibleLUCFunction ThisClass;
0081
0082 public:
0083 virtual ~InvertibleLUCFunction() {}
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093 void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &eStart=17);
0094
0095
0096
0097
0098
0099
0100
0101
0102 void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q, const Integer &u)
0103 {m_n = n; m_e = e; m_p = p; m_q = q; m_u = u;}
0104
0105 void BERDecode(BufferedTransformation &bt);
0106 void DEREncode(BufferedTransformation &bt) const;
0107
0108 Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
0109
0110 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0111 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0112 void AssignFrom(const NameValuePairs &source);
0113
0114 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
0115
0116
0117 const Integer& GetPrime1() const {return m_p;}
0118 const Integer& GetPrime2() const {return m_q;}
0119 const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
0120
0121 void SetPrime1(const Integer &p) {m_p = p;}
0122 void SetPrime2(const Integer &q) {m_q = q;}
0123 void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
0124
0125 protected:
0126 Integer m_p, m_q, m_u;
0127 };
0128
0129
0130
0131 struct LUC
0132 {
0133 static std::string StaticAlgorithmName() {return "LUC";}
0134 typedef LUCFunction PublicKey;
0135 typedef InvertibleLUCFunction PrivateKey;
0136 };
0137
0138
0139
0140
0141
0142
0143
0144 template <class STANDARD>
0145 struct LUCES : public TF_ES<LUC, STANDARD>
0146 {
0147 };
0148
0149
0150
0151
0152
0153
0154
0155
0156 template <class STANDARD, class H>
0157 struct LUCSS : public TF_SS<LUC, STANDARD, H>
0158 {
0159 };
0160
0161
0162 typedef LUCES<OAEP<SHA1> >::Decryptor LUCES_OAEP_SHA_Decryptor;
0163 typedef LUCES<OAEP<SHA1> >::Encryptor LUCES_OAEP_SHA_Encryptor;
0164
0165 typedef LUCSS<PKCS1v15, SHA1>::Signer LUCSSA_PKCS1v15_SHA_Signer;
0166 typedef LUCSS<PKCS1v15, SHA1>::Verifier LUCSSA_PKCS1v15_SHA_Verifier;
0167
0168
0169
0170
0171
0172
0173 class DL_GroupPrecomputation_LUC : public DL_GroupPrecomputation<Integer>
0174 {
0175 public:
0176 virtual ~DL_GroupPrecomputation_LUC() {}
0177
0178 const AbstractGroup<Element> & GetGroup() const {CRYPTOPP_ASSERT(false); throw 0;}
0179 Element BERDecodeElement(BufferedTransformation &bt) const {return Integer(bt);}
0180 void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {v.DEREncode(bt);}
0181
0182
0183 void SetModulus(const Integer &v) {m_p = v;}
0184 const Integer & GetModulus() const {return m_p;}
0185
0186 private:
0187 Integer m_p;
0188 };
0189
0190
0191
0192 class DL_BasePrecomputation_LUC : public DL_FixedBasePrecomputation<Integer>
0193 {
0194 public:
0195 virtual ~DL_BasePrecomputation_LUC() {}
0196
0197
0198 bool IsInitialized() const {return m_g.NotZero();}
0199 void SetBase(const DL_GroupPrecomputation<Element> &group, const Integer &base)
0200 {CRYPTOPP_UNUSED(group); m_g = base;}
0201 const Integer & GetBase(const DL_GroupPrecomputation<Element> &group) const
0202 {CRYPTOPP_UNUSED(group); return m_g;}
0203 void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage)
0204 {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(maxExpBits); CRYPTOPP_UNUSED(storage);}
0205 void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation)
0206 {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(storedPrecomputation);}
0207 void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const
0208 {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(storedPrecomputation);}
0209 Integer Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
0210 Integer CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent, const DL_FixedBasePrecomputation<Integer> &pc2, const Integer &exponent2) const
0211 {
0212 CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(exponent); CRYPTOPP_UNUSED(pc2); CRYPTOPP_UNUSED(exponent2);
0213
0214 throw NotImplemented("DL_BasePrecomputation_LUC: CascadeExponentiate not implemented");
0215 }
0216
0217 private:
0218 Integer m_g;
0219 };
0220
0221
0222
0223 class DL_GroupParameters_LUC : public DL_GroupParameters_IntegerBasedImpl<DL_GroupPrecomputation_LUC, DL_BasePrecomputation_LUC>
0224 {
0225 public:
0226 virtual ~DL_GroupParameters_LUC() {}
0227
0228
0229 bool IsIdentity(const Integer &element) const {return element == Integer::Two();}
0230 void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
0231 Element MultiplyElements(const Element &a, const Element &b) const
0232 {
0233 CRYPTOPP_UNUSED(a); CRYPTOPP_UNUSED(b);
0234 throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");
0235 }
0236 Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const
0237 {
0238 CRYPTOPP_UNUSED(element1); CRYPTOPP_UNUSED(exponent1); CRYPTOPP_UNUSED(element2); CRYPTOPP_UNUSED(exponent2);
0239 throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");
0240 }
0241
0242
0243 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
0244 {
0245 return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();
0246 }
0247
0248 private:
0249 int GetFieldType() const {return 2;}
0250 };
0251
0252
0253
0254 class DL_GroupParameters_LUC_DefaultSafePrime : public DL_GroupParameters_LUC
0255 {
0256 public:
0257 typedef NoCofactorMultiplication DefaultCofactorOption;
0258
0259 protected:
0260 unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const {return modulusSize-1;}
0261 };
0262
0263
0264
0265 class DL_Algorithm_LUC_HMP : public DL_ElgamalLikeSignatureAlgorithm<Integer>
0266 {
0267 public:
0268 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "LUC-HMP";}
0269
0270 virtual ~DL_Algorithm_LUC_HMP() {}
0271
0272 void Sign(const DL_GroupParameters<Integer> ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const;
0273 bool Verify(const DL_GroupParameters<Integer> ¶ms, const DL_PublicKey<Integer> &publicKey, const Integer &e, const Integer &r, const Integer &s) const;
0274
0275 size_t RLen(const DL_GroupParameters<Integer> ¶ms) const
0276 {return params.GetGroupOrder().ByteCount();}
0277 };
0278
0279
0280
0281 struct DL_SignatureKeys_LUC
0282 {
0283 typedef DL_GroupParameters_LUC GroupParameters;
0284 typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
0285 typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
0286 };
0287
0288
0289
0290
0291
0292
0293
0294 template <class H>
0295 struct LUC_HMP : public DL_SS<DL_SignatureKeys_LUC, DL_Algorithm_LUC_HMP, DL_SignatureMessageEncodingMethod_DSA, H>
0296 {
0297 };
0298
0299
0300
0301 struct DL_CryptoKeys_LUC
0302 {
0303 typedef DL_GroupParameters_LUC_DefaultSafePrime GroupParameters;
0304 typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
0305 typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
0306 };
0307
0308
0309
0310
0311
0312
0313
0314
0315 template <class HASH = SHA1, class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = true, bool LABEL_OCTETS = false>
0316 struct LUC_IES
0317 : public DL_ES<
0318 DL_CryptoKeys_LUC,
0319 DL_KeyAgreementAlgorithm_DH<Integer, COFACTOR_OPTION>,
0320 DL_KeyDerivationAlgorithm_P1363<Integer, DHAES_MODE, P1363_KDF2<HASH> >,
0321 DL_EncryptionAlgorithm_Xor<HMAC<HASH>, DHAES_MODE, LABEL_OCTETS>,
0322 LUC_IES<> >
0323 {
0324 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "LUC-IES";}
0325 };
0326
0327
0328
0329
0330 typedef DH_Domain<DL_GroupParameters_LUC_DefaultSafePrime> LUC_DH;
0331
0332 NAMESPACE_END
0333
0334 #if CRYPTOPP_MSC_VERSION
0335 # pragma warning(pop)
0336 #endif
0337
0338 #endif