1 /*
2 * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "amp_config.h"
9 #include "amp_defines.h"
10 #include "amp_utils.h"
11 #include "be_inl.h"
12
13 #include "mbedtls/md5.h"
14
15 #define MOD_STR "CRYPTO"
16 #define DEBUG_HEX_DUMP(str, buff, len) hexdump(str, buff, len)
17 // #define DEBUG_HEX_DUMP(str, buff, len)
18
19 #if 0
20 static duk_ret_t native_crypto_encrypt(duk_context *ctx)
21 {
22 char *crypte_mode = NULL, *aes_type = NULL, *aes_padding = NULL;
23
24 int i = 0;
25 char *key = NULL;
26 char *iv = NULL;
27 unsigned char *input = NULL;
28 unsigned char *dest = NULL;
29 if (!duk_is_string(ctx, 0) || !duk_is_string(ctx, 1) || !duk_is_string(ctx, 2) || !duk_is_object(ctx, 3)){
30 amp_error(MOD_STR, "invalid parameter");;
31 goto done;
32 }
33 crypte_mode = (const char *)duk_get_string(ctx, 0);
34 aes_type = (const char *)duk_get_string(ctx, 1);
35 aes_padding = (const char *)duk_get_string(ctx, 2);
36
37 if (strcasecmp(crypte_mode, "aes") != 0) {
38 amp_error(MOD_STR, "not supprted crypte_mode");
39 goto done; /* now just support aes encryption */
40 }
41
42 if (strcasecmp(aes_type, "cbc") != 0) {
43 amp_warn(MOD_STR, "aes type only cbc supported");
44 goto done;
45 }
46
47 /* input.key */
48 memset(&options, 0, sizeof(udp_options_t));
49 duk_get_prop_string(ctx, 1, "key");
50 if (!duk_is_string(ctx, -1)) {
51 amp_warn(MOD_STR, "key not specify");
52 duk_pop(ctx);
53 goto out;
54 }
55 key = (const char *)duk_get_string(ctx, -1);
56 duk_pop(ctx);
57
58 /* input.iv */
59 duk_get_prop_string(ctx, 1, "iv");
60 if (!duk_is_string(ctx, -1)) {
61 amp_warn(MOD_STR, "iv not specify");
62 duk_pop(ctx);
63 goto out;
64 }
65 strncpy(options.ip, duk_get_string(ctx, -1), sizeof(options.ip) - 1);
66 duk_pop(ctx);
67
68 crypte_mode = (const char *)duk_get_string(ctx, 3);
69 amp_debug(MOD_STR, "native_crypto_encrypt");
70
71 if (!arg0 || !symbol_is_string(arg0) || !arg1 || !symbol_is_string(arg1) ||
72 !arg2 || !symbol_is_string(arg2) || !arg3 || !symbol_is_object(arg3)) {
73 goto done;
74 }
75 return 1;
76
77 done:
78 duk_push_string(ctx, "");
79 return 1;
80 }
81
82 #endif
83
native_crypto_decrypt(duk_context * ctx)84 static duk_ret_t native_crypto_decrypt(duk_context *ctx)
85 {
86 amp_debug(MOD_STR, "native_crypto_decrypt");
87 return 1;
88 }
89
native_crypto_md5(duk_context * ctx)90 static duk_ret_t native_crypto_md5(duk_context *ctx)
91 {
92 char *src = NULL, *data = NULL;
93 int src_len = 0;
94 char *dest;
95 amp_md5_context s_ctx;
96 uint8_t result[16] = {0};
97 char md5[33] = {0};
98 int i = 0;
99
100 // amp_debug(MOD_STR, "native_crypto_md5, type:%d %d", duk_get_type(ctx, 0), duk_get_length(ctx, 0));
101 if (!duk_is_object(ctx, 0)) {
102 amp_warn(MOD_STR, "invalid parameter");
103 goto done;
104 }
105
106 src_len = duk_get_length(ctx, 0);
107 src = (char *)aos_malloc(src_len);
108 if (!src) {
109 amp_error(MOD_STR, "allocate memory failed");
110 goto done;
111 }
112
113 for (i = 0; i < src_len; i++) {
114 duk_get_prop_index(ctx, 0, i);
115 src[i] = (char)duk_get_int(ctx, -1);
116 duk_pop(ctx);
117 }
118
119 // DEBUG_HEX_DUMP("content data:", src, src_len);
120 amp_md5(src, src_len, result);
121 // DEBUG_HEX_DUMP("result data:", result, 16);
122
123 for(i = 0; i < 16; ++i){
124 num2hex(result[i], &md5[2 * i]);
125 }
126 // amp_debug(MOD_STR, "md5: %s", md5);
127 duk_push_string(ctx, md5);
128 return 1;
129 done:
130 duk_push_string(ctx, "");
131 return 1;
132 }
133
134
module_crypto_register(void)135 void module_crypto_register(void)
136 {
137 duk_context *ctx = be_get_context();
138 duk_push_object(ctx);
139
140 AMP_ADD_FUNCTION("md5", native_crypto_md5, 1);
141 // AMP_ADD_FUNCTION("encrypt", native_crypto_encrypt, 1);
142 // AMP_ADD_FUNCTION("decrypt", native_crypto_decrypt, 1);
143
144 duk_put_prop_string(ctx, -2, "crypto");
145 }
146