如何在 Crypto++ 中将两个源合并为一个新源?

xmo*_*oex 5 c++ crypto++ dsa

情况

我有两个任意来源,比如说StringSource来自签名的一个和FileSource来自相应签名文件的一个。我现在想验证当前执行的文件签名,如下所示:

bool VerifyFile(const ECDSA<ECP, SHA512>::PublicKey &key,
                const std::string &filename,
                const std::string &signatureString) {
    std::string fileContentString;
    FileSource(filename.c_str(), true,
               new CryptoPP::StringSink(fileContentString));

    bool result = false;
    StringSource(signatureString + fileContentString, true,
                 new SignatureVerificationFilter(
                         ECDSA<ECP, SHA512>::Verifier(key),
                         new ArraySink((byte *) &result, sizeof(result))
                 ) // SignatureVerificationFilter
    );
    return result;
}
Run Code Online (Sandbox Code Playgroud)

我的问题

我不想将文件的内容显式提取到字符串中,然后进行串联并随后进行验证。

问题

有没有一种方法可以将两个任意源传递给验证实体,其中一个表示签名,另一个表示签名内容(可能是文件或字符串)?

到目前为止我尝试过的

我尝试重定向Source::TransferAll(...)到 a ,但没有成功。RedirecterSignatureVerificationFilter

jww*_*jww 2

我有两个任意源,比如说来自签名的 StringSource 和来自相应签名文件的 FileSource。我现在想验证文件签名......

在同一个过滤器链上使用多个源可能很棘手。我知道图书馆有一些内置的课程,但我从来不喜欢它们。它们采用多个输入通道并将它们解复用为单个通道。test.cpp您可以在、 函数SecretRecoverFile(第 650 行左右)和InformationRecoverFile(第 700 行左右)中看到它们的运行情况。


有没有一种方法可以将两个任意源传递给验证实体,其中一个表示签名,另一个表示签名内容(可能是文件或字符串)?

这是我将如何处理你想做的事情。下面的示例使用两个源并共享一个过滤器链。我通过使用 .hash 散列两个字符串来降低复杂性HashFilter。您的示例使用消息、签名、密钥对,SignatureVerificationFilter但它比向您展示如何操作所需的更复杂。

该示例分为四个部分:

  • 第 0 部分- 设置数据。创建两个 16K ASCII 字符串。一个字符串也被写入到一个文件中。
  • 第1部分- 打印数据。Hash(s1)Hash(s2)Hash(s1+s2)打印出来。
  • 第 2 部分- 使用两个字符串源。Hash(s1+s2)是使用两个创建的StringSources
  • 第三部分- 使用一个字符串源和一个文件源。使用一和一Hash(s1+s2)创建StringSourceFileSource

为了说明这一点,简化的示例计算Hash(s1+s2)。在您的上下文中,操作是Verify(key, s1+s2),其中key是公钥,s1是签名,s2是文件的内容。

第 0 部分- 数据设置如下。这很无聊。通知s3是一个串联s1是和s2

std::string s1, s2, s3;
const size_t size = 1024*16+1;

random_string(s1, size);
random_string(s2, size);

s3 = s1 + s2;
Run Code Online (Sandbox Code Playgroud)

第 1 部分- 数据打印如下。和s1的哈希值s2s3被打印。s3是最重要的。s3是我们需要使用两个不同的来源来实现的。

std::string r;
StringSource ss1(s1, true, new HashFilter(hash, new StringSink(r)));

std::cout << "s1: ";
hex.Put((const byte*)r.data(), r.size());
std::cout << std::endl;

r.clear();
StringSource ss2(s2, true, new HashFilter(hash, new StringSink(r)));

std::cout << "s2: ";
hex.Put((const byte*)r.data(), r.size());
std::cout << std::endl;

r.clear();
StringSource ss3(s3, true, new HashFilter(hash, new StringSink(r)));

std::cout << "s3: ";
hex.Put((const byte*)r.data(), r.size());
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样:

std::string s1, s2, s3;
const size_t size = 1024*16+1;

random_string(s1, size);
random_string(s2, size);

s3 = s1 + s2;
Run Code Online (Sandbox Code Playgroud)

第 2 部分- 这就是事情变得有趣的地方。我们使用两种不同的方式StringSource来处理s1处理s2

StringSource ss4(s1, false);
StringSource ss5(s2, false);

HashFilter hf1(hash, new StringSink(r));

ss4.Attach(new Redirector(hf1));
ss4.Pump(LWORD_MAX);
ss4.Detach();

ss5.Attach(new Redirector(hf1));
ss5.Pump(LWORD_MAX);
ss5.Detach();

hf1.MessageEnd();

std::cout << "s1 + s2: ";
hex.Put((const byte*)r.data(), r.size());
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)

它产生以下输出:

