2017-04-19 40 views
1

我想为BitTorrent BEP 44试验#1中运行测试向量,但我不会创建相同的签名失败,因为他们做的:BitTorrent的测试用例libsodium

305ac8aeb6c9c151fa120f120ea2cfb923564e11552d06a5d856091e5e853cff 1260d3f39e4999684aa92eb73ffd136e6f4f3ecbfda0ce53a1608ecd7ae21f01

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

c44ad65291c2b1087218db8a43e3fa7b73cfa01b585b0ff9e6b962ed50e701a1 6065277417ff5bbae43d9b76e52129d27bf2e33e8b043ea67ace7ff91d ae4d02

使用此代码:

#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); 
} 

似乎有点傻,我失踪,但我无法看到它。

+0

你检查是否私钥计算出与规范所示的相同的pubkey? – the8472

+1

我已经找到了答案,但还没有时间在这里回答。显然,私钥有(至少)两种不同的格式。第一种格式称为'ref10',由libsodium使用。第二种格式是'ref10',按照[这里](https://github.com/orlp/ed25519/issues/10#issuecomment-242761092)的说明使用单向函数进行转换。如果有人使用[这些函数](例如https://github.com/jedisct1/libsodium/blob/master/src/libsodium/include/sodium/crypto_sign_edwards25519sha512batch.h)以一个简单的答案来打我,我会标记它作为答案。 –

回答

1

如解释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); 
}