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
//! # tlock_age
//!
//! tlock_age is a library to encrypt and decrypt messages using tlock encryption scheme and age encryption.
//! The reference interroperable Go implementation is available at [drand/tlock](https://github.com/drand/tlock).
//!
//! tlock relies on BLS, content is encrypted against BLS public key. Public key group is assessed based on the public key size.
//! age implementation is [str4d/rage](https://github.com/str4d/rage). When encrypting file, it generates an additional stanza `grease-the-join`, which other tlock implementation have to ignore.
//!
//! Both encryption and decryption are performed wihout network access. tlock_age expects the caller interact with the drand network of their choice, through whichever method they wish. To that end, a dedicated method `decrypt_header` is provided.
//! It's important to note that `decrypt_header` consumes bytes from the reader, and reader requires these headers to be passed as well. The caller needs to duplicates these inputs if needed.
//!
//! ## Features
//!
//! - `armor`: Enable armor output. This is required if you want to output bytes are ASCII printable.
//!
//! ## Example
//!
//! For a working example, refer to [examples/encrypt_decrypt.rs](../examples/encrypt_decrypt.rs).
use ;
use ;
use Error;
/// Encrypt using tlock encryption scheme and age encryption.
///
/// `round` and `public_key` information are stored as an age header.
///
/// If you want to armor the output to output bytes are ASCII printable, you must enable `armor` feature.
///
/// Example using an empty 100-byte message, fastnet public key, at round 1000
///
/// ```rust
/// // curl -sS https://api.drand.sh/dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493/info | jq -r '.public_key'
/// let chain_hash = hex::decode("dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493").unwrap();
/// let pk_bytes = hex::decode("a0b862a7527fee3a731bcb59280ab6abd62d5c0b6ea03dc4ddf6612fdfc9d01f01c31542541771903475eb1ec6615f8d0df0b8b6dce385811d6dcf8cbefb8759e5e616a3dfd054c928940766d9a5b9db91e3b697e5d70a975181e007f87fca5e").unwrap();
/// let round = 1000;
/// let src = vec![0u8; 100];
///
/// // without armor
/// let mut encrypted = vec![];
/// tlock_age::encrypt(&mut encrypted, src.as_slice(), &chain_hash, &pk_bytes, round).unwrap();
///
/// // with armor
/// let mut encrypted = vec![];
/// let mut encrypted = tlock_age::armor::ArmoredWriter::wrap_output(encrypted).unwrap();
/// tlock_age::encrypt(&mut encrypted, src.as_slice(), &chain_hash, &pk_bytes, round);
/// encrypted.finish().unwrap();
/// ```
/// Information stored in tlock age header
/// Decrypt tlock age header.
///
/// tlock_age uses age encryption, and age header. These information might be needed before decryption.
/// For instance, one need to retrieve the round a message is encrypted to, in order to retrieve it.
///
/// Example using an empty 100-byte message, fastnet public key, at round 1000
///
/// ```rust
/// let chain_hash = hex::decode("dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493").unwrap();
/// // curl -sS https://api.drand.sh/dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493/public/1000 | jq -r '.signature'
/// let signature = hex::decode("b09eacd45767c4d58306b98901ad0d6086e2663766f3a4ec71d00cf26f0f49eaf248abc7151c60cf419c4e8b37e80412").unwrap();
///
/// // This message is the encryption of an empty 100-byte message, using fastnet public key, at round 1000
/// let encrypted = "-----BEGIN AGE ENCRYPTED FILE-----
/// YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHRsb2NrIDEwMDAgZGJkNTA2ZDZlZjc2
/// ZTVmMzg2ZjQxYzY1MWRjYjgwOGM1YmNiZDc1NDcxY2M0ZWFmYTNmNGRmN2FkNGU0
/// YzQ5MwppZ09hY2VnNC9hNUNrYzJXVTlhWkpkRHpFUkZOWWNaSDZVdy9tc2tiWXlK
/// SWx3Rnc2QWU4TDMzUXpZQ2NiY0dSCkRPZXFYeGl1dWI1SUVkRUVlV1doWkFTNmxk
/// TG02TDhZTDFCTE1HZ1ZPVHF2b2xQVDhYQVlSMGk4aUJNTFJyTGMKYStBbFQvZEtI
/// eWZIa0VrYW56ZFBNQ0l4WDFmOGlkU2N6RHpVZk92WGYrYwotPiAlVl8tZ3JlYXNl
/// CldreTFpeWxFdGd5MlZXRjJzZDdiRHRXc3dkc0VRQmVuUTA0MUdoY3R2QjQKLS0t
/// IEJyTDlWVEIvem5mbjFxYzlzYTUyTGFITm5iOGVQTFIwMzg5bUJQQU92a2cKB1ca
/// 67u0wjEVoC/sRHsi+F7E2SJ6Z7bQQAva+dS1YYpXt07AdXOmqllqVGy8YGhi3wSf
/// ZlxKpBAQw+Lz+nvn5vCJPPNQcp264wTd6om5h9QnuHBkxkbr7JBqBeRxNucdl6El
/// 2af/n+e6s7cuhvvZp/TPTtV000+2Crq8kmAQIpzTdVIe
/// -----END AGE ENCRYPTED FILE-----".as_bytes();
///
/// let header = tlock_age::decrypt_header(encrypted).unwrap();
/// ```
/// Decrypt using tlock encryption scheme and age encryption.
///
/// round and public key information are retrieved from age header.
/// signature has to be the one for that round.
/// src can be armored or not, decryption supports both.
///
/// Example using an empty 100-byte message, fastnet public key, at round 1000
///
/// ```rust
/// let chain_hash = hex::decode("dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493").unwrap();
/// // curl -sS https://api.drand.sh/dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493/public/1000 | jq -r '.signature'
/// let signature = hex::decode("b09eacd45767c4d58306b98901ad0d6086e2663766f3a4ec71d00cf26f0f49eaf248abc7151c60cf419c4e8b37e80412").unwrap();
///
/// // This message is the encryption of an empty 100-byte message, using fastnet public key, at round 1000
/// let encrypted = "-----BEGIN AGE ENCRYPTED FILE-----
/// YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHRsb2NrIDEwMDAgZGJkNTA2ZDZlZjc2
/// ZTVmMzg2ZjQxYzY1MWRjYjgwOGM1YmNiZDc1NDcxY2M0ZWFmYTNmNGRmN2FkNGU0
/// YzQ5MwppZ09hY2VnNC9hNUNrYzJXVTlhWkpkRHpFUkZOWWNaSDZVdy9tc2tiWXlK
/// SWx3Rnc2QWU4TDMzUXpZQ2NiY0dSCkRPZXFYeGl1dWI1SUVkRUVlV1doWkFTNmxk
/// TG02TDhZTDFCTE1HZ1ZPVHF2b2xQVDhYQVlSMGk4aUJNTFJyTGMKYStBbFQvZEtI
/// eWZIa0VrYW56ZFBNQ0l4WDFmOGlkU2N6RHpVZk92WGYrYwotPiAlVl8tZ3JlYXNl
/// CldreTFpeWxFdGd5MlZXRjJzZDdiRHRXc3dkc0VRQmVuUTA0MUdoY3R2QjQKLS0t
/// IEJyTDlWVEIvem5mbjFxYzlzYTUyTGFITm5iOGVQTFIwMzg5bUJQQU92a2cKB1ca
/// 67u0wjEVoC/sRHsi+F7E2SJ6Z7bQQAva+dS1YYpXt07AdXOmqllqVGy8YGhi3wSf
/// ZlxKpBAQw+Lz+nvn5vCJPPNQcp264wTd6om5h9QnuHBkxkbr7JBqBeRxNucdl6El
/// 2af/n+e6s7cuhvvZp/TPTtV000+2Crq8kmAQIpzTdVIe
/// -----END AGE ENCRYPTED FILE-----".as_bytes();
///
/// let decrypted = vec![];
/// tlock_age::decrypt(decrypted, encrypted, &chain_hash, &signature).unwrap();
/// ```