从原始 r 和 s 创建 DER 格式的 ECDSA 签名

Creating a DER formatted ECDSA signature from raw r and s

我有一个原始 ECDSA 签名:R 和 S 值。我需要签名的 DER 编码版本。有没有一种直接的方法可以使用 c 接口在 openssl 中执行此操作?

我目前的尝试是使用 i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp) 来填充一个 ECDSA_SIG*。调用 returns 非零,但目标缓冲区似乎没有改变。

我最初用 rs 值填充我的 ECDSA_SIG。我没有看到任何错误。手册页说当我调用 ECDSA_SIG_new

时应该分配 r 和 s
ECDSA_SIG* ec_sig = ECDSA_SIG_new();

if (NULL == BN_bin2bn(sig, 32, (ec_sig->r))) {
  dumpOpenSslErrors();
 }
DBG("post r  :%s\n", BN_bn2hex(ec_sig->r));

if (NULL == BN_bin2bn(sig + 32, 32, (ec_sig->s))) {
  dumpOpenSslErrors();
 }
DBG("post s  :%s\n", BN_bn2hex(ec_sig->s));

S 和 R 现已设置:

post r :397116930C282D1FCB71166A2D06728120CF2EE5CF6CCD4E2D822E8E0AE24A30 post s :9E997D4718A7603942834FBDD22A4B856FC4083704EDE62033CF1A77CB9822A9

现在制作编码签名。

int sig_size = i2d_ECDSA_SIG(ec_sig, NULL);
if (sig_size > 255) {
  DBG("signature is too large wants %d\n", sig_size);
 }
DBG("post i2d:%s\n", BN_bn2hex(ec_sig->s));

s 没有改变:

post i2d:9E997D4718A7603942834FBDD22A4B856FC4083704EDE62033CF1A77CB9822A9

此时我已经准备好了足够多的字节,我将目标设置为全部 6,因此很容易看到发生了什么变化。

unsigned char* sig_bytes = new unsigned char[256];
memset(sig_bytes, 6, 256);

sig_size = i2d_ECDSA_SIG(ec_sig, (&sig_bytes));
DBG("New size %d\n", sig_size);
DBG("post i2d:%s\n", BN_bn2hex(ec_sig->s));

hexDump("Sig ", (const byte*)sig_bytes, sig_size);

新尺寸为 71 New size 71 还是一样:

  `post i2d:9E997D4718A7603942834FBDD22A4B856FC4083704EDE62033CF1A77CB9822A9`

十六进制转储全是 6。

  --Sig --  
  0x06:   0x06:   0x06:   0x06:   0x06:   0x06:   0x06:   0x06: 
  0x06:   ...

转储仍然是全 6,即使调用没有 return0。我错过了什么绑定到 DER 编码这个原始签名?

i2d_ECDSA_SIG 修改它的第二个参数,增加签名的大小。来自 ecdsa.h:

/** DER encode content of ECDSA_SIG object (note: this function modifies *pp
 *  (*pp += length of the DER encoded signature)).
 *  \param  sig  pointer to the ECDSA_SIG object
 *  \param  pp   pointer to a unsigned char pointer for the output or NULL
 *  \return the length of the DER encoded ECDSA_SIG object or 0
 */
int   i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);

所以你需要在调用i2d_ECDSA_SIG时跟踪sig_bytes的原始值:

int sig_size = i2d_ECDSA_SIG(ec_sig, NULL);
unsigned char *sig_bytes = malloc(sig_size);
unsigned char *p;
memset(sig_bytes, 6, sig_size);

p = sig_bytes;
new_sig_size = i2d_ECDSA_SIG(_sig, &p);

// The value of p is now sig_bytes + sig_size, and the signature resides at sig_bytes

输出:

30 45 02 20 39 71 16 93 0C 28 2D 1F CB 71 16 6A
2D 06 72 81 20 CF 2E E5 CF 6C CD 4E 2D 82 2E 8E
0A E2 4A 30 02 21 00 9E 99 7D 47 18 A7 60 39 42
83 4F BD D2 2A 4B 85 6F C4 08 37 04 ED E6 20 33
CF 1A 77 CB 98 22 A9