python ECDSA 和 C micro-ecc 库之间的 ECDSA 签名和验证问题

Pra*_*B V 3 c python cryptography public-key-encryption ecdsa

当我执行以下操作时,我遇到问题:

  1. 使用 python 创建 ECDSA SECP256k1 曲线的公钥对并将其打印在终端上。
  2. 将密钥对复制粘贴到 Python 脚本和 Visual Studio 中(使用 micro-ecc 库的 C 编码)。因此,每次运行代码时都不会生成新密钥。
  3. 使用私钥在 Python 中签署消息(“Hello”),并在终端上打印签名和消息。我知道必须首先使用标准哈希算法(例如 SHA256)对消息进行哈希处理,但在本例中,我直接使用消息进行签名。
  4. 将签名和相同的消息(哈希)复制到 Visual Studio 中。
  5. 当我在 Visual Studio 中调用验证 API 时,尽管使用相同的公钥、消息、签名和曲线,验证还是失败了。

我正在使用的库:

  1. Visual Studio 中用于 C 的 micro-ecc https://github.com/kmackay/micro-ecc
  2. 用于 python 的 ECDSA https://github.com/warner/python-ecdsa

我已经测试并确保的其他事情:

  1. 对于相同的密钥对(例如(sk,vk)),在 python 中进行签名和验证都可以正常工作。
  2. 对于相同的密钥对(例如(sk,vk)),在 Visual Studio 中进行签名和验证都可以正常工作。
  3. 在 Visual Studio 中使用 microECC 进行签名并在 python 中使用 ECDSA 进行验证失败,使用 ECDSA 进行签名并在 python 中使用 microECC 进行验证也失败。
  4. 我确信从终端粘贴的密钥复制实际上是预期的密钥,因为我已经从复制的签名密钥中交叉验证了验证密钥并且它是匹配的。

首先,我通过运行此脚本生成密钥

生成密钥.py

import ecdsa
from ecdsa import SigningKey, SECP256k1


sk  = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1) 
vk  = sk.get_verifying_key()

sklst = []
for e in bytearray(sk.to_string()):
    sklst.append(e)

vklst = []
for e in bytearray(vk.to_string()):
    vklst.append(e)

print("Private Key is:")
print(sklst)

print("Public Key is:")
print(vklst)
Run Code Online (Sandbox Code Playgroud)

GenerateKeys.py 的输出

Private Key is:
[38, 108, 90, 112, 230, 138, 62, 97, 107, 90, 227, 165, 207, 80, 251, 154, 17, 4, 73, 53, 33, 162, 33, 200, 243, 205, 116, 43, 36, 59, 201, 84]
Public Key is:
[163, 238, 83, 33, 229, 249, 105, 12, 141, 7, 214, 134, 148, 1, 198, 45, 13, 31, 9, 223, 85, 201, 98, 248, 73, 160, 40, 255, 64, 214, 250, 121, 234, 103, 212, 148, 197, 48, 210, 38, 166, 51, 30, 81, 119, 240, 125, 104, 237, 24, 3, 216, 229, 87, 45, 7, 115, 69, 94, 187, 236, 91, 142, 18]
Run Code Online (Sandbox Code Playgroud)

将私钥复制粘贴到 Sign.py python 脚本中并生成签名和公钥我已使用“Hello”作为创建签名的消息

符号.py

Private Key is:
[38, 108, 90, 112, 230, 138, 62, 97, 107, 90, 227, 165, 207, 80, 251, 154, 17, 4, 73, 53, 33, 162, 33, 200, 243, 205, 116, 43, 36, 59, 201, 84]
Public Key is:
[163, 238, 83, 33, 229, 249, 105, 12, 141, 7, 214, 134, 148, 1, 198, 45, 13, 31, 9, 223, 85, 201, 98, 248, 73, 160, 40, 255, 64, 214, 250, 121, 234, 103, 212, 148, 197, 48, 210, 38, 166, 51, 30, 81, 119, 240, 125, 104, 237, 24, 3, 216, 229, 87, 45, 7, 115, 69, 94, 187, 236, 91, 142, 18]
Run Code Online (Sandbox Code Playgroud)

Sign.py 的输出:

