forked from diybitcoinhardware/f469-disco
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhashlib.c
357 lines (303 loc) · 14.2 KB
/
hashlib.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
#include <assert.h>
#include <string.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "py/builtin.h"
#include "crypto/sha2.h"
#include "crypto/ripemd160.h"
#include "crypto/pbkdf2.h"
#include "crypto/hmac.h"
typedef struct _mp_obj_hash_t {
mp_obj_base_t base;
char state[0];
} mp_obj_hash_t;
/****************************** SHA1 ******************************/
STATIC mp_obj_t hashlib_sha1_update(mp_obj_t self_in, mp_obj_t arg);
STATIC mp_obj_t hashlib_sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(SHA1_CTX));
o->base.type = type;
sha1_Init((SHA1_CTX*)o->state);
if (n_args == 1) {
hashlib_sha1_update(MP_OBJ_FROM_PTR(o), args[0]);
}
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t hashlib_sha1_copy(mp_obj_t self_in) {
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(SHA1_CTX));
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
o->base.type = self->base.type;
memcpy(o->state, self->state, sizeof(SHA1_CTX));
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t hashlib_sha1_update(mp_obj_t self_in, mp_obj_t arg) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
sha1_Update((SHA1_CTX*)self->state, bufinfo.buf, bufinfo.len);
return mp_const_none;
}
STATIC mp_obj_t hashlib_sha1_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, SHA1_DIGEST_LENGTH);
sha1_Final((SHA1_CTX*)self->state, (byte*)vstr.buf);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(hashlib_sha1_update_obj, hashlib_sha1_update);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(hashlib_sha1_digest_obj, hashlib_sha1_digest);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(hashlib_sha1_copy_obj, hashlib_sha1_copy);
STATIC const mp_rom_map_elem_t hashlib_sha1_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&hashlib_sha1_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&hashlib_sha1_digest_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_ROM_INT(SHA1_DIGEST_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_ROM_INT(SHA1_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&hashlib_sha1_copy_obj) },
};
STATIC MP_DEFINE_CONST_DICT(hashlib_sha1_locals_dict, hashlib_sha1_locals_dict_table);
STATIC const mp_obj_type_t hashlib_sha1_type = {
{ &mp_type_type },
.name = MP_QSTR_sha1,
.make_new = hashlib_sha1_make_new,
.locals_dict = (void*)&hashlib_sha1_locals_dict,
};
/****************************** SHA256 ******************************/
STATIC mp_obj_t hashlib_sha256_update(mp_obj_t self_in, mp_obj_t arg);
STATIC mp_obj_t hashlib_sha256_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(SHA256_CTX));
o->base.type = type;
sha256_Init((SHA256_CTX*)o->state);
if (n_args == 1) {
hashlib_sha256_update(MP_OBJ_FROM_PTR(o), args[0]);
}
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t hashlib_sha256_copy(mp_obj_t self_in) {
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(SHA256_CTX));
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
o->base.type = self->base.type;
memcpy(o->state, self->state, sizeof(SHA256_CTX));
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t hashlib_sha256_update(mp_obj_t self_in, mp_obj_t arg) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
sha256_Update((SHA256_CTX*)self->state, bufinfo.buf, bufinfo.len);
return mp_const_none;
}
STATIC mp_obj_t hashlib_sha256_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, SHA256_DIGEST_LENGTH);
sha256_Final((SHA256_CTX*)self->state, (byte*)vstr.buf);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(hashlib_sha256_update_obj, hashlib_sha256_update);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(hashlib_sha256_digest_obj, hashlib_sha256_digest);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(hashlib_sha256_copy_obj, hashlib_sha256_copy);
STATIC const mp_rom_map_elem_t hashlib_sha256_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&hashlib_sha256_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&hashlib_sha256_digest_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_ROM_INT(SHA256_DIGEST_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_ROM_INT(SHA256_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&hashlib_sha256_copy_obj) },
};
STATIC MP_DEFINE_CONST_DICT(hashlib_sha256_locals_dict, hashlib_sha256_locals_dict_table);
STATIC const mp_obj_type_t hashlib_sha256_type = {
{ &mp_type_type },
.name = MP_QSTR_sha256,
.make_new = hashlib_sha256_make_new,
.locals_dict = (void*)&hashlib_sha256_locals_dict,
};
/****************************** SHA512 ******************************/
STATIC mp_obj_t hashlib_sha512_update(mp_obj_t self_in, mp_obj_t arg);
STATIC mp_obj_t hashlib_sha512_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(SHA512_CTX));
o->base.type = type;
sha512_Init((SHA512_CTX*)o->state);
if (n_args == 1) {
hashlib_sha512_update(MP_OBJ_FROM_PTR(o), args[0]);
}
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t hashlib_sha512_copy(mp_obj_t self_in) {
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(SHA512_CTX));
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
o->base.type = self->base.type;
memcpy(o->state, self->state, sizeof(SHA512_CTX));
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t hashlib_sha512_update(mp_obj_t self_in, mp_obj_t arg) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
sha512_Update((SHA512_CTX*)self->state, bufinfo.buf, bufinfo.len);
return mp_const_none;
}
STATIC mp_obj_t hashlib_sha512_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, SHA512_DIGEST_LENGTH);
sha512_Final((SHA512_CTX*)self->state, (byte*)vstr.buf);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(hashlib_sha512_update_obj, hashlib_sha512_update);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(hashlib_sha512_digest_obj, hashlib_sha512_digest);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(hashlib_sha512_copy_obj, hashlib_sha512_copy);
STATIC const mp_rom_map_elem_t hashlib_sha512_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&hashlib_sha512_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&hashlib_sha512_digest_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_ROM_INT(SHA512_DIGEST_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_ROM_INT(SHA512_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&hashlib_sha512_copy_obj) },
};
STATIC MP_DEFINE_CONST_DICT(hashlib_sha512_locals_dict, hashlib_sha512_locals_dict_table);
STATIC const mp_obj_type_t hashlib_sha512_type = {
{ &mp_type_type },
.name = MP_QSTR_sha512,
.make_new = hashlib_sha512_make_new,
.locals_dict = (void*)&hashlib_sha512_locals_dict,
};
/****************************** RIPEMD160 ******************************/
STATIC mp_obj_t hashlib_ripemd160_update(mp_obj_t self_in, mp_obj_t arg);
STATIC mp_obj_t hashlib_ripemd160_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(RIPEMD160_CTX));
o->base.type = type;
ripemd160_Init((RIPEMD160_CTX*)o->state);
if (n_args == 1) {
hashlib_ripemd160_update(MP_OBJ_FROM_PTR(o), args[0]);
}
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t hashlib_ripemd160_copy(mp_obj_t self_in) {
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(RIPEMD160_CTX));
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
o->base.type = self->base.type;
memcpy(o->state, self->state, sizeof(RIPEMD160_CTX));
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t hashlib_ripemd160_update(mp_obj_t self_in, mp_obj_t arg) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
ripemd160_Update((RIPEMD160_CTX*)self->state, bufinfo.buf, bufinfo.len);
return mp_const_none;
}
STATIC mp_obj_t hashlib_ripemd160_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, RIPEMD160_DIGEST_LENGTH);
ripemd160_Final((RIPEMD160_CTX*)self->state, (byte*)vstr.buf);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(hashlib_ripemd160_update_obj, hashlib_ripemd160_update);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(hashlib_ripemd160_digest_obj, hashlib_ripemd160_digest);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(hashlib_ripemd160_copy_obj, hashlib_ripemd160_copy);
STATIC const mp_rom_map_elem_t hashlib_ripemd160_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&hashlib_ripemd160_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&hashlib_ripemd160_digest_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_ROM_INT(RIPEMD160_DIGEST_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_ROM_INT(RIPEMD160_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&hashlib_ripemd160_copy_obj) },
};
STATIC MP_DEFINE_CONST_DICT(hashlib_ripemd160_locals_dict, hashlib_ripemd160_locals_dict_table);
STATIC const mp_obj_type_t hashlib_ripemd160_type = {
{ &mp_type_type },
.name = MP_QSTR_ripemd160,
.make_new = hashlib_ripemd160_make_new,
.locals_dict = (void*)&hashlib_ripemd160_locals_dict,
};
/************************** pbkdf2_hmac **************************/
// hashlib.pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None)
STATIC mp_obj_t hashlib_pbkdf2_hmac(size_t n_args, const mp_obj_t *args) {
// hash_name
mp_buffer_info_t typebuf;
mp_get_buffer_raise(args[0], &typebuf, MP_BUFFER_READ);
// password
mp_buffer_info_t pwdbuf;
mp_get_buffer_raise(args[1], &pwdbuf, MP_BUFFER_READ);
// salt
mp_buffer_info_t saltbuf;
mp_get_buffer_raise(args[2], &saltbuf, MP_BUFFER_READ);
// number of iterations
mp_int_t iter = mp_obj_get_int(args[3]);
// only sha256 or sha512 are supported
if(strcmp(typebuf.buf, "sha256") == 0){
// output length (dklen) if available
mp_int_t l = SHA256_DIGEST_LENGTH;
if(n_args > 4){
l = mp_obj_get_int(args[4]);
}
vstr_t vstr;
vstr_init_len(&vstr, l);
pbkdf2_hmac_sha256(pwdbuf.buf, pwdbuf.len, saltbuf.buf, saltbuf.len, iter, (byte*)vstr.buf, l);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
if(strcmp(typebuf.buf, "sha512") == 0){
// output length (dklen) if available
mp_int_t l = SHA512_DIGEST_LENGTH;
if(n_args > 4){
l = mp_obj_get_int(args[4]);
}
vstr_t vstr;
vstr_init_len(&vstr, l);
pbkdf2_hmac_sha512(pwdbuf.buf, pwdbuf.len, saltbuf.buf, saltbuf.len, iter, (byte*)vstr.buf, l);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
mp_raise_ValueError("Unsupported hash type");
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(hashlib_pbkdf2_hmac_obj, 4, hashlib_pbkdf2_hmac);
/************************** hmac_sha512 **************************/
STATIC mp_obj_t hashlib_hmac_sha512(mp_uint_t n_args, const mp_obj_t *args){
//mp_obj_t key, mp_obj_t msg
mp_buffer_info_t keybuf;
mp_get_buffer_raise(args[0], &keybuf, MP_BUFFER_READ);
mp_buffer_info_t msgbuf;
mp_get_buffer_raise(args[1], &msgbuf, MP_BUFFER_READ);
vstr_t vstr;
vstr_init_len(&vstr, 64);
hmac_sha512_oneline(keybuf.buf, keybuf.len, msgbuf.buf, msgbuf.len, (byte*)vstr.buf);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(hashlib_hmac_sha512_obj, 2, hashlib_hmac_sha512);
STATIC mp_obj_t hashlib_new(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t typebuf;
mp_get_buffer_raise(args[0], &typebuf, MP_BUFFER_READ);
if(strcmp(typebuf.buf, "ripemd160") == 0){
return hashlib_ripemd160_make_new(&hashlib_ripemd160_type, n_args-1, 0, args+1);
}
if(strcmp(typebuf.buf, "sha256") == 0){
return hashlib_sha256_make_new(&hashlib_sha256_type, n_args-1, 0, args+1);
}
if(strcmp(typebuf.buf, "sha512") == 0){
return hashlib_sha512_make_new(&hashlib_sha512_type, n_args-1, 0, args+1);
}
if(strcmp(typebuf.buf, "sha1") == 0){
return hashlib_sha1_make_new(&hashlib_sha1_type, n_args-1, 0, args+1);
}
mp_raise_ValueError("Unsupported hash type");
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(hashlib_new_obj, 1, hashlib_new);
/****************************** MODULE ******************************/
STATIC const mp_rom_map_elem_t hashlib_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_hashlib) },
{ MP_ROM_QSTR(MP_QSTR_new), MP_ROM_PTR(&hashlib_new_obj) },
{ MP_ROM_QSTR(MP_QSTR_sha1), MP_ROM_PTR(&hashlib_sha1_type) },
{ MP_ROM_QSTR(MP_QSTR_sha256), MP_ROM_PTR(&hashlib_sha256_type) },
{ MP_ROM_QSTR(MP_QSTR_sha512), MP_ROM_PTR(&hashlib_sha512_type) },
{ MP_ROM_QSTR(MP_QSTR_ripemd160), MP_ROM_PTR(&hashlib_ripemd160_type) },
{ MP_ROM_QSTR(MP_QSTR_pbkdf2_hmac), MP_ROM_PTR(&hashlib_pbkdf2_hmac_obj) },
{ MP_ROM_QSTR(MP_QSTR_hmac_sha512), MP_ROM_PTR(&hashlib_hmac_sha512_obj) },
};
STATIC MP_DEFINE_CONST_DICT(hashlib_module_globals, hashlib_module_globals_table);
const mp_obj_module_t hashlib_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&hashlib_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_hashlib, hashlib_user_cmodule, MODULE_HASHLIB_ENABLED);