@geastack/host 0.1.26 → 0.1.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/bluetooth.cpp +22 -0
- package/host/ble.cpp +44 -0
- package/host/bluetooth_central_apple.mm +412 -0
- package/host/crypto.cpp +318 -0
- package/host/crypto_apple.mm +69 -0
- package/host/files.cpp +189 -0
- package/host/files_apple.mm +25 -0
- package/host/image.cpp +14 -5
- package/host/menu_bar.cpp +26 -0
- package/host/menu_bar_apple.mm +174 -0
- package/host/opus.cpp +31 -0
- package/host/opus_apple.mm +134 -0
- package/host/websocket.cpp +29 -2
- package/host/websocket_apple.mm +30 -9
- package/host/websocket_curl.cpp +15 -4
- package/include/gea/embedded-host.h +4 -0
- package/include/host/backends.h +25 -0
- package/include/host/crypto.h +46 -0
- package/include/host/files.h +42 -0
- package/include/host/menu_bar.h +37 -0
- package/include/host/navigator.h +67 -1
- package/include/host/opus.h +35 -0
- package/include/host/websocket.h +2 -0
- package/package.json +1 -1
- package/services/app_state.cpp +8 -0
package/host/crypto.cpp
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Portable cryptography for the `Cryptography` host API (host/crypto.h):
|
|
3
|
+
// SHA-256 (FIPS 180-4), HMAC (RFC 2104), HKDF (RFC 5869) and AES-CTR
|
|
4
|
+
// (FIPS 197 / SP 800-38A). P-256 and the random source come from the platform
|
|
5
|
+
// through the crypto_platform hooks; the weak fallbacks below cover hosts
|
|
6
|
+
// without one.
|
|
7
|
+
#include "host/crypto.h"
|
|
8
|
+
|
|
9
|
+
#include <algorithm>
|
|
10
|
+
#include <array>
|
|
11
|
+
#include <cstdio>
|
|
12
|
+
#include <cstring>
|
|
13
|
+
|
|
14
|
+
#if defined(ESP_PLATFORM)
|
|
15
|
+
#include "esp_random.h"
|
|
16
|
+
#endif
|
|
17
|
+
|
|
18
|
+
namespace gea::host {
|
|
19
|
+
namespace {
|
|
20
|
+
|
|
21
|
+
// MARK: SHA-256
|
|
22
|
+
|
|
23
|
+
constexpr std::array<std::uint32_t, 64> kRound = {
|
|
24
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
25
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
26
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
27
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
28
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
29
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
30
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
31
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
inline std::uint32_t rotr(std::uint32_t value, int bits) { return (value >> bits) | (value << (32 - bits)); }
|
|
35
|
+
|
|
36
|
+
class Sha256 {
|
|
37
|
+
public:
|
|
38
|
+
void update(const std::uint8_t *data, std::size_t length) {
|
|
39
|
+
for (std::size_t index = 0; index < length; index++) {
|
|
40
|
+
block_[used_++] = data[index];
|
|
41
|
+
if (used_ == 64) {
|
|
42
|
+
compress();
|
|
43
|
+
used_ = 0;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
total_ += length;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
std::array<std::uint8_t, 32> finish() {
|
|
50
|
+
const std::uint64_t bits = total_ * 8;
|
|
51
|
+
const std::uint8_t one = 0x80;
|
|
52
|
+
const std::uint8_t zero = 0;
|
|
53
|
+
update(&one, 1);
|
|
54
|
+
while (used_ != 56) update(&zero, 1);
|
|
55
|
+
std::uint8_t length[8];
|
|
56
|
+
for (int index = 0; index < 8; index++) length[index] = static_cast<std::uint8_t>(bits >> (56 - 8 * index));
|
|
57
|
+
update(length, 8);
|
|
58
|
+
std::array<std::uint8_t, 32> digest{};
|
|
59
|
+
for (int word = 0; word < 8; word++) {
|
|
60
|
+
for (int byte = 0; byte < 4; byte++) {
|
|
61
|
+
digest[word * 4 + byte] = static_cast<std::uint8_t>(state_[word] >> (24 - 8 * byte));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return digest;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private:
|
|
68
|
+
void compress() {
|
|
69
|
+
std::uint32_t w[64];
|
|
70
|
+
for (int index = 0; index < 16; index++) {
|
|
71
|
+
w[index] = (std::uint32_t(block_[index * 4]) << 24) | (std::uint32_t(block_[index * 4 + 1]) << 16) |
|
|
72
|
+
(std::uint32_t(block_[index * 4 + 2]) << 8) | std::uint32_t(block_[index * 4 + 3]);
|
|
73
|
+
}
|
|
74
|
+
for (int index = 16; index < 64; index++) {
|
|
75
|
+
const std::uint32_t s0 = rotr(w[index - 15], 7) ^ rotr(w[index - 15], 18) ^ (w[index - 15] >> 3);
|
|
76
|
+
const std::uint32_t s1 = rotr(w[index - 2], 17) ^ rotr(w[index - 2], 19) ^ (w[index - 2] >> 10);
|
|
77
|
+
w[index] = w[index - 16] + s0 + w[index - 7] + s1;
|
|
78
|
+
}
|
|
79
|
+
std::uint32_t a = state_[0], b = state_[1], c = state_[2], d = state_[3];
|
|
80
|
+
std::uint32_t e = state_[4], f = state_[5], g = state_[6], h = state_[7];
|
|
81
|
+
for (int index = 0; index < 64; index++) {
|
|
82
|
+
const std::uint32_t s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
83
|
+
const std::uint32_t choose = (e & f) ^ (~e & g);
|
|
84
|
+
const std::uint32_t t1 = h + s1 + choose + kRound[index] + w[index];
|
|
85
|
+
const std::uint32_t s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
|
86
|
+
const std::uint32_t majority = (a & b) ^ (a & c) ^ (b & c);
|
|
87
|
+
const std::uint32_t t2 = s0 + majority;
|
|
88
|
+
h = g;
|
|
89
|
+
g = f;
|
|
90
|
+
f = e;
|
|
91
|
+
e = d + t1;
|
|
92
|
+
d = c;
|
|
93
|
+
c = b;
|
|
94
|
+
b = a;
|
|
95
|
+
a = t1 + t2;
|
|
96
|
+
}
|
|
97
|
+
state_[0] += a;
|
|
98
|
+
state_[1] += b;
|
|
99
|
+
state_[2] += c;
|
|
100
|
+
state_[3] += d;
|
|
101
|
+
state_[4] += e;
|
|
102
|
+
state_[5] += f;
|
|
103
|
+
state_[6] += g;
|
|
104
|
+
state_[7] += h;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
std::uint32_t state_[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
|
108
|
+
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
|
|
109
|
+
std::uint8_t block_[64] = {};
|
|
110
|
+
std::size_t used_ = 0;
|
|
111
|
+
std::uint64_t total_ = 0;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
CryptoBytes hmac(const CryptoBytes &key, const std::uint8_t *data, std::size_t length) {
|
|
115
|
+
std::uint8_t block[64] = {};
|
|
116
|
+
if (key.size() > 64) {
|
|
117
|
+
Sha256 hash;
|
|
118
|
+
hash.update(key.data(), key.size());
|
|
119
|
+
const auto digest = hash.finish();
|
|
120
|
+
std::memcpy(block, digest.data(), digest.size());
|
|
121
|
+
} else if (!key.empty()) {
|
|
122
|
+
std::memcpy(block, key.data(), key.size());
|
|
123
|
+
}
|
|
124
|
+
std::uint8_t inner[64];
|
|
125
|
+
std::uint8_t outer[64];
|
|
126
|
+
for (int index = 0; index < 64; index++) {
|
|
127
|
+
inner[index] = block[index] ^ 0x36;
|
|
128
|
+
outer[index] = block[index] ^ 0x5c;
|
|
129
|
+
}
|
|
130
|
+
Sha256 first;
|
|
131
|
+
first.update(inner, 64);
|
|
132
|
+
first.update(data, length);
|
|
133
|
+
const auto innerDigest = first.finish();
|
|
134
|
+
Sha256 second;
|
|
135
|
+
second.update(outer, 64);
|
|
136
|
+
second.update(innerDigest.data(), innerDigest.size());
|
|
137
|
+
const auto mac = second.finish();
|
|
138
|
+
return CryptoBytes(mac.begin(), mac.end());
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// MARK: AES (encryption direction only: CTR needs nothing else)
|
|
142
|
+
|
|
143
|
+
constexpr std::array<std::uint8_t, 256> kSbox = {
|
|
144
|
+
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82,
|
|
145
|
+
0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26,
|
|
146
|
+
0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96,
|
|
147
|
+
0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
|
|
148
|
+
0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb,
|
|
149
|
+
0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
|
|
150
|
+
0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff,
|
|
151
|
+
0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
|
|
152
|
+
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32,
|
|
153
|
+
0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
|
|
154
|
+
0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6,
|
|
155
|
+
0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
|
|
156
|
+
0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e,
|
|
157
|
+
0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
|
|
158
|
+
0xb0, 0x54, 0xbb, 0x16,
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
inline std::uint8_t xtime(std::uint8_t value) {
|
|
162
|
+
return static_cast<std::uint8_t>((value << 1) ^ ((value & 0x80) ? 0x1b : 0x00));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
class Aes {
|
|
166
|
+
public:
|
|
167
|
+
bool setKey(const CryptoBytes &key) {
|
|
168
|
+
const std::size_t words = key.size() / 4;
|
|
169
|
+
if (key.size() != 16 && key.size() != 24 && key.size() != 32) return false;
|
|
170
|
+
rounds_ = static_cast<int>(words) + 6;
|
|
171
|
+
const std::size_t total = 4 * static_cast<std::size_t>(rounds_ + 1);
|
|
172
|
+
std::memcpy(schedule_, key.data(), key.size());
|
|
173
|
+
std::uint8_t rcon = 1;
|
|
174
|
+
for (std::size_t word = words; word < total; word++) {
|
|
175
|
+
std::uint8_t temp[4];
|
|
176
|
+
std::memcpy(temp, schedule_ + (word - 1) * 4, 4);
|
|
177
|
+
if (word % words == 0) {
|
|
178
|
+
const std::uint8_t first = temp[0];
|
|
179
|
+
temp[0] = static_cast<std::uint8_t>(kSbox[temp[1]] ^ rcon);
|
|
180
|
+
temp[1] = kSbox[temp[2]];
|
|
181
|
+
temp[2] = kSbox[temp[3]];
|
|
182
|
+
temp[3] = kSbox[first];
|
|
183
|
+
rcon = xtime(rcon);
|
|
184
|
+
} else if (words > 6 && word % words == 4) {
|
|
185
|
+
for (auto &byte : temp) byte = kSbox[byte];
|
|
186
|
+
}
|
|
187
|
+
for (int index = 0; index < 4; index++) {
|
|
188
|
+
schedule_[word * 4 + index] = schedule_[(word - words) * 4 + index] ^ temp[index];
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
void encrypt(const std::uint8_t in[16], std::uint8_t out[16]) const {
|
|
195
|
+
std::uint8_t state[16];
|
|
196
|
+
std::memcpy(state, in, 16);
|
|
197
|
+
addRoundKey(state, 0);
|
|
198
|
+
for (int round = 1; round <= rounds_; round++) {
|
|
199
|
+
for (auto &byte : state) byte = kSbox[byte];
|
|
200
|
+
// ShiftRows on the column-major state.
|
|
201
|
+
std::uint8_t shifted[16];
|
|
202
|
+
for (int column = 0; column < 4; column++) {
|
|
203
|
+
for (int row = 0; row < 4; row++) shifted[column * 4 + row] = state[((column + row) % 4) * 4 + row];
|
|
204
|
+
}
|
|
205
|
+
std::memcpy(state, shifted, 16);
|
|
206
|
+
if (round != rounds_) {
|
|
207
|
+
for (int column = 0; column < 4; column++) {
|
|
208
|
+
std::uint8_t *c = state + column * 4;
|
|
209
|
+
const std::uint8_t all = c[0] ^ c[1] ^ c[2] ^ c[3];
|
|
210
|
+
const std::uint8_t first = c[0];
|
|
211
|
+
c[0] ^= all ^ xtime(c[0] ^ c[1]);
|
|
212
|
+
c[1] ^= all ^ xtime(c[1] ^ c[2]);
|
|
213
|
+
c[2] ^= all ^ xtime(c[2] ^ c[3]);
|
|
214
|
+
c[3] ^= all ^ xtime(c[3] ^ first);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
addRoundKey(state, round);
|
|
218
|
+
}
|
|
219
|
+
std::memcpy(out, state, 16);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private:
|
|
223
|
+
void addRoundKey(std::uint8_t state[16], int round) const {
|
|
224
|
+
for (int index = 0; index < 16; index++) state[index] ^= schedule_[round * 16 + index];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
std::uint8_t schedule_[240] = {};
|
|
228
|
+
int rounds_ = 0;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
} // namespace
|
|
232
|
+
|
|
233
|
+
CryptoBytes CryptographyFacade::sha256(const CryptoBytes &data) const {
|
|
234
|
+
Sha256 hash;
|
|
235
|
+
hash.update(data.data(), data.size());
|
|
236
|
+
const auto digest = hash.finish();
|
|
237
|
+
return CryptoBytes(digest.begin(), digest.end());
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
CryptoBytes CryptographyFacade::hmacSha256(const CryptoBytes &key, const CryptoBytes &data) const {
|
|
241
|
+
return hmac(key, data.data(), data.size());
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
CryptoBytes CryptographyFacade::hkdfSha256(const CryptoBytes &keyMaterial, const CryptoBytes &salt,
|
|
245
|
+
const CryptoBytes &info, double length) const {
|
|
246
|
+
const std::size_t wanted = length > 0 ? static_cast<std::size_t>(length) : 0;
|
|
247
|
+
if (wanted == 0 || wanted > 255 * 32) return {};
|
|
248
|
+
const CryptoBytes pseudoRandomKey = hmac(salt, keyMaterial.data(), keyMaterial.size());
|
|
249
|
+
CryptoBytes output;
|
|
250
|
+
CryptoBytes previous;
|
|
251
|
+
for (std::uint8_t counter = 1; output.size() < wanted; counter++) {
|
|
252
|
+
CryptoBytes input = previous;
|
|
253
|
+
input.insert(input.end(), info.begin(), info.end());
|
|
254
|
+
input.push_back(counter);
|
|
255
|
+
previous = hmac(pseudoRandomKey, input.data(), input.size());
|
|
256
|
+
output.insert(output.end(), previous.begin(), previous.end());
|
|
257
|
+
}
|
|
258
|
+
output.resize(wanted);
|
|
259
|
+
return output;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
CryptoBytes CryptographyFacade::aesCtr(const CryptoBytes &key, const CryptoBytes &counter,
|
|
263
|
+
const CryptoBytes &data) const {
|
|
264
|
+
Aes aes;
|
|
265
|
+
if (counter.size() != 16 || !aes.setKey(key)) return {};
|
|
266
|
+
std::uint8_t block[16];
|
|
267
|
+
std::uint8_t stream[16];
|
|
268
|
+
std::memcpy(block, counter.data(), 16);
|
|
269
|
+
CryptoBytes output(data.size());
|
|
270
|
+
for (std::size_t offset = 0; offset < data.size(); offset += 16) {
|
|
271
|
+
aes.encrypt(block, stream);
|
|
272
|
+
const std::size_t count = std::min<std::size_t>(16, data.size() - offset);
|
|
273
|
+
for (std::size_t index = 0; index < count; index++) output[offset + index] = data[offset + index] ^ stream[index];
|
|
274
|
+
for (int index = 15; index >= 0 && ++block[index] == 0; index--) {
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return output;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
CryptoBytes CryptographyFacade::randomBytes(double length) const {
|
|
281
|
+
const std::size_t wanted = length > 0 ? static_cast<std::size_t>(length) : 0;
|
|
282
|
+
CryptoBytes bytes(wanted);
|
|
283
|
+
if (wanted && !crypto_platform::randomFill(bytes.data(), bytes.size())) return {};
|
|
284
|
+
return bytes;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
CryptoBytes CryptographyFacade::p256GenerateKey() const { return crypto_platform::p256GenerateKey(); }
|
|
288
|
+
|
|
289
|
+
CryptoBytes CryptographyFacade::p256SharedSecret(const CryptoBytes &privateKey, const CryptoBytes &peerPublicKey) const {
|
|
290
|
+
if (privateKey.size() != 97 || peerPublicKey.size() != 65 || peerPublicKey[0] != 0x04) return {};
|
|
291
|
+
return crypto_platform::p256SharedSecret(privateKey, peerPublicKey);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
bool CryptographyFacade::p256Supported() const { return crypto_platform::p256Supported(); }
|
|
295
|
+
|
|
296
|
+
namespace crypto_platform {
|
|
297
|
+
|
|
298
|
+
__attribute__((weak)) bool randomFill(std::uint8_t *bytes, std::size_t length) {
|
|
299
|
+
#if defined(ESP_PLATFORM)
|
|
300
|
+
esp_fill_random(bytes, length);
|
|
301
|
+
return true;
|
|
302
|
+
#else
|
|
303
|
+
FILE *source = std::fopen("/dev/urandom", "rb");
|
|
304
|
+
if (!source) return false;
|
|
305
|
+
const std::size_t read = std::fread(bytes, 1, length, source);
|
|
306
|
+
std::fclose(source);
|
|
307
|
+
return read == length;
|
|
308
|
+
#endif
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
__attribute__((weak)) CryptoBytes p256GenerateKey() { return {}; }
|
|
312
|
+
|
|
313
|
+
__attribute__((weak)) CryptoBytes p256SharedSecret(const CryptoBytes &, const CryptoBytes &) { return {}; }
|
|
314
|
+
|
|
315
|
+
__attribute__((weak)) bool p256Supported() { return false; }
|
|
316
|
+
|
|
317
|
+
} // namespace crypto_platform
|
|
318
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Apple platform hooks for the `Cryptography` host API (host/crypto.h): the
|
|
3
|
+
// system random source and P-256 key agreement from Security.framework.
|
|
4
|
+
#if defined(__APPLE__)
|
|
5
|
+
|
|
6
|
+
#import <Foundation/Foundation.h>
|
|
7
|
+
#import <Security/Security.h>
|
|
8
|
+
|
|
9
|
+
#include "host/crypto.h"
|
|
10
|
+
|
|
11
|
+
__asm__(".linker_option \"-framework\", \"Security\"");
|
|
12
|
+
|
|
13
|
+
namespace gea::host::crypto_platform {
|
|
14
|
+
namespace {
|
|
15
|
+
|
|
16
|
+
CryptoBytes bytesOf(CFDataRef data) {
|
|
17
|
+
if (!data) return {};
|
|
18
|
+
const auto *start = CFDataGetBytePtr(data);
|
|
19
|
+
return CryptoBytes(start, start + CFDataGetLength(data));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
SecKeyRef keyOf(const CryptoBytes &bytes, CFStringRef keyClass) {
|
|
23
|
+
NSData *data = [NSData dataWithBytes:bytes.data() length:bytes.size()];
|
|
24
|
+
NSDictionary *attributes = @{
|
|
25
|
+
(id)kSecAttrKeyType : (id)kSecAttrKeyTypeECSECPrimeRandom,
|
|
26
|
+
(id)kSecAttrKeyClass : (__bridge id)keyClass,
|
|
27
|
+
(id)kSecAttrKeySizeInBits : @256,
|
|
28
|
+
};
|
|
29
|
+
return SecKeyCreateWithData((__bridge CFDataRef)data, (__bridge CFDictionaryRef)attributes, nullptr);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
} // namespace
|
|
33
|
+
|
|
34
|
+
bool randomFill(std::uint8_t *bytes, std::size_t length) {
|
|
35
|
+
return SecRandomCopyBytes(kSecRandomDefault, length, bytes) == errSecSuccess;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
CryptoBytes p256GenerateKey() {
|
|
39
|
+
NSDictionary *attributes = @{(id)kSecAttrKeyType : (id)kSecAttrKeyTypeECSECPrimeRandom, (id)kSecAttrKeySizeInBits : @256};
|
|
40
|
+
SecKeyRef key = SecKeyCreateRandomKey((__bridge CFDictionaryRef)attributes, nullptr);
|
|
41
|
+
if (!key) return {};
|
|
42
|
+
// The private key's external representation is X9.63: 04 || X || Y || K.
|
|
43
|
+
CFDataRef external = SecKeyCopyExternalRepresentation(key, nullptr);
|
|
44
|
+
CFRelease(key);
|
|
45
|
+
CryptoBytes bytes = bytesOf(external);
|
|
46
|
+
if (external) CFRelease(external);
|
|
47
|
+
return bytes.size() == 97 ? bytes : CryptoBytes{};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
CryptoBytes p256SharedSecret(const CryptoBytes &privateKey, const CryptoBytes &peerPublicKey) {
|
|
51
|
+
SecKeyRef mine = keyOf(privateKey, kSecAttrKeyClassPrivate);
|
|
52
|
+
SecKeyRef theirs = keyOf(peerPublicKey, kSecAttrKeyClassPublic);
|
|
53
|
+
CryptoBytes secret;
|
|
54
|
+
if (mine && theirs) {
|
|
55
|
+
CFDataRef shared = SecKeyCopyKeyExchangeResult(mine, kSecKeyAlgorithmECDHKeyExchangeStandard, theirs,
|
|
56
|
+
(__bridge CFDictionaryRef) @{}, nullptr);
|
|
57
|
+
secret = bytesOf(shared);
|
|
58
|
+
if (shared) CFRelease(shared);
|
|
59
|
+
}
|
|
60
|
+
if (mine) CFRelease(mine);
|
|
61
|
+
if (theirs) CFRelease(theirs);
|
|
62
|
+
return secret;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
bool p256Supported() { return true; }
|
|
66
|
+
|
|
67
|
+
} // namespace gea::host::crypto_platform
|
|
68
|
+
|
|
69
|
+
#endif
|
package/host/files.cpp
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The `Files` host API (host/files.h): portable stdio/POSIX file access. File
|
|
3
|
+
// work runs on an internal stack so flash-backed volumes stay safe on boards
|
|
4
|
+
// whose app task lives in PSRAM.
|
|
5
|
+
#include "host/files.h"
|
|
6
|
+
|
|
7
|
+
#include <cerrno>
|
|
8
|
+
#include <cstdio>
|
|
9
|
+
#include <cstdlib>
|
|
10
|
+
#include <sys/stat.h>
|
|
11
|
+
|
|
12
|
+
#if defined(_WIN32)
|
|
13
|
+
#include <direct.h>
|
|
14
|
+
#include <io.h>
|
|
15
|
+
#else
|
|
16
|
+
#include <unistd.h>
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
#include "platform/internal_stack.h"
|
|
20
|
+
|
|
21
|
+
namespace gea::host {
|
|
22
|
+
namespace {
|
|
23
|
+
|
|
24
|
+
bool isDirectory(const std::string &path) {
|
|
25
|
+
struct stat info {};
|
|
26
|
+
return ::stat(path.c_str(), &info) == 0 && S_ISDIR(info.st_mode);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
bool makeOne(const std::string &path) {
|
|
30
|
+
#if defined(_WIN32)
|
|
31
|
+
return ::_mkdir(path.c_str()) == 0 || errno == EEXIST;
|
|
32
|
+
#else
|
|
33
|
+
return ::mkdir(path.c_str(), 0777) == 0 || errno == EEXIST;
|
|
34
|
+
#endif
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
bool makeAll(const std::string &path) {
|
|
38
|
+
if (path.empty()) return false;
|
|
39
|
+
for (std::size_t index = 1; index < path.size(); index++) {
|
|
40
|
+
if (path[index] == '/') makeOne(path.substr(0, index));
|
|
41
|
+
}
|
|
42
|
+
makeOne(path);
|
|
43
|
+
return isDirectory(path);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
bool seek(std::FILE *file, double offset) {
|
|
47
|
+
#if defined(_WIN32)
|
|
48
|
+
return _fseeki64(file, static_cast<long long>(offset), SEEK_SET) == 0;
|
|
49
|
+
#else
|
|
50
|
+
return ::fseeko(file, static_cast<off_t>(offset), SEEK_SET) == 0;
|
|
51
|
+
#endif
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
template <typename Action>
|
|
55
|
+
auto onStack(Action action) -> decltype(action()) {
|
|
56
|
+
decltype(action()) result{};
|
|
57
|
+
gea::platform::onInternalStack([&] { result = action(); });
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
} // namespace
|
|
62
|
+
|
|
63
|
+
std::string FilesFacade::dataDirectory() const {
|
|
64
|
+
static const std::string directory = [] {
|
|
65
|
+
std::string path = files_platform::dataDirectory();
|
|
66
|
+
if (!path.empty()) gea::platform::onInternalStack([&] { makeAll(path); });
|
|
67
|
+
return path;
|
|
68
|
+
}();
|
|
69
|
+
return directory;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
bool FilesFacade::makeDirectory(const std::string &path) const {
|
|
73
|
+
return onStack([&] { return makeAll(path); });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
bool FilesFacade::exists(const std::string &path) const {
|
|
77
|
+
return onStack([&] {
|
|
78
|
+
struct stat info {};
|
|
79
|
+
return !path.empty() && ::stat(path.c_str(), &info) == 0;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
double FilesFacade::size(const std::string &path) const {
|
|
84
|
+
return onStack([&] {
|
|
85
|
+
struct stat info {};
|
|
86
|
+
if (path.empty() || ::stat(path.c_str(), &info) != 0 || S_ISDIR(info.st_mode)) return -1.0;
|
|
87
|
+
return static_cast<double>(info.st_size);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
std::vector<std::uint8_t> FilesFacade::read(const std::string &path, double offset, double length) const {
|
|
92
|
+
return onStack([&] {
|
|
93
|
+
std::vector<std::uint8_t> bytes;
|
|
94
|
+
if (path.empty() || offset < 0 || length <= 0) return bytes;
|
|
95
|
+
std::FILE *file = std::fopen(path.c_str(), "rb");
|
|
96
|
+
if (!file) return bytes;
|
|
97
|
+
if (seek(file, offset)) {
|
|
98
|
+
bytes.resize(static_cast<std::size_t>(length));
|
|
99
|
+
bytes.resize(std::fread(bytes.data(), 1, bytes.size(), file));
|
|
100
|
+
}
|
|
101
|
+
std::fclose(file);
|
|
102
|
+
return bytes;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
bool FilesFacade::append(const std::string &path, const std::vector<std::uint8_t> &bytes) const {
|
|
107
|
+
return onStack([&] {
|
|
108
|
+
std::FILE *file = path.empty() ? nullptr : std::fopen(path.c_str(), "ab");
|
|
109
|
+
if (!file) return false;
|
|
110
|
+
const std::size_t written = bytes.empty() ? 0 : std::fwrite(bytes.data(), 1, bytes.size(), file);
|
|
111
|
+
const bool closed = std::fclose(file) == 0;
|
|
112
|
+
return closed && written == bytes.size();
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
bool FilesFacade::writeAt(const std::string &path, double offset, const std::vector<std::uint8_t> &bytes) const {
|
|
117
|
+
return onStack([&] {
|
|
118
|
+
if (path.empty() || offset < 0) return false;
|
|
119
|
+
std::FILE *file = std::fopen(path.c_str(), "r+b");
|
|
120
|
+
if (!file) file = std::fopen(path.c_str(), "w+b");
|
|
121
|
+
if (!file) return false;
|
|
122
|
+
bool ok = seek(file, offset);
|
|
123
|
+
if (ok && !bytes.empty()) ok = std::fwrite(bytes.data(), 1, bytes.size(), file) == bytes.size();
|
|
124
|
+
return std::fclose(file) == 0 && ok;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
bool FilesFacade::truncate(const std::string &path, double size) const {
|
|
129
|
+
return onStack([&] {
|
|
130
|
+
if (path.empty() || size < 0) return false;
|
|
131
|
+
#if defined(_WIN32)
|
|
132
|
+
std::FILE *file = std::fopen(path.c_str(), "r+b");
|
|
133
|
+
if (!file) return false;
|
|
134
|
+
const bool ok = _chsize_s(_fileno(file), static_cast<long long>(size)) == 0;
|
|
135
|
+
std::fclose(file);
|
|
136
|
+
return ok;
|
|
137
|
+
#else
|
|
138
|
+
return ::truncate(path.c_str(), static_cast<off_t>(size)) == 0;
|
|
139
|
+
#endif
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
bool FilesFacade::remove(const std::string &path) const {
|
|
144
|
+
return onStack([&] { return !path.empty() && std::remove(path.c_str()) == 0; });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
bool FilesFacade::rename(const std::string &from, const std::string &to) const {
|
|
148
|
+
return onStack([&] {
|
|
149
|
+
if (from.empty() || to.empty()) return false;
|
|
150
|
+
#if defined(_WIN32)
|
|
151
|
+
std::remove(to.c_str());
|
|
152
|
+
#endif
|
|
153
|
+
return std::rename(from.c_str(), to.c_str()) == 0;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
namespace files_platform {
|
|
158
|
+
|
|
159
|
+
__attribute__((weak)) std::string dataDirectory() {
|
|
160
|
+
#if defined(ESP_PLATFORM)
|
|
161
|
+
return "/sdcard";
|
|
162
|
+
#else
|
|
163
|
+
const char *xdg = std::getenv("XDG_DATA_HOME");
|
|
164
|
+
const char *home = std::getenv("HOME");
|
|
165
|
+
#if defined(_WIN32)
|
|
166
|
+
if (!home) home = std::getenv("APPDATA");
|
|
167
|
+
#endif
|
|
168
|
+
std::string base = xdg && *xdg ? std::string(xdg) : home ? std::string(home) + "/.local/share" : std::string(".");
|
|
169
|
+
#if defined(__linux__) || defined(__APPLE__)
|
|
170
|
+
std::string name = "gea-app";
|
|
171
|
+
#if defined(__linux__)
|
|
172
|
+
if (std::FILE *comm = std::fopen("/proc/self/comm", "r")) {
|
|
173
|
+
char buffer[64] = {};
|
|
174
|
+
if (std::fgets(buffer, sizeof buffer, comm)) {
|
|
175
|
+
name = buffer;
|
|
176
|
+
while (!name.empty() && (name.back() == '\n' || name.back() == '\r')) name.pop_back();
|
|
177
|
+
}
|
|
178
|
+
std::fclose(comm);
|
|
179
|
+
}
|
|
180
|
+
#endif
|
|
181
|
+
return base + "/" + name;
|
|
182
|
+
#else
|
|
183
|
+
return base + "/gea-app";
|
|
184
|
+
#endif
|
|
185
|
+
#endif
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
} // namespace files_platform
|
|
189
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Apple data directory for the `Files` host API (host/files.h): Application
|
|
3
|
+
// Support/<app name> on macOS, the app container's Application Support on iOS.
|
|
4
|
+
#if defined(__APPLE__)
|
|
5
|
+
|
|
6
|
+
#import <Foundation/Foundation.h>
|
|
7
|
+
|
|
8
|
+
#include "host/files.h"
|
|
9
|
+
|
|
10
|
+
namespace gea::host::files_platform {
|
|
11
|
+
|
|
12
|
+
std::string dataDirectory() {
|
|
13
|
+
NSURL *base = [NSFileManager.defaultManager URLsForDirectory:NSApplicationSupportDirectory
|
|
14
|
+
inDomains:NSUserDomainMask]
|
|
15
|
+
.firstObject;
|
|
16
|
+
if (!base) return std::string();
|
|
17
|
+
NSBundle *bundle = NSBundle.mainBundle;
|
|
18
|
+
NSString *name = [bundle objectForInfoDictionaryKey:@"CFBundleName"] ?: bundle.bundleIdentifier
|
|
19
|
+
?: NSProcessInfo.processInfo.processName;
|
|
20
|
+
return std::string([base.path stringByAppendingPathComponent:name].UTF8String ?: "");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
} // namespace gea::host::files_platform
|
|
24
|
+
|
|
25
|
+
#endif
|
package/host/image.cpp
CHANGED
|
@@ -133,6 +133,15 @@ bool isAbsoluteFilePath(const char *path) {
|
|
|
133
133
|
#endif
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
+
// Storage is ready for `path`: an SD-card path needs the card mounted; a path
|
|
137
|
+
// on any other volume (the flash apps volume, /nand, SPIFFS) is read as is, so
|
|
138
|
+
// a board with no card slot still shows images from its own storage.
|
|
139
|
+
bool storageReadyFor(const std::string &path) {
|
|
140
|
+
if (gea::platform::storage::ensureMounted())
|
|
141
|
+
return true;
|
|
142
|
+
return path.compare(0, 7, "/sdcard") != 0;
|
|
143
|
+
}
|
|
144
|
+
|
|
136
145
|
// mkdir -p of the parent directories in `path` (FAT/VFS); existing dirs are
|
|
137
146
|
// fine.
|
|
138
147
|
void makeParentDirs(const std::string &path) {
|
|
@@ -275,7 +284,7 @@ bool ImageService::writeFile(const std::string &path,
|
|
|
275
284
|
bool result = false;
|
|
276
285
|
gea::platform::onInternalStack([&] {
|
|
277
286
|
if (path.empty() || bytes.empty() ||
|
|
278
|
-
!
|
|
287
|
+
!storageReadyFor(path))
|
|
279
288
|
return;
|
|
280
289
|
makeParentDirs(path);
|
|
281
290
|
gea::platform::storage::FileOperations files;
|
|
@@ -287,7 +296,7 @@ bool ImageService::writeFile(const std::string &path,
|
|
|
287
296
|
bool ImageService::removeFile(const std::string &path) const {
|
|
288
297
|
bool result = false;
|
|
289
298
|
gea::platform::onInternalStack([&] {
|
|
290
|
-
if (!path.empty() &&
|
|
299
|
+
if (!path.empty() && storageReadyFor(path))
|
|
291
300
|
result = std::remove(path.c_str()) == 0;
|
|
292
301
|
});
|
|
293
302
|
return result;
|
|
@@ -297,7 +306,7 @@ std::vector<std::uint8_t>
|
|
|
297
306
|
ImageService::readFile(const std::string &path) const {
|
|
298
307
|
std::vector<std::uint8_t> result;
|
|
299
308
|
gea::platform::onInternalStack([&] {
|
|
300
|
-
if (!path.empty() &&
|
|
309
|
+
if (!path.empty() && storageReadyFor(path))
|
|
301
310
|
result = readWholeFile(path);
|
|
302
311
|
});
|
|
303
312
|
return result;
|
|
@@ -321,7 +330,7 @@ std::vector<std::uint8_t> ImageService::readFileRange(const std::string &path,
|
|
|
321
330
|
std::vector<std::uint8_t> out;
|
|
322
331
|
gea::platform::onInternalStack([&] {
|
|
323
332
|
if (path.empty() || offset < 0 || length <= 0 ||
|
|
324
|
-
!
|
|
333
|
+
!storageReadyFor(path))
|
|
325
334
|
return;
|
|
326
335
|
std::FILE *f = gea::platform::storage::openRead(path);
|
|
327
336
|
if (!f)
|
|
@@ -338,7 +347,7 @@ std::vector<std::uint8_t> ImageService::readFileRange(const std::string &path,
|
|
|
338
347
|
std::string ImageService::listFiles(const std::string &path) const {
|
|
339
348
|
std::string out;
|
|
340
349
|
gea::platform::onInternalStack([&] {
|
|
341
|
-
if (path.empty() || !
|
|
350
|
+
if (path.empty() || !storageReadyFor(path))
|
|
342
351
|
return;
|
|
343
352
|
#ifdef GEA_HOST_HAS_DIRENT
|
|
344
353
|
DIR *dir = opendir(path.c_str());
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The `MenuBar` host API (host/menu_bar.h). The menu bar itself is the
|
|
3
|
+
// platform's (host/menu_bar_apple.mm on macOS); these fallbacks make every
|
|
4
|
+
// other host a no-op.
|
|
5
|
+
#include "host/menu_bar.h"
|
|
6
|
+
|
|
7
|
+
namespace gea::host {
|
|
8
|
+
|
|
9
|
+
bool MenuBarFacade::supported() const { return menu_bar_platform::supported(); }
|
|
10
|
+
bool MenuBarFacade::install(const std::string &symbol) const { return menu_bar_platform::install(symbol); }
|
|
11
|
+
void MenuBarFacade::setSymbol(const std::string &symbol) const { menu_bar_platform::setSymbol(symbol); }
|
|
12
|
+
bool MenuBarFacade::shown() const { return menu_bar_platform::shown(); }
|
|
13
|
+
void MenuBarFacade::show() const { menu_bar_platform::show(); }
|
|
14
|
+
void MenuBarFacade::hide() const { menu_bar_platform::hide(); }
|
|
15
|
+
|
|
16
|
+
namespace menu_bar_platform {
|
|
17
|
+
|
|
18
|
+
__attribute__((weak)) bool supported() { return false; }
|
|
19
|
+
__attribute__((weak)) bool install(const std::string &) { return false; }
|
|
20
|
+
__attribute__((weak)) void setSymbol(const std::string &) {}
|
|
21
|
+
__attribute__((weak)) bool shown() { return true; }
|
|
22
|
+
__attribute__((weak)) void show() {}
|
|
23
|
+
__attribute__((weak)) void hide() {}
|
|
24
|
+
|
|
25
|
+
} // namespace menu_bar_platform
|
|
26
|
+
} // namespace gea::host
|