Private Key is:
[38, 108, 90, 112, 230, 138, 62, 97, 107, 90, 227, 165, 207, 80, 251, 154, 17, 4, 73, 53, 33, 162, 33, 200, 243, 205, 116, 43, 36, 59, 201, 84]
Public Key is:
[163, 238, 83, 33, 229, 249, 105, 12, 141, 7, 214, 134, 148, 1, 198, 45, 13, 31, 9, 223, 85, 201, 98, 248, 73, 160, 40, 255, 64, 214, 250, 121, 234, 103, 212, 148, 197, 48, 210, 38, 166, 51, 30, 81, 119, 240, 125, 104, 237, 24, 3, 216, 229, 87, 45, 7, 115, 69, 94, 187, 236, 91, 142, 18]
Signature is:
[47, 107, 101, 228, 187, 209, 97, 180, 83, 149, 133, 71, 62, 15, 86, 186, 192, 222, 108, 221, 249, 128, 124, 7, 139, 110, 103, 108, 62, 89, 136, 152, 226, 43, 104, 166, 92, 247, 9, 201, 135, 96, 19, 75, 55, 229, 67, 198, 188, 90, 246, 17, 157, 1, 229, 71, 151, 206, 211, 95, 41, 51, 96, 42]
Run Code Online (Sandbox Code Playgroud)

复制粘贴签名并在带有 micro ecc 库的 Visual Studio 中使用相同的消息:-

源代码.c

import ecdsa
from ecdsa import SigningKey, SECP256k1


signinKey_lst = [38, 108, 90, 112, 230, 138, 62, 97, 107, 90, 227, 165, 207, 80, 251, 154, 17, 4, 73, 53, 33, 162, 33, 200, 243, 205, 116, 43, 36, 59, 201, 84]
signinKey_lst = bytearray(signinKey_lst)
signinKey = ecdsa.SigningKey.from_string(signinKey_lst, curve=ecdsa.SECP256k1)
verifyKey = signinKey.get_verifying_key()

signinKey_lst = []
for e in bytearray(signinKey.to_string()):
    signinKey_lst.append(e)

verifyKey_lst = []
for e in bytearray(verifyKey.to_string()):
    verifyKey_lst.append(e)

print("Public Key is:")
print(verifyKey_lst)

msg = "Hello"

sign = signinKey.sign(msg)
sign_lst = []

verifyKey_lst = []
for e in bytearray(sign):
    sign_lst.append(e)

print("Signature is:")
print(sign_lst)
Run Code Online (Sandbox Code Playgroud)

通过运行 source.c 输出 Visual Studio:-

/*************************************************************************************************
Validating the Encrypted hash with the public key and the hash used


Public Key:
[  148, 49, 144, 80, 185, 77, 185, 14, 186, 168, 164, 110, 123, 192, 55, 219, 184, 133, 153, 65, 144, 169, 175, 171, 203, 225, 88, 134, 51, 199, 254, 215, 237, 144, 141, 137, 80, 190, 25, 35, 33, 136, 248, 190, 114, 60, 128, 34, 155, 157, 83, 68, 187, 154, 137, 9, 51, 112, 155, 54, 88, 104, 82, 138 ]

Msg:
[  72, 101, 108, 108, 111 ]

signature:
[  186, 247, 43, 62, 152, 84, 40, 197, 74, 135, 80, 18, 152, 150, 121, 177, 155, 242, 1, 11, 171, 155, 45, 19, 174, 171, 190, 66, 31, 125, 214, 136, 41, 116, 139, 82, 71, 208, 4, 80, 47, 154, 100, 173, 110, 164, 25, 19, 7, 253, 175, 123, 34, 1, 99, 86, 241, 241, 211, 45, 15, 35, 210, 69 ]

uECC_verify() failed

/*************************************************************************************************/

C:\Users\prajwal.bv\source\repos\Crypt_sample1\x64\Debug\Crypt_sample1.exe (process 17160) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Run Code Online (Sandbox Code Playgroud)

我预计运行 source.c 的输出是 uECC_verify() 成功。但它打印 uECC_verify() 失败。

Ste*_*cht 6

ECDSA 签名过程分为两个阶段:

  • 根据消息计算哈希摘要
  • 然后签署该摘要。

除非另有说明,Python ECDSA 默认使用 SHA-1。

消息“Hello”的 SHA-1 摘要采用十六进制编码:f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0

这意味着您在调用 uECC_verify 函数时需要使用此摘要。

验证

将 .c 代码中的 msg 数组替换为以下代码行:

uint8_t msg[] = { 0xf7, 0xff, 0x9e, 0x8b, 0x7b, 0xb2, 0xe0, 0x9b, 0x70, 0x93, 0x5a, 0x5d, 0x78, 0x5e, 0x0c, 0xc5, 0xd9, 0xd0, 0xab, 0xf0};
Run Code Online (Sandbox Code Playgroud)

然后验证功能成功。

输出

调试控制台中的输出如下所示:

uECC_验证输出

  • 谢谢,我尝试了 api 作为signinKey.sign(msg, hashfunc = hashlib.sha256) 并且它有效。我并不打算使用我自己的哈希函数。此外,我还看到有一个signinKey.sign_digest() API可以在不进行散列的情况下进行签名。 (2认同)