forked from abergie5b/RSAImpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.c
51 lines (44 loc) · 1.13 KB
/
rsa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "rsa.h"
BIGNUM* get_rsa_priv_key(BIGNUM* p, BIGNUM* q, BIGNUM* e)
{
/*
given two large prime numbers, compute a private key
using the modulo inverse of the totatives of the product p*q
*/
BN_CTX *ctx = BN_CTX_new();
BIGNUM* p_minus_one = BN_new();
BIGNUM* q_minus_one = BN_new();
BIGNUM* one = BN_new();
BIGNUM* tt = BN_new();
BN_dec2bn(&one, "1");
BN_sub(p_minus_one, p, one);
BN_sub(q_minus_one, q, one);
BN_mul(tt, p_minus_one, q_minus_one, ctx);
BIGNUM* res = BN_new();
BN_mod_inverse(res, e, tt, ctx);
BN_CTX_free(ctx);
return res;
}
BIGNUM* rsa_encrypt(BIGNUM* message, BIGNUM* mod, BIGNUM* pub_key)
{
/*
compute the RSA cipher on message
the ciphertext is congruent to: message^mod (modulo pub_key)
*/
BN_CTX *ctx = BN_CTX_new();
BIGNUM* enc = BN_new();
BN_mod_exp(enc, message, mod, pub_key, ctx);
BN_CTX_free(ctx);
return enc;
}
BIGNUM* rsa_decrypt(BIGNUM* enc, BIGNUM* priv_key, BIGNUM* pub_key)
{
/*
compute the original message: (message ^ mod) ^ pub_key
*/
BN_CTX *ctx = BN_CTX_new();
BIGNUM* dec = BN_new();
BN_mod_exp(dec, enc, priv_key, pub_key, ctx);
BN_CTX_free(ctx);
return dec;
}