aboutsummaryrefslogtreecommitdiff
path: root/nitrocli/src/nitrokey.rs
blob: d5e21be49161dc8060b5db67c87306a2d40be6da (plain)
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
// nitrokey.rs

// *************************************************************************
// * Copyright (C) 2017 Daniel Mueller (deso@posteo.net)                   *
// *                                                                       *
// * This program is free software: you can redistribute it and/or modify  *
// * it under the terms of the GNU General Public License as published by  *
// * the Free Software Foundation, either version 3 of the License, or     *
// * (at your option) any later version.                                   *
// *                                                                       *
// * This program is distributed in the hope that it will be useful,       *
// * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
// * GNU General Public License for more details.                          *
// *                                                                       *
// * You should have received a copy of the GNU General Public License     *
// * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
// *************************************************************************

use crc32::crc;
use std::cmp;
use std::mem;


// The Nitrokey Storage vendor ID.
pub const VID: u16 = 0x20A0;
// The Nitrokey Storage product ID.
pub const PID: u16 = 0x4109;

// Magic number identifying a storage response.
pub const MAGIC_NUMBER_STICK20_CONFIG: u16 = 0x3318;


#[derive(Debug)]
#[derive(PartialEq)]
#[repr(u8)]
pub enum Command {
  // The command to enable the encrypted volume.
  EnableEncryptedVolume = 0x20,
  // The command to disable the encrypted volume.
  DisableEncryptedVolume = 0x21,
  // Retrieve the device status.
  GetDeviceStatus = 0x2E,
}


/// A report is the entity we send to the Nitrokey Storage HID.
///
/// A report is always 64 bytes in size. The last four bytes comprise a
/// CRC of the actual payload. Note that when sending or receiving a
/// report it usually is preceded by a one byte report ID. This report
/// ID is zero here and not represented in the actual report object in
/// our design.
#[repr(packed)]
pub struct Report<Payload>
  where Payload: AsRef<[u8]>,
{
  // The actual payload data. A report may encapsulate a command to send
  // to the stick or a response to receive from it.
  pub data: Payload,
  pub crc: u32,
}


impl<P> Report<P>
  where P: AsRef<[u8]> + Default,
{
  pub fn new() -> Report<P> {
    return Report {
      data: P::default(),
      crc: 0,
    };
  }

  pub fn is_valid(&self) -> bool {
    // TODO: Certain commands return a wrong CRC code that does not
    //       match the actual report content. For now we defuse the
    //       check but that cannot stay.
    // return self.crc == crc(self.data.as_ref());
    return self.crc != 0;
  }
}


impl<P> AsRef<[u8]> for Report<P>
  where P: AsRef<[u8]>,
{
  fn as_ref(&self) -> &[u8] {
    unsafe { return mem::transmute::<&Report<P>, &[u8; 64]>(self) };
  }
}


impl<P> From<P> for Report<P>
  where P: AsRef<[u8]>,
{
  fn from(payload: P) -> Report<P> {
    let crc = crc(payload.as_ref());
    return Report {
      data: payload,
      crc: crc,
    };
  }
}


impl<P> AsMut<[u8]> for Report<P>
  where P: AsRef<[u8]>,
{
  fn as_mut(&mut self) -> &mut [u8] {
    unsafe { return mem::transmute::<&mut Report<P>, &mut [u8; 64]>(self) };
  }
}


pub struct EmptyPayload {
  pub data: [u8; 60],
}

impl Default for EmptyPayload {
  fn default() -> EmptyPayload {
    return EmptyPayload {
      data: [0u8; 60],
    };
  }
}

impl AsRef<[u8]> for EmptyPayload {
  fn as_ref(&self) -> &[u8] {
    unsafe { return mem::transmute::<&EmptyPayload, &[u8; 60]>(self) };
  }
}

impl<P> AsRef<Response<P>> for EmptyPayload {
  fn as_ref(&self) -> &Response<P> {
    unsafe { return mem::transmute::<&EmptyPayload, &Response<P>>(self) };
  }
}


macro_rules! defaultCommandType {
  ( $name:ident ) => {
    #[allow(dead_code)]
    #[repr(packed)]
    pub struct $name {
      command: Command,
      padding: [u8; 59],
    }
  }
}

macro_rules! defaultCommandNew {
  ( $name:ident, $command:ident ) => {
    impl $name {
      pub fn new() -> $name {
        return $name{
          command: Command::$command,
          padding: [0; 59],
        };
      }
    }
  }
}

