📄 eccrypto.h
字号:
: Base(oid, Q) {} \
Self(const EC &ec, const Point &G, const Integer &n, const Point &Q) \
: Base(ec, G, n, Q) {} \
Self(BufferedTransformation &bt) \
: Base(bt) {}
/// Elliptic Curve Private Key
template <class EC>
class ECPrivateKey : public ECPublicKey<EC>
{
public:
typedef typename EC::Point Point;
ECPrivateKey(const ECParameters<EC> ¶ms, const Point &Q, const Integer &d)
: ECPublicKey<EC>(params, Q), m_d(d) {}
ECPrivateKey(const OID &oid, const Point &Q, const Integer &d)
: ECPublicKey<EC>(oid, Q), m_d(d) {}
ECPrivateKey(const EC &ec, const Point &G, const Integer &n, const Point &Q, const Integer &d)
: ECPublicKey<EC>(ec, G, n, Q), m_d(d) {}
// generate a random private key
ECPrivateKey(RandomNumberGenerator &rng, const ECParameters<EC> ¶ms)
: ECPublicKey<EC>(params, Point()) {Randomize(rng);}
ECPrivateKey(RandomNumberGenerator &rng, const OID &oid)
: ECPublicKey<EC>(oid, Point()) {Randomize(rng);}
ECPrivateKey(RandomNumberGenerator &rng, const EC &ec, const Point &G, const Integer &n)
: ECPublicKey<EC>(ec, G, n, Point()) {Randomize(rng);}
// decode private key
ECPrivateKey(BufferedTransformation &bt);
void DEREncode(BufferedTransformation &bt) const;
const Integer& GetPrivateExponent() const {return m_d;}
protected:
typedef typename EC::FieldElement FieldElement;
void Randomize(RandomNumberGenerator &rng);
void RawDecode(BERSequenceDecoder &bt, bool needParameters);
Integer m_d;
};
#define EC_PRIVATE_KEY_CONSTRUCTORS(Self, Base) \
Self(const ECPrivateKey<EC> &key) \
: Base(key) {} \
Self(const ECParameters<EC> ¶ms, const Point &Q, const Integer &d) \
: Base(params, Q, d) {} \
Self(const OID& oid, const Point &Q, const Integer &d) \
: Base(oid, Q, d) {} \
Self(const EC &ec, const Point &G, const Integer &n, const Point &Q, const Integer &d) \
: Base(ec, G, n, Q, d) {} \
Self(RandomNumberGenerator &rng, const ECParameters<EC> ¶ms) \
: Base(rng, params) {} \
Self(RandomNumberGenerator &rng, const OID& oid) \
: Base(rng, oid) {} \
Self(RandomNumberGenerator &rng, const EC &ec, const Point &G, const Integer &n) \
: Base(rng, ec, G, n) {} \
Self(BufferedTransformation &bt) \
: Base(bt) {}
/// Elliptic Curve Digest Signature Verifier
template <class EC, ECSignatureScheme SS = ECNR>
class ECDigestVerifier : public ECPublicKey<EC>, public PK_WithPrecomputation<DigestVerifier>
{
public:
typedef typename EC::Point Point;
EC_PUBLIC_KEY_CONSTRUCTORS(ECDigestVerifier, ECPublicKey<EC>)
bool VerifyDigest(const byte *digest, unsigned int digestLen, const byte *signature) const;
unsigned int MaxDigestLength() const {return 0xffff;}
unsigned int DigestSignatureLength() const {return 2*ExponentLength();}
// exposed for validation testing
bool RawVerify(const Integer &e, const Integer &n, const Integer &s) const;
};
/// Elliptic Curve Digest Signer
template <class EC, ECSignatureScheme SS = ECNR>
class ECDigestSigner : public ECPrivateKey<EC>, public PK_WithPrecomputation<DigestSigner>
{
public:
typedef typename EC::Point Point;
EC_PRIVATE_KEY_CONSTRUCTORS(ECDigestSigner, ECPrivateKey<EC>)
void SignDigest(RandomNumberGenerator &, const byte *digest, unsigned int digestLen, byte *signature) const;
unsigned int MaxDigestLength() const {return 0xffff;}
unsigned int DigestSignatureLength() const {return 2*ExponentLength();}
/// exposed for validation testing
void RawSign(const Integer &k, const Integer &e, Integer &n, Integer &s) const;
};
/// Elliptic Curve Message Signer
template <class EC, class H, ECSignatureScheme SS = ECNR>
class ECSigner : public SignerTemplate<ECDigestSigner<EC, SS>, H>, public PK_WithPrecomputation<PK_Signer>
{
typedef ECDigestSigner<EC, SS> Base;
public:
typedef typename EC::Point Point;
EC_PRIVATE_KEY_CONSTRUCTORS(ECSigner, Base)
};
/// Elliptic Curve Message Signature Verifier
template <class EC, class H, ECSignatureScheme SS = ECNR>
class ECVerifier : public VerifierTemplate<ECDigestVerifier<EC, SS>, H>, public PK_WithPrecomputation<PK_Verifier>
{
typedef ECDigestVerifier<EC, SS> Base;
public:
typedef typename EC::Point Point;
EC_PUBLIC_KEY_CONSTRUCTORS(ECVerifier, Base)
};
/// Elliptic Curve ECIES, AKA <a href="http://www.weidai.com/scan-mirror/ca.html#EC-DHAES">EC-DHAES</a>
template <class EC, class MAC = HMAC<SHA>, class KDF = P1363_KDF2<SHA> >
class ECEncryptor : public ECPublicKey<EC>, public PK_WithPrecomputation<PK_Encryptor>
{
public:
typedef typename EC::Point Point;
EC_PUBLIC_KEY_CONSTRUCTORS(ECEncryptor, ECPublicKey<EC>)
unsigned int MaxPlainTextLength(unsigned int cipherTextLength) const
{return cipherTextLength < CipherTextLength(0) ? 0 : cipherTextLength - CipherTextLength(0);}
unsigned int CipherTextLength(unsigned int plainTextLength) const
{return plainTextLength + MAC::DIGESTSIZE + EncodedPointSize();}
void Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText)
{
Integer x(rng, 1, m_n-1);
Point Q = m_Gpc.Multiply(x);
EncodePoint(cipherText, Q);
cipherText += EncodedPointSize();
SecByteBlock agreedSecret(FieldElementLength());
Point Q1 = m_Qpc.Multiply(x);
Q1.x.Encode(agreedSecret, agreedSecret.size);
SecByteBlock derivedKey(plainTextLength + MAC::DEFAULT_KEYLENGTH);
KDF::DeriveKey(derivedKey, derivedKey.size, agreedSecret, agreedSecret.size);
xorbuf(cipherText, plainText, derivedKey, plainTextLength);
MAC mac(derivedKey + plainTextLength);
mac.CalculateDigest(cipherText + plainTextLength, cipherText, plainTextLength);
}
};
/// Elliptic Curve ECIES, AKA <a href="http://www.weidai.com/scan-mirror/ca.html#/// Elliptic Curve ECIES, AKA <a href="http://www.weidai.com/scan-mirror/ca.html#EC-DHAES">EC-DHAES</a>
template <class EC, class MAC = HMAC<SHA>, class KDF = P1363_KDF2<SHA> >
class ECDecryptor : public ECPrivateKey<EC>, public PK_Decryptor
{
public:
typedef typename EC::Point Point;
EC_PRIVATE_KEY_CONSTRUCTORS(ECDecryptor, ECPrivateKey<EC>)
unsigned int MaxPlainTextLength(unsigned int cipherTextLength) const
{return cipherTextLength < CipherTextLength(0) ? 0 : cipherTextLength - CipherTextLength(0);}
unsigned int CipherTextLength(unsigned int plainTextLength) const
{return plainTextLength + MAC::DIGESTSIZE + EncodedPointSize();}
unsigned int Decrypt(const byte *cipherText, unsigned int cipherTextLength, byte *plainText)
{
Point Q;
if (!GetCurve().DecodePoint(Q, cipherText, EncodedPointSize()) || !GetCurve().VerifyPoint(Q) || Q.identity)
return 0;
cipherText += EncodedPointSize();
const Integer e[2] = {m_n, m_d};
Point R[2];
GetCurve().SimultaneousMultiply(R, Q, e, 2);
if (!R[0].identity || R[1].identity)
return 0;
SecByteBlock agreedSecret(FieldElementLength());
R[1].x.Encode(agreedSecret, agreedSecret.size);
unsigned int plainTextLength = MaxPlainTextLength(cipherTextLength);
SecByteBlock derivedKey(plainTextLength + MAC::DEFAULT_KEYLENGTH);
KDF::DeriveKey(derivedKey, derivedKey.size, agreedSecret, agreedSecret.size);
MAC mac(derivedKey + plainTextLength);
if (!mac.VerifyDigest(cipherText + plainTextLength, cipherText, plainTextLength))
return 0;
xorbuf(plainText, cipherText, derivedKey, plainTextLength);
return plainTextLength;
}
};
NAMESPACE_END
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -