我正在尝试移植一些代码来进行RSA验证。我用Google搜索,并在xlnx u-boot中找到了一些代码 我把这个rsa-mod-exp.c放在我的代码中,当然还有其他一些.h文件。我的SOC是小端,所以我使用小端定义 完成上述所有操作后,我尝试创建一个签名来验证我的代码。 在此之前,我注意到u-boot rsa结构与openssl RSA结构不同。因此,为了获得u-boot rsa结构,我需要安装一个名为dumppublickey的工具。我从here找到了一个并安装了。
OpenSSL RSA public key structure:
struct {
BIGNUM *n; // public modulus
BIGNUM *e; // public exponent
BIGNUM *d; // private exponent
BIGNUM *p; // secret prime factor
BIGNUM *q; // secret prime factor
BIGNUM *dmp1; // d mod (p-1)
BIGNUM *dmq1; // d mod (q-1)
BIGNUM *iqmp; // q^-1 mod p
// ...
};RSA
U-boot RSA public key structure:
struct rsa_public_key {
uint len; /* len of modulus[] in number of uint32_t */
uint32_t n0inv; /* -1 / modulus[0] mod 2^32 */
uint32_t modulus; / modulus as little endian array */
uint32_t rr; / R^2 as little endian array */
uint64_t exponent; /* public exponent */
};
现在,除了签名之外,每件事情都准备好了。这是我创建签名的步骤。
1. Create private key.It's a 2048 bit and the public exponent is three.
openssl genpkey -algorithm RSA -out key.pem -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:3
2. Create cert file.
openssl req -new -x509 -key key.pem -out newcert.pem -days 3650
3. Get the U-boot structure public key.
dumppublickey newcert.pem
I got :
{
64, //lenth
0x63b40d47, //n0inv
{2799858569,......,1105502226,1577608370,1150603850,3030884821}, // modules (N)
{909785043,.......,3050452165,3418434484,2529136815,837355943}, // r^2
3 //exponent
}
4. Sign my file using the private key.
echo "hello world" > a.txt
openssl dgst -sign key.pem -sha256 -hex a.txt
I got :
RSA-SHA256(a.txt)= 157eb60ad0c77427cd....6b02b5d331d610b27f32aaa0
我在我的代码中放了密钥长度,n0inv,模数,rr,exponent和签名。请注意模数和rr I useu_int32数组,如上所示,而签名我使用的u_int8数组如下所示。
{0x15,0x7e.0xb6,0x0a,.......0xaa,0xa0}
以下是我认为rsa-mod-exp.c(函数rsa_mod_exp_sw)会给我的。我以为我会得到文件a.txt的sha256哈希值的结果。与我使用openssl的结果相同。
openssl dgst -sha256 a.txt
但是,我没有得到这个sha256哈希值。我有点奇怪。 任何人都可以指出哪个部分是错的以及如何纠正它?