forked from ammarahm-ed/react-native-mmkv-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodedInputDataCrypt.cpp
280 lines (244 loc) · 9.73 KB
/
CodedInputDataCrypt.cpp
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
/*
* Tencent is pleased to support the open source community by making
* MMKV available.
*
* Copyright (C) 2020 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "CodedInputDataCrypt.h"
#include "MMKVLog.h"
#include "PBUtility.h"
#include <cassert>
#include <cerrno>
#include <cstring>
#include <stdexcept>
#ifdef MMKV_APPLE
# if __has_feature(objc_arc)
# error This file must be compiled with MRC. Use -fno-objc-arc flag.
# endif
#endif // MMKV_APPLE
#ifndef MMKV_DISABLE_CRYPT
using namespace std;
namespace mmkv {
CodedInputDataCrypt::CodedInputDataCrypt(const void *oData, size_t length, AESCrypt &crypt)
: m_ptr((uint8_t *) oData), m_size(length), m_position(0), m_decryptPosition(0), m_decrypter(crypt) {
m_decryptBufferSize = AES_KEY_LEN * 2;
m_decryptBufferPosition = static_cast<size_t>(crypt.m_number);
m_decryptBufferDiscardPosition = m_decryptBufferPosition;
m_decryptBufferDecryptLength = m_decryptBufferPosition;
m_decryptBuffer = (uint8_t *) malloc(m_decryptBufferSize);
if (!m_decryptBuffer) {
throw runtime_error(strerror(errno));
}
}
CodedInputDataCrypt::~CodedInputDataCrypt() {
if (m_decryptBuffer) {
free(m_decryptBuffer);
}
}
void CodedInputDataCrypt::seek(size_t addedSize) {
m_position += addedSize;
m_decryptPosition += addedSize;
if (m_position > m_size) {
throw out_of_range("OutOfSpace");
}
assert(m_position % AES_KEY_LEN == m_decrypter.m_number);
}
void CodedInputDataCrypt::consumeBytes(size_t length, bool discardPreData) {
if (discardPreData) {
m_decryptBufferDiscardPosition = m_decryptBufferPosition;
}
auto decryptedBytesLeft = m_decryptBufferDecryptLength - m_decryptBufferPosition;
if (decryptedBytesLeft >= length) {
return;
}
length -= decryptedBytesLeft;
// if there's some data left inside m_decrypter.m_vector, use them first
// it will be faster when always decrypt with (n * AES_KEY_LEN) bytes
if (m_decrypter.m_number != 0) {
auto alignDecrypter = AES_KEY_LEN - m_decrypter.m_number;
// make sure no data left inside m_decrypter.m_vector after decrypt
if (length < alignDecrypter) {
length = alignDecrypter;
} else {
length -= alignDecrypter;
length = ((length + AES_KEY_LEN - 1) / AES_KEY_LEN) * AES_KEY_LEN;
length += alignDecrypter;
}
} else {
length = ((length + AES_KEY_LEN - 1) / AES_KEY_LEN) * AES_KEY_LEN;
}
auto bytesLeftInSrc = m_size - m_decryptPosition;
length = min(bytesLeftInSrc, length);
auto bytesLeftInBuffer = m_decryptBufferSize - m_decryptBufferDecryptLength;
// try move some space
if (bytesLeftInBuffer < length && m_decryptBufferDiscardPosition > 0) {
auto posToMove = (m_decryptBufferDiscardPosition / AES_KEY_LEN) * AES_KEY_LEN;
if (posToMove) {
auto sizeToMove = m_decryptBufferDecryptLength - posToMove;
memmove(m_decryptBuffer, m_decryptBuffer + posToMove, sizeToMove);
m_decryptBufferPosition -= posToMove;
m_decryptBufferDecryptLength -= posToMove;
m_decryptBufferDiscardPosition = 0;
bytesLeftInBuffer = m_decryptBufferSize - m_decryptBufferDecryptLength;
}
}
// still no enough sapce, try realloc()
if (bytesLeftInBuffer < length) {
auto newSize = m_decryptBufferSize + length;
auto newBuffer = realloc(m_decryptBuffer, newSize);
if (!newBuffer) {
throw runtime_error(strerror(errno));
}
m_decryptBuffer = (uint8_t *) newBuffer;
m_decryptBufferSize = newSize;
}
m_decrypter.decrypt(m_ptr + m_decryptPosition, m_decryptBuffer + m_decryptBufferDecryptLength, length);
m_decryptPosition += length;
m_decryptBufferDecryptLength += length;
assert(m_decryptPosition == m_size || m_decrypter.m_number == 0);
}
void CodedInputDataCrypt::skipBytes(size_t length) {
m_position += length;
auto decryptedBytesLeft = m_decryptBufferDecryptLength - m_decryptBufferPosition;
if (decryptedBytesLeft >= length) {
m_decryptBufferPosition += length;
return;
}
length -= decryptedBytesLeft;
// if this happens, we need optimization like the alignDecrypter above
assert(m_decrypter.m_number == 0);
size_t alignSize = ((length + AES_KEY_LEN - 1) / AES_KEY_LEN) * AES_KEY_LEN;
auto bytesLeftInSrc = m_size - m_decryptPosition;
auto size = min(alignSize, bytesLeftInSrc);
decryptedBytesLeft = size - length;
for (size_t index = 0, round = size / AES_KEY_LEN; index < round; index++) {
m_decrypter.decrypt(m_ptr + m_decryptPosition, m_decryptBuffer, AES_KEY_LEN);
m_decryptPosition += AES_KEY_LEN;
size -= AES_KEY_LEN;
}
if (size) {
m_decrypter.decrypt(m_ptr + m_decryptPosition, m_decryptBuffer, size);
m_decryptPosition += size;
m_decryptBufferPosition = size - decryptedBytesLeft;
m_decryptBufferDecryptLength = size;
} else {
m_decryptBufferPosition = AES_KEY_LEN - decryptedBytesLeft;
m_decryptBufferDecryptLength = AES_KEY_LEN;
}
assert(m_decryptBufferPosition <= m_decryptBufferDecryptLength);
assert(m_decryptPosition - m_decryptBufferDecryptLength + m_decryptBufferPosition == m_position);
}
inline void CodedInputDataCrypt::statusBeforeDecrypt(size_t rollbackSize, AESCryptStatus &status) {
rollbackSize += m_decryptBufferDecryptLength - m_decryptBufferPosition;
m_decrypter.statusBeforeDecrypt(m_ptr + m_decryptPosition, m_decryptBuffer + m_decryptBufferDecryptLength,
rollbackSize, status);
}
int8_t CodedInputDataCrypt::readRawByte() {
assert(m_position <= m_decryptPosition);
if (m_position == m_size) {
auto msg = "reach end, m_position: " + to_string(m_position) + ", m_size: " + to_string(m_size);
throw out_of_range(msg);
}
m_position++;
assert(m_decryptBufferPosition < m_decryptBufferSize);
auto *bytes = (int8_t *) m_decryptBuffer;
return bytes[m_decryptBufferPosition++];
}
int32_t CodedInputDataCrypt::readRawVarint32(bool discardPreData) {
consumeBytes(10, discardPreData);
int8_t tmp = this->readRawByte();
if (tmp >= 0) {
return tmp;
}
int32_t result = tmp & 0x7f;
if ((tmp = this->readRawByte()) >= 0) {
result |= tmp << 7;
} else {
result |= (tmp & 0x7f) << 7;
if ((tmp = this->readRawByte()) >= 0) {
result |= tmp << 14;
} else {
result |= (tmp & 0x7f) << 14;
if ((tmp = this->readRawByte()) >= 0) {
result |= tmp << 21;
} else {
result |= (tmp & 0x7f) << 21;
result |= (tmp = this->readRawByte()) << 28;
if (tmp < 0) {
// discard upper 32 bits
for (int i = 0; i < 5; i++) {
if (this->readRawByte() >= 0) {
return result;
}
}
throw invalid_argument("InvalidProtocolBuffer malformed varint32");
}
}
}
}
return result;
}
int32_t CodedInputDataCrypt::readInt32() {
return this->readRawVarint32();
}
# ifndef MMKV_APPLE
string CodedInputDataCrypt::readString(KeyValueHolderCrypt &kvHolder) {
kvHolder.offset = static_cast<uint32_t>(m_position);
int32_t size = this->readRawVarint32(true);
if (size < 0) {
throw length_error("InvalidProtocolBuffer negativeSize");
}
auto s_size = static_cast<size_t>(size);
if (s_size <= m_size - m_position) {
consumeBytes(s_size);
kvHolder.keySize = static_cast<uint16_t>(s_size);
string result((char *) (m_decryptBuffer + m_decryptBufferPosition), s_size);
m_position += s_size;
m_decryptBufferPosition += s_size;
return result;
} else {
throw out_of_range("InvalidProtocolBuffer truncatedMessage");
}
}
# endif
void CodedInputDataCrypt::readData(KeyValueHolderCrypt &kvHolder) {
int32_t size = this->readRawVarint32();
if (size < 0) {
throw length_error("InvalidProtocolBuffer negativeSize");
}
auto s_size = static_cast<size_t>(size);
if (s_size <= m_size - m_position) {
if (KeyValueHolderCrypt::isValueStoredAsOffset(s_size)) {
kvHolder.type = KeyValueHolderType_Offset;
kvHolder.valueSize = static_cast<uint32_t>(s_size);
kvHolder.pbKeyValueSize =
static_cast<uint8_t>(pbRawVarint32Size(kvHolder.valueSize) + pbRawVarint32Size(kvHolder.keySize));
size_t rollbackSize = kvHolder.pbKeyValueSize + kvHolder.keySize;
statusBeforeDecrypt(rollbackSize, kvHolder.cryptStatus);
skipBytes(s_size);
} else {
consumeBytes(s_size);
kvHolder.type = KeyValueHolderType_Direct;
kvHolder = KeyValueHolderCrypt(m_decryptBuffer + m_decryptBufferPosition, s_size);
m_decryptBufferPosition += s_size;
m_position += s_size;
}
} else {
throw out_of_range("InvalidProtocolBuffer truncatedMessage");
}
}
} // namespace mmkv
#endif // MMKV_DISABLE_CRYPT