forked from diybitcoinhardware/f469-disco
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuhmac.c
148 lines (123 loc) · 5.51 KB
/
uhmac.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
#include <assert.h>
#include <string.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "py/builtin.h"
#include "crypto/sha2.h"
#include "crypto/hmac.h"
#define DIGEST_HMAC_SHA256 sizeof(HMAC_SHA256_CTX)
#define DIGEST_HMAC_SHA512 sizeof(HMAC_SHA512_CTX)
typedef struct _mp_obj_hmac_t {
mp_obj_base_t base;
size_t digestmod;
char state[0];
} mp_obj_hmac_t;
/****************************** HMAC ******************************/
STATIC mp_obj_t hmac_HMAC_update(mp_obj_t self_in, mp_obj_t arg);
STATIC mp_obj_t hmac_HMAC_make_new_helper(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kwargs){
mp_arg_check_num(n_args, 0, 0, 3, true);
enum { ARG_key, ARG_message, ARG_digestmod };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_key, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_msg, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_digestmod, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kwargs, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// digestmode
mp_buffer_info_t digestmodbuf;
mp_get_buffer_raise(args[ARG_digestmod].u_obj, &digestmodbuf, MP_BUFFER_READ);
size_t digestmod = 0;
// only sha256 or sha512 are supported
if(strcmp(digestmodbuf.buf, "sha256") == 0){
digestmod = DIGEST_HMAC_SHA256;
}else if(strcmp(digestmodbuf.buf, "sha512") == 0){
digestmod = DIGEST_HMAC_SHA512;
}else{
mp_raise_ValueError("Unsupported hash type");
return mp_const_none;
}
mp_obj_hmac_t *o = m_new_obj_var(mp_obj_hmac_t, char, digestmod+sizeof(size_t));
o->digestmod = digestmod;
o->base.type = type;
mp_buffer_info_t key;
mp_get_buffer_raise(args[ARG_key].u_obj, &key, MP_BUFFER_READ);
if(digestmod == DIGEST_HMAC_SHA256){
hmac_sha256_Init((HMAC_SHA256_CTX*)o->state, key.buf, key.len);
}else if(digestmod == DIGEST_HMAC_SHA512){
hmac_sha512_Init((HMAC_SHA512_CTX*)o->state, key.buf, key.len);
}
if (args[ARG_message].u_obj != MP_ROM_NONE) {
hmac_HMAC_update(MP_OBJ_FROM_PTR(o), args[ARG_message].u_obj);
}
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t hmac_HMAC_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, MP_OBJ_FUN_ARGS_MAX, true);
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
return hmac_HMAC_make_new_helper(type, n_args, args, &kw_args);
}
STATIC mp_obj_t hmac_HMAC_copy(mp_obj_t self_in) {
mp_obj_hmac_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_hmac_t *o = m_new_obj_var(mp_obj_hmac_t, char, self->digestmod+sizeof(size_t));
o->base.type = self->base.type;
o->digestmod = self->digestmod;
memcpy(o->state, self->state, self->digestmod);
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t hmac_HMAC_update(mp_obj_t self_in, mp_obj_t arg) {
mp_obj_hmac_t *self = MP_OBJ_TO_PTR(self_in);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
if(self->digestmod == DIGEST_HMAC_SHA256){
hmac_sha256_Update((HMAC_SHA256_CTX*)self->state, bufinfo.buf, bufinfo.len);
}else if(self->digestmod == DIGEST_HMAC_SHA512){
hmac_sha512_Update((HMAC_SHA512_CTX*)self->state, bufinfo.buf, bufinfo.len);
}
return mp_const_none;
}
STATIC mp_obj_t hmac_HMAC_digest(mp_obj_t self_in) {
mp_obj_hmac_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
if(self->digestmod == DIGEST_HMAC_SHA256){
vstr_init_len(&vstr, SHA256_DIGEST_LENGTH);
hmac_sha256_Final((HMAC_SHA256_CTX*)self->state, (byte*)vstr.buf);
}else if(self->digestmod == DIGEST_HMAC_SHA512){
vstr_init_len(&vstr, SHA512_DIGEST_LENGTH);
hmac_sha512_Final((HMAC_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(hmac_HMAC_update_obj, hmac_HMAC_update);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(hmac_HMAC_digest_obj, hmac_HMAC_digest);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(hmac_HMAC_copy_obj, hmac_HMAC_copy);
STATIC const mp_rom_map_elem_t hmac_HMAC_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&hmac_HMAC_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&hmac_HMAC_digest_obj) },
{ MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&hmac_HMAC_copy_obj) },
};
STATIC MP_DEFINE_CONST_DICT(hmac_HMAC_locals_dict, hmac_HMAC_locals_dict_table);
STATIC const mp_obj_type_t hmac_HMAC_type = {
{ &mp_type_type },
.name = MP_QSTR_HMAC,
.make_new = hmac_HMAC_make_new,
.locals_dict = (void*)&hmac_HMAC_locals_dict,
};
// key, msg=None, digestmod
STATIC mp_obj_t hmac_new(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
return hmac_HMAC_make_new_helper(&hmac_HMAC_type, n_args, args, kwargs);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(hmac_new_obj, 0, hmac_new);
/****************************** MODULE ******************************/
STATIC const mp_rom_map_elem_t hmac_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_hmac) },
{ MP_ROM_QSTR(MP_QSTR_new), MP_ROM_PTR(&hmac_new_obj) },
{ MP_ROM_QSTR(MP_QSTR_HMAC), MP_ROM_PTR(&hmac_new_obj) },
};
STATIC MP_DEFINE_CONST_DICT(hmac_module_globals, hmac_module_globals_table);
const mp_obj_module_t hmac_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&hmac_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_hmac, hmac_user_cmodule, MODULE_HASHLIB_ENABLED);