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

How to combine two Sources into new one in Crypto++?

情况

我有两个任意来源,假设一个 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;
}

我的问题

我不想将文件内容显式提取为字符串,然后进行连接并在之后进行验证。

问题

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

到目前为止我尝试了什么

我尝试 Source::TransferAll(...)Redirecter,但没有成功重定向到 SignatureVerificationFilter

I have two arbitrary sources, lets say a StringSource from a signature and a FileSource from the corresponding signed file. I now want to verify the files signature ...

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


Is there a way to pass two arbitrary sources where one represents the signature and the other one the signed content (might be a file or a string) to the verification entity?

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

示例分为四个部分:

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

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

Part 0 - 数据设置如下。这很无聊。注意 s3s1s2.

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

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

s3 = s1 + s2;

第 1 部分 - 数据打印如下。打印 s1s2s3 的哈希值。 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;

输出如下所示:

$ ./test.exe
s1: 45503354F9BC56C9B5B61276375A4C60F83A2F01
s2: 6A3AD5B683DE7CA57F07E8099268A8BC80FA200B
s3: BFC1882CEB24697A2B34D7CF8B95604B7109F28D
...

第 2 部分 - 这是事情变得有趣的地方。我们使用两个不同的StringSource来分别处理s1s2

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;

它产生以下输出:

$ ./test.exe
s1: 45503354F9BC56C9B5B61276375A4C60F83A2F01
s2: 6A3AD5B683DE7CA57F07E8099268A8BC80FA200B
s3: BFC1882CEB24697A2B34D7CF8B95604B7109F28D
s1 + s2: BFC1882CEB24697A2B34D7CF8B95604B7109F28D
...

上面的代码中发生了几件事。首先,我们将散列过滤器链动态附加和分离到源 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() 发送到过滤器链中),那么您将得到 [=29= 的串联] 和 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();

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

$ ./test.exe
s1: 45503354F9BC56C9B5B61276375A4C60F83A2F01
s2: 6A3AD5B683DE7CA57F07E8099268A8BC80FA200B
s3: BFC1882CEB24697A2B34D7CF8B95604B7109F28D
s1 + s2: 45503354F9BC56C9B5B61276375A4C60F83A2F016A3AD5B683DE7CA57F07E8099268A8BC80FA200BDA39A3EE5E6B4B0D3255BFEF95601890AFD80709

第 3 部分 - 这是您的用例。我们使用 StringSourceFileSource 分别处理 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;

这是 运行 完整示例的样子:

$ ./test.exe
s1: 45503354F9BC56C9B5B61276375A4C60F83A2F01
s2: 6A3AD5B683DE7CA57F07E8099268A8BC80FA200B
s3: BFC1882CEB24697A2B34D7CF8B95604B7109F28D
s1 + s2: BFC1882CEB24697A2B34D7CF8B95604B7109F28D
s1 + s2 (file): BFC1882CEB24697A2B34D7CF8B95604B7109F28D

通知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;
}

并且:

$ g++ test.cxx ./libcryptopp.a -o test.exe
$ ./test.exe
s1: 45503354F9BC56C9B5B61276375A4C60F83A2F01
s2: 6A3AD5B683DE7CA57F07E8099268A8BC80FA200B
s3: BFC1882CEB24697A2B34D7CF8B95604B7109F28D
s1 + s2: BFC1882CEB24697A2B34D7CF8B95604B7109F28D
s1 + s2 (file): BFC1882CEB24697A2B34D7CF8B95604B7109F28D

这里有一个 class 可以减轻您的痛苦。它在 MultipleSources class 中汇集了上述概念。 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;
};

你可以这样称呼它:

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();

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

MultipleSources ms(srcs, hf);
ms.PumpAll();