Code

network plugin: Use CBC rather than ECB.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 11 Apr 2009 00:02:33 +0000 (02:02 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 11 Apr 2009 00:02:33 +0000 (02:02 +0200)
src/network.c
src/network.h

index ef2ba1f6eb688b763df258ec98f890bb8b7080d4..bbe6b6fd7f58600be4f60982ba1605a58e6837ab 100644 (file)
@@ -189,10 +189,6 @@ typedef struct receive_list_entry_s receive_list_entry_t;
 /*
  * Private variables
  */
-#if HAVE_GCRYPT_H
-static char network_encryption_iv[] = NET_ENCR_IV;
-#endif /* HAVE_GCRYPT_H */
-
 static int network_config_ttl = 0;
 static int network_config_forward = 0;
 
@@ -788,6 +784,7 @@ static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
   err = gcry_cipher_decrypt (se->cypher,
       buffer + sizeof (pea.head), buffer_len - sizeof (pea.head),
       /* in = */ NULL, /* in len = */ 0);
+  gcry_cipher_reset (se->cypher);
   if (err != 0)
   {
     ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
@@ -1193,8 +1190,16 @@ static int network_set_encryption (sockent_t *se, /* {{{ */
 
   se->shared_secret = sstrdup (shared_secret);
 
+  /*
+   * We use CBC *without* an initialization vector: The cipher is reset after
+   * each packet and we would have to re-set the IV each time. The first
+   * encrypted block will contain the SHA-224 checksum anyway, so this should
+   * be quite unpredictable. Also, there's a 2 byte field in the header that's
+   * being filled with random numbers. So we only use CBC so the blocks
+   * *within* one packet are chained.
+   */
   err = gcry_cipher_open (&se->cypher,
-      GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_ECB, /* flags = */ 0);
+      GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, /* flags = */ 0);
   if (err != 0)
   {
     ERROR ("network plugin: gcry_cipher_open returned: %s",
@@ -1202,17 +1207,6 @@ static int network_set_encryption (sockent_t *se, /* {{{ */
     return (-1);
   }
 
-  err = gcry_cipher_setiv (se->cypher, network_encryption_iv,
-      sizeof (network_encryption_iv));
-  if (err != 0)
-  {
-    ERROR ("network plugin: gcry_cipher_setiv returned: %s",
-        gcry_strerror (err));
-    gcry_cipher_close (se->cypher);
-    se->cypher = NULL;
-    return (-1);
-  }
-
   assert (se->shared_secret != NULL);
   gcry_md_hash_buffer (GCRY_MD_SHA256, hash,
       se->shared_secret, strlen (se->shared_secret));
@@ -1875,6 +1869,7 @@ static void networt_send_buffer_encrypted (const sockent_t *se, /* {{{ */
   err = gcry_cipher_encrypt (se->cypher,
       buffer + sizeof (pea.head), buffer_size - sizeof (pea.head),
       /* in = */ NULL, /* in len = */ 0);
+  gcry_cipher_reset (se->cypher);
   if (err != 0)
   {
     ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
index baca2d307abad6b01409a232966a9de78b3749e7..777616c4725cc75ddb13f9844e5c833aa81e7e35 100644 (file)
@@ -70,8 +70,4 @@
 #define TYPE_SIGN_SHA256     0x0200
 #define TYPE_ENCR_AES256     0x0210
 
-#define NET_ENCR_IV { \
-       0xd6, 0x0c, 0x90, 0xc2, 0x23, 0xdd, 0x3e, 0xcc, \
-       0xc9, 0x86, 0xe9, 0xb6, 0xe0, 0x15, 0xb7, 0x39 }
-
 #endif /* NETWORK_H */