std::string r;
StringSource ss1(s1, true, new HashFilter(hash, new StringSink(r)));

std::cout << "s1: ";
hex.Put((const byte*)r.data(), r.size());
std::cout << std::endl;

r.clear();
StringSource ss2(s2, true, new HashFilter(hash, new StringSink(r)));

std::cout << "s2: ";
hex.Put((const byte*)r.data(), r.size());
std::cout << std::endl;

r.clear();
StringSource ss3(s3, true, new HashFilter(hash, new StringSink(r)));

std::cout << "s3: ";
hex.Put((const byte*)r.data(), r.size());
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)

上面的代码中发生了几件事。首先,我们动态地将哈希过滤器链附加和分离到源ss4ss5

其次,一旦连接了过滤器,我们就会Pump(LWORD_MAX)将所有数据从源泵入过滤器链。我们不使用PumpAll()因为PumpAll()表示当前消息的结束并生成一个MessageEnd(). 我们正在分多个部分处理一条消息;我们不处理多条消息。所以MessageEnd()当我们决定的时候我们只想要一个。

第三,一旦我们完成了源代码,我们就不再调用析构Detach函数StringSource导致虚假MessageEnd()消息进入过滤器链。同样,我们正在分多个部分处理一条消息;我们不处理多条消息。所以MessageEnd()当我们决定的时候我们只想要一个。

第四,当我们将数据发送到过滤器后,我们调用hf.MessageEnd()告诉过滤器处理所有待处理或缓冲的数据。这是我们想要打电话的时候MessageEnd(),而不是之前。

第五,我们Detach()在完成时调用而不是Attach(). Detach()删除现有的过滤器链并避免内存泄漏。Attach()附加新链但不删除现有过滤器或链。由于我们使用的是Redirector我们的HashFilter生存。最终将HashFilter作为自动堆栈变量进行清理。

顺便说一句,如果使用ss4.PumpAll()和(或允许析构函数发送到过滤器链中),那么您将得到和的串联,因为它对于过滤器来说看起来像是两条不同的消息,而不是两个部分的一条消息。下面的代码是错误的:ss5.PumpAll()MessageEnd()Hash(s1)Hash(s2)

StringSource ss4(s1, false);
StringSource ss5(s2, false);

HashFilter hf1(hash, new StringSink(r));

ss4.Attach(new Redirector(hf1));
// ss4.Pump(LWORD_MAX);
ss4.PumpAll();  // MessageEnd
ss4.Detach();

ss5.Attach(new Redirector(hf1));
// ss5.Pump(LWORD_MAX);
ss5.PumpAll();  // MessageEnd
ss5.Detach();

// Third MessageEnd
hf1.MessageEnd();
Run Code Online (Sandbox Code Playgroud)

上面的错误代码会产生Hash(s1) || Hash(s2) || Hash(<empty string>)

$ ./test.exe
s1: 45503354F9BC56C9B5B61276375A4C60F83A2F01
s2: 6A3AD5B683DE7CA57F07E8099268A8BC80FA200B
s3: BFC1882CEB24697A2B34D7CF8B95604B7109F28D
...
Run Code Online (Sandbox Code Playgroud)

第 3 部分- 这是您的用例。我们使用StringSourceand单独FileSource处理s1s2请记住,该字符串s2被写入名为test.dat.

StringSource ss6(s1, false);
FileSource fs1("test.dat", false);

HashFilter hf2(hash, new StringSink(r));

ss6.Attach(new Redirector(hf2));
ss6.Pump(LWORD_MAX);
ss6.Detach();

fs1.Attach(new Redirector(hf2));
fs1.Pump(LWORD_MAX);
fs1.Detach();

hf2.MessageEnd();

std::cout << "s1 + s2 (file): ";
hex.Put((const byte*)r.data(), r.size());
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)

运行完整示例如下:

StringSource ss4(s1, false);
StringSource ss5(s2, false);

HashFilter hf1(hash, new StringSink(r));

ss4.Attach(new Redirector(hf1));
ss4.Pump(LWORD_MAX);
ss4.Detach();

ss5.Attach(new Redirector(hf1));
ss5.Pump(LWORD_MAX);
ss5.Detach();

hf1.MessageEnd();

std::cout << "s1 + s2: ";
hex.Put((const byte*)r.data(), r.size());
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)

注意s3= s1 + s2= s1 + s2 (file)


$ cat test.cxx

#include "cryptlib.h"
#include "filters.h"
#include "files.h"
#include "sha.h"
#include "hex.h"

#include <string>
#include <iostream>

void random_string(std::string& str, size_t len)
{
    const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
    const size_t size = sizeof(alphanum) - 1;

    str.reserve(len);
    for (size_t i = 0; i < len; ++i)
        str.push_back(alphanum[rand() % size]);
}

