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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//! A `no_std` crate for compile-time encrypted secrets.
//!
//! This crate provides encrypted storage for sensitive data that is encrypted at compile time
//! and only decrypted at runtime when accessed. This prevents secrets from appearing in
//! plaintext in the final binary.
//!
//! # Features
//!
//! - **Compile-time encryption**: Secrets are encrypted during compilation
//! - **Multiple algorithms**: XOR (simple, fast) and RC4 (stream cipher)
//! - **Drop strategies**: Control what happens to decrypted data on drop:
//! - `Zeroize`: Overwrites memory with zeros
//! - `ReEncrypt`: Re-encrypts the data
//! - `NoOp`: Leaves data unchanged
//! - **Thread-safe**: `Sync` implementation allows concurrent access
//! - `no_std` compatible: Works in embedded environments
//!
//! # Examples
//!
//! ## XOR Algorithm
//!
//! XOR is the simplest and fastest algorithm. It uses a single-byte key:
//!
//! ```rust
//! use const_secret::{
//! Encrypted, StringLiteral,
//! drop_strategy::Zeroize,
//! xor::{ReEncrypt, Xor},
//! };
//!
//! // Zeroize on drop (safest - clears memory)
//! const SECRET_ZEROIZE: Encrypted<Xor<0xAA, Zeroize>, StringLiteral, 5> =
//! Encrypted::<Xor<0xAA, Zeroize>, StringLiteral, 5>::new(*b"hello");
//!
//! // Re-encrypt on drop (good for frequently accessed secrets)
//! const SECRET_REENCRYPT: Encrypted<Xor<0xBB, ReEncrypt<0xBB>>, StringLiteral, 6> =
//! Encrypted::<Xor<0xBB, ReEncrypt<0xBB>>, StringLiteral, 6>::new(*b"secret");
//!
//! // No-op on drop (fastest, but leaves data in memory)
//! const SECRET_NOOP: Encrypted<Xor<0xCC, Zeroize>, StringLiteral, 4> =
//! Encrypted::<Xor<0xCC, Zeroize>, StringLiteral, 4>::new(*b"test");
//! ```
//!
//! ## RC4 Algorithm
//!
//! RC4 is a stream cipher with variable-length keys (1-256 bytes).
//! **Note:** RC4 is cryptographically broken; use only for basic obfuscation:
//!
//! ```rust
//! use const_secret::{
//! Encrypted, StringLiteral, ByteArray,
//! drop_strategy::Zeroize,
//! rc4::{ReEncrypt, Rc4},
//! };
//!
//! const KEY: [u8; 16] = *b"my-secret-key-16";
//!
//! // RC4 with zeroize drop strategy
//! const RC4_SECRET: Encrypted<Rc4<16, Zeroize<[u8; 16]>>, StringLiteral, 6> =
//! Encrypted::<Rc4<16, Zeroize<[u8; 16]>>, StringLiteral, 6>::new(*b"rc4sec", KEY);
//!
//! // RC4 with re-encrypt drop strategy
//! const RC4_REENCRYPT: Encrypted<Rc4<16, ReEncrypt<16>>, StringLiteral, 8> =
//! Encrypted::<Rc4<16, ReEncrypt<16>>, StringLiteral, 8>::new(*b"rc4data!", KEY);
//! ```
//!
//! ## Usage Modes
//!
//! ### `StringLiteral` Mode
//!
//! For UTF-8 string data. Returns `&str` on dereference:
//!
//! ```rust
//! use const_secret::{
//! Encrypted, StringLiteral,
//! drop_strategy::Zeroize,
//! xor::Xor,
//! };
//!
//! const API_KEY: Encrypted<Xor<0xAA, Zeroize>, StringLiteral, 34> =
//! Encrypted::<Xor<0xAA, Zeroize>, StringLiteral, 34>::new(
//! *b"sk-live-1234567890abcdefghijklmnop"
//! );
//!
//! fn main() {
//! let key: &str = &*API_KEY;
//! assert_eq!(key, "sk-live-1234567890abcdefghijklmnop");
//! }
//! ```
//!
//! ### `ByteArray` Mode
//!
//! For binary data. Returns `&[u8; N]` on dereference:
//!
//! ```rust
//! use const_secret::{
//! Encrypted, ByteArray,
//! drop_strategy::Zeroize,
//! xor::Xor,
//! };
//!
//! const BINARY_SECRET: Encrypted<Xor<0xBB, Zeroize>, ByteArray, 16> =
//! Encrypted::<Xor<0xBB, Zeroize>, ByteArray, 16>::new([
//! 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
//! 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
//! ]);
//!
//! fn main() {
//! let data: &[u8; 16] = &*BINARY_SECRET;
//! assert_eq!(data[0], 0x01);
//! }
//! ```
//!
//! ## Choosing an Algorithm
//!
//! | Algorithm | Speed | Security | Use Case |
//! |-----------|-------|----------|----------|
//! | XOR | Fast | Basic | Simple obfuscation, speed critical |
//! | RC4 | Medium| Broken | Variable key length, slightly better obfuscation |
//!
//! ## Drop Strategies
//!
//! | Strategy | Behavior on Drop | Best For |
//! |------------|------------------|----------|
//! | `Zeroize` | Overwrites with zeros | Maximum security |
//! | `ReEncrypt`| Re-encrypts data | If you prefer the residue to remain encrypted after using |
//! | `NoOp` | Leaves unchanged | Performance critical, non-sensitive |
//!
//! # Architecture
//!
//! The crate uses a type-level architecture:
//! - [`Algorithm`]: Trait defining encryption algorithm and associated data
//! - [`Encrypted<A, M, N>`]: Main struct holding encrypted data
//! - [`DropStrategy`]: Trait for handling drop behavior
//! - Mode markers: [`StringLiteral`] and [`ByteArray`]
extern crate std;
extern crate alloc;
use crateDropStrategy;
use ;
/// Decryption state constants for thread-safe lazy decryption
pub const STATE_UNENCRYPTED: u8 = 0;
pub const STATE_DECRYPTING: u8 = 1;
pub const STATE_DECRYPTED: u8 = 2;
/// A trait that defines an encryption algorithm and its associated types.
///
/// This trait is implemented by algorithm types (like [`xor::Xor`]
/// and [`rc4::Rc4`]) to specify:
/// - The drop strategy to use when the encrypted data is dropped
/// - The extra data type that the algorithm needs to store alongside the buffer
///
/// The `Extra` associated type allows algorithms to store additional data
/// (like encryption keys for RC4) within the [`Encrypted`] struct.
/// Mode marker type indicating the encrypted data should be treated as a UTF-8 string literal.
///
/// When used as the `M` type parameter of [`Encrypted<A, M, N>`], dereferencing
/// returns `&str` instead of `&[u8; N]`.
///
/// # Safety
///
/// The original plaintext must be valid UTF-8. The encryption algorithm must
/// preserve the byte values such that decryption produces valid UTF-8.
;
/// Mode marker type indicating the encrypted data should be treated as a byte array.
///
/// When used as the `M` type parameter of [`Encrypted<A, M, N>`], dereferencing
/// returns `&[u8; N]` (a reference to the raw byte array).
;
/// An encrypted container that holds data encrypted at compile time.
///
/// This struct stores encrypted data that is decrypted on first access via
/// the [`Deref`](core::ops::Deref) implementation. The decryption happens
/// exactly once, after which the plaintext is cached for subsequent accesses.
///
/// # Type Parameters
///
/// - `A`: The encryption algorithm type implementing [`Algorithm`]
/// - `M`: The mode marker type ([`StringLiteral`] or [`ByteArray`])
/// - `N`: The size of the encrypted buffer in bytes
///
/// # Thread Safety
///
/// The struct is `Sync`, allowing concurrent access from multiple threads.
/// The first thread to access the data performs the decryption; subsequent
/// accesses read the already-decrypted buffer.
///
/// # Drop Behavior
///
/// When dropped, the data is handled according to the algorithm's
/// [`DropStrategy`]:
/// - [`Zeroize`](crate::drop_strategy::Zeroize): Overwrites with zeros
/// - [`ReEncrypt`](crate::xor::ReEncrypt) / [`ReEncrypt`](crate::rc4::ReEncrypt): Re-encrypts
/// - [`NoOp`](crate::drop_strategy::NoOp): Leaves data unchanged
///
/// # Example
///
/// ```rust
/// use const_secret::{
/// Encrypted, StringLiteral,
/// drop_strategy::Zeroize,
/// xor::Xor,
/// };
///
/// const SECRET: Encrypted<Xor<0xAA, Zeroize>, StringLiteral, 5> =
/// Encrypted::<Xor<0xAA, Zeroize>, StringLiteral, 5>::new(*b"hello");
///
/// fn main() {
/// // Decrypts on first access
/// let decrypted: &str = &*SECRET;
/// assert_eq!(decrypted, "hello");
/// }
/// ```
// SAFETY: `Encrypted` is `Sync` because:
// 1. The 3-state `decryption_state` (AtomicU8) ensures proper synchronization:
// - Only one thread can transition from UNENCRYPTED to DECRYPTING
// - Other threads spin-wait until state becomes DECRYPTED
// 2. The thread that wins the race gets exclusive mutable access during decryption
// 3. After decryption completes (state = DECRYPTED), the buffer is immutable
// 4. Multiple threads can safely read the stable, decrypted buffer concurrently
unsafe