check
[oweals/gnunet.git] / src / include / gnunet_crypto_lib.h
index 361d244e22fca6c7756953dec5e2fe5dcdd9829f..8836645344d077bb583ecfd52df8b5299603d66f 100644 (file)
@@ -43,11 +43,26 @@ extern "C"
 #include "gnunet_common.h"
 #include "gnunet_scheduler_lib.h"
 
-
+/**
+ * Desired quality level for cryptographic operations.
+ */
 enum GNUNET_CRYPTO_Quality
 {
+  /**
+   * No good quality of the operation is needed (i.e.,
+   * random numbers can be pseudo-random).
+   */
   GNUNET_CRYPTO_QUALITY_WEAK,
-  GNUNET_CRYPTO_QUALITY_STRONG
+
+  /**
+   * High-quality operations are desired.
+   */
+  GNUNET_CRYPTO_QUALITY_STRONG,
+
+  /**
+   * Randomness for IVs etc. is required.
+   */
+  GNUNET_CRYPTO_QUALITY_NONCE
 };
 
 
@@ -76,6 +91,11 @@ enum GNUNET_CRYPTO_Quality
 #define GNUNET_CRYPTO_RSA_KEY_LENGTH 258
 
 
+/**
+ * Length of a hash value
+ */
+#define GNUNET_CRYPTO_HASH_LENGTH 512/8
+
 /**
  * The private information of an RSA key pair.
  */
@@ -191,6 +211,15 @@ struct GNUNET_CRYPTO_AesInitializationVector
 };
 
 
+/**
+ * @brief type for (message) authentication keys
+ */
+struct GNUNET_CRYPTO_AuthKey
+{
+  unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
+};
+
+
 /* **************** Functions and Macros ************* */
 
 
@@ -202,25 +231,30 @@ struct GNUNET_CRYPTO_AesInitializationVector
  * @param len the length of the buffer in bytes
  * @return the resulting CRC32 checksum
  */
-int GNUNET_CRYPTO_crc32_n (const void *buf, unsigned int len);
+int32_t GNUNET_CRYPTO_crc32_n (const void *buf, 
+                              size_t len);
 
 
 /**
  * Produce a random value.
  *
+ * @param mode desired quality of the random number
  * @param i the upper limit (exclusive) for the random number
- * @return a random value in the interval [0,i[.
+ * @return a random value in the interval [0,i) (exclusive).
  */
-unsigned int GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality,
-                                       unsigned int i);
+uint32_t GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode,
+                                  uint32_t i);
 
 
 /**
- * Random on unsigned 64-bit values.  We break them down into signed
- * 32-bit values and reassemble the 64-bit random value bit-wise.
+ * Random on unsigned 64-bit values. 
+ *
+ * @param mode desired quality of the random number
+ * @param max value returned will be in range [0,max) (exclusive)
+ * @return random 64-bit number
  */
-unsigned long long GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
-                                             unsigned long long u);
+uint64_t GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
+                                  uint64_t max);
 
 
 /**
@@ -236,14 +270,16 @@ unsigned int *GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
 
 /**
  * Create a new Session key.
+ *
+ * @param key key to initialize
  */
 void GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey
                                            *key);
 
