check
[oweals/gnunet.git] / src / include / gnunet_crypto_lib.h
index d31dfcfa69fe048f0d687fb7549cf76fdc90682e..8836645344d077bb583ecfd52df8b5299603d66f 100644 (file)
@@ -57,7 +57,12 @@ enum GNUNET_CRYPTO_Quality
   /**
    * High-quality operations are desired.
    */
-  GNUNET_CRYPTO_QUALITY_STRONG
+  GNUNET_CRYPTO_QUALITY_STRONG,
+
+  /**
+   * Randomness for IVs etc. is required.
+   */
+  GNUNET_CRYPTO_QUALITY_NONCE
 };
 
 
@@ -86,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.
  */
@@ -201,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 ************* */
 
 
@@ -257,7 +276,6 @@ unsigned int *GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode,
 void GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey
                                            *key);
 
-
 /**
  * Check that a new session key is well-formed.
  *
@@ -305,6 +323,36 @@ ssize_t GNUNET_CRYPTO_aes_decrypt (const void *block,
                                   void *result);
 
 
+/**
+ * @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
@@ -362,7 +410,7 @@ void GNUNET_CRYPTO_hash (const void *block,
  * @param hmac where to store the hmac
  */
 void 
-GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AesSessionKey *key,
+GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
                    const void *plaintext,
                    size_t plaintext_len,
                    GNUNET_HashCode *hmac);
@@ -388,7 +436,6 @@ struct GNUNET_CRYPTO_FileHashContext;
 /**
  * Compute the hash of an entire file.
  *
- * @param sched scheduler to use
  * @param priority scheduling priority to use
  * @param filename name of file to hash
  * @param blocksize number of bytes to process in one task
@@ -397,8 +444,7 @@ struct GNUNET_CRYPTO_FileHashContext;
  * @return NULL on (immediate) errror
  */
 struct GNUNET_CRYPTO_FileHashContext *
-GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
-                        enum GNUNET_SCHEDULER_Priority priority,
+GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
                         const char *filename,
                         size_t blocksize,
                         GNUNET_CRYPTO_HashCompletedCallback callback,
@@ -484,6 +530,20 @@ 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
@@ -511,6 +571,117 @@ 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.
  *
@@ -518,16 +689,23 @@ int GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
  */
 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.
  *