exactly wrong
[oweals/gnunet.git] / src / util / crypto_rsa.c
index 3ac910eb061c7dd407f6780c717335c2cc7d55cb..cbd9f8f372bc963940dbcca4385ce712914b3cf6 100644 (file)
@@ -186,43 +186,43 @@ key_from_sexp (gcry_mpi_t * array,
 
 /**
  * Extract the public key of the host.
- * @param hostkey the hostkey to extract into the result.
- * @param result where to write the result.
+ * @param priv the private key
+ * @param pub where to write the public key
  */
 void
 GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
-                                  *hostkey,
+                                  *priv,
                                   struct
                                   GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
-                                  *result)
+                                  *pub)
 {
   gcry_mpi_t skey[2];
   size_t size;
   int rc;
 
-  rc = key_from_sexp (skey, hostkey->sexp, "public-key", "ne");
+  rc = key_from_sexp (skey, priv->sexp, "public-key", "ne");
   if (rc)
-    rc = key_from_sexp (skey, hostkey->sexp, "private-key", "ne");
+    rc = key_from_sexp (skey, priv->sexp, "private-key", "ne");
   if (rc)
-    rc = key_from_sexp (skey, hostkey->sexp, "rsa", "ne");
+    rc = key_from_sexp (skey, priv->sexp, "rsa", "ne");
   GNUNET_assert (0 == rc);
-  result->len =
+  pub->len =
     htons (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) -
-           sizeof (result->padding));
-  result->sizen = htons (GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
-  result->padding = 0;
+           sizeof (pub->padding));
+  pub->sizen = htons (GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
+  pub->padding = 0;
   size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
   GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
-                                      &result->key[0], size, &size, skey[0]));
-  adjust (&result->key[0], size, GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
+                                      &pub->key[0], size, &size, skey[0]));
+  adjust (&pub->key[0], size, GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
   size =
     GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
   GNUNET_assert (0 ==
                  gcry_mpi_print (GCRYMPI_FMT_USG,
-                                 &result->key
+                                 &pub->key
                                  [GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH],
                                  size, &size, skey[1]));
-  adjust (&result->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], size,
+  adjust (&pub->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], size,
           GNUNET_CRYPTO_RSA_KEY_LENGTH -
           GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
   gcry_mpi_release (skey[0]);
@@ -615,7 +615,8 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
       return ret;
     }
   /* hostkey file exists already, read it! */
-  fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ);
+  fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
+                             GNUNET_DISK_PERM_NONE);
   if (NULL == fd)
     {
       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
@@ -642,12 +643,12 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
       if (GNUNET_YES != GNUNET_DISK_file_test (filename))
         {
           /* eh, what!? File we opened is now gone!? */
-          GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
+          GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
                                     "stat", filename);
           if (GNUNET_YES != GNUNET_DISK_file_unlock (fd, 0, sizeof (struct RsaPrivateKeyBinaryEncoded)))
             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
                                       "fcntl", filename);
-          GNUNET_assert (0 == GNUNET_DISK_file_close (fd));
+          GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
 
           return NULL;
         }
@@ -749,15 +750,15 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block,
 /**
  * Decrypt a given block with the hostkey.
  *
- * @param hostkey the hostkey with which to decrypt this block
+ * @param key the key with which to decrypt this block
  * @param block the data to decrypt, encoded as returned by encrypt
  * @param result pointer to a location where the result can be stored
  * @param max the maximum number of bits to store for the result, if
  *        the decrypted block is bigger, an error is returned
- * @returns the size of the decrypted block, -1 on error
+ * @return the size of the decrypted block, -1 on error
  */
 ssize_t
-GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey,
+GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
                            const struct GNUNET_CRYPTO_RsaEncryptedData *block,
                            void *result, 
                           size_t max)
@@ -771,7 +772,7 @@ GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey,
   unsigned char *tmp;
 
 #if EXTRA_CHECKS
-  GNUNET_assert (0 == gcry_pk_testkey (hostkey->sexp));
+  GNUNET_assert (0 == gcry_pk_testkey (key->sexp));
 #endif
   size = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData);
   GNUNET_assert (0 == gcry_mpi_scan (&val,
@@ -781,7 +782,7 @@ GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey,
                  gcry_sexp_build (&data, &erroff,
                                   "(enc-val(flags)(rsa(a %m)))", val));
   gcry_mpi_release (val);
-  GNUNET_assert (0 == gcry_pk_decrypt (&resultsexp, data, hostkey->sexp));
+  GNUNET_assert (0 == gcry_pk_decrypt (&resultsexp, data, key->sexp));
   gcry_sexp_release (data);
   /* resultsexp has format "(value %m)" */
   GNUNET_assert (NULL !=
@@ -804,13 +805,13 @@ GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey,
 /**
  * Sign a given block.
  *
- * @param hostkey private key to use for the signing
+ * @param key private key to use for the signing
  * @param purpose what to sign (size, purpose)
- * @param result where to write the signature
+ * @param sig where to write the signature
  * @return GNUNET_SYSERR on error, GNUNET_OK on success
  */
 int
-GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey,
+GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
                         const struct GNUNET_CRYPTO_RsaSignaturePurpose
                         *purpose, struct GNUNET_CRYPTO_RsaSignature *sig)
 {
@@ -834,7 +835,7 @@ GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey,
            - 1], &hc, sizeof (GNUNET_HashCode));
   GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
   GNUNET_free (buff);
-  GNUNET_assert (0 == gcry_pk_sign (&result, data, hostkey->sexp));
+  GNUNET_assert (0 == gcry_pk_sign (&result, data, key->sexp));
   gcry_sexp_release (data);
   GNUNET_assert (0 == key_from_sexp (&rval, result, "rsa", "s"));
   gcry_sexp_release (result);