-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathoperations.c
244 lines (203 loc) · 8.47 KB
/
operations.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
#include "operations.h"
#include "apdu.h"
#include "ui.h"
#include "to_string.h"
#include <stdint.h>
#include <string.h>
struct operation_group_header {
uint8_t magic_byte;
uint8_t hash[32];
} __attribute__((packed));
struct contract {
uint8_t originated;
union {
struct {
uint8_t curve_code;
uint8_t pkh[HASH_SIZE];
} implicit;
struct {
uint8_t pkh[HASH_SIZE];
uint8_t padding;
} originated;
} u;
} __attribute__((packed));
struct operation_header {
uint8_t tag;
struct contract contract;
} __attribute__((packed));
struct delegation_contents {
uint8_t curve_code;
uint8_t hash[HASH_SIZE];
} __attribute__((packed));
// Argument is to distinguish between different parse errors for debugging purposes only
__attribute__((noreturn))
static void parse_error(
#ifndef TEZOS_DEBUG
__attribute__((unused))
#endif
uint32_t lineno) {
#ifdef TEZOS_DEBUG
THROW(0x9000 + lineno);
#else
THROW(EXC_PARSE_ERROR);
#endif
}
#define PARSE_ERROR() parse_error(__LINE__)
static void advance_ix(size_t *ix, size_t length, size_t amount) {
if (*ix + amount > length) PARSE_ERROR();
*ix += amount;
}
static uint8_t next_byte(const void *data, size_t *ix, size_t length, uint32_t lineno) {
if (*ix == length) parse_error(lineno);
uint8_t res = ((const char *)data)[*ix];
(*ix)++;
return res;
}
#define NEXT_BYTE(data, ix, length) next_byte(data, ix, length, __LINE__)
static inline uint64_t parse_z(const void *data, size_t *ix, size_t length, uint32_t lineno) {
uint64_t acc = 0;
uint64_t shift = 0;
while (true) {
uint64_t byte = next_byte(data, ix, length, lineno);
acc |= (byte & 0x7F) << shift;
shift += 7;
if (!(byte & 0x80)) {
break;
}
}
return acc;
}
#define PARSE_Z(data, ix, length) parse_z(data, ix, length, __LINE__)
// This macro assumes:
// * Beginning of data: const void *data
// * Total length of data: size_t length
// * Current index of data: size_t ix
// Any function that uses these macros should have these as local variables
#define NEXT_TYPE(type) ({ \
const type *val = data + ix; \
advance_ix(&ix, length, sizeof(type)); \
val; \
})
static inline void compute_pkh(cx_curve_t curve, size_t path_length, uint32_t *bip32_path,
struct parsed_operation_group *out) {
struct key_pair *pair = generate_key_pair(curve, path_length, bip32_path);
os_memset(&pair->private_key, 0, sizeof(pair->private_key));
cx_ecfp_public_key_t *key = public_key_hash(out->signing.hash, curve, &pair->public_key);
memcpy(&out->public_key, key, sizeof(out->public_key));
out->signing.curve_code = curve_to_curve_code(curve);
out->signing.originated = 0;
}
static inline void parse_implicit(struct parsed_contract *out, uint8_t curve_code,
const uint8_t hash[HASH_SIZE]) {
out->originated = 0;
out->curve_code = curve_code;
memcpy(out->hash, hash, sizeof(out->hash));
}
static inline void parse_contract(struct parsed_contract *out, const struct contract *in) {
out->originated = in->originated;
if (out->originated == 0) { // implicit
out->curve_code = in->u.implicit.curve_code;
memcpy(out->hash, in->u.implicit.pkh, sizeof(out->hash));
} else { // originated
out->curve_code = TEZOS_NO_CURVE;
memcpy(out->hash, in->u.originated.pkh, sizeof(out->hash));
}
}
struct parsed_operation_group *parse_operations(const void *data, size_t length, cx_curve_t curve,
size_t path_length, uint32_t *bip32_path,
allowed_operation_set ops) {
static struct parsed_operation_group out;
check_null(data);
check_null(bip32_path);
memset(&out, 0, sizeof(out));
out.operation.tag = OPERATION_TAG_NONE;
compute_pkh(curve, path_length, bip32_path, &out); // sets up "signing" and "public_key" members
size_t ix = 0;
// Verify magic byte, ignore block hash
const struct operation_group_header *ogh = NEXT_TYPE(struct operation_group_header);
if (ogh->magic_byte != MAGIC_BYTE_UNSAFE_OP) PARSE_ERROR();
while (ix < length) {
const struct operation_header *hdr = NEXT_TYPE(struct operation_header);
out.total_fee += PARSE_Z(data, &ix, length); // fee
PARSE_Z(data, &ix, length); // counter
PARSE_Z(data, &ix, length); // gas limit
if (PARSE_Z(data, &ix, length) != 0) PARSE_ERROR(); // storage limit
enum operation_tag tag = hdr->tag;
if (!is_operation_allowed(ops, tag)) PARSE_ERROR();
if (tag == OPERATION_TAG_REVEAL) {
// Public key up next! Ensure it matches signing key.
// Ignore source :-) and do not parse it from hdr.
// We don't much care about reveals, they have very little in the way of bad security
// implications and any fees have already been accounted for
if (NEXT_BYTE(data, &ix, length) != out.signing.curve_code) PARSE_ERROR();
size_t klen = out.public_key.W_len;
advance_ix(&ix, length, klen);
if (memcmp(out.public_key.W, data + ix - klen, klen) != 0) PARSE_ERROR();
continue;
}
if (out.operation.tag != OPERATION_TAG_NONE) {
// We are only currently allowing one non-reveal operation
PARSE_ERROR();
}
// This is the one allowable non-reveal operation per set
out.operation.tag = (uint8_t)tag;
parse_contract(&out.operation.source, &hdr->contract);
if (out.operation.source.originated == 0) {
if (memcmp(&out.operation.source, &out.signing, sizeof(out.signing))) PARSE_ERROR();
}
// This should by default be blanked out
out.operation.delegate.curve_code = TEZOS_NO_CURVE;
out.operation.delegate.originated = 0;
switch (tag) {
case OPERATION_TAG_DELEGATION:
{
uint8_t delegate_present = NEXT_BYTE(data, &ix, length);
if (delegate_present) {
const struct delegation_contents *dlg = NEXT_TYPE(struct delegation_contents);
parse_implicit(&out.operation.destination, dlg->curve_code, dlg->hash);
} else {
// Encode "not present"
out.operation.destination.originated = 0;
out.operation.destination.curve_code = TEZOS_NO_CURVE;
}
}
break;
case OPERATION_TAG_ORIGINATION:
{
struct origination_header {
uint8_t curve_code;
uint8_t hash[HASH_SIZE];
} __attribute__((packed));
const struct origination_header *hdr = NEXT_TYPE(struct origination_header);
parse_implicit(&out.operation.destination, hdr->curve_code, hdr->hash);
out.operation.amount = PARSE_Z(data, &ix, length);
if (NEXT_BYTE(data, &ix, length) != 0) {
out.operation.flags |= ORIGINATION_FLAG_SPENDABLE;
}
if (NEXT_BYTE(data, &ix, length) != 0) {
out.operation.flags |= ORIGINATION_FLAG_DELEGATABLE;
}
if (NEXT_BYTE(data, &ix, length) != 0) {
// Has delegate
const struct delegation_contents *dlg = NEXT_TYPE(struct delegation_contents);
parse_implicit(&out.operation.delegate, dlg->curve_code, dlg->hash);
}
if (NEXT_BYTE(data, &ix, length) != 0) PARSE_ERROR(); // Has script
}
break;
case OPERATION_TAG_TRANSACTION:
{
out.operation.amount = PARSE_Z(data, &ix, length);
const struct contract *destination = NEXT_TYPE(struct contract);
parse_contract(&out.operation.destination, destination);
uint8_t params = NEXT_BYTE(data, &ix, length);
if (params) PARSE_ERROR(); // TODO: Support params
}
break;
default:
PARSE_ERROR();
}
}
if (out.operation.tag == OPERATION_TAG_NONE) PARSE_ERROR(); // Must have one non-reveal op
return &out; // Success!
}