1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "py/builtin.h"
6 #include "py/mperrno.h"
7 #include "py/obj.h"
8 #include "py/runtime.h"
9 #include "ulog/ulog.h"
10
11 #define LOG_TAG "DRIVER_CRYPTO"
12
13 extern const mp_obj_type_t driver_crypto_type;
14
15 // this is the actual C-structure for our new object
16 typedef struct {
17 // base represents some basic information, like type
18 mp_obj_base_t Base;
19 // a member created by us
20 char *ModuleName;
21 } mp_crypto_obj_t;
22
crypto_obj_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)23 void crypto_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
24 {
25 LOGD(LOG_TAG, "entern %s;\n", __func__);
26 mp_crypto_obj_t *self = MP_OBJ_TO_PTR(self_in);
27 mp_printf(print, "ModuleName(%s)", self->ModuleName);
28 }
29
crypto_obj_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)30 STATIC mp_obj_t crypto_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
31 {
32 LOGD(LOG_TAG, "entern %s;\n", __func__);
33 mp_crypto_obj_t *driver_obj = m_new_obj(mp_crypto_obj_t);
34 if (!driver_obj) {
35 mp_raise_OSError(MP_EINVAL);
36 }
37
38 driver_obj->Base.type = &driver_crypto_type;
39 driver_obj->ModuleName = "crypto";
40
41 return MP_OBJ_FROM_PTR(driver_obj);
42 }
43
obj_open(size_t n_args,const mp_obj_t * args)44 STATIC mp_obj_t obj_open(size_t n_args, const mp_obj_t *args)
45 {
46 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
47 int ret = -1;
48 void *instance = NULL;
49 if (n_args < 1) {
50 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
51 return mp_const_none;
52 }
53 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
54 mp_crypto_obj_t *driver_obj = (mp_crypto_obj_t *)self;
55 if (driver_obj == NULL) {
56 LOGE(LOG_TAG, "driver_obj is NULL\n");
57 return mp_const_none;
58 }
59 LOGD(LOG_TAG, "%s:out\n", __func__);
60
61 return mp_const_none;
62 }
63 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(crypto_obj_open, 1, obj_open);
64
obj_close(size_t n_args,const mp_obj_t * args)65 STATIC mp_obj_t obj_close(size_t n_args, const mp_obj_t *args)
66 {
67 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
68 int ret = -1;
69 void *instance = NULL;
70 if (n_args < 1) {
71 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
72 return mp_const_none;
73 }
74 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
75 mp_crypto_obj_t *driver_obj = (mp_crypto_obj_t *)self;
76 if (driver_obj == NULL) {
77 LOGE(LOG_TAG, "driver_obj is NULL\n");
78 return mp_const_none;
79 }
80 LOGD(LOG_TAG, "%s:out\n", __func__);
81
82 return mp_const_none;
83 }
84 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(crypto_obj_close, 1, obj_close);
85
obj_md5(size_t n_args,const mp_obj_t * args)86 STATIC mp_obj_t obj_md5(size_t n_args, const mp_obj_t *args)
87 {
88 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
89 int ret = -1;
90 uint8_t result[16] = { 0 };
91 char md5[33] = { 0 };
92 int i = 0;
93 if (n_args < 2) {
94 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
95 return mp_const_none;
96 }
97 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
98 mp_crypto_obj_t *driver_obj = (mp_crypto_obj_t *)self;
99 if (driver_obj == NULL) {
100 LOGE(LOG_TAG, "driver_obj is NULL\n");
101 return mp_const_none;
102 }
103
104 mp_buffer_info_t src;
105 mp_get_buffer_raise(args[1], &src, MP_BUFFER_READ);
106 LOGD(LOG_TAG, "%s:src.buf = %p;src.len = %d;\n", __func__, src.buf, src.len);
107
108 // mbedtls_md5(src.buf, src.len, result);
109 for (i = 0; i < 16; ++i) {
110 // num2hex(result[i], &md5[2 * i]);
111 }
112 LOGD(LOG_TAG, "%s:out\n", __func__);
113
114 return mp_const_none;
115 }
116 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(crypto_obj_md5, 2, obj_md5);
117
obj_encrypt(size_t n_args,const mp_obj_t * args)118 STATIC mp_obj_t obj_encrypt(size_t n_args, const mp_obj_t *args)
119 {
120 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
121 int ret = -1;
122 void *instance = NULL;
123 if (n_args < 1) {
124 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
125 return mp_const_none;
126 }
127 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
128 mp_crypto_obj_t *driver_obj = (mp_crypto_obj_t *)self;
129 if (driver_obj == NULL) {
130 LOGE(LOG_TAG, "driver_obj is NULL\n");
131 return mp_const_none;
132 }
133 LOGD(LOG_TAG, "%s:out\n", __func__);
134
135 return mp_const_none;
136 }
137 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(crypto_obj_encrypt, 1, obj_encrypt);
138
obj_decrypt(size_t n_args,const mp_obj_t * args)139 STATIC mp_obj_t obj_decrypt(size_t n_args, const mp_obj_t *args)
140 {
141 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
142 int ret = -1;
143 void *instance = NULL;
144 if (n_args < 1) {
145 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
146 return mp_const_none;
147 }
148 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
149 mp_crypto_obj_t *driver_obj = (mp_crypto_obj_t *)self;
150 if (driver_obj == NULL) {
151 LOGE(LOG_TAG, "driver_obj is NULL\n");
152 return mp_const_none;
153 }
154 LOGD(LOG_TAG, "%s:out\n", __func__);
155
156 return mp_const_none;
157 }
158 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(crypto_obj_decrypt, 1, obj_decrypt);
159
160 STATIC const mp_rom_map_elem_t crypto_locals_dict_table[] = {
161 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_Crypto) },
162 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&crypto_obj_open) },
163 { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&crypto_obj_close) },
164 { MP_ROM_QSTR(MP_QSTR_md5), MP_ROM_PTR(&crypto_obj_md5) },
165 { MP_ROM_QSTR(MP_QSTR_encrypt), MP_ROM_PTR(&crypto_obj_encrypt) },
166 { MP_ROM_QSTR(MP_QSTR_decrypt), MP_ROM_PTR(&crypto_obj_decrypt) },
167 };
168
169 STATIC MP_DEFINE_CONST_DICT(crypto_locals_dict, crypto_locals_dict_table);
170
171 const mp_obj_type_t driver_crypto_type = {
172 .base = { &mp_type_type },
173 .name = MP_QSTR_Crypto,
174 .print = crypto_obj_print,
175 .make_new = crypto_obj_make_new,
176 .locals_dict = (mp_obj_dict_t *)&crypto_locals_dict,
177 };
178