digital-signature - BitTorrent 测试用例因 libsodium 失败

标签 digital-signature bittorrent libsodium

我正在尝试按照 BitTorrent BEP 44 中的描述运行测试向量。测试 #1,但我没有创建与他们相同的签名:

305ac8aeb6c9c151fa120f120ea2cfb923564e11552d06a5d856091e5e853cff 1260d3f39e4999684aa92eb73ffd136e6f4f3ecbfda0ce53a1608ecd7ae21f01

相反,我使用 libsodium 创建的签名是:

c44ad65291c2b1087218db8a43e3fa7b73cfa01b585b0ff9e6b962ed50e701a1 6065277417ff5bbae43d9b76e52129d27bf2e33e8b043ea67ace7ff91dae4d02

使用此代码:

#include <string.h>
#include <stdio.h>
#include <sodium/crypto_sign.h>

// Test vector #1 from http://bittorrent.org/beps/bep_0044.html
// Using libsodium.
int main(int argc, char *argv[])
{
    const char* buf = "3:seqi1e1:v12:Hello World!";

    const char* sk =
        "\xe0\x6d\x31\x83\xd1\x41\x59\x22\x84\x33\xed\x59\x92\x21\xb8\x0b"
        "\xd0\xa5\xce\x83\x52\xe4\xbd\xf0\x26\x2f\x76\x78\x6e\xf1\xc7\x4d"
        "\xb7\xe7\xa9\xfe\xa2\xc0\xeb\x26\x9d\x61\xe3\xb3\x8e\x45\x0a\x22"
        "\xe7\x54\x94\x1a\xc7\x84\x79\xd6\xc5\x4e\x1f\xaf\x60\x37\x88\x1d";

    unsigned char signature[crypto_sign_BYTES];

    crypto_sign_detached(signature,
            NULL,
            (const unsigned char*) buf,
            strlen(buf),
            (const unsigned char*) sk);

    char signed_buf[crypto_sign_BYTES * 2];

    for (int i = 0; i < sizeof(signature); ++i) {
        sprintf(signed_buf + i*2, "%.2x", signature[i]);
    }

    printf("%s\n", signed_buf);
}

似乎是我错过了一些愚蠢的东西,但我就是看不到它。

最佳答案

如上所述here私钥似乎(至少)有两种不同的格式。其中之一称为 ref10,它是 libsodium 使用的。它由 32 字节的种子与另外 32 字节的公钥连接而成。

我找不到其他格式的名称,但是 - 正如上面链接中所解释的 - 它基本上是使用 sha512 进行哈希处理的种子。更准确地说

void ref10_to_lib(
        unsigned char *private_key,
        const unsigned char *ref10_private_key)
{
    sha512(ref10_private_key, 32, private_key);
    private_key[0] &= 248;
    private_key[31] &= 63;
    private_key[31] |= 64;
}

BitTorrent 规范使用第二种格式,为了能够使用它,必须使用已弃用的 crypto_sign_edwards25519sha512batch函数而不是 crypto_sign_detached 如下:

#include <string.h>
#include <stdio.h>
#include <sodium/crypto_sign.h>
#include <sodium/crypto_sign_edwards25519sha512batch.h>

// Test vector #1 from http://bittorrent.org/beps/bep_0044.html
// Using libsodium.
int main(int argc, char *argv[])
{
    const char* buf = "3:seqi1e1:v12:Hello World!";

    const char* sk =
        "\xe0\x6d\x31\x83\xd1\x41\x59\x22\x84\x33\xed\x59\x92\x21\xb8\x0b"
        "\xd0\xa5\xce\x83\x52\xe4\xbd\xf0\x26\x2f\x76\x78\x6e\xf1\xc7\x4d"
        "\xb7\xe7\xa9\xfe\xa2\xc0\xeb\x26\x9d\x61\xe3\xb3\x8e\x45\x0a\x22"
        "\xe7\x54\x94\x1a\xc7\x84\x79\xd6\xc5\x4e\x1f\xaf\x60\x37\x88\x1d";

    unsigned char signature[crypto_sign_BYTES];

    crypto_sign_edwards25519sha512batch(
            signature,
            NULL,
            (const unsigned char*) buf,
            strlen(buf),
            (const unsigned char*) sk);

    char signed_buf[crypto_sign_BYTES * 2];

    for (int i = 0; i < sizeof(signature); ++i) {
        sprintf(signed_buf + i*2, "%.2x", signature[i]);
    }

    printf("%s\n", signed_buf);
}

关于digital-signature - BitTorrent 测试用例因 libsodium 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43500538/

相关文章:

python - 使用 PYODBC 将 BitTorrent 位字段插入 MSSQL 中的 VarBinary(MAX)

c - 如何将钠链接到我的 C 程序

php - 在 WAMP 上将 Libsodium 与 PHP 结合使用

xml - 是什么阻止我在东西上添加自己的签名?

JAVA 签名对象 - 没有安装的提供程序支持此 key : sun. security.rsa.RSAPrivateCrtKeyImpl

java - 如何签署自定义 JCE 安全提供程序

phpodium_crypto_pwhash_str_verify() 失败

java - 更新数字签名的 PDF 并重新签名

bittorrent - 解析来自 Tracker 服务器的 Annouce 响应中的 Peers IP 地址(bitTorrent)

ubuntu - 如何通过 ssh 使用远程 ubuntu 服务器下载 torrent 文件?