diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-01-17 14:03:53 +0100 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-01-17 14:03:53 +0100 |
commit | 90545e798c5d046cec0742bbc26f794da0c248ca (patch) | |
tree | 572a0fe6d0befd961a293e6a035fed37345b6a93 | |
parent | 551d36ad77df853998aa5726bd7d26c945122270 (diff) | |
download | libgnunetchat-90545e798c5d046cec0742bbc26f794da0c248ca.tar.gz libgnunetchat-90545e798c5d046cec0742bbc26f794da0c248ca.zip |
Added file hash checking after decryption and using hash as iv
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | src/gnunet_chat_lib.c | 6 | ||||
-rw-r--r-- | src/gnunet_chat_util.c | 18 | ||||
-rw-r--r-- | src/gnunet_chat_util.h | 4 |
3 files changed, 20 insertions, 8 deletions
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c index 8332f1d..be07f85 100644 --- a/src/gnunet_chat_lib.c +++ b/src/gnunet_chat_lib.c | |||
@@ -651,7 +651,7 @@ GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, | |||
651 | struct GNUNET_CRYPTO_SymmetricSessionKey key; | 651 | struct GNUNET_CRYPTO_SymmetricSessionKey key; |
652 | GNUNET_CRYPTO_symmetric_create_session_key(&key); | 652 | GNUNET_CRYPTO_symmetric_create_session_key(&key); |
653 | 653 | ||
654 | if (GNUNET_OK != util_encrypt_file(filename, &key)) | 654 | if (GNUNET_OK != util_encrypt_file(filename, &hash, &key)) |
655 | { | 655 | { |
656 | GNUNET_free(filename); | 656 | GNUNET_free(filename); |
657 | return NULL; | 657 | return NULL; |
@@ -1043,7 +1043,9 @@ GNUNET_CHAT_file_open_preview (struct GNUNET_CHAT_File *file) | |||
1043 | remove(file->preview); | 1043 | remove(file->preview); |
1044 | 1044 | ||
1045 | if ((GNUNET_OK != GNUNET_DISK_file_copy(filename, file->preview)) || | 1045 | if ((GNUNET_OK != GNUNET_DISK_file_copy(filename, file->preview)) || |
1046 | (GNUNET_OK != util_decrypt_file(file->preview, &(file->key)))) | 1046 | (GNUNET_OK != util_decrypt_file(file->preview, |
1047 | &(file->hash), | ||
1048 | &(file->key)))) | ||
1047 | { | 1049 | { |
1048 | GNUNET_free(file->preview); | 1050 | GNUNET_free(file->preview); |
1049 | file->preview = NULL; | 1051 | file->preview = NULL; |
diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c index 9876f8b..5fde9a3 100644 --- a/src/gnunet_chat_util.c +++ b/src/gnunet_chat_util.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2021 GNUnet e.V. | 3 | Copyright (C) 2021--2022 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -86,9 +86,10 @@ util_hash_file (const char *filename, struct GNUNET_HashCode *hash) | |||
86 | 86 | ||
87 | int | 87 | int |
88 | util_encrypt_file (const char *filename, | 88 | util_encrypt_file (const char *filename, |
89 | const struct GNUNET_HashCode *hash, | ||
89 | const struct GNUNET_CRYPTO_SymmetricSessionKey *key) | 90 | const struct GNUNET_CRYPTO_SymmetricSessionKey *key) |
90 | { | 91 | { |
91 | GNUNET_assert((filename) && (key)); | 92 | GNUNET_assert((filename) && (hash) && (key)); |
92 | 93 | ||
93 | uint64_t size; | 94 | uint64_t size; |
94 | 95 | ||
@@ -131,7 +132,7 @@ util_encrypt_file (const char *filename, | |||
131 | if (index > 0) | 132 | if (index > 0) |
132 | memcpy(&iv, ((uint8_t*) data) + (block_size * (index - 1)), sizeof(iv)); | 133 | memcpy(&iv, ((uint8_t*) data) + (block_size * (index - 1)), sizeof(iv)); |
133 | else | 134 | else |
134 | memset(&iv, 0, sizeof(iv)); | 135 | memcpy(&iv, hash, sizeof(iv)); |
135 | 136 | ||
136 | result = GNUNET_CRYPTO_symmetric_encrypt( | 137 | result = GNUNET_CRYPTO_symmetric_encrypt( |
137 | location, | 138 | location, |
@@ -162,9 +163,10 @@ util_encrypt_file (const char *filename, | |||
162 | 163 | ||
163 | int | 164 | int |
164 | util_decrypt_file (const char *filename, | 165 | util_decrypt_file (const char *filename, |
166 | const struct GNUNET_HashCode *hash, | ||
165 | const struct GNUNET_CRYPTO_SymmetricSessionKey *key) | 167 | const struct GNUNET_CRYPTO_SymmetricSessionKey *key) |
166 | { | 168 | { |
167 | GNUNET_assert((filename) && (key)); | 169 | GNUNET_assert((filename) && (hash) && (key)); |
168 | 170 | ||
169 | uint64_t size; | 171 | uint64_t size; |
170 | 172 | ||
@@ -206,7 +208,7 @@ util_decrypt_file (const char *filename, | |||
206 | if (index > 0) | 208 | if (index > 0) |
207 | memcpy(&iv, ((uint8_t*) data) + (block_size * (index - 1)), sizeof(iv)); | 209 | memcpy(&iv, ((uint8_t*) data) + (block_size * (index - 1)), sizeof(iv)); |
208 | else | 210 | else |
209 | memset(&iv, 0, sizeof(iv)); | 211 | memcpy(&iv, hash, sizeof(iv)); |
210 | 212 | ||
211 | result = GNUNET_CRYPTO_symmetric_decrypt( | 213 | result = GNUNET_CRYPTO_symmetric_decrypt( |
212 | location, | 214 | location, |
@@ -220,6 +222,12 @@ util_decrypt_file (const char *filename, | |||
220 | break; | 222 | break; |
221 | } | 223 | } |
222 | 224 | ||
225 | struct GNUNET_HashCode check; | ||
226 | GNUNET_CRYPTO_hash(data, size, &check); | ||
227 | |||
228 | if (0 != GNUNET_CRYPTO_hash_cmp(hash, &check)) | ||
229 | result = -1; | ||
230 | |||
223 | if (GNUNET_OK != GNUNET_DISK_file_unmap(mapping)) | 231 | if (GNUNET_OK != GNUNET_DISK_file_unmap(mapping)) |
224 | result = -1; | 232 | result = -1; |
225 | 233 | ||
diff --git a/src/gnunet_chat_util.h b/src/gnunet_chat_util.h index 7497b67..74d1259 100644 --- a/src/gnunet_chat_util.h +++ b/src/gnunet_chat_util.h | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2021 GNUnet e.V. | 3 | Copyright (C) 2021--2022 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -44,10 +44,12 @@ util_hash_file (const char *filename, struct GNUNET_HashCode *hash); | |||
44 | 44 | ||
45 | int | 45 | int |
46 | util_encrypt_file (const char *filename, | 46 | util_encrypt_file (const char *filename, |
47 | const struct GNUNET_HashCode *hash, | ||
47 | const struct GNUNET_CRYPTO_SymmetricSessionKey *key); | 48 | const struct GNUNET_CRYPTO_SymmetricSessionKey *key); |
48 | 49 | ||
49 | int | 50 | int |
50 | util_decrypt_file (const char *filename, | 51 | util_decrypt_file (const char *filename, |
52 | const struct GNUNET_HashCode *hash, | ||
51 | const struct GNUNET_CRYPTO_SymmetricSessionKey *key); | 53 | const struct GNUNET_CRYPTO_SymmetricSessionKey *key); |
52 | 54 | ||
53 | int | 55 | int |