about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorPaul Fariello <paul@fariello.eu>2019-03-06 18:57:11 +0140
committerPaul Fariello <paul@fariello.eu>2019-04-10 16:31:45 +0200
commit605e06411ca6d184baf19157620d0473d7c3d557 (patch)
treeb86af7ac283ec66b11a7b0dc63b558144230c007 /src
parent695694051e2f56d947ea8baf9199c1ff7bf81121 (diff)
downloadprofani-tty-605e06411ca6d184baf19157620d0473d7c3d557.tar.gz
Get rid of libsodium
Diffstat (limited to 'src')
-rw-r--r--src/command/cmd_funcs.c2
-rw-r--r--src/omemo/crypto.c84
-rw-r--r--src/omemo/omemo.c20
-rw-r--r--src/omemo/omemo.h1
4 files changed, 73 insertions, 34 deletions
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c
index de3372cd..1ce034d9 100644
--- a/src/command/cmd_funcs.c
+++ b/src/command/cmd_funcs.c
@@ -7944,7 +7944,7 @@ cmd_omemo_start(ProfWin *window, const char *const command, gchar **args)
         }
 
         if (!omemo_loaded()) {
-            win_println(window, THEME_DEFAULT, '!', "You have not generated or loaded a cryptographic materials, use '/omemo init'");
+            win_println(window, THEME_DEFAULT, '!', "You have not generated or loaded a cryptographic materials, use '/omemo gen'");
             return TRUE;
         }
 
diff --git a/src/omemo/crypto.c b/src/omemo/crypto.c
index 73b2ba0d..5119443a 100644
--- a/src/omemo/crypto.c
+++ b/src/omemo/crypto.c
@@ -1,19 +1,15 @@
 #include <assert.h>
 #include <signal/signal_protocol.h>
 #include <signal/signal_protocol_types.h>
-#include <sodium.h>
 #include <gcrypt.h>
 