-
 /**
  * Check that a new session key is well-formed.
  *
+ * @param key key to check
  * @return GNUNET_OK if the key is valid
  */
 int GNUNET_CRYPTO_aes_check_session_key (const struct
@@ -259,39 +295,69 @@ int GNUNET_CRYPTO_aes_check_session_key (const struct
  * @param sessionkey the key used to encrypt
  * @param iv the initialization vector to use, use INITVALUE
  *        for streams.
- * @returns the size of the encrypted block, -1 for errors
+ * @return the size of the encrypted block, -1 for errors
  */
-int GNUNET_CRYPTO_aes_encrypt (const void *block,
-                               uint16_t len,
-                               const struct GNUNET_CRYPTO_AesSessionKey
-                               *sessionkey,
-                               const struct
-                               GNUNET_CRYPTO_AesInitializationVector *iv,
-                               void *result);
+ssize_t GNUNET_CRYPTO_aes_encrypt (const void *block,
+                                  size_t len,
+                                  const struct GNUNET_CRYPTO_AesSessionKey
+                                  *sessionkey,
+                                  const struct
+                                  GNUNET_CRYPTO_AesInitializationVector *iv,
+                                  void *result);
 
 
 /**
  * Decrypt a given block with the sessionkey.
  *
- * @param sessionkey the key used to decrypt
  * @param block the data to decrypt, encoded as returned by encrypt
  * @param size how big is the block?
+ * @param sessionkey the key used to decrypt
  * @param iv the initialization vector to use
  * @param result address to store the result at
  * @return -1 on failure, size of decrypted block on success
  */
-int GNUNET_CRYPTO_aes_decrypt (const struct GNUNET_CRYPTO_AesSessionKey
-                               *sessionkey, const void *block, uint16_t size,
-                               const struct
-                               GNUNET_CRYPTO_AesInitializationVector *iv,
-                               void *result);
+ssize_t GNUNET_CRYPTO_aes_decrypt (const void *block, 
+                                  size_t size,
+                                  const struct GNUNET_CRYPTO_AesSessionKey *sessionkey, 
+                                  const struct GNUNET_CRYPTO_AesInitializationVector *iv,
+                                  void *result);
 
 
 /**
- * Convert GNUNET_CRYPTO_hash to ASCII encoding.
- * @param block the GNUNET_CRYPTO_hash code
+ * @brief Derive an IV
+ * @param iv initialization vector
+ * @param skey session key
+ * @param salt salt for the derivation
+ * @param salt_len size of the salt
+ * @param ... pairs of void * & size_t for context chunks, terminated by NULL
+ */
+void
+GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
+    const struct GNUNET_CRYPTO_AesSessionKey *skey,
+    const void *salt, size_t salt_len,
+    ...);
+
+
+/**
+ * @brief Derive an IV
+ * @param iv initialization vector
+ * @param skey session key
+ * @param salt salt for the derivation
+ * @param salt_len size of the salt
+ * @param argp pairs of void * & size_t for context chunks, terminated by NULL
+ */
+void
+GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
+    const struct GNUNET_CRYPTO_AesSessionKey *skey,
+    const void *salt, size_t salt_len,
+    va_list argp);
+
+
+/**
+ * Convert hash to ASCII encoding.
+ * @param block the hash code
  * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
- *  safely cast to char*, a '\0' termination is set).
+ *  safely cast to char*, a '\\0' termination is set).
  */
 void GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
                                 struct GNUNET_CRYPTO_HashAsciiEncoded
@@ -314,21 +380,42 @@ int GNUNET_CRYPTO_hash_from_string (const char *enc,
  * a.a or a.e (they're used elsewhere), and
  * be somewhat consistent. And of course, the
  * result should be a positive number.
- * @return number between 0 and 65536
+ *
+ * @param a some hash code
+ * @param b some hash code
+ * @return number between 0 and UINT32_MAX
  */
-unsigned int GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
-                                              const GNUNET_HashCode * b);
+uint32_t GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
+                                         const GNUNET_HashCode * b);
 
 
 /**
- * Hash block of given size.
- * @param block the data to GNUNET_CRYPTO_hash, length is given as a second argument
+ * Compute hash of a given block.
+ *
+ * @param block the data to hash
+ * @param size size of the block
  * @param ret pointer to where to write the hashcode
  */
-void GNUNET_CRYPTO_hash (const void *block, unsigned int size,
+void GNUNET_CRYPTO_hash (const void *block, 
+                        size_t size,
                          GNUNET_HashCode * ret);
 
 
+/**
+ * Calculate HMAC of a message (RFC 2104)
+ *
+ * @param key secret key
+ * @param plaintext input plaintext
+ * @param plaintext_len length of plaintext
+ * @param hmac where to store the hmac
+ */
+void 
+GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
+                   const void *plaintext,
+                   size_t plaintext_len,
+                   GNUNET_HashCode *hmac);
+
+
 /**
  * Function called once the hash computation over the
  * specified file has completed.
@@ -341,34 +428,54 @@ typedef void (*GNUNET_CRYPTO_HashCompletedCallback) (void *cls,
                                                      res);
 
 
+/**
+ * Handle to file hashing operation.
+ */
+struct GNUNET_CRYPTO_FileHashContext;
+
 /**
  * Compute the hash of an entire file.
  *
- * @param sched scheduler to use
  * @param priority scheduling priority to use
- * @param run_on_shutdown should we complete even on shutdown?
  * @param filename name of file to hash
  * @param blocksize number of bytes to process in one task
  * @param callback function to call upon completion
  * @param callback_cls closure for callback
+ * @return NULL on (immediate) errror
  */
