00001 #ifndef CRYPTOPP_ECCRYPTO_H
00002 #define CRYPTOPP_ECCRYPTO_H
00003
00004
00005
00006
00007 #include "pubkey.h"
00008 #include "integer.h"
00009 #include "asn.h"
00010 #include "hmac.h"
00011 #include "sha.h"
00012 #include "gfpcrypt.h"
00013 #include "dh.h"
00014 #include "mqv.h"
00015 #include "ecp.h"
00016 #include "ec2n.h"
00017
00018 NAMESPACE_BEGIN(CryptoPP)
00019
00020
00021
00022
00023
00024 template <class EC>
00025 class DL_GroupParameters_EC : public DL_GroupParametersImpl<EcPrecomputation<EC> >
00026 {
00027 typedef DL_GroupParameters_EC<EC> ThisClass;
00028
00029 public:
00030 typedef EC EllipticCurve;
00031 typedef typename EllipticCurve::Point Point;
00032 typedef Point Element;
00033 typedef IncompatibleCofactorMultiplication DefaultCofactorOption;
00034
00035 DL_GroupParameters_EC() : m_compress(false), m_encodeAsOID(false) {}
00036 DL_GroupParameters_EC(const OID &oid)
00037 : m_compress(false), m_encodeAsOID(false) {Initialize(oid);}
00038 DL_GroupParameters_EC(const EllipticCurve &ec, const Point &G, const Integer &n, const Integer &k = Integer::Zero())
00039 : m_compress(false), m_encodeAsOID(false) {Initialize(ec, G, n, k);}
00040 DL_GroupParameters_EC(BufferedTransformation &bt)
00041 : m_compress(false), m_encodeAsOID(false) {BERDecode(bt);}
00042
00043 void Initialize(const EllipticCurve &ec, const Point &G, const Integer &n, const Integer &k = Integer::Zero())
00044 {
00045 m_groupPrecomputation.SetCurve(ec);
00046 SetSubgroupGenerator(G);
00047 m_n = n;
00048 m_k = k;
00049 }
00050 void Initialize(const OID &oid);
00051
00052
00053 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00054 void AssignFrom(const NameValuePairs &source);
00055
00056
00057
00058
00059 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
00060
00061
00062 const DL_FixedBasePrecomputation<Element> & GetBasePrecomputation() const {return m_gpc;}
00063 DL_FixedBasePrecomputation<Element> & AccessBasePrecomputation() {return m_gpc;}
00064 const Integer & GetSubgroupOrder() const {return m_n;}
00065 Integer GetCofactor() const;
00066 bool ValidateGroup(RandomNumberGenerator &rng, unsigned int level) const;
00067 bool ValidateElement(unsigned int level, const Element &element, const DL_FixedBasePrecomputation<Element> *precomp) const;
00068 bool FastSubgroupCheckAvailable() const {return false;}
00069 void EncodeElement(bool reversible, const Element &element, byte *encoded) const
00070 {
00071 if (reversible)
00072 GetCurve().EncodePoint(encoded, element, m_compress);
00073 else
00074 element.x.Encode(encoded, GetEncodedElementSize(false));
00075 }
00076 unsigned int GetEncodedElementSize(bool reversible) const
00077 {
00078 if (reversible)
00079 return GetCurve().EncodedPointSize(m_compress);
00080 else
00081 return GetCurve().GetField().MaxElementByteLength();
00082 }
00083 Element DecodeElement(const byte *encoded, bool checkForGroupMembership) const
00084 {
00085 Point result;
00086 if (!GetCurve().DecodePoint(result, encoded, GetEncodedElementSize(true)))
00087 throw DL_BadElement();
00088 if (checkForGroupMembership && !ValidateElement(1, result, NULL))
00089 throw DL_BadElement();
00090 return result;
00091 }
00092 Integer ConvertElementToInteger(const Element &element) const;
00093 Integer GetMaxExponent() const {return GetSubgroupOrder()-1;}
00094 bool IsIdentity(const Element &element) const {return element.identity;}
00095 void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
00096
00097
00098 OID GetAlgorithmID() const;
00099
00100
00101 Element MultiplyElements(const Element &a, const Element &b) const;
00102 Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const;
00103
00104
00105
00106
00107 static OID GetNextRecommendedParametersOID(const OID &oid);
00108
00109 void BERDecode(BufferedTransformation &bt);
00110 void DEREncode(BufferedTransformation &bt) const;
00111
00112 void SetPointCompression(bool compress) {m_compress = compress;}
00113 bool GetPointCompression() const {return m_compress;}
00114
00115 void SetEncodeAsOID(bool encodeAsOID) {m_encodeAsOID = encodeAsOID;}
00116 bool GetEncodeAsOID() const {return m_encodeAsOID;}
00117
00118 const EllipticCurve& GetCurve() const {return m_groupPrecomputation.GetCurve();}
00119
00120 bool operator==(const ThisClass &rhs) const
00121 {return DL_GroupParametersImpl<EcPrecomputation<EC> >::operator==(rhs);}
00122
00123 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
00124 const Point& GetBasePoint() const {return GetSubgroupGenerator();}
00125 const Integer& GetBasePointOrder() const {return GetSubgroupOrder();}
00126 void LoadRecommendedParameters(const OID &oid) {Initialize(oid);}
00127 #endif
00128
00129 protected:
00130 unsigned int FieldElementLength() const {return GetCurve().GetField().MaxElementByteLength();}
00131 unsigned int ExponentLength() const {return m_n.ByteCount();}
00132
00133 OID m_oid;
00134 Integer m_n;
00135 bool m_compress, m_encodeAsOID;
00136 mutable Integer m_k;
00137 };
00138
00139 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters_EC<ECP>;
00140 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters_EC<EC2N>;
00141 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKeyImpl<DL_GroupParameters_EC<ECP> >;
00142 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKeyImpl<DL_GroupParameters_EC<EC2N> >;
00143
00144
00145 template <class EC>
00146 class DL_PublicKey_EC : public DL_PublicKeyImpl<DL_GroupParameters_EC<EC> >
00147 {
00148 public:
00149 typedef typename EC::Point Element;
00150
00151 void Initialize(const DL_GroupParameters_EC<EC> ¶ms, const Element &Q)
00152 {AccessGroupParameters() = params; SetPublicElement(Q);}
00153 void Initialize(const EC &ec, const Element &G, const Integer &n, const Element &Q)
00154 {AccessGroupParameters().Initialize(ec, G, n); SetPublicElement(Q);}
00155
00156
00157 void BERDecodeKey2(BufferedTransformation &bt, bool parametersPresent, unsigned int size);
00158 void DEREncodeKey(BufferedTransformation &bt) const;
00159 };
00160
00161 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_EC<ECP>;
00162 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_EC<EC2N>;
00163 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKeyImpl<DL_GroupParameters_EC<ECP> >;
00164 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKeyImpl<DL_GroupParameters_EC<EC2N> >;
00165
00166
00167 template <class EC>
00168 class DL_PrivateKey_EC : public DL_PrivateKeyImpl<DL_GroupParameters_EC<EC> >
00169 {
00170 public:
00171 typedef typename EC::Point Element;
00172
00173 void Initialize(const DL_GroupParameters_EC<EC> ¶ms, const Integer &x)
00174 {AccessGroupParameters() = params; SetPrivateExponent(x);}
00175 void Initialize(const EC &ec, const Element &G, const Integer &n, const Integer &x)
00176 {AccessGroupParameters().Initialize(ec, G, n); SetPrivateExponent(x);}
00177 void Initialize(RandomNumberGenerator &rng, const DL_GroupParameters_EC<EC> ¶ms)
00178 {GenerateRandom(rng, params);}
00179 void Initialize(RandomNumberGenerator &rng, const EC &ec, const Element &G, const Integer &n)
00180 {GenerateRandom(rng, DL_GroupParameters_EC<EC>(ec, G, n));}
00181
00182
00183 void BERDecodeKey2(BufferedTransformation &bt, bool parametersPresent, unsigned int size);
00184 void DEREncodeKey(BufferedTransformation &bt) const;
00185 };
00186
00187 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_EC<ECP>;
00188 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_EC<EC2N>;
00189
00190
00191 template <class EC, class COFACTOR_OPTION = DL_GroupParameters_EC<EC>::DefaultCofactorOption>
00192 struct ECDH
00193 {
00194 typedef DH_Domain<DL_GroupParameters_EC<EC>, COFACTOR_OPTION> Domain;
00195 };
00196
00197
00198 template <class EC, class COFACTOR_OPTION = DL_GroupParameters_EC<EC>::DefaultCofactorOption>
00199 struct ECMQV
00200 {
00201 typedef MQV_Domain<DL_GroupParameters_EC<EC>, COFACTOR_OPTION> Domain;
00202 };
00203
00204
00205 template <class EC>
00206 struct DL_Keys_EC
00207 {
00208 typedef DL_PublicKey_EC<EC> PublicKey;
00209 typedef DL_PrivateKey_EC<EC> PrivateKey;
00210 };
00211
00212 template <class EC, class H = SHA>
00213 struct ECDSA;
00214
00215 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_WithSignaturePairwiseConsistencyTest<DL_PrivateKey_EC<ECP>, ECDSA<ECP> >;
00216 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_WithSignaturePairwiseConsistencyTest<DL_PrivateKey_EC<EC2N>, ECDSA<EC2N> >;
00217
00218
00219 template <class EC>
00220 struct DL_Keys_ECDSA
00221 {
00222 typedef DL_PublicKey_EC<EC> PublicKey;
00223 typedef DL_PrivateKey_WithSignaturePairwiseConsistencyTest<DL_PrivateKey_EC<EC>, ECDSA<EC> > PrivateKey;
00224 };
00225
00226 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_GDSA<ECP::Point>;
00227 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_GDSA<EC2N::Point>;
00228
00229
00230 template <class EC>
00231 class DL_Algorithm_ECDSA : public DL_Algorithm_GDSA<typename EC::Point>
00232 {
00233 public:
00234 static const char * StaticAlgorithmName() {return "ECDSA";}
00235 };
00236
00237
00238 template <class EC>
00239 class DL_Algorithm_ECNR : public DL_Algorithm_NR<typename EC::Point>
00240 {
00241 public:
00242 static const char * StaticAlgorithmName() {return "ECNR";}
00243 };
00244
00245
00246 template <class EC, class H>
00247 struct ECDSA : public DL_SSA<DL_Keys_ECDSA<EC>, DL_Algorithm_ECDSA<EC>, H>
00248 {
00249 };
00250
00251
00252 template <class EC, class H = SHA>
00253 struct ECNR : public DL_SSA<DL_Keys_EC<EC>, DL_Algorithm_ECNR<EC>, H>
00254 {
00255 };
00256
00257
00258
00259
00260
00261 template <class EC, class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = false>
00262 struct ECIES
00263 : public DL_ES<
00264 DL_Keys_EC<EC>,
00265 DL_KeyAgreementAlgorithm_DH<typename EC::Point, COFACTOR_OPTION>,
00266 DL_KeyDerivationAlgorithm_P1363<typename EC::Point, DHAES_MODE, P1363_KDF2<SHA1> >,
00267 DL_EncryptionAlgorithm_Xor<HMAC<SHA1>, DHAES_MODE>,
00268 ECIES<EC> >
00269 {
00270 static std::string StaticAlgorithmName() {return "ECIES";}
00271 };
00272
00273 NAMESPACE_END
00274
00275 #endif