gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnunet] branch master updated: NEWS: -


From: gnunet
Subject: [gnunet] branch master updated: NEWS: -
Date: Mon, 17 Jul 2023 09:51:46 +0200

This is an automated email from the git hooks/post-receive script.

martin-schanzenbach pushed a commit to branch master
in repository gnunet.

The following commit(s) were added to refs/heads/master by this push:
     new 7e08ec566 NEWS: -
7e08ec566 is described below

commit 7e08ec5663903e8a1bbfeeee214d8b2d1ab07f15
Author: Martin Schanzenbach <schanzen@gnunet.org>
AuthorDate: Mon Jul 17 09:51:22 2023 +0200

    NEWS: -
    
    Safer API for IDENTITY encryption
---
 src/identity/gnunet-identity.c        | 29 +++++++++++++--------------
 src/identity/identity_api.c           | 37 ++++++++++++++++++++++++-----------
 src/identity/test_identity.c          | 20 +++++++++++--------
 src/include/gnunet_identity_service.h | 21 ++++++++++----------
 4 files changed, 62 insertions(+), 45 deletions(-)

diff --git a/src/identity/gnunet-identity.c b/src/identity/gnunet-identity.c
index 59354a2e1..7adc86eb8 100644
--- a/src/identity/gnunet-identity.c
+++ b/src/identity/gnunet-identity.c
@@ -259,20 +259,20 @@ static void
 write_encrypted_message (void)
 {
   struct GNUNET_IDENTITY_PublicKey recipient;
-  unsigned char ct[strlen (write_msg) + 1024];
+  size_t ct_len = strlen (write_msg) + 1
+                  + GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES;
+  unsigned char ct[ct_len];
   if (GNUNET_IDENTITY_public_key_from_string (pubkey_msg, &recipient) !=
       GNUNET_SYSERR)
   {
     size_t msg_len = strlen (write_msg) + 1;
-    ssize_t res = GNUNET_IDENTITY_encrypt (write_msg,
-                                           msg_len,
-                                           &recipient,
-                                           ct, strlen (write_msg) + 1024);
-    if (-1 != res)
+    if (GNUNET_OK == GNUNET_IDENTITY_encrypt (write_msg,
+                                              msg_len,
+                                              &recipient,
+                                              ct, ct_len))
     {
       char *serialized_msg;
-      serialized_msg = GNUNET_STRINGS_data_to_string_alloc (ct,
-                                                            res);
+      serialized_msg = GNUNET_STRINGS_data_to_string_alloc (ct, ct_len);
       fprintf (stdout,
                "%s\n",
                serialized_msg);
@@ -309,14 +309,13 @@ read_encrypted_message (struct GNUNET_IDENTITY_Ego *ego)
                                                         deserialized_msg,
                                                         &msg_len))
   {
-    ssize_t res = GNUNET_IDENTITY_decrypt (deserialized_msg,
-                                           msg_len,
-                                           GNUNET_IDENTITY_ego_get_private_key 
(
-                                             ego),
-                                           deserialized_msg, msg_len);
-    if (-1 != res)
+    if (GNUNET_OK == GNUNET_IDENTITY_decrypt (deserialized_msg,
+                                              msg_len,
+                                              
GNUNET_IDENTITY_ego_get_private_key (
+                                                ego),
+                                              deserialized_msg, msg_len))
     {
-      deserialized_msg[res - 1] = '\0';
+      deserialized_msg[msg_len - 1] = '\0';
       fprintf (stdout,
                "%s\n",
                deserialized_msg);
diff --git a/src/identity/identity_api.c b/src/identity/identity_api.c
index 8a7e5c10b..81e318d19 100644
--- a/src/identity/identity_api.c
+++ b/src/identity/identity_api.c
@@ -1174,7 +1174,7 @@ GNUNET_IDENTITY_encrypt_old (const void *block,
 }
 
 
-ssize_t
+enum GNUNET_GenericReturnValue
 GNUNET_IDENTITY_encrypt (const void *pt,
                          size_t pt_size,
                          const struct GNUNET_IDENTITY_PublicKey *pub,
@@ -1187,33 +1187,40 @@ GNUNET_IDENTITY_encrypt (const void *pt,
   unsigned char nonce[crypto_secretbox_NONCEBYTES];
   unsigned char key[crypto_secretbox_KEYBYTES];
 
+  if (ct_size < pt_size + GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Output buffer size for ciphertext too small: Got %lu, want 
>=%lu\n",
+                ct_size, pt_size + GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES);
+    return GNUNET_SYSERR;
+  }
   switch (ntohl (pub->type))
   {
   case GNUNET_IDENTITY_TYPE_ECDSA:
     if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_fo_kem_encaps (&(pub->ecdsa_key),
                                                             kemc,
                                                             &k))
-      return -1;
+      return GNUNET_SYSERR;
     break;
   case GNUNET_IDENTITY_TYPE_EDDSA:
     if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_fo_kem_encaps (&pub->eddsa_key,
                                                             kemc,
                                                             &k))
-      return -1;
+      return GNUNET_SYSERR;
     break;
   default:
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported key type\n");
-    return -1;
+    return GNUNET_SYSERR;
   }
   memcpy (key, &k, crypto_secretbox_KEYBYTES);
   memcpy (nonce, ((char* ) &k) + crypto_secretbox_KEYBYTES,
           crypto_secretbox_NONCEBYTES);
   crypto_secretbox_easy (encrypted_data, pt, pt_size, nonce, key);
-  return pt_size + crypto_secretbox_MACBYTES + sizeof (*kemc);
+  return GNUNET_OK;
 }
 
 
-ssize_t
+enum GNUNET_GenericReturnValue
 GNUNET_IDENTITY_decrypt (const void *ct_buf,
                          size_t ct_size,
                          const struct GNUNET_IDENTITY_PrivateKey *priv,
@@ -1225,31 +1232,39 @@ GNUNET_IDENTITY_decrypt (const void *ct_buf,
   unsigned char *encrypted_data = (unsigned char*) &kemc[1];
   unsigned char nonce[crypto_secretbox_NONCEBYTES];
   unsigned char key[crypto_secretbox_KEYBYTES];
+  size_t expected_pt_len = ct_size - GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES;
 
+  if (pt_size < expected_pt_len)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Output buffer size for plaintext too small: Got %lu, want 
>=%lu\n",
+                pt_size, expected_pt_len);
+    return GNUNET_SYSERR;
+  }
   switch (ntohl (priv->type))
   {
   case GNUNET_IDENTITY_TYPE_ECDSA:
     if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_fo_kem_decaps (&(priv->ecdsa_key),
                                                             kemc,
                                                             &k))
-      return -1;
+      return GNUNET_SYSERR;
     break;
   case GNUNET_IDENTITY_TYPE_EDDSA:
     if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_fo_kem_decaps (&(priv->eddsa_key),
                                                             kemc,
                                                             &k))
