-
Notifications
You must be signed in to change notification settings - Fork 24
/
rsa_context.hh
129 lines (107 loc) · 3.17 KB
/
rsa_context.hh
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
#ifndef RSA_CONTEXT_HH
#define RSA_CONTEXT_HH
#include <string>
#include <openssl/rsa.h>
/**
* class rsa_context
*
* Interface for RSA processing.
*
*/
class rsa_context
{
public:
/**
* Constructor.
* It will randomly generate the RSA key pair with the given key length.
*
* @param keylen Length of key in bits. Supported length are 512, 1024, 2048, and 4096 bits.
*/
rsa_context(int keylen);
/**
* Constructor.
* It will load key from the file using the given password.
* Currently supports PEM format only
*
* @param filename file path that contains the rsa private key
* @param passwd password used to encrypt the private key
*/
rsa_context(const std::string &filename, const std::string &passwd);
/**
* Constructor.
* It will load key from the file using the given password.
* Currently supports PEM format only
*
* @param filename file path that contains the rsa private key
* @param passwd password used to encrypt the private key
*/
rsa_context(const char *filename, const char *passwd);
virtual ~rsa_context();
/**
* Return key length.
*
* @return key length in bits.
*/
int get_key_bits();
/**
* Return a maximum amount of plain text that can be encrypted.
*
* @return maximum plain text size in bytes.
*/
int max_ptext_bytes();
/**
* Return whether chinese remainder theorem (CRT) is used for processing or not.
*
* @return true if CRT is enabled, false otherwise.
*/
bool is_crt_available();
virtual void dump();
/**
* Encrypts the data with RSA algorithm using public key.
* All encryption/decryption methods assume RSA_PKCS1_PADDING
*
* @param out Buffer for output.
* @param out_len In: allocated buffer space for output result, Out: out put size.
* @param in Intput plain text.
* @param in_len Intpu plain text size.
*/
virtual void pub_encrypt(unsigned char *out, int *out_len,
const unsigned char *in, int in_len);
/**
* Decrypt the data with RSA algorithm using private key.
* All encryption/decryption methods assume RSA_PKCS1_PADDING
*
* @param out Buffer for output.
* @param out_len In: allocated buffer space for output result, Out: output size.
* @param in Buffer that stores cipher text.
* @param in_len Length of cipher text
*/
virtual void priv_decrypt(unsigned char *out, int *out_len,
const unsigned char *in, int in_len);
/**
* Decrypt the data with RSA algorithm using private key in a batch
* All encryption/decryption methods assume RSA_PKCS1_PADDING
*
* @param out Buffers for plain text.
* @param out_len In: allocated buffer space for output results, Out: output sizes.
* @param in Buffers that stores ciphertext.
* @param in_len Length of cipher texts.
* @param n Ciphertexts count.
*/
virtual void priv_decrypt_batch(unsigned char **out, int *out_len,
const unsigned char **in, const int *in_len,
int n);
float get_elapsed_ms_kernel();
static const int max_batch = 2048 / 2;
protected:
void dump_bn(BIGNUM *bn, const char *name);
// returns -1 if it fails
int remove_padding(unsigned char *out, int *out_len, BIGNUM *bn);
RSA *rsa;
BN_CTX *bn_ctx;
float elapsed_ms_kernel;
private:
void set_crt();
bool crt_available;
};
#endif