-
Notifications
You must be signed in to change notification settings - Fork 22
/
oaep_rsa2048_openssl.c
115 lines (95 loc) · 2.7 KB
/
oaep_rsa2048_openssl.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "openssl/bn.h"
#include "openssl/sha.h"
#include "openssl/rsa.h"
#include "openssl/evp.h"
void unhex(char* hex, unsigned char* data, size_t ilen)
{
for (size_t i = 0; i < ilen; ++i) {
sscanf(hex, "%2hhx", &data[i]);
hex += 2;
}
}
void printBN(BIGNUM *r)
{
char* out = BN_bn2hex(r);
const char *padding="0000000000000000000000000000000000000000000000000000000000000000";
int padLen1 = 64 - strlen(out);
if(padLen1 < 0) padLen1 = 0;
printf("%*.*s%s\n", padLen1, padLen1, padding, out);
OPENSSL_free(out);
}
int main(int argc, char* argv[])
{
// Our args
int encrypt;
int success = 0;
if (argc == 4) {
encrypt = 1;
} else if (argc - optind == 5) {
encrypt = 0;
} else {
printf("usage: \t%s X, Y, D, M\nor \t%s X, Y, R, S, M\n", argv[0], argv[0]);
return -1;
}
// OpenSSL stuff:
int ret;
RSA *r = NULL;
r = RSA_new();
char* str = argv[argc - 1];
unsigned char* msg = (unsigned char*)malloc(strlen(str) / 2 * sizeof(unsigned char));
size_t mlen = strlen(str)/2;
unhex(str, msg, mlen);
unsigned char* to;
if (encrypt) {
// public key setup
BN_hex2bn(&r->n, argv[1]);
BN_hex2bn(&r->e, argv[2]);
to = (unsigned char*)malloc(RSA_size(r));
ret = RSA_public_encrypt(mlen, msg, to, r, RSA_PKCS1_OAEP_PADDING);
if (ret <= 0) {
printf("Failed to encrypt with those args.\n");
return -1;
} else {
for (int i = 0; i < ret; ++i)
printf("%02x", to[i]);
printf("\n");
}
} else {
BN_CTX * tmp_bn;
tmp_bn = BN_CTX_new();
// private key setup
BN_hex2bn(&r->p, argv[1]);
BN_hex2bn(&r->q, argv[2]);
BN_hex2bn(&r->e, argv[3]);
BN_hex2bn(&r->d, argv[4]);
// compute n
r->n = BN_new();
BN_mul(r->n, r->p ,r->q,tmp_bn);
BN_CTX_free(tmp_bn);
to = (unsigned char*)malloc(RSA_size(r));
ret = RSA_check_key(r);
if (ret != 1){
/* error */
printf(" failure RSA_check_key returned %d", ret);
success = -1;
}
ret = RSA_private_decrypt(mlen, msg, to, r, RSA_PKCS1_OAEP_PADDING );
if (ret <= 0) {
/* error */
printf(" failure RSA_private_decrypt returned %d", ret);
success = -1;
} else {
for (int i = 0; i < ret; ++i)
printf("%02x", to[i]);
printf("\n");
}
}
RSA_free(r);
return success;
}