00001 #ifndef CRYPTOPP_LUC_H
00002 #define CRYPTOPP_LUC_H
00003
00007 #include "pkcspad.h"
00008 #include "oaep.h"
00009 #include "integer.h"
00010
00011 #include <limits.h>
00012
00013 NAMESPACE_BEGIN(CryptoPP)
00014
00016 class LUCFunction : virtual public TrapdoorFunction
00017 {
00018 public:
00019 LUCFunction(const Integer &n, const Integer &e) : n(n), e(e) {}
00020 LUCFunction(BufferedTransformation &bt);
00021 void DEREncode(BufferedTransformation &bt) const;
00022
00023 Integer ApplyFunction(const Integer &x) const;
00024 Integer PreimageBound() const {return n;}
00025 Integer ImageBound() const {return n;}
00026
00027 protected:
00028 LUCFunction() {}
00029 Integer n, e;
00030 };
00031
00033 class InvertibleLUCFunction : public LUCFunction, public InvertibleTrapdoorFunction
00034 {
00035 public:
00036 InvertibleLUCFunction(const Integer &n, const Integer &e,
00037 const Integer &p, const Integer &q, const Integer &u);
00038
00039 InvertibleLUCFunction(RandomNumberGenerator &rng, unsigned int keybits, const Integer &eStart=17);
00040 InvertibleLUCFunction(BufferedTransformation &bt);
00041 void DEREncode(BufferedTransformation &bt) const;
00042
00043 Integer CalculateInverse(const Integer &x) const;
00044
00045 protected:
00046 Integer p, q, u;
00047 };
00048
00050 template <class B>
00051 class LUCPrivateKeyTemplate : public B
00052 {
00053 public:
00054 LUCPrivateKeyTemplate(const Integer &n, const Integer &e,
00055 const Integer &p, const Integer &q, const Integer &u)
00056 : PublicKeyBaseTemplate<InvertibleLUCFunction>(
00057 InvertibleLUCFunction(n, e, p, q, u)) {}
00058
00059 LUCPrivateKeyTemplate(RandomNumberGenerator &rng, unsigned int keybits, const Integer &eStart=17)
00060 : PublicKeyBaseTemplate<InvertibleLUCFunction>(
00061 InvertibleLUCFunction(rng, keybits, eStart)) {}
00062
00063 LUCPrivateKeyTemplate(BufferedTransformation &bt)
00064 : PublicKeyBaseTemplate<InvertibleLUCFunction>(bt) {}
00065 };
00066
00068 template <class B, class V>
00069 class LUCPublicKeyTemplate : public B
00070 {
00071 public:
00072 LUCPublicKeyTemplate(const Integer &n, const Integer &e)
00073 : PublicKeyBaseTemplate<LUCFunction>(LUCFunction(n, e)) {}
00074
00075 LUCPublicKeyTemplate(const V &priv)
00076 : PublicKeyBaseTemplate<LUCFunction>(priv.GetTrapdoorFunction()) {}
00077
00078 LUCPublicKeyTemplate(BufferedTransformation &bt)
00079 : PublicKeyBaseTemplate<LUCFunction>(bt) {}
00080 };
00081
00083 typedef LUCPrivateKeyTemplate<DecryptorTemplate<OAEP<SHA>, InvertibleLUCFunction> >
00084 LUCES_OAEP_SHA_Decryptor;
00086 typedef LUCPublicKeyTemplate<EncryptorTemplate<OAEP<SHA>, LUCFunction>, LUCES_OAEP_SHA_Decryptor>
00087 LUCES_OAEP_SHA_Encryptor;
00089 typedef LUCPrivateKeyTemplate<SignerTemplate<DigestSignerTemplate<PKCS_SignaturePaddingScheme, InvertibleLUCFunction>, PKCS_DecoratedHashModule<SHA> > >
00090 LUCSSA_PKCS1v15_SHA_Signer;
00092 typedef LUCPublicKeyTemplate<VerifierTemplate<DigestVerifierTemplate<PKCS_SignaturePaddingScheme, LUCFunction>, PKCS_DecoratedHashModule<SHA> >, LUCSSA_PKCS1v15_SHA_Signer>
00093 LUCSSA_PKCS1v15_SHA_Verifier;
00094
00095
00096
00098 class LUCELG_Encryptor : public PK_FixedLengthEncryptor
00099 {
00100 public:
00101 LUCELG_Encryptor(const Integer &p, const Integer &g, const Integer &y);
00102 LUCELG_Encryptor(BufferedTransformation &bt);
00103
00104 void DEREncode(BufferedTransformation &bt) const;
00105
00106 void Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText);
00107
00108 unsigned int MaxPlainTextLength() const {return STDMIN(255U, modulusLen-3);}
00109 unsigned int CipherTextLength() const {return 2*modulusLen;}
00110
00111 const Integer & GetPrime() const {return p;}
00112 const Integer & GetGenerator() const {return g;}
00113 const Integer & GetPublicResidue() const {return y;}
00114
00115 protected:
00116 LUCELG_Encryptor() {}
00117 void RawEncrypt(const Integer &k, const Integer &m, Integer &a, Integer &b) const;
00118 unsigned int ExponentBitLength() const;
00119
00120 Integer p, g, y;
00121 unsigned int modulusLen;
00122 };
00123
00125 class LUCELG_Decryptor : public LUCELG_Encryptor, public PK_FixedLengthDecryptor
00126 {
00127 public:
00128 LUCELG_Decryptor(const Integer &p, const Integer &g, const Integer &y, const Integer &x);
00129 LUCELG_Decryptor(RandomNumberGenerator &rng, unsigned int pbits);
00130
00131 LUCELG_Decryptor(RandomNumberGenerator &rng, const Integer &p, const Integer &g);
00132
00133 LUCELG_Decryptor(BufferedTransformation &bt);
00134 void DEREncode(BufferedTransformation &bt) const;
00135
00136 unsigned int Decrypt(const byte *cipherText, byte *plainText);
00137
00138 protected:
00139 void RawDecrypt(const Integer &a, const Integer &b, Integer &m) const;
00140
00141 Integer x;
00142 };
00143
00144
00145
00147 class LUCELG_DigestVerifier : public DigestVerifier
00148 {
00149 public:
00150 LUCELG_DigestVerifier(const Integer &p, const Integer &q, const Integer &g, const Integer &y);
00151 LUCELG_DigestVerifier(BufferedTransformation &bt);
00152
00153 void DEREncode(BufferedTransformation &bt) const;
00154 bool VerifyDigest(const byte *digest, unsigned int digestLen, const byte *signature) const;
00155
00156 unsigned int MaxDigestLength() const {return UINT_MAX;}
00157 unsigned int DigestSignatureLength() const {return p.ByteCount()+q.ByteCount();}
00158
00159 protected:
00160 LUCELG_DigestVerifier() {}
00161 bool RawVerify(const Integer &m, const Integer &a, const Integer &b) const;
00162 Integer EncodeDigest(const byte *digest, unsigned int digestLen) const;
00163
00164 Integer p, q, g, y;
00165 };
00166
00168 class LUCELG_DigestSigner : public LUCELG_DigestVerifier, public DigestSigner
00169 {
00170 public:
00171 LUCELG_DigestSigner(const Integer &p, const Integer &q, const Integer &g, const Integer &y, const Integer &x);
00172 LUCELG_DigestSigner(RandomNumberGenerator &rng, unsigned int pbits);
00173 LUCELG_DigestSigner(RandomNumberGenerator &rng, const Integer &p, const Integer &q, const Integer &g);
00174 LUCELG_DigestSigner(BufferedTransformation &bt);
00175
00176 void DEREncode(BufferedTransformation &bt) const;
00177 void SignDigest(RandomNumberGenerator &rng, const byte *digest, unsigned int digestLen, byte *signature) const;
00178
00179 protected:
00180 void RawSign(RandomNumberGenerator &rng, const Integer &m, Integer &a, Integer &b) const;
00181
00182 Integer x;
00183 };
00184
00186 template <class H>
00187 class LUCELG_Signer : public SignerTemplate<LUCELG_DigestSigner, H>
00188 {
00189 typedef LUCELG_DigestSigner Base;
00190 public:
00191 LUCELG_Signer(const Integer &p, const Integer &q, const Integer &g, const Integer &y, const Integer &x)
00192 : Base(p, q, g, y, x) {}
00193
00194
00195 LUCELG_Signer(RandomNumberGenerator &rng, unsigned int keybits)
00196 : Base(rng, keybits) {}
00197
00198
00199 LUCELG_Signer(RandomNumberGenerator &rng, const Integer &p, const Integer &q, const Integer &g)
00200 : Base(rng, p, q, g) {}
00201
00202
00203 LUCELG_Signer(BufferedTransformation &storedKey)
00204 : Base(storedKey) {}
00205 };
00206
00208 template <class H>
00209 class LUCELG_Verifier : public VerifierTemplate<LUCELG_DigestVerifier, H>
00210 {
00211 typedef LUCELG_DigestVerifier Base;
00212 public:
00213 LUCELG_Verifier(const Integer &p, const Integer &q, const Integer &g, const Integer &y)
00214 : Base(p, q, g, y) {}
00215
00216
00217 LUCELG_Verifier(const LUCELG_Signer<H> &priv)
00218 : Base(priv) {}
00219
00220
00221 LUCELG_Verifier(BufferedTransformation &storedKey)
00222 : Base(storedKey) {}
00223 };
00224
00225
00226
00228 class LUCDIF : public PK_SimpleKeyAgreementDomain
00229 {
00230 public:
00231 LUCDIF(const Integer &p, const Integer &g);
00232 LUCDIF(RandomNumberGenerator &rng, unsigned int pbits);
00233 LUCDIF(BufferedTransformation &domainParams);
00234
00235 void DEREncode(BufferedTransformation &domainParams) const;
00236
00237 bool ValidateDomainParameters(RandomNumberGenerator &rng) const;
00238 unsigned int AgreedValueLength() const {return p.ByteCount();}
00239 unsigned int PrivateKeyLength() const {return p.ByteCount();}
00240 unsigned int PublicKeyLength() const {return p.ByteCount();}
00241
00242 void GenerateKeyPair(RandomNumberGenerator &rng, byte *secretKey, byte *publicKey) const;
00243 bool Agree(byte *agreedValue, const byte *secretKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const;
00244
00245 const Integer &Prime() const {return p;}
00246 const Integer &Generator() const {return g;}
00247
00248 private:
00249 unsigned int ExponentBitLength() const;
00250
00251 Integer p, g;
00252 };
00253
00254 NAMESPACE_END
00255
00256 #endif