-      return -1;
+      return GNUNET_SYSERR;
     break;
   default:
-    return -1;
+    return GNUNET_SYSERR;
   }
   memcpy (key, &k, crypto_secretbox_KEYBYTES);
   memcpy (nonce, ((char* ) &k) + crypto_secretbox_KEYBYTES,
           crypto_secretbox_NONCEBYTES);
   if (crypto_secretbox_open_easy (pt, encrypted_data, ct_size - sizeof (*kemc),
                                   nonce, key))
-    return -1;
-  return ct_size - sizeof (*kemc) - crypto_secretbox_MACBYTES;
+    return GNUNET_SYSERR;
+  return GNUNET_OK;
 }
 
 
diff --git a/src/identity/test_identity.c b/src/identity/test_identity.c
index b9ec59ab3..92c074981 100644
--- a/src/identity/test_identity.c
+++ b/src/identity/test_identity.c
@@ -259,16 +259,20 @@ create_cb (void *cls,
   CHECK (NULL != pk);
   CHECK (GNUNET_EC_NONE == ec);
   struct GNUNET_IDENTITY_PublicKey pub;
-  unsigned char ct[1024];
-  char pt[strlen ("test") + 1];
-  ssize_t len;
+  size_t pt_len = strlen ("test") + 1;
+  unsigned char ct[pt_len + GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES];
+  char pt[pt_len];
+  enum GNUNET_GenericReturnValue res;
 
   GNUNET_IDENTITY_key_get_public (pk, &pub);
-  len = GNUNET_IDENTITY_encrypt ("test", strlen ("test") + 1, &pub, ct,
-                                 sizeof(ct));
-  CHECK (-1 != len);
-  GNUNET_IDENTITY_decrypt (ct, len, pk, pt, sizeof (pt));
-  CHECK (-1 != len);
+  res = GNUNET_IDENTITY_encrypt ("test", pt_len, &pub, ct,
+                                 pt_len
+                                 + GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES);
+  CHECK (GNUNET_OK == res);
+  res = GNUNET_IDENTITY_decrypt (ct, pt_len
+                                 + GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES,
+                                 pk, pt, pt_len);
+  CHECK (GNUNET_OK == res);
   CHECK (0 == strcmp (pt, "test"));
   op =
     GNUNET_IDENTITY_rename (h, "test-id", "test", &success_rename_cont, NULL);
diff --git a/src/include/gnunet_identity_service.h 
b/src/include/gnunet_identity_service.h
index db39a92eb..fd0458f62 100644
--- a/src/include/gnunet_identity_service.h
+++ b/src/include/gnunet_identity_service.h
@@ -45,6 +45,7 @@
 #ifndef GNUNET_IDENTITY_SERVICE_H
 #define GNUNET_IDENTITY_SERVICE_H
 
+#include "gnunet_common.h"
 #ifdef __cplusplus
 extern "C" {
 #if 0 /* keep Emacsens' auto-indent happy */
@@ -750,26 +751,26 @@ GNUNET_IDENTITY_decrypt_old (
   const struct GNUNET_CRYPTO_EcdhePublicKey *ecc,
   void *result);
 
+#define GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES (crypto_secretbox_MACBYTES \
+                                                + sizeof (struct \
+                                                          
GNUNET_CRYPTO_FoKemC))
+
 /**
  * Encrypt a block with #GNUNET_IDENTITY_PublicKey and derives a
  * #GNUNET_CRYPTO_EcdhePublicKey which is required for decryption
  * using ecdh to derive a symmetric key.
  *
  * Note that the result buffer for the ciphertext must be the length of
- * the message to encrypt plus:
- * - Length of a struct GNUNET_CRYPTO_FoKemC
- * - the authentication tag of libsodium, e.g. crypto_secretbox_NONCEBYTES
+ * the message to encrypt plus #GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES.
  *
  * @param block the block to encrypt
  * @param size the size of the @a block
  * @param pub public key to encrypt for
  * @param result the output parameter in which to store the encrypted result
  *               can be the same or overlap with @c block
- * @returns the size of the encrypted block, -1 for errors.
- *          Due to the use of CFB and therefore an effective stream cipher,
- *          this size should be the same as @c len.
+ * @returns GNUNET_OK on success.
  */
-ssize_t
+enum GNUNET_GenericReturnValue
 GNUNET_IDENTITY_encrypt (const void *block,
                          size_t size,
                          const struct GNUNET_IDENTITY_PublicKey *pub,
@@ -786,11 +787,9 @@ GNUNET_IDENTITY_encrypt (const void *block,
  * @param priv private key to use for ecdh
  * @param result address to store the result at
  *               can be the same or overlap with @c block
- * @return -1 on failure, size of decrypted block on success.
- *         Due to the use of CFB and therefore an effective stream cipher,
- *         this size should be the same as @c size.
+ * @returns GNUNET_OK on success.
  */
-ssize_t
+enum GNUNET_GenericReturnValue
 GNUNET_IDENTITY_decrypt (const void *block,
                          size_t size,
                          const struct GNUNET_IDENTITY_PrivateKey *priv,

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]