diff options
author | William Wennerström <william@wstrm.dev> | 2020-06-28 15:16:03 +0200 |
---|---|---|
committer | William Wennerström <william@wstrm.dev> | 2020-11-16 21:58:08 +0100 |
commit | f4ab1ca9e75bbc635781c795089bf669ee08942d (patch) | |
tree | b928a6dfcf9c093b6aee7bcecf0dfd139c1298e2 /src | |
parent | e98644f631b516e38dd4142103356ceb31628aab (diff) | |
download | profani-tty-f4ab1ca9e75bbc635781c795089bf669ee08942d.tar.gz |
Move file encryption function to public header
Diffstat (limited to 'src')
-rw-r--r-- | src/command/cmd_funcs.c | 7 | ||||
-rw-r--r-- | src/omemo/crypto.c | 31 | ||||
-rw-r--r-- | src/omemo/crypto.h | 11 | ||||
-rw-r--r-- | src/omemo/omemo.c | 32 | ||||
-rw-r--r-- | src/omemo/omemo.h | 9 |
5 files changed, 47 insertions, 43 deletions
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index f000ae1e..25d21264 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -73,7 +73,6 @@ #include "plugins/plugins.h" #include "ui/ui.h" #include "ui/window_list.h" -#include "omemo/crypto.h" #include "xmpp/xmpp.h" #include "xmpp/connection.h" #include "xmpp/contact.h" @@ -4873,8 +4872,8 @@ cmd_sendfile(ProfWin* window, const char* const command, gchar** args) FILE *tmpfh = fdopen(tmpfd, "wb"); int crypt_res; - alt_scheme = AES256_GCM_URL_SCHEME; - alt_fragment = aes256gcm_encrypt_file(fh, tmpfh, file_size(fd), &crypt_res); + alt_scheme = OMEMO_AESGCM_URL_SCHEME; + alt_fragment = omemo_encrypt_file(fh, tmpfh, file_size(fd), &crypt_res); if (crypt_res != 0) { char *msg = "Failed to encrypt file."; cons_show_error(msg); @@ -4949,7 +4948,7 @@ cmd_sendfile(ProfWin* window, const char* const command, gchar** args) out: #ifdef HAVE_OMEMO if (alt_fragment != NULL) - aes256gcm_fragment_free(alt_fragment); + omemo_free(alt_fragment); #endif if (filename != NULL) free(filename); diff --git a/src/omemo/crypto.c b/src/omemo/crypto.c index 7dd3be10..3be64636 100644 --- a/src/omemo/crypto.c +++ b/src/omemo/crypto.c @@ -479,34 +479,3 @@ char *aes256gcm_create_secure_fragment(unsigned char *key, unsigned char *nonce) return fragment; } - -void aes256gcm_fragment_free(char *fragment) { - gcry_free(fragment); -} - -char *aes256gcm_encrypt_file(FILE *in, FILE *out, off_t file_size, int *gcry_res) { - unsigned char *key = gcry_random_bytes_secure( - AES256_GCM_KEY_LENGTH, - GCRY_VERY_STRONG_RANDOM); - - // Create nonce/IV with random bytes. - unsigned char nonce[AES256_GCM_NONCE_LENGTH]; - gcry_create_nonce(nonce, AES256_GCM_NONCE_LENGTH); - - char *fragment = aes256gcm_create_secure_fragment(key, nonce); - *gcry_res = aes256gcm_crypt_file(in, out, file_size, key, nonce, true); - - if (*gcry_res != GPG_ERR_NO_ERROR) { - gcry_free(fragment); - fragment = NULL; - } - - gcry_free(key); - - return fragment; -} - -//int aes256gcm_decrypt_file(FILE *in, FILE *out, off_t file_size, -// unsigned char key[], unsigned char nonce[]) { -// return aes256gcm_crypt_file(in, out, file_size, key, nonce, false); -//} diff --git a/src/omemo/crypto.h b/src/omemo/crypto.h index f24fa163..e8d91ecc 100644 --- a/src/omemo/crypto.h +++ b/src/omemo/crypto.h @@ -33,13 +33,13 @@ * */ #include <stdio.h> +#include <stdbool.h> #include <signal/signal_protocol_types.h> #define AES128_GCM_KEY_LENGTH 16 #define AES128_GCM_IV_LENGTH 12 #define AES128_GCM_TAG_LENGTH 16 -#define AES256_GCM_URL_SCHEME "aesgcm" #define AES256_GCM_KEY_LENGTH 32 #define AES256_GCM_NONCE_LENGTH 12 @@ -186,9 +186,8 @@ int aes128gcm_decrypt(unsigned char *plaintext, size_t ciphertext_len, const unsigned char *const iv, size_t iv_len, const unsigned char *const key, const unsigned char *const tag); -char *aes256gcm_encrypt_file(FILE *in, FILE *out, off_t file_size, int *gcry_res); +int aes256gcm_crypt_file(FILE *in, FILE *out, off_t file_size, + unsigned char key[], unsigned char nonce[], bool encrypt); -//int aes256gcm_decrypt_file(FILE *in, FILE *out, off_t file_size, -// unsigned char key[], unsigned char nonce[]); - -void aes256gcm_fragment_free(char *fragment); +char *aes256gcm_create_secure_fragment(unsigned char *key, + unsigned char *nonce); diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c index c6c34ac1..c6f92e46 100644 --- a/src/omemo/omemo.c +++ b/src/omemo/omemo.c @@ -1653,3 +1653,35 @@ _generate_signed_pre_key(void) signal_protocol_signed_pre_key_store_key(omemo_ctx.store, signed_pre_key); SIGNAL_UNREF(signed_pre_key); } + + +void omemo_free(void *a) { + gcry_free(a); +} + +char *omemo_encrypt_file(FILE *in, FILE *out, off_t file_size, int *gcry_res) { + unsigned char *key = gcry_random_bytes_secure( + AES256_GCM_KEY_LENGTH, + GCRY_VERY_STRONG_RANDOM); + + // Create nonce/IV with random bytes. + unsigned char nonce[AES256_GCM_NONCE_LENGTH]; + gcry_create_nonce(nonce, AES256_GCM_NONCE_LENGTH); + + char *fragment = aes256gcm_create_secure_fragment(key, nonce); + *gcry_res = aes256gcm_crypt_file(in, out, file_size, key, nonce, true); + + if (*gcry_res != GPG_ERR_NO_ERROR) { + gcry_free(fragment); + fragment = NULL; + } + + gcry_free(key); + + return fragment; +} + +//int omemo_decrypt_file(FILE *in, FILE *out, off_t file_size, +// unsigned char key[], unsigned char nonce[]) { +// return aes256gcm_crypt_file(in, out, file_size, key, nonce, false); +//} diff --git a/src/omemo/omemo.h b/src/omemo/omemo.h index ecfc42d9..0bddd9cd 100644 --- a/src/omemo/omemo.h +++ b/src/omemo/omemo.h @@ -40,6 +40,8 @@ #define OMEMO_ERR_UNSUPPORTED_CRYPTO -10000 #define OMEMO_ERR_GCRYPT -20000 +#define OMEMO_AESGCM_URL_SCHEME "aesgcm" + typedef enum { PROF_OMEMOPOLICY_MANUAL, PROF_OMEMOPOLICY_AUTOMATIC, @@ -93,5 +95,8 @@ void omemo_start_muc_sessions(const char* const roomjid); void omemo_start_device_session(const char* const jid, uint32_t device_id, GList* prekeys, uint32_t signed_prekey_id, const unsigned char* const signed_prekey, size_t signed_prekey_len, const unsigned char* const signature, size_t signature_len, const unsigned char* const identity_key, size_t identity_key_len); gboolean omemo_loaded(void); -char* omemo_on_message_send(ProfWin* win, const char* const message, gboolean request_receipt, gboolean muc, const char* const replace_id); -char* omemo_on_message_recv(const char* const from, uint32_t sid, const unsigned char* const iv, size_t iv_len, GList* keys, const unsigned char* const payload, size_t payload_len, gboolean muc, gboolean* trusted); +char * omemo_on_message_send(ProfWin *win, const char *const message, gboolean request_receipt, gboolean muc, const char *const replace_id); +char * omemo_on_message_recv(const char *const from, uint32_t sid, const unsigned char *const iv, size_t iv_len, GList *keys, const unsigned char *const payload, size_t payload_len, gboolean muc, gboolean *trusted); + +char *omemo_encrypt_file(FILE *in, FILE *out, off_t file_size, int *gcry_res); +void omemo_free(void *a); |