問題
最近在使用 DH_compute_key() 函式計算 DH 共享密鑰時,發現計算得出的密鑰長度少了一位,導致后續所有資料都出現了錯誤,最終導致程式崩潰,
BN_bn2bin() 函式也具有相同的問題
程序
查看 openssl 官方檔案 發現以下內容:
DH_compute_key() computes the DH_compute_key() computes the
shared secret from the private DH value in dh and the other
party's public value in pub_key and stores it in key. key must
point to DH_size(dh) bytes of memory. The padding style is RFC
5246 (8.1.2) that strips leading zero bytes. It is not
constant time due to the leading zero bytes being stripped.
The return value should be considered public.
DH_compute_key_padded() is similar but stores a fixed number
of bytes. The padding style is NIST SP 800-56A (C.1) that
retains leading zero bytes. It is constant time due to the
leading zero bytes being retained. The return value should be
considered public.shared secret from the private DH value in
dh and the other party's public value in pub_key and stores it
in key. key must point to DH_size(dh) bytes of memory. The
padding style is RFC 5246 (8.1.2) that strips leading zero
bytes. It is not constant time due to the leading zero bytes
being stripped. The return value should be considered public.
解決
就是說:DH_compute_key() 函式會去掉結果最前面的 ‘0x00’ 位元組,而 DH_compute_key_padded() 不會
修改為使用 DH_compute_key_padded() 函式后程式運行正確,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/287657.html
標籤:區塊鏈
下一篇:聯邦學習開坑!
