-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathots.c
More file actions
411 lines (324 loc) · 8.84 KB
/
ots.c
File metadata and controls
411 lines (324 loc) · 8.84 KB
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
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <inttypes.h>
#include <limits.h>
#include "ots.h"
#include "util.h"
#include "decoder.h"
#define STR_DETAIL(x) #x
#define STR(x) STR_DETAIL(x)
typedef unsigned char u8;
#define SUPPORTED_VERSION 0x1
// 160-bit hash
#define MIN_FILE_DIGEST_LENGTH 20
// 256-bit hash
#define MAX_FILE_DIGEST_LENGTH 32
// maximum size of attestation payload
#define MAX_TMPBUF_SIZE MAX_PAYLOAD_SIZE
// ots
const u8 succinct_proof_magic[] = { 0x6f, 0x74, 0x73 };
const u8 ots_proof_magic[] = {
0x00, 0x4f, 0x70, 0x65, 0x6e, 0x54, 0x69, 0x6d,
0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x73, 0x00,
0x00, 0x50, 0x72, 0x6f, 0x6f, 0x66, 0x00, 0xbf,
0x89, 0xe2, 0xe8, 0x84, 0xe8, 0x92, 0x94
};
const u8 pending_attestation[ATTESTATION_TAG_SIZE] = {
0x83, 0xdf, 0xe3, 0x0d, 0x2e, 0xf9, 0x0c, 0x8e
};
const u8 bitcoin_block_header_attestation[ATTESTATION_TAG_SIZE] = {
0x05, 0x88, 0x96, 0x0d, 0x73, 0xd7, 0x19, 0x01
};
const u8 litecoin_block_header_attestation[ATTESTATION_TAG_SIZE] = {
0x06, 0x86, 0x9a, 0x0d, 0x73, 0xd7, 0x1b, 0x45
};
int is_filehash_op(struct token *tok)
{
return tok->type == TOK_OP &&
tok->data.op.class == OP_CLS_CRYPTO &&
tok->data.op.crypto.datalen != 0;
}
// NOTE: this is technically a varuint
static int consume_version(struct cursor *cursor, u8 *ver) {
int ok;
check_cursor(1);
*ver = *cursor->p;
ok = consume_matching_byte(cursor, SUPPORTED_VERSION);
if (!ok) {
cursor->state = DECODER_ERR_UNSUPPORTED_VERSION;
return 0;
}
return ok;
}
int parse_op_class(u8 type, enum op_class *class)
{
switch ((enum binary_op)type) {
case OP_APPEND:
case OP_PREPEND:
*class = OP_CLS_BINARY;
return 1;
}
switch ((enum unary_op)type) {
case OP_HEXLIFY:
case OP_REVERSE:
*class = OP_CLS_UNARY;
return 1;
}
switch ((enum crypto_op)type) {
case OP_KECCAK256:
case OP_SHA1:
case OP_SHA256:
case OP_RIPEMD160:
*class = OP_CLS_CRYPTO;
return 1;
}
return 0;
}
int ots_cryptodata_size(struct crypto *crypto) {
switch (crypto->op) {
case OP_RIPEMD160:
case OP_SHA1:
return 20;
case OP_SHA256:
case OP_KECCAK256:
return 32;
}
return 20;
}
int parse_crypto_op_payload(struct cursor *cursor, struct op *op)
{
const u8 *digest;
int digest_len;
op->class = OP_CLS_CRYPTO;
digest = cursor->p;
#define consume_hash(typ) \
digest_len = sizeof(op->crypto.cryptodata.typ); \
if (!consume_bytes(cursor, NULL, digest_len)) return 0; \
memcpy(op->crypto.cryptodata.typ, digest, digest_len); \
op->crypto.datalen = digest_len; \
return 1
switch (op->crypto.op) {
case OP_SHA1: consume_hash(sha1);
case OP_SHA256: consume_hash(sha256);
case OP_KECCAK256: consume_hash(keccak256);
case OP_RIPEMD160: consume_hash(ripemd160);
}
#undef consume_hash
// backtrack on failure
(cursor->p)--;
return 0;
}
int parse_crypto_op_body(struct cursor *cursor, u8 tag, struct op *op)
{
if (!parse_op_class(tag, &op->class)) {
decoder_errmsg = "could not parse op class";
cursor->state = DECODER_ERR_CORRUPT;
return 0;
}
if (op->class != OP_CLS_CRYPTO) {
decoder_errmsg = "tried to parse crypto op, but is not crypto op class";
cursor->state = DECODER_ERR_CORRUPT;
return 0;
}
op->crypto.op = tag;
return parse_crypto_op_payload(cursor, op);
}
static int parse_crypto_op(struct cursor *cursor, struct op *op)
{
u8 tag;
if (!consume_byte(cursor, &tag))
return 0;
return parse_crypto_op_body(cursor, tag, op);
}
static int parse_binary_op_payload(struct cursor *cursor, struct op *op) {
static const unsigned int min_len = 1;
op->class = OP_CLS_BINARY;
return consume_varbytes(cursor, MAX_RESULT_LENGTH, min_len,
&op->binary.data_len,
&op->binary.bindata);
}
int consume_op(struct cursor *cursor, u8 tag, struct op *op) {
if (!parse_op_class(tag, &op->class)) {
decoder_errmsg = "could not parse OP class";
cursor->state = DECODER_ERR_CORRUPT;
return 0;
}
switch (op->class) {
case OP_CLS_CRYPTO:
op->crypto.op = tag;
op->crypto.datalen = 0;
return 1;
/* return parse_crypto_op_payload(cursor, op); */
case OP_CLS_BINARY:
op->binary.op = tag;
return parse_binary_op_payload(cursor, op);
case OP_CLS_UNARY:
op->unary_op = tag;
return 1;
}
assert(!"unhandled op->class");
return 0;
}
const u8 *get_attestation_tag(enum attestation_type at, int *len)
{
switch(at) {
case ATTESTATION_BITCOIN_BLOCK_HEADER:
*len = sizeof(bitcoin_block_header_attestation);
return bitcoin_block_header_attestation;
case ATTESTATION_LITECOIN_BLOCK_HEADER:
*len = sizeof(litecoin_block_header_attestation);
return litecoin_block_header_attestation;
case ATTESTATION_PENDING:
*len = sizeof(pending_attestation);
return pending_attestation;
case ATTESTATION_UNKNOWN:
*len = 0;
return NULL;
}
*len = 0;
return NULL;
}
static enum attestation_type parse_attestation_type(const u8 *data) {
if (attestation_eq(data, bitcoin_block_header_attestation))
return ATTESTATION_BITCOIN_BLOCK_HEADER;
else if (attestation_eq(data, pending_attestation))
return ATTESTATION_PENDING;
else if (attestation_eq(data, litecoin_block_header_attestation))
return ATTESTATION_LITECOIN_BLOCK_HEADER;
return ATTESTATION_UNKNOWN;
}
int consume_attestation_body(struct cursor *cursor,
struct attestation *attestation,
enum attestation_type att_type)
{
int len;
consume_varint(cursor, MAX_PAYLOAD_SIZE, &len);
attestation->type = att_type;
attestation->data_len = len;
attestation->raw_data_len = len;
attestation->raw_data = cursor->p;
switch (attestation->type) {
case ATTESTATION_PENDING:
if (!consume_varbytes(cursor, MAX_PAYLOAD_SIZE-1, 1,
&attestation->data_len,
(u8**)&attestation->data))
return 0;
break;
case ATTESTATION_UNKNOWN:
attestation->data = cursor->p;
consume_bytes(cursor, NULL, len);
break;
case ATTESTATION_LITECOIN_BLOCK_HEADER:
case ATTESTATION_BITCOIN_BLOCK_HEADER:
if (!consume_varint(cursor, INT_MAX,
&attestation->height))
return 0;
attestation->data = (unsigned char*)&attestation->height;
break;
}
return 1;
}
static int consume_attestation(struct cursor *cursor, struct attestation *att)
{
const u8 *tag = cursor->p;
if (!consume_bytes(cursor, NULL, ATTESTATION_TAG_SIZE))
return 0;
enum attestation_type att_type = parse_attestation_type(tag);
return consume_attestation_body(cursor, att, att_type);
}
#define consume_tag(tag) \
if (!consume_byte(cursor, &tag)) return 0
static int consume_timestamp(struct cursor *cursor, struct token *token,
ots_token_cb *cb);
static int consume_tag_or_attestation(struct cursor *cursor, u8 tag,
struct token *token, ots_token_cb *cb) {
if (tag == 0x00) {
if (!consume_attestation(cursor, &token->data.attestation))
return 0;
token->type = TOK_ATTESTATION;
(*cb)(token);
}
else {
if (!consume_op(cursor, tag, &token->data.op))
return 0;
token->type = TOK_OP;
(*cb)(token);
if (!consume_timestamp(cursor, token, cb))
return 0;
/* debug("FFFFFFFFF\n"); */
}
return 1;
}
static int consume_timestamp(struct cursor *cursor, struct token *token,
ots_token_cb *cb)
{
u8 tag;
consume_tag(tag);
while (tag == 0xff) {
token->type = TOK_TIMESTAMP;
(*cb)(token);
consume_tag(tag);
if (!consume_tag_or_attestation(cursor, tag, token, cb))
return 0;
consume_tag(tag);
}
if (!consume_tag_or_attestation(cursor, tag, token, cb)) {
decoder_errmsg = "failed to consume final timestamp tag or attestation";
return 0;
}
return 1;
}
#undef consume_tag
static int consume_magic(struct cursor *cursor) {
check_cursor(sizeof(ots_proof_magic));
return consume_bytes(cursor, ots_proof_magic, sizeof(ots_proof_magic));
}
static int consume_header(struct cursor *cursor, ots_token_cb *cb,
void *user_data)
{
struct token tok = { .user_data = user_data };
int ok = 0;
u8 version;
ok = consume_magic(cursor);
if (!ok) return ok;
ok = consume_version(cursor, &version);
if (!ok) return ok;
tok.type = TOK_VERSION;
tok.data.version.number = version;
tok.data.version.has_filehash = true;
(*cb)(&tok);
return 1;
}
static int consume_crypto_op(struct cursor *cursor, struct token *token) {
if (!parse_crypto_op(cursor, &token->data.op)) {
cursor->state = DECODER_ERR_CORRUPT;
decoder_errmsg = "expected file hash";
return 0;
}
token->type = TOK_OP;
return 1;
}
enum decoder_state parse_ots_proof(const u8 *buf, int len, ots_token_cb *cb,
void *user_data)
{
struct token token = { .user_data = user_data };
struct cursor cursor_data;
struct cursor *cursor = &cursor_data;
init_cursor(cursor);
cursor->p = buf;
cursor->end = buf + len;
if (!consume_header(cursor, cb, user_data))
return cursor->state;
token.type = TOK_FILEHASH;
(*cb)(&token);
if (!consume_crypto_op(cursor, &token))
return cursor->state;
(*cb)(&token);
if (!consume_timestamp(cursor, &token, cb))
return cursor->state;
cursor->state = DECODER_PARSE_OK;
return cursor->state;
}