GNUnet 0.28.0-dev.3-7-g31e20e2e6
 
Loading...
Searching...
No Matches
crypto_symmetric.c File Reference

Symmetric encryption services; combined cipher AES+TWOFISH (256-bit each) More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include <gcrypt.h>
Include dependency graph for crypto_symmetric.c:

Go to the source code of this file.

Macros

#define LOG(kind, ...)
 

Functions

void GNUNET_CRYPTO_symmetric_create_session_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key)
 Create a new SessionKey (for symmetric encryption).
 
static int setup_cipher_aes (gcry_cipher_hd_t *handle, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
 Initialize AES cipher.
 
static int setup_cipher_twofish (gcry_cipher_hd_t *handle, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
 Initialize TWOFISH cipher.
 
ssize_t GNUNET_CRYPTO_symmetric_encrypt (const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
 Encrypt a block using a symmetric sessionkey.
 
ssize_t GNUNET_CRYPTO_symmetric_decrypt (const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
 Decrypt a given block using a symmetric sessionkey.
 
void GNUNET_CRYPTO_aes_ctr (const void *in_buf, size_t in_buf_len, const unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH], const unsigned char iv[GNUNET_CRYPTO_AES_IV_LENGTH], void *out_buf)
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_xsalsa20poly1305_decrypt (size_t in_buf_len, const unsigned char in_buf[in_buf_len], const struct GNUNET_CRYPTO_XSalsa20SecretKey *key, const struct GNUNET_CRYPTO_XSalsa20Nonce *nonce, void *out_buf)
 Encrypt the given data using XSalsa20-Poly1305.
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_xsalsa20poly1305_encrypt (size_t in_buf_len, const unsigned char in_buf[in_buf_len], const struct GNUNET_CRYPTO_XSalsa20SecretKey *key, const struct GNUNET_CRYPTO_XSalsa20Nonce *nonce, void *out_buf)
 Encrypt the given data using XSalsa20-Poly1305.
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_aead_decrypt (size_t ct_len, const unsigned char ct[ct_len], size_t aad_len, const unsigned char aad[aad_len], const struct GNUNET_CRYPTO_AeadSecretKey *key, const struct GNUNET_CRYPTO_AeadNonce *nonce, const struct GNUNET_CRYPTO_AeadMac *mac, void *pt)
 Decrypt the given data using XChaCha20-Poly1305.
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_aead_encrypt (size_t pt_len, const unsigned char pt[pt_len], size_t aad_len, const unsigned char aad[aad_len], const struct GNUNET_CRYPTO_AeadSecretKey *key, const struct GNUNET_CRYPTO_AeadNonce *nonce, void *ct, struct GNUNET_CRYPTO_AeadMac *mac)
 Encrypt the given data using XChaCha20-Poly1305.
 
void GNUNET_CRYPTO_aead_create_key (struct GNUNET_CRYPTO_AeadSecretKey *key)
 Create a new AEAD key.
 

Detailed Description

Symmetric encryption services; combined cipher AES+TWOFISH (256-bit each)

Author
Christian Grothoff
Ioana Patrascu

Definition in file crypto_symmetric.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)
Value:
GNUNET_log_from (kind, "util-crypto-symmetric", \
__VA_ARGS__)
#define GNUNET_log_from(kind, comp,...)

Definition at line 33 of file crypto_symmetric.c.

44{
45 gcry_randomize (key->aes_key,
47 GCRY_STRONG_RANDOM);
48 gcry_randomize (key->twofish_key,
50 GCRY_STRONG_RANDOM);
51}
52
53
62static int
63setup_cipher_aes (gcry_cipher_hd_t *handle,
64 const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
66{
67 int rc;
68
69 GNUNET_assert (0 ==
70 gcry_cipher_open (handle, GCRY_CIPHER_AES256,
71 GCRY_CIPHER_MODE_CFB, 0));
72 rc = gcry_cipher_setkey (*handle,
73 sessionkey->aes_key,
74 sizeof(sessionkey->aes_key));
75 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
76 rc = gcry_cipher_setiv (*handle,
77 iv->aes_iv,
78 sizeof(iv->aes_iv));
79 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
80 return GNUNET_OK;
81}
82
83
92static int
93setup_cipher_twofish (gcry_cipher_hd_t *handle,
94 const struct
96 const struct
98{
99 int rc;
100
101 GNUNET_assert (0 ==
102 gcry_cipher_open (handle, GCRY_CIPHER_TWOFISH,
103 GCRY_CIPHER_MODE_CFB, 0));
104 rc = gcry_cipher_setkey (*handle,
105 sessionkey->twofish_key,
106 sizeof(sessionkey->twofish_key));
107 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
108 rc = gcry_cipher_setiv (*handle,
109 iv->twofish_iv,
110 sizeof(iv->twofish_iv));
111 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
112 return GNUNET_OK;
113}
114
115
116ssize_t
117GNUNET_CRYPTO_symmetric_encrypt (const void *block,
118 size_t size,
119 const struct
121 const struct
123 ,
124 void *result)
125{
126 gcry_cipher_hd_t handle;
127 char tmp[GNUNET_NZL (size)];
128
129 if (GNUNET_OK != setup_cipher_aes (&handle, sessionkey, iv))
130 return -1;
131 GNUNET_assert (0 == gcry_cipher_encrypt (handle, tmp, size, block, size));
132 gcry_cipher_close (handle);
133 if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
134 return -1;
135 GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, size, tmp, size));
136 gcry_cipher_close (handle);
137 memset (tmp, 0, sizeof(tmp));
138 return size;
139}
140
141
142ssize_t
143GNUNET_CRYPTO_symmetric_decrypt (const void *block,
144 size_t size,
145 const struct
147 const struct
149 ,
150 void *result)
151{
152 gcry_cipher_hd_t handle;
153 char tmp[size];
154
155 if (GNUNET_OK != setup_cipher_twofish (&handle, sessionkey, iv))
156 return -1;
157 GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, block, size));
158 gcry_cipher_close (handle);
159 if (GNUNET_OK != setup_cipher_aes (&handle, sessionkey, iv))
160 return -1;
161 GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, tmp, size));
162 gcry_cipher_close (handle);
163 memset (tmp, 0, sizeof(tmp));
164 return size;
165}
166
167
168void
170 const void *in_buf,
171 size_t in_buf_len,
172 const unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH],
173 const unsigned char iv[GNUNET_CRYPTO_AES_IV_LENGTH],
174 void *out_buf)
175{
176 gcry_cipher_hd_t handle;
177 int rc;
178
179 GNUNET_assert (0 == gcry_cipher_open (&handle, GCRY_CIPHER_AES256,
180 GCRY_CIPHER_MODE_CTR, 0));
181 rc = gcry_cipher_setkey (handle,
182 key,
184 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
185 rc = gcry_cipher_setctr (handle,
186 iv,
188 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
189 GNUNET_assert (0 == gcry_cipher_encrypt (handle, out_buf, in_buf_len, in_buf,
190 in_buf_len));
191 gcry_cipher_close (handle);
192}
193
194
197 size_t in_buf_len,
198 const unsigned char in_buf[in_buf_len],
200 const struct GNUNET_CRYPTO_XSalsa20Nonce *nonce,
201 void *out_buf)
202{
203 ssize_t ctlen = in_buf_len - crypto_secretbox_xsalsa20poly1305_MACBYTES;
204 if (ctlen < 0)
205 return GNUNET_SYSERR;
206 if (0 != crypto_secretbox_open_detached (
207 out_buf,
208 in_buf
209 + crypto_secretbox_xsalsa20poly1305_MACBYTES, // Ciphertext
210 in_buf, // Tag
211 ctlen,
212 nonce->nonce,
213 key->key))
214 {
215 return GNUNET_SYSERR;
216 }
217 return GNUNET_OK;
218
219}
220
221
224 size_t in_buf_len,
225 const unsigned char in_buf[in_buf_len],
227 const struct GNUNET_CRYPTO_XSalsa20Nonce *nonce,
228 void *out_buf)
229{
230 if (in_buf_len > crypto_secretbox_xsalsa20poly1305_MESSAGEBYTES_MAX)
231 return GNUNET_SYSERR;
232 crypto_secretbox_detached (out_buf
233 + crypto_secretbox_xsalsa20poly1305_MACBYTES, // Ciphertext
234 out_buf, // TAG
235 in_buf,
236 in_buf_len,
237 nonce->nonce,
238 key->key);
239 return GNUNET_OK;
240}
241
242
245 size_t ct_len,
246 const unsigned char ct[ct_len],
247 size_t aad_len,
248 const unsigned char aad[aad_len],
249 const struct GNUNET_CRYPTO_AeadSecretKey *key,
250 const struct GNUNET_CRYPTO_AeadNonce *nonce,
251 const struct GNUNET_CRYPTO_AeadMac *mac,
252 void *pt)
253{
254 if (0 != crypto_aead_xchacha20poly1305_ietf_decrypt_detached (
255 pt,
256 NULL,
257 ct, // Tag
258 ct_len,
259 mac->mac,
260 aad,
261 aad_len,
262 nonce->npub,
263 key->k))
264 {
265 return GNUNET_SYSERR;
266 }
267 return GNUNET_OK;
268
269}
270
271
274 size_t pt_len,
275 const unsigned char pt[pt_len],
276 size_t aad_len,
277 const unsigned char aad[aad_len],
278 const struct GNUNET_CRYPTO_AeadSecretKey *key,
279 const struct GNUNET_CRYPTO_AeadNonce *nonce,
280 void *ct,
281 struct GNUNET_CRYPTO_AeadMac *mac)
282{
283 crypto_aead_xchacha20poly1305_ietf_encrypt_detached (ct, // Ciphertext
284 mac->mac, // TAG
285 NULL,
286 pt,
287 pt_len,
288 aad,
289 aad_len,
290 NULL,
291 nonce->npub,
292 key->k);
293 return GNUNET_OK;
294}
295
296
297void
299{
300 crypto_aead_xchacha20poly1305_ietf_keygen (key->k);
301}
302
303
304/* end of crypto_symmetric.c */
static int setup_cipher_aes(gcry_cipher_hd_t *handle, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
Initialize AES cipher.
void GNUNET_CRYPTO_aes_ctr(const void *in_buf, size_t in_buf_len, const unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH], const unsigned char iv[GNUNET_CRYPTO_AES_IV_LENGTH], void *out_buf)
static int setup_cipher_twofish(gcry_cipher_hd_t *handle, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
Initialize TWOFISH cipher.
struct GNUNET_HashCode key
The key used in the DHT.
static int result
Global testing status.
static struct GNUNET_VPN_Handle * handle
Handle to vpn service.
Definition gnunet-vpn.c:35
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_aead_decrypt(size_t ct_len, const unsigned char ct[ct_len], size_t aad_len, const unsigned char aad[aad_len], const struct GNUNET_CRYPTO_AeadSecretKey *key, const struct GNUNET_CRYPTO_AeadNonce *nonce, const struct GNUNET_CRYPTO_AeadMac *mac, void *pt)
Decrypt the given data using XChaCha20-Poly1305.
ssize_t GNUNET_CRYPTO_symmetric_encrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Encrypt a block using a symmetric sessionkey.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_aead_encrypt(size_t pt_len, const unsigned char pt[pt_len], size_t aad_len, const unsigned char aad[aad_len], const struct GNUNET_CRYPTO_AeadSecretKey *key, const struct GNUNET_CRYPTO_AeadNonce *nonce, void *ct, struct GNUNET_CRYPTO_AeadMac *mac)
Encrypt the given data using XChaCha20-Poly1305.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_xsalsa20poly1305_decrypt(size_t in_buf_len, const unsigned char in_buf[in_buf_len], const struct GNUNET_CRYPTO_XSalsa20SecretKey *key, const struct GNUNET_CRYPTO_XSalsa20Nonce *nonce, void *out_buf)
Encrypt the given data using XSalsa20-Poly1305.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_xsalsa20poly1305_encrypt(size_t in_buf_len, const unsigned char in_buf[in_buf_len], const struct GNUNET_CRYPTO_XSalsa20SecretKey *key, const struct GNUNET_CRYPTO_XSalsa20Nonce *nonce, void *out_buf)
Encrypt the given data using XSalsa20-Poly1305.
ssize_t GNUNET_CRYPTO_symmetric_decrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Decrypt a given block using a symmetric sessionkey.
#define GNUNET_CRYPTO_AES_IV_LENGTH
length of an AES key in bytes
#define GNUNET_NZL(l)
Macro used to avoid using 0 for the length of a variable-size array (Non-Zero-Length).
#define GNUNET_CRYPTO_AES_KEY_LENGTH
length of the sessionkey in bytes
void GNUNET_CRYPTO_aead_create_key(struct GNUNET_CRYPTO_AeadSecretKey *key)
Create a new AEAD key.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static unsigned int size
Size of the "table".
Definition peer.c:68
type for session keys
unsigned char mac[16]
Initialization vector.
type for session keys
unsigned char npub[24]
Initialization vector.
unsigned char twofish_key[(256/8)]
Actual key for TwoFish.
unsigned char aes_key[(256/8)]
Actual key for AES.
unsigned char nonce[24]
Initialization vector.

Function Documentation

◆ setup_cipher_aes()

static int setup_cipher_aes ( gcry_cipher_hd_t *  handle,
const struct GNUNET_CRYPTO_SymmetricSessionKey sessionkey,
const struct GNUNET_CRYPTO_SymmetricInitializationVector iv 
)
static

Initialize AES cipher.

Parameters
handlehandle to initialize
sessionkeysession key to use
ivinitialization vector to use
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 64 of file crypto_symmetric.c.

67{
68 int rc;
69
70 GNUNET_assert (0 ==
71 gcry_cipher_open (handle, GCRY_CIPHER_AES256,
72 GCRY_CIPHER_MODE_CFB, 0));
73 rc = gcry_cipher_setkey (*handle,
74 sessionkey->aes_key,
75 sizeof(sessionkey->aes_key));
76 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
77 rc = gcry_cipher_setiv (*handle,
78 iv->aes_iv,
79 sizeof(iv->aes_iv));
80 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
81 return GNUNET_OK;
82}

References GNUNET_CRYPTO_SymmetricInitializationVector::aes_iv, GNUNET_CRYPTO_SymmetricSessionKey::aes_key, GNUNET_assert, GNUNET_OK, and handle.

Referenced by GNUNET_CRYPTO_symmetric_decrypt(), and GNUNET_CRYPTO_symmetric_encrypt().

Here is the caller graph for this function:

◆ setup_cipher_twofish()

static int setup_cipher_twofish ( gcry_cipher_hd_t *  handle,
const struct GNUNET_CRYPTO_SymmetricSessionKey sessionkey,
const struct GNUNET_CRYPTO_SymmetricInitializationVector iv 
)
static

Initialize TWOFISH cipher.

Parameters
handlehandle to initialize
sessionkeysession key to use
ivinitialization vector to use
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 94 of file crypto_symmetric.c.

99{
100 int rc;
101
102 GNUNET_assert (0 ==
103 gcry_cipher_open (handle, GCRY_CIPHER_TWOFISH,
104 GCRY_CIPHER_MODE_CFB, 0));
105 rc = gcry_cipher_setkey (*handle,
106 sessionkey->twofish_key,
107 sizeof(sessionkey->twofish_key));
108 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
109 rc = gcry_cipher_setiv (*handle,
110 iv->twofish_iv,
111 sizeof(iv->twofish_iv));
112 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
113 return GNUNET_OK;
114}

References GNUNET_assert, GNUNET_OK, handle, GNUNET_CRYPTO_SymmetricInitializationVector::twofish_iv, and GNUNET_CRYPTO_SymmetricSessionKey::twofish_key.

Referenced by GNUNET_CRYPTO_symmetric_decrypt(), and GNUNET_CRYPTO_symmetric_encrypt().

Here is the caller graph for this function:

◆ GNUNET_CRYPTO_aes_ctr()

void GNUNET_CRYPTO_aes_ctr ( const void *  in_buf,
size_t  in_buf_len,
const unsigned char  key[GNUNET_CRYPTO_AES_KEY_LENGTH],
const unsigned char  iv[GNUNET_CRYPTO_AES_IV_LENGTH],
void *  out_buf 
)

Definition at line 170 of file crypto_symmetric.c.

176{
177 gcry_cipher_hd_t handle;
178 int rc;
179
180 GNUNET_assert (0 == gcry_cipher_open (&handle, GCRY_CIPHER_AES256,
181 GCRY_CIPHER_MODE_CTR, 0));
182 rc = gcry_cipher_setkey (handle,
183 key,
185 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
186 rc = gcry_cipher_setctr (handle,
187 iv,
189 GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
190 GNUNET_assert (0 == gcry_cipher_encrypt (handle, out_buf, in_buf_len, in_buf,
191 in_buf_len));
192 gcry_cipher_close (handle);
193}

References GNUNET_assert, GNUNET_CRYPTO_AES_IV_LENGTH, GNUNET_CRYPTO_AES_KEY_LENGTH, handle, and key.