Add the function BN_get_low_byte to efficiently get access to the bytes in a BIGNUM #23014
Replies: 2 comments
-
No worries, and thanks for your input! Regarding your suggested new function, I'm curious if you've looked at BN_get_word is what you're after in terms of functionality. It may require some additional math operations to make your bignum representable as a ulong, but it should do the trick, and is likely a more API friendly approach. |
Beta Was this translation helpful? Give feedback.
-
From your description, I think you should use This will likely be more efficient than what you were doing before. Repeatedly taking the bottom 7 bits and shifting means that you're constantly shifting the whole |
Beta Was this translation helpful? Give feedback.
-
I apologize in advance for posting here but I don't know how to make a pull request.
I just ported an application which uses OpenSSL's BIGNUM library to version 1.1.1 and lost access to the internals of the BIGNUM. I could not find an efficient way to rewrite the code using BN_bn2bin, so I added a one-line function to crypto/bn/bn_lib.c to get the least significant byte of the BIGNUM:
int BN_get_low_byte (const BIGNUM* a)
{
return a->top ? a->d[0] & 0xFF : 0;
}
The routine I was having trouble rewriting processes seven bits at a time, as a base-128 integer, so it obtains the low byte from the function above and then performs a 7-bit right shift to get the next 7 bits.
The BN_get_word function wasn't a viable alternative since it gives an error code when the BIGNUM is more than one word in size.
I hereby offer the code for inclusion in all versions of OpenSSL with no need to include an attribution.
Mike
Beta Was this translation helpful? Give feedback.
All reactions