1 /*
2 * Copyright (c) 2020 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "cipher_module.h"
17 #include "cipher.h"
18 #include "log.h"
19 #include "securec.h"
20
21 namespace OHOS {
22 namespace ACELite {
Rsa(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)23 JSIValue CipherModule::Rsa(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
24 {
25 JSIValue undefValue = JSI::CreateUndefined();
26 if ((args == nullptr) || argsNum == 0 || JSI::ValueIsUndefined(args[0])) {
27 return undefValue;
28 }
29
30 JSIValue success = JSI::GetNamedProperty(args[0], CB_SUCCESS);
31 JSIValue fail = JSI::GetNamedProperty(args[0], CB_FAIL);
32 JSIValue complete = JSI::GetNamedProperty(args[0], CB_COMPLETE);
33 char *strAction = JSI::GetStringProperty(args[0], "action");
34 char *strKey = JSI::GetStringProperty(args[0], "key");
35 char *strText = JSI::GetStringProperty(args[0], "text");
36 char *strTransformation = JSI::GetStringProperty(args[0], "transformation");
37 JSIValue code = JSI::CreateNumber(ERR_CODE);
38 JSIValue data = JSI::CreateString("System error");
39 JSIValue argvFail[ARGC_TWO] = { data, code };
40 JSIValue result = JSI::CreateObject();
41 JSIValue argvSuccess[ARGC_ONE] = { result };
42 int ret = ERROR_CODE_GENERAL;
43 RsaData textIn = { 0 };
44 RsaData textOut = { 0 };
45 RsaKeyData rsaKey = { 0 };
46 if ((strKey == nullptr) || (strText == nullptr)) {
47 JSI::CallFunction(fail, thisVal, argvFail, ARGC_TWO);
48 goto RELEASE;
49 }
50 rsaKey.key = strKey;
51 rsaKey.keyLen = strlen(strKey);
52 rsaKey.trans = strTransformation;
53 rsaKey.action = strAction;
54 textIn.data = strText;
55 textIn.length = strlen(strText);
56
57 ret = RsaCrypt(&rsaKey, &textIn, &textOut);
58 if ((ret != ERROR_SUCCESS) || (textOut.length == 0)) {
59 HILOG_ERROR(HILOG_MODULE_ACE, "RsaCrypt failed.");
60 JSI::CallFunction(fail, thisVal, argvFail, ARGC_TWO);
61 goto RELEASE;
62 }
63 textOut.data = static_cast<char *>(ace_malloc(textOut.length));
64 if (textOut.data == nullptr) {
65 HILOG_ERROR(HILOG_MODULE_ACE, "ace malloc failed.");
66 JSI::CallFunction(fail, thisVal, argvFail, ARGC_TWO);
67 goto RELEASE;
68 }
69 ret = memset_s(textOut.data, textOut.length, 0, textOut.length);
70 if (ret) {
71 HILOG_ERROR(HILOG_MODULE_ACE, "memset failed.");
72 }
73 ret = RsaCrypt(&rsaKey, &textIn, &textOut);
74 if (ret != ERROR_SUCCESS) {
75 HILOG_ERROR(HILOG_MODULE_ACE, "RsaCrypt failed.");
76 JSI::CallFunction(fail, thisVal, argvFail, ARGC_TWO);
77 goto RELEASE;
78 }
79 JSI::SetStringProperty(result, "text", (char*)textOut.data);
80 JSI::CallFunction(success, thisVal, argvSuccess, ARGC_ONE);
81
82 RELEASE:
83 JSI::ReleaseString(strAction);
84 JSI::ReleaseString(strText);
85 ResetStrBuf(strKey);
86 JSI::ReleaseString(strKey);
87 JSI::ReleaseString(strTransformation);
88 JSI::CallFunction(complete, thisVal, nullptr, 0);
89 JSI::ReleaseValueList(success, fail, complete, result, code, data, ARGS_END);
90 if (textOut.data != nullptr) {
91 ace_free(textOut.data);
92 }
93
94 return undefValue;
95 }
96
Aes(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)97 JSIValue CipherModule::Aes(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
98 {
99 JSIValue undefValue = JSI::CreateUndefined();
100 if ((args == nullptr) || argsNum == 0 || JSI::ValueIsUndefined(args[0])) {
101 return undefValue;
102 }
103 JSIValue success = JSI::GetNamedProperty(args[0], CB_SUCCESS);
104 JSIValue fail = JSI::GetNamedProperty(args[0], CB_FAIL);
105 JSIValue complete = JSI::GetNamedProperty(args[0], CB_COMPLETE);
106 char *strAction = JSI::GetStringProperty(args[0], "action");
107 char *strText = JSI::GetStringProperty(args[0], "text");
108 char *strKey = JSI::GetStringProperty(args[0], "key");
109 char *strTransformation = JSI::GetStringProperty(args[0], "transformation");
110 char *strIv = JSI::GetStringProperty(args[0], "iv");
111 double ivOffset = JSI::GetNumberProperty(args[0], "ivOffset");
112 JSIValue jsIvLen = JSI::GetNamedProperty(args[0], "ivLen");
113 JSIValue result = JSI::CreateObject();
114 JSIValue code = JSI::CreateNumber(ERR_CODE);
115 JSIValue argvSuccess[ARGC_ONE] = { result };
116 JSIValue data = JSI::CreateString("System error");
117 JSIValue argvFail[ARGC_TWO] = { data, code };
118 AesCryptContext aes = {{NULL, NULL, 0, 0, 0}, CIPHER_AES_ECB, {NULL, NULL, 0, 0}};
119 AesIvMode iv = {NULL, NULL, 0, 0};
120 int ret;
121 double ivLen = -1;
122 if (JSI::ValueIsNumber(jsIvLen)) {
123 ivLen = JSI::ValueToNumber(jsIvLen);
124 if (ivLen < 0) {
125 HILOG_ERROR(HILOG_MODULE_ACE, "Ivlen:%lf is not natural number.", ivLen);
126 JSI::CallFunction(fail, thisVal, argvFail, ARGC_TWO);
127 goto RELEASE;
128 }
129 }
130 iv.ivBuf = strIv;
131 iv.ivLen = ivLen;
132 iv.ivOffset = ivOffset;
133 iv.transformation = strTransformation;
134 ret = InitAesCryptData(strAction, strText, strKey, &iv, &aes);
135 if (ret != ERROR_SUCCESS) {
136 HILOG_ERROR(HILOG_MODULE_ACE, "InitAesCryptData failed.");
137 JSI::CallFunction(fail, thisVal, argvFail, ARGC_TWO);
138 goto RELEASE;
139 }
140 ret = AesCrypt(&aes);
141 if (ret != ERROR_SUCCESS) {
142 HILOG_ERROR(HILOG_MODULE_ACE, "AesCrypt failed.");
143 JSI::CallFunction(fail, thisVal, argvFail, ARGC_TWO);
144 goto RELEASE;
145 }
146 JSI::SetStringProperty(result, "text", (char*)aes.data.text);
147 JSI::CallFunction(success, thisVal, argvSuccess, ARGC_ONE);
148
149 RELEASE:
150 JSI::ReleaseString(strAction);
151 JSI::ReleaseString(strText);
152 ResetStrBuf(strKey);
153 ResetStrBuf(strIv);
154 JSI::ReleaseString(strKey);
155 JSI::ReleaseString(strIv);
156 JSI::ReleaseString(strTransformation);
157 JSI::CallFunction(complete, thisVal, nullptr, 0);
158 JSI::ReleaseValueList(success, fail, complete, result, code, data, jsIvLen, ARGS_END);
159 DeinitAesCryptData(&aes);
160 return undefValue;
161 }
162
ResetStrBuf(char * strBuf)163 void CipherModule::ResetStrBuf(char *strBuf)
164 {
165 if (strBuf == nullptr) {
166 return;
167 }
168
169 int ret = memset_s(strBuf, strlen(strBuf), 0, strlen(strBuf));
170 if (ret != EOK) {
171 HILOG_ERROR(HILOG_MODULE_ACE, "memset failed.");
172 }
173 }
174 } // namespace ACELite
175 } // namespace OHOS
176