summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-10-24 15:55:52 -0400
committerirungentoo <irungentoo@gmail.com>2013-10-24 15:55:52 -0400
commitbba10544e775ffc6840af948116b2abb0870480d (patch)
tree262c07b014ca928c9de2dd55ac4236d739692d9c /toxcore/net_crypto.c
parent9bef5f5bffde83d19c02c6e112fea82d587790dc (diff)
Added symmetric crypto functions to net_crypto.
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c50
1 files changed, 48 insertions, 2 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 3c16f0ac..14cee200 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -77,7 +77,7 @@ int encrypt_data_fast(uint8_t *enc_key, uint8_t *nonce,
77 77
78 /* Unpad the encrypted message. */ 78 /* Unpad the encrypted message. */
79 memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES); 79 memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES);
80 return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; 80 return length + crypto_box_MACBYTES;
81} 81}
82 82
83/* Fast decrypt. Depends on enc_ley from encrypt_precompute. */ 83/* Fast decrypt. Depends on enc_ley from encrypt_precompute. */
@@ -104,7 +104,7 @@ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce,
104 104
105 /* Unpad the plain message. */ 105 /* Unpad the plain message. */
106 memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_MACBYTES); 106 memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_MACBYTES);
107 return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; 107 return length - crypto_box_MACBYTES;
108} 108}
109 109
110int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, 110int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
@@ -123,6 +123,39 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
123 return decrypt_data_fast(k, nonce, encrypted, length, plain); 123 return decrypt_data_fast(k, nonce, encrypted, length, plain);
124} 124}
125 125
126int encrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *plain, uint32_t length, uint8_t *encrypted)
127{
128 if (length + crypto_secretbox_MACBYTES > MAX_DATA_SIZE || length == 0)
129 return -1;
130
131 uint8_t temp_plain[MAX_DATA_SIZE + crypto_secretbox_ZEROBYTES] = {0};
132 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_secretbox_BOXZEROBYTES];
133
134 memcpy(temp_plain + crypto_secretbox_ZEROBYTES, plain, length); // Pad the message with 32 0 bytes.
135
136 crypto_secretbox(temp_encrypted, temp_plain, length + crypto_secretbox_ZEROBYTES, nonce, secret_key);
137 /* Unpad the encrypted message. */
138 memcpy(encrypted, temp_encrypted + crypto_secretbox_BOXZEROBYTES, length + crypto_secretbox_MACBYTES);
139 return length + crypto_secretbox_MACBYTES;
140}
141
142int decrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *encrypted, uint32_t length, uint8_t *plain)
143{
144 if (length > MAX_DATA_SIZE || length <= crypto_secretbox_BOXZEROBYTES)
145 return -1;
146
147 uint8_t temp_plain[MAX_DATA_SIZE + crypto_secretbox_ZEROBYTES];
148 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_secretbox_BOXZEROBYTES] = {0};
149
150 memcpy(temp_encrypted + crypto_secretbox_BOXZEROBYTES, encrypted, length); // Pad the message with 16 0 bytes.
151
152 if (crypto_secretbox_open(temp_plain, temp_encrypted, length + crypto_secretbox_BOXZEROBYTES, nonce, secret_key) == -1)
153 return -1;
154
155 memcpy(plain, temp_plain + crypto_secretbox_ZEROBYTES, length - crypto_secretbox_MACBYTES);
156 return length - crypto_secretbox_MACBYTES;
157}
158
126/* Increment the given nonce by 1. */ 159/* Increment the given nonce by 1. */
127static void increment_nonce(uint8_t *nonce) 160static void increment_nonce(uint8_t *nonce)
128{ 161{
@@ -136,16 +169,29 @@ static void increment_nonce(uint8_t *nonce)
136 } 169 }
137} 170}
138 171
172#if crypto_box_NONCEBYTES != crypto_secretbox_NONCEBYTES
173/*if they no longer equal each other, this function must be slit into two.*/
174#error random_nonce(): crypto_box_NONCEBYTES must equal crypto_secretbox_NONCEBYTES.
175#endif
139/* Fill the given nonce with random bytes. */ 176/* Fill the given nonce with random bytes. */
140void random_nonce(uint8_t *nonce) 177void random_nonce(uint8_t *nonce)
141{ 178{
142 randombytes(nonce, crypto_box_NONCEBYTES); 179 randombytes(nonce, crypto_box_NONCEBYTES);
143} 180}
144 181
182/* Fill a key crypto_secretbox_KEYBYTES big with random bytes */
183void new_symmetric_key(uint8_t *key)
184{
185 randombytes(key, crypto_secretbox_KEYBYTES);
186}
145 187
146static uint8_t base_nonce[crypto_box_NONCEBYTES]; 188static uint8_t base_nonce[crypto_box_NONCEBYTES];
147static uint8_t nonce_set = 0; 189static uint8_t nonce_set = 0;
148 190
191#if crypto_box_NONCEBYTES != crypto_secretbox_NONCEBYTES
192/*if they no longer equal each other, this function must be slit into two.*/
193#error new_nonce(): crypto_box_NONCEBYTES must equal crypto_secretbox_NONCEBYTES.
194#endif
149/* Gives a nonce guaranteed to be different from previous ones.*/ 195/* Gives a nonce guaranteed to be different from previous ones.*/
150void new_nonce(uint8_t *nonce) 196void new_nonce(uint8_t *nonce)
151{ 197{