-
Notifications
You must be signed in to change notification settings - Fork 8
/
rsa.cpp
147 lines (112 loc) · 4.03 KB
/
rsa.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "rsa.hpp"
#include <stdio.h>
/*
createRSA
Creates an RSA object from a public key. Use this if the key is loaded into a variable.
\param key: This the public or private key that is being used to make the RSA object
\param is_public_key: This should be 1 if this is a public key, or 0 otherwise.
\return RSA*: A pointer to an RSA object.
*/
RSA* createRSA(unsigned char* key, int is_public_key) {
RSA *rsa= NULL;
BIO *keybio ;
keybio = BIO_new_mem_buf(key, -1);
if (keybio==NULL)
{
printf( "Failed to create key BIO");
return 0;
}
if(is_public_key)
{
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa,NULL, NULL);
}
else
{
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL);
}
return rsa;
};
/**
createRSAWithFilename
Creates an RSA object from a public key in a file. Use this if the key is in a file.
\param filename: This filename of the file containing the public or private key that is being used to make the RSA object
\param is_public_key: This should be 1 if this is a public key, or 0 otherwise.
\return RSA*: A pointer to an RSA object.
*/
RSA * createRSAWithFilename(char * filename,int is_public_key)
{
FILE * fp = fopen(filename,"rb");
if(fp == NULL)
{
printf("Unable to open file %s \n",filename);
return NULL;
}
RSA *rsa= RSA_new() ;
if(is_public_key)
{
rsa = PEM_read_RSA_PUBKEY(fp, &rsa,NULL, NULL);
}
else
{
rsa = PEM_read_RSAPrivateKey(fp, &rsa,NULL, NULL);
}
fclose(fp);
return rsa;
}
int padding = RSA_PKCS1_PADDING;
/**
public_encrypt
This function encrypts data using a a public key.
\param data, the data to be encrypted.
\param data_len, the length of the data.
\key_file, the filename of the file with the key.
\encrypted, a pointer to the location where the encrypted string will go.
\returns int, indicating success or failure.
*/
int public_encrypt(unsigned char * data,int data_len,char * key_file, unsigned char *encrypted) {
RSA * rsa = createRSAWithFilename(key_file,1);
int result = RSA_public_encrypt(data_len,data,encrypted,rsa,padding);
return result;
}
/**
private_decrypt
This function decrypts data using a private key, which was encrypted with the corresponding public key.
\param enc_data, the data that was encrypted.
\param data_len, the length of the data.
\key_file, the filename of the file with the key.
\decrypted, a pointer to the location where the decrypted data will go.
\returns int, indicating success or failure.
*/
int private_decrypt(unsigned char * enc_data,int data_len,char * key_file, unsigned char *decrypted) {
RSA * rsa = createRSAWithFilename(key_file,0);
int result = RSA_private_decrypt(data_len,enc_data,decrypted,rsa,padding);
return result;
}
/**
private_encrypt
This function encrypts data using a private key. This is used to create a digital signature.
\param data, the data to be encrypted.
\param data_len, the length of the data.
\key_file, the filename of the file with the key.
\encrypted, a pointer to the location where the encrypted string will go.
\returns int, indicating success or failure.
*/
int private_encrypt(unsigned char * data,int data_len,char * key_file, unsigned char *encrypted) {
RSA* rsa = createRSAWithFilename(key_file,0);
int result = RSA_private_encrypt(data_len, data, encrypted, rsa, padding);
return result;
}
/**
public_decrypt
This function decrypts a digital signature using a public key, which was encrypted with the corresponding private key.
\param enc_data, the data that was encrypted.
\param data_len, the length of the data.
\key_file, the filename of the file with the key.
\decrypted, a pointer to the location where the decrypted data will go.
\returns int, indicating success or failure.
*/
int public_decrypt(unsigned char * enc_data,int data_len,char * key_file, unsigned char *decrypted) {
RSA* rsa = createRSAWithFilename(key_file,1);
int result = RSA_public_decrypt(data_len,enc_data,decrypted,rsa,padding);
return result;
}