Alle 20:31, venerd� 3 settembre 2004, Faizul Abdullah ha scritto:
> Can someone kindly post a sample code to do signing
> and signature verification using cryptopp 5.2.1
> please?
Below you found a complete sample for EC over GF(p)
-------------------------------------------------------------------------------------------------------------------------------------------
#include <cryptopp/files.h>
#include <cryptopp/filters.h>
#include <cryptopp/osrng.h>
#include <cryptopp/oids.h>
#include <cryptopp/ecp.h>
#include <cryptopp/eccrypto.h>
#include <unistd.h>
#include <stdlib.h>

#define PUBLICKEY 0x0F
#define PRIVATEKEY 0xF0
#define KEYPAIR 0xFF

using namespace CryptoPP;
using namespace std;

const char* g_pszPubFile = "ECDSA.pub";
const char* g_pszPrivFile = "ECDSA.pvk";
const string g_strName = ECDSA<ECP>::StaticAlgorithmName() + " over GF(p)";

struct NameToOID
{
    const char* pszName;
    OID oid;
};

const struct NameToOID Table[15] =
{
    {"112r1", ASN1::secp112r1()},
    {"112r2", ASN1::secp112r2()},
    {"128r1", ASN1::secp128r1()},
    {"128r2", ASN1::secp128r2()},
    {"160r1", ASN1::secp160r1()},
    {"160k1", ASN1::secp160k1()},
    {"160r2", ASN1::secp160r2()},
    {"192r1", ASN1::secp192r1()},
    {"192k1", ASN1::secp192k1()},
    {"224k1", ASN1::secp224k1()},
    {"224r1", ASN1::secp224r1()},
    {"256r1", ASN1::secp256r1()},
    {"256k1", ASN1::secp256k1()},
    {"384r1", ASN1::secp384r1()},
    {"521r1", ASN1::secp521r1()}
};

void ShowGroupParam(const DL_GroupParameters_EC<ECP>& group)
{
    cout << g_strName.c_str() << " Group Parameter:" << endl;
    cout << "Curve Parameter: Field Modulus p = " << 
group.GetCurve().FieldSize() << endl;
    cout << "\t\t Field Element a = " << group.GetCurve().GetA() << endl;
    cout << "\t\t Field Element b = " << group.GetCurve().GetB() << endl;
    cout << "SubGroup Generator g = Point(" << group.GetSubgroupGenerator().x 
<< " , " << group.GetSubgroupGenerator().y << ")" << endl;
    cout << "SubGroup Order n = " << group.GetSubgroupOrder() << endl;
    cout << "CoFactor k       = " << group.GetCofactor() << endl;
}

void ShowPrivateKey(const DL_Keys_EC<ECP>::PrivateKey& key)
{
    cout << g_strName.c_str() << " Private Key x = ";
    cout << key.GetPrivateExponent() << endl;
}

void ShowPublicKey(const DL_Keys_EC<ECP>::PublicKey& key)
{
    cout << g_strName.c_str() << " Public Key y = ";
    cout << "Point(" << key.GetPublicElement().x << " , " << 
key.GetPublicElement().y << ")" << endl;
}

void ShowKeys(RandomNumberGenerator& rng, int what)
{
    if (what & PUBLICKEY)
    {
        FileSource pubFile(g_pszPubFile, true);
        ECDSA<ECP>::Verifier pub(pubFile);
        if (pub.AccessKey().Validate(rng, 3))
        {
            ShowGroupParam(pub.GetKey().GetGroupParameters());
            ShowPublicKey(pub.GetKey());
        }
        else
            cout << g_pszPubFile << " contain an invalid " << g_strName.c_str() << 
"Public Key!" << endl;
    }
    if (what & PRIVATEKEY)
    {
        FileSource privFile(g_pszPrivFile, true);
        ECDSA<ECP>::Signer priv(privFile);
        if (priv.AccessKey().Validate(rng, 3))
        {
            if (!(what & PUBLICKEY))
                ShowGroupParam(priv.GetKey().GetGroupParameters());
            ShowPrivateKey(priv.GetKey());
        }
        else
            cout << g_pszPrivFile << " contain an invalid " << g_strName.c_str() << 
"Private Key!" << endl;
    }
}

