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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//! A hybrid encryption library that provides RSA + AES-GCM encryption and decryption functionality.
//!
//! This library implements a hybrid encryption scheme where:
//! - Data is encrypted with AES-256-GCM (for performance and to handle large data)
//! - The AES key is encrypted with RSA (for secure key exchange)
//!
//! # Examples
//!
//! ```
//! // Generate a new RSA key pair
//! let (private_key, public_key) = generate_rsa_keypair().unwrap();
//! let private_key_pem = private_key.to_pkcs1_pem().unwrap();
//! let public_key_pem = public_key.to_public_key_pem().unwrap();
//!
//! // Encrypt data
//! let data = b"Hello, world!";
//! let encrypted = encrypt(&public_key_pem, data).unwrap();
//!
//! // Decrypt data
//! let decrypted = decrypt(&private_key_pem, &encrypted).unwrap();
//! assert_eq!(data, &decrypted[..]);
//! ```
use ;
use ;
use ;
/// Represents an encrypted message using hybrid encryption (RSA + AES-GCM).
///
/// Contains three components:
/// - The RSA-encrypted AES key
/// - The nonce used for AES-GCM encryption
/// - The AES-GCM encrypted data
/// Encrypts data using a hybrid RSA + AES-GCM approach.
///
/// Takes a PEM-encoded RSA public key and plaintext data, then:
/// 1. Generates a random AES-256 key
/// 2. Encrypts the data with the AES key
/// 3. Encrypts the AES key with the RSA public key
/// 4. Returns a serialized structure containing everything needed for decryption
///
/// # Arguments
///
/// * `public_key` - A PEM-encoded RSA public key
/// * `data` - The plaintext data to encrypt
///
/// # Returns
///
/// A serialized `EncryptedMessage` containing the encrypted data
///
/// # Errors
///
/// Returns an error if:
/// - The public key is invalid or cannot be parsed
/// - The encryption process fails
/// - Serialization fails
/// Encrypts data using a hybrid RSA + AES-GCM approach with a pre-parsed RSA public key.
///
/// This is similar to `encrypt` but takes an already parsed `RsaPublicKey` object
/// rather than a PEM-encoded string.
///
/// # Arguments
///
/// * `public_key` - The RSA public key object
/// * `data` - The plaintext data to encrypt
///
/// # Returns
///
/// A serialized `EncryptedMessage` containing the encrypted data
///
/// # Errors
///
/// Returns an error if:
/// - The AES encryption fails
/// - The RSA encryption of the AES key fails
/// - Serialization fails
/// Decrypts data that was encrypted with the hybrid RSA + AES-GCM approach.
///
/// Takes a PEM-encoded RSA private key and encrypted data, then:
/// 1. Deserializes the encrypted message structure
/// 2. Decrypts the AES key using the RSA private key
/// 3. Uses the decrypted AES key to decrypt the actual data
///
/// # Arguments
///
/// * `private_key` - A PEM-encoded RSA private key
/// * `data` - The encrypted data to decrypt (as serialized by `encrypt`)
///
/// # Returns
///
/// The decrypted plaintext data
///
/// # Errors
///
/// Returns an error if:
/// - The private key is invalid or cannot be parsed
/// - The encrypted data cannot be deserialized
/// - The RSA decryption of the AES key fails
/// - The AES decryption of the data fails
/// Decrypts data that was encrypted with the hybrid RSA + AES-GCM approach using a pre-parsed private key.
///
/// This is similar to `decrypt` but takes an already parsed `RsaPrivateKey` object
/// rather than a PEM-encoded string.
///
/// # Arguments
///
/// * `private_key` - The RSA private key object
/// * `encrypted_data` - The encrypted data to decrypt (as serialized by `encrypt` or `raw_encrypt`)
///
/// # Returns
///
/// The decrypted plaintext data
///
/// # Errors
///
/// Returns an error if:
/// - The encrypted data cannot be deserialized
/// - The RSA decryption of the AES key fails
/// - The AES decryption of the data fails
/// Generates a new RSA key pair for use with the encryption/decryption functions.
///
/// Creates a 2048-bit RSA key pair that can be used for hybrid encryption.
///
/// # Returns
///
/// A tuple containing the private and public keys: `(RsaPrivateKey, RsaPublicKey)`
///
/// # Errors
///
/// Returns an error if the key generation process fails