c++ - 什么库或软件盲目签名和验证?

标签 c++ cryptography rsa digital-signature blind-signature

<分区>

我最近听说了盲签名。我看到了关于它们如何工作的维基百科文章,但不想实现它。我可以使用哪些库(最好与 linux 和 windows 兼容)来实现盲签名并验证文件是否已签名?

我尝试查看 openssl 手册页,但我认为它不支持盲签名 http://www.openssl.org/docs/apps/dgst.html

我可以在 C++ 或 .NET 中实现我的应用程序,并且在生成过程和解析它们的输出时没有任何问题,但是比软件更喜欢库。

最佳答案

I can implement my app in either C++ or .NET ... What lib or software to sign and verify blindly?

这是一个 Crypto++基于答案。 Crypto++ 是 Wei Dai 编写的密码方案类库。该示例取自 Raw RSA | Blind Signatures在维基上。

jack 劳合社 Botan ,这是一个 C++11 加密和 TLS 库,可能具有 native 盲签名支持。

Crypto++ 缺少盲签名类。下面的方法遵循基本算法,详见 Blind Signatures。 .但是,它与维基百科的不同之处在于应用了 s(s'(x)) = x 交叉检查。交叉检查存在于 Chaum's original paper 中,但维基文章中缺少它。与 Chaum 的论文和维基百科的第二个区别是,下面的代码使用 H(m) 而不是 m。这是由于 Rabin in 1979 .

根据 Usability of padding scheme in blinded RSA signature?,您可能希望先应用填充函数或 RSA blind signatures in practice .另见 Is there a standard padding/format for RSA Blind Signatures?


#include "cryptlib.h"
#include "integer.h"
#include "nbtheory.h"
#include "osrng.h"
#include "rsa.h"
#include "sha.h"
using namespace CryptoPP;

#include <iostream>
#include <stdexcept>
using std::cout;
using std::endl;
using std::runtime_error;

int main(int argc, char* argv[])
{
    // Bob artificially small key pair
    AutoSeededRandomPool prng;
    RSA::PrivateKey privKey;

    privKey.GenerateRandomWithKeySize(prng, 64);
    RSA::PublicKey pubKey(privKey);

    // Convenience
    const Integer& n = pubKey.GetModulus();
    const Integer& e = pubKey.GetPublicExponent();
    const Integer& d = privKey.GetPrivateExponent();

    // Print params
    cout << "Pub mod: " << std::hex << pubKey.GetModulus() << endl;
    cout << "Pub exp: " << std::hex << e << endl;
    cout << "Priv mod: " << std::hex << privKey.GetModulus() << endl;
    cout << "Priv exp: " << std::hex << d << endl;

    // For sizing the hashed message buffer. This should be SHA256 size.
    const size_t SIG_SIZE = UnsignedMin(SHA256::BLOCKSIZE, n.ByteCount());

    // Scratch
    SecByteBlock buff1, buff2, buff3;

    // Alice original message to be signed by Bob
    SecByteBlock orig((const byte*)"secret", 6);
    Integer m(orig.data(), orig.size());
    cout << "Message: " << std::hex << m << endl;

    // Hash message per Rabin (1979)
    buff1.resize(SIG_SIZE);
    SHA256 hash1;
    hash1.CalculateTruncatedDigest(buff1, buff1.size(), orig, orig.size());

    // H(m) as Integer
    Integer hm(buff1.data(), buff1.size());
    cout << "H(m): " << std::hex << hm << endl;

    // Alice blinding
    Integer r;
    do {
        r.Randomize(prng, Integer::One(), n - Integer::One());
    } while (!RelativelyPrime(r, n));

    // Blinding factor
    Integer b = a_exp_b_mod_c(r, e, n);
    cout << "Random: " << std::hex << b << endl;

    // Alice blinded message
    Integer mm = a_times_b_mod_c(hm, b, n);
    cout << "Blind msg: " << std::hex << mm << endl;

    // Bob sign
    Integer ss = privKey.CalculateInverse(prng, mm);
    cout << "Blind sign: " << ss << endl;

    // Alice checks s(s'(x)) = x. This is from Chaum's paper
    Integer c = pubKey.ApplyFunction(ss);
    cout << "Check sign: " << c << endl;
    if (c != mm)
        throw runtime_error("Alice cross-check failed");

    // Alice remove blinding
    Integer s = a_times_b_mod_c(ss, r.InverseMod(n), n);
    cout << "Unblind sign: " << s << endl;

    // Eve verifies
    Integer v = pubKey.ApplyFunction(s);    
    cout << "Verify: " << std::hex << v << endl;

    // Convert to a string
    size_t req = v.MinEncodedSize();
    buff2.resize(req);
    v.Encode(&buff2[0], buff2.size());

    // Hash message per Rabin (1979)
    buff3.resize(SIG_SIZE);
    SHA256 hash2;
    hash2.CalculateTruncatedDigest(buff3, buff3.size(), orig, orig.size());

    // Constant time compare
    bool equal = buff2.size() == buff3.size() && VerifyBufsEqual(
        buff2.data(), buff3.data(), buff3.size());

    if (!equal)
        throw runtime_error("Eve verified failed");

    cout << "Verified signature" << endl;

    return 0;
}

这是构建和运行程序的结果:

$ g++ blind.cxx ./libcryptopp.a -o blind.exe
$ ./blind.exe
Pub mod: bbf62585f8486acbh
Pub exp: 11h
Priv mod: bbf62585f8486acbh
Priv exp: 31c1280c6bb08635h
Message: 736563726574h
H(m): 2bb80d537b1da3e3h
Random: 7db0ecdb0a09fad5h
Blinded msg: a8bf62a25b7b4b53h
Blind sign: 2646ab6b9d5b48dfh
Check sign: a8bf62a25b7b4b53h
Unblind sign: 418d211b9cbb2d00h
Verify: 2bb80d537b1da3e3h
Verified signature

关于c++ - 什么库或软件盲目签名和验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10471217/

相关文章:

java - 如何将字符串从 Java 服务器发送到 C++ 客户端?

node.js - Node 加密 aes256-cbc 0x0 填充示例

cryptography - RSA 密码系统的蒙哥马利模乘法的最终减法

java - iOS 和 JAva 中的 AES 和 RSA 加密

c++ - 按某个数据成员对结构的 STL 容器进行排序

c++ - 未解析的外部符号

c - 验证 Authenticode 签名是否来 self 们公司,用于自动更新程序

python - 如何在 Python 中验证 RSA SHA1 签名?

java - 将 "RSA/ECB/PKCS7Padding"与充气城堡一起使用

c++ - 如何在exe文件中构建C++项目嵌入所有动态链接库?