int main(int argc, char* argv[])
{
    using namespace CryptoPP;

    ////////////////////////// Part 0 //////////////////////////

    // Deterministic
    std::srand(0);

    std::string s1, s2, s3, r;
    const size_t size = 1024*16+1;

    random_string(s1, size);
    random_string(s2, size);

    // Concatenate for verification
    s3 = s1 + s2;

    // Write s2 to file
    StringSource(s2, true, new FileSink("test.dat"));

    // Hashing, resets after use
    SHA1 hash;

    // Printing hex encoded string to std::cout
    HexEncoder hex(new FileSink(std::cout));

    ////////////////////////// Part 1 //////////////////////////

    r.clear();
    StringSource ss1(s1, true, new HashFilter(hash, new StringSink(r)));

    std::cout << "s1: ";
    hex.Put((const byte*)r.data(), r.size());
    std::cout << std::endl;

    r.clear();
    StringSource ss2(s2, true, new HashFilter(hash, new StringSink(r)));

    std::cout << "s2: ";
    hex.Put((const byte*)r.data(), r.size());
    std::cout << std::endl;

    r.clear();
    StringSource ss3(s3, true, new HashFilter(hash, new StringSink(r)));

    std::cout << "s3: ";
    hex.Put((const byte*)r.data(), r.size());
    std::cout << std::endl;

    ////////////////////////// Part 2 //////////////////////////

    r.clear();
    StringSource ss4(s1, false);
    StringSource ss5(s2, false);

    HashFilter hf1(hash, new StringSink(r));

    ss4.Attach(new Redirector(hf1));
    ss4.Pump(LWORD_MAX);
    ss4.Detach();

    ss5.Attach(new Redirector(hf1));
    ss5.Pump(LWORD_MAX);
    ss5.Detach();

    hf1.MessageEnd();

    std::cout << "s1 + s2: ";
    hex.Put((const byte*)r.data(), r.size());
    std::cout << std::endl;

    ////////////////////////// Part 3 //////////////////////////

    r.clear();
    StringSource ss6(s1, false);
    FileSource fs1("test.dat", false);

    HashFilter hf2(hash, new StringSink(r));

    ss6.Attach(new Redirector(hf2));
    ss6.Pump(LWORD_MAX);
    ss6.Detach();

    fs1.Attach(new Redirector(hf2));
    fs1.Pump(LWORD_MAX);
    fs1.Detach();

    hf2.MessageEnd();

    std::cout << "s1 + s2 (file): ";
    hex.Put((const byte*)r.data(), r.size());
    std::cout << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

和:

$ ./test.exe
s1: 45503354F9BC56C9B5B61276375A4C60F83A2F01
s2: 6A3AD5B683DE7CA57F07E8099268A8BC80FA200B
s3: BFC1882CEB24697A2B34D7CF8B95604B7109F28D
s1 + s2: BFC1882CEB24697A2B34D7CF8B95604B7109F28D
...
Run Code Online (Sandbox Code Playgroud)

这里有一个课程可以减轻您的痛苦。它将上述概念汇集在一个MultipleSources类中。MultipleSources只是该Source接口的部分实现,但它应该具有您需要的所有部分。

class MultipleSources
{
public:
    MultipleSources(std::vector<Source*>& source, Filter& filter)
    : m_s(source), m_f(filter)
    {
    }

    void Pump(lword pumpMax, bool messageEnd)
    {
        for (size_t i=0; pumpMax && i<m_s.size(); ++i)
        {
            lword n = pumpMax;
            m_s[i]->Attach(new Redirector(m_f));            
            m_s[i]->Pump2(n);
            m_s[i]->Detach();
            pumpMax -= n;
        }

        if (messageEnd)
            m_f.MessageEnd();
    }

    void PumpAll()
    {
        for (size_t i=0; i<m_s.size(); ++i)
        {
            m_s[i]->Attach(new Redirector(m_f));
            m_s[i]->Pump(LWORD_MAX);
            m_s[i]->Detach();
        }

        m_f.MessageEnd();
    }

private:
    std::vector<Source*>& m_s;
    Filter &m_f;
};
Run Code Online (Sandbox Code Playgroud)

你可以这样称呼它:

StringSource ss(s1, false);
FileSource fs("test.dat", false);
HashFilter hf(hash, new StringSink(r));

std::vector<Source*> srcs;
srcs.push_back(&ss);
srcs.push_back(&fs);

MultipleSources ms(srcs, hf);
ms.Pump(LWORD_MAX, false);

hf.MessageEnd();
Run Code Online (Sandbox Code Playgroud)

或者您可以使用并获得相同的结果,但在这种情况下PumpAll您不会调用,因为表示消息结束。hf.MessageEnd();PumpAll

MultipleSources ms(srcs, hf);
ms.PumpAll();
Run Code Online (Sandbox Code Playgroud)