-void GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
-                              enum GNUNET_SCHEDULER_Priority priority,
-                              int run_on_shutdown,
-                              const char *filename,
-                              size_t blocksize,
-                              GNUNET_CRYPTO_HashCompletedCallback callback,
-                              void *callback_cls);
+struct GNUNET_CRYPTO_FileHashContext *
+GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
+                        const char *filename,
+                        size_t blocksize,
+                        GNUNET_CRYPTO_HashCompletedCallback callback,
+                        void *callback_cls);
+
+
+/**
+ * Cancel a file hashing operation.
+ *
+ * @param fhc operation to cancel (callback must not yet have been invoked)
+ */
+void
+GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
 
 
 /**
  * Create a random hash code.
+ *
+ * @param mode desired quality level
+ * @param result hash code that is randomized
  */
-void GNUNET_CRYPTO_hash_create_random (GNUNET_HashCode * result);
+void GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
+                                      GNUNET_HashCode * result);
 
 
 /**
  * compute result(delta) = b - a
+ *
+ * @param a some hash code
+ * @param b some hash code
+ * @param result set to b - a 
  */
 void GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
                                     const GNUNET_HashCode * b,
@@ -377,6 +484,10 @@ void GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
 
 /**
  * compute result(b) = a + delta
+ *
+ * @param a some hash code
+ * @param delta some hash code
+ * @param result set to a + delta
  */
 void GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
                              const GNUNET_HashCode * delta,
@@ -385,6 +496,10 @@ void GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
 
 /**
  * compute result = a ^ b
+ *
+ * @param a some hash code
+ * @param b some hash code
+ * @param result set to a ^ b 
  */
 void GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a,
                              const GNUNET_HashCode * b,
@@ -393,8 +508,12 @@ void GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a,
 
 /**
  * Convert a hashcode into a key.
+ *
+ * @param hc hash code that serves to generate the key
+ * @param skey set to a valid session key
+ * @param iv set to a valid initialization vector
  */