macro_rules! defaultPayloadAsRef {
  ( $name:ty ) => {
    impl AsRef<[u8]> for $name {
      fn as_ref(&self) -> &[u8] {
        unsafe {
          return mem::transmute::<&$name, &[u8; 60]>(self)
        };
      }
    }
  }
}

macro_rules! defaultCommand {
  ( $name:ident, $command:ident ) => {
    defaultCommandType!($name);
    defaultCommandNew!($name, $command);
    defaultPayloadAsRef!($name);
  }
}


#[allow(dead_code)]
#[repr(packed)]
pub struct EnableEncryptedVolumeCommand {
  command: Command,
  // The kind of password. Unconditionally 'P' because the User PIN is
  // used to enable the encrypted volume.
  kind: u8,
  // The password has a maximum length of twenty characters.
  password: [u8; 20],
  padding: [u8; 38],
}


impl EnableEncryptedVolumeCommand {
  pub fn new(password: &Vec<u8>) -> EnableEncryptedVolumeCommand {
    let mut report = EnableEncryptedVolumeCommand {
      command: Command::EnableEncryptedVolume,
      kind: 'P' as u8,
      password: [0; 20],
      padding: [0; 38],
    };

    debug_assert!(password.len() <= report.password.len());

    let len = cmp::min(report.password.len(), password.len());
    report.password[..len].copy_from_slice(&password[..len]);
    return report;
  }
}

defaultPayloadAsRef!(EnableEncryptedVolumeCommand);

defaultCommand!(DisableEncryptedVolumeCommand, DisableEncryptedVolume);
defaultCommand!(DeviceStatusCommand, GetDeviceStatus);


#[allow(dead_code)]
#[derive(Debug)]
#[derive(PartialEq)]
#[repr(u8)]
pub enum CommandStatus {
  Okay = 0,
  WrongCrc = 1,
  WrongSlot = 2,
  SlotNotProgrammed = 3,
  WrongPassword = 4,
  NotAuthorized = 5,
  TimestampWarning = 6,
  NoNameError = 7,
}


#[allow(dead_code)]
#[derive(PartialEq)]
#[repr(u8)]
pub enum StorageStatus {
  Idle = 0,
  Okay = 1,
  Busy = 2,
  WrongPassword = 3,
  BusyProgressbar = 4,
  PasswordMatrixReady = 5,
  NoUserPasswordUnlock = 6,
  SmartcardError = 7,
  SecurityBitActive = 8,
}


#[repr(packed)]
pub struct Response<Payload> {
  pub device_status: StorageStatus,
  pub command: Command,
  pub command_crc: u32,
  pub command_status: CommandStatus,
  pub data: Payload,
}

impl<P> AsRef<[u8]> for Response<P> {
  fn as_ref(&self) -> &[u8] {
    unsafe { return mem::transmute::<&Response<P>, &[u8; 60]>(self) };
  }
}


#[repr(packed)]
pub struct DeviceStatusResponse {
  pub padding0: [u8; 22],
  pub magic: u16,
  pub unencrypted_volume_read_only: u8,
  pub encrypted_volume_read_only: u8,
  pub padding1: u8,
  pub version_major: u8,
  pub padding2: u8,
  pub version_minor: u8,
  pub hidden_volume_read_only: u8,
  pub firmware_locked: u8,
  pub new_sdcard_found: u8,
  pub sdcard_fill_with_random: u8,
  pub active_sdcard_id: u32,
  pub volume_active: u8,
  pub new_smartcard_found: u8,
  pub user_password_retry_count: u8,
  pub admin_password_retry_count: u8,
  pub active_smartcard_id: u32,
  pub storage_keys_missing: u8,
}


#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn encrypted_volume_report() {
    let password = "test42".to_string().into_bytes();
    let report = EnableEncryptedVolumeCommand::new(&password);
    let expected = ['t' as u8, 'e' as u8, 's' as u8, 't' as u8, '4' as u8, '2' as u8, 0u8, 0u8,
                    0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8];
    assert_eq!(report.password, expected);
  }

  #[test]
  #[cfg(debug)]
  #[should_panic(expected = "assertion failed")]
  fn overly_long_password() {
    let password = "012345678912345678901".to_string().into_bytes();
    EnableEncryptedVolumeCommand::new(&password);
  }

  #[test]
  fn report_crc() {
    let password = "passphrase".to_string().into_bytes();
    let payload = EnableEncryptedVolumeCommand::new(&password);
    let report = Report::from(payload);

    // The expected checksum was computed using the original
    // functionality.
    assert_eq!(report.crc, 0xeeb583c);
  }
}