[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnunet] branch master updated: NEWS: Added new CCA-secure KEM and use i
From: |
gnunet |
Subject: |
[gnunet] branch master updated: NEWS: Added new CCA-secure KEM and use in IDENTITY encryption |
Date: |
Sun, 16 Jul 2023 19:27:03 +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 a2c3a72d0 NEWS: Added new CCA-secure KEM and use in IDENTITY encryption
a2c3a72d0 is described below
commit a2c3a72d0157c0de4966cea417d1802727c3ed1e
Author: Martin Schanzenbach <schanzen@gnunet.org>
AuthorDate: Sun Jul 16 19:26:09 2023 +0200
NEWS: Added new CCA-secure KEM and use in IDENTITY encryption
---
src/identity/gnunet-identity.c | 103 ++-----
src/identity/identity_api.c | 99 ++++++-
src/identity/test_identity.c | 30 +-
src/identity/test_identity_messages.sh | 21 +-
src/include/gnunet_crypto_lib.h | 114 +++++++-
src/include/gnunet_identity_service.h | 61 +++-
src/messenger/messenger_api_message.c | 492 +++++++++++++++++++--------------
src/util/crypto_ecc.c | 104 +++++--
8 files changed, 674 insertions(+), 350 deletions(-)
diff --git a/src/identity/gnunet-identity.c b/src/identity/gnunet-identity.c
index 06e6fb613..c4cae78ca 100644
--- a/src/identity/gnunet-identity.c
+++ b/src/identity/gnunet-identity.c
@@ -259,29 +259,23 @@ static void
write_encrypted_message (void)
{
struct GNUNET_IDENTITY_PublicKey recipient;
+ unsigned char ct[strlen (write_msg) + 1024];
if (GNUNET_IDENTITY_public_key_from_string (pubkey_msg, &recipient) !=
GNUNET_SYSERR)
{
- struct GNUNET_CRYPTO_EcdhePublicKey message_key;
size_t msg_len = strlen (write_msg) + 1;
- ssize_t res = GNUNET_IDENTITY_encrypt (write_msg,
- msg_len,
- &recipient,
- &message_key,
- write_msg);
+ ssize_t res = GNUNET_IDENTITY_encrypt2 (write_msg,
+ msg_len,
+ &recipient,
+ ct, strlen (write_msg) + 1024);
if (-1 != res)
{
- char *keystr;
char *serialized_msg;
- keystr = GNUNET_STRINGS_data_to_string_alloc (&message_key,
- sizeof(struct
-
GNUNET_CRYPTO_EcdhePublicKey));
- serialized_msg = GNUNET_STRINGS_data_to_string_alloc (write_msg,
- msg_len);
+ serialized_msg = GNUNET_STRINGS_data_to_string_alloc (ct,
+ res);
fprintf (stdout,
- "%s.%s\n",
- keystr, serialized_msg);
- GNUNET_free (keystr);
+ "%s\n",
+ serialized_msg);
GNUNET_free (serialized_msg);
}
else
@@ -307,75 +301,36 @@ write_encrypted_message (void)
static void
read_encrypted_message (struct GNUNET_IDENTITY_Ego *ego)
{
- // message contains ECDHE key and ciphertext divided by ".", so split up
first
- char delim[2] = ".";
- char *key_msg = strtok (read_msg, delim);
- char *cipher;
- if (NULL == key_msg)
+ char *deserialized_msg;
+ size_t msg_len;
+ if (GNUNET_OK == GNUNET_STRINGS_string_to_data_alloc (read_msg, strlen (
+ read_msg),
+ (void **) &
+ deserialized_msg,
+ &msg_len))
{
- fprintf (stderr, "Invalid message format.\n");
- global_ret = 1;
- return;
- }
- cipher = strtok (NULL, delim);
- if (NULL == cipher)
- {
- fprintf (stderr, "Invalid message format, text missing.\n");
- global_ret = 1;
- return;
- }
-
- if (NULL != strtok (NULL, delim))
- {
- fprintf (stderr,
- "Invalid message format, expecting only key and cipher
components.\n");
- global_ret = 1;
- return;
- }
-
- struct GNUNET_CRYPTO_EcdhePublicKey message_key;
- if (GNUNET_OK == GNUNET_STRINGS_string_to_data (key_msg, strlen (
- key_msg),
- &message_key,
- sizeof(message_key)))
- {
- char *deserialized_msg;
- size_t msg_len;
- if (GNUNET_OK == GNUNET_STRINGS_string_to_data_alloc (cipher, strlen (
- cipher),
- (void **) &
- deserialized_msg,
- &msg_len))
+ ssize_t res = GNUNET_IDENTITY_decrypt2 (deserialized_msg,
+ msg_len,
+
GNUNET_IDENTITY_ego_get_private_key (
+ ego),
+ deserialized_msg, msg_len);
+ if (-1 != res)
{
- ssize_t res = GNUNET_IDENTITY_decrypt (deserialized_msg,
- msg_len,
-
GNUNET_IDENTITY_ego_get_private_key (
- ego),
- &message_key,
- deserialized_msg);
- if (-1 != res)
- {
- deserialized_msg[res - 1] = '\0';
- fprintf (stdout,
- "%s\n",
- deserialized_msg);
- }
- else
- {
- fprintf (stderr, "Failed to decrypt message.\n");
- global_ret = 1;
- }
- GNUNET_free (deserialized_msg);
+ deserialized_msg[res - 1] = '\0';
+ fprintf (stdout,
+ "%s\n",
+ deserialized_msg);
}
else
{
- fprintf (stderr, "Invalid message format.\n");
+ fprintf (stderr, "Failed to decrypt message.\n");
global_ret = 1;
}
+ GNUNET_free (deserialized_msg);
}
else
{
- fprintf (stderr, "Invalid message ephemeral key.\n");
+ fprintf (stderr, "Invalid message format.\n");
global_ret = 1;
}
}
diff --git a/src/identity/identity_api.c b/src/identity/identity_api.c
index 47a78e2bb..0731fc06f 100644
--- a/src/identity/identity_api.c
+++ b/src/identity/identity_api.c
@@ -1136,11 +1136,11 @@ GNUNET_IDENTITY_signature_verify_raw_ (uint32_t purpose,
ssize_t
-GNUNET_IDENTITY_encrypt (const void *block,
- size_t size,
- const struct GNUNET_IDENTITY_PublicKey *pub,
- struct GNUNET_CRYPTO_EcdhePublicKey *ecc,
- void *result)
+GNUNET_IDENTITY_encrypt_old (const void *block,
+ size_t size,
+ const struct GNUNET_IDENTITY_PublicKey *pub,
+ struct GNUNET_CRYPTO_EcdhePublicKey *ecc,
+ void *result)
{
struct GNUNET_CRYPTO_EcdhePrivateKey pk;
GNUNET_CRYPTO_ecdhe_key_create (&pk);
@@ -1175,11 +1175,90 @@ GNUNET_IDENTITY_encrypt (const void *block,
ssize_t
-GNUNET_IDENTITY_decrypt (const void *block,
- size_t size,
- const struct GNUNET_IDENTITY_PrivateKey *priv,
- const struct GNUNET_CRYPTO_EcdhePublicKey *ecc,
- void *result)
+GNUNET_IDENTITY_encrypt2 (const void *pt,
+ size_t pt_size,
+ const struct GNUNET_IDENTITY_PublicKey *pub,
+ void *ct_buf,
+ size_t ct_size)
+{
+ struct GNUNET_HashCode k;
+ struct GNUNET_CRYPTO_FoKemC *kemc = (struct GNUNET_CRYPTO_FoKemC*) ct_buf;
+ unsigned char *encrypted_data = (unsigned char*) &kemc[1];
+ unsigned char nonce[crypto_secretbox_NONCEBYTES];
+ unsigned char key[crypto_secretbox_KEYBYTES];
+
+ 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;
+ break;
+ case GNUNET_IDENTITY_TYPE_EDDSA:
+ if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_fo_kem_encaps (&pub->eddsa_key,
+ kemc,
+ &k))
+ return -1;
+ break;
+ default:
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported key type\n");
+ return -1;
+ }
+ 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);
+}
+
+
+ssize_t
+GNUNET_IDENTITY_decrypt2 (const void *ct_buf,
+ size_t ct_size,
+ const struct GNUNET_IDENTITY_PrivateKey *priv,
+ void *pt,
+ size_t pt_size)
+{
+ struct GNUNET_HashCode k;
+ struct GNUNET_CRYPTO_FoKemC *kemc = (struct GNUNET_CRYPTO_FoKemC*) ct_buf;
+ unsigned char *encrypted_data = (unsigned char*) &kemc[1];
+ unsigned char nonce[crypto_secretbox_NONCEBYTES];
+ unsigned char key[crypto_secretbox_KEYBYTES];
+
+ 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;
+ break;
+ case GNUNET_IDENTITY_TYPE_EDDSA:
+ if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_fo_kem_decaps (&(priv->eddsa_key),
+ kemc,
+ &k))
+ return -1;
+ break;
+ default:
+ return -1;
+ }
+ 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;
+}
+
+
+ssize_t
+GNUNET_IDENTITY_decrypt_old (const void *block,
+ size_t size,
+ const struct GNUNET_IDENTITY_PrivateKey *priv,
+ const struct GNUNET_CRYPTO_EcdhePublicKey *ecc,
+ void *result)
{
struct GNUNET_HashCode hash;
switch (ntohl (priv->type))
diff --git a/src/identity/test_identity.c b/src/identity/test_identity.c
index 2c44f73ba..af17b5bcc 100644
--- a/src/identity/test_identity.c
+++ b/src/identity/test_identity.c
@@ -53,15 +53,15 @@ static struct GNUNET_IDENTITY_Operation *op;
static struct GNUNET_SCHEDULER_Task *endbadly_task;
#define CHECK(cond) \
- do \
- { \
- if (! (cond)) \
- { \
- GNUNET_break (0); \
- end (); \
- return; \
- } \
- } while (0)
+ do \
+ { \
+ if (! (cond)) \
+ { \
+ GNUNET_break (0); \
+ end (); \
+ return; \
+ } \
+ } while (0)
/**
@@ -258,6 +258,18 @@ 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;
+
+ GNUNET_IDENTITY_key_get_public (pk, &pub);
+ len = GNUNET_IDENTITY_encrypt2 ("test", strlen ("test") + 1, &pub, ct,
+ sizeof(ct));
+ CHECK (-1 != len);
+ GNUNET_IDENTITY_decrypt2 (ct, len, pk, pt, sizeof (pt));
+ CHECK (-1 != len);
+ CHECK (0 == strcmp (pt, "test"));
op =
GNUNET_IDENTITY_rename (h, "test-id", "test", &success_rename_cont, NULL);
}
diff --git a/src/identity/test_identity_messages.sh
b/src/identity/test_identity_messages.sh
index d29d14acc..edb4d5805 100755
--- a/src/identity/test_identity_messages.sh
+++ b/src/identity/test_identity_messages.sh
@@ -20,10 +20,21 @@ which timeout >/dev/null 2>&1 && DO_TIMEOUT="timeout 30"
TEST_MSG="This is a test message. 123"
gnunet-arm -s -c test_identity.conf
gnunet-identity -C recipientego -c test_identity.conf
+gnunet-identity -C recipientegoed -X -c test_identity.conf
RECIPIENT_KEY=`gnunet-identity -d -e recipientego -q -c test_identity.conf`
MSG_ENC=`gnunet-identity -W "$TEST_MSG" -k $RECIPIENT_KEY -c
test_identity.conf`
-MSG_DEC=`gnunet-identity -R "$MSG_ENC" -e recipientego -c test_identity.conf`
+if [ $? == 0 ]
+then
+ MSG_DEC=`gnunet-identity -R "$MSG_ENC" -e recipientego -c test_identity.conf`
+fi
+RECIPIENT_KEY_ED=`gnunet-identity -d -e recipientegoed -q -c
test_identity.conf`
+MSG_ENC_ED=`gnunet-identity -W "$TEST_MSG" -k $RECIPIENT_KEY_ED -c
test_identity.conf`
+if [ $? == 0 ]
+then
+ MSG_DEC_ED=`gnunet-identity -R "$MSG_ENC_ED" -e recipientegoed -c
test_identity.conf`
+fi
gnunet-identity -D recipientego -c test_identity.conf
+gnunet-identity -D recipientegoed -c test_identity.conf
gnunet-arm -e -c test_identity.conf
if [ "$TEST_MSG" != "$MSG_DEC" ]
then
@@ -31,5 +42,9 @@ then
echo "Failed - \"$TEST_MSG\" != \"$MSG_DEC\""
exit 1
fi
-
-
+if [ "$TEST_MSG" != "$MSG_DEC_ED" ]
+then
+ diff <(echo "$TEST_MSG" ) <(echo "$MSG_DEC_ED")
+ echo "Failed - \"$TEST_MSG\" != \"$MSG_DEC_ED\""
+ exit 1
+fi
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
index 1c17e72d9..289aa5649 100644
--- a/src/include/gnunet_crypto_lib.h
+++ b/src/include/gnunet_crypto_lib.h
@@ -1853,6 +1853,106 @@ GNUNET_CRYPTO_eddsa_kem_decaps (const struct
const struct GNUNET_CRYPTO_EcdhePublicKey *c,
struct GNUNET_HashCode *key_material);
+/**
+ * @ingroup crypto
+ * Encapsulate key material for a EdDSA public key.
+ * Dual to #GNUNET_CRRYPTO_eddsa_kem_decaps.
+ *
+ * @param priv private key to use for the ECDH (y)
+ * @param c public key from EdDSA to use for the ECDH (X=h(x)G)
+ * @param key_material where to write the key material H(yX)=H(h(x)yG)
+ * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
+ */
+enum GNUNET_GenericReturnValue
+GNUNET_CRYPTO_eddsa_kem_encaps (const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
+ struct GNUNET_CRYPTO_EcdhePublicKey *c,
+ struct GNUNET_HashCode *key_material);
+
+/**
+ * This is the encapsulated key of our FO-KEM.
+ */
+struct GNUNET_CRYPTO_FoKemC
+{
+ /* The output of the FO-OWTF F(x) */
+ struct GNUNET_HashCode y;
+
+ /* The ephemeral public key from the DH in the KEM */
+ struct GNUNET_CRYPTO_EcdhePublicKey pub;
+};
+
+/**
+ * @ingroup crypto
+ * Encapsulate key material using a CCA-secure KEM.
+ * The KEM is using a OWTF with image oracle constructed from
+ * a Fujusaki-Okamoto transformation using ElGamal (DH plus XOR OTP).
+ * Dual to #GNUNET_CRRYPTO_eddsa_fo_kem_decaps.
+ *
+ * @param pub public key to encapsulated for
+ * @param[out] c the encapsulation
+ * @param[out] key_material the encapsulated key
+ * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
+ */
+enum GNUNET_GenericReturnValue
+GNUNET_CRYPTO_eddsa_fo_kem_encaps (
+ const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
+ struct GNUNET_CRYPTO_FoKemC *c,
+ struct GNUNET_HashCode *key_material);
+
+
+/**
+ * @ingroup crypto
+ * Decapsulate key material using a CCA-secure KEM.
+ * The KEM is using a OWTF with image oracle constructed from
+ * a Fujusaki-Okamoto transformation using ElGamal (DH plus XOR OTP).
+ * Dual to #GNUNET_CRRYPTO_eddsa_fo_kem_encaps.
+ *
+ * @param priv private key this encapsulation is for
+ * @param c the encapsulation
+ * @param[out] key_material the encapsulated key
+ * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
+ */
+enum GNUNET_GenericReturnValue
+GNUNET_CRYPTO_eddsa_fo_kem_decaps (const struct
+ GNUNET_CRYPTO_EddsaPrivateKey *priv,
+ const struct GNUNET_CRYPTO_FoKemC *c,
+ struct GNUNET_HashCode *key_material);
+
+/**
+ * @ingroup crypto
+ * Encapsulate key material using a CCA-secure KEM.
+ * The KEM is using a OWTF with image oracle constructed from
+ * a Fujusaki-Okamoto transformation using ElGamal (DH plus XOR OTP).
+ * Dual to #GNUNET_CRRYPTO_eddsa_fo_kem_decaps.
+ *
+ * @param pub public key to encapsulated for
+ * @param[out] c the encapsulation
+ * @param[out] key_material the encapsulated key
+ * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
+ */
+enum GNUNET_GenericReturnValue
+GNUNET_CRYPTO_ecdsa_fo_kem_encaps (const struct
+ GNUNET_CRYPTO_EcdsaPublicKey *pub,
+ struct GNUNET_CRYPTO_FoKemC *c,
+ struct GNUNET_HashCode *key_material);
+
+
+/**
+ * @ingroup crypto
+ * Decapsulate key material using a CCA-secure KEM.
+ * The KEM is using a OWTF with image oracle constructed from
+ * a Fujusaki-Okamoto transformation using ElGamal (DH plus XOR OTP).
+ * Dual to #GNUNET_CRRYPTO_eddsa_fo_kem_encaps.
+ *
+ * @param priv private key this encapsulation is for
+ * @param c the encapsulation
+ * @param[out] key_material the encapsulated key
+ * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
+ */
+enum GNUNET_GenericReturnValue
+GNUNET_CRYPTO_ecdsa_fo_kem_decaps (const struct
+ GNUNET_CRYPTO_EcdsaPrivateKey *priv,
+ struct GNUNET_CRYPTO_FoKemC *c,
+ struct GNUNET_HashCode *key_material);
/**
* @ingroup crypto
@@ -1885,20 +1985,6 @@ GNUNET_CRYPTO_ecdh_eddsa (const struct
GNUNET_CRYPTO_EcdhePrivateKey *priv,
const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
struct GNUNET_HashCode *key_material);
-/**
- * @ingroup crypto
- * Encapsulate key material for a EdDSA public key.
- * Dual to #GNUNET_CRRYPTO_eddsa_kem_decaps.
- *
- * @param priv private key to use for the ECDH (y)
- * @param c public key from EdDSA to use for the ECDH (X=h(x)G)
- * @param key_material where to write the key material H(yX)=H(h(x)yG)
- * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
- */
-enum GNUNET_GenericReturnValue
-GNUNET_CRYPTO_eddsa_kem_encaps (const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
- struct GNUNET_CRYPTO_EcdhePublicKey *c,
- struct GNUNET_HashCode *key_material);
/**
* @ingroup crypto
diff --git a/src/include/gnunet_identity_service.h
b/src/include/gnunet_identity_service.h
index f38e373b1..15e49d781 100644
--- a/src/include/gnunet_identity_service.h
+++ b/src/include/gnunet_identity_service.h
@@ -721,11 +721,11 @@ GNUNET_IDENTITY_signature_verify_raw_ (
* this size should be the same as @c len.
*/
ssize_t
-GNUNET_IDENTITY_encrypt (const void *block,
- size_t size,
- const struct GNUNET_IDENTITY_PublicKey *pub,
- struct GNUNET_CRYPTO_EcdhePublicKey *ecc,
- void *result);
+GNUNET_IDENTITY_encrypt_old (const void *block,
+ size_t size,
+ const struct GNUNET_IDENTITY_PublicKey *pub,
+ struct GNUNET_CRYPTO_EcdhePublicKey *ecc,
+ void *result);
/**
@@ -743,13 +743,62 @@ GNUNET_IDENTITY_encrypt (const void *block,
* this size should be the same as @c size.
*/
ssize_t
-GNUNET_IDENTITY_decrypt (
+GNUNET_IDENTITY_decrypt_old (
const void *block,
size_t size,
const struct GNUNET_IDENTITY_PrivateKey *priv,
const struct GNUNET_CRYPTO_EcdhePublicKey *ecc,
void *result);
+/**
+ * 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
+ *
+ * @param block the block to encrypt
+ * @param size the size of the @a block
+ * @param pub public key to use for ecdh
+ * @param ecc where to write the ecc public key
+ * @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.
+ */
+ssize_t
+GNUNET_IDENTITY_encrypt2 (const void *block,
+ size_t size,
+ const struct GNUNET_IDENTITY_PublicKey *pub,
+ void *result,
+ size_t result_size);
+
+
+/**
+ * Decrypt a given block with #GNUNET_IDENTITY_PrivateKey and a given
+ * #GNUNET_CRYPTO_EcdhePublicKey using ecdh to derive a symmetric key.
+ *
+ * @param block the data to decrypt, encoded as returned by encrypt
+ * @param size the size of the @a block to decrypt
+ * @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.
+ */
+ssize_t
+GNUNET_IDENTITY_decrypt2 (
+ const void *block,
+ size_t size,
+ const struct GNUNET_IDENTITY_PrivateKey *priv,
+ void *result,
+ size_t result_size);
+
/**
* Creates a (Base32) string representation of the public key.
diff --git a/src/messenger/messenger_api_message.c
b/src/messenger/messenger_api_message.c
index ac63f16ca..0e27588ba 100644
--- a/src/messenger/messenger_api_message.c
+++ b/src/messenger/messenger_api_message.c
@@ -41,7 +41,8 @@ struct GNUNET_MESSENGER_ShortMessage
struct GNUNET_MESSENGER_Message*
create_message (enum GNUNET_MESSENGER_MessageKind kind)
{
- struct GNUNET_MESSENGER_Message *message = GNUNET_new(struct
GNUNET_MESSENGER_Message);
+ struct GNUNET_MESSENGER_Message *message = GNUNET_new (struct
+
GNUNET_MESSENGER_Message);
message->header.kind = kind;
@@ -67,32 +68,36 @@ create_message (enum GNUNET_MESSENGER_MessageKind kind)
return message;
}
+
struct GNUNET_MESSENGER_Message*
copy_message (const struct GNUNET_MESSENGER_Message *message)
{
- GNUNET_assert(message);
+ GNUNET_assert (message);
- struct GNUNET_MESSENGER_Message *copy = GNUNET_new(struct
GNUNET_MESSENGER_Message);
+ struct GNUNET_MESSENGER_Message *copy = GNUNET_new (struct
+
GNUNET_MESSENGER_Message);
- GNUNET_memcpy(copy, message, sizeof(struct GNUNET_MESSENGER_Message));
+ GNUNET_memcpy (copy, message, sizeof(struct GNUNET_MESSENGER_Message));
switch (message->header.kind)
{
case GNUNET_MESSENGER_KIND_NAME:
- copy->body.name.name = GNUNET_strdup(message->body.name.name);
+ copy->body.name.name = GNUNET_strdup (message->body.name.name);
break;
case GNUNET_MESSENGER_KIND_TEXT:
- copy->body.text.text = GNUNET_strdup(message->body.text.text);
+ copy->body.text.text = GNUNET_strdup (message->body.text.text);
break;
case GNUNET_MESSENGER_KIND_FILE:
- copy->body.file.uri = GNUNET_strdup(message->body.file.uri);
+ copy->body.file.uri = GNUNET_strdup (message->body.file.uri);
break;
case GNUNET_MESSENGER_KIND_PRIVATE:
- copy->body.privacy.data = copy->body.privacy.length ?
GNUNET_malloc(copy->body.privacy.length) : NULL;
+ copy->body.privacy.data = copy->body.privacy.length ? GNUNET_malloc (
+ copy->body.privacy.length) : NULL;
if (copy->body.privacy.data)
{
- GNUNET_memcpy(copy->body.privacy.data, message->body.privacy.data,
copy->body.privacy.length);
+ GNUNET_memcpy (copy->body.privacy.data, message->body.privacy.data,
+ copy->body.privacy.length);
}
break;
@@ -103,6 +108,7 @@ copy_message (const struct GNUNET_MESSENGER_Message
*message)
return copy;
}
+
static void
destroy_message_body (enum GNUNET_MESSENGER_MessageKind kind,
struct GNUNET_MESSENGER_MessageBody *body)
@@ -110,44 +116,47 @@ destroy_message_body (enum GNUNET_MESSENGER_MessageKind
kind,
switch (kind)
{
case GNUNET_MESSENGER_KIND_NAME:
- GNUNET_free(body->name.name);
+ GNUNET_free (body->name.name);
break;
case GNUNET_MESSENGER_KIND_TEXT:
- GNUNET_free(body->text.text);
+ GNUNET_free (body->text.text);
break;
case GNUNET_MESSENGER_KIND_FILE:
- GNUNET_free(body->file.uri);
+ GNUNET_free (body->file.uri);
break;
case GNUNET_MESSENGER_KIND_PRIVATE:
- GNUNET_free(body->privacy.data);
+ GNUNET_free (body->privacy.data);
break;
default:
break;
}
}
+
void
cleanup_message (struct GNUNET_MESSENGER_Message *message)
{
- GNUNET_assert(message);
+ GNUNET_assert (message);
destroy_message_body (message->header.kind, &(message->body));
}
+
void
destroy_message (struct GNUNET_MESSENGER_Message *message)
{
- GNUNET_assert(message);
+ GNUNET_assert (message);
destroy_message_body (message->header.kind, &(message->body));
- GNUNET_free(message);
+ GNUNET_free (message);
}
+
int
is_message_session_bound (const struct GNUNET_MESSENGER_Message *message)
{
- GNUNET_assert(message);
+ GNUNET_assert (message);
if ((GNUNET_MESSENGER_KIND_JOIN == message->header.kind) ||
(GNUNET_MESSENGER_KIND_LEAVE == message->header.kind) ||
@@ -159,15 +168,18 @@ is_message_session_bound (const struct
GNUNET_MESSENGER_Message *message)
return GNUNET_NO;
}
+
static void
fold_short_message (const struct GNUNET_MESSENGER_Message *message,
struct GNUNET_MESSENGER_ShortMessage *shortened)
{
shortened->kind = message->header.kind;
- GNUNET_memcpy(&(shortened->body), &(message->body), sizeof(struct
GNUNET_MESSENGER_MessageBody));
+ GNUNET_memcpy (&(shortened->body), &(message->body), sizeof(struct
+
GNUNET_MESSENGER_MessageBody));
}
+
static void
unfold_short_message (struct GNUNET_MESSENGER_ShortMessage *shortened,
struct GNUNET_MESSENGER_Message *message)
@@ -176,9 +188,11 @@ unfold_short_message (struct GNUNET_MESSENGER_ShortMessage
*shortened,
message->header.kind = shortened->kind;
- GNUNET_memcpy(&(message->body), &(shortened->body), sizeof(struct
GNUNET_MESSENGER_MessageBody));
+ GNUNET_memcpy (&(message->body), &(shortened->body), sizeof(struct
+
GNUNET_MESSENGER_MessageBody));
}
+
#define member_size(type, member) sizeof(((type*) NULL)->member)
static uint16_t
@@ -189,40 +203,43 @@ get_message_body_kind_size (enum
GNUNET_MESSENGER_MessageKind kind)
switch (kind)
{
case GNUNET_MESSENGER_KIND_INFO:
- length += member_size(struct GNUNET_MESSENGER_Message,
body.info.messenger_version);
+ length += member_size (struct GNUNET_MESSENGER_Message,
+ body.info.messenger_version);
break;
case GNUNET_MESSENGER_KIND_PEER:
- length += member_size(struct GNUNET_MESSENGER_Message, body.peer.peer);
+ length += member_size (struct GNUNET_MESSENGER_Message, body.peer.peer);
break;
case GNUNET_MESSENGER_KIND_ID:
- length += member_size(struct GNUNET_MESSENGER_Message, body.id.id);
+ length += member_size (struct GNUNET_MESSENGER_Message, body.id.id);
break;
case GNUNET_MESSENGER_KIND_MISS:
- length += member_size(struct GNUNET_MESSENGER_Message, body.miss.peer);
+ length += member_size (struct GNUNET_MESSENGER_Message, body.miss.peer);
break;
case GNUNET_MESSENGER_KIND_MERGE:
- length += member_size(struct GNUNET_MESSENGER_Message,
body.merge.previous);
+ length += member_size (struct GNUNET_MESSENGER_Message,
+ body.merge.previous);
break;
case GNUNET_MESSENGER_KIND_REQUEST:
- length += member_size(struct GNUNET_MESSENGER_Message, body.request.hash);
+ length += member_size (struct GNUNET_MESSENGER_Message, body.request.hash);
break;
case GNUNET_MESSENGER_KIND_INVITE:
- length += member_size(struct GNUNET_MESSENGER_Message, body.invite.door);
- length += member_size(struct GNUNET_MESSENGER_Message, body.invite.key);
+ length += member_size (struct GNUNET_MESSENGER_Message, body.invite.door);
+ length += member_size (struct GNUNET_MESSENGER_Message, body.invite.key);
break;
case GNUNET_MESSENGER_KIND_TEXT:
break;
case GNUNET_MESSENGER_KIND_FILE:
- length += member_size(struct GNUNET_MESSENGER_Message, body.file.key);
- length += member_size(struct GNUNET_MESSENGER_Message, body.file.hash);
- length += member_size(struct GNUNET_MESSENGER_Message, body.file.name);
+ length += member_size (struct GNUNET_MESSENGER_Message, body.file.key);
+ length += member_size (struct GNUNET_MESSENGER_Message, body.file.hash);
+ length += member_size (struct GNUNET_MESSENGER_Message, body.file.name);
break;
case GNUNET_MESSENGER_KIND_PRIVATE:
- length += member_size(struct GNUNET_MESSENGER_Message, body.privacy.key);
+ length += member_size (struct GNUNET_MESSENGER_Message, body.privacy.key);
break;
case GNUNET_MESSENGER_KIND_DELETE:
- length += member_size(struct GNUNET_MESSENGER_Message, body.deletion.hash);
- length += member_size(struct GNUNET_MESSENGER_Message,
body.deletion.delay);
+ length += member_size (struct GNUNET_MESSENGER_Message,
body.deletion.hash);
+ length += member_size (struct GNUNET_MESSENGER_Message,
+ body.deletion.delay);
break;
default:
break;
@@ -231,6 +248,7 @@ get_message_body_kind_size (enum
GNUNET_MESSENGER_MessageKind kind)
return length;
}
+
typedef uint32_t kind_t;
uint16_t
@@ -241,9 +259,9 @@ get_message_kind_size (enum GNUNET_MESSENGER_MessageKind
kind,
if (GNUNET_YES == include_header)
{
- length += member_size(struct GNUNET_MESSENGER_Message, header.timestamp);
- length += member_size(struct GNUNET_MESSENGER_Message, header.sender_id);
- length += member_size(struct GNUNET_MESSENGER_Message, header.previous);
+ length += member_size (struct GNUNET_MESSENGER_Message, header.timestamp);
+ length += member_size (struct GNUNET_MESSENGER_Message, header.sender_id);
+ length += member_size (struct GNUNET_MESSENGER_Message, header.previous);
}
length += sizeof(kind_t);
@@ -251,6 +269,7 @@ get_message_kind_size (enum GNUNET_MESSENGER_MessageKind
kind,
return length + get_message_body_kind_size (kind);
}
+
static uint16_t
get_message_body_size (enum GNUNET_MESSENGER_MessageKind kind,
const struct GNUNET_MESSENGER_MessageBody *body)
@@ -260,16 +279,16 @@ get_message_body_size (enum GNUNET_MESSENGER_MessageKind
kind,
switch (kind)
{
case GNUNET_MESSENGER_KIND_INFO:
- length += GNUNET_IDENTITY_public_key_get_length(&(body->info.host_key));
+ length += GNUNET_IDENTITY_public_key_get_length (&(body->info.host_key));
break;
case GNUNET_MESSENGER_KIND_JOIN:
- length += GNUNET_IDENTITY_public_key_get_length(&(body->join.key));
+ length += GNUNET_IDENTITY_public_key_get_length (&(body->join.key));
break;
case GNUNET_MESSENGER_KIND_NAME:
length += (body->name.name ? strlen (body->name.name) : 0);
break;
case GNUNET_MESSENGER_KIND_KEY:
- length += GNUNET_IDENTITY_public_key_get_length(&(body->key.key));
+ length += GNUNET_IDENTITY_public_key_get_length (&(body->key.key));
break;
case GNUNET_MESSENGER_KIND_TEXT:
length += strlen (body->text.text);
@@ -287,16 +306,18 @@ get_message_body_size (enum GNUNET_MESSENGER_MessageKind
kind,
return length;
}
+
uint16_t
get_message_size (const struct GNUNET_MESSENGER_Message *message,
int include_header)
{
- GNUNET_assert(message);
+ GNUNET_assert (message);
uint16_t length = 0;
if (GNUNET_YES == include_header)
- length +=
GNUNET_IDENTITY_signature_get_length(&(message->header.signature));
+ length += GNUNET_IDENTITY_signature_get_length (
+ &(message->header.signature));
length += get_message_kind_size (message->header.kind, include_header);
length += get_message_body_size (message->header.kind, &(message->body));
@@ -304,6 +325,7 @@ get_message_size (const struct GNUNET_MESSENGER_Message
*message,
return length;
}
+
static uint16_t
get_short_message_size (const struct GNUNET_MESSENGER_ShortMessage *message,
int include_body)
@@ -312,19 +334,24 @@ get_short_message_size (const struct
GNUNET_MESSENGER_ShortMessage *message,
if (message)
return minimum_size + get_message_body_kind_size (message->kind)
- + (include_body == GNUNET_YES? get_message_body_size
(message->kind, &(message->body)) : 0);
+ + (include_body == GNUNET_YES? get_message_body_size (message->kind,
+
&(message->body))
+ : 0);
else
return minimum_size;
}
+
static uint16_t
calc_usual_padding ()
{
uint16_t padding = 0;
uint16_t kind_size;
- for (int i = 0; i <= GNUNET_MESSENGER_KIND_MAX; i++) {
- kind_size = get_message_kind_size ((enum GNUNET_MESSENGER_MessageKind) i,
GNUNET_YES);
+ for (int i = 0; i <= GNUNET_MESSENGER_KIND_MAX; i++)
+ {
+ kind_size = get_message_kind_size ((enum GNUNET_MESSENGER_MessageKind) i,
+ GNUNET_YES);
if (kind_size > padding)
padding = kind_size;
@@ -333,6 +360,7 @@ calc_usual_padding ()
return padding + GNUNET_MESSENGER_PADDING_MIN;
}
+
#define max(x, y) (x > y? x : y)
static uint16_t
@@ -340,13 +368,13 @@ calc_padded_length (uint16_t length)
{
static uint16_t usual_padding = 0;
- if (!usual_padding)
- usual_padding = calc_usual_padding();
+ if (! usual_padding)
+ usual_padding = calc_usual_padding ();
- const uint16_t padded_length = max(
- length + GNUNET_MESSENGER_PADDING_MIN,
- usual_padding
- );
+ const uint16_t padded_length = max (
+ length + GNUNET_MESSENGER_PADDING_MIN,
+ usual_padding
+ );
if (padded_length <= GNUNET_MESSENGER_PADDING_LEVEL0)
return GNUNET_MESSENGER_PADDING_LEVEL0;
@@ -361,35 +389,36 @@ calc_padded_length (uint16_t length)
}
+
#define min(x, y) (x < y? x : y)
#define encode_step_ext(dst, offset, src, size) do { \
- GNUNET_memcpy(dst + offset, src, size); \
- offset += size; \
+ GNUNET_memcpy (dst + offset, src, size); \
+ offset += size; \
} while (0)
#define encode_step(dst, offset, src) do { \
- encode_step_ext(dst, offset, src, sizeof(*src)); \
+ encode_step_ext (dst, offset, src, sizeof(*src)); \
} while (0)
#define encode_step_key(dst, offset, src, length) do { \
- ssize_t result = GNUNET_IDENTITY_write_public_key_to_buffer( \
- src, dst + offset, length - offset \
- ); \
- if (result < 0) \
- GNUNET_break (0); \
- else \
- offset += result; \
+ ssize_t result = GNUNET_IDENTITY_write_public_key_to_buffer ( \
+ src, dst + offset, length - offset \
+ ); \
+ if (result < 0) \
+ GNUNET_break (0); \
+ else \
+ offset += result; \
} while (0)
#define encode_step_signature(dst, offset, src, length) do { \
- ssize_t result = GNUNET_IDENTITY_write_signature_to_buffer( \
- src, dst + offset, length - offset \
- ); \
- if (result < 0) \
- GNUNET_break (0); \
- else \
- offset += result; \
+ ssize_t result = GNUNET_IDENTITY_write_signature_to_buffer ( \
+ src, dst + offset, length - offset \
+ ); \
+ if (result < 0) \
+ GNUNET_break (0); \
+ else \
+ offset += result; \
} while (0)
static void
@@ -403,56 +432,64 @@ encode_message_body (enum GNUNET_MESSENGER_MessageKind
kind,
switch (kind)
{
case GNUNET_MESSENGER_KIND_INFO:
- version = GNUNET_htobe32(body->info.messenger_version);
+ version = GNUNET_htobe32 (body->info.messenger_version);
- encode_step_key(buffer, offset, &(body->info.host_key), length);
- encode_step(buffer, offset, &version);
+ encode_step_key (buffer, offset, &(body->info.host_key), length);
+ encode_step (buffer, offset, &version);
break;
case GNUNET_MESSENGER_KIND_JOIN:
- encode_step_key(buffer, offset, &(body->join.key), length);
+ encode_step_key (buffer, offset, &(body->join.key), length);
break;
case GNUNET_MESSENGER_KIND_NAME:
if (body->name.name)
- encode_step_ext(buffer, offset, body->name.name, min(length - offset,
strlen(body->name.name)));
+ encode_step_ext (buffer, offset, body->name.name, min (length - offset,
+ strlen (
+
body->name.name)));
break;
case GNUNET_MESSENGER_KIND_KEY:
- encode_step_key(buffer, offset, &(body->key.key), length);
+ encode_step_key (buffer, offset, &(body->key.key), length);
break;
case GNUNET_MESSENGER_KIND_PEER:
- encode_step(buffer, offset, &(body->peer.peer));
+ encode_step (buffer, offset, &(body->peer.peer));
break;
case GNUNET_MESSENGER_KIND_ID:
- encode_step(buffer, offset, &(body->id.id));
+ encode_step (buffer, offset, &(body->id.id));
break;
case GNUNET_MESSENGER_KIND_MISS:
- encode_step(buffer, offset, &(body->miss.peer));
+ encode_step (buffer, offset, &(body->miss.peer));
break;
case GNUNET_MESSENGER_KIND_MERGE:
- encode_step(buffer, offset, &(body->merge.previous));
+ encode_step (buffer, offset, &(body->merge.previous));
break;
case GNUNET_MESSENGER_KIND_REQUEST:
- encode_step(buffer, offset, &(body->request.hash));
+ encode_step (buffer, offset, &(body->request.hash));
break;
case GNUNET_MESSENGER_KIND_INVITE:
- encode_step(buffer, offset, &(body->invite.door));
- encode_step(buffer, offset, &(body->invite.key));
+ encode_step (buffer, offset, &(body->invite.door));
+ encode_step (buffer, offset, &(body->invite.key));
break;
case GNUNET_MESSENGER_KIND_TEXT:
- encode_step_ext(buffer, offset, body->text.text, min(length - offset,
strlen(body->text.text)));
+ encode_step_ext (buffer, offset, body->text.text, min (length - offset,
+ strlen (
+
body->text.text)));
break;
case GNUNET_MESSENGER_KIND_FILE:
- encode_step(buffer, offset, &(body->file.key));
- encode_step(buffer, offset, &(body->file.hash));
- encode_step_ext(buffer, offset, body->file.name, sizeof(body->file.name));
- encode_step_ext(buffer, offset, body->file.uri, min(length - offset,
strlen(body->file.uri)));
+ encode_step (buffer, offset, &(body->file.key));
+ encode_step (buffer, offset, &(body->file.hash));
+ encode_step_ext (buffer, offset, body->file.name, sizeof(body->file.name));
+ encode_step_ext (buffer, offset, body->file.uri, min (length - offset,
+ strlen (
+ body->file.uri)));
break;
case GNUNET_MESSENGER_KIND_PRIVATE:
- encode_step(buffer, offset, &(body->privacy.key));
- encode_step_ext(buffer, offset, body->privacy.data, min(length - offset,
body->privacy.length));
+ encode_step (buffer, offset, &(body->privacy.key));
+ encode_step_ext (buffer, offset, body->privacy.data, min (length - offset,
+ body->privacy.
+ length));
break;
case GNUNET_MESSENGER_KIND_DELETE:
- encode_step(buffer, offset, &(body->deletion.hash));
- encode_step(buffer, offset, &(body->deletion.delay));
+ encode_step (buffer, offset, &(body->deletion.hash));
+ encode_step (buffer, offset, &(body->deletion.delay));
break;
default:
break;
@@ -464,43 +501,48 @@ encode_message_body (enum GNUNET_MESSENGER_MessageKind
kind,
const uint16_t padding = length - offset;
const uint16_t used_padding = sizeof(padding) + sizeof(char);
- GNUNET_assert(padding >= used_padding);
+ GNUNET_assert (padding >= used_padding);
buffer[offset++] = '\0';
if (padding > used_padding)
- GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, buffer + offset,
padding - used_padding);
+ GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, buffer + offset,
+ padding - used_padding);
- GNUNET_memcpy(buffer + length - sizeof(padding), &padding, sizeof(padding));
+ GNUNET_memcpy (buffer + length - sizeof(padding), &padding, sizeof(padding));
}
+
void
encode_message (const struct GNUNET_MESSENGER_Message *message,
uint16_t length,
char *buffer,
int include_header)
{
- GNUNET_assert((message) && (buffer));
+ GNUNET_assert ((message) && (buffer));
uint16_t offset = 0;
if (GNUNET_YES == include_header)
- encode_step_signature(buffer, offset, &(message->header.signature),
length);
+ encode_step_signature (buffer, offset, &(message->header.signature),
+ length);
- const kind_t kind = GNUNET_htobe32((kind_t) message->header.kind);
+ const kind_t kind = GNUNET_htobe32 ((kind_t) message->header.kind);
if (GNUNET_YES == include_header)
{
- encode_step(buffer, offset, &(message->header.timestamp));
- encode_step(buffer, offset, &(message->header.sender_id));
- encode_step(buffer, offset, &(message->header.previous));
+ encode_step (buffer, offset, &(message->header.timestamp));
+ encode_step (buffer, offset, &(message->header.sender_id));
+ encode_step (buffer, offset, &(message->header.previous));
}
- encode_step(buffer, offset, &kind);
+ encode_step (buffer, offset, &kind);
- encode_message_body (message->header.kind, &(message->body), length, buffer,
offset);
+ encode_message_body (message->header.kind, &(message->body), length, buffer,
+ offset);
}
+
static void
encode_short_message (const struct GNUNET_MESSENGER_ShortMessage *message,
uint16_t length,
@@ -509,46 +551,47 @@ encode_short_message (const struct
GNUNET_MESSENGER_ShortMessage *message,
struct GNUNET_HashCode hash;
uint16_t offset = sizeof(hash);
- const kind_t kind = GNUNET_htobe32((kind_t) message->kind);
+ const kind_t kind = GNUNET_htobe32 ((kind_t) message->kind);
- encode_step(buffer, offset, &kind);
+ encode_step (buffer, offset, &kind);
encode_message_body (message->kind, &(message->body), length, buffer,
offset);
- GNUNET_CRYPTO_hash(
- buffer + sizeof(hash),
- length - sizeof(hash),
- &hash
- );
+ GNUNET_CRYPTO_hash (
+ buffer + sizeof(hash),
+ length - sizeof(hash),
+ &hash
+ );
- GNUNET_memcpy(buffer, &hash, sizeof(hash));
+ GNUNET_memcpy (buffer, &hash, sizeof(hash));
}
+
#define decode_step_ext(src, offset, dst, size) do { \
- GNUNET_memcpy(dst, src + offset, size); \
- offset += size; \
+ GNUNET_memcpy (dst, src + offset, size); \
+ offset += size; \
} while (0)
-#define decode_step(src, offset, dst) do { \
- decode_step_ext(src, offset, dst, sizeof(*dst)); \
+#define decode_step(src, offset, dst) do { \
+ decode_step_ext (src, offset, dst, sizeof(*dst)); \
} while (0)
-#define decode_step_malloc(src, offset, dst, size, zero) do { \
- dst = GNUNET_malloc(size + zero); \
- if (zero) dst[size] = 0;
\
- decode_step_ext(src, offset, dst, size);
\
+#define decode_step_malloc(src, offset, dst, size, zero) do { \
+ dst = GNUNET_malloc (size + zero); \
+ if (zero) dst[size] = 0; \
+ decode_step_ext (src, offset, dst, size); \
} while (0)
#define decode_step_key(src, offset, dst, length) do { \
- enum GNUNET_GenericReturnValue result; \
- size_t read; \
- result = GNUNET_IDENTITY_read_public_key_from_buffer( \
- src + offset, length - offset, dst, &read \
- ); \
- if (GNUNET_SYSERR == result) \
- GNUNET_break(0); \
- else \
- offset += read; \
+ enum GNUNET_GenericReturnValue result; \
+ size_t read; \
+ result = GNUNET_IDENTITY_read_public_key_from_buffer ( \
+ src + offset, length - offset, dst, &read \
+ ); \
+ if (GNUNET_SYSERR == result) \
+ GNUNET_break (0); \
+ else \
+ offset += read; \
} while (0)
static uint16_t
@@ -560,7 +603,7 @@ decode_message_body (enum GNUNET_MESSENGER_MessageKind
*kind,
{
uint16_t padding = 0;
- GNUNET_memcpy(&padding, buffer + length - sizeof(padding), sizeof(padding));
+ GNUNET_memcpy (&padding, buffer + length - sizeof(padding), sizeof(padding));
if (padding > length - offset)
padding = 0;
@@ -576,60 +619,60 @@ decode_message_body (enum GNUNET_MESSENGER_MessageKind
*kind,
switch (*kind)
{
case GNUNET_MESSENGER_KIND_INFO: {
- decode_step_key(buffer, offset, &(body->info.host_key), length);
- decode_step(buffer, offset, &version);
-
- body->info.messenger_version = GNUNET_be32toh(version);
- break;
- } case GNUNET_MESSENGER_KIND_JOIN: {
- decode_step_key(buffer, offset, &(body->join.key), length);
- break;
- } case GNUNET_MESSENGER_KIND_NAME:
+ decode_step_key (buffer, offset, &(body->info.host_key), length);
+ decode_step (buffer, offset, &version);
+
+ body->info.messenger_version = GNUNET_be32toh (version);
+ break;
+ } case GNUNET_MESSENGER_KIND_JOIN: {
+ decode_step_key (buffer, offset, &(body->join.key), length);
+ break;
+ } case GNUNET_MESSENGER_KIND_NAME:
if (length - offset > 0)
- decode_step_malloc(buffer, offset, body->name.name, length - offset, 1);
+ decode_step_malloc (buffer, offset, body->name.name, length - offset, 1);
else
body->name.name = NULL;
break;
case GNUNET_MESSENGER_KIND_KEY:
- decode_step_key(buffer, offset, &(body->key.key), length);
+ decode_step_key (buffer, offset, &(body->key.key), length);
break;
case GNUNET_MESSENGER_KIND_PEER:
- decode_step(buffer, offset, &(body->peer.peer));
+ decode_step (buffer, offset, &(body->peer.peer));
break;
case GNUNET_MESSENGER_KIND_ID:
- decode_step(buffer, offset, &(body->id.id));
+ decode_step (buffer, offset, &(body->id.id));
break;
case GNUNET_MESSENGER_KIND_MISS:
- decode_step(buffer, offset, &(body->miss.peer));
+ decode_step (buffer, offset, &(body->miss.peer));
break;
case GNUNET_MESSENGER_KIND_MERGE:
- decode_step(buffer, offset, &(body->merge.previous));
+ decode_step (buffer, offset, &(body->merge.previous));
break;
case GNUNET_MESSENGER_KIND_REQUEST:
- decode_step(buffer, offset, &(body->request.hash));
+ decode_step (buffer, offset, &(body->request.hash));
break;
case GNUNET_MESSENGER_KIND_INVITE:
- decode_step(buffer, offset, &(body->invite.door));
- decode_step(buffer, offset, &(body->invite.key));
+ decode_step (buffer, offset, &(body->invite.door));
+ decode_step (buffer, offset, &(body->invite.key));
break;
case GNUNET_MESSENGER_KIND_TEXT:
- decode_step_malloc(buffer, offset, body->text.text, length - offset, 1);
+ decode_step_malloc (buffer, offset, body->text.text, length - offset, 1);
break;
case GNUNET_MESSENGER_KIND_FILE:
- decode_step(buffer, offset, &(body->file.key));
- decode_step(buffer, offset, &(body->file.hash));
- decode_step_ext(buffer, offset, body->file.name, sizeof(body->file.name));
- decode_step_malloc(buffer, offset, body->file.uri, length - offset, 1);
+ decode_step (buffer, offset, &(body->file.key));
+ decode_step (buffer, offset, &(body->file.hash));
+ decode_step_ext (buffer, offset, body->file.name, sizeof(body->file.name));
+ decode_step_malloc (buffer, offset, body->file.uri, length - offset, 1);
break;
case GNUNET_MESSENGER_KIND_PRIVATE:
- decode_step(buffer, offset, &(body->privacy.key));
+ decode_step (buffer, offset, &(body->privacy.key));
body->privacy.length = (length - offset);
- decode_step_malloc(buffer, offset, body->privacy.data, length - offset, 0);
+ decode_step_malloc (buffer, offset, body->privacy.data, length - offset,
0);
break;
case GNUNET_MESSENGER_KIND_DELETE:
- decode_step(buffer, offset, &(body->deletion.hash));
- decode_step(buffer, offset, &(body->deletion.delay));
+ decode_step (buffer, offset, &(body->deletion.hash));
+ decode_step (buffer, offset, &(body->deletion.delay));
break;
default:
*kind = GNUNET_MESSENGER_KIND_UNKNOWN;
@@ -639,6 +682,7 @@ decode_message_body (enum GNUNET_MESSENGER_MessageKind
*kind,
return padding;
}
+
int
decode_message (struct GNUNET_MESSENGER_Message *message,
uint16_t length,
@@ -646,19 +690,20 @@ decode_message (struct GNUNET_MESSENGER_Message *message,
int include_header,
uint16_t *padding)
{
- GNUNET_assert(
- (message) &&
- (buffer) &&
- (length >= get_message_kind_size(GNUNET_MESSENGER_KIND_UNKNOWN,
include_header))
- );
+ GNUNET_assert (
+ (message) &&
+ (buffer) &&
+ (length >= get_message_kind_size (GNUNET_MESSENGER_KIND_UNKNOWN,
+ include_header))
+ );
uint16_t offset = 0;
if (GNUNET_YES == include_header)
{
- ssize_t result = GNUNET_IDENTITY_read_signature_from_buffer(
- &(message->header.signature), buffer, length - offset
- );
+ ssize_t result = GNUNET_IDENTITY_read_signature_from_buffer (
+ &(message->header.signature), buffer, length - offset
+ );
if (result < 0)
return GNUNET_NO;
@@ -668,26 +713,30 @@ decode_message (struct GNUNET_MESSENGER_Message *message,
const uint16_t count = length - offset;
- if (count < get_message_kind_size (GNUNET_MESSENGER_KIND_UNKNOWN,
include_header))
+ if (count < get_message_kind_size (GNUNET_MESSENGER_KIND_UNKNOWN,
+ include_header))
return GNUNET_NO;
kind_t kind;
if (GNUNET_YES == include_header)
{
- decode_step(buffer, offset, &(message->header.timestamp));
- decode_step(buffer, offset, &(message->header.sender_id));
- decode_step(buffer, offset, &(message->header.previous));
+ decode_step (buffer, offset, &(message->header.timestamp));
+ decode_step (buffer, offset, &(message->header.sender_id));
+ decode_step (buffer, offset, &(message->header.previous));
}
- decode_step(buffer, offset, &kind);
+ decode_step (buffer, offset, &kind);
- message->header.kind = (enum GNUNET_MESSENGER_MessageKind)
GNUNET_be32toh(kind);
+ message->header.kind = (enum GNUNET_MESSENGER_MessageKind) GNUNET_be32toh (
+ kind);
if (count < get_message_kind_size (message->header.kind, include_header))
return GNUNET_NO;
- const uint16_t result = decode_message_body (&(message->header.kind),
&(message->body), length, buffer, offset);
+ const uint16_t result = decode_message_body (&(message->header.kind),
+ &(message->body), length,
buffer,
+ offset);
if (padding)
*padding = result;
@@ -695,6 +744,7 @@ decode_message (struct GNUNET_MESSENGER_Message *message,
return GNUNET_YES;
}
+
static int
decode_short_message (struct GNUNET_MESSENGER_ShortMessage *message,
uint16_t length,
@@ -706,27 +756,28 @@ decode_short_message (struct
GNUNET_MESSENGER_ShortMessage *message,
if (length < get_short_message_size (NULL, GNUNET_NO))
return GNUNET_NO;
- GNUNET_memcpy(&hash, buffer, sizeof(hash));
+ GNUNET_memcpy (&hash, buffer, sizeof(hash));
- GNUNET_CRYPTO_hash(
- buffer + sizeof(hash),
- length - sizeof(hash),
- &expected
- );
+ GNUNET_CRYPTO_hash (
+ buffer + sizeof(hash),
+ length - sizeof(hash),
+ &expected
+ );
- if (0 != GNUNET_CRYPTO_hash_cmp(&hash, &expected))
+ if (0 != GNUNET_CRYPTO_hash_cmp (&hash, &expected))
return GNUNET_NO;
kind_t kind;
- decode_step(buffer, offset, &kind);
+ decode_step (buffer, offset, &kind);
- message->kind = (enum GNUNET_MESSENGER_MessageKind) GNUNET_be32toh(kind);
+ message->kind = (enum GNUNET_MESSENGER_MessageKind) GNUNET_be32toh (kind);
if (length < get_short_message_size (message, GNUNET_NO))
return GNUNET_NO;
- decode_message_body (&(message->kind), &(message->body), length, buffer,
offset);
+ decode_message_body (&(message->kind), &(message->body), length, buffer,
+ offset);
if (GNUNET_MESSENGER_KIND_UNKNOWN == message->kind)
return GNUNET_NO;
@@ -734,21 +785,23 @@ decode_short_message (struct
GNUNET_MESSENGER_ShortMessage *message,
return GNUNET_YES;
}
+
void
hash_message (const struct GNUNET_MESSENGER_Message *message,
uint16_t length,
const char *buffer,
struct GNUNET_HashCode *hash)
{
- GNUNET_assert((message) && (buffer) && (hash));
+ GNUNET_assert ((message) && (buffer) && (hash));
- const ssize_t offset = GNUNET_IDENTITY_signature_get_length(
- &(message->header.signature)
- );
+ const ssize_t offset = GNUNET_IDENTITY_signature_get_length (
+ &(message->header.signature)
+ );
GNUNET_CRYPTO_hash (buffer + offset, length - offset, hash);
}
+
void
sign_message (struct GNUNET_MESSENGER_Message *message,
uint16_t length,
@@ -756,26 +809,27 @@ sign_message (struct GNUNET_MESSENGER_Message *message,
const struct GNUNET_HashCode *hash,
const struct GNUNET_MESSENGER_Ego *ego)
{
- GNUNET_assert((message) && (buffer) && (hash) && (ego));
+ GNUNET_assert ((message) && (buffer) && (hash) && (ego));
struct GNUNET_MESSENGER_MessageSignature signature;
signature.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE);
signature.purpose.size = htonl (sizeof(signature));
- GNUNET_memcpy(&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
- GNUNET_IDENTITY_sign(&(ego->priv), &signature, &(message->header.signature));
+ GNUNET_memcpy (&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
+ GNUNET_IDENTITY_sign (&(ego->priv), &signature,
&(message->header.signature));
uint16_t offset = 0;
- encode_step_signature(buffer, offset, &(message->header.signature), length);
+ encode_step_signature (buffer, offset, &(message->header.signature), length);
}
+
int
verify_message (const struct GNUNET_MESSENGER_Message *message,
const struct GNUNET_HashCode *hash,
const struct GNUNET_IDENTITY_PublicKey *key)
{
- GNUNET_assert((message) && (hash) && (key));
+ GNUNET_assert ((message) && (hash) && (key));
if (ntohl (key->type) != ntohl (message->header.signature.type))
return GNUNET_SYSERR;
@@ -785,67 +839,77 @@ verify_message (const struct GNUNET_MESSENGER_Message
*message,
signature.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE);
signature.purpose.size = htonl (sizeof(signature));
- GNUNET_memcpy(&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
+ GNUNET_memcpy (&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
- return
GNUNET_IDENTITY_signature_verify(GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE,
&signature,
- &(message->header.signature), key);
+ return GNUNET_IDENTITY_signature_verify (
+ GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE, &signature,
+ &(message->header.signature), key);
}
+
int
encrypt_message (struct GNUNET_MESSENGER_Message *message,
const struct GNUNET_IDENTITY_PublicKey *key)
{
- GNUNET_assert((message) && (key));
+ GNUNET_assert ((message) && (key));
struct GNUNET_MESSENGER_ShortMessage shortened;
fold_short_message (message, &shortened);
const uint16_t length = get_short_message_size (&shortened, GNUNET_YES);
- const uint16_t padded_length = calc_padded_length(length);
+ const uint16_t padded_length = calc_padded_length (length);
message->header.kind = GNUNET_MESSENGER_KIND_PRIVATE;
- message->body.privacy.data = GNUNET_malloc(padded_length);
+ message->body.privacy.data = GNUNET_malloc (padded_length);
message->body.privacy.length = padded_length;
encode_short_message (&shortened, padded_length, message->body.privacy.data);
- if (padded_length == GNUNET_IDENTITY_encrypt (message->body.privacy.data,
padded_length, key,
- &(message->body.privacy.key),
- message->body.privacy.data))
+ if (padded_length == GNUNET_IDENTITY_encrypt_old (message->body.privacy.data,
+ padded_length, key,
+
&(message->body.privacy.key),
+
message->body.privacy.data))
{
destroy_message_body (shortened.kind, &(shortened.body));
return GNUNET_YES;
}
else
{
- GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Encrypting message failed!\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Encrypting message failed!\n");
unfold_short_message (&shortened, message);
return GNUNET_NO;
}
}
+
int
decrypt_message (struct GNUNET_MESSENGER_Message *message,
const struct GNUNET_IDENTITY_PrivateKey *key)
{
- GNUNET_assert((message) && (key));
-
- if (message->body.privacy.length != GNUNET_IDENTITY_decrypt
(message->body.privacy.data, message->body.privacy.length,
- key,
&(message->body.privacy.key),
-
message->body.privacy.data))
+ GNUNET_assert ((message) && (key));
+
+ if (message->body.privacy.length != GNUNET_IDENTITY_decrypt_old (
+ message->body.privacy.data, message->body.privacy.length,
+ key,
+ &(message->body.privacy.key),
+ message->body.
+ privacy.data))
{
- GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Decrypting message failed!\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Decrypting message failed!\n");
return GNUNET_NO;
}
struct GNUNET_MESSENGER_ShortMessage shortened;
- if (GNUNET_YES != decode_short_message (&shortened,
message->body.privacy.length, message->body.privacy.data))
+ if (GNUNET_YES != decode_short_message (&shortened,
+ message->body.privacy.length,
+ message->body.privacy.data))
{
- GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Decoding decrypted message
failed!\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Decoding decrypted message failed!\n");
return GNUNET_NO;
}
@@ -855,31 +919,34 @@ decrypt_message (struct GNUNET_MESSENGER_Message *message,
return GNUNET_YES;
}
+
struct GNUNET_MQ_Envelope*
pack_message (struct GNUNET_MESSENGER_Message *message,
struct GNUNET_HashCode *hash,
const struct GNUNET_MESSENGER_Ego *ego,
int mode)
{
- GNUNET_assert(message);
+ GNUNET_assert (message);
if (ego)
message->header.signature.type = ego->priv.type;
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packing message kind=%u and sender:
%s\n",
- message->header.kind, GNUNET_sh2s(&(message->header.sender_id)));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Packing message kind=%u and sender: %s\n",
+ message->header.kind, GNUNET_sh2s
(&(message->header.sender_id)));
struct GNUNET_MessageHeader *header;
const uint16_t length = get_message_size (message, GNUNET_YES);
- const uint16_t padded_length = calc_padded_length(length);
+ const uint16_t padded_length = calc_padded_length (length);
struct GNUNET_MQ_Envelope *env;
char *buffer;
if (GNUNET_MESSENGER_PACK_MODE_ENVELOPE == mode)
{
- env = GNUNET_MQ_msg_extra(header, padded_length,
GNUNET_MESSAGE_TYPE_CADET_CLI);
+ env = GNUNET_MQ_msg_extra (header, padded_length,
+ GNUNET_MESSAGE_TYPE_CADET_CLI);
buffer = (char*) &(header[1]);
}
@@ -887,7 +954,7 @@ pack_message (struct GNUNET_MESSENGER_Message *message,
{
env = NULL;
- buffer = GNUNET_malloc(padded_length);
+ buffer = GNUNET_malloc (padded_length);
}
encode_message (message, padded_length, buffer, GNUNET_YES);
@@ -901,11 +968,12 @@ pack_message (struct GNUNET_MESSENGER_Message *message,
}
if (GNUNET_MESSENGER_PACK_MODE_ENVELOPE != mode)
- GNUNET_free(buffer);
+ GNUNET_free (buffer);
return env;
}
+
int
filter_message_sending (const struct GNUNET_MESSENGER_Message *message)
{
diff --git a/src/util/crypto_ecc.c b/src/util/crypto_ecc.c
index b45ec1d57..9dd9883dc 100644
--- a/src/util/crypto_ecc.c
+++ b/src/util/crypto_ecc.c
@@ -803,18 +803,41 @@ GNUNET_CRYPTO_eddsa_kem_encaps (const struct
GNUNET_CRYPTO_EddsaPublicKey *pub,
}
-/**
- * This implementation is not testes/publicly exposed yet
- */
-struct GNUNET_CRYPTO_FoKemC
+enum GNUNET_GenericReturnValue
+GNUNET_CRYPTO_ecdsa_fo_kem_encaps (const struct
+ GNUNET_CRYPTO_EcdsaPublicKey *pub,
+ struct GNUNET_CRYPTO_FoKemC *c,
+ struct GNUNET_HashCode *key_material)
{
+ struct GNUNET_HashCode x;
+ struct GNUNET_HashCode ux;
+ struct GNUNET_HashCode w;
struct GNUNET_HashCode y;
- struct GNUNET_CRYPTO_EcdhePublicKey pub;
-};
+ struct GNUNET_CRYPTO_EcdhePrivateKey sk;
+ enum GNUNET_GenericReturnValue ret;
+
+ // This is the input to the FO OWTF
+ GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE, &x, sizeof(x));
+
+ // We build our OWTF using a FO-transformation of ElGamal:
+ // U(x)
+ GNUNET_CRYPTO_hash (&x, sizeof (x), &ux);
+ GNUNET_memcpy (&sk, &ux, sizeof (sk));
+
+ // B := g^U(x)
+ GNUNET_CRYPTO_ecdhe_key_get_public (&sk, &c->pub);
+
+ if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdh_ecdsa (&sk, pub, &w))
+ return -1;
+ // w xor x (one-time pad)
+ GNUNET_CRYPTO_hash_xor (&w, &x, &c->y);
+
+ // k := H(x) FIXME: U and H must be different?
+ GNUNET_memcpy (key_material, &ux, sizeof (ux));
+ return GNUNET_OK;
+}
+
-/**
- * This implementation is not testes/publicly exposed yet
- */
enum GNUNET_GenericReturnValue
GNUNET_CRYPTO_eddsa_fo_kem_encaps (const struct
GNUNET_CRYPTO_EddsaPublicKey *pub,
@@ -851,28 +874,20 @@ GNUNET_CRYPTO_eddsa_fo_kem_encaps (const struct
}
-/**
- * This implementation is not testes/publicly exposed yet
- */
-enum GNUNET_GenericReturnValue
-GNUNET_CRYPTO_eddsa_fo_kem_decaps (const struct
- GNUNET_CRYPTO_EddsaPrivateKey *priv,
- struct GNUNET_CRYPTO_FoKemC *c,
- struct GNUNET_HashCode *key_material)
+static enum GNUNET_GenericReturnValue
+fo_kem_decaps (const struct GNUNET_HashCode *w,
+ const struct GNUNET_CRYPTO_FoKemC *c,
+ struct GNUNET_HashCode *key_material)
{
struct GNUNET_HashCode x;
struct GNUNET_HashCode ux;
- struct GNUNET_HashCode w;
struct GNUNET_HashCode y;
struct GNUNET_CRYPTO_EcdhePrivateKey sk;
struct GNUNET_CRYPTO_EcdhePublicKey pub_test;
enum GNUNET_GenericReturnValue ret;
- ret = GNUNET_CRYPTO_eddsa_ecdh (priv, &c->pub, &w);
- if (GNUNET_OK != ret)
- return ret;
// w xor x (one-time pad)
- GNUNET_CRYPTO_hash_xor (&w, &c->y, &x);
+ GNUNET_CRYPTO_hash_xor (w, &c->y, &x);
// We build our OWTF using a FO-transformation of ElGamal:
// U(x)
@@ -891,6 +906,51 @@ GNUNET_CRYPTO_eddsa_fo_kem_decaps (const struct
}
+/**
+ * This implementation is not testes/publicly exposed yet
+ */
+enum GNUNET_GenericReturnValue
+GNUNET_CRYPTO_eddsa_fo_kem_decaps (const struct
+ GNUNET_CRYPTO_EddsaPrivateKey *priv,
+ const struct GNUNET_CRYPTO_FoKemC *c,
+ struct GNUNET_HashCode *key_material)
+{
+ struct GNUNET_HashCode x;
+ struct GNUNET_HashCode ux;
+ struct GNUNET_HashCode w;
+ struct GNUNET_HashCode y;
+ struct GNUNET_CRYPTO_EcdhePrivateKey sk;
+ struct GNUNET_CRYPTO_EcdhePublicKey pub_test;
+ enum GNUNET_GenericReturnValue ret;
+
+ ret = GNUNET_CRYPTO_eddsa_ecdh (priv, &c->pub, &w);
+ if (GNUNET_OK != ret)
+ return ret;
+ return fo_kem_decaps (&w, c, key_material);
+}
+
+
+enum GNUNET_GenericReturnValue
+GNUNET_CRYPTO_ecdsa_fo_kem_decaps (const struct
+ GNUNET_CRYPTO_EcdsaPrivateKey *priv,
+ struct GNUNET_CRYPTO_FoKemC *c,
+ struct GNUNET_HashCode *key_material)
+{
+ struct GNUNET_HashCode x;
+ struct GNUNET_HashCode ux;
+ struct GNUNET_HashCode w;
+ struct GNUNET_HashCode y;
+ struct GNUNET_CRYPTO_EcdhePrivateKey sk;
+ struct GNUNET_CRYPTO_EcdhePublicKey pub_test;
+ enum GNUNET_GenericReturnValue ret;
+
+ ret = GNUNET_CRYPTO_ecdsa_ecdh (priv, &c->pub, &w);
+ if (GNUNET_OK != ret)
+ return ret;
+ return fo_kem_decaps (&w, c, key_material);
+}
+
+
enum GNUNET_GenericReturnValue
GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnunet] branch master updated: NEWS: Added new CCA-secure KEM and use in IDENTITY encryption,
gnunet <=