Skip to content

Commit a33fb5e

Browse files
committedAug 25, 2020
changed salt & magin number
1 parent 80fea82 commit a33fb5e

File tree

5 files changed

+81
-43
lines changed

5 files changed

+81
-43
lines changed
 

‎include/mtsdata.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef enum {
2323
MTSD_EENCODE_PAYLOAD_SIZE,
2424
MTSD_EDECODE_CORRUPTED_PAYLOAD,
2525
MTSD_EPARSE_UNKNOWN_KEY,
26+
MTSD_ETIME,
2627
} mtsd_err;
2728

2829
typedef struct mtsd_field {

‎lib/container.c

+54-11
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66

77
#pragma pack(push, 1)
88
typedef struct {
9+
uint8_t magic_number;
910
uint16_t crc16;
10-
uint16_t date;
11+
uint32_t date;
1112
uint8_t random_bytes[MTSD_RANDOM_BYTES];
1213
uint8_t is_compressed;
1314
} mtsd_header;
1415
#pragma pack(pop)
1516

1617
static uint16_t crc16(uint8_t* data, size_t size);
17-
static uint16_t get_date_now ();
18+
static mtsd_res get_date_now (uint32_t* out);
19+
static void derive_salt(const uint8_t* data, size_t data_size, const uint8_t* pass, size_t pass_len, uint8_t* salt);
1820

1921
mtsd_res mtsd_encrypt(mtsd_document* doc,
2022
uint8_t* password,
@@ -42,10 +44,16 @@ mtsd_res mtsd_encrypt(mtsd_document* doc,
4244
}
4345
MTSD_FREE(encoded);
4446

45-
MTSD_CHECK_GOTO(mtsd_encrypt_payload(out + sizeof(mtsd_header), *size, password, password_len, ((mtsd_header*)out)->random_bytes), error);
47+
MTSD_CHECK_GOTO(get_date_now(&((mtsd_header*)out)->date), error);
4648

47-
((mtsd_header*)out)->date = get_date_now();
48-
((mtsd_header*)out)->crc16 = crc16(out + 2, sizeof(mtsd_header) + *size - 2);
49+
MTSD_CHECK_GOTO(mtsd_random_bytes(((mtsd_header*)out)->random_bytes, MTSD_RANDOM_BYTES), error);
50+
51+
uint8_t salt[MTSD_SALT_SIZE];
52+
derive_salt(out, (sizeof(mtsd_header) + *size), password, password_len, salt);
53+
MTSD_CHECK_GOTO(mtsd_encrypt_payload(out + sizeof(mtsd_header), *size, password, password_len, salt), error);
54+
55+
((mtsd_header*)out)->magic_number = 0x7D;
56+
((mtsd_header*)out)->crc16 = crc16(out + 3, sizeof(mtsd_header) + *size - 3);
4957

5058
*encrypted = (uint8_t*)out;
5159
*size = (sizeof(mtsd_header) + *size);
@@ -68,7 +76,9 @@ mtsd_res mtsd_decrypt(uint8_t* encrypted,
6876
MTSD_MALLOC(cloned, size - sizeof(mtsd_header));
6977
memcpy(cloned, encrypted + sizeof(mtsd_header), size - sizeof(mtsd_header));
7078

71-
MTSD_CHECK_GOTO(mtsd_decrypt_payload(cloned, size - sizeof(mtsd_header), password, password_len, ((mtsd_header*)encrypted)->random_bytes), error);
79+
uint8_t salt[MTSD_SALT_SIZE];
80+
derive_salt(encrypted, size, password, password_len, salt);
81+
MTSD_CHECK_GOTO(mtsd_decrypt_payload(cloned, size - sizeof(mtsd_header), password, password_len, salt), error);
7282

7383
if (((mtsd_header*)encrypted)->is_compressed) {
7484
encoded = mtsd_malloc(MTSD_PAYLOAD_MAX_SIZE);
@@ -119,10 +129,43 @@ static uint16_t crc16 (uint8_t* data, size_t size) {
119129
return crc;
120130
}
121131

122-
static uint16_t get_date_now () {
132+
static mtsd_res get_date_now (uint32_t* out) {
123133
time_t t = time(NULL);
124-
struct tm* local = localtime(&t);
125-
uint16_t delta_y = local->tm_year + 1900 - MTSD_DATE_FROM;
126-
uint16_t delta_d = (delta_y * 366) + (local->tm_yday + 1);
127-
return delta_d;
134+
if (t == (time_t)-1) {
135+
mtsd_error(MTSD_ESELF, MTSD_ETIME, "cannot get system time");
136+
return MTSD_ERR;
137+
}
138+
*out = (uint32_t)((uint64_t)t - MTSD_DATE_FROM);
139+
return MTSD_OK;
140+
}
141+
142+
static void derive_salt(const uint8_t* data, size_t data_size, const uint8_t* pass, size_t pass_len, uint8_t* salt) {
143+
#ifndef DEBUG
144+
mtsd_header* header = (mtsd_header*)data;
145+
salt[0] = (data_size >> (8*0)) & 0xFF;
146+
salt[1] = (data_size >> (8*1)) & 0xFF;
147+
salt[2] = (header->date >> (8*0)) & 0xFF;
148+
salt[3] = (header->date >> (8*1)) & 0xFF;
149+
salt[4] = (header->date >> (8*2)) & 0xFF;
150+
salt[5] = (header->date >> (8*3)) & 0xFF;
151+
salt[6] = header->random_bytes[0];
152+
salt[7] = header->random_bytes[1];
153+
salt[8] = header->random_bytes[2];
154+
salt[9] = header->random_bytes[3];
155+
salt[10] = (pass_len >> (8*0)) & 0xFF;
156+
salt[11] = 0x42;
157+
salt[12] = 0xf0;
158+
salt[13] = 0xe1;
159+
salt[14] = 0xeb;
160+
salt[15] = 0xa9;
161+
162+
for (size_t pi = 0, si = 11; pi < pass_len && si < MTSD_SALT_SIZE; pi += 1, si += 1) {
163+
salt[si] ^= pass[pi];
164+
}
165+
#else
166+
salt[0] = 0x0; salt[1] = 0x1; salt[2] = 0x2; salt[3] = 0x3;
167+
salt[4] = 0x4; salt[5] = 0x5; salt[6] = 0x6; salt[7] = 0x7;
168+
salt[8] = 0xF; salt[9] = 0xE; salt[10] = 0xD; salt[11] = 0xC;
169+
salt[12] = 0xB; salt[13] = 0xA; salt[14] = 0x9; salt[15] = 0x8;
170+
#endif
128171
}

‎lib/encryption.c

+16-26
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,12 @@
44
#include <argon2.h>
55
#include <randombytes.h>
66

7-
#ifdef DEBUG
8-
#include <string.h>
9-
10-
static uint8_t DEBUG_RND_BYTES[] = {
11-
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
12-
0xF, 0xE, 0xD, 0xC, 0xB, 0xA, 0x9, 0x8,
13-
};
14-
#endif
15-
16-
static mtsd_res derive_bytes(uint8_t* random_bytes, uint8_t* pwd, size_t pwd_size, uint8_t* out);
7+
static mtsd_res derive_bytes(uint8_t* salt, uint8_t* pwd, size_t pwd_size, uint8_t* out);
178

189
mtsd_res mtsd_encrypt_payload(uint8_t* data, size_t data_size,
19-
uint8_t* pwd, size_t pwd_size, uint8_t* random_bytes) {
20-
#ifndef DEBUG
21-
int err = randombytes(random_bytes, MTSD_RANDOM_BYTES);
22-
if (err != 0) {
23-
mtsd_error(MTSD_ERANDOMBYTES, err, NULL);
24-
return MTSD_ERR;
25-
}
26-
#else
27-
memcpy(random_bytes, DEBUG_RND_BYTES, MTSD_RANDOM_BYTES);
28-
#endif
29-
10+
uint8_t* pwd, size_t pwd_size, uint8_t* salt) {
3011
uint8_t derived_bytes[AES_KEYLEN + AES_BLOCKLEN];
31-
MTSD_CHECK (derive_bytes(random_bytes, pwd, pwd_size, derived_bytes));
12+
MTSD_CHECK (derive_bytes(salt, pwd, pwd_size, derived_bytes));
3213
uint8_t* key = derived_bytes;
3314
uint8_t* iv = derived_bytes + AES_KEYLEN;
3415

@@ -39,9 +20,9 @@ mtsd_res mtsd_encrypt_payload(uint8_t* data, size_t data_size,
3920
}
4021

4122
mtsd_res mtsd_decrypt_payload(uint8_t* data, size_t data_size,
42-
uint8_t* pwd, size_t pwd_size, uint8_t* random_bytes) {
23+
uint8_t* pwd, size_t pwd_size, uint8_t* salt) {
4324
uint8_t derived_bytes[AES_KEYLEN + AES_BLOCKLEN];
44-
MTSD_CHECK (derive_bytes(random_bytes, pwd, pwd_size, derived_bytes));
25+
MTSD_CHECK (derive_bytes(salt, pwd, pwd_size, derived_bytes));
4526
uint8_t* key = derived_bytes;
4627
uint8_t* iv = derived_bytes + AES_KEYLEN;
4728

@@ -59,11 +40,11 @@ static void argon2_free (uint8_t *memory, size_t bytes_to_allocate) {
5940
mtsd_free(memory);
6041
}
6142

62-
static mtsd_res derive_bytes(uint8_t* random_bytes, uint8_t* pwd, size_t pwd_size, uint8_t* out) {
43+
static mtsd_res derive_bytes(uint8_t* salt, uint8_t* pwd, size_t pwd_size, uint8_t* out) {
6344
struct Argon2_Context ctx = {
6445
.out = out, .outlen = (AES_KEYLEN + AES_BLOCKLEN),
6546
.pwd = pwd, .pwdlen = pwd_size,
66-
.salt = random_bytes, .saltlen = MTSD_RANDOM_BYTES,
47+
.salt = salt, .saltlen = MTSD_SALT_SIZE,
6748

6849
.t_cost = 10,
6950
.m_cost = (1 << 15), // 32 MB
@@ -84,3 +65,12 @@ static mtsd_res derive_bytes(uint8_t* random_bytes, uint8_t* pwd, size_t pwd_siz
8465
}
8566
return MTSD_OK;
8667
}
68+
69+
mtsd_res mtsd_random_bytes(uint8_t* out, size_t size) {
70+
int err = randombytes(out, size);
71+
if (err != 0) {
72+
mtsd_error(MTSD_ERANDOMBYTES, err, NULL);
73+
return MTSD_ERR;
74+
}
75+
return MTSD_OK;
76+
}

‎lib/private.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include "mtsdata.h"
22

3-
#define MTSD_RANDOM_BYTES 16
3+
#define MTSD_RANDOM_BYTES 4
4+
#define MTSD_SALT_SIZE 16
45
#define MTSD_PAYLOAD_MAX_SIZE 0xFFFF
5-
#define MTSD_DATE_FROM 1999
6+
#define MTSD_DATE_FROM 915148800 // 01/01/1999
67

78
#define MTSD_CHECK(result) if ((result) != MTSD_OK) {\
89
return MTSD_ERR;\
@@ -48,10 +49,12 @@ mtsd_res mtsd_encrypt_payload(/* In Out */ uint8_t* data,
4849
/* In */ size_t data_size,
4950
/* In */ uint8_t* pwd,
5051
/* In */ size_t pwd_size,
51-
/* Out */ uint8_t* random_bytes);
52+
/* In */ uint8_t* salt);
5253

5354
mtsd_res mtsd_decrypt_payload(/* In Out */ uint8_t* data,
5455
/* In */ size_t data_size,
5556
/* In */ uint8_t* pwd,
5657
/* In */ size_t pwd_size,
57-
/* In */ uint8_t* random_bytes);
58+
/* In */ uint8_t* salt);
59+
60+
mtsd_res mtsd_random_bytes(uint8_t* out, size_t size);

‎test/payload.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
from Crypto.Cipher import AES
44
from hexdump import hexdump
55

6+
# pip install argon2-cffi pycryptodome hexdump
67
# python test/payload.py && hexdump -C data.mtsd.bin --skip 21
78

89
PASSWORD = b'pass'
910

10-
RANDOM_BYTES = bytes([
11+
SALT = bytes([
1112
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
1213
0xF, 0xE, 0xD, 0xC, 0xB, 0xA, 0x9, 0x8,
1314
])
@@ -33,7 +34,7 @@
3334

3435
derived_bytes = argon2.low_level.hash_secret_raw(
3536
secret=PASSWORD,
36-
salt=RANDOM_BYTES,
37+
salt=SALT,
3738
time_cost=1,
3839
memory_cost=1<<12,
3940
parallelism=1,

0 commit comments

Comments
 (0)
Please sign in to comment.