void GenerateECPKey(RandomNumberGenerator& rng, const OID& oid)
{
    ECDSA<ECP>::Signer priv(rng, oid);
    FileSink privFile(g_pszPrivFile);
    priv.AccessKey().AccessGroupParameters().SetEncodeAsOID(true);
    priv.AccessKey().DEREncode(privFile);
    privFile.MessageEnd();
    ShowGroupParam(priv.GetKey().GetGroupParameters());
    ShowPrivateKey(priv.GetKey());

    ECDSA<ECP>::Verifier pub(priv);
    FileSink pubFile(g_pszPubFile);
    pub.AccessKey().AccessGroupParameters().SetEncodeAsOID(true);
    pub.AccessKey().DEREncode(pubFile);
    pubFile.MessageEnd();
    ShowPublicKey(pub.GetKey());
}

void ECPSignFile(const char *messageFilename, const char *signatureFilename, 
RandomNumberGenerator& rng)
{
    FileSource privFile(g_pszPrivFile, true);
    ECDSA<ECP>::Signer priv(privFile);
    FileSource f(messageFilename, true, new SignerFilter(rng, priv, 
        new FileSink(signatureFilename)));
}

bool ECPVerifyFile(const char *messageFilename, const char *signatureFilename, 
RandomNumberGenerator& rng)
{
    FileSource pubFile(g_pszPubFile, true);
    ECDSA<ECP>::Verifier pub(pubFile);
    FileSource signatureFile(signatureFilename, true);
    if (signatureFile.MaxRetrievable() != pub.SignatureLength())
        return false;
    SecByteBlock signature(pub.SignatureLength());
    signatureFile.Get(signature, signature.size());

    VerifierFilter *verifierFilter = new VerifierFilter(pub);
    verifierFilter->Put(signature, pub.SignatureLength());
    FileSource f(messageFilename, true, verifierFilter);

    return verifierFilter->GetLastResult();
}

void Usage()
{
    FileSource usage("usage.dat", true, new FileSink(cout));
    cout << "EC names:" << endl;
    for (int i = 0; i < 15; i++)
        cout << Table[i].pszName << endl;
} 

int main(int argc, char* argv[])
{
    if (argc == 1)
    {
        Usage();
        return 0;
    }

    try
    {
        AutoSeededX917RNG<DES_EDE3> rng;
        bool bSign = false, bVerify = false;
        int c = 0;
        char *pszShowArg = NULL, *pszEC = NULL;
        while ((c = getopt(argc, argv, "k:svw:")) != -1)
        {
            switch (c)
            {
                case '?':
                    Usage();
                    return 0;
                case 'k':
                    pszEC = optarg;
                    break;
                case 's':
                    bSign = true;
                    break;
                case 'v':
                    bVerify = true;
                    break;
                case 'w':
                    pszShowArg = optarg;
                    break;
            }
        }
        int nArg = argc - optind;
        if ((nArg != 2 && (bSign || bVerify)) || (nArg > 0 && !(bSign || bVerify))) 
        {
            Usage();
            return 3;
        }       
        if (pszEC != NULL)
        {
            cout << "Generate " << g_strName.c_str() << " KeyPair for secp" << pszEC  
<< endl;
            int i;
            for (i = 0; i < 15; i++)
            {
                if (strcmp(pszEC, Table[i].pszName))
                    continue;
                GenerateECPKey(rng, Table[i].oid); 
                break;
            }
            if (i == 15)
            {
                Usage();
                return 0;
            }
        }
        if (pszShowArg != NULL)
        {
            int what = 0;
            if (strcmp("pvk", pszShowArg) == 0)
                what = PRIVATEKEY;
            else if (strcmp("pub", pszShowArg) == 0)
                what = PUBLICKEY;
            else if (strcmp("pair", pszShowArg) == 0)
                what = KEYPAIR;
            else
            {
                Usage();
                return 0;
            }
            ShowKeys(rng, what);
        }
        if (nArg == 0)
            return 0;
        if (bSign)
        {
            cout << g_strName.c_str();
            cout << " Signing\nMessage: " << argv[optind] << "\tSignature: " << 
argv[optind + 1] << endl;
            ECPSignFile(argv[optind], argv[optind + 1], rng); 
            cout << "Done!" << endl;
        }
        if (bVerify)
        {
            cout << g_strName.c_str();
            cout << " Verifying\nMessage: " << argv[optind] << "\tSignature: " << 
argv[optind + 1] << endl;
            if (ECPVerifyFile(argv[optind], argv[optind + 1], rng))
                cout << "Verify OK" << endl;
            else
                cout << "Verify Failed" << endl; 
        }
    }
    catch (const CryptoPP::Exception &e)
    {
        cout << "\nCryptoPP::Exception caught: " << e.what() << endl;
        return 1;
    }
    catch (const std::exception &e)
    {
        cout << "\nstd::exception caught: " << e.what() << endl;
        return 2;
    }
    return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 
Gerardo Maiorano

Reply via email to