Skip to content

Commit 92813b8

Browse files
committed
v1.2
slimmed down memory usage to 1*INPUT from 3*INPUT (add) and 1*INPUT+1*ENTRY (get)
1 parent 48627fe commit 92813b8

2 files changed

Lines changed: 41 additions & 51 deletions

File tree

blobpak.c

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define ALIGN16(v) ((v + 0xf) & 0xfffffff0)
2424

2525
// """""random""""" 64 bits
26+
// requires a set rng seed (srand(seed))
2627
uint64_t getNoise(void) {
2728
uint8_t noise[8];
2829
uint8_t c_time[8];
@@ -82,15 +83,13 @@ uint32_t findEntryDataSize(uint32_t encryptedEntryDataSize, char* entryName, uin
8283
}
8384

8485
// create an entry
85-
uint32_t createEntry(uint32_t entryDataSize, char* password, char* entryName, void* inData, void* outData) {
86-
srand(time(NULL));
86+
uint32_t createEntry(uint32_t entryDataSize, char* password, char* entryName, void* entryStart, void** newEntryStart) {
87+
srand(time(NULL)); // TODO: seed as uarg?
8788
int randomBlockSize = (rand() % RANDOM_BLOCK_MAX_SIZE) + 1;
8889
uint32_t entrySize = ALIGN16(entryDataSize) + sizeof(entry_t) + randomBlockSize;
89-
void* entry = malloc(entrySize);
90-
if (!entry)
91-
return -1;
90+
void* entry = entryStart + (RANDOM_BLOCK_MAX_SIZE - randomBlockSize);
9291

93-
// add a random data block of random size <= 0x600 before the actual entry
92+
// add a random data block of random size <= RANDOM_BLOCK_MAX_SIZE before the actual entry
9493
uint8_t randomBlockKey[16], randomBlockIV[16];
9594
*(uint64_t*)randomBlockKey = getNoise();
9695
*(uint64_t*)(randomBlockKey + 8) = getNoise();
@@ -108,20 +107,18 @@ uint32_t createEntry(uint32_t entryDataSize, char* password, char* entryName, vo
108107
// add the decrypted data and encrypt it there
109108
uint8_t dataKey[16], dataIV[16];
110109
void* entryData = entry + randomBlockSize + sizeof(entry_t);
111-
memset(entryData, 0, entryDataSize);
112-
memcpy(entryData, inData, entryDataSize);
113110
calculateDataKey(password, dataKey, dataIV, entryHead);
114111
aes_cbc(dataKey, dataIV, entryData, ALIGN16(entryDataSize), 1);
115-
memcpy(outData, entry, entrySize);
116-
112+
117113
// cleanup
118-
memset(entry, 0xFF, entrySize);
119-
free(entry);
120114
memset(randomBlockKey, 0xFF, 16);
121115
memset(randomBlockIV, 0xFF, 16);
122116
memset(dataKey, 0xFF, 16);
123117
memset(dataIV, 0xFF, 16);
124118

119+
// ret
120+
*newEntryStart = entry;
121+
125122
return entrySize;
126123
}
127124

@@ -130,68 +127,68 @@ int addEntry(char* name, char* password, char* pak, int iput, int ouput) {
130127
FILE* fp = NULL;
131128
uint32_t fileSize = 0;
132129
void* fileData = NULL;
130+
131+
void* entry = malloc(RANDOM_BLOCK_MAX_SIZE + sizeof(entry_t));
132+
if (!entry)
133+
return -2;
134+
133135
if (iput == IPUT_FILE) { // read the data from a file
134136
fp = fopen(name, "rb");
135137
if (!fp)
136138
return -1;
137139
fseek(fp, 0L, SEEK_END);
138140
fileSize = ftell(fp);
139-
fileData = malloc(fileSize);
140-
if (!fileData) {
141+
entry = realloc(entry, ALIGN16(fileSize) + RANDOM_BLOCK_MAX_SIZE + sizeof(entry_t));
142+
if (!entry) {
141143
fclose(fp);
142144
return -2;
143145
}
146+
fileData = entry + RANDOM_BLOCK_MAX_SIZE + sizeof(entry_t);
144147
memset(fileData, 0, fileSize);
145148
fseek(fp, 0L, SEEK_SET);
146149
fread(fileData, fileSize, 1, fp);
147150
fclose(fp);
148151
} else { // read the data from stdin
149152
void* tmp_p = NULL;
150153
uint32_t tmp_l = 0;
151-
fileData = malloc(STDIN_BUF_INCR);
152-
if (!fileData)
154+
entry = realloc(entry, STDIN_BUF_INCR + RANDOM_BLOCK_MAX_SIZE + sizeof(entry_t));
155+
if (!entry)
153156
return -2;
154-
memset(fileData, 0, STDIN_BUF_INCR);
157+
fileData = entry + RANDOM_BLOCK_MAX_SIZE + sizeof(entry_t);
158+
// memset(fileData, 0, STDIN_BUF_INCR); // dont memset, helps the random block
155159
while (tmp_l = read(fileno(stdin), fileData + fileSize, STDIN_BUF_INCR), tmp_l == STDIN_BUF_INCR) {
156160
fileSize += STDIN_BUF_INCR;
157-
tmp_p = realloc(fileData, fileSize + STDIN_BUF_INCR);
161+
tmp_p = realloc(entry, fileSize + STDIN_BUF_INCR + RANDOM_BLOCK_MAX_SIZE + sizeof(entry_t));
158162
if (!tmp_p) {
159163
memset(fileData, 0xFF, fileSize);
160164
return -2;
161165
}
162-
fileData = tmp_p;
163-
memset(fileData + fileSize, 0, STDIN_BUF_INCR);
166+
entry = tmp_p;
167+
fileData = entry + RANDOM_BLOCK_MAX_SIZE + sizeof(entry_t);
168+
// memset(fileData + fileSize, 0, STDIN_BUF_INCR); // dont memset, helps the random block
164169
}
165170
fileSize += tmp_l;
166171
if (!fileSize) {
167-
free(fileData);
172+
free(entry);
168173
return -2;
169174
}
170175
}
171176

172-
// prep entry mem
173-
void* entry = malloc(fileSize + RANDOM_BLOCK_MAX_SIZE + sizeof(entry_t));
174-
if (!entry) {
175-
memset(fileData, 0xFF, fileSize);
176-
free(fileData);
177-
return -2;
178-
}
179-
memset(entry, 0, fileSize + RANDOM_BLOCK_MAX_SIZE + sizeof(entry_t));
180-
181177
// create the entry
182-
uint32_t entrySize = createEntry(fileSize, password, name, fileData, entry);
183-
memset(fileData, 0xFF, fileSize);
184-
free(fileData);
178+
void* entryBlock = entry; // lord forgive me
179+
uint32_t entrySize = createEntry(fileSize, password, name, entry, &entry);
185180
if (entrySize == 0xFFFFFFFF) {
186-
free(entry);
181+
memset(fileData, 0xFF, fileSize);
182+
free(entryBlock);
187183
return -3;
188184
}
189185

190186
// output the entry
191187
if (ouput == OUPUT_FILE) { // append entry to file
192188
fp = fopen(pak, "ab");
193189
if (!fp) {
194-
free(entry);
190+
memset(entry, 0xFF, entrySize);
191+
free(entryBlock);
195192
return -4;
196193
}
197194
fwrite(entry, entrySize, 1, fp);
@@ -207,7 +204,7 @@ int addEntry(char* name, char* password, char* pak, int iput, int ouput) {
207204
fclose(fp);
208205
}
209206
memset(entry, 0xFF, entrySize);
210-
free(entry);
207+
free(entryBlock);
211208

212209
return 0;
213210
}
@@ -264,32 +261,25 @@ int getEntry(char* name, char* password, char* pak, int iput, int ouput) {
264261
}
265262
entry_t* entry = pakData + entryOffset;
266263

267-
// find the entry size & alloc a block for it
264+
// data to decrypt
265+
void* entryData = (void*)entry + sizeof(entry_t);
266+
267+
// find the entry size
268268
uint32_t entryDataSize = findEntryDataSize(entry->enc_size, name, pakSize - entryOffset);
269269
if (entryDataSize >= (pakSize - entryOffset)) {
270270
free(pakData);
271271
return -4;
272272
}
273-
void* entryData = malloc(ALIGN16(entryDataSize));
274-
if (!entryData) {
275-
free(pakData);
276-
return -5;
277-
}
278273

279274
if (ouput == OUPUT_FILE) {
280275
// open a file for write b4 sensitive data
281276
fp = fopen(name, "wb");
282277
if (!fp) {
283278
free(pakData);
284-
free(entryData);
285279
return -6;
286280
}
287281
}
288282

289-
// copy data to decrypt
290-
memset(entryData, 0, ALIGN16(entryDataSize));
291-
memcpy(entryData, (void*)entry + sizeof(entry_t), ALIGN16(entryDataSize));
292-
293283
// calculate keys & decrypt the data
294284
uint8_t dataKey[16], dataIV[16];
295285
calculateDataKey(password, dataKey, dataIV, entry);
@@ -314,9 +304,9 @@ int getEntry(char* name, char* password, char* pak, int iput, int ouput) {
314304
fclose(fp);
315305
}
316306
}
317-
free(pakData);
307+
318308
memset(entryData, 0xFF, ALIGN16(entryDataSize));
319-
free(entryData);
309+
free(pakData);
320310
memset(dataKey, 0xFF, 16);
321311
memset(dataIV, 0xFF, 16);
322312

blobpak.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022 skgleba
2+
* Copyright (C) 2022-2023 skgleba
33
*
44
* This software may be modified and distributed under the terms
55
* of the MIT license. See the LICENSE file for details.
@@ -13,7 +13,7 @@
1313
#define STDIN_BUF_INCR 0x200
1414
#define RANDOM_BLOCK_MAX_SIZE 0x600
1515

16-
#define VER_STRING "blobpak v1.1 by skgleba"
16+
#define VER_STRING "blobpak v1.2 by skgleba"
1717

1818
typedef struct entry_t {
1919
unsigned char entryID[ENC_ENTRY_ID_SIZE];

0 commit comments

Comments
 (0)