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
use {base64, serde_yaml};
use serde::{self, Deserialize};
use serialize::hex::ToHex;
use sodiumoxide::crypto::{pwhash, secretbox, box_};
use std::{io, fs};
use std::io::Write;
use std::path::{Path, PathBuf};
pub const REPO_VERSION_LOWEST: u32 = 0;
pub const REPO_VERSION_CURRENT: u32 = 1;
pub const DATA_SUBDIR: &'static str = "chunk";
pub const NAME_SUBDIR: &'static str = "name";
pub const INDEX_SUBDIR: &'static str = "index";
pub const LOCK_FILE: &'static str = ".lock";
pub const PUBKEY_FILE: &'static str = "pub_key";
pub const SECKEY_FILE: &'static str = "sec_key";
pub const VERSION_FILE: &'static str = "version";
pub const CONFIG_YML_FILE: &'static str = "config.yml";
pub fn lock_file_path(path: &Path) -> PathBuf {
path.join(LOCK_FILE)
}
pub fn pub_key_file_path(path: &Path) -> PathBuf {
path.join(PUBKEY_FILE)
}
pub fn sec_key_file_path(path: &Path) -> PathBuf {
path.join(SECKEY_FILE)
}
pub fn version_file_path(path: &Path) -> PathBuf {
path.join(VERSION_FILE)
}
pub fn config_yml_file_path(path: &Path) -> PathBuf {
path.join(CONFIG_YML_FILE)
}
pub fn write_seckey_file(path: &Path,
encrypted_seckey: &[u8],
nonce: &secretbox::Nonce,
salt: &pwhash::Salt)
-> io::Result<()> {
let mut seckey_file = fs::File::create(path)?;
let mut writer = &mut seckey_file as &mut Write;
writer.write_all(encrypted_seckey.to_hex().as_bytes())?;
writer.write_all(nonce.0.to_hex().as_bytes())?;
writer.write_all(salt.0.to_hex().as_bytes())?;
writer.flush()?;
Ok(())
}
pub fn write_version_file(repo_path: &Path, version: u32) -> super::Result<()> {
let path = version_file_path(repo_path);
let path_tmp = path.with_extension("tmp");
let mut file = fs::File::create(&path_tmp)?;
{
let mut writer = &mut file as &mut Write;
write!(writer, "{}", version)?;
}
file.flush()?;
file.sync_data()?;
fs::rename(path_tmp, &path)?;
Ok(())
}
pub fn write_config_v0(repo_path: &Path,
pk: box_::PublicKey,
sk: &box_::SecretKey,
passphrase: &str)
-> super::Result<()> {
{
let pubkey_path = pub_key_file_path(repo_path);
let mut pubkey_file = fs::File::create(pubkey_path)?;
(&mut pubkey_file as &mut Write)
.write_all(pk.0.to_hex().as_bytes())?;
pubkey_file.flush()?;
}
{
let salt = pwhash::gen_salt();
let nonce = secretbox::gen_nonce();
let derived_key = super::derive_key(passphrase, &salt)?;
let encrypted_seckey = secretbox::seal(&sk.0, &nonce, &derived_key);
let seckey_path = sec_key_file_path(repo_path);
write_seckey_file(&seckey_path, &encrypted_seckey, &nonce, &salt)?;
}
write_version_file(repo_path, 0)?;
Ok(())
}
pub fn write_config_v1(repo_path: &Path,
pk: &box_::PublicKey,
sk: &box_::SecretKey,
passphrase: &str,
chunking: ChunkingAlgorithm)
-> super::Result<()> {
let salt = pwhash::gen_salt();
let nonce = secretbox::gen_nonce();
let sealed_sk = {
let derived_key = super::derive_key(passphrase, &salt)?;
secretbox::seal(&sk.0, &nonce, &derived_key)
};
let config = Repo {
version: 1,
encryption: Encryption::Curve25519(Curve25519 {
sealed_sec_key: sealed_sk,
pub_key: *pk,
nonce: nonce,
salt: salt,
}),
chunking: Some(chunking),
};
let config_str =
serde_yaml::to_string(&config).expect("yaml serialization failed");
let config_path = config_yml_file_path(repo_path);
let config_path_tmp = config_path.with_extension("tmp");
let mut config_file = fs::File::create(&config_path_tmp)?;
(&mut config_file as &mut Write)
.write_all(config_str.as_bytes())?;
config_file.flush()?;
config_file.sync_data()?;
fs::rename(config_path_tmp, &config_path)?;
write_version_file(repo_path, REPO_VERSION_CURRENT)?;
Ok(())
}
trait MyTryFromBytes: Sized {
type Err: 'static + Sized + ::std::error::Error;
fn try_from(&[u8]) -> Result<Self, Self::Err>;
}
impl MyTryFromBytes for box_::PublicKey {
type Err = io::Error;
fn try_from(slice: &[u8]) -> Result<Self, Self::Err> {
box_::PublicKey::from_slice(slice).ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidData,
"can't derive PublicKey from invalid binary data")
})
}
}
impl MyTryFromBytes for secretbox::Nonce {
type Err = io::Error;
fn try_from(slice: &[u8]) -> Result<Self, Self::Err> {
secretbox::Nonce::from_slice(slice).ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidData,
"can't derive Nonce from invalid binary data")
})
}
}
impl MyTryFromBytes for pwhash::Salt {
type Err = io::Error;
fn try_from(slice: &[u8]) -> Result<Self, Self::Err> {
pwhash::Salt::from_slice(slice).ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidData,
"can't derive Nonce from invalid binary data")
})
}
}
impl MyTryFromBytes for Vec<u8> {
type Err = io::Error;
fn try_from(slice: &[u8]) -> Result<Self, Self::Err> {
Ok(Vec::from(slice))
}
}
fn from_base64<T, D>(deserializer: D) -> Result<T, D::Error>
where D: serde::Deserializer,
T: MyTryFromBytes
{
use serde::de::Error;
String::deserialize(deserializer)
.and_then(|string| {
base64::decode(&string)
.map_err(|err| Error::custom(err.to_string()))
})
.and_then(|ref bytes| {
T::try_from(bytes).map_err(|err| {
Error::custom(format!("{}", &err as &::std::error::Error))
})
})
}
fn as_base64<T, S>(key: &T, serializer: S) -> Result<S::Ok, S::Error>
where T: AsRef<[u8]>,
S: serde::Serializer
{
serializer.serialize_str(&base64::encode(key.as_ref()))
}
#[derive(Serialize, Deserialize)]
pub struct Curve25519 {
#[serde(serialize_with = "as_base64", deserialize_with = "from_base64")]
pub sealed_sec_key: Vec<u8>,
#[serde(serialize_with = "as_base64", deserialize_with = "from_base64")]
pub pub_key: box_::PublicKey,
#[serde(serialize_with = "as_base64", deserialize_with = "from_base64")]
pub salt: pwhash::Salt,
#[serde(serialize_with = "as_base64", deserialize_with = "from_base64")]
pub nonce: secretbox::Nonce,
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
pub enum ChunkingAlgorithm {
#[serde(rename = "bup")]
Bup { chunk_bits: u32 },
}
impl Default for ChunkingAlgorithm {
fn default() -> ChunkingAlgorithm {
ChunkingAlgorithm::Bup { chunk_bits: 17 }
}
}
impl ChunkingAlgorithm {
pub fn valid(self) -> bool {
match self {
ChunkingAlgorithm::Bup { chunk_bits: bits } => {
30 >= bits && bits >= 10
}
}
}
}
#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Encryption {
#[serde(rename = "none")]
None,
#[serde(rename = "curve25519_blake2b_salsa20_poly1305")]
Curve25519(Curve25519),
}
#[derive(Serialize, Deserialize)]
pub struct Repo {
pub version: u32,
pub chunking: Option<ChunkingAlgorithm>,
pub encryption: Encryption,
}