This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
auth_demo.c
211 lines (185 loc) · 5.69 KB
/
auth_demo.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
// a demo to use "auth" lib
// script args: <21 bytes auth> <32 bytes code hash> <1 byte hash type> <1 byte
// entry category> see `CkbEntryType`
//
// witness lock: signature
#include "blake2b.h"
#include "blockchain.h"
#include "ckb_auth.h"
#include "ckb_consts.h"
#include "ckb_syscalls.h"
#define BLAKE2B_BLOCK_SIZE 32
#define TEMP_SIZE 32768
#define MAX_WITNESS_SIZE 32768
#define SCRIPT_SIZE 32768
#define ONE_BATCH_SIZE 32768
static int extract_witness_lock(uint8_t *witness, uint64_t len,
mol_seg_t *lock_bytes_seg) {
if (len < 20) {
return CKB_INVALID_DATA;
}
uint32_t lock_length = *((uint32_t *)(&witness[16]));
if (len < 20 + lock_length) {
return CKB_INVALID_DATA;
} else {
lock_bytes_seg->ptr = &witness[20];
lock_bytes_seg->size = lock_length;
}
return CKB_SUCCESS;
}
int load_and_hash_witness(blake2b_state *ctx, size_t start, size_t index,
size_t source, bool hash_length) {
uint8_t temp[ONE_BATCH_SIZE];
uint64_t len = ONE_BATCH_SIZE;
int ret = ckb_load_witness(temp, &len, start, index, source);
if (ret != CKB_SUCCESS) {
return ret;
}
if (hash_length) {
blake2b_update(ctx, (char *)&len, sizeof(uint64_t));
}
uint64_t offset = (len > ONE_BATCH_SIZE) ? ONE_BATCH_SIZE : len;
blake2b_update(ctx, temp, offset);
while (offset < len) {
uint64_t current_len = ONE_BATCH_SIZE;
ret = ckb_load_witness(temp, ¤t_len, start + offset, index, source);
if (ret != CKB_SUCCESS) {
return ret;
}
uint64_t current_read =
(current_len > ONE_BATCH_SIZE) ? ONE_BATCH_SIZE : current_len;
blake2b_update(ctx, temp, current_read);
offset += current_read;
}
return CKB_SUCCESS;
}
int generate_sighash_all(uint8_t *msg, size_t msg_len) {
int ret;
uint64_t len = 0;
unsigned char temp[MAX_WITNESS_SIZE];
uint64_t read_len = MAX_WITNESS_SIZE;
uint64_t witness_len = MAX_WITNESS_SIZE;
if (msg_len < BLAKE2B_BLOCK_SIZE) {
return CKB_INVALID_DATA;
}
/* Load witness of first input */
ret = ckb_load_witness(temp, &read_len, 0, 0, CKB_SOURCE_GROUP_INPUT);
if (ret != CKB_SUCCESS) {
return CKB_INVALID_DATA;
}
witness_len = read_len;
if (read_len > MAX_WITNESS_SIZE) {
read_len = MAX_WITNESS_SIZE;
}
/* load signature */
mol_seg_t lock_bytes_seg;
ret = extract_witness_lock(temp, read_len, &lock_bytes_seg);
if (ret != 0) {
return CKB_INVALID_DATA;
}
/* Load tx hash */
unsigned char tx_hash[BLAKE2B_BLOCK_SIZE];
len = BLAKE2B_BLOCK_SIZE;
ret = ckb_load_tx_hash(tx_hash, &len, 0);
if (ret != CKB_SUCCESS) {
return ret;
}
if (len != BLAKE2B_BLOCK_SIZE) {
return CKB_INVALID_DATA;
}
/* Prepare sign message */
blake2b_state blake2b_ctx;
blake2b_init(&blake2b_ctx, BLAKE2B_BLOCK_SIZE);
blake2b_update(&blake2b_ctx, tx_hash, BLAKE2B_BLOCK_SIZE);
/* Clear lock field to zero, then digest the first witness
* lock_bytes_seg.ptr actually points to the memory in temp buffer
* */
memset((void *)lock_bytes_seg.ptr, 0, lock_bytes_seg.size);
blake2b_update(&blake2b_ctx, (char *)&witness_len, sizeof(uint64_t));
blake2b_update(&blake2b_ctx, temp, read_len);
// remaining of first witness
if (read_len < witness_len) {
ret = load_and_hash_witness(&blake2b_ctx, read_len, 0,
CKB_SOURCE_GROUP_INPUT, false);
if (ret != CKB_SUCCESS) {
return CKB_INVALID_DATA;
}
}
// Digest same group witnesses
size_t i = 1;
while (1) {
ret =
load_and_hash_witness(&blake2b_ctx, 0, i, CKB_SOURCE_GROUP_INPUT, true);
if (ret == CKB_INDEX_OUT_OF_BOUND) {
break;
}
if (ret != CKB_SUCCESS) {
return CKB_INVALID_DATA;
}
i += 1;
}
// Digest witnesses that not covered by inputs
i = (size_t)ckb_calculate_inputs_len();
while (1) {
ret = load_and_hash_witness(&blake2b_ctx, 0, i, CKB_SOURCE_INPUT, true);
if (ret == CKB_INDEX_OUT_OF_BOUND) {
break;
}
if (ret != CKB_SUCCESS) {
return CKB_INVALID_DATA;
}
i += 1;
}
blake2b_final(&blake2b_ctx, msg, BLAKE2B_BLOCK_SIZE);
return 0;
}
int main() {
int ret;
uint64_t len = 0;
unsigned char temp[TEMP_SIZE];
unsigned char script[SCRIPT_SIZE];
len = SCRIPT_SIZE;
ret = ckb_load_script(script, &len, 0);
if (ret != CKB_SUCCESS) {
return CKB_INVALID_DATA;
}
if (len > SCRIPT_SIZE) {
return CKB_INVALID_DATA;
}
mol_seg_t script_seg;
script_seg.ptr = (uint8_t *)script;
script_seg.size = len;
if (MolReader_Script_verify(&script_seg, false) != MOL_OK) {
return CKB_INVALID_DATA;
}
mol_seg_t args_seg = MolReader_Script_get_args(&script_seg);
mol_seg_t args_bytes_seg = MolReader_Bytes_raw_bytes(&args_seg);
if (args_bytes_seg.size != (21 + 32 + 1 + 1)) {
return CKB_INVALID_DATA;
}
// Load the first witness, or the witness of the same index as the first input
// using current script.
uint64_t witness_len = MAX_WITNESS_SIZE;
ret = ckb_load_witness(temp, &witness_len, 0, 0, CKB_SOURCE_GROUP_INPUT);
if (ret != CKB_SUCCESS) {
return CKB_INVALID_DATA;
}
mol_seg_t lock_bytes_seg;
ret = extract_witness_lock(temp, witness_len, &lock_bytes_seg);
if (ret != 0) {
return CKB_INVALID_DATA;
}
uint8_t msg32[32];
ret = generate_sighash_all(msg32, 32);
if (ret != 0)
return CKB_INVALID_DATA;
CkbEntryType entry;
memcpy(entry.code_hash, args_bytes_seg.ptr + 21, 32);
entry.hash_type = *(args_bytes_seg.ptr + 21 + 32);
entry.entry_category = *(args_bytes_seg.ptr + 21 + 32 + 1);
CkbAuthType auth;
auth.algorithm_id = *args_bytes_seg.ptr;
memcpy(auth.content, args_bytes_seg.ptr + 1, 20);
return ckb_auth(&entry, &auth, lock_bytes_seg.ptr, lock_bytes_seg.size,
msg32);
}