forked from LedgerHQ/app-monero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonero_io.c
415 lines (352 loc) · 12.1 KB
/
monero_io.c
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Copyright 2017 Cedric Mesnil <cslashm@gmail.com>, Ledger SAS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "os.h"
#include "cx.h"
#include "monero_types.h"
#include "monero_api.h"
#include "monero_vars.h"
#if defined(IODUMMYCRYPT)
#warning IODUMMYCRYPT activated
#endif
#if defined(IONOCRYPT)
#warning IONOCRYPT activated
#endif
/*
* io_buff: contains current message part
* io_off: offset in current message part
* io_length: length of current message part
*/
/* ----------------------------------------------------------------------- */
/* MISC */
/* ----------------------------------------------------------------------- */
void monero_io_set_offset(unsigned int offset) {
if (offset == IO_OFFSET_END) {
G_monero_vstate.io_offset = G_monero_vstate.io_length;
}
else if (offset == IO_OFFSET_MARK) {
G_monero_vstate.io_offset = G_monero_vstate.io_mark;
}
else if (offset < G_monero_vstate.io_length) {
G_monero_vstate.io_offset = offset;
}
else {
THROW(ERROR_IO_OFFSET);
return ;
}
}
void monero_io_mark() {
G_monero_vstate.io_mark = G_monero_vstate.io_offset;
}
void monero_io_inserted(unsigned int len) {
G_monero_vstate.io_offset += len;
G_monero_vstate.io_length += len;
}
void monero_io_discard(int clear) {
G_monero_vstate.io_length = 0;
G_monero_vstate.io_offset = 0;
G_monero_vstate.io_mark = 0;
if (clear) {
monero_io_clear();
}
}
void monero_io_clear() {
os_memset(G_monero_vstate.io_buffer, 0 , MONERO_IO_BUFFER_LENGTH);
}
/* ----------------------------------------------------------------------- */
/* INSERT data to be sent */
/* ----------------------------------------------------------------------- */
void monero_io_hole(unsigned int sz) {
if ((G_monero_vstate.io_length + sz) > MONERO_IO_BUFFER_LENGTH) {
THROW(ERROR_IO_FULL);
return ;
}
os_memmove(G_monero_vstate.io_buffer+G_monero_vstate.io_offset+sz,
G_monero_vstate.io_buffer+G_monero_vstate.io_offset,
G_monero_vstate.io_length-G_monero_vstate.io_offset);
G_monero_vstate.io_length += sz;
}
void monero_io_insert(unsigned char const *buff, unsigned int len) {
monero_io_hole(len);
os_memmove(G_monero_vstate.io_buffer+G_monero_vstate.io_offset, buff, len);
G_monero_vstate.io_offset += len;
}
void monero_io_insert_hmac_for(unsigned char* buffer, int len) {
unsigned char hmac[32];
cx_hmac_sha256(G_monero_vstate.hmac_key, 32, buffer, len, hmac, 32);
monero_io_insert(hmac,32);
}
void monero_io_insert_encrypt(unsigned char* buffer, int len) {
//for now, only 32bytes block are allowed
if (len != 32) {
THROW(SW_WRONG_DATA);
return ;
}
monero_io_hole(len);
#if defined(IODUMMYCRYPT)
for (int i = 0; i<len; i++) {
G_monero_vstate.io_buffer[G_monero_vstate.io_offset+i] = buffer[i] ^ 0x55;
}
#elif defined(IONOCRYPT)
os_memmove(G_monero_vstate.io_buffer+G_monero_vstate.io_offset, buffer, len);
#else
cx_aes(&G_monero_vstate.spk, CX_ENCRYPT|CX_CHAIN_CBC|CX_LAST|CX_PAD_NONE,
buffer, len,
G_monero_vstate.io_buffer+G_monero_vstate.io_offset, len);
#endif
G_monero_vstate.io_offset += len;
if (G_monero_vstate.tx_in_progress) {
monero_io_insert_hmac_for(G_monero_vstate.io_buffer+G_monero_vstate.io_offset-len, len);
}
}
void monero_io_insert_u32(unsigned int v32) {
monero_io_hole(4);
G_monero_vstate.io_buffer[G_monero_vstate.io_offset+0] = v32>>24;
G_monero_vstate.io_buffer[G_monero_vstate.io_offset+1] = v32>>16;
G_monero_vstate.io_buffer[G_monero_vstate.io_offset+2] = v32>>8;
G_monero_vstate.io_buffer[G_monero_vstate.io_offset+3] = v32>>0;
G_monero_vstate.io_offset += 4;
}
void monero_io_insert_u24(unsigned int v24) {
monero_io_hole(3);
G_monero_vstate.io_buffer[G_monero_vstate.io_offset+0] = v24>>16;
G_monero_vstate.io_buffer[G_monero_vstate.io_offset+1] = v24>>8;
G_monero_vstate.io_buffer[G_monero_vstate.io_offset+2] = v24>>0;
G_monero_vstate.io_offset += 3;
}
void monero_io_insert_u16(unsigned int v16) {
monero_io_hole(2);
G_monero_vstate.io_buffer[G_monero_vstate.io_offset+0] = v16>>8;
G_monero_vstate.io_buffer[G_monero_vstate.io_offset+1] = v16>>0;
G_monero_vstate.io_offset += 2;
}
void monero_io_insert_u8(unsigned int v8) {
monero_io_hole(1);
G_monero_vstate.io_buffer[G_monero_vstate.io_offset+0] = v8;
G_monero_vstate.io_offset += 1;
}
void monero_io_insert_t(unsigned int T) {
if (T &0xFF00) {
monero_io_insert_u16(T);
} else {
monero_io_insert_u8(T);
}
}
void monero_io_insert_tl(unsigned int T, unsigned int L) {
monero_io_insert_t(T);
if (L < 128) {
monero_io_insert_u8(L);
} else if (L < 256) {
monero_io_insert_u16(0x8100|L);
} else {
monero_io_insert_u8(0x82);
monero_io_insert_u16(L);
}
}
void monero_io_insert_tlv(unsigned int T, unsigned int L, unsigned char const *V) {
monero_io_insert_tl(T,L);
monero_io_insert(V, L);
}
/* ----------------------------------------------------------------------- */
/* FECTH data from received buffer */
/* ----------------------------------------------------------------------- */
void monero_io_assert_available(int sz) {
if ((G_monero_vstate.io_length-G_monero_vstate.io_offset) < sz) {
THROW(SW_WRONG_LENGTH + (sz&0xFF));
}
}
int monero_io_fetch(unsigned char* buffer, int len) {
monero_io_assert_available(len);
if (buffer) {
os_memmove(buffer, G_monero_vstate.io_buffer+G_monero_vstate.io_offset, len);
}
G_monero_vstate.io_offset += len;
return len;
}
static void monero_io_verify_hmac_for(const unsigned char* buffer, int len, unsigned char *expected_hmac) {
unsigned char hmac[32];
cx_hmac_sha256(G_monero_vstate.hmac_key, 32,
buffer, len,
hmac, 32);
if (os_memcmp(hmac, expected_hmac, 32)) {
THROW(SW_SECURITY_TRUSTED_INPUT);
}
}
int monero_io_fetch_decrypt(unsigned char* buffer, int len) {
//for now, only 32bytes block allowed
if (len != 32) {
THROW(SW_SECURE_MESSAGING_NOT_SUPPORTED);
return 0;
}
if (G_monero_vstate.tx_in_progress) {
monero_io_assert_available(len+32);
monero_io_verify_hmac_for(G_monero_vstate.io_buffer+G_monero_vstate.io_offset, len,
G_monero_vstate.io_buffer+G_monero_vstate.io_offset + len);
} else {
monero_io_assert_available(len);
}
if (buffer) {
#if defined(IODUMMYCRYPT)
for (int i = 0; i<len; i++) {
buffer[i] = G_monero_vstate.io_buffer[G_monero_vstate.io_offset+i] ^ 0x55;
}
#elif defined(IONOCRYPT)
os_memmove(buffer, G_monero_vstate.io_buffer+G_monero_vstate.io_offset, len);
#else //IOCRYPT
cx_aes(&G_monero_vstate.spk, CX_DECRYPT|CX_CHAIN_CBC|CX_LAST|CX_PAD_NONE,
G_monero_vstate.io_buffer+G_monero_vstate.io_offset, len,
buffer, len);
#endif
}
G_monero_vstate.io_offset += len;
if (G_monero_vstate.tx_in_progress) {
G_monero_vstate.io_offset += 32;
}
return len;
}
int monero_io_fetch_decrypt_key(unsigned char* buffer) {
unsigned char* k;
monero_io_assert_available(32);
k = G_monero_vstate.io_buffer+G_monero_vstate.io_offset;
//view?
if (os_memcmp(k, C_FAKE_SEC_VIEW_KEY, 32)==0) {
G_monero_vstate.io_offset += 32;
if (G_monero_vstate.tx_in_progress) {
monero_io_assert_available(32);
monero_io_verify_hmac_for(C_FAKE_SEC_VIEW_KEY, 32, G_monero_vstate.io_buffer+G_monero_vstate.io_offset);
G_monero_vstate.io_offset += 32;
}
os_memmove(buffer, G_monero_vstate.a,32);
return 32;
}
//spend?
else if (os_memcmp(k, C_FAKE_SEC_SPEND_KEY, 32)==0) {
G_monero_vstate.io_offset += 32;
if (G_monero_vstate.tx_in_progress) {
monero_io_assert_available(32);
monero_io_verify_hmac_for(C_FAKE_SEC_SPEND_KEY, 32, G_monero_vstate.io_buffer+G_monero_vstate.io_offset);
}
os_memmove(buffer, G_monero_vstate.b,32);
return 32;
}
//else
else {
return monero_io_fetch_decrypt(buffer, 32);
}
}
unsigned int monero_io_fetch_u32() {
unsigned int v32;
monero_io_assert_available(4);
v32 = ( (G_monero_vstate.io_buffer[G_monero_vstate.io_offset+0] << 24) |
(G_monero_vstate.io_buffer[G_monero_vstate.io_offset+1] << 16) |
(G_monero_vstate.io_buffer[G_monero_vstate.io_offset+2] << 8) |
(G_monero_vstate.io_buffer[G_monero_vstate.io_offset+3] << 0) );
G_monero_vstate.io_offset += 4;
return v32;
}
unsigned int monero_io_fetch_u24() {
unsigned int v24;
monero_io_assert_available(3);
v24 = ( (G_monero_vstate.io_buffer[G_monero_vstate.io_offset+0] << 16) |
(G_monero_vstate.io_buffer[G_monero_vstate.io_offset+1] << 8) |
(G_monero_vstate.io_buffer[G_monero_vstate.io_offset+2] << 0) );
G_monero_vstate.io_offset += 3;
return v24;
}
unsigned int monero_io_fetch_u16() {
unsigned int v16;
monero_io_assert_available(2);
v16 = ( (G_monero_vstate.io_buffer[G_monero_vstate.io_offset+0] << 8) |
(G_monero_vstate.io_buffer[G_monero_vstate.io_offset+1] << 0) );
G_monero_vstate.io_offset += 2;
return v16;
}
unsigned int monero_io_fetch_u8() {
unsigned int v8;
monero_io_assert_available(1);
v8 = G_monero_vstate.io_buffer[G_monero_vstate.io_offset] ;
G_monero_vstate.io_offset += 1;
return v8;
}
int monero_io_fetch_t(unsigned int *T) {
*T = monero_io_fetch_u8();
if ((*T & 0x1F) == 0x1F) {
*T = (*T << 8) | monero_io_fetch_u8();
}
return 0;
}
int monero_io_fetch_l(unsigned int *L) {
*L = monero_io_fetch_u8();
if ((*L & 0x80) != 0) {
*L &= 0x7F;
if (*L == 1) {
*L = monero_io_fetch_u8();
} else if (*L == 2) {
*L = monero_io_fetch_u16() ;
} else {
*L = -1;
}
}
return 0;
}
int monero_io_fetch_tl(unsigned int *T, unsigned int *L) {
monero_io_fetch_t(T);
monero_io_fetch_l(L);
return 0;
}
int monero_io_fetch_nv(unsigned char* buffer, int len) {
monero_io_assert_available(len);
monero_nvm_write(buffer, G_monero_vstate.io_buffer+G_monero_vstate.io_offset, len);
G_monero_vstate.io_offset += len;
return len;
}
/* ----------------------------------------------------------------------- */
/* REAL IO */
/* ----------------------------------------------------------------------- */
#define MAX_OUT MONERO_APDU_LENGTH
int monero_io_do(unsigned int io_flags) {
// if IO_ASYNCH_REPLY has been set,
// monero_io_exchange will return when IO_RETURN_AFTER_TX will set in ui
if (io_flags & IO_ASYNCH_REPLY) {
monero_io_exchange(CHANNEL_APDU | IO_ASYNCH_REPLY, 0);
}
//else send data now
else {
G_monero_vstate.io_offset = 0;
if(G_monero_vstate.io_length > MAX_OUT) {
THROW(SW_FILE_FULL);
return SW_FILE_FULL;
}
os_memmove(G_io_apdu_buffer, G_monero_vstate.io_buffer+G_monero_vstate.io_offset, G_monero_vstate.io_length);
if (io_flags & IO_RETURN_AFTER_TX) {
monero_io_exchange(CHANNEL_APDU |IO_RETURN_AFTER_TX, G_monero_vstate.io_length);
return 0;
} else {
monero_io_exchange(CHANNEL_APDU, G_monero_vstate.io_length);
}
}
//--- set up received data ---
G_monero_vstate.io_offset = 0;
G_monero_vstate.io_length = 0;
G_monero_vstate.io_protocol_version = G_io_apdu_buffer[0];
G_monero_vstate.io_ins = G_io_apdu_buffer[1];
G_monero_vstate.io_p1 = G_io_apdu_buffer[2];
G_monero_vstate.io_p2 = G_io_apdu_buffer[3];
G_monero_vstate.io_lc = 0;
G_monero_vstate.io_le = 0;
G_monero_vstate.io_lc = G_io_apdu_buffer[4];
os_memmove(G_monero_vstate.io_buffer, G_io_apdu_buffer+5, G_monero_vstate.io_lc);
G_monero_vstate.io_length = G_monero_vstate.io_lc;
return 0;
}