GNUnet 0.27.0-17-g14611e095
 
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 unsigned char key[GNUNET_CRYPTO_XSALSA20_KEY_LENGTH], const unsigned char nonce[GNUNET_CRYPTO_XSALSA20_IV_LENGTH], void *out_buf)
 
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_xsalsa20poly1305_encrypt (size_t in_buf_len, const unsigned char in_buf[in_buf_len], const unsigned char key[GNUNET_CRYPTO_XSALSA20_KEY_LENGTH], const unsigned char nonce[GNUNET_CRYPTO_XSALSA20_IV_LENGTH], void *out_buf)
 

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],
199 const unsigned char key[GNUNET_CRYPTO_XSALSA20_KEY_LENGTH],
200 const unsigned char nonce[GNUNET_CRYPTO_XSALSA20_IV_LENGTH],
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, key))
213 {
214 return GNUNET_SYSERR;
215 }
216 return GNUNET_OK;
217
218}
219
220
223 size_t in_buf_len,
224 const unsigned char in_buf[in_buf_len],
225 const unsigned char key[GNUNET_CRYPTO_XSALSA20_KEY_LENGTH],
226 const unsigned char nonce[GNUNET_CRYPTO_XSALSA20_IV_LENGTH],
227 void *out_buf)
228{
229 if (in_buf_len > crypto_secretbox_xsalsa20poly1305_MESSAGEBYTES_MAX)
230 return GNUNET_SYSERR;
231 crypto_secretbox_detached (out_buf
232 + crypto_secretbox_xsalsa20poly1305_MACBYTES, // Ciphertext
233 out_buf, // TAG
234 in_buf,
235 in_buf_len,
236 nonce,
237 key);
238 return GNUNET_OK;
239}
240
241
242/* 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.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_xsalsa20poly1305_decrypt(size_t in_buf_len, const unsigned char in_buf[in_buf_len], const unsigned char key[GNUNET_CRYPTO_XSALSA20_KEY_LENGTH], const unsigned char nonce[GNUNET_CRYPTO_XSALSA20_IV_LENGTH], void *out_buf)
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_encrypt(size_t in_buf_len, const unsigned char in_buf[in_buf_len], const unsigned char key[GNUNET_CRYPTO_XSALSA20_KEY_LENGTH], const unsigned char nonce[GNUNET_CRYPTO_XSALSA20_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
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.
#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
#define GNUNET_CRYPTO_XSALSA20_KEY_LENGTH
length of an XSalsa20 key in bytes
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_CRYPTO_XSALSA20_IV_LENGTH
length of an XSALSA20 IV in bytes
@ 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
unsigned char twofish_key[(256/8)]
Actual key for TwoFish.
unsigned char aes_key[(256/8)]
Actual key for AES.

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.

◆ GNUNET_CRYPTO_xsalsa20poly1305_decrypt()

enum GNUNET_GenericReturnValue GNUNET_CRYPTO_xsalsa20poly1305_decrypt ( size_t  in_buf_len,
const unsigned char  in_buf[in_buf_len],
const unsigned char  key[GNUNET_CRYPTO_XSALSA20_KEY_LENGTH],
const unsigned char  nonce[GNUNET_CRYPTO_XSALSA20_IV_LENGTH],
void *  out_buf 
)

Definition at line 197 of file crypto_symmetric.c.

203{
204 ssize_t ctlen = in_buf_len - crypto_secretbox_xsalsa20poly1305_MACBYTES;
205 if (ctlen < 0)
206 return GNUNET_SYSERR;
207 if (0 != crypto_secretbox_open_detached (
208 out_buf,
209 in_buf
210 + crypto_secretbox_xsalsa20poly1305_MACBYTES, // Ciphertext
211 in_buf, // Tag
212 ctlen,
213 nonce, key))
214 {
215 return GNUNET_SYSERR;
216 }
217 return GNUNET_OK;
218
219}

References GNUNET_OK, GNUNET_SYSERR, and key.

◆ GNUNET_CRYPTO_xsalsa20poly1305_encrypt()

enum GNUNET_GenericReturnValue GNUNET_CRYPTO_xsalsa20poly1305_encrypt ( size_t  in_buf_len,
const unsigned char  in_buf[in_buf_len],
const unsigned char  key[GNUNET_CRYPTO_XSALSA20_KEY_LENGTH],
const unsigned char  nonce[GNUNET_CRYPTO_XSALSA20_IV_LENGTH],
void *  out_buf 
)

Definition at line 223 of file crypto_symmetric.c.

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,
238 key);
239 return GNUNET_OK;
240}

References GNUNET_OK, GNUNET_SYSERR, and key.