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
//! A safe Rust interface to browser-based cryptographic operations using the
//! Web Crypto API.
//!
//! This crate provides a type-safe wrapper around the browser's native
//! cryptographic functionality, making it easier to perform common
//! cryptographic operations in WebAssembly applications.
//!
//! # Features
//!
//! - Type-safe cryptographic algorithm implementations
//! - Secure nonce generation and handling
//! - AES-256-GCM encryption and decryption
//! - Proper error handling and conversion from Web API exceptions
//!
//! # Examples
//!
//! ```rust,no_run
//! use browser_crypto::aes256gcm::Aes256Gcm;
//! use browser_crypto::algorithm::Algorithm;
//!
//! async fn encrypt_data() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a new AES-256-GCM instance with a key
//! let key_bytes = [0u8; 32]; // Replace with your secure key
//! let cipher = Aes256Gcm::from_key(&key_bytes).await?;
//!
//! // Generate a random nonce
//! let nonce = Aes256Gcm::generate_nonce()?;
//!
//! // Encrypt some data
//! let data = b"Secret message";
//! let encrypted = cipher.encrypt(&nonce, data).await?;
//!
//! // Decrypt the data
//! let decrypted = cipher.decrypt(&nonce, &encrypted).await?;
//! assert_eq!(data.to_vec(), decrypted);
//!
//! Ok(())
//! }
//! ```
//!
//! # Security Considerations
//!
//! This crate relies on the browser's implementation of the Web Crypto API,
//! which:
//!
//! - Uses the platform's secure random number generator
//! - Implements cryptographic operations in native code
//! - Provides protection against timing attacks
//! - Follows modern cryptographic standards
//!
//! However, users should be aware that:
//!
//! - Keys should be generated and stored securely
//! - Nonces should never be reused with the same key
//! - The security of the application depends on the security of the browser
//!
//! # Features Flags
//!
//! - `log-error`: Enables console logging of unknown errors (useful for
//! debugging)
//!
//! # Browser Compatibility
//!
//! This crate requires a browser with support for:
//!
//! - Web Crypto API
//! - WebAssembly
//! - Async/await
//!
//! Most modern browsers (Chrome, Firefox, Safari, Edge) support these features.
//!
//! # Error Handling
//!
//! The crate provides detailed error types that map directly to Web Crypto API
//! exceptions, making it easier to handle and debug cryptographic operations:
//!
//! - `Error`: General Web Crypto API errors
//! - `EncryptionError`: Encryption-specific errors
//! - `DecryptionError`: Decryption-specific errors
//! - `NonceError`: Nonce generation and validation errors
//! - `ImportKeyError`: Key import and format errors
//!
//! # Implementation Details
//!
//! This crate uses `wasm-bindgen` to interface with the Web Crypto API and
//! provides a safe Rust interface for:
//!
//! - Key management
//! - Nonce generation
//! - Encryption/decryption operations
//! - Error handling and conversion
//!
//! The implementation focuses on safety, correctness, and ergonomic use in Rust
//! while maintaining the security properties of the underlying Web Crypto API.
use Promise;
use ;
use JsFuture;
use ;
/// Utility functions
/// Resolves a JavaScript Promise to a Rust Result
///
/// # Arguments
/// * `promise` - JavaScript Promise to resolve
///
/// # Returns
/// Result containing resolved value or error
async
/// General errors that can occur when interacting with the Web Crypto API.
/// Gets the Web Crypto API interface
///
/// # Returns
/// Result containing SubtleCrypto interface or Error