Unnecessary code in sw_wallet.cpp
I find and report unnecessary parts in signature code using Secure enclave.
/plugins/wallet_plugin/se_wallet.cpp#L207
fc::ecdsa_sig sig = ECDSA_SIG_new();
BIGNUM *r = BN_new(), *s = BN_new();
const UInt8* der_bytes = CFDataGetBytePtr(signature);
BN_bin2bn(der_bytes+4, der_bytes[3], r);
BN_bin2bn(der_bytes+6+der_bytes[3], der_bytes[4+der_bytes[3]+1], s);
ECDSA_SIG_set0(sig, r, s);
The code to convert CFData to ECDSA_Sig in line 207 can be implemented simply using the d2i_ECDSA_SIG function.
See the code below.
https://github.com/eoshubio/eoshub-ios/blob/master/eoshub/EOSIO/Wallet/Sig/R1Key.mm#L501
/plugins/wallet_plugin/se_wallet.cpp#L198
CFDataRef digestData = CFDataCreateWithBytesNoCopy(nullptr, (UInt8*)d.data(), d.data_size(), kCFAllocatorNull);
I do not know why, but CFDataCreateWithBytesNoCopy produces an invalid signature in the swift code.
For someone who wants to implement a Secure enclave signature with swift, I'll leave the code below.
guard let digestData = CFDataCreate(kCFAllocatorDefault, Array(d), d.count) else {
return nil
}
cc @bytemaster
cc @spoonincode
Thanks for pointing this out; are you planning to do a PR or would you be fine if I just went ahead and fixed it? Be aware you need to store the ECDSA_SIG in a fc::ecdsa_sig for proper cleanup semantics.
Also, I think the code you pointed to from eoshub-ios is leaky -- d2i_ECDSA_SIG() returns a newly allocated ECDSA_SIG which will leak the ECDSA_SIG you create on line 503.
@spoonincode If you do not mind, I want to PR in a very short time. It will be an honor to me. And thanks for pointing out the memory leaks that can occur in eoshub-ios.