@geastack/host 0.1.27 → 0.1.29
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 +23 -0
- package/audio_runtime.cpp +131 -29
- package/bluetooth.cpp +22 -0
- package/host/audio.cpp +132 -4
- 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/fetch.cpp +47 -2
- package/host/files.cpp +194 -0
- package/host/files_apple.mm +25 -0
- package/host/image.cpp +17 -6
- package/host/media.cpp +8 -7
- package/host/menu.cpp +49 -0
- package/host/menu_apple.mm +249 -0
- package/host/menu_bar.cpp +31 -0
- package/host/menu_bar_apple.mm +220 -0
- package/host/opus.cpp +31 -0
- package/host/opus_apple.mm +134 -0
- package/host/tile_loader.cpp +5 -3
- 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 +5 -0
- package/include/host/audio.h +12 -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.h +93 -0
- package/include/host/menu_bar.h +48 -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/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/fetch.cpp
CHANGED
|
@@ -728,8 +728,11 @@ gea::host::FetchResponse gea::host::downloadFile(const std::string &url, const s
|
|
|
728
728
|
|
|
729
729
|
#include <algorithm>
|
|
730
730
|
#include <cctype>
|
|
731
|
+
#include <cstdio>
|
|
731
732
|
#include <cstdlib>
|
|
732
733
|
#include <sstream>
|
|
734
|
+
|
|
735
|
+
#include "platform/file_cache.h"
|
|
733
736
|
#ifndef __EMSCRIPTEN__
|
|
734
737
|
#include <thread>
|
|
735
738
|
#endif
|
|
@@ -912,18 +915,60 @@ gea::host::FetchResponse gea::host::fetch(const std::string &url, const FetchReq
|
|
|
912
915
|
return gea::framework::host::FetchHost::fetch(url, init);
|
|
913
916
|
}
|
|
914
917
|
|
|
915
|
-
|
|
918
|
+
#ifdef __EMSCRIPTEN__
|
|
919
|
+
// The browser has no device files to stream.
|
|
916
920
|
gea::host::FetchResponse gea::host::uploadFile(const std::string &, const std::string &,
|
|
917
921
|
const std::string &, const std::string &,
|
|
918
922
|
const std::string &, const std::string &) {
|
|
919
923
|
return gea::host::FetchResponse{};
|
|
920
924
|
}
|
|
921
925
|
|
|
922
|
-
// Off-device: no streaming download to a device file path.
|
|
923
926
|
gea::host::FetchResponse gea::host::downloadFile(const std::string &, const std::string &,
|
|
924
927
|
const std::string &) {
|
|
925
928
|
return gea::host::FetchResponse{};
|
|
926
929
|
}
|
|
930
|
+
#else
|
|
931
|
+
// A desktop host has the memory to hold the file, so both go through the
|
|
932
|
+
// ordinary fetch backend; the path is resolved like every other file path.
|
|
933
|
+
gea::host::FetchResponse gea::host::uploadFile(const std::string &url, const std::string &authorization,
|
|
934
|
+
const std::string &contentType, const std::string &prefix,
|
|
935
|
+
const std::string &filePath, const std::string &suffix) {
|
|
936
|
+
gea::host::FetchResponse result{};
|
|
937
|
+
std::FILE *file = std::fopen(gea::platform::storage::hostPath(filePath).c_str(), "rb");
|
|
938
|
+
if (!file) {
|
|
939
|
+
result.status = -11; // same code as a board that cannot open the file
|
|
940
|
+
return result;
|
|
941
|
+
}
|
|
942
|
+
gea::host::FetchRequestInit init;
|
|
943
|
+
init.method = "POST";
|
|
944
|
+
init.headers["Accept"] = "*/*";
|
|
945
|
+
if (!authorization.empty()) init.headers["Authorization"] = authorization;
|
|
946
|
+
if (!contentType.empty()) init.headers["Content-Type"] = contentType;
|
|
947
|
+
init.body = prefix;
|
|
948
|
+
char chunk[64 * 1024];
|
|
949
|
+
std::size_t got = 0;
|
|
950
|
+
while ((got = std::fread(chunk, 1, sizeof(chunk), file)) > 0) init.body.append(chunk, got);
|
|
951
|
+
std::fclose(file);
|
|
952
|
+
init.body += suffix;
|
|
953
|
+
return gea::host::fetch(url, init);
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
gea::host::FetchResponse gea::host::downloadFile(const std::string &url, const std::string &authorization,
|
|
957
|
+
const std::string &destPath) {
|
|
958
|
+
gea::host::FetchRequestInit init;
|
|
959
|
+
init.headers["Accept"] = "*/*";
|
|
960
|
+
if (!authorization.empty()) init.headers["Authorization"] = authorization;
|
|
961
|
+
gea::host::FetchResponse result = gea::host::fetch(url, init);
|
|
962
|
+
if (!result.ok) return result;
|
|
963
|
+
gea::host::FetchResponse written = result;
|
|
964
|
+
written.body.clear();
|
|
965
|
+
std::FILE *file = std::fopen(gea::platform::storage::hostPath(destPath).c_str(), "wb");
|
|
966
|
+
const bool saved = file && std::fwrite(result.body.data(), 1, result.body.size(), file) == result.body.size();
|
|
967
|
+
if (file) std::fclose(file);
|
|
968
|
+
if (!saved) written.ok = false;
|
|
969
|
+
return written;
|
|
970
|
+
}
|
|
971
|
+
#endif
|
|
927
972
|
|
|
928
973
|
#endif // ESP_PLATFORM && network capability
|
|
929
974
|
|
package/host/files.cpp
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
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/file_cache.h"
|
|
20
|
+
#include "platform/internal_stack.h"
|
|
21
|
+
|
|
22
|
+
namespace gea::host {
|
|
23
|
+
namespace {
|
|
24
|
+
|
|
25
|
+
bool isDirectory(const std::string &path) {
|
|
26
|
+
struct stat info {};
|
|
27
|
+
return ::stat(path.c_str(), &info) == 0 && S_ISDIR(info.st_mode);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
bool makeOne(const std::string &path) {
|
|
31
|
+
#if defined(_WIN32)
|
|
32
|
+
return ::_mkdir(path.c_str()) == 0 || errno == EEXIST;
|
|
33
|
+
#else
|
|
34
|
+
return ::mkdir(path.c_str(), 0777) == 0 || errno == EEXIST;
|
|
35
|
+
#endif
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
bool makeAll(const std::string &path) {
|
|
39
|
+
if (path.empty()) return false;
|
|
40
|
+
for (std::size_t index = 1; index < path.size(); index++) {
|
|
41
|
+
if (path[index] == '/') makeOne(path.substr(0, index));
|
|
42
|
+
}
|
|
43
|
+
makeOne(path);
|
|
44
|
+
return isDirectory(path);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
bool seek(std::FILE *file, double offset) {
|
|
48
|
+
#if defined(_WIN32)
|
|
49
|
+
return _fseeki64(file, static_cast<long long>(offset), SEEK_SET) == 0;
|
|
50
|
+
#else
|
|
51
|
+
return ::fseeko(file, static_cast<off_t>(offset), SEEK_SET) == 0;
|
|
52
|
+
#endif
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Where `path` lives on this host (a desktop app's "/sdcard" is a folder of its own).
|
|
56
|
+
std::string at(const std::string &path) { return gea::platform::storage::hostPath(path); }
|
|
57
|
+
|
|
58
|
+
template <typename Action>
|
|
59
|
+
auto onStack(Action action) -> decltype(action()) {
|
|
60
|
+
decltype(action()) result{};
|
|
61
|
+
gea::platform::onInternalStack([&] { result = action(); });
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
} // namespace
|
|
66
|
+
|
|
67
|
+
std::string FilesFacade::dataDirectory() const {
|
|
68
|
+
static const std::string directory = [] {
|
|
69
|
+
std::string path = files_platform::dataDirectory();
|
|
70
|
+
if (!path.empty()) gea::platform::onInternalStack([&] { makeAll(path); });
|
|
71
|
+
return path;
|
|
72
|
+
}();
|
|
73
|
+
return directory;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
bool FilesFacade::makeDirectory(const std::string &path) const {
|
|
77
|
+
return onStack([&] { return makeAll(at(path)); });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
bool FilesFacade::exists(const std::string &path) const {
|
|
81
|
+
return onStack([&] {
|
|
82
|
+
struct stat info {};
|
|
83
|
+
return !path.empty() && ::stat(at(path).c_str(), &info) == 0;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
double FilesFacade::size(const std::string &path) const {
|
|
88
|
+
return onStack([&] {
|
|
89
|
+
struct stat info {};
|
|
90
|
+
if (path.empty() || ::stat(at(path).c_str(), &info) != 0 || S_ISDIR(info.st_mode)) return -1.0;
|
|
91
|
+
return static_cast<double>(info.st_size);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
std::vector<std::uint8_t> FilesFacade::read(const std::string &path, double offset, double length) const {
|
|
96
|
+
return onStack([&] {
|
|
97
|
+
std::vector<std::uint8_t> bytes;
|
|
98
|
+
if (path.empty() || offset < 0 || length <= 0) return bytes;
|
|
99
|
+
std::FILE *file = std::fopen(at(path).c_str(), "rb");
|
|
100
|
+
if (!file) return bytes;
|
|
101
|
+
if (seek(file, offset)) {
|
|
102
|
+
bytes.resize(static_cast<std::size_t>(length));
|
|
103
|
+
bytes.resize(std::fread(bytes.data(), 1, bytes.size(), file));
|
|
104
|
+
}
|
|
105
|
+
std::fclose(file);
|
|
106
|
+
return bytes;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
bool FilesFacade::append(const std::string &path, const std::vector<std::uint8_t> &bytes) const {
|
|
111
|
+
return onStack([&] {
|
|
112
|
+
std::FILE *file = path.empty() ? nullptr : std::fopen(at(path).c_str(), "ab");
|
|
113
|
+
if (!file) return false;
|
|
114
|
+
const std::size_t written = bytes.empty() ? 0 : std::fwrite(bytes.data(), 1, bytes.size(), file);
|
|
115
|
+
const bool closed = std::fclose(file) == 0;
|
|
116
|
+
return closed && written == bytes.size();
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
bool FilesFacade::writeAt(const std::string &path, double offset, const std::vector<std::uint8_t> &bytes) const {
|
|
121
|
+
return onStack([&] {
|
|
122
|
+
if (path.empty() || offset < 0) return false;
|
|
123
|
+
const std::string host = at(path);
|
|
124
|
+
std::FILE *file = std::fopen(host.c_str(), "r+b");
|
|
125
|
+
if (!file) file = std::fopen(host.c_str(), "w+b");
|
|
126
|
+
if (!file) return false;
|
|
127
|
+
bool ok = seek(file, offset);
|
|
128
|
+
if (ok && !bytes.empty()) ok = std::fwrite(bytes.data(), 1, bytes.size(), file) == bytes.size();
|
|
129
|
+
return std::fclose(file) == 0 && ok;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
bool FilesFacade::truncate(const std::string &path, double size) const {
|
|
134
|
+
return onStack([&] {
|
|
135
|
+
if (path.empty() || size < 0) return false;
|
|
136
|
+
#if defined(_WIN32)
|
|
137
|
+
std::FILE *file = std::fopen(at(path).c_str(), "r+b");
|
|
138
|
+
if (!file) return false;
|
|
139
|
+
const bool ok = _chsize_s(_fileno(file), static_cast<long long>(size)) == 0;
|
|
140
|
+
std::fclose(file);
|
|
141
|
+
return ok;
|
|
142
|
+
#else
|
|
143
|
+
return ::truncate(at(path).c_str(), static_cast<off_t>(size)) == 0;
|
|
144
|
+
#endif
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
bool FilesFacade::remove(const std::string &path) const {
|
|
149
|
+
return onStack([&] { return !path.empty() && std::remove(at(path).c_str()) == 0; });
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
bool FilesFacade::rename(const std::string &from, const std::string &to) const {
|
|
153
|
+
return onStack([&] {
|
|
154
|
+
if (from.empty() || to.empty()) return false;
|
|
155
|
+
#if defined(_WIN32)
|
|
156
|
+
std::remove(at(to).c_str());
|
|
157
|
+
#endif
|
|
158
|
+
return std::rename(at(from).c_str(), at(to).c_str()) == 0;
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
namespace files_platform {
|
|
163
|
+
|
|
164
|
+
__attribute__((weak)) std::string dataDirectory() {
|
|
165
|
+
#if defined(ESP_PLATFORM)
|
|
166
|
+
return "/sdcard";
|
|
167
|
+
#else
|
|
168
|
+
const char *xdg = std::getenv("XDG_DATA_HOME");
|
|
169
|
+
const char *home = std::getenv("HOME");
|
|
170
|
+
#if defined(_WIN32)
|
|
171
|
+
if (!home) home = std::getenv("APPDATA");
|
|
172
|
+
#endif
|
|
173
|
+
std::string base = xdg && *xdg ? std::string(xdg) : home ? std::string(home) + "/.local/share" : std::string(".");
|
|
174
|
+
#if defined(__linux__) || defined(__APPLE__)
|
|
175
|
+
std::string name = "gea-app";
|
|
176
|
+
#if defined(__linux__)
|
|
177
|
+
if (std::FILE *comm = std::fopen("/proc/self/comm", "r")) {
|
|
178
|
+
char buffer[64] = {};
|
|
179
|
+
if (std::fgets(buffer, sizeof buffer, comm)) {
|
|
180
|
+
name = buffer;
|
|
181
|
+
while (!name.empty() && (name.back() == '\n' || name.back() == '\r')) name.pop_back();
|
|
182
|
+
}
|
|
183
|
+
std::fclose(comm);
|
|
184
|
+
}
|
|
185
|
+
#endif
|
|
186
|
+
return base + "/" + name;
|
|
187
|
+
#else
|
|
188
|
+
return base + "/gea-app";
|
|
189
|
+
#endif
|
|
190
|
+
#endif
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
} // namespace files_platform
|
|
194
|
+
} // 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
|