Reputation: 26326
According to OpenSSL 1.1.0's manual, it says:
BN_bin2bn() converts the positive integer in big-endian form of length len at s into a BIGNUM and places it in ret. If ret is NULL, a new BIGNUM is created.
But then in the following minimal example:
#include <iostream>
#include <boost/algorithm/hex.hpp>
#include <openssl/bn.h>
#include <vector>
int main()
{
std::string in = "200ec31326d7a933222e3b43a7d6c920a1d2e8a74d1e6f4980ca78b2d9c1aaba6c2ad71f0f1d0cbb40695f27be048982589bccf30066a8735db4a6b0928925077e";
std::vector<unsigned char> out;
// convert hex to binary bytes
boost::algorithm::unhex(in.begin(), in.end(), std::back_inserter(out));
BIGNUM *eccsig_r = nullptr;
// convert bytes to a BIGNUM object
BN_bin2bn(&out[1], 32, eccsig_r);
std::cout<<eccsig_r<<std::endl; // prints 0!
return 0;
}
The pointer address of eccsig_r
remains 0 (or nullptr). If I understand what the manual says, it should be that eccsig_r
is never nullptr
again after calling bin2bn()
.
Why is eccsig_r
still nullptr
? I can't understand this. Please advise. I'm on Debian 9.
PS: For full disclosure, that hex you see up there is a simple ECC signature that I serialized. I don't believe that has any effect on this. Please correct me if I'm wrong.
Upvotes: 1
Views: 1047
Reputation: 26326
OK. I think I figured it out. If ret
is nullptr
, then the return value will create a new BIGNUM in the return of bin2bn()
. It's a weird way of doing things, in my opinion. I don't understand the purpose, but this works:
#include <iostream>
#include <boost/algorithm/hex.hpp>
#include <openssl/bn.h>
#include <vector>
int main()
{
std::string in = "200ec31326d7a933222e3b43a7d6c920a1d2e8a74d1e6f4980ca78b2d9c1aaba6c2ad71f0f1d0cbb40695f27be048982589bccf30066a8735db4a6b0928925077e";
std::vector<unsigned char> out;
// convert hex to binary bytes
boost::algorithm::unhex(in.begin(), in.end(), std::back_inserter(out));
BIGNUM *eccsig_r = nullptr;
// convert bytes to a BIGNUM object
eccsig_r = BN_bin2bn(&out[1], 32, nullptr);
std::cout<<eccsig_r<<std::endl; // doesn't print zero anymore!
return 0;
}
Please correct me if I'm wrong.
Upvotes: 1