-
Notifications
You must be signed in to change notification settings - Fork 5
/
lab_util.h
78 lines (67 loc) · 1.62 KB
/
lab_util.h
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <string.h>
#include <openssl/bn.h>
BIGNUM* get_rsa_priv_key(BIGNUM* p, BIGNUM* q, BIGNUM* e){
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* rsaEnc(BIGNUM* message, BIGNUM* mod, BIGNUM* 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* rsaDec(BIGNUM* enc, BIGNUM* priv_key, BIGNUM* 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;
}
int hex_to_int(char c){
if (c >= 97)
c = c - 32;
int first = c / 16 - 3;
int second = c % 16;
int result = first * 10 + second;
if (result > 9) result--;
return result;
}
int hex_to_ascii(const char c, const char d){
int high = hex_to_int(c) * 16;
int low = hex_to_int(d);
return high+low;
}
void printHX(const char* st){
int length = strlen(st);
if (length % 2 != 0) {
printf("%s\n", "invalid hex length");
return;
}
int i;
char buf = 0;
for(i = 0; i < length; i++) {
if(i % 2 != 0)
printf("%c", hex_to_ascii(buf, st[i]));
else
buf = st[i];
}
printf("\n");
}
void printBN(char* msg, BIGNUM * a){
char * number_str = BN_bn2hex(a);
printf("%s 0x%s\n", msg, number_str);
OPENSSL_free(number_str);
}