我的应用程序生成SHA 256哈希值的“xx_xx.hex”文件,然后生成RSA签名的哈希。X-Cube-CRYPTOLIB和python脚本生成的SHA 256值相同。但是RSA签名是不一样的。
我使用openssl创建了1024个密钥对,哈希值由X-Cube生成-CRYPTOLIB是十进制格式,但它相当于python脚本生成的哈希值(十六进制格式)。
生成的SHA 256值为:5b4c333945c47a952bfe1571e5b58b4674985fa754f19abcb5739f4c2dda433b
但是X-Cube-CRYPTOLIB十进制值生成的签名不等同于python脚本生成的签名(十六进制格式)。
下面是python脚本的结果;
十六进制签名:72869266541a76bc2a73437e53c1c917e4b2dd323f8b9735e44f6d600e7fe414c0899e41e0a89c557bcd3673dac2084409fabb0ef52574108363fdd3bdc44d2239371fe3e0617966f93964db42d12ad91b254b70af7add8cd1043d7f6a5857ea834c885480d5ac2 d792c514233fcc62eea3149fc8fa50e0e2579f780ff6e1c37
请找到下面的图像来检查由X-Cube-CRYPTOLIB生成的签名,它是不完整的。但仅供参考,我们可以看到几个字节。computed singature X-Cube-CRYPTOLIB
Python脚本:
import hashlib
import sys
from intelhex import IntelHex
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding, rsa
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
import os
import binascii
# Path to the hex file you want to load
file_path = 'W:/xxx/xxx/xxx_input.hex'
privkey_path = 'W:/xxx/xxx/rivate_1024key.pem'
pubkey_path = 'W:/xxx/xxx/public_1024key.pem'
output_file = 'W:/xxx/xxx//Hash.txt'
outhex_file = 'W:/xxx/xxx/xxx_output.hex'
# Define the ranges() function
def ranges(addresses):
start = addresses[0]
end = addresses[0]
for address in addresses[1:]:
if address == end + 1:
end = address
else:
yield start, end
start = address
end = address
yield start, end
# Load the Intel Hex file
hex = IntelHex(file_path)
# Get the sorted list of addresses
addresses = hex.addresses()
addresses.sort()
# Iterate over address ranges and extract the data
data_list = list(ranges(addresses))
for start, end in data_list:
size = end - start + 1
data = list(hex.tobinarray(start=start, size=size))
# Convert the data to hexadecimal format
hex_data = ''.join(format(byte, '02x') for byte in data)
data_bytes = bytes.fromhex(hex_data)
hasher = hashlib.sha256()
hasher.update(data_bytes)
hash_value = hasher.hexdigest()
# Print the hash value in hexadecimal format
print("SHA256 Hash Value:", hash_value)
# Digest to be signed
# Convert the hash value to bytes
digest_bytes = bytes.fromhex(hash_value)
# Load the private key from file
try:
with open(privkey_path, 'rb') as f:
private_key = serialization.load_pem_private_key(
f.read(),
password=None,
backend=default_backend()
)
print("private_key:", private_key)
# If no exception occurred, the private key was loaded successfully
print("Private key loaded successfully.")
# Use the private key for signing or other operations
except Exception as e:
print("Error loading private key:", e)
# Save the digest bytes to a file
with open(output_file, 'wb') as file:
file.write(digest_bytes)
print("Digest:", digest_bytes)
# Sign the digest with the private key
signature = private_key.sign(
digest_bytes,
padding.PKCS1v15(), # Use PKCS1v15 padding scheme for RSA signatures
hashes.SHA256() # Use SHA256 hashing algorithm
)
# Convert the signature to hexadecimal format if needed
hex_signature = signature.hex()
# Print the hexadecimal signature
print("Hexadecimal signature:", hex_signature)
X-Cube的C代码:
const uint8_t Message[] =
{
0xcd, 0xc8, 0x7d, 0xa2, 0x23, 0xd7, 0x86, 0xdf, 0x3b, 0x45, 0xe0, 0xbb, 0xbc, 0x72, 0x13, 0x26,
0xd1, 0xee, 0x2a, 0xf8, 0x06, 0xcc, 0x31, 0x54, 0x75, 0xcc, 0x6f, 0x0d, 0x9c, 0x66, 0xe1, 0xb6,
0x23, 0x71, 0xd4, 0x5c, 0xe2, 0x39, 0x2e, 0x1a, 0xc9, 0x28, 0x44, 0xc3, 0x10, 0x10, 0x2f, 0x15,
0x6a, 0x0d, 0x8d, 0x52, 0xc1, 0xf4, 0xc4, 0x0b, 0xa3, 0xaa, 0x65, 0x09, 0x57, 0x86, 0xcb, 0x76,
0x97, 0x57, 0xa6, 0x56, 0x3b, 0xa9, 0x58, 0xfe, 0xd0, 0xbc, 0xc9, 0x84, 0xe8, 0xb5, 0x17, 0xa3,
0x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 0xc2, 0xd3, 0xf0, 0xcb, 0x35, 0xf2, 0x92, 0x80, 0xe1, 0x38, 0x6b, 0x6f, 0x64, 0xc4,
0xef, 0x22, 0xe1, 0xe1, 0xf2, 0x0d, 0x0c, 0xe8, 0xcf, 0xfb, 0x22, 0x49, 0xbd, 0x9a, 0x21, 0x37
};
const uint8_t Public_Exponent[] =
{
0x01, 0x00, 0x01
};
const uint8_t Private_Exponent[] =
{
0x33, 0xa5, 0x04, 0x2a, 0x90, 0xb2, 0x7d, 0x4f, 0x54, 0x51, 0xca, 0x9b, 0xbb, 0xd0, 0xb4, 0x47,.......};
const uint8_t P_Prime[] =
{
0xe7, 0xe8, 0x94, 0x27, 0x20, 0xa8, 0x77, 0x51, 0x72, 0x73, 0xa3, 0x56, 0x05, 0x3e, 0xa2, 0xa1,....};
const uint8_t Q_Prime[] =
{
0xb6, 0x9d, 0xca, 0x1c, 0xf7, 0xd4, 0xd7, 0xec, 0x81, 0xe7, 0x5b, 0x90, 0xfc, 0xca, 0x87, 0x4a, ..........};
const uint8_t P_Prime_Exponent[] =
{
0x28, 0xfa, 0x13, 0x93, 0x86, 0x55, 0xbe, 0x1f, 0x8a, 0x15, 0x9c, 0xba, 0xca, 0x5a, 0x72, 0xea,.............};
const uint8_t Q_Prime_Exponent[] =
{
0x1a, 0x8b, 0x38, 0xf3, 0x98, 0xfa, 0x71, 0x20, 0x49, 0x89, 0x8d, 0x7f, 0xb7, 0x9e, 0xe0, 0xa7,....};
const uint8_t Coefficient[] =
{
0x27, 0x15, 0x6a, 0xba, 0x41, 0x26, 0xd2, 0x4a, 0x81, 0xf3, 0xa5, 0x28, 0xcb, 0xfb, 0x27, 0xf5,... };
uint8_t Computed_Signature[128];
uint8_t computed_hash[CMOX_SHA256_SIZE];
const uint8_t Public_Exponent[] = {0x01, 0x00, 0x01};
uint8_t Working_Buffer[3500];
StartAddress = 0x0802A000;
EndAddress = 0x0802FF00;
/* Compute directly the digest passing all the needed parameters */
hretval = cmox_hash_compute(CMOX_SHA256_ALGO, /* Use SHA256 algorithm */
const uint8_t*)(uintptr_t)StartAddress,
(EndAddress - StartAddress + 1), /* Message to digest */
computed_hash, /* Data buffer to receive digest data */
CMOX_SHA256_SIZE, /* Expected digest size */
&computed_size); /* Size of computed digest *
/* Construct a RSA context, specifying mathematics implementation and working buffer for later processing */
cmox_rsa_construct(&Rsa_Ctx, CMOX_RSA_MATH_FUNCS, CMOX_MODEXP_PRIVATE, Working_Buffer, sizeof(Working_Buffer));
retval = cmox_rsa_setKeyCRTwithFACM(&Rsa_Key, /* RSA key structure to fill */
sizeof(Modulus) * 8, /* Private key modulus bit length */
P_Prime_Exponent, sizeof(P_Prime_Exponent), /* P prime */
Q_Prime_Exponent, sizeof(Q_Prime_Exponent), /* Q prime */
P_Prime, sizeof(P_Prime), /* P prime exponent */
Q_Prime, sizeof(Q_Prime), /* Q prime exponent */
Coefficient, sizeof(Coefficient), /* Coefficient */
Public_Exponent, sizeof(Public_Exponent)); /* Public exponent */
/* Compute directly the signature passing all the needed parameters */
retval = cmox_rsa_pkcs1v15_sign(&Rsa_Ctx, /* RSA context */
&Rsa_Key, /* RSA key to use */
computed_hash, /* Digest to sign */
CMOX_RSA_PKCS1V15_HASH_SHA256, /* Method used to compute the digest */
Computed_Signature, &computed_size); /* Data buffer to receive signature */
在脚本中,我使用PKCS 1v 15填充方案进行RSA签名,这与C代码类似。但签名还是不一样。
请指出这里的错误是什么。可能是任何数据格式转换或双散列或其他东西?
1条答案
按热度按时间yhqotfr81#
当使用公钥解密签名而不移除填充时:
有效PKCS#1 v1.5签名结果:
的第一部分
由所使用的摘要的ID组成,并将SHA-1标识为应用的摘要(参见此处)。当生成所用数据的SHA-1哈希值时:
散列是
其对应于第二部分并且因此是一致的。
这表明X Cube代码使用SHA-1(而不是 * SHA-256)对数据进行散列,并使用RSA PKCS#1 v1.5对散列进行签名(因此 * 原始 * 数据的 * 单个 * 散列),即Python的对应部分可以实现如下:
它提供了预期的签名:
**编辑:**关于您的评论:
要传递散列消息而不是消息并禁用隐式散列,必须使用
Prehashed
。这里):其中,
message_hash
是作为字节类对象的消息的散列,例如,如下: