aboutsummaryrefslogtreecommitdiff
path: root/NK_C_API_storage.cpp
blob: 59bae854f337bab501f46578ad29e93b05570af2 (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
#include "NK_C_API_storage.h"
#include "NK_C_API_helpers.h"

using namespace nitrokey;

#include "nk_strndup.h"


#ifdef __cplusplus
extern "C" {
#endif


// storage commands

NK_C_API int NK_send_startup(uint64_t seconds_from_epoch) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->send_startup(seconds_from_epoch);
  });
}

NK_C_API int NK_unlock_encrypted_volume(const char* user_pin) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->unlock_encrypted_volume(user_pin);
  });
}

NK_C_API int NK_lock_encrypted_volume() {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->lock_encrypted_volume();
  });
}

NK_C_API int NK_unlock_hidden_volume(const char* hidden_volume_password) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->unlock_hidden_volume(hidden_volume_password);
  });
}

NK_C_API int NK_lock_hidden_volume() {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->lock_hidden_volume();
  });
}

NK_C_API int NK_create_hidden_volume(uint8_t slot_nr, uint8_t start_percent, uint8_t end_percent,
                                     const char *hidden_volume_password) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->create_hidden_volume(slot_nr, start_percent, end_percent,
                            hidden_volume_password);
  });
}

NK_C_API int NK_set_unencrypted_read_only(const char *user_pin) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->set_unencrypted_read_only(user_pin);
  });
}

NK_C_API int NK_set_unencrypted_read_write(const char *user_pin) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->set_unencrypted_read_write(user_pin);
  });
}

NK_C_API int NK_set_unencrypted_read_only_admin(const char *admin_pin) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->set_unencrypted_read_only_admin(admin_pin);
  });
}

NK_C_API int NK_set_unencrypted_read_write_admin(const char *admin_pin) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->set_unencrypted_read_write_admin(admin_pin);
  });
}

NK_C_API int NK_set_encrypted_read_only(const char* admin_pin) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->set_encrypted_volume_read_only(admin_pin);
  });
}

NK_C_API int NK_set_encrypted_read_write(const char* admin_pin) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->set_encrypted_volume_read_write(admin_pin);
  });
}

NK_C_API int NK_export_firmware(const char* admin_pin) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->export_firmware(admin_pin);
  });
}

NK_C_API int NK_clear_new_sd_card_warning(const char* admin_pin) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->clear_new_sd_card_warning(admin_pin);
  });
}

NK_C_API int NK_fill_SD_card_with_random_data(const char* admin_pin) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->fill_SD_card_with_random_data(admin_pin);
  });
}

NK_C_API int NK_change_update_password(const char* current_update_password,
                                       const char* new_update_password) {
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->change_update_password(current_update_password, new_update_password);
  });
}

NK_C_API int NK_enable_firmware_update(const char* update_password){
  auto m = NitrokeyManager::instance();
  return get_without_result([&]() {
    m->enable_firmware_update(update_password);
  });
}

NK_C_API char* NK_get_status_storage_as_string() {
  auto m = NitrokeyManager::instance();
  return get_with_string_result([&]() {
    return m->get_status_storage_as_string();
  });
}

NK_C_API int NK_get_status_storage(NK_storage_status* out) {
  if (out == nullptr) {
    return -1;
  }
  auto m = NitrokeyManager::instance();
  auto result = get_with_status([&]() {
    return m->get_status_storage();
  }, proto::stick20::DeviceConfigurationResponsePacket::ResponsePayload());
  auto error_code = std::get<0>(result);
  if (error_code != 0) {
    return error_code;
  }

  auto status = std::get<1>(result);
  out->unencrypted_volume_read_only = status.ReadWriteFlagUncryptedVolume_u8 != 0;
  out->unencrypted_volume_active = status.VolumeActiceFlag_st.unencrypted;
  out->encrypted_volume_read_only = status.ReadWriteFlagCryptedVolume_u8 != 0;
  out->encrypted_volume_active = status.VolumeActiceFlag_st.encrypted;
  out->hidden_volume_read_only = status.ReadWriteFlagHiddenVolume_u8 != 0;
  out->hidden_volume_active = status.VolumeActiceFlag_st.hidden;
  out->firmware_version_major = status.versionInfo.major;
  out->firmware_version_minor = status.versionInfo.minor;
  out->firmware_locked = status.FirmwareLocked_u8 != 0;
  out->serial_number_sd_card = status.ActiveSD_CardID_u32;
  out->serial_number_smart_card = status.ActiveSmartCardID_u32;
  out->user_retry_count = status.UserPwRetryCount;
  out->admin_retry_count = status.AdminPwRetryCount;
  out->new_sd_card_found = status.NewSDCardFound_st.NewCard;
  out->filled_with_random = (status.SDFillWithRandomChars_u8 & 0x01) != 0;
  out->stick_initialized = status.StickKeysNotInitiated == 0;
  return 0;
}

NK_C_API int NK_get_storage_production_info(NK_storage_ProductionTest * out){
  if (out == nullptr) {
    return -1;
  }
  auto m = NitrokeyManager::instance();
  auto result = get_with_status([&]() {
    return m->production_info();
  }, proto::stick20::ProductionTest::ResponsePayload());

  auto error_code = std::get<0>(result);
  if (error_code != 0) {
    return error_code;
  }

  stick20::ProductionTest::ResponsePayload status = std::get<1>(result);
  // Cannot use memcpy without declaring C API struct packed
  // (which is not parsed by Python's CFFI apparently), hence the manual way.
#define a(x) out->x = status.x;
  a(FirmwareVersion_au8[0]);
  a(FirmwareVersion_au8[1]);
  a(FirmwareVersionInternal_u8);
  a(SD_Card_Size_u8);
  a(CPU_CardID_u32);
  a(SmartCardID_u32);
  a(SD_CardID_u32);
  a(SC_UserPwRetryCount);
  a(SC_AdminPwRetryCount);
  a(SD_Card_ManufacturingYear_u8);
  a(SD_Card_ManufacturingMonth_u8);
  a(SD_Card_OEM_u16);
  a(SD_WriteSpeed_u16);
  a(SD_Card_Manufacturer_u8);
#undef a
  return 0;
}

NK_C_API int NK_get_SD_usage_data(struct NK_SD_usage_data* out) {
  if (out == nullptr)
    return -1;
  auto m = NitrokeyManager::instance();
  auto result = get_with_status([&]() {
    return m->get_SD_usage_data();
  }, std::make_pair<uint8_t, uint8_t>(0, 0));
  auto error_code = std::get<0>(result);
  if (error_code != 0)
    return error_code;

  auto data = std::get<1>(result);
  out->write_level_min = std::get<0>(data);
  out->write_level_max = std::get<1>(data);

  return 0;
}

NK_C_API char* NK_get_SD_usage_data_as_string() {
  auto m = NitrokeyManager::instance();
  return get_with_string_result([&]() {
    return m->get_SD_usage_data_as_string();
  });
}

NK_C_API int NK_get_progress_bar_value() {
  auto m = NitrokeyManager::instance();
  return std::get<1>(get_with_status([&]() {
    return m->get_progress_bar_value();
  }, -2));
}

NK_C_API int NK_set_unencrypted_volume_rorw_pin_type_user() {
  auto m = NitrokeyManager::instance();
  return get_with_result([&]() {
    return m->set_unencrypted_volume_rorw_pin_type_user() ? 1 : 0;
  });
}


#ifdef __cplusplus
}
#endif