+#include "log.h"
 #include "omemo/omemo.h"
 #include "omemo/crypto.h"
 
 int
 omemo_crypto_init(void)
 {
-    if (sodium_init() < 0) {
-        return -1;
-    }
-
     if (!gcry_check_version(GCRYPT_VERSION)) {
         return -1;
     }
@@ -26,74 +22,114 @@ omemo_crypto_init(void)
 int
 omemo_random_func(uint8_t *data, size_t len, void *user_data)
 {
-    randombytes_buf(data, len);
+    gcry_randomize(data, len, GCRY_VERY_STRONG_RANDOM);
     return 0;
 }
 
 int
 omemo_hmac_sha256_init_func(void **hmac_context, const uint8_t *key, size_t key_len, void *user_data)
 {
-    *hmac_context = sodium_malloc(sizeof(crypto_auth_hmacsha256_state));
-    return crypto_auth_hmacsha256_init(*hmac_context, key, key_len);
+    gcry_error_t res;
+    gcry_mac_hd_t hd;
+
+    res = gcry_mac_open(&hd, GCRY_MAC_HMAC_SHA256, 0, NULL);
+    if (res != GPG_ERR_NO_ERROR) {
+        log_error("OMEMO: %s", gcry_strerror(res));
+        return OMEMO_ERR_GCRYPT;
+    }
+
+    *hmac_context = hd;
+    res = gcry_mac_setkey(hd, key, key_len);
+    if (res != GPG_ERR_NO_ERROR) {
+        log_error("OMEMO: %s", gcry_strerror(res));
+        return OMEMO_ERR_GCRYPT;
+    }
+
+    return 0;
 }
 
 int
 omemo_hmac_sha256_update_func(void *hmac_context, const uint8_t *data, size_t data_len, void *user_data)
 {
-    return crypto_auth_hmacsha256_update(hmac_context, data, data_len);
+    gcry_error_t res;
+
+    res = gcry_mac_write(hmac_context, data, data_len);
+    if (res != GPG_ERR_NO_ERROR) {
+        log_error("OMEMO: %s", gcry_strerror(res));
+        return OMEMO_ERR_GCRYPT;
+    }
+
+    return 0;
 }
 
 int
 omemo_hmac_sha256_final_func(void *hmac_context, signal_buffer **output, void *user_data)
 {
-    int ret;
-    unsigned char out[crypto_auth_hmacsha256_BYTES];
+    gcry_error_t res;
+    size_t mac_len = 32;
+    unsigned char out[mac_len];
 
-    if ((ret = crypto_auth_hmacsha256_final(hmac_context, out)) != 0) {
-        return ret;
+    res = gcry_mac_read(hmac_context, out, &mac_len);
+    if (res != GPG_ERR_NO_ERROR) {
+        log_error("OMEMO: %s", gcry_strerror(res));
+        return OMEMO_ERR_GCRYPT;
     }
 
-    *output = signal_buffer_create(out, crypto_auth_hmacsha256_BYTES);
+    *output = signal_buffer_create(out, mac_len);
     return 0;
 }
 
 void
 omemo_hmac_sha256_cleanup_func(void *hmac_context, void *user_data)
 {
-    sodium_free(hmac_context);
+    gcry_mac_close(hmac_context);
 }
 
 int
 omemo_sha512_digest_init_func(void **digest_context, void *user_data)
 {
-    *digest_context = sodium_malloc(sizeof(crypto_hash_sha512_state));
-    return crypto_hash_sha512_init(*digest_context);
+    gcry_error_t res;
+    gcry_md_hd_t hd;
+
+    res = gcry_md_open(&hd, GCRY_MD_SHA512, 0);
+    if (res != GPG_ERR_NO_ERROR) {
+        log_error("OMEMO: %s", gcry_strerror(res));
+        return OMEMO_ERR_GCRYPT;
+    }
+
+    *digest_context = hd;
+
+    return 0;
 }
 
 int
 omemo_sha512_digest_update_func(void *digest_context, const uint8_t *data, size_t data_len, void *user_data)
 {
-    return crypto_hash_sha512_update(digest_context, data, data_len);
+    gcry_md_write(digest_context, data, data_len);
+
+    return 0;
 }
 
 int
 omemo_sha512_digest_final_func(void *digest_context, signal_buffer **output, void *user_data)
 {
-    int ret;
-    unsigned char out[crypto_hash_sha512_BYTES];
+    gcry_error_t res;
+    unsigned char out[64];
 
-    if ((ret = crypto_hash_sha512_final(digest_context, out)) != 0) {
-        return ret;
+    res = gcry_md_extract(digest_context, GCRY_MD_SHA512, out, 64);
+    if (res != GPG_ERR_NO_ERROR) {
+        log_error("OMEMO: %s", gcry_strerror(res));
+        return OMEMO_ERR_GCRYPT;
     }
 
-    *output = signal_buffer_create(out, crypto_hash_sha512_BYTES);
+    *output = signal_buffer_create(out, 64);
     return 0;
 }
 
 void
 omemo_sha512_digest_cleanup_func(void *digest_context, void *user_data)
 {
-    sodium_free(digest_context);
+    gcry_md_close(digest_context);
 }
 
 int
diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c
index 905c5c22..1b1da807 100644
--- a/src/omemo/omemo.c
+++ b/src/omemo/omemo.c
@@ -9,7 +9,7 @@
 #include <signal/signal_protocol.h>
 #include <signal/session_builder.h>
 #include <signal/session_cipher.h>
-#include <sodium.h>
+#include <gcrypt.h>
 
 #include "config/account.h"
 #include "log.h"
@@ -218,7 +218,8 @@ omemo_generate_crypto_materials(ProfAccount *account)
         return;
     }
 
-    omemo_ctx.device_id = randombytes_uniform(0x80000000);
+    gcry_randomize(&omemo_ctx.device_id, 4, GCRY_VERY_STRONG_RANDOM);
+    omemo_ctx.device_id &= 0x7fffffff;
 
     signal_protocol_key_helper_generate_identity_key_pair(&omemo_ctx.identity_key_pair, omemo_ctx.signal);
     signal_protocol_key_helper_generate_registration_id(&omemo_ctx.registration_id, 0, omemo_ctx.signal);
@@ -245,8 +246,11 @@ omemo_generate_crypto_materials(ProfAccount *account)
 static void
 omemo_generate_short_term_crypto_materials(ProfAccount *account)
 {
+    unsigned int start;
+
+    gcry_randomize(&start, sizeof(unsigned int), GCRY_VERY_STRONG_RANDOM);
     signal_protocol_key_helper_pre_key_list_node *pre_keys_head;
-    signal_protocol_key_helper_generate_pre_keys(&pre_keys_head, randombytes_random(), 100, omemo_ctx.signal);
+    signal_protocol_key_helper_generate_pre_keys(&pre_keys_head, start, 100, omemo_ctx.signal);
 
     session_signed_pre_key *signed_pre_key;
     struct timeval tv;
@@ -442,13 +446,11 @@ omemo_on_message_send(ProfChatWin *chatwin, const char *const message, gboolean
     unsigned char *ciphertext;
     size_t ciphertext_len;
 
-    key = sodium_malloc(AES128_GCM_KEY_LENGTH);
-    iv = sodium_malloc(AES128_GCM_IV_LENGTH);
     ciphertext_len = strlen(message) + AES128_GCM_TAG_LENGTH;
     ciphertext = malloc(ciphertext_len);
 
-    randombytes_buf(key, 16);
-    randombytes_buf(iv, 16);
+    key = gcry_random_bytes_secure(16, GCRY_VERY_STRONG_RANDOM);
+    iv = gcry_random_bytes_secure(16, GCRY_VERY_STRONG_RANDOM);
 
     res = aes128gcm_encrypt(ciphertext, &ciphertext_len, (const unsigned char * const)message, strlen(message), iv, key);
     if (res != 0) {
@@ -516,8 +518,8 @@ omemo_on_message_send(ProfChatWin *chatwin, const char *const message, gboolean
     free(id);
     g_list_free_full(keys, free);
     free(ciphertext);
-    sodium_free(key);
-    sodium_free(iv);
+    gcry_free(key);
+    gcry_free(iv);
 
     return TRUE;
 }
diff --git a/src/omemo/omemo.h b/src/omemo/omemo.h
index cffc63f1..f07d42bc 100644
--- a/src/omemo/omemo.h
+++ b/src/omemo/omemo.h
@@ -4,6 +4,7 @@
 #include "config/account.h"
 
 #define OMEMO_ERR_UNSUPPORTED_CRYPTO -10000
+#define OMEMO_ERR_GCRYPT -20000
 
 typedef struct omemo_context_t omemo_context;
 
2451b7b1 ^
b934ad54 ^
0d15c710 ^
d782b007 ^


0fbaa6f5 ^
d782b007 ^
fa89e2aa ^
0d15c710 ^
fa89e2aa ^

0d15c710 ^
264fc55a ^
0c1092fd ^

1809064d ^

0c1092fd ^
3ceb9b0d ^
fa89e2aa ^
7e4b1b1d ^
0fbaa6f5 ^

fa89e2aa ^

0fbaa6f5 ^

fa89e2aa ^
264fc55a ^

d782b007 ^
0fbaa6f5 ^
3ceb9b0d ^



0fbaa6f5 ^


d782b007 ^


0fbaa6f5 ^
e2f37600 ^
2655d9e8 ^
0c1092fd ^

01394d6c ^
0fbaa6f5 ^

0c1092fd ^

13ee16de ^
0fbaa6f5 ^


















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