Reputation: 1
Please suggest me how to implement the following equations using openssl in C.
Out = A^x mod y;
Where Exponent x
is random and y
is Diffie-Hellman prime modulus.
A
= SHA256 (Buffer1 concatenate with Buffer 2).
Upvotes: 0
Views: 294
Reputation: 3393
Your question is not quite clear, so I'll just point out the parts you might be interested.
For making a SHA256 digest, look at EVP_Digest page. Also take a look at cyprto/sha1test.c for usage. Replace EVP_sha1()
with EVP_sha256()
in this line
EVP_Digest(*P,strlen((char *)*P),md,NULL,EVP_sha1(), NULL);
For x and y, they are stored as BIGNUMs in OpenSSL. Here is a clear and simple guide to BIGNUMs. Also look at crypto/bn/bntest.c for usage examples.
Now you need to convert message digest A to a BIGNUM, look here and select an appropriate function that fits your needs. BN_bin2bn()
might be what you're looking for. If the resulting BIGNUM bn_A is larger than modulus y
, this function will do bn_A = bn_A mod y
for you:
int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
, or if you expect both bn_A
and y
to be non-negative,
int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
Suppose you already assigned modulus y
and you want to come up with a random number x such that 0=<x<y
, make use of this function
int BN_rand_range(BIGNUM *rnd, BIGNUM *range);
See here for BIGNUM rand functions information.
Finally it's time to do the modulus multiplication, use this function (described here):
int BN_mod_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
For usage of this function, you can refer to function test_mod_mul
in crypto/bn/bntest.c
And that should do it.
Upvotes: 1