-void GNUNET_CRYPTO_hash_to_AES_key (const GNUNET_HashCode * hc,
+void GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
                                     struct GNUNET_CRYPTO_AesSessionKey *skey,
                                     struct
                                     GNUNET_CRYPTO_AesInitializationVector
@@ -403,6 +522,7 @@ void GNUNET_CRYPTO_hash_to_AES_key (const GNUNET_HashCode * hc,
 
 /**
  * Obtain a bit from a hashcode.
+ *
  * @param code the GNUNET_CRYPTO_hash to index bit-wise
  * @param bit index into the hashcode, [0...159]
  * @return Bit \a bit from hashcode \a code, -1 for invalid index
@@ -410,10 +530,27 @@ void GNUNET_CRYPTO_hash_to_AES_key (const GNUNET_HashCode * hc,
 int GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code,
                                 unsigned int bit);
 
+/**
+ * Determine how many low order bits match in two
+ * GNUNET_HashCodes.  i.e. - 010011 and 011111 share
+ * the first two lowest order bits, and therefore the
+ * return value is two (NOT XOR distance, nor how many
+ * bits match absolutely!).
+ *
+ * @param first the first hashcode
+ * @param second the hashcode to compare first to
+ *
+ * @return the number of bits that match
+ */
+unsigned int GNUNET_CRYPTO_hash_matching_bits(const GNUNET_HashCode *first, const GNUNET_HashCode *second);
+
 
 /**
  * Compare function for HashCodes, producing a total ordering
  * of all hashcodes.
+ *
+ * @param h1 some hash code
+ * @param h2 some hash code
  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
  */
 int GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1,
@@ -423,6 +560,10 @@ int GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1,
 /**
  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
  * in the XOR metric (Kademlia).
+ *
+ * @param h1 some hash code
+ * @param h2 some hash code
+ * @param target some hash code
  * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
  */
 int GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
@@ -430,24 +571,145 @@ int GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
                                const GNUNET_HashCode * target);
 
 
+/**
+ * @brief Derive an authentication key
+ * @param key authentication key
+ * @param rkey root key
+ * @param salt salt
+ * @param salt_len size of the salt
+ * @param argp pair of void * & size_t for context chunks, terminated by NULL
+ */
+void
+GNUNET_CRYPTO_hmac_derive_key_v(struct GNUNET_CRYPTO_AuthKey *key,
+                                const struct GNUNET_CRYPTO_AesSessionKey *rkey,
+                                const void *salt,
+                                size_t salt_len,
+                               va_list argp);
+
+
+/**
+ * @brief Derive an authentication key
+ * @param key authentication key
+ * @param rkey root key
+ * @param salt salt
+ * @param salt_len size of the salt
+ * @param ... pair of void * & size_t for context chunks, terminated by NULL
+ */
+void
+GNUNET_CRYPTO_hmac_derive_key(struct GNUNET_CRYPTO_AuthKey *key,
+                              const struct GNUNET_CRYPTO_AesSessionKey *rkey,
+                              const void *salt,
+                              size_t salt_len,
+                              ...);
+
+/**
+ * @brief Derive key
+ * @param result buffer for the derived key, allocated by caller
+ * @param out_len desired length of the derived key
+ * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
+ * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
+ * @param xts salt
+ * @param xts_len length of xts
+ * @param skm source key material
+ * @param skm_len length of skm
+ * @return GNUNET_YES on success
+ */
+int
+GNUNET_CRYPTO_hkdf (void *result, 
+                   size_t out_len,
+                   int xtr_algo, int prf_algo, 
+                   const void *xts, size_t xts_len,
+                   const void *skm, size_t skm_len, 
+                   ...);
+
+
+/**
+ * @brief Derive key
+ * @param result buffer for the derived key, allocated by caller
+ * @param out_len desired length of the derived key
+ * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
+ * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
+ * @param xts salt
+ * @param xts_len length of xts
+ * @param skm source key material
+ * @param skm_len length of skm
+ * @param argp va_list of void * & size_t pairs for context chunks
+ * @return GNUNET_YES on success
+ */
+int
+GNUNET_CRYPTO_hkdf_v (void *result, 
+                     size_t out_len,
+                     int xtr_algo, 
+                     int prf_algo, 
+                     const void *xts, size_t xts_len,
+                     const void *skm, size_t skm_len, 
+                     va_list argp);
+
+
+/**
+ * @brief Derive key
+ * @param result buffer for the derived key, allocated by caller
+ * @param out_len desired length of the derived key
+ * @param xts salt
+ * @param xts_len length of xts
+ * @param skm source key material
+ * @param skm_len length of skm
+ * @param argp va_list of void * & size_t pairs for context chunks
+ * @return GNUNET_YES on success
+ */
+int
+GNUNET_CRYPTO_kdf_v (void *result, 
+                    size_t out_len,
+                    const void *xts, size_t xts_len, 
+                    const void *skm, size_t skm_len, 
+                    va_list argp);
+
+
+/**
+ * @brief Derive key
+ * @param result buffer for the derived key, allocated by caller
+ * @param out_len desired length of the derived key
+ * @param xts salt
+ * @param xts_len length of xts
+ * @param skm source key material
+ * @param skm_len length of skm
+ * @param ... void * & size_t pairs for context chunks
+ * @return GNUNET_YES on success
+ */
+int
+GNUNET_CRYPTO_kdf (void *result, size_t out_len,
+    const void *xts, size_t xts_len, const void *skm,
+    size_t skm_len, ...);
+
+
 /**
  * Create a new private key. Caller must free return value.
+ *
+ * @return fresh private key
  */
 struct GNUNET_CRYPTO_RsaPrivateKey *GNUNET_CRYPTO_rsa_key_create (void);
 
+/**
+ * Decode the private key from the data-format back
+ * to the "normal", internal format.
+ *
+ * @param buf the buffer where the private key data is stored
+ * @param len the length of the data in 'buffer'
+ */
+struct GNUNET_CRYPTO_RsaPrivateKey *
+GNUNET_CRYPTO_rsa_decode_key (const char *buf, uint16_t len);
 
 /**
  * Create a new private key by reading it from a file.  If the
  * files does not exist, create a new key and write it to the
  * file.  Caller must free return value. Note that this function
  * can not guarantee that another process might not be trying
- * the same operation on the same file at the same time.  The
- * caller must somehow know that the file either already exists
- * with a valid key OR be sure that no other process is calling
- * this function at the same time.  If the contents of the file
+ * the same operation on the same file at the same time.  
+ * If the contents of the file
  * are invalid the old file is deleted and a fresh key is
  * created.
  *
+ * @param filename name of file to use for storage
  * @return new private key, NULL on error (for example,
  *   permission denied)
  */
@@ -458,9 +720,12 @@ struct GNUNET_CRYPTO_RsaPrivateKey
 /**
  * Deterministically (!) create a private key using only the
  * given HashCode as input to the PRNG.
+ *
+ * @param hc "random" input to PRNG
+ * @return some private key purely dependent on input
  */
 struct GNUNET_CRYPTO_RsaPrivateKey
-  *GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * input);
+  *GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * hc);
 
 
 /**
@@ -472,13 +737,15 @@ void GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey);
 
 /**
  * Extract the public key of the host.
- * @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,
+                                       GNUNET_CRYPTO_RsaPrivateKey *priv,
                                        struct
                                        GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
-                                       *result);
+                                       *pub);
 
 
 /**
@@ -489,10 +756,10 @@ void GNUNET_CRYPTO_rsa_key_get_public (const struct
  * @param size the size of block
  * @param publicKey the encoded public key used to encrypt
  * @param target where to store the encrypted block
- * @returns GNUNET_SYSERR on error, GNUNET_OK if ok
+ * @return GNUNET_SYSERR on error, GNUNET_OK if ok
  */
 int GNUNET_CRYPTO_rsa_encrypt (const void *block,
-                               uint16_t size,
+                               size_t size,
                                const struct
                                GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
                                *publicKey,
@@ -505,12 +772,14 @@ int GNUNET_CRYPTO_rsa_encrypt (const void *block,
  * @param key the key to use
  * @param block the data to decrypt, encoded as returned by encrypt, not consumed
  * @param result pointer to a location where the result can be stored
- * @param size how many bytes of a result are expected? Must be exact.
- * @returns the size of the decrypted block (that is, size) or -1 on error
+ * @param max how many bytes of a result are expected? Must be exact.
+ * @return the size of the decrypted block (that is, size) or -1 on error
  */
-int GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
-                               const struct GNUNET_CRYPTO_RsaEncryptedData
-                               *block, void *result, uint16_t size);
+ssize_t GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
+                                  const struct GNUNET_CRYPTO_RsaEncryptedData
+                                  *block,
+                                  void *result, 
+                                  size_t max);
 
 
 /**
@@ -518,13 +787,13 @@ int GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
  *
  * @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 *key,
                             const struct GNUNET_CRYPTO_RsaSignaturePurpose
                             *purpose,
-                            struct GNUNET_CRYPTO_RsaSignature *result);
+                            struct GNUNET_CRYPTO_RsaSignature *sig);
 
 
 /**
@@ -535,7 +804,7 @@ int GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
  * @param validate block to validate (size, purpose, data)
  * @param sig signature that is being validated
  * @param publicKey public key of the signer
- * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
+ * @return GNUNET_OK if ok, GNUNET_SYSERR if invalid
  */
 int GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
                               const struct GNUNET_CRYPTO_RsaSignaturePurpose
@@ -550,7 +819,7 @@ int GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
 /**
  * This function should only be called in testcases
  * where strong entropy gathering is not desired
- * (for example, for hostkey generation).
+ * (for example, for hostkey generation). 
  */
 void GNUNET_CRYPTO_random_disable_entropy_gathering (void);