2014-08-21 15:15:04 +00:00
|
|
|
#ifndef SIGNATURE_H__
|
|
|
|
#define SIGNATURE_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <cryptopp/dsa.h>
|
2014-08-21 18:26:15 +00:00
|
|
|
#include <cryptopp/asn.h>
|
|
|
|
#include <cryptopp/oids.h>
|
2014-08-22 20:28:25 +00:00
|
|
|
#include <cryptopp/osrng.h>
|
2014-08-21 18:26:15 +00:00
|
|
|
#include <cryptopp/eccrypto.h>
|
2014-08-21 15:15:04 +00:00
|
|
|
#include "CryptoConst.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace crypto
|
|
|
|
{
|
|
|
|
class Verifier
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual ~Verifier () {};
|
2014-08-23 12:41:06 +00:00
|
|
|
virtual bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const = 0;
|
2014-08-22 01:57:24 +00:00
|
|
|
virtual size_t GetPublicKeyLen () const = 0;
|
|
|
|
virtual size_t GetSignatureLen () const = 0;
|
2014-08-21 15:15:04 +00:00
|
|
|
};
|
|
|
|
|
2014-08-22 20:48:30 +00:00
|
|
|
class Signer
|
2014-08-22 20:28:25 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-08-22 20:48:30 +00:00
|
|
|
virtual ~Signer () {};
|
2014-08-23 12:41:06 +00:00
|
|
|
virtual void Sign (CryptoPP::RandomNumberGenerator& rnd, const uint8_t * buf, int len, uint8_t * signature) const = 0;
|
2014-08-22 20:28:25 +00:00
|
|
|
};
|
|
|
|
|
2014-08-21 15:15:04 +00:00
|
|
|
class DSAVerifier: public Verifier
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
DSAVerifier (const uint8_t * signingKey)
|
|
|
|
{
|
|
|
|
m_PublicKey.Initialize (dsap, dsaq, dsag, CryptoPP::Integer (signingKey, 128));
|
|
|
|
}
|
|
|
|
|
2014-08-23 12:41:06 +00:00
|
|
|
bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
|
2014-08-21 15:15:04 +00:00
|
|
|
{
|
|
|
|
CryptoPP::DSA::Verifier verifier (m_PublicKey);
|
|
|
|
return verifier.VerifyMessage (buf, len, signature, 40);
|
|
|
|
}
|
|
|
|
|
2014-08-22 01:57:24 +00:00
|
|
|
size_t GetPublicKeyLen () const { return 128; };
|
|
|
|
size_t GetSignatureLen () const { return 40; };
|
|
|
|
|
2014-08-21 15:15:04 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
CryptoPP::DSA::PublicKey m_PublicKey;
|
2014-08-21 18:26:15 +00:00
|
|
|
};
|
|
|
|
|
2014-08-22 20:48:30 +00:00
|
|
|
class DSASigner: public Signer
|
2014-08-22 20:28:25 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-08-22 20:48:30 +00:00
|
|
|
DSASigner (const uint8_t * signingPrivateKey)
|
2014-08-22 20:28:25 +00:00
|
|
|
{
|
|
|
|
m_PrivateKey.Initialize (dsap, dsaq, dsag, CryptoPP::Integer (signingPrivateKey, 20));
|
|
|
|
}
|
|
|
|
|
2014-08-23 12:41:06 +00:00
|
|
|
void Sign (CryptoPP::RandomNumberGenerator& rnd, const uint8_t * buf, int len, uint8_t * signature) const
|
2014-08-22 20:28:25 +00:00
|
|
|
{
|
|
|
|
CryptoPP::DSA::Signer signer (m_PrivateKey);
|
|
|
|
signer.SignMessage (rnd, buf, len, signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
CryptoPP::DSA::PrivateKey m_PrivateKey;
|
|
|
|
};
|
|
|
|
|
2014-08-22 20:48:30 +00:00
|
|
|
inline void CreateDSARandomKeys (CryptoPP::RandomNumberGenerator& rnd, uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
|
|
|
|
{
|
|
|
|
CryptoPP::DSA::PrivateKey privateKey;
|
|
|
|
CryptoPP::DSA::PublicKey publicKey;
|
|
|
|
privateKey.Initialize (rnd, dsap, dsaq, dsag);
|
|
|
|
privateKey.MakePublicKey (publicKey);
|
|
|
|
privateKey.GetPrivateExponent ().Encode (signingPrivateKey, 20);
|
|
|
|
publicKey.GetPublicElement ().Encode (signingPublicKey, 128);
|
|
|
|
}
|
|
|
|
|
2014-08-21 18:26:15 +00:00
|
|
|
class ECDSAP256Verifier: public Verifier
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ECDSAP256Verifier (const uint8_t * signingKey)
|
|
|
|
{
|
|
|
|
m_PublicKey.Initialize (CryptoPP::ASN1::secp256r1(),
|
|
|
|
CryptoPP::ECP::Point (CryptoPP::Integer (signingKey, 32),
|
|
|
|
CryptoPP::Integer (signingKey + 32, 32)));
|
|
|
|
}
|
|
|
|
|
2014-08-23 12:41:06 +00:00
|
|
|
bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
|
2014-08-21 18:26:15 +00:00
|
|
|
{
|
|
|
|
CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA256>::Verifier verifier (m_PublicKey);
|
|
|
|
return verifier.VerifyMessage (buf, len, signature, 64);
|
|
|
|
}
|
|
|
|
|
2014-08-22 01:57:24 +00:00
|
|
|
size_t GetPublicKeyLen () const { return 64; };
|
|
|
|
size_t GetSignatureLen () const { return 64; };
|
|
|
|
|
2014-08-21 18:26:15 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA256>::PublicKey m_PublicKey;
|
|
|
|
};
|
2014-08-22 20:28:25 +00:00
|
|
|
|
2014-08-22 20:48:30 +00:00
|
|
|
class ECDSAP256Signer: public Signer
|
2014-08-22 20:28:25 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-08-22 20:48:30 +00:00
|
|
|
ECDSAP256Signer (const uint8_t * signingPrivateKey)
|
2014-08-22 20:28:25 +00:00
|
|
|
{
|
|
|
|
m_PrivateKey.Initialize (CryptoPP::ASN1::secp256r1(), CryptoPP::Integer (signingPrivateKey, 32));
|
|
|
|
}
|
|
|
|
|
2014-08-23 12:41:06 +00:00
|
|
|
void Sign (CryptoPP::RandomNumberGenerator& rnd, const uint8_t * buf, int len, uint8_t * signature) const
|
2014-08-22 20:28:25 +00:00
|
|
|
{
|
|
|
|
CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA256>::Signer signer (m_PrivateKey);
|
|
|
|
signer.SignMessage (rnd, buf, len, signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA256>::PrivateKey m_PrivateKey;
|
|
|
|
};
|
2014-08-22 20:48:30 +00:00
|
|
|
|
|
|
|
inline void CreateECDSAP256RandomKeys (CryptoPP::RandomNumberGenerator& rnd, uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
|
|
|
|
{
|
|
|
|
CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA256>::PrivateKey privateKey;
|
|
|
|
CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA256>::PublicKey publicKey;
|
|
|
|
privateKey.Initialize (rnd, CryptoPP::ASN1::secp256r1());
|
|
|
|
privateKey.MakePublicKey (publicKey);
|
|
|
|
privateKey.GetPrivateExponent ().Encode (signingPrivateKey, 32);
|
|
|
|
auto q = publicKey.GetPublicElement ();
|
|
|
|
q.x.Encode (signingPublicKey, 32);
|
|
|
|
q.y.Encode (signingPublicKey + 32, 32);
|
|
|
|
}
|
2014-08-21 15:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|